Go's standard library ships well-reviewed cryptography packages: crypto/aes and crypto/cipher for authenticated encryption, crypto/sha256 and crypto/hmac for hashing and message authentication, crypto/ed25519 and crypto/rsa for signatures, crypto/rand for secure randomness, and crypto/tls for encrypted transport. The safest approach is to use the high-level, authenticated primitives (AES-GCM, Ed25519, TLS) and avoid inventing your own constructions.
Several patterns in older tutorials are now discouraged: AES in CFB mode (unauthenticated), plain SHA-256 for passwords, and RSA PKCS#1 v1.5 encryption. This guide shows the modern replacements.
crypto/randcrypto/tlscrypto/randKeys, nonces, tokens and salts must come from crypto/rand, never from math/rand. On Go 1.24 and later, rand.Read never returns an error in practice (a failure crashes the program), but checking the error stays harmless and works on older versions.
Go 1.24 also added rand.Text(), which returns a random base32 string suitable for tokens and one-time codes.
AES-GCM provides confidentiality and integrity in one step: if anyone modifies the ciphertext, decryption fails. The older CFB and CBC modes used in many tutorials provide no integrity check, and CFB is deprecated in the standard library. AES keys must be exactly 16, 24 or 32 bytes, so a 15-byte string such as "examplekey12345" makes aes.NewCipher return an error.
Never reuse a nonce with the same key. A fresh random 12-byte nonce per message is safe for a very large number of messages under one key. Store the key in a secret manager or a KMS, not in source code.
crypto/sha256 gives you a fixed-size digest for integrity checks and fingerprints. To prove that a message came from someone who holds a shared secret, use crypto/hmac and compare results in constant time.
Use hmac.Equal rather than bytes.Equal so the comparison time does not leak how many bytes matched. Go 1.24 added crypto/sha3 for SHA-3 and SHAKE if you need them.
A plain SHA-256 hash is too fast for passwords, so attackers can try billions of guesses per second. Use a deliberately slow, salted algorithm. The golang.org/x/crypto module provides bcrypt and argon2; Go 1.24 added crypto/pbkdf2 to the standard library, mainly for compliance cases.
bcrypt generates and stores the salt inside the hash string, so you save one value per user. It only uses the first 72 bytes of a password, so reject or pre-hash longer inputs. For new systems, Argon2id is generally preferred and is available as argon2.IDKey in golang.org/x/crypto/argon2.
Digital signatures prove who created data and that it was not changed. Ed25519 is fast, has small keys and signatures, and has no parameter choices to get wrong, so prefer it over RSA for new designs.
When you must use RSA, use at least 2048-bit keys (3072 is a common choice for new systems), rsa.EncryptOAEP for encryption, and rsa.SignPSS for signatures. Avoid EncryptPKCS1v15, which is deprecated.
crypto/tlsUse TLS for every connection that leaves the process. Go's defaults are already sound: servers accept TLS 1.2 and 1.3 and choose safe cipher suites automatically. Raise the minimum only if every client supports it.
Never set InsecureSkipVerify: true outside tests, because it disables certificate validation and allows man-in-the-middle attacks. For public sites, obtain certificates automatically with golang.org/x/crypto/acme/autocert.
Yes. It is maintained by the Go team, receives regular security fixes, and is audited. Keep your Go toolchain current, because crypto fixes ship in minor releases.
Use AES-GCM (cipher.NewGCM) or ChaCha20-Poly1305 from golang.org/x/crypto. Both are authenticated encryption, so tampering is detected.
No. SHA-256 is fast by design. Use Argon2id or bcrypt, which are slow and salted.
crypto/rand instead of math/rand?math/rand is predictable and not meant for secrets. crypto/rand reads from the operating system's secure random source.
Generate keys and nonces with crypto/rand, encrypt with AES-GCM, authenticate with HMAC, sign with Ed25519, store passwords with Argon2id or bcrypt, and protect traffic with TLS. Prefer authenticated, high-level primitives, keep keys out of source code, and update Go regularly to receive security fixes.