crypto

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAtLeastOneKeyRequired        = errors.New("at least one of privateKey or publicKey must be provided")
	ErrPublicKeyRequiredForEncrypt  = errors.New("public key is required for encryption")
	ErrPrivateKeyRequiredForDecrypt = errors.New("private key is required for decryption")
	ErrPrivateKeyRequiredForSign    = errors.New("private key is required for signing")
	ErrPublicKeyRequiredForVerify   = errors.New("public key is required for verification")
	ErrFailedDecodePemBlock         = errors.New("failed to decode PEM block")
	ErrUnsupportedPemType           = errors.New("unsupported PEM type")
	ErrInvalidAesKeySize            = errors.New("invalid AES key size")
	ErrInvalidIvSizeCbc             = errors.New("invalid IV size for CBC mode")
	ErrCiphertextNotMultipleOfBlock = errors.New("ciphertext is not a multiple of the block size")
	ErrCiphertextTooShort           = errors.New("ciphertext too short")
	ErrDataEmpty                    = errors.New("data is empty")
	ErrInvalidPadding               = errors.New("invalid padding")
	ErrNotRsaPrivateKey             = errors.New("not an RSA private key")
	ErrNotRsaPublicKey              = errors.New("not an RSA public key")
	ErrNotEcdsaPrivateKey           = errors.New("not an ECDSA private key")
	ErrNotEcdsaPublicKey            = errors.New("not an ECDSA public key")
	ErrInvalidSm4KeySize            = errors.New("invalid SM4 key size")
	ErrInvalidSignature             = errors.New("invalid signature format")
)

Functions

func GenerateEcdsaKey added in v0.12.0

func GenerateEcdsaKey(curve EcdsaCurve) (*ecdsa.PrivateKey, error)

func GenerateEciesKey added in v0.12.0

func GenerateEciesKey(curve EciesCurve) (*ecdh.PrivateKey, error)

Types

type AesMode added in v0.6.0

type AesMode string
const (
	AesModeCbc AesMode = "CBC"
	AesModeGcm AesMode = "GCM"
)

type AesOption added in v0.12.0

type AesOption func(*aesCipher)

func WithAesIv added in v0.12.0

func WithAesIv(iv []byte) AesOption

func WithAesMode added in v0.12.0

func WithAesMode(mode AesMode) AesOption

type Cipher

type Cipher interface {
	// Encrypt encrypts the plaintext string and returns the encrypted string.
	// The returned string is typically base64-encoded or hex-encoded.
	// Returns an error if encryption fails.
	Encrypt(plaintext string) (string, error)
	// Decrypt decrypts the encrypted string and returns the plaintext string.
	// The encrypted string is typically base64-encoded or hex-encoded.
	// Returns an error if decryption fails (e.g., invalid format, wrong key, corrupted data).
	Decrypt(ciphertext string) (string, error)
}

Cipher defines the interface for encryption and decryption operations.

func NewAes added in v0.12.0

func NewAes(key []byte, opts ...AesOption) (Cipher, error)

func NewAesFromBase64 added in v0.12.0

func NewAesFromBase64(keyBase64 string, opts ...AesOption) (Cipher, error)

func NewAesFromHex added in v0.12.0

func NewAesFromHex(keyHex string, opts ...AesOption) (Cipher, error)

func NewEcies added in v0.12.0

func NewEcies(privateKey *ecdh.PrivateKey, publicKey *ecdh.PublicKey, opts ...EciesOption) (Cipher, error)

func NewEciesFromBase64 added in v0.12.0

func NewEciesFromBase64(privateKeyBase64, publicKeyBase64 string, curve EciesCurve, opts ...EciesOption) (Cipher, error)

func NewEciesFromBytes added in v0.12.0

func NewEciesFromBytes(privateKeyBytes, publicKeyBytes []byte, curve EciesCurve, opts ...EciesOption) (Cipher, error)

func NewEciesFromHex added in v0.12.0

func NewEciesFromHex(privateKeyHex, publicKeyHex string, curve EciesCurve, opts ...EciesOption) (Cipher, error)

func NewSm4 added in v0.12.0

func NewSm4(key []byte, opts ...Sm4Option) (Cipher, error)

func NewSm4FromBase64 added in v0.12.0

func NewSm4FromBase64(keyBase64 string, opts ...Sm4Option) (Cipher, error)

func NewSm4FromHex added in v0.12.0

func NewSm4FromHex(keyHex string, opts ...Sm4Option) (Cipher, error)

type CipherSigner added in v0.12.0

type CipherSigner interface {
	Cipher
	Signer
}

