crypto

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AESDecrypt

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

AESDecrypt decrypts data using AES-GCM

func AESEncrypt

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

AESEncrypt encrypts data using AES-GCM

func CheckPassword

func CheckPassword(password, hash string) bool

CheckPassword compares a password with a hash

func DecryptString

func DecryptString(ciphertext string, key []byte) (string, error)

DecryptString decrypts a base64 encoded string using AES-GCM

func DeriveKey

func DeriveKey(password, salt []byte, keyLen int) ([]byte, error)

DeriveKey derives a key from a password using scrypt

func EncryptString

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

EncryptString encrypts a string using AES-GCM and returns base64 encoded result

func GenerateAPIKey

func GenerateAPIKey() (string, error)

GenerateAPIKey generates a secure API key

func GenerateRSAKeyPair

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

GenerateRSAKeyPair generates an RSA key pair

func GenerateRandomBase64

func GenerateRandomBase64(length int) (string, error)

GenerateRandomBase64 generates a random base64 string

func GenerateRandomBytes

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

GenerateRandomBytes generates cryptographically secure random bytes

func GenerateRandomHex

func GenerateRandomHex(length int) (string, error)

GenerateRandomHex generates a random hex string of specified length

func GenerateRandomString

func GenerateRandomString(length int) (string, error)

GenerateRandomString generates a cryptographically secure random string

func GenerateSalt

func GenerateSalt() ([]byte, error)

GenerateSalt generates a random salt for password hashing

func GenerateSecretKey

func GenerateSecretKey() ([]byte, error)

GenerateSecretKey generates a secure secret key for encryption

func GenerateToken

func GenerateToken(length int) (string, error)

GenerateToken generates a secure random token for authentication

func HMACSHA256

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

HMACSHA256 computes HMAC-SHA256

func HashPassword

func HashPassword(password string) (string, error)

HashPassword creates a bcrypt hash of the password

func HashPasswordSecure

func HashPasswordSecure(password string) (hash, salt string, err error)

HashPasswordSecure hashes a password with a random salt

func HashPasswordWithSalt

func HashPasswordWithSalt(password, salt []byte) ([]byte, error)

HashPasswordWithSalt hashes a password with a given salt using scrypt

func HashSHA256Hex

func HashSHA256Hex(input string) string

HashSHA256Hex returns the hex-encoded SHA-256 hash of the input string.

func RSADecrypt

func RSADecrypt(ciphertext []byte, privateKey *rsa.PrivateKey) ([]byte, error)

RSADecrypt decrypts data using RSA private key

func RSAEncrypt

func RSAEncrypt(data []byte, publicKey *rsa.PublicKey) ([]byte, error)

RSAEncrypt encrypts data using RSA public key

func RSAPrivateKeyFromPEM

func RSAPrivateKeyFromPEM(pemData []byte) (*rsa.PrivateKey, error)

RSAPrivateKeyFromPEM loads an RSA private key from PEM format

func RSAPrivateKeyToPEM

func RSAPrivateKeyToPEM(key *rsa.PrivateKey) ([]byte, error)

RSAPrivateKeyToPEM converts an RSA private key to PEM format

func RSAPublicKeyFromPEM

func RSAPublicKeyFromPEM(pemData []byte) (*rsa.PublicKey, error)

RSAPublicKeyFromPEM loads an RSA public key from PEM format

func RSAPublicKeyToPEM

func RSAPublicKeyToPEM(key *rsa.PublicKey) ([]byte, error)

RSAPublicKeyToPEM converts an RSA public key to PEM format

func RSASign

func RSASign(data []byte, privateKey *rsa.PrivateKey) ([]byte, error)

RSASign signs data using RSA private key

func RSAVerify

func RSAVerify(data, signature []byte, publicKey *rsa.PublicKey) error

RSAVerify verifies a signature using RSA public key

func VerifyHMACSHA256

func VerifyHMACSHA256(data, signature, key []byte) bool

VerifyHMACSHA256 verifies an HMAC-SHA256 signature

func VerifyPasswordSecure

func VerifyPasswordSecure(password, hash, salt string) (bool, error)

VerifyPasswordSecure verifies a password against a hash and salt

Types

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"`
	Custom    map[string]interface{} `json:"-"`
}

JWTClaims represents JWT claims

type SimpleJWT

type SimpleJWT struct {
	SecretKey []byte
}

SimpleJWT represents a simple JWT implementation

func NewSimpleJWT

func NewSimpleJWT(secretKey []byte) *SimpleJWT

NewSimpleJWT creates a new SimpleJWT instance

func (*SimpleJWT) CreateToken

func (j *SimpleJWT) CreateToken(claims JWTClaims) (string, error)

CreateToken creates a JWT token with the given claims

func (*SimpleJWT) VerifyToken

func (j *SimpleJWT) VerifyToken(token string) (*JWTClaims, error)

VerifyToken verifies and parses a JWT token

Jump to

Keyboard shortcuts

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