Go's standard library covers most security needs: crypto/rand for secure randomness, crypto/aes with crypto/cipher (AES-GCM) for encrypting data at rest, crypto/sha256 and crypto/hmac for integrity, and crypto/tls with net/http for encrypted communication. For passwords, use golang.org/x/crypto/bcrypt or argon2, never a plain hash.
The rule that matters most is to use high-level, authenticated primitives (AES-GCM, TLS, signatures) instead of assembling low-level building blocks yourself, and to keep keys out of source code.
Every encryption scheme starts with unpredictable randomness. Use crypto/rand, never math/rand, which is predictable and unsuitable for secrets.
Since Go 1.24, rand.Read is documented never to return an error and crashes the program if the system source fails, but handling the error keeps your code correct on older versions. In production, load the key from a secrets manager or environment variable instead of generating it at startup, otherwise you cannot decrypt data after a restart.
AES in GCM mode provides both confidentiality and integrity: tampered ciphertext fails to decrypt. Use a fresh random nonce for every message and store it with the ciphertext. Avoid older unauthenticated modes such as CFB or CBC without a MAC.
The key length selects the variant: 16, 24 or 32 bytes for AES-128, AES-192 or AES-256. Never reuse a nonce with the same key, because that breaks GCM's security. The last argument to Seal and Open is optional additional authenticated data, useful for binding a record to its ID.
Choose the tool by purpose:
| Goal | Use |
|---|---|
| File or data checksum | crypto/sha256 |
| Verify a message was not altered, with a shared secret | crypto/hmac with SHA-256 |
| Store user passwords | bcrypt or argon2 from golang.org/x/crypto |
| Compare secrets | hmac.Equal or subtle.ConstantTimeCompare |
Fast hashes such as SHA-256 or MD5 are the wrong choice for passwords, because attackers can try billions per second. bcrypt and argon2 are deliberately slow and salted. MD5 and SHA-1 are broken for security purposes and should not be used for signatures or integrity.
Serve and call HTTPS with crypto/tls. Set a minimum protocol version, and let Go choose cipher suites, since its defaults are sensible and kept current.
For public sites, obtain certificates automatically with golang.org/x/crypto/acme/autocert. For service-to-service traffic, configure mutual TLS by setting ClientAuth: tls.RequireAndVerifyClientCert and a ClientCAs pool. On the client side, never set InsecureSkipVerify: true outside of a local test, because it disables certificate validation and allows man-in-the-middle attacks.
Use signatures to prove who produced data, and asymmetric encryption to exchange keys. Ed25519 is a fast, simple default for signatures:
For RSA encryption use OAEP (rsa.EncryptOAEP) with SHA-256, not the older PKCS#1 v1.5 padding, and keys of at least 2048 bits. In practice, encrypt bulk data with AES-GCM and use RSA or an ECDH exchange only to protect that symmetric key.
crypto/rand for anything secret, and authenticated encryption (AES-GCM) for stored data.bcrypt or argon2, and compare secrets in constant time.govulncheck ./... regularly and keep your Go toolchain and dependencies updated.Use AES-GCM through cipher.NewGCM, or ChaCha20-Poly1305 from golang.org/x/crypto/chacha20poly1305. Both are authenticated, so tampering is detected.
No. Use bcrypt or argon2, which are slow by design and include a salt, so brute-force and rainbow-table attacks become impractical.
No. Compose the standard library's high-level primitives, and avoid inventing protocols or modes. Review any custom design with a security specialist.
Run govulncheck ./... from the Go vulnerability tooling. It reports only vulnerabilities in code paths your program actually calls.
Go makes secure code approachable: crypto/rand for randomness, AES-GCM for data at rest, HMAC and signatures for integrity, bcrypt or argon2 for passwords, and crypto/tls for transport. Prefer authenticated, high-level primitives, protect keys, enforce modern TLS and keep dependencies patched with govulncheck.