crypto

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AesCbcDecryptor added in v0.1.10

func AesCbcDecryptor[T Plaintext](
	key Base64EncodedKey,
	padding AesPadding,
) mo.Result[AesDecryptFn[T]]

AesCbcDecryptor returns a function that decrypts Base64Ciphertext (AES-CBC) to Plaintext T.

func AesCbcEncryptor added in v0.1.10

func AesCbcEncryptor[T Plaintext](
	key Base64EncodedKey,
	padding AesPadding,
) mo.Result[AesEncryptFn[T]]

AesCbcEncryptor returns a function that encrypts Plaintext with AES-CBC and returns Base64Ciphertext. Key is Base64EncodedKey (16, 24, or 32 raw bytes after decode). Wire format: IV(16)||ciphertext.

func AesEcbDecryptor added in v0.1.10

func AesEcbDecryptor[T Plaintext](
	key Base64EncodedKey,
	padding AesPadding,
) mo.Result[AesDecryptFn[T]]

AesEcbDecryptor returns a function that decrypts Base64Ciphertext (AES-ECB) to Plaintext T.

func AesEcbEncryptor added in v0.1.10

func AesEcbEncryptor[T Plaintext](
	key Base64EncodedKey,
	padding AesPadding,
) mo.Result[AesEncryptFn[T]]

AesEcbEncryptor returns a function that encrypts Plaintext with AES-ECB and returns Base64Ciphertext. Key is Base64EncodedKey (16, 24, or 32 raw bytes after decode). Wire format: ciphertext only.

func AesGcmDecryptor added in v0.1.10

func AesGcmDecryptor[T Plaintext](key Base64EncodedKey) mo.Result[AesDecryptFn[T]]

AesGcmDecryptor returns a function that decrypts Base64Ciphertext (AES-GCM) to Plaintext T.

func AesGcmEncryptor added in v0.1.10

func AesGcmEncryptor[T Plaintext](key Base64EncodedKey) mo.Result[AesEncryptFn[T]]

AesGcmEncryptor returns a function that encrypts Plaintext with AES-GCM and returns Base64Ciphertext. Key is Base64EncodedKey (16, 24, or 32 raw bytes after decode). Wire format: nonce(12)||ciphertext||tag.

func AesKeyFromBase64 added in v0.1.10

func AesKeyFromBase64(encoded string) mo.Result[Base64EncodedKey]

AesKeyFromBase64 parses a standard base64 string into Base64EncodedKey after validating that it decodes to 16, 24, or 32 bytes of key material.

func DecryptRsa added in v0.1.10

func DecryptRsa[T Plaintext](
	encryptedBase64 Base64Ciphertext,
	priv *rsa.PrivateKey,
) mo.Result[*T]

DecryptRsa decrypts Base64Ciphertext from EncryptRsa or RsaEncryptor using the given RSA private key. T is Plaintext (string or []byte). Prefer RsaDecryptor[T](PemContent(...)) or RsaDecryptor[T](KeyFilePath(...)) when reusing a key.

func Deobfuscate

func Deobfuscate(s string) string

func EncryptRsa added in v0.1.10

func EncryptRsa[T Plaintext](payload T, pub *rsa.PublicKey) mo.Result[*Base64Ciphertext]

EncryptRsa encrypts Plaintext payload T with the given RSA public key (RSA-OAEP + AES-256-GCM). Returns *Base64Ciphertext. Prefer RsaEncryptor[T](PemContent(...)) or RsaEncryptor[T](KeyFilePath(...)) when reusing a key.

func HmacSha256Generator added in v0.1.10

func HmacSha256Generator[T Plaintext](key Base64EncodedKey) mo.Result[HmacFn[T]]

HmacSha256Generator returns a function that computes HMAC-SHA256 of Plaintext message T. Key is Base64EncodedKey (standard base64); decoded secret length must be non-empty (any length). Digest is lowercase hex (64 chars) as HexHmacDigest.

func LoadRsaPrivateKey added in v0.1.10

func LoadRsaPrivateKey(pemContents string) mo.Result[*rsa.PrivateKey]

LoadRsaPrivateKey decodes a PEM-encoded RSA private key string (PKCS#8 or PKCS#1) and returns it as mo.Result[*rsa.PrivateKey].

func LoadRsaPrivateKeyFromFile added in v0.1.10

func LoadRsaPrivateKeyFromFile(path string) mo.Result[*rsa.PrivateKey]

LoadRsaPrivateKeyFromFile reads PEM from the given file path and decodes an RSA private key (PKCS#8 or PKCS#1), returning mo.Result[*rsa.PrivateKey].

func LoadRsaPublicKey added in v0.1.10

func LoadRsaPublicKey(pemContents string) mo.Result[*rsa.PublicKey]

LoadRsaPublicKey decodes a PEM-encoded RSA public key string (PKIX or PKCS#1) and returns it as mo.Result[*rsa.PublicKey].

func LoadRsaPublicKeyFromFile added in v0.1.10

func LoadRsaPublicKeyFromFile(path string) mo.Result[*rsa.PublicKey]

LoadRsaPublicKeyFromFile reads PEM from the given file path and decodes an RSA public key (PKIX or PKCS#1), returning mo.Result[*rsa.PublicKey].

func RandomAesKey added in v0.1.10

func RandomAesKey(length int) mo.Result[Base64EncodedKey]

RandomAesKey draws length bytes from crypto/rand (full entropy per byte), then returns Base64EncodedKey. length must be 16, 24, or 32 (AES-128, AES-192, AES-256).

func RsaDecryptor added in v0.1.10

func RsaDecryptor[T Plaintext, S PemContent | KeyFilePath](source S) mo.Result[RsaDecryptFn[T]]

RsaDecryptor loads the RSA private key from PemContent or KeyFilePath and returns RsaDecryptFn[T]. Input ciphertext is Base64Ciphertext (from RsaEncryptor or EncryptRsa). T is Plaintext for the result. Example: decryptRes := RsaDecryptor[string](crypto.PemContent(privateKeyPEM)); decrypt := decryptRes.MustGet(); result := decrypt(ct) Example: decryptRes := RsaDecryptor[string](crypto.KeyFilePath("/path/to/key.pem")); ...

func RsaEncryptor added in v0.1.10

func RsaEncryptor[T Plaintext, S PemContent | KeyFilePath](source S) mo.Result[RsaEncryptFn[T]]

RsaEncryptor loads the RSA public key from PemContent or KeyFilePath and returns RsaEncryptFn[T]. Hybrid encryption: RSA-OAEP key encapsulation + AES-256-GCM; output is *Base64Ciphertext. T is Plaintext (string or []byte). Example: encryptRes := RsaEncryptor[string](crypto.PemContent(publicKeyPEM)); encrypt := encryptRes.MustGet(); result := encrypt(plainText) Example: encryptRes := RsaEncryptor[string](crypto.KeyFilePath("/path/to/pub.pem")); ...

Types

type AesDecryptFn added in v0.1.10

type AesDecryptFn[T Plaintext] func(ciphertext Base64Ciphertext) mo.Result[T]

AesDecryptFn decrypts Base64Ciphertext and returns Plaintext T (string or []byte).

type AesEncryptFn added in v0.1.10

type AesEncryptFn[T Plaintext] func(payload T) mo.Result[Base64Ciphertext]

AesEncryptFn encrypts Plaintext and returns Base64Ciphertext (AES payload, base64 on the wire).

type AesPadding added in v0.1.10

type AesPadding int

AesPadding is the padding scheme for CBC and ECB. Aligns with Bouncy Castle: PKCS7PADDING, NOPADDING.

const (
	AesPaddingPKCS7 AesPadding = iota
	AesPaddingNone
)

type Base64Ciphertext added in v0.1.10

type Base64Ciphertext string

Base64Ciphertext is base64-encoded ciphertext on the wire (RSA hybrid, AES base64 form).

type Base64EncodedKey added in v0.1.10

type Base64EncodedKey string

Base64EncodedKey is a standard base64 encoding of raw AES key material (16, 24, or 32 bytes).

type HexHmacDigest added in v0.1.10

type HexHmacDigest string

HexHmacDigest is lowercase hexadecimal encoding of a digest (e.g. HMAC-SHA256 as 64 hex chars).

type HmacFn added in v0.1.10

type HmacFn[T Plaintext] func(message T) mo.Result[HexHmacDigest]

HmacFn computes HMAC over Plaintext message T and returns HexHmacDigest (lowercase hex).

type KeyFilePath added in v0.1.10

type KeyFilePath string

KeyFilePath is the path to a file containing PEM-encoded key material. Use with RsaEncryptor or RsaDecryptor.

type PemContent added in v0.1.10

type PemContent string

PemContent is PEM-encoded key material (public or private) as a string. Use with RsaEncryptor or RsaDecryptor.

type Plaintext added in v0.1.10

type Plaintext interface {
	string | []byte
}

Plaintext is plaintext or message material: string (UTF-8/text) or raw []byte. Used for symmetric/HMAC input, AES decrypt output, and RSA decrypt output shape.

type RsaDecryptFn added in v0.1.10

type RsaDecryptFn[T Plaintext] func(ciphertext Base64Ciphertext) mo.Result[*T]

RsaDecryptFn decrypts Base64Ciphertext; T is Plaintext (string or []byte) for the recovered plaintext.

type RsaEncryptFn added in v0.1.10

type RsaEncryptFn[T Plaintext] func(payload T) mo.Result[*Base64Ciphertext]

RsaEncryptFn encrypts a Plaintext payload and returns *Base64Ciphertext (hybrid RSA-OAEP + AES-GCM blob, base64 on the wire).

Jump to

Keyboard shortcuts

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