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 GenerateRandomBytes(length int) ([]byte, error)
- func PasswordHash(password string, cost int) (string, error)
- func PasswordVerify(password, hash string) bool
- 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 GenerateRandomBytes ¶
GenerateRandomBytes generates random bytes of specified length
func PasswordHash ¶
PasswordHash generates a hash of the password
func PasswordVerify ¶
PasswordVerify validates the password against a given hash
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: 加密失败时的错误信息