Security Hardening

16 articles
How to Encrypt and Decrypt Data in Go (AES, RSA) Use the `crypto/aes` package for symmetric encryption (fast, same key) and `crypto/rand` with `crypto/cipher` for secure initialization vectors, while relying on `crypto/rsa` for asymmetric operations where you encrypt with a public key and decrypt with a private key. How to Generate and Validate API Keys in Go Generate API keys using `crypto/rand` to create cryptographically secure random bytes, then encode them as Base64 or Hex strings for storage. How to Generate Self-Signed Certificates in Go Generate a self-signed certificate in Go using crypto/x509 and crypto/rand packages. How to Implement Input Validation and Sanitization in Go Prevent security vulnerabilities in Go by using html/template for automatic escaping and filepath.IsLocal or os.OpenRoot for safe file access. How to Implement Rate Limiting to Prevent Abuse Implement rate limiting in Go is best done using the `golang.org/x/time/rate` package, which provides a token bucket algorithm to control the frequency of operations. How to Implement RBAC (Role-Based Access Control) in Go Implement RBAC in Go by manually mapping roles to permissions and checking them in your middleware or handlers. How to Implement Session-Based Authentication in Go Implement session-based authentication in Go by generating a random session ID, storing it server-side, and managing it via secure HTTP-only cookies. How to Prevent XSS and CSRF in Go Web Applications Prevent CSRF by using net/http.CrossOriginProtection and stop XSS by escaping user input with html.EscapeString. How to Sign and Verify Data in Go Sign and verify data in Go using crypto/rsa or crypto/ecdsa packages with SHA256 hashing for secure digital signatures. How to Use crypto/rand for Secure Random Numbers in Go Generate secure random bytes in Go using the crypto/rand package's Read function for cryptographic safety. How to Use govulncheck for Vulnerability Scanning Run `govulncheck` directly on your Go source code or binary to identify known vulnerabilities without needing to build the project first. How to Use govulncheck to Find Known Vulnerabilities Run govulncheck ./... to scan your Go module for known vulnerabilities in the code and dependencies. How to Use HMAC in Go Compute an HMAC in Go by importing crypto/hmac and crypto/sha256, creating a new HMAC instance with your key, writing your message, and calling Sum to get the result. How to Use HTTPS/TLS in Go Enable HTTPS in Go by loading certificate and key files into a tls.Config and passing it to your HTTP server. How to Use the crypto/tls Package for Custom TLS Configurations You use the `crypto/tls` package by creating a `tls.Config` struct, customizing fields like `RootCAs` or `InsecureSkipVerify`, and passing it to `tls.Dial` or `http.Transport`. Security Best Practices for Go Web Applications Enable GODEBUG path protections and report vulnerabilities to security@golang.org to secure Go web applications.