sha256.Sum256, sha256.New, sha256.Size
func Sum256(data []byte) [Size]byte
func New() hash.Hash
const Size = 32
const BlockSize = 64Compute a SHA-256 checksum of a byte slice, all at once or incrementally.
| Parameter | Description |
|---|---|
data | The 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.
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 trueRelated: crypto/hmac, encoding/hex