SHA256 Digest For Strings And Bytes

Hash short payloads with sha256.Sum256 and render the digest as hex, or stream larger data through sha256.New with io.Copy.

GoSecurity Boundaries

Code

snippet
package main

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

func main() {
	payload := []byte("deploy manifest v2")

	// Sum256:一次性计算,返回 [32]byte 定长数组
	sum := sha256.Sum256(payload)
	fmt.Println("digest hex:", hex.EncodeToString(sum[:]))
	fmt.Println("digest bytes:", len(sum))

	// 相同输入必得相同摘要;哪怕改动一个字节也完全不同
	other := sha256.Sum256([]byte("deploy manifest v3"))
	fmt.Println("same digest:", sum == other)

	// New + Write:适合分块写入的流式场景,Sum 与之等价
	h := sha256.New()
	h.Write([]byte("deploy "))
	h.Write([]byte("manifest v2"))
	fmt.Println("streamed matches:", hex.EncodeToString(h.Sum(nil)) == hex.EncodeToString(sum[:]))
}

Inside the snippet

Count the function main as the entire surface area of SHA256 Digest For Strings And Bytes.

Putting it to use

The demo output is labeled (digest hex:, digest bytes:, same digest:), so the output doubles as a step-by-step trace of what the file did.

Core lines

other := sha256.Sum256([]byte("deploy manifest v3"))

Interactive Tools for This Topic

FAQ

What does SHA256 Digest For Strings And Bytes print when run?
Invoke the file as-is — the `digest hex:`, `digest bytes:`, `same digest:` prefixes identify which step printed each line.