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 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)
- type Sm4Mode
- type Sm4Option
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 AesOption ¶ added in v0.12.0
type AesOption func(*aesCipher)
func WithAesMode ¶ added in v0.12.0
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 NewAesFromBase64 ¶ added in v0.12.0
func NewAesFromHex ¶ added in v0.12.0
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 NewSm4FromBase64 ¶ added in v0.12.0
type CipherSigner ¶ added in v0.12.0
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 RsaOption ¶ added in v0.12.0
type RsaOption func(*rsaCipher)
func WithRsaMode ¶ added in v0.12.0
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)
Click to show internal directories.
Click to hide internal directories.