Documentation
¶
Index ¶
- Variables
- func GenerateECDSAKey(curve ECDSACurve) (*ecdsa.PrivateKey, error)
- func GenerateECIESKey(curve ECIESCurve) (*ecdh.PrivateKey, error)
- type AESMode
- type AESOption
- type Cipher
- func NewAES(key []byte, opts ...AESOption) (Cipher, error)
- func NewAESFromBase64(keyBase64 string, opts ...AESOption) (Cipher, error)
- func NewAESFromHex(keyHex string, opts ...AESOption) (Cipher, error)
- func NewECIES(privateKey *ecdh.PrivateKey, publicKey *ecdh.PublicKey) (Cipher, error)
- func NewECIESFromBase64(privateKeyBase64, publicKeyBase64 string, curve ECIESCurve) (Cipher, error)
- func NewECIESFromBytes(privateKeyBytes, publicKeyBytes []byte, curve ECIESCurve) (Cipher, error)
- func NewECIESFromHex(privateKeyHex, publicKeyHex string, curve ECIESCurve) (Cipher, error)
- func NewSM4(key []byte, opts ...SM4Option) (Cipher, error)
- func NewSM4FromBase64(keyBase64 string, opts ...SM4Option) (Cipher, error)
- func NewSM4FromHex(keyHex string, opts ...SM4Option) (Cipher, error)
- type CipherSigner
- func NewRSA(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, opts ...RSAOption) (CipherSigner, error)
- func NewRSAFromBase64(privateKeyBase64, publicKeyBase64 string, opts ...RSAOption) (CipherSigner, error)
- func NewRSAFromHex(privateKeyHex, publicKeyHex string, opts ...RSAOption) (CipherSigner, error)
- func NewRSAFromPEM(privatePEM, publicPEM []byte, opts ...RSAOption) (CipherSigner, error)
- func NewSM2(privateKey *sm2.PrivateKey, publicKey *sm2.PublicKey) (CipherSigner, error)
- func NewSM2FromBase64(privateKeyBase64, publicKeyBase64 string) (CipherSigner, error)
- func NewSM2FromHex(privateKeyHex, publicKeyHex string) (CipherSigner, error)
- func NewSM2FromPEM(privatePEM, publicPEM []byte) (CipherSigner, error)
- type ECDSACurve
- type ECIESCurve
- type RSAMode
- type RSAOption
- type RSASignMode
- type SM4Mode
- type SM4Option
- type Signer
- func NewECDSA(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) (Signer, error)
- func NewECDSAFromBase64(privateKeyBase64, publicKeyBase64 string) (Signer, error)
- func NewECDSAFromHex(privateKeyHex, publicKeyHex string) (Signer, error)
- func NewECDSAFromPEM(privatePEM, publicPEM []byte) (Signer, error)
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 ¶
func GenerateECDSAKey(curve ECDSACurve) (*ecdsa.PrivateKey, error)
func GenerateECIESKey ¶
func GenerateECIESKey(curve ECIESCurve) (*ecdh.PrivateKey, error)
Types ¶
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 NewECIESFromBase64 ¶
func NewECIESFromBase64(privateKeyBase64, publicKeyBase64 string, curve ECIESCurve) (Cipher, error)
func NewECIESFromBytes ¶
func NewECIESFromBytes(privateKeyBytes, publicKeyBytes []byte, curve ECIESCurve) (Cipher, error)
func NewECIESFromHex ¶
func NewECIESFromHex(privateKeyHex, publicKeyHex string, curve ECIESCurve) (Cipher, error)
type CipherSigner ¶
CipherSigner defines the interface for encryption, decryption, signing, and verifying.
func NewRSA ¶
func NewRSA(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, opts ...RSAOption) (CipherSigner, error)
func NewRSAFromBase64 ¶
func NewRSAFromBase64(privateKeyBase64, publicKeyBase64 string, opts ...RSAOption) (CipherSigner, error)
func NewRSAFromHex ¶
func NewRSAFromHex(privateKeyHex, publicKeyHex string, opts ...RSAOption) (CipherSigner, error)
func NewRSAFromPEM ¶ added in v0.28.0
func NewRSAFromPEM(privatePEM, publicPEM []byte, opts ...RSAOption) (CipherSigner, error)
func NewSM2 ¶
func NewSM2(privateKey *sm2.PrivateKey, publicKey *sm2.PublicKey) (CipherSigner, error)
func NewSM2FromBase64 ¶
func NewSM2FromBase64(privateKeyBase64, publicKeyBase64 string) (CipherSigner, error)
func NewSM2FromHex ¶
func NewSM2FromHex(privateKeyHex, publicKeyHex string) (CipherSigner, error)
func NewSM2FromPEM ¶
func NewSM2FromPEM(privatePEM, publicPEM []byte) (CipherSigner, error)
type ECDSACurve ¶
type ECDSACurve string
const ( EcdsaCurveP224 ECDSACurve = "P224" EcdsaCurveP256 ECDSACurve = "P256" EcdsaCurveP384 ECDSACurve = "P384" EcdsaCurveP521 ECDSACurve = "P521" )
type ECIESCurve ¶
type ECIESCurve string
const ( EciesCurveP256 ECIESCurve = "P256" EciesCurveP384 ECIESCurve = "P384" EciesCurveP521 ECIESCurve = "P521" EciesCurveX25519 ECIESCurve = "X25519" )
type RSAOption ¶
type RSAOption func(*rsaCipher)
func WithRSAMode ¶
func WithRSASignMode ¶
func WithRSASignMode(signMode RSASignMode) RSAOption
type RSASignMode ¶
type RSASignMode string
const ( RsaSignModePSS RSASignMode = "PSS" RsaSignModePKCS1v15 RSASignMode = "PKCS1v15" )
type Signer ¶
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 NewECDSAFromBase64 ¶
func NewECDSAFromHex ¶
func NewECDSAFromPEM ¶ added in v0.28.0
Click to show internal directories.
Click to hide internal directories.