Go's standard library covers most of what a secure application needs: crypto/* for hashing, encryption and TLS, html/template for context-aware escaping, database/sql for parameterized queries and net/http for hardened servers. Security and privacy in Go come down to using these packages correctly, adding golang.org/x/crypto for password hashing, and keeping dependencies patched.
This guide shows the techniques that matter most, with code you can run, and flags the older patterns (MD5, SHA-1, math/rand for secrets, plain-text passwords) that you should no longer use.
Anything unguessable, such as tokens, session IDs and keys, must come from crypto/rand, never from math/rand. Since Go 1.24, rand.Text() returns a random base32 string suitable for tokens.
32 random bytes give 256 bits of entropy. Compare secrets with crypto/subtle.ConstantTimeCompare so response time does not leak how many bytes matched.
Passwords need a slow, salted algorithm. Use bcrypt, scrypt or Argon2 from golang.org/x/crypto. SHA-256 alone is a fast hash and is wrong for passwords.
bcrypt ignores input beyond 72 bytes, so reject longer passwords or pre-hash them. For integrity checks and fingerprints use crypto/sha256 or crypto/sha3 (added in Go 1.24); treat MD5 and SHA-1 as broken for security purposes.
Use an authenticated mode so tampering is detected. AES-GCM is available in crypto/cipher, and the nonce must never repeat for the same key.
Store keys in a secrets manager or KMS, never in source code or the repository.
The zero-value http.Server has no timeouts, which makes it easy to exhaust. Set them, require modern TLS and cap request bodies.
Prefer tls.VersionTLS13 when all clients support it. Never set InsecureSkipVerify: true outside tests, since it disables certificate validation and enables man-in-the-middle attacks.
| Attack | Standard-library defence |
|---|---|
| SQL injection | Placeholders: db.QueryContext(ctx, "SELECT ... WHERE id = $1", id) (the placeholder syntax depends on the driver) |
| Cross-site scripting | html/template, which escapes by context; never use text/template for HTML |
| Path traversal | os.Root (Go 1.24) or filepath.IsLocal to confine file access |
| Command injection | exec.Command(name, args...) with separate arguments, never a shell string |
Never build SQL by concatenating user input. Do not convert untrusted strings to template.HTML, because that tells the template engine to skip escaping.
log/slog, implement LogValuer on sensitive types so they print as [REDACTED].Run govulncheck ./... regularly and in CI; it reports only vulnerabilities your code actually reaches. Keep Go itself current, because security fixes to the standard library, including crypto/tls and net/http, ship in minor releases. Use go mod verify to confirm module contents match the checksum database.
Yes. It uses the operating system's cryptographically secure random source. math/rand is predictable and must not be used for secrets.
No. Use the standard library's high-level primitives such as AES-GCM, and well-reviewed modules such as golang.org/x/crypto. Custom schemes almost always have flaws.
Hash them with bcrypt or Argon2id using a per-password salt, which these libraries handle, and compare with the library's verify function.
Mostly, since it is memory-safe by default, but unsafe, cgo and data races can still introduce vulnerabilities. Run go test -race and review any use of unsafe.
Secure Go code uses crypto/rand for secrets, bcrypt or Argon2 for passwords, AES-GCM for encryption, hardened http.Server settings with modern TLS, html/template and placeholders against injection, and govulncheck to catch vulnerable dependencies. Avoid MD5, SHA-1 and math/rand for anything security related.