Go handles security and encryption mainly through its standard library: the crypto packages for hashing, encryption and signatures, crypto/tls for secure connections, and crypto/rand for secure randomness. The language itself adds memory safety, since there is no pointer arithmetic and bounds are checked. You still have to choose the right algorithms and use them correctly.
The sections below cover the building blocks you will use most often, with current recommendations for 2026.
Go is memory safe in normal code: arrays and slices are bounds-checked, memory is garbage collected, and there is no manual free. The unsafe package and cgo opt out of those guarantees, so keep them out of security-sensitive code.
Two habits matter more than any API:
crypto/rand, never math/rand, for tokens, keys, salts and nonces.crypto/subtle.ConstantTimeCompare to avoid timing leaks.Use SHA-256 or SHA-512 from crypto/sha256 and crypto/sha512 for integrity checks, and HMAC (crypto/hmac) to authenticate messages with a key. MD5 and SHA-1 are broken for security purposes; keep them only for legacy checksums.
Never store passwords with a fast hash. Use a slow, salted password hash such as bcrypt, scrypt or Argon2id, available in golang.org/x/crypto.
bcrypt truncates input at 72 bytes, so for very long passphrases consider Argon2id (argon2.IDKey). The standard library also gained crypto/pbkdf2 and crypto/hkdf in Go 1.24.
For encrypting data, use an authenticated mode. AES-GCM provides both confidentiality and tamper detection. Never reuse a nonce with the same key.
Raw ECB or CBC without a MAC is easy to misuse and should be avoided. golang.org/x/crypto/chacha20poly1305 is a good alternative on hardware without AES acceleration.
Prefer Ed25519 (crypto/ed25519) or ECDSA (crypto/ecdsa) for signatures, and use RSA at 2048 bits or more when interoperability requires it. For key exchange, crypto/ecdh provides X25519 and NIST curves. Go 1.24 added crypto/mlkem for post-quantum key encapsulation, and the TLS stack enables a hybrid X25519MLKEM768 exchange by default in current releases.
crypto/tls and net/http support TLS 1.2 and 1.3. Defaults are sensible, so set only what you need and keep the minimum at TLS 1.2.
For clients, never set InsecureSkipVerify: true in production. For automatic certificates use golang.org/x/crypto/acme/autocert. Set timeouts on servers to limit slow-client attacks.
The standard library has no JWT package. Use a maintained module such as github.com/golang-jwt/jwt/v5, always verify the signing method and validate exp, aud and iss. Prefer short-lived tokens and keep secrets in a secrets manager or environment, never in source.
Other practices that catch real bugs:
database/sql placeholders, never string concatenation, to prevent SQL injection.html/template, not text/template, for HTML output so values are escaped.govulncheck ./... to find known vulnerabilities in dependencies and standard library calls.go vet and a linter such as gosec in CI.Use crypto/aes with cipher.NewGCM, or chacha20poly1305 from x/crypto. Both give authenticated encryption.
math/rand safe for tokens?No. Use crypto/rand. Since Go 1.22 math/rand/v2 is better seeded, but it is still not intended for security.
Use bcrypt or Argon2id from golang.org/x/crypto, which add a salt and a tunable cost. Do not use SHA-256 alone.
Yes. crypto/tls negotiates TLS 1.3 by default when both sides support it.
Go gives you memory safety, a strong crypto standard library, and TLS built into net/http. Use crypto/rand for randomness, bcrypt or Argon2id for passwords, AES-GCM for encryption, Ed25519 for signatures, and verify TLS certificates. Add govulncheck to your pipeline and keep Go current.