Documentation
¶
Overview ¶
Package crypto provides encryption and signing utilities:
- AES: GCM and CBC modes with padding
- RSA: key generation, encryption/decryption, signing/verification
- HMAC-SHA signatures
- JWT: create and parse JSON Web Tokens (HS256/RS256)
Quick start ¶
// AES-GCM
key := crypto.RandomKey(32) // AES-256
ciphertext, _ := crypto.AESGCMEncrypt(key, []byte("secret"))
plaintext, _ := crypto.AESGCMDecrypt(key, ciphertext)
// RSA
priv, pub, _ := crypto.GenerateRSAKeyPair(2048)
enc, _ := crypto.RSAEncrypt(pub, []byte("data"))
dec, _ := crypto.RSADecrypt(priv, enc)
// JWT
token, _ := crypto.NewJWT(crypto.JWTConfig{Secret: []byte("key")}).Sign(claims)
Index ¶
- func AESCBCDecrypt(key, data []byte) ([]byte, error)
- func AESCBCEncrypt(key, plaintext []byte) ([]byte, error)
- func AESCFBDecrypt(key, ciphertext []byte) ([]byte, error)
- func AESCFBEncrypt(key, plaintext []byte) ([]byte, error)
- func AESGCMDecrypt(key, data []byte) ([]byte, error)
- func AESGCMDecryptBase64(key []byte, encoded string) ([]byte, error)
- func AESGCMEncrypt(key, plaintext []byte) ([]byte, error)
- func AESGCMEncryptBase64(key, plaintext []byte) (string, error)
- func Base64Decode(s string) ([]byte, error)
- func Base64Encode(data []byte) string
- func Base64URLDecode(s string) ([]byte, error)
- func Base64URLEncode(data []byte) string
- func ExportRSAPrivateKeyPEM(priv *rsa.PrivateKey) (string, error)
- func ExportRSAPublicKeyPEM(pub *rsa.PublicKey) (string, error)
- func GenerateRSAKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error)
- func MustRandomKey(n int) []byte
- func ParseRSAPrivateKeyPEM(pemStr string) (*rsa.PrivateKey, error)
- func ParseRSAPublicKeyPEM(pemStr string) (*rsa.PublicKey, error)
- func RC4Decrypt(str string, key []byte) string
- func RC4Encrypt(str string, key []byte) string
- func RSADecrypt(priv *rsa.PrivateKey, ciphertext []byte) ([]byte, error)
- func RSAEncrypt(pub *rsa.PublicKey, plaintext []byte) ([]byte, error)
- func RSASign(priv *rsa.PrivateKey, data []byte) ([]byte, error)
- func RSAVerify(pub *rsa.PublicKey, data, sig []byte) error
- func RandomBase64(n int) (string, error)
- func RandomHex(n int) (string, error)
- func RandomKey(n int) ([]byte, error)
- func RandomReader() io.Reader
- func SignSHA256(data, key []byte) []byte
- func SignSHA512(data, key []byte) []byte
- func VerifySHA256(data, key, sig []byte) bool
- func VerifySHA512(data, key, sig []byte) bool
- type JWT
- type JWTAlgorithm
- type JWTClaims
- type JWTConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AESCBCDecrypt ¶
AESCBCDecrypt decrypts data produced by AESCBCEncrypt.
func AESCBCEncrypt ¶
AESCBCEncrypt encrypts plaintext using AES-CBC with PKCS7 padding. The key must be 16, 24, or 32 bytes. A random IV is prepended to the output.
func AESCFBDecrypt ¶
AESCFBDecrypt decrypts AES-CFB ciphertext (IV prepended).
func AESCFBEncrypt ¶
AESCFBEncrypt encrypts plaintext using AES in CFB mode. The returned ciphertext includes a random IV prepended to the encrypted data.
func AESGCMDecrypt ¶
AESGCMDecrypt decrypts data produced by AESGCMEncrypt.
func AESGCMDecryptBase64 ¶
AESGCMDecryptBase64 decrypts a base64-encoded ciphertext.
func AESGCMEncrypt ¶
AESGCMEncrypt encrypts plaintext using AES-GCM with the given key. The key must be 16, 24, or 32 bytes (AES-128/192/256). Returns nonce + ciphertext concatenated.
func AESGCMEncryptBase64 ¶
AESGCMEncryptBase64 encrypts and returns base64-encoded result.
func Base64Decode ¶
Base64Decode decodes a standard base64 string.
func Base64Encode ¶
Base64Encode returns standard base64 encoding.
func Base64URLDecode ¶
Base64URLDecode decodes a URL-safe base64 string (no padding).
func Base64URLEncode ¶
Base64URLEncode returns URL-safe base64 encoding (no padding).
func ExportRSAPrivateKeyPEM ¶
func ExportRSAPrivateKeyPEM(priv *rsa.PrivateKey) (string, error)
ExportRSAPrivateKeyPEM exports a private key as PEM-encoded string.
func ExportRSAPublicKeyPEM ¶
ExportRSAPublicKeyPEM exports a public key as PEM-encoded string.
func GenerateRSAKeyPair ¶
GenerateRSAKeyPair generates an RSA key pair of the given bit size.
func MustRandomKey ¶
MustRandomKey is like RandomKey but panics on error.
func ParseRSAPrivateKeyPEM ¶
func ParseRSAPrivateKeyPEM(pemStr string) (*rsa.PrivateKey, error)
ParseRSAPrivateKeyPEM parses a PEM-encoded RSA private key.
func ParseRSAPublicKeyPEM ¶
ParseRSAPublicKeyPEM parses a PEM-encoded RSA public key.
func RC4Decrypt ¶
RC4Decrypt decrypts a hex-encoded RC4 ciphertext.
func RC4Encrypt ¶
RC4Encrypt encrypts str with RC4 and returns hex-encoded result. RC4 is considered insecure; prefer AES for new code.
func RSADecrypt ¶
func RSADecrypt(priv *rsa.PrivateKey, ciphertext []byte) ([]byte, error)
RSADecrypt decrypts ciphertext using RSA-OAEP with SHA-256.
func RSAEncrypt ¶
RSAEncrypt encrypts plaintext using RSA-OAEP with SHA-256.
func RSASign ¶
func RSASign(priv *rsa.PrivateKey, data []byte) ([]byte, error)
RSASign signs data using RSASSA-PKCS1-v1.5 with SHA-256.
func RandomBase64 ¶
RandomBase64 returns n cryptographically secure random bytes as base64.
func RandomReader ¶
RandomReader returns the crypto/rand reader for io.Copy usage.
func VerifySHA256 ¶
VerifySHA256 verifies an HMAC-SHA256 signature in constant time.
func VerifySHA512 ¶
VerifySHA512 verifies an HMAC-SHA512 signature in constant time.
Types ¶
type JWT ¶
type JWT struct {
// contains filtered or unexported fields
}
JWT is a JWT token signer/verifier.
func (*JWT) Parse ¶
Parse parses and verifies a JWT token, returning the claims. Does not check expiration; use Verify for full validation.
type JWTAlgorithm ¶
type JWTAlgorithm string
JWTAlgorithm represents a JWT signing algorithm.
const ( JWTAlgHS256 JWTAlgorithm = "HS256" JWTAlgHS512 JWTAlgorithm = "HS512" JWTAlgRS256 JWTAlgorithm = "RS256" )
type JWTClaims ¶
type JWTClaims struct {
Issuer string `json:"iss,omitempty"`
Subject string `json:"sub,omitempty"`
Audience string `json:"aud,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
NotBefore int64 `json:"nbf,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
ID string `json:"jti,omitempty"`
Extra map[string]interface{} `json:"-"`
}
JWTClaims represents JWT claims.
type JWTConfig ¶
type JWTConfig struct {
Algorithm JWTAlgorithm // default HS256
Secret []byte // for HS256/HS512
RSAPriv *rsa.PrivateKey // for RS256 signing
RSAPub *rsa.PublicKey // for RS256 verification
Issuer string // iss claim
Audience string // aud claim
ExpiresIn time.Duration // exp claim
}
JWTConfig configures the JWT signer.