Go's standard library gives you every cryptographic primitive a simple blockchain needs: hashing with crypto/sha256, digital signatures with crypto/ed25519 or crypto/ecdsa, authenticated encryption with crypto/aes and crypto/cipher, message authentication with crypto/hmac and secure randomness with crypto/rand. Combined with encoding/json and encoding/hex you can build and verify a hash-linked chain of blocks without any third-party dependency.
The standard library is enough to learn and prototype. Production blockchains also need networking, consensus, persistence and audited key management, which are outside the standard library.
crypto/sha256crypto/ed25519crypto/sha256A hash function turns any input into a fixed-size fingerprint. Changing one byte of the input changes the whole hash, which is what makes blocks tamper-evident.
sha256.Sum256 returns a [32]byte array in one call. For large inputs use sha256.New() and write to it in chunks, because it implements io.Writer. SHA-256 is fine for block hashes. Never use it directly to hash passwords, where you need a slow password hashing function.
Each block stores the hash of the previous block, so editing any earlier block breaks every later link.
The mine method is a toy proof of work: it increments Nonce until the hash has the required prefix. Validation recomputes every hash and checks each link, so tampering is detected.
crypto/ed25519Hashes prove integrity. Signatures prove who authorised a transaction. Ed25519 is the simplest modern choice in Go: small keys, fast, and no parameters to get wrong.
The public key is the account identity, the private key signs. If you need compatibility with Bitcoin or Ethereum style keys, crypto/ecdsa supports P-256 and the other NIST curves, but the secp256k1 curve those chains use is not in the standard library and needs an external package.
Blockchains do not encrypt block data by default, but wallets and off-chain storage do. Use an authenticated mode so tampering is detected.
Never reuse a nonce with the same key. Always take randomness from crypto/rand, never from math/rand.
| Need | Package |
|---|---|
| Block and transaction hashes | crypto/sha256, crypto/sha512, crypto/sha3 (Go 1.24+) |
| Signatures | crypto/ed25519, crypto/ecdsa |
| Message authentication | crypto/hmac |
| Encryption | crypto/aes with crypto/cipher (GCM) |
| Secure random numbers | crypto/rand |
| Text encoding of hashes and keys | encoding/hex, encoding/base64 |
| Certificates and TLS for node traffic | crypto/x509, crypto/tls |
You can build a working prototype. A production network also needs peer-to-peer networking, a consensus protocol, durable storage and hardened key management.
Use Ed25519 unless you must interoperate with a system that requires ECDSA. It is simple to use correctly and fast.
math/rand safe for keys and nonces?No. It is predictable. Use crypto/rand for anything security-related.
Not in the standard library. You need a third-party package for that curve.
Go's standard library covers hashing, signatures, encryption and secure randomness, which is everything a teaching or prototype blockchain needs. Link blocks with SHA-256, sign transactions with Ed25519, protect secrets with AES-GCM and take randomness only from crypto/rand. For a production system, add audited key storage and a proven consensus implementation.