Go's standard library provides the building blocks for security: crypto/sha256 for hashing, crypto/hmac for message authentication, crypto/cipher with AES-GCM for authenticated encryption, crypto/rand for secure randomness, crypto/tls for transport security and crypto/x509 for certificates. Use authenticated modes such as GCM, never MD5 or SHA-1 for security, and never math/rand for secrets.
The examples below replace common outdated patterns, such as AES-CFB without authentication and plain SHA-256 for passwords, with current practice.
| Goal | Use | Avoid |
|---|---|---|
| Integrity checksum | crypto/sha256, crypto/sha512, crypto/sha3 | MD5, SHA-1 |
| Message authentication | crypto/hmac with SHA-256 | Plain hash with a secret appended |
| Encrypt data | AES-GCM or ChaCha20-Poly1305 | ECB, unauthenticated CFB or CBC |
| Random keys and tokens | crypto/rand | math/rand, math/rand/v2 |
| Password storage | golang.org/x/crypto/argon2, bcrypt or scrypt | SHA-256, MD5 |
| Signatures | Ed25519 (crypto/ed25519), ECDSA | RSA below 2048 bits |
| Transport | crypto/tls (TLS 1.2 and 1.3) | Disabling verification |
crypto/sha3 and crypto/hkdf joined the standard library in Go 1.24. Argon2, bcrypt and scrypt live in the official golang.org/x/crypto module, not in the standard library.
A hash detects accidental change. It does not prove who produced the data, because anyone can recompute it. Use HMAC when both sides share a secret key.
Always compare tags with hmac.Equal, which runs in constant time, instead of == or bytes.Equal.
GCM encrypts and authenticates in one step, so tampered ciphertext fails to decrypt. The key must be 16, 24 or 32 bytes (AES-128, AES-192, AES-256). The nonce must be unique for every message under the same key.
The earlier common pattern, cipher.NewCFBEncrypter, is deprecated since Go 1.24 because it provides no authentication. Never reuse a nonce with the same key, and keep keys out of source code: load them from a secrets manager or environment.
crypto/rand reads from the operating system's secure source. Since Go 1.24, rand.Text() returns a random base32 string suitable for tokens, and rand.Read no longer returns errors in practice.
Fast hashes such as SHA-256 are the wrong tool for passwords, because attackers can test billions of guesses per second. Use a deliberately slow, salted algorithm from golang.org/x/crypto.
bcrypt generates and embeds the salt for you. Argon2id (argon2.IDKey) is the current recommendation for new designs when you can tune memory cost. Since Go 1.24 the standard library also has crypto/pbkdf2, but it needs high iteration counts to be safe.
Ed25519 offers fast signatures with small keys and no parameter choices:
For network traffic, configure TLS explicitly and keep certificate verification on:
Go negotiates TLS 1.3 when both sides support it. Do not set InsecureSkipVerify: true outside local tests.
Use SHA-256 or SHA-512 for integrity checks. MD5 and SHA-1 are broken for security purposes and are only acceptable for non-security checksums.
No. Use bcrypt, scrypt or Argon2id from golang.org/x/crypto, which are slow and salted by design.
Reusing a nonce under the same key lets attackers recover plaintext relationships and forge messages. Generate a fresh random nonce per message, or use a counter.
No. Its output is predictable. Use crypto/rand for tokens, keys, salts and nonces.
Hash with SHA-256 or SHA-3, authenticate with HMAC, encrypt with AES-GCM or ChaCha20-Poly1305, and generate secrets with crypto/rand. Store passwords with bcrypt or Argon2id, sign with Ed25519, and run TLS with certificate verification on. Avoid MD5, SHA-1 and unauthenticated cipher modes.