hash

Guided tour · Hashing · pkg.go.dev →

The Hash interface every hash algorithm in stdlib implements. A growable io.Writer that produces a fixed-size digest.

The interface

Hash embeds io.Writer, adds Sum(b []byte) []byte that appends the current digest without resetting, Reset(), Size(), and BlockSize(). Hash32 and Hash64 add Sum32/Sum64 for short hashes.

The pattern, same for every hash

h := sha256.New()            // or md5.New, fnv.New32a, crc32.NewIEEE, etc.
io.Copy(h, someReader)
digest := h.Sum(nil)         // fresh slice with the digest
fmt.Printf("%x\n", digest)