Documentation
¶
Index ¶
- Variables
- func AesDecrypt(cryptedText, key, iv []byte) ([]byte, error)
- func AesEncrypt(plainText, key, iv []byte) ([]byte, error)
- func GenerateAESKey(length int) ([]byte, error)
- func GenerateSM4Key() ([]byte, error)
- func PKCS5Padding(plaintext []byte, blockSize int) []byte
- func PKCS5UnPadding(origData []byte) []byte
- func SHA256Sum(data []byte) []byte
- func SHA512Sum(data []byte) []byte
- type AESCipher
- type AESGCMCipher
- type Cipher
- type ECDHCipher
- type ECDSACipher
- type HMAC
- type Hasher
- type KeyExchanger
- type RSACipher
- func (r *RSACipher) Decrypt(ciphertext []byte) ([]byte, error)
- func (r *RSACipher) Encrypt(plain []byte) ([]byte, error)
- func (r *RSACipher) ExportPrivateKey() (string, error)
- func (r *RSACipher) ExportPublicKey() (string, error)
- func (r *RSACipher) Name() string
- func (r *RSACipher) PrivateKey() *rsa.PrivateKey
- func (r *RSACipher) PublicKey() *rsa.PublicKey
- type SHA256Hasher
- type SHA512Hasher
- type SM2Cipher
- func (s *SM2Cipher) Decrypt(ciphertext []byte) ([]byte, error)
- func (s *SM2Cipher) DecryptAsn1(ciphertext []byte) ([]byte, error)
- func (s *SM2Cipher) Encrypt(plain []byte) ([]byte, error)
- func (s *SM2Cipher) EncryptAsn1(plain []byte) ([]byte, error)
- func (s *SM2Cipher) Name() string
- func (s *SM2Cipher) PrivateKey() *sm2.PrivateKey
- func (s *SM2Cipher) PublicKey() *sm2.PublicKey
- func (s *SM2Cipher) Sign(digest []byte) (string, error)
- func (s *SM2Cipher) Verify(data []byte, signature string) (bool, error)
- type SM3Hasher
- type SM4Cipher
- type Signer
- type Verifier
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultAESKey = []byte("f51d66a73d8a0927")
DefaultAESKey 默认AES密钥(16字节)
Functions ¶
Types ¶
type AESCipher ¶
type AESCipher struct {
// contains filtered or unexported fields
}
func NewAESCipher ¶
type AESGCMCipher ¶ added in v0.0.2
type AESGCMCipher struct {
// contains filtered or unexported fields
}
func NewAESGCMCipher ¶ added in v0.0.2
func NewAESGCMCipher(key []byte) (*AESGCMCipher, error)
NewAESGCMCipher 创建AES-GCM模式的Cipher
func (*AESGCMCipher) Decrypt ¶ added in v0.0.2
func (a *AESGCMCipher) Decrypt(ciphertext []byte) ([]byte, error)
Decrypt 使用AES-GCM解密
func (*AESGCMCipher) Encrypt ¶ added in v0.0.2
func (a *AESGCMCipher) Encrypt(plain []byte) ([]byte, error)
Encrypt 使用AES-GCM加密
func (*AESGCMCipher) Name ¶ added in v0.0.2
func (a *AESGCMCipher) Name() string
type Cipher ¶
type Cipher interface {
// Encrypt 使用指定的密钥和初始化向量(IV)加密明文数据,返回加密后的数据
Encrypt(plain []byte) ([]byte, error)
// Decrypt 使用指定的密钥和初始化向量(IV)解密密文数据,返回解密后的数据
Decrypt(cipher []byte) ([]byte, error)
// Name 返回算法名称
Name() string
}
Cipher 定义了加密算法的接口,支持加密和解密操作
type ECDHCipher ¶
type ECDHCipher struct {
// contains filtered or unexported fields
}
func (*ECDHCipher) DeriveSharedSecret ¶
func (e *ECDHCipher) DeriveSharedSecret(peerPubBytes []byte) ([]byte, error)
DeriveSharedSecret 计算共享密钥
func (*ECDHCipher) PrivateKey ¶
func (e *ECDHCipher) PrivateKey() *ecdh.PrivateKey
PrivateKey 获取私钥(仅用于安全存储,禁止对外泄露)
func (*ECDHCipher) PublicKey ¶
func (e *ECDHCipher) PublicKey() *ecdh.PublicKey
PublicKey 获取公钥(用于导出、传输给对方)
func (*ECDHCipher) PublicKeyBytes ¶
func (e *ECDHCipher) PublicKeyBytes() []byte
PublicKeyBytes 获取公钥字节
type ECDSACipher ¶
type ECDSACipher struct {
// contains filtered or unexported fields
}
func (*ECDSACipher) PrivateKey ¶
func (e *ECDSACipher) PrivateKey() *ecdsa.PrivateKey
func (*ECDSACipher) PublicKeyBytes ¶
func (e *ECDSACipher) PublicKeyBytes() []byte
PublicKeyBytes 公钥/私钥导出
type Hasher ¶
type Hasher interface {
// Sum 计算输入数据的哈希值,返回哈希结果
Sum(data []byte) ([]byte, error)
// Name 返回哈希算法名称
Name() string
}
Hasher 定义了哈希算法的接口,支持计算数据的哈希值
type KeyExchanger ¶
type KeyExchanger interface {
DeriveSharedSecret(peerPubBytes []byte) ([]byte, error)
// PublicKeyBytes 获取本地公钥字节
PublicKeyBytes() []byte
}
KeyExchanger 定义了密钥协商算法的接口 适用于ECDH、SM2密钥交换等场景
type RSACipher ¶
type RSACipher struct {
// contains filtered or unexported fields
}
func NewRSACipherFromKey ¶
func NewRSACipherFromKey(priv *rsa.PrivateKey, pub *rsa.PublicKey) *RSACipher
NewRSACipherFromKey 用已有密钥初始化
func (*RSACipher) ExportPrivateKey ¶
ExportPrivateKey 导出私钥PEM
func (*RSACipher) ExportPublicKey ¶
ExportPublicKey 导出公钥PEM
type SHA256Hasher ¶
type SHA256Hasher struct{}
func (*SHA256Hasher) Name ¶
func (h *SHA256Hasher) Name() string
type SHA512Hasher ¶
type SHA512Hasher struct{}
func (*SHA512Hasher) Name ¶
func (h *SHA512Hasher) Name() string
type SM2Cipher ¶
type SM2Cipher struct {
// contains filtered or unexported fields
}
SM2Cipher 实现 Cipher 接口,支持国密SM2加解密 仅适用于小数据块加密(如密钥交换),不适合大数据流 生产环境请妥善管理私钥
func NewSM2CipherFromKey ¶
func NewSM2CipherFromKey(priv *sm2.PrivateKey, pub *sm2.PublicKey) *SM2Cipher
NewSM2CipherFromKey 用已有密钥初始化
func (*SM2Cipher) DecryptAsn1 ¶
DecryptAsn1 解密ASN.1格式密文
func (*SM2Cipher) EncryptAsn1 ¶
EncryptAsn1 加密并输出ASN.1格式(更通用,兼容多数平台)
func (*SM2Cipher) PrivateKey ¶
func (s *SM2Cipher) PrivateKey() *sm2.PrivateKey
PrivateKey 获取私钥(仅用于安全存储,禁止对外泄露)
type SM4Cipher ¶
type SM4Cipher struct {
// contains filtered or unexported fields
}
SM4Cipher 实现国密SM4对称加密算法(ECB模式,适合小数据块)
Click to show internal directories.
Click to hide internal directories.