Skip to content

SHA256 hashes

The crypto/sha256 package computes SHA‑256 hashes. Use sha256.Sum256 for a quick hash or sha256.New for streaming.

Hash a string and a file (simulated).

package main
import (
"crypto/sha256"
"fmt"
)
func main() {
data := []byte("hello world")
hash := sha256.Sum256(data)
fmt.Printf("%x\n", hash)
// Using New
h := sha256.New()
h.Write([]byte("hello "))
h.Write([]byte("world"))
fmt.Printf("%x\n", h.Sum(nil))
}
Terminal window
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9