CipherSigner defines the interface for encryption, decryption, signing, and verifying.

func NewRsa added in v0.12.0

func NewRsa(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, opts ...RsaOption) (CipherSigner, error)

func NewRsaFromBase64 added in v0.12.0

func NewRsaFromBase64(privateKeyBase64, publicKeyBase64 string, opts ...RsaOption) (CipherSigner, error)

func NewRsaFromHex added in v0.12.0

func NewRsaFromHex(privateKeyHex, publicKeyHex string, opts ...RsaOption) (CipherSigner, error)

func NewRsaFromPem added in v0.12.0

func NewRsaFromPem(privatePem, publicPem []byte, opts ...RsaOption) (CipherSigner, error)

func NewSm2 added in v0.12.0

func NewSm2(privateKey *sm2.PrivateKey, publicKey *sm2.PublicKey) (CipherSigner, error)

func NewSm2FromBase64 added in v0.12.0

func NewSm2FromBase64(privateKeyBase64, publicKeyBase64 string) (CipherSigner, error)

func NewSm2FromHex added in v0.12.0

func NewSm2FromHex(privateKeyHex, publicKeyHex string) (CipherSigner, error)

func NewSm2FromPem added in v0.12.0

func NewSm2FromPem(privatePem, publicPem []byte) (CipherSigner, error)

type EcdsaCurve added in v0.12.0

type EcdsaCurve string
const (
	EcdsaCurveP224 EcdsaCurve = "P224"
	EcdsaCurveP256 EcdsaCurve = "P256"
	EcdsaCurveP384 EcdsaCurve = "P384"
	EcdsaCurveP521 EcdsaCurve = "P521"
)

type EcdsaOption added in v0.12.0

type EcdsaOption func(*ecdsaCipher)

type EciesCurve added in v0.12.0

type EciesCurve string
const (
	EciesCurveP256   EciesCurve = "P256"
	EciesCurveP384   EciesCurve = "P384"
	EciesCurveP521   EciesCurve = "P521"
	EciesCurveX25519 EciesCurve = "X25519"
)

type EciesOption added in v0.12.0

type EciesOption func(*eciesCipher)

type RsaMode added in v0.6.0

type RsaMode string
const (
	RsaModeOaep     RsaMode = "OAEP"
	RsaModePkcs1v15 RsaMode = "PKCS1v15"
)

type RsaOption added in v0.12.0

type RsaOption func(*rsaCipher)

func WithRsaMode added in v0.12.0

func WithRsaMode(mode RsaMode) RsaOption

func WithRsaSignMode added in v0.12.0

func WithRsaSignMode(signMode RsaSignMode) RsaOption

type RsaSignMode added in v0.12.0

type RsaSignMode string
const (
	RsaSignModePss      RsaSignMode = "PSS"
	RsaSignModePkcs1v15 RsaSignMode = "PKCS1v15"
)

type Signer added in v0.12.0

type Signer interface {
	// Sign signs the data string and returns the signature.
	// The returned signature is typically base64-encoded.
	// Returns an error if signing fails.
	Sign(data string) (signature string, err error)
	// Verify verifies the signature against the data.
	// Returns true if the signature is valid, false otherwise.
	// Returns an error if verification process fails (e.g., invalid format).
	Verify(data, signature string) (bool, error)
}

Signer defines the interface for signing and verifying operations.

func NewEcdsa added in v0.12.0

func NewEcdsa(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, opts ...EcdsaOption) (Signer, error)

func NewEcdsaFromBase64 added in v0.12.0

func NewEcdsaFromBase64(privateKeyBase64, publicKeyBase64 string, opts ...EcdsaOption) (Signer, error)

func NewEcdsaFromHex added in v0.12.0

func NewEcdsaFromHex(privateKeyHex, publicKeyHex string, opts ...EcdsaOption) (Signer, error)

func NewEcdsaFromPem added in v0.12.0

func NewEcdsaFromPem(privatePem, publicPem []byte, opts ...EcdsaOption) (Signer, error)

type Sm4Mode added in v0.6.0

type Sm4Mode string
const (
	Sm4ModeCbc Sm4Mode = "CBC"
	Sm4ModeEcb Sm4Mode = "ECB"
)

type Sm4Option added in v0.12.0

type Sm4Option func(*sm4Cipher)

func WithSm4Iv added in v0.12.0

func WithSm4Iv(iv []byte) Sm4Option

func WithSm4Mode added in v0.12.0

func WithSm4Mode(mode Sm4Mode) Sm4Option

Jump to

Keyboard shortcuts

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