SHA3 Digests With crypto/sha3
Hash data with SHA3-256 in one shot or incrementally, and read variable-length SHAKE output for key derivation and identifiers.
Code
package main
import (
"crypto/sha3"
"encoding/hex"
"fmt"
)
func main() {
payload := []byte("audit trail entry #42")
// Sum256:一次性算出 32 字节摘要
sum := sha3.Sum256(payload)
fmt.Println("sha3-256:", hex.EncodeToString(sum[:]))
// New256 实现 io.Writer,可分块喂数据
h := sha3.New256()
h.Write(payload[:6])
h.Write(payload[6:])
fmt.Println("incremental:", hex.EncodeToString(h.Sum(nil)))
// SHAKE 是可扩展输出函数:任意长度读取
shake := sha3.NewSHAKE128()
shake.Write(payload)
out := make([]byte, 16)
if _, err := shake.Read(out); err != nil {
fmt.Println("shake read failed:", err)
return
}
fmt.Println("shake128-16:", hex.EncodeToString(out))
fmt.Println("digest length:", sha3.New256().Size())
}Order of operations
Count the function main as the entire surface area of SHA3 Digests With crypto/sha3.
Error handling is inline: 1 place test err != nil and branch immediately.
Putting it to use
Running the file prints labeled results such as sha3-256:, incremental:, shake read failed: — each operation’s effect is visible in the output.
Core lines
out := make([]byte, 16)
Interactive Tools for This Topic
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.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.SHA-256 Hash GeneratorGenerate SHA-256 cryptographic hashes from text input using the Web Crypto API. Compare hashes and verify data integrity with hexadecimal digest output.HMAC GeneratorCompute an HMAC-SHA-256, SHA-384, or SHA-512 signature for a message and key using the Web Crypto API, with hex and Base64 output.
FAQ
- What does SHA3 Digests With crypto/sha3 print when run?
- Run it as a complete program — the output lines carry labels such as `sha3-256:`, `incremental:`, `shake read failed:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.