SHA256 Hashes
crypto/sha256 for one-shot and streaming SHA-256 hashing, hex encoding the digest, and when not to use SHA256 for passwords.
The crypto/sha256 package computes a fixed 32-byte digest. Use sha256.Sum256 for small byte slices and hex encoding when you need a printable checksum.
The returned digest is an array. Slice it when passing it to functions such as hex.EncodeToString.
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
)
func main() {
digest := sha256.Sum256([]byte("hello"))
fmt.Println(hex.EncodeToString(digest[:]))
}In production
For large input, stream data through sha256.New with io.Copy instead of loading it all into memory. SHA-256 is suitable for checksums and content hashes, but not password storage; use a dedicated password-hashing function such as bcrypt or Argon2id. Use hmac.Equal when verifying message authentication codes.
Enjoyed this? Get more software craft delivered to your inbox.