Documentation ¶
Overview ¶
* @Author: kamalyes 501893067@qq.com * @Date: 2024-10-23 17:37:08 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-10-23 17:58:07 * @FilePath: \go-toolbox\pkg\sign\aes.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 00:55:15 * @FilePath: \go-toolbox\pkg\sign\base.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 00:55:15 * @FilePath: \go-toolbox\pkg\sign\hash.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
* @Author: kamalyes 501893067@qq.com * @Date: 2024-11-10 21:51:58 * @LastEditors: kamalyes 501893067@qq.com * @LastEditTime: 2024-11-11 00:55:15 * @FilePath: \go-toolbox\pkg\sign\rsa.go * @Description: * * Copyright (c) 2024 by kamalyes, All Rights Reserved.
Index ¶
- Constants
- Variables
- func AesDecrypt(cipherText string, key []byte) (string, error)
- func AesEncrypt(plainText string, key []byte) (string, error)
- func ExportRsaPrivateKeyToPEM(privateKey *rsa.PrivateKey) (string, error)
- func ExportRsaPublicKeyToPEM(publicKey *rsa.PublicKey) (string, error)
- func GenerateByteKey(password string, length int) []byte
- func HmacSha256Base64(message string, secret string) string
- func HmacSha256Hex(message string, secret string) string
- func ParsePrivateKey(content []byte) (*rsa.PrivateKey, error)
- func ParsePublicKey(pemData []byte) (*rsa.PublicKey, error)
- func SHA256(text string) string
- type GenericHasher
- type HashCryptoFunc
- type Hasher
- type RsaCrypto
- type RsaKeyPair
- type RsaKeySize
Constants ¶
const ( PrivateKeyType = "PRIVATE KEY" PublicKeyType = "PUBLIC KEY" )
PEM类型常量
Variables ¶
var ( ErrPrivateKey = errors.New("私钥文件错误") ErrPublicKey = errors.New("公钥解析错误") ErrEncryptFail = errors.New("加密失败") ErrDecryptFail = errors.New("解密失败") ErrPemBlockTypeFail = errors.New("PEM Block 类型不是PUBLIC KEY") ErrNotRsaPrivateKey = errors.New("不是RSA Private密钥") ErrNotRsaPublicKeyKey = errors.New("不是RSA Public密钥") ErrSaltEmpty = errors.New("盐值不能为空") // 新增盐值为空的错误 )
错误定义
var ErrUnsupportedAlgorithm = errors.New("不支持的哈希算法")
ErrUnsupportedAlgorithm 表示不支持的算法错误。
var SupportHashCryptoFunc = map[HashCryptoFunc]func() hash.Hash{ AlgorithmMD5: md5.New, AlgorithmSHA1: sha1.New, AlgorithmSHA224: sha256.New224, AlgorithmSHA256: sha256.New, AlgorithmSHA384: sha512.New384, AlgorithmSHA512: sha512.New, }
SupportHashCryptoFunc 支持的哈希算法映射
Functions ¶
func ExportRsaPrivateKeyToPEM ¶ added in v0.11.6
func ExportRsaPrivateKeyToPEM(privateKey *rsa.PrivateKey) (string, error)
ExportRsaPrivateKeyToPEM 将 RSA 私钥导出为 PEM 格式
func ExportRsaPublicKeyToPEM ¶ added in v0.11.6
ExportRsaPublicKeyToPEM 将 RSA 公钥导出为 PEM 格式
func GenerateByteKey ¶
GenerateByteKey 生成一个指定字节的密钥
func HmacSha256Base64 ¶
HmacSha256Base64 计算hmac
func HmacSha256Hex ¶
HmacSha256Hex 字符串计算sha256之后转hex
func ParsePrivateKey ¶ added in v0.11.6
func ParsePrivateKey(content []byte) (*rsa.PrivateKey, error)
ParsePrivateKey 解析PEM格式的私钥
func ParsePublicKey ¶ added in v0.11.6
ParsePublicKey 解析 PEM 格式的公钥
Types ¶
type GenericHasher ¶ added in v0.11.6
type GenericHasher struct {
// contains filtered or unexported fields
}
GenericHasher 结构体用于通用哈希计算。
type HashCryptoFunc ¶ added in v0.11.6
type HashCryptoFunc string
定义算法名称常量
const ( AlgorithmMD5 HashCryptoFunc = "MD5" AlgorithmSHA1 HashCryptoFunc = "SHA1" AlgorithmSHA224 HashCryptoFunc = "SHA224" AlgorithmSHA256 HashCryptoFunc = "SHA256" AlgorithmSHA384 HashCryptoFunc = "SHA384" AlgorithmSHA512 HashCryptoFunc = "SHA512" )
type Hasher ¶ added in v0.11.6
Hasher 接口定义了一个计算哈希值的方法。
func NewHasher ¶ added in v0.11.6
func NewHasher(algorithm HashCryptoFunc) (Hasher, error)
NewHasher 创建一个新的 GenericHasher 实例。
type RsaCrypto ¶ added in v0.11.6
type RsaCrypto interface { Encrypt(input []byte, salt []byte) ([]byte, error) Decrypt(input []byte) ([]byte, error) DecryptBase64(input string) ([]byte, error) GetPrivateKey() *rsa.PrivateKey GetPublicKey() *rsa.PublicKey }
RsaCrypto 定义了RSA加解密器的接口
func NewRsaCryptoFromKeys ¶ added in v0.11.6
func NewRsaCryptoFromKeys(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, hashFunc func() hash.Hash) RsaCrypto
NewRsaCryptoFromKeys 根据公钥和私钥创建RSA加解密器
func NewRsaCryptoFromPrivateFile ¶ added in v0.11.6
NewRsaCryptoFromPrivateFile 根据私钥文件创建RSA加解密器
type RsaKeyPair ¶ added in v0.11.6
type RsaKeyPair struct { PrivateKey *rsa.PrivateKey PublicKey *rsa.PublicKey }
RsaKeyPair 包含RSA密钥对
func GenerateRsaKeyPair ¶ added in v0.11.6
func GenerateRsaKeyPair(keySize RsaKeySize) (*RsaKeyPair, error)
GenerateRsaKeyPair 生成指定大小的 RSA 密钥对
type RsaKeySize ¶ added in v0.11.6
type RsaKeySize int
RsaKeySize 定义了支持的RSA密钥大小
const ( RsaKeySize512 RsaKeySize = 512 RsaKeySize1024 RsaKeySize = 1024 RsaKeySize2048 RsaKeySize = 2048 RsaKeySize4096 RsaKeySize = 4096 )