Documentation
¶
Index ¶
- Constants
- func ClearBytes(data []byte)
- func DecryptAESGCM(ciphertext, key, nonce, authTag []byte) ([]byte, error)
- func DeriveKEK(password string, salt []byte) ([]byte, error)
- func DeriveKeyFromMnemonic(mnemonic string) ([]byte, error)
- func EncryptAESGCM(plaintext, key []byte) (ciphertext, nonce, authTag []byte, err error)
- func GenerateBIP39Mnemonic() (string, error)
- func GenerateRandomBytes(size int) ([]byte, error)
- func HashPassword(password string) (string, error)
- func HashRecoveryKey(recoveryKey []byte) string
- func IsMnemonicValid(mnemonic string) bool
- func ValidatePasswordStrength(password string) bool
- func VerifyPassword(password, hash string) bool
Constants ¶
const ( // AESKeySize AES-256密钥长度(32字节=256位) AESKeySize = 32 // GCMNonceSize GCM模式Nonce长度(12字节=96位) GCMNonceSize = 12 // GCMTagSize GCM模式认证标签长度(16字节=128位) GCMTagSize = 16 )
const ( // Argon2Time Argon2id迭代次数 Argon2Time = 3 // Argon2Memory Argon2id内存消耗(64MB) Argon2Memory = 64 * 1024 // Argon2Threads Argon2id并行度 Argon2Threads = 4 // Argon2KeyLength 输出密钥长度(32字节=256位) Argon2KeyLength = 32 )
Argon2id参数配置(平衡安全性和性能)
const ( // BcryptCost bcrypt加密强度,平衡性能和安全性 BcryptCost = 12 // MinPasswordLength 最小密码长度 MinPasswordLength = 8 )
const ( // EntropyBits 助记词熵位数,256位对应24个单词 EntropyBits = 256 // RecoverySalt 恢复密钥派生时使用的盐值 RecoverySalt = "vaulthub-recovery" // RecoveryIterations PBKDF2迭代次数 RecoveryIterations = 100000 // RecoveryKeyLength 派生密钥长度(字节) RecoveryKeyLength = 32 )
const (
// SaltSize 盐值大小(32字节)
SaltSize = 32
)
Variables ¶
This section is empty.
Functions ¶
func ClearBytes ¶
func ClearBytes(data []byte)
ClearBytes 安全清零字节切片 用于清除敏感数据(如密钥、密码)在内存中的痕迹 防止内存泄露或被dump时暴露敏感信息 参数:
- data: 需要清零的字节切片
func DecryptAESGCM ¶
DecryptAESGCM 使用AES-256-GCM解密数据 参数:
- ciphertext: 密文数据
- key: 解密密钥(必须是32字节)
- nonce: 加密时使用的Nonce(12字节)
- authTag: 认证标签(16字节)
返回:
- []byte: 解密后的明文数据
- error: 错误信息(如果认证失败,说明数据被篡改)
func DeriveKEK ¶
DeriveKEK 从用户密码派生KEK(密钥加密密钥) 使用Argon2id算法,提供高强度的密钥派生 参数:
- password: 用户密码
- salt: 随机盐值(必须是32字节)
返回:
- []byte: 派生的32字节密钥
- error: 错误信息
func DeriveKeyFromMnemonic ¶
DeriveKeyFromMnemonic 从BIP39助记词派生加密密钥 使用PBKDF2算法,迭代100000次,输出32字节密钥 参数:
- mnemonic: BIP39助记词(24个单词,空格分隔)
返回:
- 32字节密钥和可能的错误
func EncryptAESGCM ¶
EncryptAESGCM 使用AES-256-GCM加密数据 AES-GCM是一种认证加密(AEAD)模式,同时提供机密性和完整性保护 参数:
- plaintext: 待加密的明文数据
- key: 加密密钥(必须是32字节)
返回:
- ciphertext: 密文数据
- nonce: 随机生成的Nonce(12字节,每次加密必须唯一)
- authTag: 认证标签(16字节,用于验证数据完整性)
- error: 错误信息
func GenerateBIP39Mnemonic ¶
GenerateBIP39Mnemonic 生成24个单词的BIP39助记词 使用256位熵,生成24个英文单词 返回助记词字符串(单词之间用空格分隔)和可能的错误
func GenerateRandomBytes ¶
GenerateRandomBytes 生成加密安全的随机字节 使用 crypto/rand 包,适用于生成密钥、盐值、Nonce等敏感随机数 参数:
- size: 需要生成的字节数
返回:
- []byte: 随机字节
- error: 错误信息
func HashRecoveryKey ¶
HashRecoveryKey 计算恢复密钥的SHA256哈希 用于在数据库中存储恢复密钥的哈希值,以便验证用户输入的恢复密钥是否正确 参数:
- recoveryKey: 从助记词派生的32字节密钥
返回:
- 64字符的十六进制哈希字符串
func IsMnemonicValid ¶
IsMnemonicValid 验证BIP39助记词是否有效 检查助记词格式、长度和校验和 参数:
- mnemonic: 待验证的助记词
返回:
- true表示有效,false表示无效
func ValidatePasswordStrength ¶
ValidatePasswordStrength 验证密码强度 要求:至少8位,包含大小写字母、数字
Types ¶
This section is empty.