// Hash hashes the data in a chunk into a cas.Key. // // Hash makes sure to never return a Special Key. func Hash(chunk *chunks.Chunk) cas.Key { var pers [blake2.PersonalSize]byte copy(pers[:], personalizationPrefix) copy(pers[len(personalizationPrefix):], chunk.Type) config := &blake2.Config{ Size: cas.KeySize, Personal: pers[:], Tree: &blake2.Tree{ // We are faking tree mode without any intent to actually // follow all the rules, to be able to feed the level // into the hash function. These settings are dubious, but // we need to do something to make having Tree legal. Fanout: 0, MaxDepth: 255, InnerHashSize: cas.KeySize, NodeDepth: chunk.Level, }, } h := blake2.New(config) if len(chunk.Buf) == 0 { return cas.Empty } _, _ = h.Write(chunk.Buf) keybuf := h.Sum(nil) return makeKey(keybuf) }
func (s *Convergent) computeBoxedKey(key []byte) []byte { conf := blake2.Config{ Size: cas.KeySize, Key: s.secret[:], Personal: personalizeKey, } h := blake2.New(&conf) // hash.Hash docs say it never fails _, _ = h.Write(key) return h.Sum(nil) }
// Nonce summarizes key, type and level so mismatch of e.g. type can // be detected. func (s *Convergent) makeNonce(key []byte) *[nonceSize]byte { conf := blake2.Config{ Size: nonceSize, Personal: personalizeNonce, } h := blake2.New(&conf) // hash.Hash docs say it never fails _, _ = h.Write(key) var ret [nonceSize]byte h.Sum(ret[:0]) return &ret }