crypto

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 18 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AESCBCDecrypt

func AESCBCDecrypt(key, data []byte) ([]byte, error)

AESCBCDecrypt decrypts data produced by AESCBCEncrypt.

func AESCBCEncrypt

func AESCBCEncrypt(key, plaintext []byte) ([]byte, error)

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

func AESCFBDecrypt(key, ciphertext []byte) ([]byte, error)

AESCFBDecrypt decrypts AES-CFB ciphertext (IV prepended).

func AESCFBEncrypt

func AESCFBEncrypt(key, plaintext []byte) ([]byte, error)

AESCFBEncrypt encrypts plaintext using AES in CFB mode. The returned ciphertext includes a random IV prepended to the encrypted data.

func AESGCMDecrypt

func AESGCMDecrypt(key, data []byte) ([]byte, error)

AESGCMDecrypt decrypts data produced by AESGCMEncrypt.

func AESGCMDecryptBase64

func AESGCMDecryptBase64(key []byte, encoded string) ([]byte, error)

AESGCMDecryptBase64 decrypts a base64-encoded ciphertext.

func AESGCMEncrypt

func AESGCMEncrypt(key, plaintext []byte) ([]byte, error)

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

func AESGCMEncryptBase64(key, plaintext []byte) (string, error)

AESGCMEncryptBase64 encrypts and returns base64-encoded result.

func Base64Decode

func Base64Decode(s string) ([]byte, error)

Base64Decode decodes a standard base64 string.

func Base64Encode

func Base64Encode(data []byte) string

Base64Encode returns standard base64 encoding.

func Base64URLDecode

func Base64URLDecode(s string) ([]byte, error)

Base64URLDecode decodes a URL-safe base64 string (no padding).

func Base64URLEncode

func Base64URLEncode(data []byte) string

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

func ExportRSAPublicKeyPEM(pub *rsa.PublicKey) (string, error)

ExportRSAPublicKeyPEM exports a public key as PEM-encoded string.

func GenerateRSAKeyPair

func GenerateRSAKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error)

GenerateRSAKeyPair generates an RSA key pair of the given bit size.

func MustRandomKey

func MustRandomKey(n int) []byte

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

func ParseRSAPublicKeyPEM(pemStr string) (*rsa.PublicKey, error)

ParseRSAPublicKeyPEM parses a PEM-encoded RSA public key.

func RC4Decrypt

func RC4Decrypt(str string, key []byte) string

RC4Decrypt decrypts a hex-encoded RC4 ciphertext.

func RC4Encrypt

func RC4Encrypt(str string, key []byte) string

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

func RSAEncrypt(pub *rsa.PublicKey, plaintext []byte) ([]byte, error)

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 RSAVerify

func RSAVerify(pub *rsa.PublicKey, data, sig []byte) error

RSAVerify verifies an RSASSA-PKCS1-v1.5 signature.

func RandomBase64

func RandomBase64(n int) (string, error)

RandomBase64 returns n cryptographically secure random bytes as base64.

func RandomHex

func RandomHex(n int) (string, error)

RandomHex returns n cryptographically secure random bytes as a hex string.

func RandomKey

func RandomKey(n int) ([]byte, error)

RandomKey returns n cryptographically secure random bytes.

func RandomReader

func RandomReader() io.Reader

RandomReader returns the crypto/rand reader for io.Copy usage.

func SignSHA256

func SignSHA256(data, key []byte) []byte

SignSHA256 signs data using HMAC-SHA256.

func SignSHA512

func SignSHA512(data, key []byte) []byte

SignSHA512 signs data using HMAC-SHA512.

func VerifySHA256

func VerifySHA256(data, key, sig []byte) bool

VerifySHA256 verifies an HMAC-SHA256 signature in constant time.

func VerifySHA512

func VerifySHA512(data, key, sig []byte) bool

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 NewJWT

func NewJWT(cfg JWTConfig) *JWT

NewJWT creates a new JWT signer with the given config.

func (*JWT) Parse

func (j *JWT) Parse(token string) (*JWTClaims, error)

Parse parses and verifies a JWT token, returning the claims. Does not check expiration; use Verify for full validation.

func (*JWT) Sign

func (j *JWT) Sign(claims JWTClaims) (string, error)

Sign creates a signed JWT token from the given claims.

func (*JWT) Verify

func (j *JWT) Verify(token string) (*JWTClaims, error)

Verify parses, verifies, and checks expiration of a JWT token.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL