Documentation
¶
Index ¶
- Variables
- type AESCipher
- type AESMode
- type Cipher
- func NewAES(key, iv []byte, mode ...AESMode) (Cipher, error)
- func NewAESFromBase64(keyBase64, ivBase64 string, mode ...AESMode) (Cipher, error)
- func NewAESFromHex(keyHex, ivHex string, mode ...AESMode) (Cipher, error)
- func NewRSA(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, mode ...RSAMode) (Cipher, error)
- func NewRSAFromBase64(privateKeyBase64, publicKeyBase64 string, mode ...RSAMode) (Cipher, error)
- func NewRSAFromHex(privateKeyHex, publicKeyHex string, mode ...RSAMode) (Cipher, error)
- func NewRSAFromPEM(privatePEM, publicPEM []byte, mode ...RSAMode) (Cipher, error)
- func NewSM2(privateKey *sm2.PrivateKey, publicKey *sm2.PublicKey) (Cipher, error)
- func NewSM2FromBase64(privateKeyBase64, publicKeyBase64 string) (Cipher, error)
- func NewSM2FromHex(privateKeyHex, publicKeyHex string) (Cipher, error)
- func NewSM2FromPEM(privatePEM, publicPEM []byte) (Cipher, error)
- func NewSM4(key, iv []byte, mode ...SM4Mode) (Cipher, error)
- func NewSM4FromBase64(keyBase64, ivBase64 string, mode ...SM4Mode) (Cipher, error)
- func NewSM4FromHex(keyHex, ivHex string, mode ...SM4Mode) (Cipher, error)
- type Decryptor
- type Encryptor
- type RSACipher
- type RSAMode
- type SM2Cipher
- type SM4Cipher
- type SM4Mode
Constants ¶
This section is empty.
Variables ¶
var ( // Common. 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") ErrFailedDecodePEMBlock = errors.New("failed to decode PEM block") ErrUnsupportedPEMType = errors.New("unsupported PEM type") // AES. 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") // RSA. ErrNotRSAPrivateKey = errors.New("not an RSA private key") ErrNotRSAPublicKey = errors.New("not an RSA public key") // SM4. ErrInvalidSM4KeySize = errors.New("invalid SM4 key size") )
Functions ¶
This section is empty.
Types ¶
type AESCipher ¶
type AESCipher struct {
// contains filtered or unexported fields
}
AESCipher implements Cipher interface using AES encryption.
type Cipher ¶
Cipher defines the interface for both encryption and decryption operations.
func NewAES ¶
NewAES creates a new AES cipher with the given key, IV, and optional mode. For CBC mode: key must be 16, 24, or 32 bytes (AES-128, AES-192, AES-256), IV must be 16 bytes. For GCM mode: key must be 16, 24, or 32 bytes, IV is not used (GCM generates random nonce). If mode is not specified, defaults to AESModeGCM.
func NewAESFromBase64 ¶
NewAESFromBase64 creates a new AES cipher from base64-encoded key and IV strings. If mode is not specified, defaults to AESModeGCM.
func NewAESFromHex ¶
NewAESFromHex creates a new AES cipher from hex-encoded key and IV strings. If mode is not specified, defaults to AESModeGCM.
func NewRSA ¶
NewRSA creates a new RSA cipher with the given private and public keys and optional mode. For encryption-only operations, privateKey can be nil. For decryption-only operations, publicKey can be nil. If mode is not specified, defaults to RSAModeOAEP (recommended).
func NewRSAFromBase64 ¶
NewRSAFromBase64 creates a new RSA cipher from base64-encoded DER keys. Either privateKeyBase64 or publicKeyBase64 can be empty, but not both. If mode is not specified, defaults to RSAModeOAEP (recommended).
func NewRSAFromHex ¶
NewRSAFromHex creates a new RSA cipher from hex-encoded DER keys. Either privateKeyHex or publicKeyHex can be empty, but not both. If mode is not specified, defaults to RSAModeOAEP (recommended).
func NewRSAFromPEM ¶
NewRSAFromPEM creates a new RSA cipher from PEM-encoded keys. Either privatePEM or publicPEM can be nil, but not both. If mode is not specified, defaults to RSAModeOAEP (recommended).
func NewSM2 ¶
NewSM2 creates a new SM2 cipher with the given private and public keys. For encryption-only operations, privateKey can be nil. For decryption-only operations, publicKey can be nil.
func NewSM2FromBase64 ¶
NewSM2FromBase64 creates a new SM2 cipher from base64-encoded keys. Either privateKeyBase64 or publicKeyBase64 can be empty, but not both.
func NewSM2FromHex ¶
NewSM2FromHex creates a new SM2 cipher from hex-encoded keys. Either privateKeyHex or publicKeyHex can be empty, but not both.
func NewSM2FromPEM ¶
NewSM2FromPEM creates a new SM2 cipher from PEM-encoded keys. Either privatePEM or publicPEM can be nil, but not both.
func NewSM4 ¶
NewSM4 creates a new SM4 cipher with the given key, IV, and optional mode. Key must be 16 bytes (128 bits). For CBC mode: IV must be 16 bytes. For ECB mode: IV is not used. If mode is not specified, defaults to SM4ModeCBC.
func NewSM4FromBase64 ¶
NewSM4FromBase64 creates a new SM4 cipher from base64-encoded key and IV strings. If mode is not specified, defaults to SM4ModeCBC.
type Decryptor ¶
type Decryptor interface {
// 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)
}
Decryptor defines the interface for decrypting ciphertext data.
type Encryptor ¶
type Encryptor 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)
}
Encryptor defines the interface for encrypting plaintext data.
type RSACipher ¶
type RSACipher struct {
// contains filtered or unexported fields
}
RSACipher implements Cipher interface using RSA encryption.
type SM2Cipher ¶
type SM2Cipher struct {
// contains filtered or unexported fields
}
SM2Cipher implements Cipher interface using SM2 encryption (国密算法).