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, opts ...ECIESOption) (Cipher, error)
- func NewECIESFromBase64(privateKeyBase64, publicKeyBase64 string, curve ECIESCurve, ...) (Cipher, error)
- func NewECIESFromBytes(privateKeyBytes, publicKeyBytes []byte, curve ECIESCurve, opts ...ECIESOption) (Cipher, error)
- func NewECIESFromHex(privateKeyHex, publicKeyHex string, curve ECIESCurve, opts ...ECIESOption) (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 ECDSAOption
- type ECIESCurve
- type ECIESOption
- type RSAMode
- type RSAOption
- type RSASignMode
- type SM2Cipher
- type SM4Mode
- type SM4Option
- type Signer
- func NewECDSA(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, opts ...ECDSAOption) (Signer, error)
- func NewECDSAFromBase64(privateKeyBase64, publicKeyBase64 string, opts ...ECDSAOption) (Signer, error)
- func NewECDSAFromHex(privateKeyHex, publicKeyHex string, opts ...ECDSAOption) (Signer, error)
- func NewECDSAFromPem(privatePem, publicPem []byte, opts ...ECDSAOption) (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 NewECIES ¶
func NewECIES(privateKey *ecdh.PrivateKey, publicKey *ecdh.PublicKey, opts ...ECIESOption) (Cipher, error)
func NewECIESFromBase64 ¶
func NewECIESFromBase64(privateKeyBase64, publicKeyBase64 string, curve ECIESCurve, opts ...ECIESOption) (Cipher, error)
func NewECIESFromBytes ¶
func NewECIESFromBytes(privateKeyBytes, publicKeyBytes []byte, curve ECIESCurve, opts ...ECIESOption) (Cipher, error)
func NewECIESFromHex ¶
func NewECIESFromHex(privateKeyHex, publicKeyHex string, curve ECIESCurve, opts ...ECIESOption) (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 ¶
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 ECDSAOption ¶
type ECDSAOption func(*ecdsaCipher)
type ECIESCurve ¶
type ECIESCurve string
const ( EciesCurveP256 ECIESCurve = "P256" EciesCurveP384 ECIESCurve = "P384" EciesCurveP521 ECIESCurve = "P521" EciesCurveX25519 ECIESCurve = "X25519" )
type ECIESOption ¶
type ECIESOption func(*eciesCipher)
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 NewECDSA ¶
func NewECDSA(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, opts ...ECDSAOption) (Signer, error)
func NewECDSAFromBase64 ¶
func NewECDSAFromBase64(privateKeyBase64, publicKeyBase64 string, opts ...ECDSAOption) (Signer, error)
func NewECDSAFromHex ¶
func NewECDSAFromHex(privateKeyHex, publicKeyHex string, opts ...ECDSAOption) (Signer, error)
func NewECDSAFromPem ¶
func NewECDSAFromPem(privatePem, publicPem []byte, opts ...ECDSAOption) (Signer, error)
Click to show internal directories.
Click to hide internal directories.