Documentation
¶
Index ¶
- func CRC32[M string | []byte](s M) uint32
- func CRC64[M string | []byte](s M) uint64
- func DESDecryptCBC(key, ciphertext []byte) ([]byte, error)deprecated
- func DESDecryptECB(key, ciphertext []byte) ([]byte, error)deprecated
- func DESEncryptCBC(key, plaintext []byte) ([]byte, error)deprecated
- func DESEncryptECB(key, plaintext []byte) ([]byte, error)deprecated
- func Decrypt(key, ciphertext []byte) ([]byte, error)
- func DecryptCBC(key, ciphertext []byte) ([]byte, error)
- func DecryptCFB(key, ciphertext []byte) ([]byte, error)
- func DecryptCTR(key, ciphertext []byte) ([]byte, error)
- func DecryptECB(key, ciphertext []byte) ([]byte, error)deprecated
- func DecryptOFB(key, ciphertext []byte) ([]byte, error)
- func DeriveKey(password, salt []byte, iterations, keyLen int, hashFunc func() hash.Hash) []byte
- func ECDHComputeShared(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) ([]byte, error)
- func ECDHComputeSharedSHA256(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, keyLength int) ([]byte, error)
- func ECDHComputeSharedWithKDF(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, keyLength int, ...) ([]byte, error)
- func ECDHKeyExchange(alicePrivateKey *ecdsa.PrivateKey, bobPublicKey *ecdsa.PublicKey, ...) ([]byte, error)
- func ECDHPublicKeyFromCoordinates(curve elliptic.Curve, x, y *big.Int) (*ecdsa.PublicKey, error)
- func ECDHPublicKeyToCoordinates(publicKey *ecdsa.PublicKey) (x, y *big.Int, err error)
- func ECDHSharedSecretTest(keyPair1, keyPair2 *ECDHKeyPair) (bool, error)
- func ECDSAPrivateKeyFromPEM(pemData []byte) (*ecdsa.PrivateKey, error)
- func ECDSAPrivateKeyToPEM(privateKey *ecdsa.PrivateKey) ([]byte, error)
- func ECDSAPublicKeyFromPEM(pemData []byte) (*ecdsa.PublicKey, error)
- func ECDSAPublicKeyToPEM(publicKey *ecdsa.PublicKey) ([]byte, error)
- func ECDSASign(privateKey *ecdsa.PrivateKey, data []byte, hashFunc func() hash.Hash) (r, s *big.Int, err error)
- func ECDSASignSHA256(privateKey *ecdsa.PrivateKey, data []byte) (r, s *big.Int, err error)
- func ECDSASignSHA512(privateKey *ecdsa.PrivateKey, data []byte) (r, s *big.Int, err error)
- func ECDSASignatureFromBytes(data []byte) (r, s *big.Int, err error)
- func ECDSASignatureToBytes(r, s *big.Int) ([]byte, error)
- func ECDSAVerify(publicKey *ecdsa.PublicKey, data []byte, r, s *big.Int, ...) bool
- func ECDSAVerifySHA256(publicKey *ecdsa.PublicKey, data []byte, r, s *big.Int) bool
- func ECDSAVerifySHA512(publicKey *ecdsa.PublicKey, data []byte, r, s *big.Int) bool
- func Encrypt(key, plaintext []byte) ([]byte, error)
- func EncryptCBC(key, plaintext []byte) ([]byte, error)
- func EncryptCFB(key, plaintext []byte) ([]byte, error)
- func EncryptCTR(key, plaintext []byte) ([]byte, error)
- func EncryptECB(key, plaintext []byte) ([]byte, error)deprecated
- func EncryptOFB(key, plaintext []byte) ([]byte, error)
- func GetCurveName(curve elliptic.Curve) string
- func GetRSAKeySize(key *rsa.PublicKey) int
- func GetULIDTimestamp(id string) (int64, error)
- func HMACMd5[M string | []byte](key, message M) string
- func HMACSHA1[M string | []byte](key, message M) string
- func HMACSHA256[M string | []byte](key, message M) string
- func HMACSHA384[M string | []byte](key, message M) string
- func HMACSHA512[M string | []byte](key, message M) string
- func Hash32[M string | []byte](s M) uint32
- func Hash32a[M string | []byte](s M) uint32
- func Hash64[M string | []byte](s M) uint64
- func Hash64a[M string | []byte](s M) uint64
- func IsValidCurve(curve elliptic.Curve) bool
- func Md5[M string | []byte](s M) stringdeprecated
- func MustGetULIDTimestamp(id string) int64
- func PBKDF2SHA256(password, salt []byte, iterations, keyLen int) []byte
- func PBKDF2SHA512(password, salt []byte, iterations, keyLen int) []byte
- func PrivateKeyFromPEM(pemData []byte) (*rsa.PrivateKey, error)
- func PublicKeyFromPEM(pemData []byte) (*rsa.PublicKey, error)
- func RSADecryptOAEP(privateKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error)
- func RSADecryptPKCS1v15(privateKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error)
- func RSAEncryptOAEP(publicKey *rsa.PublicKey, plaintext []byte) ([]byte, error)
- func RSAEncryptPKCS1v15(publicKey *rsa.PublicKey, plaintext []byte) ([]byte, error)
- func RSAMaxMessageLength(publicKey *rsa.PublicKey, padding string) (int, error)
- func RSASignPKCS1v15(privateKey *rsa.PrivateKey, message []byte) ([]byte, error)
- func RSASignPSS(privateKey *rsa.PrivateKey, message []byte) ([]byte, error)
- func RSAVerifyPKCS1v15(publicKey *rsa.PublicKey, message []byte, signature []byte) error
- func RSAVerifyPSS(publicKey *rsa.PublicKey, message []byte, signature []byte) error
- func SHA1[M string | []byte](s M) stringdeprecated
- func Sha224[M string | []byte](s M) string
- func Sha256[M string | []byte](s M) string
- func Sha384[M string | []byte](s M) string
- func Sha512[M string | []byte](s M) string
- func Sha512_224[M string | []byte](s M) string
- func Sha512_256[M string | []byte](s M) string
- func TripleDESDecryptCBC(key, ciphertext []byte) ([]byte, error)deprecated
- func TripleDESDecryptECB(key, ciphertext []byte) ([]byte, error)deprecated
- func TripleDESEncryptCBC(key, plaintext []byte) ([]byte, error)deprecated
- func TripleDESEncryptECB(key, plaintext []byte) ([]byte, error)deprecated
- func ULID() string
- func ULIDWithTimestamp() (string, int64)
- func UUID() string
- func ValidateECDHKeyPair(keyPair *ECDHKeyPair) error
- type ECDHKeyPair
- type ECDSAKeyPair
- type ECDSASignature
- type PBKDF2Config
- type RSAKeyPair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DESDecryptCBC
deprecated
func DESDecryptECB
deprecated
DESDecryptECB 使用 DES 在 ECB 模式下解密密文
Deprecated: DES is considered insecure due to its short key length (56 bits). Use AES-based functions instead. 警告:DES 已被认为是不安全的,仅用于兼容性目的。推荐使用 AES。 警告:ECB 模式在密码学上是不安全的,相同的明文块会产生相同的密文块。 #nosec G401 - DES 是弱加密算法,但保留用于向后兼容
func DESEncryptCBC
deprecated
func DESEncryptECB
deprecated
DESEncryptECB 使用 DES 在 ECB 模式下加密明文
Deprecated: DES is considered insecure due to its short key length (56 bits). Use AES-based functions instead. 警告:DES 已被认为是不安全的,仅用于兼容性目的。推荐使用 AES。 警告:ECB 模式在密码学上是不安全的,相同的明文块会产生相同的密文块。 #nosec G401 - DES 是弱加密算法,但保留用于向后兼容
func DecryptCBC ¶
DecryptCBC 使用 AES-256 在 CBC 模式下解密密文。
func DecryptCFB ¶
DecryptCFB 使用 AES-256 在 CFB 模式下解密密文。
func DecryptCTR ¶
DecryptCTR 使用 AES-256 在 CTR 模式下解密密文。
func DecryptECB
deprecated
DecryptECB 使用 AES-256 在 ECB 模式下解密密文。
Deprecated: ECB mode is cryptographically insecure as identical plaintext blocks produce identical ciphertext blocks, leaking information about the plaintext structure. Use Decrypt (AES-GCM) for authenticated decryption, or DecryptCBC/DecryptCTR for alternatives.
func DecryptOFB ¶
DecryptOFB 使用 AES-256 在 OFB 模式下解密密文。
func DeriveKey ¶
DeriveKey 通用的密钥派生函数,支持自定义哈希算法
参数:
- password: 原始密码或密钥材料
- salt: 盐值
- iterations: 迭代次数
- keyLen: 派生密钥的长度
- hashFunc: 哈希函数构造器(如 sha256.New)
示例:
key := DeriveKey([]byte("password"), salt, 100000, 32, sha256.New)
func ECDHComputeShared ¶
ECDHComputeShared 计算 ECDH 共享密钥
func ECDHComputeSharedSHA256 ¶
func ECDHComputeSharedSHA256(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, keyLength int) ([]byte, error)
ECDHComputeSharedSHA256 使用 SHA256 KDF 计算 ECDH 共享密钥
func ECDHComputeSharedWithKDF ¶
func ECDHComputeSharedWithKDF(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, keyLength int, kdf func() hash.Hash) ([]byte, error)
ECDHComputeSharedWithKDF 计算 ECDH 共享密钥并使用 KDF 派生最终密钥
func ECDHKeyExchange ¶
func ECDHKeyExchange(alicePrivateKey *ecdsa.PrivateKey, bobPublicKey *ecdsa.PublicKey, keyLength int) ([]byte, error)
ECDHKeyExchange 执行完整的 ECDH 密钥交换
func ECDHPublicKeyFromCoordinates ¶
ECDHPublicKeyFromCoordinates 从 x, y 坐标创建 ECDH 公钥
func ECDHPublicKeyToCoordinates ¶
ECDHPublicKeyToCoordinates 将 ECDH 公钥转换为 x, y 坐标
func ECDHSharedSecretTest ¶
func ECDHSharedSecretTest(keyPair1, keyPair2 *ECDHKeyPair) (bool, error)
ECDHSharedSecretTest 测试两个密钥对是否能生成相同的共享密钥(用于测试)
func ECDSAPrivateKeyFromPEM ¶
func ECDSAPrivateKeyFromPEM(pemData []byte) (*ecdsa.PrivateKey, error)
ECDSAPrivateKeyFromPEM 从 PEM 格式解析 ECDSA 私钥
func ECDSAPrivateKeyToPEM ¶
func ECDSAPrivateKeyToPEM(privateKey *ecdsa.PrivateKey) ([]byte, error)
ECDSAPrivateKeyToPEM 将 ECDSA 私钥转换为 PEM 格式
func ECDSAPublicKeyFromPEM ¶
ECDSAPublicKeyFromPEM 从 PEM 格式解析 ECDSA 公钥
func ECDSAPublicKeyToPEM ¶
ECDSAPublicKeyToPEM 将 ECDSA 公钥转换为 PEM 格式
func ECDSASign ¶
func ECDSASign(privateKey *ecdsa.PrivateKey, data []byte, hashFunc func() hash.Hash) (r, s *big.Int, err error)
ECDSASign 使用私钥对数据进行 ECDSA 签名
func ECDSASignSHA256 ¶
ECDSASignSHA256 使用 SHA256 哈希对数据进行 ECDSA 签名
func ECDSASignSHA512 ¶
ECDSASignSHA512 使用 SHA512 哈希对数据进行 ECDSA 签名
func ECDSASignatureFromBytes ¶
ECDSASignatureFromBytes 从字节数组解析 ECDSA 签名(DER 解码)
func ECDSASignatureToBytes ¶
ECDSASignatureToBytes 将 ECDSA 签名转换为字节数组(DER 编码)
func ECDSAVerify ¶
func ECDSAVerify(publicKey *ecdsa.PublicKey, data []byte, r, s *big.Int, hashFunc func() hash.Hash) bool
ECDSAVerify 使用公钥验证 ECDSA 签名
func ECDSAVerifySHA256 ¶
ECDSAVerifySHA256 使用 SHA256 哈希验证 ECDSA 签名
func ECDSAVerifySHA512 ¶
ECDSAVerifySHA512 使用 SHA512 哈希验证 ECDSA 签名
func EncryptCBC ¶
EncryptCBC 使用 AES-256 在 CBC 模式下加密明文。
func EncryptCFB ¶
EncryptCFB 使用 AES-256 在 CFB 模式下加密明文。
func EncryptCTR ¶
EncryptCTR 使用 AES-256 在 CTR 模式下加密明文。
func EncryptECB
deprecated
EncryptECB 使用 AES-256 在 ECB 模式下加密明文。
Deprecated: ECB mode is cryptographically insecure as identical plaintext blocks produce identical ciphertext blocks, leaking information about the plaintext structure. Use Encrypt (AES-GCM) for authenticated encryption, or EncryptCBC/EncryptCTR for alternatives.
func EncryptOFB ¶
EncryptOFB 使用 AES-256 在 OFB 模式下加密明文。
func GetULIDTimestamp ¶
GetULIDTimestamp 从 ULID 中提取时间戳(毫秒级)
func HMACSHA256 ¶
HMACSHA256 使用 SHA256 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。
func HMACSHA384 ¶
HMACSHA384 使用 SHA384 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。
func HMACSHA512 ¶
HMACSHA512 使用 SHA512 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。
func MustGetULIDTimestamp ¶
MustGetULIDTimestamp 从 ULID 中提取时间戳(毫秒级)
func PBKDF2SHA256 ¶
PBKDF2SHA256 使用 PBKDF2-HMAC-SHA256 从密码派生密钥
参数:
- password: 原始密码或密钥材料
- salt: 盐值,建议至少 16 字节,应使用 crypto/rand 生成
- iterations: 迭代次数,建议至少 100000,越高越安全但越慢
- keyLen: 派生密钥的长度(字节)
使用场景:
- 密码存储(配合随机盐)
- 密钥派生(从用户密码生成加密密钥)
- 密钥扩展(从短密钥生成长密钥)
安全建议:
- 盐值必须唯一且随机,每个密码使用不同的盐
- 迭代次数建议:桌面应用 ≥ 100,000,Web应用 ≥ 50,000
- 对于更高安全需求,考虑使用 Argon2id(需要外部依赖)
func PBKDF2SHA512 ¶
PBKDF2SHA512 使用 PBKDF2-HMAC-SHA512 从密码派生密钥
参数与 PBKDF2SHA256 相同,但使用 SHA-512 作为底层哈希函数 SHA-512 比 SHA-256 更慢,但在某些平台上可能更安全
func PrivateKeyFromPEM ¶
func PrivateKeyFromPEM(pemData []byte) (*rsa.PrivateKey, error)
PrivateKeyFromPEM 从 PEM 格式加载私钥
func PublicKeyFromPEM ¶
PublicKeyFromPEM 从 PEM 格式加载公钥
func RSADecryptOAEP ¶
func RSADecryptOAEP(privateKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error)
RSADecryptOAEP 使用 OAEP 填充方式进行 RSA 解密
func RSADecryptPKCS1v15 ¶
func RSADecryptPKCS1v15(privateKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error)
RSADecryptPKCS1v15 使用 PKCS1v15 填充方式进行 RSA 解密
func RSAEncryptOAEP ¶
RSAEncryptOAEP 使用 OAEP 填充方式进行 RSA 加密
func RSAEncryptPKCS1v15 ¶
RSAEncryptPKCS1v15 使用 PKCS1v15 填充方式进行 RSA 加密
func RSAMaxMessageLength ¶
RSAMaxMessageLength 计算 RSA 加密时的最大消息长度
func RSASignPKCS1v15 ¶
func RSASignPKCS1v15(privateKey *rsa.PrivateKey, message []byte) ([]byte, error)
RSASignPKCS1v15 使用 PKCS1v15 填充方式进行数字签名
func RSASignPSS ¶
func RSASignPSS(privateKey *rsa.PrivateKey, message []byte) ([]byte, error)
RSASignPSS 使用 PSS 填充方式进行数字签名
func RSAVerifyPKCS1v15 ¶
RSAVerifyPKCS1v15 使用 PKCS1v15 填充方式验证数字签名
func RSAVerifyPSS ¶
RSAVerifyPSS 使用 PSS 填充方式验证数字签名
func Sha512_224 ¶
Sha512_224 计算输入字符串或字节切片的 SHA-512/224 哈希值,并返回十六进制表示的字符串。
func Sha512_256 ¶
Sha512_256 计算输入字符串或字节切片的 SHA-512/256 哈希值,并返回十六进制表示的字符串。
func TripleDESDecryptCBC
deprecated
func TripleDESDecryptECB
deprecated
func TripleDESEncryptCBC
deprecated
func TripleDESEncryptECB
deprecated
func ULIDWithTimestamp ¶
func ValidateECDHKeyPair ¶
func ValidateECDHKeyPair(keyPair *ECDHKeyPair) error
ValidateECDHKeyPair 验证 ECDH 密钥对的有效性
Types ¶
type ECDHKeyPair ¶
type ECDHKeyPair struct {
PrivateKey *ecdsa.PrivateKey
PublicKey *ecdsa.PublicKey
}
ECDHKeyPair represents an ECDH key pair (same as ECDSA but used for key exchange)
func GenerateECDHKey ¶
func GenerateECDHKey(curve elliptic.Curve) (*ECDHKeyPair, error)
GenerateECDHKey 生成 ECDH 密钥对
func GenerateECDHP256Key ¶
func GenerateECDHP256Key() (*ECDHKeyPair, error)
GenerateECDHP256Key 生成 P-256 ECDH 密钥对
func GenerateECDHP384Key ¶
func GenerateECDHP384Key() (*ECDHKeyPair, error)
GenerateECDHP384Key 生成 P-384 ECDH 密钥对
func GenerateECDHP521Key ¶
func GenerateECDHP521Key() (*ECDHKeyPair, error)
GenerateECDHP521Key 生成 P-521 ECDH 密钥对
type ECDSAKeyPair ¶
type ECDSAKeyPair struct {
PrivateKey *ecdsa.PrivateKey
PublicKey *ecdsa.PublicKey
}
ECDSAKeyPair represents an ECDSA key pair
func GenerateECDSAKey ¶
func GenerateECDSAKey(curve elliptic.Curve) (*ECDSAKeyPair, error)
GenerateECDSAKey 生成 ECDSA 密钥对
func GenerateECDSAP256Key ¶
func GenerateECDSAP256Key() (*ECDSAKeyPair, error)
GenerateECDSAP256Key 生成 P-256 (secp256r1) ECDSA 密钥对
func GenerateECDSAP384Key ¶
func GenerateECDSAP384Key() (*ECDSAKeyPair, error)
GenerateECDSAP384Key 生成 P-384 (secp384r1) ECDSA 密钥对
func GenerateECDSAP521Key ¶
func GenerateECDSAP521Key() (*ECDSAKeyPair, error)
GenerateECDSAP521Key 生成 P-521 (secp521r1) ECDSA 密钥对
type ECDSASignature ¶
ECDSASignature represents an ECDSA signature
type PBKDF2Config ¶
type PBKDF2Config struct {
Iterations int // 迭代次数,建议 ≥ 100000
SaltLen int // 盐值长度,建议 ≥ 16
KeyLen int // 派生密钥长度
HashFunc func() hash.Hash // 哈希函数,默认 sha256.New
}
PBKDF2Config PBKDF2 配置参数
func DefaultPBKDF2Config ¶
func DefaultPBKDF2Config() PBKDF2Config
DefaultPBKDF2Config 返回推荐的 PBKDF2 配置
配置说明:
- 迭代次数:100,000(适合桌面应用)
- 盐值长度:32 字节
- 密钥长度:32 字节(适用于 AES-256)
- 哈希函数:SHA-256
func FastPBKDF2Config ¶
func FastPBKDF2Config() PBKDF2Config
FastPBKDF2Config 返回快速但安全性较低的 PBKDF2 配置
配置说明:
- 迭代次数:50,000(适合 Web 应用,减少服务器负载)
- 盐值长度:16 字节
- 密钥长度:32 字节
- 哈希函数:SHA-256
警告:仅在性能要求高且威胁模型允许的情况下使用
func StrongPBKDF2Config ¶
func StrongPBKDF2Config() PBKDF2Config
StrongPBKDF2Config 返回高安全性的 PBKDF2 配置
配置说明:
- 迭代次数:200,000(更高安全性)
- 盐值长度:64 字节
- 密钥长度:64 字节
- 哈希函数:SHA-512
适用于高安全需求场景,但性能开销较大
type RSAKeyPair ¶
type RSAKeyPair struct {
PrivateKey *rsa.PrivateKey
PublicKey *rsa.PublicKey
}
RSAKeyPair 表示 RSA 公私钥对
func GenerateRSAKeyPair ¶
func GenerateRSAKeyPair(keySize int) (*RSAKeyPair, error)
GenerateRSAKeyPair 生成指定长度的 RSA 密钥对 keySize: 密钥长度,建议使用 2048、3072 或 4096 位
func (*RSAKeyPair) PrivateKeyToPEM ¶
func (kp *RSAKeyPair) PrivateKeyToPEM() ([]byte, error)
PrivateKeyToPEM 将私钥转换为 PEM 格式
func (*RSAKeyPair) PublicKeyToPEM ¶
func (kp *RSAKeyPair) PublicKeyToPEM() ([]byte, error)
PublicKeyToPEM 将公钥转换为 PEM 格式