Go's standard library has a full set of cryptographic packages under crypto/. For encrypting data, use AES in GCM mode through crypto/aes and crypto/cipher, with a random nonce from crypto/rand. For integrity use crypto/hmac with SHA-256, for signatures use crypto/ed25519, and for passwords use a slow hash such as Argon2id or bcrypt rather than a plain hash.
Choosing the right primitive matters more than the code. The sections below show the safe default for each job and the old approaches to avoid.
| Goal | Use | Avoid |
|---|---|---|
| Encrypt and authenticate data | crypto/aes + cipher.NewGCM (AES-GCM), or ChaCha20-Poly1305 from golang.org/x/crypto | CBC or CFB without a MAC, ECB, DES, RC4 |
| Random keys, nonces, tokens | crypto/rand | math/rand |
| Hashing data | crypto/sha256, crypto/sha512, crypto/sha3 (Go 1.24+) | MD5, SHA-1 for security |
| Message authentication | crypto/hmac | Hashing key + message yourself |
| Digital signatures | crypto/ed25519, crypto/ecdsa, RSA-PSS | RSA PKCS#1 v1.5 for new designs |
| Key agreement | crypto/ecdh | Home-made schemes |
| Passwords | Argon2id or bcrypt from golang.org/x/crypto | SHA-256 of the password |
| Network encryption | crypto/tls | Hand-rolled protocols |
AES-GCM is authenticated encryption: it hides the data and detects tampering. You need a 16, 24 or 32 byte key and a unique nonce for every message. Reusing a nonce with the same key breaks the security of GCM.
Open returns an error if the ciphertext, nonce or additional data was modified, so you never process tampered input. The last argument of Seal and Open is optional additional authenticated data, such as a record ID, that is authenticated but not encrypted. A common older example used CBC or CFB mode, which provides no integrity check and should not be used for new code.
Always take keys, salts and tokens from crypto/rand. The math/rand package is predictable and is not safe for secrets. Go 1.24 added rand.Text, which returns a random base32 string suited to tokens.
A hash such as SHA-256 proves nothing about who produced the data. To authenticate a message with a shared secret, use HMAC and compare the results in constant time.
hmac.Equal avoids timing leaks that a plain bytes.Equal can have. For a plain checksum, sha256.Sum256(data) returns a 32 byte array.
Fast hashes such as SHA-256 let an attacker test billions of guesses per second. Use a deliberately slow, salted algorithm. Argon2id is the current recommendation, and bcrypt remains acceptable.
The salt is generated and stored inside the hash string, so you save one value per user. Note that bcrypt only uses the first 72 bytes of the password. To derive an encryption key from a password, use Argon2 or crypto/pbkdf2 (in the standard library since Go 1.24), never a single hash.
Ed25519 is the simplest modern signature scheme: small keys, fast, and no parameters to get wrong.
For RSA, use rsa.EncryptOAEP for encryption and rsa.SignPSS for signatures with keys of at least 2048 bits. RSA encrypts only small payloads, so real systems encrypt the data with AES-GCM and use public-key cryptography to protect or agree on that key. For TLS, use crypto/tls with its defaults, which already select safe versions and cipher suites.
==. Use hmac.Equal or subtle.ConstantTimeCompare.Go 1.24 also added a FIPS 140-3 validated cryptographic module that you can enable with the GOFIPS140 setting if you need compliance, and the standard library follows the Go security policy for fixes, so keep your Go version current.
Use AES-GCM. It authenticates the data as well as encrypting it. CBC needs a separate MAC and is easy to misuse.
Use 32 bytes for AES-256. 16 bytes (AES-128) is also considered secure, but 32 bytes is the common default.
math/rand for tokens?No. It is predictable. Use crypto/rand for anything security related.
Use golang.org/x/crypto/argon2 (Argon2id) or golang.org/x/crypto/bcrypt. Do not use SHA-256 or MD5 for passwords.
Use AES-GCM with a fresh random nonce for encryption, crypto/rand for every secret value, HMAC-SHA-256 for message authentication and Ed25519 for signatures. Hash passwords with Argon2id or bcrypt, keep keys out of source code and let crypto/tls handle network encryption. Avoid MD5, SHA-1, DES and unauthenticated cipher modes.