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.
Code
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
bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Webhook HMAC Signature VerifierVerify and generate HMAC-SHA256 webhook signatures for Stripe, GitHub, and raw hex headers locally using Web Crypto and constant-time comparison.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.