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.

GoData Integrity

Code

gosnippet
33 lines
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

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:`.