// MergeKey adds the identities, revocations, and subkeys of another PGPKeyBundle to this key
509968
func (to *PGPKeyBundle) MergeKey(from *PGPKeyBundle) {

	// First, merge identities, adding any signatures found in matching identities
	for name, fromIdentity := range from.Identities {
		if toIdentity, ok := to.Identities[name]; ok {
			to.Identities[name].Signatures = combineSignatures(toIdentity.Signatures, fromIdentity.Signatures)

			// There's a primary self-signature that we use. Always take the later
			// of the two.
			ssTo := to.Identities[name].SelfSignature
			ssFrom := fromIdentity.SelfSignature
			if ssFrom.CreationTime.After(ssTo.CreationTime) {
				to.Identities[name].SelfSignature = ssFrom
			}

		} else {
			to.Identities[fromIdentity.Name] = fromIdentity
		}
	}

	// Then, merge revocations
	to.Revocations = combineSignatures(to.Revocations, from.Revocations)

	// Finally, merge subkeys
	existingSubkeys := make(map[[20]byte]int)
	for i, subkey := range to.Subkeys {
		existingSubkeys[subkey.PublicKey.Fingerprint] = i
	}
	for _, subkey := range from.Subkeys {
		if i, ok := existingSubkeys[subkey.PublicKey.Fingerprint]; ok {
			if subkey.Sig.CreationTime.After(to.Subkeys[i].Sig.CreationTime) {
				to.Subkeys[i].Sig = subkey.Sig
				if subkey.Revocation != nil {
					to.Subkeys[i].Revocation = subkey.Revocation
				}
			}
		} else {
			to.Subkeys = append(to.Subkeys, subkey)
		}
	}
}
38599
func (m *MockAuthorizeRequester) Merge(arg0 fosite.Requester) {
	m.ctrl.Call(m, "Merge", arg0)
}
790841
func (mo *MergeOperatorStringAppend) PartialMerge(key,
	leftOperand, rightOperand []byte) ([]byte, bool) {
	mo.m.Lock()
	mo.numPartial++
	mo.m.Unlock()

	return []byte(string(leftOperand) + mo.Sep + string(rightOperand)), true
}
1239350
func (t *TeamKeyManager) SigningKey() (libkb.NaclSigningKeyPair, error) {
	if t.signingKey == nil {
		key, err := libkb.MakeNaclSigningKeyPairFromSecretBytes(derivedSecret(t.sharedSecret, libkb.TeamEdDSADerivationString))
		if err != nil {
			return libkb.NaclSigningKeyPair{}, err
		}
		t.signingKey = &key
	}
	return *t.signingKey, nil
}
407920
func CryptoPublicKey(publicKey []byte) (crypto.PublicKey, error) {
	// reuse the same RSA keys for SSH and TLS keys
	pubKey, _, _, _, err := ssh.ParseAuthorizedKey(publicKey)
	if err != nil {
		return nil, trace.Wrap(err)
	}
	cryptoPubKey, ok := pubKey.(ssh.CryptoPublicKey)
	if !ok {
		return nil, trace.BadParameter("expected ssh.CryptoPublicKey, got %T", pubKey)
	}
	return cryptoPubKey.CryptoPublicKey(), nil
}
885864
func NewIdentityKey(b []byte) *IdentityKey {
	ensureKeyLength(b)
	k := &IdentityKey{}
	copy(k.key[:], b)
	return k
}
1164379
func MergeKeyValSet(setKeyVals, entryKeyVals []KeyVal) []KeyVal {
	retList := keyValCopy(setKeyVals)
	seenKeywords := []Keyword{}
	for i := range retList {
		word := retList[i].Keyword()
		for _, kv := range HasKeyword(entryKeyVals, word) {
			// match on the keyword prefix and suffix here
			if kv.Keyword() == word {
				retList[i] = kv
			}
		}
		seenKeywords = append(seenKeywords, word)
	}
	for i := range entryKeyVals {
		if !InKeywordSlice(entryKeyVals[i].Keyword(), seenKeywords) {
			retList = append(retList, entryKeyVals[i])
		}
	}
	return retList
}
182087
func (gi globalIdentity) PutKey(v string) (allocator.AllocatorKey, error) {
	b, err := kvstore.Decode(v)
	if err != nil {
		return nil, err
	}
	return globalIdentity{labels.NewLabelArrayFromSortedList(string(b))}, nil
}
759414
func SharedKey(secretKey [32]byte, publicKey [32]byte) ([32]byte, error) {
	var sharedKey [32]byte
	curve25519.ScalarMult(&sharedKey, &secretKey, &publicKey)
	c := byte(0)
	for i := 0; i < 32; i++ {
		c |= sharedKey[i]
	}
	if c == 0 {
		return sharedKey, errors.New("weak public key")
	}
	var nonce [16]byte
	chacha.HChaCha20(&sharedKey, &nonce, &sharedKey)
	return sharedKey, nil
}
231113
func relationKey(endpoints []Endpoint) string {
	eps := epSlice{}
	for _, ep := range endpoints {
		eps = append(eps, ep)
	}
	sort.Sort(eps)
	endpointNames := []string{}
	for _, ep := range eps {
		endpointNames = append(endpointNames, ep.String())
	}
	return strings.Join(endpointNames, " ")
}
548856
func NewDSAPublicKey(creationTime time.Time, pub *dsa.PublicKey) *PublicKey {
	pk := &PublicKey{
		CreationTime: creationTime,
		PubKeyAlgo:   PubKeyAlgoDSA,
		PublicKey:    pub,
		p:            fromBig(pub.P),
		q:            fromBig(pub.Q),
		g:            fromBig(pub.G),
		y:            fromBig(pub.Y),
	}

	pk.setFingerPrintAndKeyId()
	return pk
}
1235711
func FindPGPPrivateKey(k *PGPKeyBundle) bool {
	if k.PrivateKey.PrivateKey != nil {
		return true
	}

	for _, subKey := range k.Subkeys {
		if subKey.PrivateKey != nil && subKey.PrivateKey.PrivateKey != nil {
			return true
		}
	}

	return false
}
574172
func DeepMerge(b, a Metadata) Metadata {
	out := Metadata{}
	for k, v := range b {
		out[k] = v
	}
	for k, v := range a {

		maybe, err := Metadatify(v)
		if err != nil {
			// if the new value is not meta. just overwrite the dest vaue
			if out[k] != nil {
				log.Debugf("Overwriting key: %s, old: %s, new: %s", k, out[k], v)
			}
			out[k] = v
			continue
		}

		// it is meta. What about dest?
		outv, exists := out[k]
		if !exists {
			// the new value is meta, but there's no dest value. just write it
			out[k] = v
			continue
		}

		outMetadataValue, err := Metadatify(outv)
		if err != nil {
			// the new value is meta and there's a dest value, but the dest
			// value isn't meta. just overwrite
			out[k] = v
			continue
		}

		// both are meta. merge them.
		out[k] = DeepMerge(outMetadataValue, maybe)
	}
	return out
}
645943
func createMergeArray(keys, revs []string) ([]map[string]interface{}, error) {
	if keys == nil && revs == nil {
		return nil, nil
	}
	if revs == nil {
		mergeArray := make([]map[string]interface{}, len(keys))
		for i, k := range keys {
			mergeArray[i] = map[string]interface{}{
				"_key": k,
			}
		}
		return mergeArray, nil
	}
	if keys == nil {
		mergeArray := make([]map[string]interface{}, len(revs))
		for i, r := range revs {
			mergeArray[i] = map[string]interface{}{
				"_rev": r,
			}
		}
		return mergeArray, nil
	}
	if len(keys) != len(revs) {
		return nil, WithStack(InvalidArgumentError{Message: fmt.Sprintf("#keys must be equal to #revs, got %d, %d", len(keys), len(revs))})
	}
	mergeArray := make([]map[string]interface{}, len(keys))
	for i, k := range keys {
		mergeArray[i] = map[string]interface{}{
			"_key": k,
			"_rev": revs[i],
		}
	}
	return mergeArray, nil

}
225342
func (m *PostgreSQLBackend) splitKey(fullPath string) (string, string, string) {
	var parentPath string
	var path string

	pieces := strings.Split(fullPath, "/")
	depth := len(pieces)
	key := pieces[depth-1]

	if depth == 1 {
		parentPath = ""
		path = "/"
	} else if depth == 2 {
		parentPath = "/"
		path = "/" + pieces[0] + "/"
	} else {
		parentPath = "/" + strings.Join(pieces[:depth-2], "/") + "/"
		path = "/" + strings.Join(pieces[:depth-1], "/") + "/"
	}

	return parentPath, path, key
}
1354342
func mergeConfigs(cc *config.CloudConfig, md datasource.Metadata) (out config.CloudConfig) {
	if cc != nil {
		out = *cc
	}

	if md.Hostname != "" {
		if out.Hostname != "" {
			log.Printf("Warning: user-data hostname (%s) overrides metadata hostname (%s)\n", out.Hostname, md.Hostname)
		} else {
			out.Hostname = md.Hostname
		}
	}
	for _, key := range md.SSHPublicKeys {
		out.SSHAuthorizedKeys = append(out.SSHAuthorizedKeys, key)
	}
	return
}
368857
func (m *Mapping) key() mappingKey {
	// Normalize addresses to handle address space randomization.
	// Round up to next 4K boundary to avoid minor discrepancies.
	const mapsizeRounding = 0x1000

	size := m.Limit - m.Start
	size = size + mapsizeRounding - 1
	size = size - (size % mapsizeRounding)
	key := mappingKey{
		size:   size,
		offset: m.Offset,
	}

	switch {
	case m.BuildID != "":
		key.buildIDOrFile = m.BuildID
	case m.File != "":
		key.buildIDOrFile = m.File
	default:
		// A mapping containing neither build ID nor file name is a fake mapping. A
		// key with empty buildIDOrFile is used for fake mappings so that they are
		// treated as the same mapping during merging.
	}
	return key
}