Documentation ¶
Overview ¶
Package crypto 加密解密
Index ¶
- Constants
- Variables
- func FillBase64(s string) string
- func GetMD5(text string) string
- func GetRandom(l int) []byte
- func GetSHA1(text string) string
- func GetSHA256(text string) string
- func GetSHA512(text string) string
- func GetSM3(text string) string
- func TLSConfigFromFile(certfile, keyfile, rootfile string) (*tls.Config, error)
- func TLSConfigFromPEM(certpem, keypem, rootpem []byte) (*tls.Config, error)
- type AES
- func (w *AES) Decode(b []byte) (string, error)
- func (w *AES) DecodeBase64(s string) (string, error)
- func (w *AES) Decrypt(s string) string
- func (w *AES) Encode(b []byte) (CValue, error)
- func (w *AES) Encrypt(s string) string
- func (w *AES) EncryptTo(s string) CValue
- func (w *AES) SetKeyIV(key, iv string) error
- func (w *AES) SetPadding(p Padding)
- type AESType
- type CValue
- type CertOpt
- type CompressType
- type Compressor
- type ECC
- func (w *ECC) CreateCert(opt *CertOpt) error
- func (w *ECC) Decode(b []byte) (string, error)
- func (w *ECC) DecodeBase64(s string) (string, error)
- func (w *ECC) Decrypt(s string) string
- func (w *ECC) Encode(b []byte) (CValue, error)
- func (w *ECC) Encrypt(s string) string
- func (w *ECC) EncryptTo(s string) CValue
- func (w *ECC) GenerateKey(ec ECShortName) (CValue, CValue, error)
- func (w *ECC) Keys() (CValue, CValue)
- func (w *ECC) SetPrivateKey(key string) error
- func (w *ECC) SetPrivateKeyFromFile(keyPath string) error
- func (w *ECC) SetPublicKey(key string) error
- func (w *ECC) SetPublicKeyFromFile(keyPath string) error
- func (w *ECC) Sign(b []byte) (CValue, error)
- func (w *ECC) ToFile(pubfile, prifile string) error
- func (w *ECC) VerifySign(signature, data []byte) (bool, error)
- func (w *ECC) VerifySignFromBase64(signature string, data []byte) (bool, error)
- func (w *ECC) VerifySignFromHex(signature string, data []byte) (bool, error)
- type ECShortName
- type HASH
- type HashType
- type Padding
- type RSA
- func (w *RSA) CreateCert(opt *CertOpt) error
- func (w *RSA) Decode(b []byte) (string, error)
- func (w *RSA) DecodeBase64(s string) (string, error)
- func (w *RSA) Decrypt(s string) string
- func (w *RSA) Encode(b []byte) (CValue, error)
- func (w *RSA) Encrypt(s string) string
- func (w *RSA) EncryptTo(s string) CValue
- func (w *RSA) GenerateKey(bits RSABits) (CValue, CValue, error)
- func (w *RSA) Keys() (CValue, CValue)
- func (w *RSA) SetPrivateKey(key string) error
- func (w *RSA) SetPrivateKeyFromFile(keyPath string) error
- func (w *RSA) SetPublicKey(key string) error
- func (w *RSA) SetPublicKeyFromFile(keyPath string) error
- func (w *RSA) Sign(b []byte) (CValue, error)
- func (w *RSA) ToFile(pubfile, prifile string) error
- func (w *RSA) VerifySign(signature, data []byte) (bool, error)
- func (w *RSA) VerifySignFromBase64(signature string, data []byte) (bool, error)
- func (w *RSA) VerifySignFromHex(signature string, data []byte) (bool, error)
- type RSABits
- type SM2
- func (w *SM2) CreateCert(opt *CertOpt) error
- func (w *SM2) Decode(b []byte) (string, error)
- func (w *SM2) DecodeBase64(s string) (string, error)
- func (w *SM2) Decrypt(s string) string
- func (w *SM2) Encode(b []byte) (CValue, error)
- func (w *SM2) EncodeAsn1(b []byte) (CValue, error)
- func (w *SM2) Encrypt(s string) string
- func (w *SM2) EncryptTo(s string) CValue
- func (w *SM2) GenerateKey() (CValue, CValue, error)
- func (w *SM2) Keys() (CValue, CValue)
- func (w *SM2) SetPrivateKey(key string) error
- func (w *SM2) SetPrivateKeyFromFile(keyPath string) error
- func (w *SM2) SetPublicKey(key string) error
- func (w *SM2) SetPublicKeyFromFile(keyPath string) error
- func (w *SM2) Sign(b []byte) (CValue, error)
- func (w *SM2) ToFile(pubfile, prifile string) error
- func (w *SM2) VerifySign(signature, data []byte) (bool, error)
- func (w *SM2) VerifySignFromBase64(signature string, data []byte) (bool, error)
- func (w *SM2) VerifySignFromHex(signature string, data []byte) (bool, error)
- type SM4
- func (w *SM4) Decode(b []byte) (string, error)
- func (w *SM4) DecodeBase64(s string) (string, error)
- func (w *SM4) Decrypt(s string) string
- func (w *SM4) Encode(b []byte) (CValue, error)
- func (w *SM4) Encrypt(s string) string
- func (w *SM4) EncryptTo(s string) CValue
- func (w *SM4) SetKeyIV(key, iv []byte) error
- type SM4Type
Constants ¶
const SM4BlockSize = 16
Variables ¶
var EmptyValue = CValue([]byte{})
Functions ¶
func TLSConfigFromFile ¶
TLSConfigFromFile 从文件载入证书
Types ¶
type AES ¶
type AES struct {
// contains filtered or unexported fields
}
AES aes算法
func (*AES) DecodeBase64 ¶
DecodeBase64 aes解密base64编码的字符串
type AESType ¶
type AESType byte
const ( // AES128CBC aes128cbc算法 AES128CBC AESType = iota // AES192CBC aes192cbc算法 AES192CBC // AES256CBC aes256cbc算法 AES256CBC // AES128CFB aes128cfb算法 AES128CFB // AES192CFB aes192cfb算法 AES192CFB // AES256CFB aes256cfb算法 AES256CFB // AES128ECB aes128ecb算法 AES128ECB // AES192ECB aes192ecb算法 AES192ECB // AES256ECB aes256ecb算法 AES256ECB )
type CValue ¶
type CValue []byte
CValue 加密后的数据,可输出[]byte,hex string,base64string
func (CValue) Base64String ¶
Base64String 加密结果以标准base64字符串形式输出
func (CValue) Base64StringNoTail ¶
Base64StringNoTail 加密结果以标准base64字符串形式输出,去除`=`
func (CValue) URLBase64String ¶
URLBase64String 加密结果以URLbase64字符串形式输出
type CompressType ¶
type CompressType byte
CompressType 压缩编码类型
const ( CompressZlib CompressType = iota CompressGZip CompressSnappy CompressZstd )
type Compressor ¶
type Compressor struct {
// contains filtered or unexported fields
}
func NewCompressor ¶
func NewCompressor(t CompressType) *Compressor
type ECC ¶
type ECC struct {
// contains filtered or unexported fields
}
ECC ecc算法
func NewECC ¶
func NewECC() *ECC
NewECC 创建一个新的ecc算法器
签名算法采用sha256 支持 openssl ecparam -name prime256v1/secp384r1 格式的密钥
func (*ECC) CreateCert ¶
CreateCert 创建基于ecc算法的数字证书,opt.RootKey无效时,会重新创建私钥和根证书
func (*ECC) DecodeBase64 ¶
DecodeBase64 从base64字符串解码
func (*ECC) GenerateKey ¶
func (w *ECC) GenerateKey(ec ECShortName) (CValue, CValue, error)
GenerateKey 创建ecc密钥对
返回,pubkey,prikey,error
func (*ECC) SetPrivateKey ¶
SetPrivateKey 设置base64编码的私钥
func (*ECC) SetPrivateKeyFromFile ¶
SetPrivateKeyFromFile 从文件获取私钥
func (*ECC) SetPublicKeyFromFile ¶
SetPublicKeyFromFile 从文件获取公钥
func (*ECC) VerifySign ¶
VerifySign 验证签名
func (*ECC) VerifySignFromBase64 ¶
VerifySignFromBase64 验证base64格式的签名
type ECShortName ¶
type ECShortName byte
var ( // ECPrime256v1 as elliptic.P256() and openssl ecparam -name prime256v1 ECPrime256v1 ECShortName = 1 // ECSecp384r1 as elliptic.P384() and openssl ecparam -name secp384r1 ECSecp384r1 ECShortName = 2 )
type HASH ¶
type HASH struct {
// contains filtered or unexported fields
}
HASH hash算法
func NewHash ¶
NewHash creates a new hash algorithm instance based on the provided hash type and HMAC key. It uses a sync.Pool to reuse hash instances, improving performance.
Parameters: - t: The hash type, which can be one of the following: HashMD5, HashHMACSHA1, HashHMACSHA256, HashSHA1, HashSHA256, HashSHA512, HashSM3. - hmacKey: The HMAC key to be used for HashHMACSHA1 and HashHMACSHA256 hash types. If not required, an empty slice can be passed.
Returns: - A pointer to the newly created HASH instance.
type RSA ¶
type RSA struct {
// contains filtered or unexported fields
}
RSA rsa算法
func (*RSA) CreateCert ¶
CreateCert 创建基于rsa算法的数字证书,opt.RootKey无效时,会重新创建私钥和根证书
func (*RSA) DecodeBase64 ¶
DecodeBase64 从base64字符串解码
func (*RSA) SetPrivateKey ¶
SetPrivateKey 设置base64编码的私钥
func (*RSA) SetPrivateKeyFromFile ¶
SetPrivateKeyFromFile 从文件获取私钥
func (*RSA) SetPublicKeyFromFile ¶
SetPublicKeyFromFile 从文件获取公钥
func (*RSA) VerifySign ¶
VerifySign 验证签名
func (*RSA) VerifySignFromBase64 ¶
VerifySignFromBase64 验证base64格式的签名
type SM2 ¶
type SM2 struct {
// contains filtered or unexported fields
}
func (*SM2) CreateCert ¶
CreateCert 创建基于sm2算法的数字证书,opt.RootKey无效时,会重新创建私钥和根证书
func (*SM2) DecodeBase64 ¶
DecodeBase64 从base64字符串解码
func (*SM2) SetPrivateKey ¶
SetPrivateKey 设置base64编码的私钥
func (*SM2) SetPrivateKeyFromFile ¶
SetPrivateKeyFromFile 从文件获取私钥
func (*SM2) SetPublicKeyFromFile ¶
SetPublicKeyFromFile 从文件获取公钥
func (*SM2) VerifySign ¶
VerifySign 验证签名
func (*SM2) VerifySignFromBase64 ¶
VerifySignFromBase64 验证base64格式的签名
type SM4 ¶
type SM4 struct {
// contains filtered or unexported fields
}
sm4
func (*SM4) DecodeBase64 ¶
DecodeBase64 解密base64编码的字符串