Developer Documentation
Go
Updated September 19, 2026

Cryptographic Hashing in Go

Compute secure SHA-256 digests and encode them as hexadecimal or Base64 in Go.

Cryptographic hash functions turn arbitrary input into a fixed-size deterministic digest. SHA-256 is a sensible default for checksums, content fingerprints, and integrity verification.

Quick Start#

The following example uses the standard Go API or a widely used package for this task.

Quick start#

Go
1package main23import (4    "crypto/sha256"5    "fmt"6)78func main() {9    sum := sha256.Sum256([]byte("ToolPorta"))10    fmt.Printf("%x\n", sum)11}

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Hash a stream#

Go
1f, err := os.Open("archive.zip")2if err != nil { return err }3defer f.Close()45h := sha256.New()6if _, err := io.Copy(h, f); err != nil { return err }7checksum := hex.EncodeToString(h.Sum(nil))

Important Notes#

Use sha256.Sum256 for data already in memory and sha256.New with io.Copy for large streams.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Hashing in other languages: