crypto/sha256

SHA-256 checksums with Sum256 and hash.Hash, and why they are not a password store, checked on Go 1.24.0.

模块页正文目前为英文。

The crypto/sha256 package implements SHA-224 and SHA-256 as defined in FIPS 180-4. Import it as "crypto/sha256". Examples were checked on Go 1.24.0.

A SHA-256 checksum is a public digest of the input bytes. It is not a password hash: it has no salt, it is fast, and it is not designed to store passwords. It is also not a message authentication code, because it has no key; see crypto/hmac for that. The examples hash only short literals and do not read files or create production keys.

Examples were checked against Go 1.24.0. See the official package documentation for the complete API.

Go reference overview →

API index

APISummary
sha256.Sum256, sha256.New, sha256.SizeCompute a SHA-256 checksum of a byte slice, all at once or incrementally.
sha256.Sum256Two different inputs do not have the same SHA-256 checksum.

Sum256 and hash.Hash

Sum256(data) returns the 32-byte SHA-256 checksum of data as an array. New() returns a hash.Hash that implements the same algorithm incrementally: Write may be called more than once, and Sum(nil) returns the digest. For the same input, the two forms match. sha256.Size is 32 and BlockSize is 64.

sha256.Sum256, sha256.New, sha256.Size

func Sum256(data []byte) [Size]byte
func New() hash.Hash
const Size = 32
const BlockSize = 64

Compute a SHA-256 checksum of a byte slice, all at once or incrementally.

ParameterDescription
dataThe bytes to hash. Sum256 does not modify data. Passing nil or an empty slice hashes the empty input.

Returns: Sum256 returns a [32]byte array. Hash.Sum(nil) returns a 32-byte slice. Size is 32.

  • The digest is shown with encoding/hex. It is a checksum for these exact bytes, not a proof of a file on disk and not a password verifier.
go
package main

import (
	"bytes"
	"crypto/sha256"
	"encoding/hex"
	"fmt"
)

func main() {
	sum := sha256.Sum256([]byte("hello"))
	fmt.Println(hex.EncodeToString(sum[:]))
	h := sha256.New()
	h.Write([]byte("he"))
	h.Write([]byte("llo"))
	fmt.Println(hex.EncodeToString(h.Sum(nil)), sha256.Size, bytes.Equal(sum[:], h.Sum(nil)))
}

Expected output

2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 32 true

Related: crypto/hmac, encoding/hex

Official documentation →

Integrity checks are not password storage

A different input produces a different digest, which is why SHA-256 is used to check that bytes have not changed. That is an integrity check, not authentication (no key) and not password storage (no salt, and the function is too fast for that use). For an HMAC tag under a key, use crypto/hmac. For storing passwords, use a dedicated password-hashing API; this page does not demonstrate one.

sha256.Sum256

func Sum256(data []byte) [Size]byte

Two different inputs do not have the same SHA-256 checksum.

ParameterDescription
dataThe bytes to hash. "hello" and "Hello" are different inputs.

Returns: Each call returns a [32]byte digest. Comparing the two arrays reports they are not equal.

  • Do not treat a raw SHA-256 digest as a stored password. Do not paste production keys or secret files into these examples.
go
package main

import (
	"bytes"
	"crypto/sha256"
	"fmt"
)

func main() {
	hello := sha256.Sum256([]byte("hello"))
	helloCap := sha256.Sum256([]byte("Hello"))
	fmt.Println(bytes.Equal(hello[:], helloCap[:]))
}

Expected output

false

Related: crypto/hmac

Official documentation →