Documentation
¶
Index ¶
- Variables
- func Decrypt(data, secret string, passwordBased bool, config CipherConfig) (string, error)
- func Encrypt(data, secret string, passwordBased bool, config CipherConfig) (string, error)
- func FormatPrivateKeyPEM(rawKey string) string
- func FormatPublicKeyPEM(rawKey string) string
- func GenerateRandomBytes(length int) ([]byte, error)
- func ParseRSAPrivateKey(keyBytes []byte) (*rsa.PrivateKey, error)
- func ParseRSAPublicKey(keyBytes []byte) (*rsa.PublicKey, error)
- func PasswordHash(password string, cost int) (string, error)
- func PasswordVerify(password, hash string) bool
- func RSASign(data, privateKey string, keyFromFile bool) (string, error)
- func RSAVerify(data, sign, publicKey string, keyFromFile bool) (bool, error)
- type AES
- type CipherConfig
- type IDCipher
- type Input
- type SM4
Constants ¶
This section is empty.
Variables ¶
var DefaultCipherConfig = CipherConfig{ Cipher: "AES-128-CBC", AllowedCiphers: map[string][]int{ "AES-128-CBC": {16, 16}, "AES-192-CBC": {16, 24}, "AES-256-CBC": {16, 32}, }, KdfHash: "sha256", MacHash: "sha256", AuthKeyInfo: "AuthorizationKey", DerivationIterations: 100000, }
DefaultCipherConfig is the default cipher configuration
Functions ¶
func Decrypt ¶
func Decrypt(data, secret string, passwordBased bool, config CipherConfig) (string, error)
Decrypt decrypts data using a password or a key
func Encrypt ¶
func Encrypt(data, secret string, passwordBased bool, config CipherConfig) (string, error)
Encrypt encrypts data using a password or a key
func FormatPrivateKeyPEM ¶ added in v0.16.0
FormatPrivateKeyPEM 将裸 base64 私钥串包装为标准 PKCS1 PEM 文本。
参数:
- rawKey: 不含 PEM 头尾的 base64 私钥串(可含空格,内部会清理)
返回值:
- string: 标准 PEM 格式私钥文本(每行 64 字符)
示例:
pemText := security.FormatPrivateKeyPEM("MIIEpAIBAAK...")
func FormatPublicKeyPEM ¶ added in v0.16.0
FormatPublicKeyPEM 将裸 base64 公钥串包装为标准 X.509 SubjectPublicKeyInfo PEM 文本。
参数:
- rawKey: 不含 PEM 头尾的 base64 公钥串(可含空格,内部会清理)
返回值:
- string: 标准 PEM 格式公钥文本(每行 64 字符)
示例:
pemText := security.FormatPublicKeyPEM("MIIBIjANBgkq...")
func GenerateRandomBytes ¶
GenerateRandomBytes generates random bytes of specified length
func ParseRSAPrivateKey ¶ added in v0.16.0
func ParseRSAPrivateKey(keyBytes []byte) (*rsa.PrivateKey, error)
ParseRSAPrivateKey 解析私钥字节为 rsa.PrivateKey。
自动兼容:PKCS1 PEM、PKCS8 PEM、裸 base64 串(自动包装为 PKCS1 PEM)。
参数:
- keyBytes: 私钥内容字节
返回值:
- *rsa.PrivateKey: 解析后的私钥对象
- error: 无法识别的格式时返回错误
func ParseRSAPublicKey ¶ added in v0.16.0
ParseRSAPublicKey 解析公钥字节为 rsa.PublicKey。
自动兼容:X.509 SubjectPublicKeyInfo PEM("PUBLIC KEY")、PKCS1 PEM("RSA PUBLIC KEY")、 裸 base64 串(自动包装为 "PUBLIC KEY" PEM)。
参数:
- keyBytes: 公钥内容字节
返回值:
- *rsa.PublicKey: 解析后的公钥对象
- error: 无法识别的格式时返回错误
func PasswordHash ¶
PasswordHash generates a hash of the password
func PasswordVerify ¶
PasswordVerify validates the password against a given hash
func RSASign ¶ added in v0.16.0
RSASign 使用 RSA 私钥对待签名内容进行 SHA256withRSA(RSA2)签名。
兼容三种私钥来源:
- 裸 base64 私钥串(无 PEM 头尾、无换行),自动按 PKCS1 包装;
- 标准 PEM 文本(PKCS1 "RSA PRIVATE KEY" 或 PKCS8 "PRIVATE KEY");
- 私钥文件路径(keyFromFile=true 时按文件读取,内容同样支持上述两种格式)。
参数:
- data: 待签名的原始字符串(如支付平台签名串)
- privateKey: 私钥字符串或私钥文件路径
- keyFromFile: true 表示 privateKey 为文件路径
返回值:
- string: base64 编码后的签名值
- error: 私钥格式错误或签名失败时返回错误
示例:
sign, err := security.RSASign("待签名内容", "MIIEpAIBAAK...", false)
func RSAVerify ¶ added in v0.16.0
RSAVerify 使用公钥对签名进行 SHA256withRSA(RSA2)验签。
兼容三种公钥来源:
- 裸 base64 公钥串(无 PEM 头尾、无换行),自动按 X.509 SubjectPublicKeyInfo 包装;
- 标准 PEM 公钥文本("PUBLIC KEY" 或 "RSA PUBLIC KEY");
- 公钥文件路径(keyFromFile=true 时按文件读取)。
参数:
- data: 被签名的原始内容
- sign: base64 编码的签名值
- publicKey: 公钥字符串或公钥文件路径
- keyFromFile: true 表示 publicKey 为文件路径
返回值:
- bool: true 验签通过;false 验签不通过
- error: 公钥格式错误等异常情况(验签不通过不算异常,返回 false,nil)
示例:
ok, err := security.RSAVerify("待验签内容", sign, platformPublicKey, false)
Types ¶
type AES ¶
type AES struct {
Text string `json:"text" default:""`
Key string `json:"key" default:"jcbase.aes_key__"`
Iv string `json:"iv" default:"jcbase.aes_iv___"`
}
type CipherConfig ¶
type CipherConfig struct {
Cipher string
AllowedCiphers map[string][]int
KdfHash string
MacHash string
AuthKeyInfo string
DerivationIterations int
}
CipherConfig stores the cipher configuration
type IDCipher ¶ added in v0.13.24
type IDCipher struct {
// contains filtered or unexported fields
}
IDCipher uint64 ID 加解密器。
底层复用 SM4(CBC + 标准 Base64),并在标准 Base64 基础上做 URL-safe 字符替换 (+ → -、/ → _)且保留 = 填充。注意:SM4 自带的 Encoding:"RawURL" 会去掉填充, 若业务需要保留填充,应使用 "Std" 编码并依赖本实现的字符替换。
func NewIDCipher ¶ added in v0.13.24
NewIDCipher 创建 ID 加解密器。 sm4 需预先配置 Mode/Encoding(Key/Iv 可留空,加解密时会应用默认值)。 若需要保留 base64 填充,sm4.Encoding 应使用 "Std"。
func (*IDCipher) DecryptUint64 ¶ added in v0.13.24
DecryptUint64 解密 URL-safe base64 字符串为 uint64(兼容 URL-safe 与标准 base64 两种字符集)。 解密前将 URL-safe 字符归一化为标准 base64;标准 base64 密文不含 - 和 _,不受影响。
参数:
- cipherText: 加密后的 ID 字符串
返回值:
- uint64: 解密后的 ID
- error: SM4 未配置、解密失败或结果无法解析为 uint64 时返回错误
type Input ¶
type Input struct {
Value interface{}
DefaultValue interface{}
}
type SM4 ¶ added in v0.10.0
type SM4 struct {
Text string `json:"text" default:""` // 待加密/解密的文本内容,实际加密或解密操作将基于该文本进行
Key string `json:"key" default:"jcbase.sm4_key__"` // 加密密钥,SM4算法要求密钥长度必须为16字节,用于加密和解密数据
Iv string `json:"iv" default:"jcbase.sm4_iv___"` // 初始化向量(Initialization Vector),SM4算法要求IV长度必须为16字节,用于增强加密安全性
Mode string `json:"mode" default:"CBC"` // 加密模式,可选值包括:CBC、GCM、CFB、OFB、CTR,指定加密操作使用的具体模式
Encoding string `json:"encoding" default:"Std"` // 输出/输入编码格式,可选值包括:Std(标准Base64)、Raw(无填充Base64)、RawURL(URL安全无填充Base64)、Hex(十六进制)
}
SM4 SM4加解密结构体 提供SM4对称加密算法的加解密功能
func (SM4) Decrypt ¶ added in v0.10.0
Decrypt 通用解密方法,根据Mode字段选择解密模式 参数:
- plaintext: 解密后的明文输出指针
返回:
- error: 解密失败时的错误信息
func (SM4) DecryptCBC ¶ added in v0.10.0
DecryptCBC 使用CBC模式解密数据 参数:
- plaintext: 解密后的明文输出指针
返回:
- error: 解密失败时的错误信息
func (SM4) DecryptGCM ¶ added in v0.10.0
DecryptGCM 使用GCM模式解密数据 参数:
- plaintext: 解密后的明文输出指针
返回:
- error: 解密失败时的错误信息
func (SM4) Encrypt ¶ added in v0.10.0
Encrypt 通用加密方法,根据Mode字段选择加密模式 参数:
- cipherText: 加密后的密文输出指针
返回:
- error: 加密失败时的错误信息
func (SM4) EncryptCBC ¶ added in v0.10.0
EncryptCBC 使用CBC模式加密数据 参数:
- cipherText: 加密后的密文输出指针
返回:
- error: 加密失败时的错误信息
func (SM4) EncryptGCM ¶ added in v0.10.0
EncryptGCM 使用GCM模式加密数据(推荐使用,提供认证加密) 参数:
- cipherText: 加密后的密文输出指针
返回:
- error: 加密失败时的错误信息