// getAntiTag returns a digest for the entire header stack. It needs to be run // before a new header is inserted but after deterministic headers are appended // to the bottom of the header stack. func (m *encMessage) getAntiTag() []byte { digest, err := blake2s.New(nil) if err != nil { panic(err) } digest.Write(m.payload[headerBytes:]) return digest.Sum(nil) }
// makeKeyID generates a 16 Byte KeyID based on the hash of a Public Key func makeKeyID(pub []byte) []byte { digest, err := blake2s.New(&blake2s.Config{Size: 16}) if err != nil { panic(err) } digest.Write(pub) return digest.Sum(nil) }
// testAntiTag creates a Blake2 hash of the entire payload (less the top // headerBytes) and compares it with the provided hash. If the two collide, it // returns True. func (m *decMessage) testAntiTag(tag []byte) bool { digest, err := blake2s.New(nil) if err != nil { panic(err) } digest.Write(m.payload[headerBytes:]) if bytes.Compare(tag, digest.Sum(nil)) == 0 { return true } return false }
// Generate 1-byte checksum using blake2s. func (ks *Keys) checksum() ([]byte, error) { var written int blakeHasher, err := blake2s.New(&blake2s.Config{Size: 1}) if err != nil { return nil, err } written, err = blakeHasher.Write(ks.Public) if err != nil { return nil, err } if written != 32 { return nil, ErrChecksumFail } checksum := blakeHasher.Sum(nil) if len(checksum) != 1 { return nil, ErrChecksumFail } return checksum, nil }
// Generate 1-byte checksum using blake2s. func (self *Keys) checksum() ([]byte, error) { var written int blakeHasher, err := blake2s.New(&blake2s.Config{Size: 1}) if err != nil { return nil, err } written, err = blakeHasher.Write(self.Public) if err != nil { return nil, err } if written != 32 { return nil, ChecksumGenerationError } checksum := blakeHasher.Sum(nil) if len(checksum) != 1 { return nil, ChecksumGenerationError } return checksum, nil }
type hashDesc struct { name string maxSize int maker func(size uint8) (hash.Hash, error) } var algorithms = map[string]hashDesc{ "blake2b": { "BLAKE2b", blake2b.Size, func(size uint8) (hash.Hash, error) { return blake2b.New(&blake2b.Config{Size: size}) }, }, "blake2s": { "BLAKE2s", blake2s.Size, func(size uint8) (hash.Hash, error) { return blake2s.New(&blake2s.Config{Size: size}) }, }, } func calcSum(f *os.File, h hash.Hash) (sum []byte, err error) { h.Reset() _, err = io.Copy(h, f) sum = h.Sum(nil) return } func main() { flag.Parse() algo, ok := algorithms[*algoFlag] if !ok {