Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Encoding and Data Formats

Go crypto/sha256 and crypto/md5 hashes

Reference for Go hashing: crypto/sha256, crypto/sha512, crypto/md5, the hash.Hash interface, hex output, streaming files, HMAC and constant-time comparison.

A hash function turns any amount of input into a short fixed-size fingerprint. The same input always gives the same digest, and a good hash makes it practically impossible to find two inputs with the same one. Go's crypto/sha256, crypto/sha512, crypto/sha1 and crypto/md5 packages all follow the same pattern.

Each package offers a one-shot function (sha256.Sum256(data)) that returns an array, and a constructor (sha256.New()) that returns a hash.Hash for streaming data in pieces. SHA-256 is the usual choice today. MD5 and SHA-1 are broken for security and are only suitable for checksums and legacy formats.

Go
sum := sha256.Sum256([]byte("hello")) // [32]byte
fmt.Printf("%x\n", sum)

Try it

Go One-shot and streaming digests

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go HMAC and safe comparison

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func sha256.Sum256(data []byte) [32]byte
func sha256.Sum224(data []byte) [28]byte
func sha256.New() hash.Hash
func sha512.Sum512(data []byte) [64]byte
func md5.Sum(data []byte) [16]byte
func md5.New() hash.Hash

type hash.Hash interface {
	io.Writer
	Sum(b []byte) []byte
	Reset()
	Size() int
	BlockSize() int
}

func hmac.New(h func() hash.Hash, key []byte) hash.Hash
func hmac.Equal(mac1, mac2 []byte) bool
Name Description
data The bytes to hash. Convert strings with []byte(s).
b (in Sum) A slice to append the digest to, usually nil. It does not hash b.
h (in hmac.New) A constructor such as sha256.New, not a call.

Returns: the Sum functions return a fixed-size array; hash.Hash.Sum returns a slice with the digest appended and does not change the state, so you can keep writing afterwards.

Edge cases:

  • Write on a hash.Hash never returns an error.
  • Sum256 returns an array (a value), so slice it with sum[:] to get a []byte.
  • New values are not safe for concurrent use. Create one per goroutine.

Usage notes

  • Passwords: never store a plain SHA-256 or MD5 of a password. Fast hashes make guessing cheap. Use a slow password hash such as bcrypt, scrypt or Argon2 from golang.org/x/crypto.
  • MD5 and SHA-1 are fine for cache keys, deduplication and checking against accidental corruption. Do not use them for signatures, certificates or anything an attacker can influence.
  • Authentication: to check that a message came from someone with a shared key, use HMAC, not sha256(key + message). The plain construction is vulnerable to length-extension attacks.
  • Compare digests with hmac.Equal (or crypto/subtle.ConstantTimeCompare) rather than == on strings or bytes.Equal, to avoid timing leaks.
  • Hex output: fmt.Sprintf("%x", sum) and hex.EncodeToString(sum[:]) give the same text. Use %X for upper case.
  • Large files: copy a file into h with io.Copy; memory use stays constant.
  • Other hashes: hash/fnv, hash/crc32 and hash/maphash are fast, non-cryptographic hashes for hash tables and checksums. crypto/sha3 has been in the standard library since Go 1.24.