crypto

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AESKeySize AES-256密钥长度(32字节=256位)
	AESKeySize = 32
	// GCMNonceSize GCM模式Nonce长度(12字节=96位)
	GCMNonceSize = 12
	// GCMTagSize GCM模式认证标签长度(16字节=128位)
	GCMTagSize = 16
)
View Source
const (
	// Argon2Time Argon2id迭代次数
	Argon2Time = 3
	// Argon2Memory Argon2id内存消耗(64MB)
	Argon2Memory = 64 * 1024
	// Argon2Threads Argon2id并行度
	Argon2Threads = 4
	// Argon2KeyLength 输出密钥长度(32字节=256位)
	Argon2KeyLength = 32
)

Argon2id参数配置(平衡安全性和性能)

View Source
const (
	// BcryptCost bcrypt加密强度,平衡性能和安全性
	BcryptCost = 12
	// MinPasswordLength 最小密码长度
	MinPasswordLength = 8
)
View Source
const (
	// EntropyBits 助记词熵位数,256位对应24个单词
	EntropyBits = 256

	// RecoverySalt 恢复密钥派生时使用的盐值
	RecoverySalt = "vaulthub-recovery"

	// RecoveryIterations PBKDF2迭代次数
	RecoveryIterations = 100000

	// RecoveryKeyLength 派生密钥长度(字节)
	RecoveryKeyLength = 32
)
View Source
const (
	// SaltSize 盐值大小(32字节)
	SaltSize = 32
)

Variables

This section is empty.

Functions

func ClearBytes

func ClearBytes(data []byte)

ClearBytes 安全清零字节切片 用于清除敏感数据(如密钥、密码)在内存中的痕迹 防止内存泄露或被dump时暴露敏感信息 参数:

  • data: 需要清零的字节切片

func DecryptAESGCM

func DecryptAESGCM(ciphertext, key, nonce, authTag []byte) ([]byte, error)

DecryptAESGCM 使用AES-256-GCM解密数据 参数:

  • ciphertext: 密文数据
  • key: 解密密钥(必须是32字节)
  • nonce: 加密时使用的Nonce(12字节)
  • authTag: 认证标签(16字节)

返回:

  • []byte: 解密后的明文数据
  • error: 错误信息(如果认证失败,说明数据被篡改)

func DeriveKEK

func DeriveKEK(password string, salt []byte) ([]byte, error)

DeriveKEK 从用户密码派生KEK(密钥加密密钥) 使用Argon2id算法,提供高强度的密钥派生 参数:

  • password: 用户密码
  • salt: 随机盐值(必须是32字节)

返回:

  • []byte: 派生的32字节密钥
  • error: 错误信息

func DeriveKeyFromMnemonic

func DeriveKeyFromMnemonic(mnemonic string) ([]byte, error)

DeriveKeyFromMnemonic 从BIP39助记词派生加密密钥 使用PBKDF2算法,迭代100000次,输出32字节密钥 参数:

  • mnemonic: BIP39助记词(24个单词,空格分隔)

返回:

  • 32字节密钥和可能的错误

func EncryptAESGCM

func EncryptAESGCM(plaintext, key []byte) (ciphertext, nonce, authTag []byte, err error)

EncryptAESGCM 使用AES-256-GCM加密数据 AES-GCM是一种认证加密(AEAD)模式,同时提供机密性和完整性保护 参数:

  • plaintext: 待加密的明文数据
  • key: 加密密钥(必须是32字节)

返回:

  • ciphertext: 密文数据
  • nonce: 随机生成的Nonce(12字节,每次加密必须唯一)
  • authTag: 认证标签(16字节,用于验证数据完整性)
  • error: 错误信息

func GenerateBIP39Mnemonic

func GenerateBIP39Mnemonic() (string, error)

GenerateBIP39Mnemonic 生成24个单词的BIP39助记词 使用256位熵,生成24个英文单词 返回助记词字符串(单词之间用空格分隔)和可能的错误

func GenerateRandomBytes

func GenerateRandomBytes(size int) ([]byte, error)

GenerateRandomBytes 生成加密安全的随机字节 使用 crypto/rand 包,适用于生成密钥、盐值、Nonce等敏感随机数 参数:

  • size: 需要生成的字节数

返回:

  • []byte: 随机字节
  • error: 错误信息

func HashPassword

func HashPassword(password string) (string, error)

HashPassword 使用bcrypt加密密码

func HashRecoveryKey

func HashRecoveryKey(recoveryKey []byte) string

HashRecoveryKey 计算恢复密钥的SHA256哈希 用于在数据库中存储恢复密钥的哈希值,以便验证用户输入的恢复密钥是否正确 参数:

  • recoveryKey: 从助记词派生的32字节密钥

返回:

  • 64字符的十六进制哈希字符串

func IsMnemonicValid

func IsMnemonicValid(mnemonic string) bool

IsMnemonicValid 验证BIP39助记词是否有效 检查助记词格式、长度和校验和 参数:

  • mnemonic: 待验证的助记词

返回:

  • true表示有效,false表示无效

func ValidatePasswordStrength

func ValidatePasswordStrength(password string) bool

ValidatePasswordStrength 验证密码强度 要求:至少8位,包含大小写字母、数字

func VerifyPassword

func VerifyPassword(password, hash string) bool

VerifyPassword 验证密码是否匹配

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL