cryptox

package
v0.0.0-...-8c32fbf Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: AGPL-3.0 Imports: 30 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CRC32

func CRC32[M string | []byte](s M) uint32

CRC32 计算输入字符串或字节切片的 CRC32 校验值。

func CRC64

func CRC64[M string | []byte](s M) uint64

CRC64 计算输入字符串或字节切片的 CRC64 校验值。

func DESDecryptCBC deprecated

func DESDecryptCBC(key, ciphertext []byte) ([]byte, error)

DESDecryptCBC 使用 DES 在 CBC 模式下解密密文

Deprecated: DES is considered insecure due to its short key length (56 bits). Use AES-based functions instead. 警告:DES 已被认为是不安全的,仅用于兼容性目的。推荐使用 AES。 #nosec G401 - DES 是弱加密算法,但保留用于向后兼容

func DESDecryptECB deprecated

func DESDecryptECB(key, ciphertext []byte) ([]byte, error)

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 DESEncryptCBC(key, plaintext []byte) ([]byte, error)

DESEncryptCBC 使用 DES 在 CBC 模式下加密明文

Deprecated: DES is considered insecure due to its short key length (56 bits). Use AES-based functions instead. 警告:DES 已被认为是不安全的,仅用于兼容性目的。推荐使用 AES。 #nosec G401 - DES 是弱加密算法,但保留用于向后兼容

func DESEncryptECB deprecated

func DESEncryptECB(key, plaintext []byte) ([]byte, error)

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 Decrypt

func Decrypt(key, ciphertext []byte) ([]byte, error)

Decrypt 使用 AES-256 在 GCM 模式下解密密文。

func DecryptCBC

func DecryptCBC(key, ciphertext []byte) ([]byte, error)

DecryptCBC 使用 AES-256 在 CBC 模式下解密密文。

func DecryptCFB

func DecryptCFB(key, ciphertext []byte) ([]byte, error)

DecryptCFB 使用 AES-256 在 CFB 模式下解密密文。

func DecryptCTR

func DecryptCTR(key, ciphertext []byte) ([]byte, error)

DecryptCTR 使用 AES-256 在 CTR 模式下解密密文。

func DecryptECB deprecated

func DecryptECB(key, ciphertext []byte) ([]byte, error)

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

func DecryptOFB(key, ciphertext []byte) ([]byte, error)

DecryptOFB 使用 AES-256 在 OFB 模式下解密密文。

func DeriveKey

func DeriveKey(password, salt []byte, iterations, keyLen int, hashFunc func() hash.Hash) []byte

DeriveKey 通用的密钥派生函数,支持自定义哈希算法

参数:

  • password: 原始密码或密钥材料
  • salt: 盐值
  • iterations: 迭代次数
  • keyLen: 派生密钥的长度
  • hashFunc: 哈希函数构造器(如 sha256.New)

示例:

key := DeriveKey([]byte("password"), salt, 100000, 32, sha256.New)

func ECDHComputeShared

func ECDHComputeShared(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) ([]byte, error)

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

func ECDHPublicKeyFromCoordinates(curve elliptic.Curve, x, y *big.Int) (*ecdsa.PublicKey, error)

ECDHPublicKeyFromCoordinates 从 x, y 坐标创建 ECDH 公钥

func ECDHPublicKeyToCoordinates

func ECDHPublicKeyToCoordinates(publicKey *ecdsa.PublicKey) (x, y *big.Int, err error)

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

func ECDSAPublicKeyFromPEM(pemData []byte) (*ecdsa.PublicKey, error)

ECDSAPublicKeyFromPEM 从 PEM 格式解析 ECDSA 公钥

func ECDSAPublicKeyToPEM

func ECDSAPublicKeyToPEM(publicKey *ecdsa.PublicKey) ([]byte, error)

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

func ECDSASignSHA256(privateKey *ecdsa.PrivateKey, data []byte) (r, s *big.Int, err error)

ECDSASignSHA256 使用 SHA256 哈希对数据进行 ECDSA 签名

func ECDSASignSHA512

func ECDSASignSHA512(privateKey *ecdsa.PrivateKey, data []byte) (r, s *big.Int, err error)

ECDSASignSHA512 使用 SHA512 哈希对数据进行 ECDSA 签名

func ECDSASignatureFromBytes

func ECDSASignatureFromBytes(data []byte) (r, s *big.Int, err error)

ECDSASignatureFromBytes 从字节数组解析 ECDSA 签名(DER 解码)

func ECDSASignatureToBytes

func ECDSASignatureToBytes(r, s *big.Int) ([]byte, error)

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

func ECDSAVerifySHA256(publicKey *ecdsa.PublicKey, data []byte, r, s *big.Int) bool

ECDSAVerifySHA256 使用 SHA256 哈希验证 ECDSA 签名

func ECDSAVerifySHA512

func ECDSAVerifySHA512(publicKey *ecdsa.PublicKey, data []byte, r, s *big.Int) bool

ECDSAVerifySHA512 使用 SHA512 哈希验证 ECDSA 签名

func Encrypt

func Encrypt(key, plaintext []byte) ([]byte, error)

Encrypt 使用 AES-256 在 GCM 模式下加密明文。

func EncryptCBC

func EncryptCBC(key, plaintext []byte) ([]byte, error)

EncryptCBC 使用 AES-256 在 CBC 模式下加密明文。

func EncryptCFB

func EncryptCFB(key, plaintext []byte) ([]byte, error)

EncryptCFB 使用 AES-256 在 CFB 模式下加密明文。

func EncryptCTR

func EncryptCTR(key, plaintext []byte) ([]byte, error)

EncryptCTR 使用 AES-256 在 CTR 模式下加密明文。

func EncryptECB deprecated

func EncryptECB(key, plaintext []byte) ([]byte, error)

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

func EncryptOFB(key, plaintext []byte) ([]byte, error)

EncryptOFB 使用 AES-256 在 OFB 模式下加密明文。

func GetCurveName

func GetCurveName(curve elliptic.Curve) string

GetCurveName 获取椭圆曲线的名称

func GetRSAKeySize

func GetRSAKeySize(key *rsa.PublicKey) int

GetRSAKeySize 获取 RSA 密钥长度(位)

func GetULIDTimestamp

func GetULIDTimestamp(id string) (int64, error)

GetULIDTimestamp 从 ULID 中提取时间戳(毫秒级)

func HMACMd5

func HMACMd5[M string | []byte](key, message M) string

HMACMd5 使用 MD5 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。

func HMACSHA1

func HMACSHA1[M string | []byte](key, message M) string

HMACSHA1 使用 SHA1 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。

func HMACSHA256

func HMACSHA256[M string | []byte](key, message M) string

HMACSHA256 使用 SHA256 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。

func HMACSHA384

func HMACSHA384[M string | []byte](key, message M) string

HMACSHA384 使用 SHA384 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。

func HMACSHA512

func HMACSHA512[M string | []byte](key, message M) string

HMACSHA512 使用 SHA512 作为底层哈希函数计算 HMAC 值,并返回十六进制表示的字符串。

func Hash32

func Hash32[M string | []byte](s M) uint32

Hash32 使用 FNV-1 算法计算输入字符串或字节切片的 32 位哈希值。

func Hash32a

func Hash32a[M string | []byte](s M) uint32

Hash32a 使用 FNV-1a 算法计算输入字符串或字节切片的 32 位哈希值。

func Hash64

func Hash64[M string | []byte](s M) uint64

Hash64 使用 FNV-1 算法计算输入字符串或字节切片的 64 位哈希值。

func Hash64a

func Hash64a[M string | []byte](s M) uint64

Hash64a 使用 FNV-1a 算法计算输入字符串或字节切片的 64 位哈希值。

func IsValidCurve

func IsValidCurve(curve elliptic.Curve) bool

IsValidCurve 检查椭圆曲线是否有效

func Md5 deprecated

func Md5[M string | []byte](s M) string

Md5 计算输入字符串或字节切片的 MD5 哈希值,并返回十六进制表示的字符串。

Deprecated: MD5 is cryptographically broken and should not be used for security purposes. Use Sha256 or Sha512 instead.

func MustGetULIDTimestamp

func MustGetULIDTimestamp(id string) int64

MustGetULIDTimestamp 从 ULID 中提取时间戳(毫秒级)

func PBKDF2SHA256

func PBKDF2SHA256(password, salt []byte, iterations, keyLen int) []byte

PBKDF2SHA256 使用 PBKDF2-HMAC-SHA256 从密码派生密钥

参数:

  • password: 原始密码或密钥材料
  • salt: 盐值,建议至少 16 字节,应使用 crypto/rand 生成
  • iterations: 迭代次数,建议至少 100000,越高越安全但越慢
  • keyLen: 派生密钥的长度(字节)

使用场景:

  • 密码存储(配合随机盐)
  • 密钥派生(从用户密码生成加密密钥)
  • 密钥扩展(从短密钥生成长密钥)

安全建议:

  • 盐值必须唯一且随机,每个密码使用不同的盐
  • 迭代次数建议:桌面应用 ≥ 100,000,Web应用 ≥ 50,000
  • 对于更高安全需求,考虑使用 Argon2id(需要外部依赖)

func PBKDF2SHA512

func PBKDF2SHA512(password, salt []byte, iterations, keyLen int) []byte

PBKDF2SHA512 使用 PBKDF2-HMAC-SHA512 从密码派生密钥

参数与 PBKDF2SHA256 相同,但使用 SHA-512 作为底层哈希函数 SHA-512 比 SHA-256 更慢,但在某些平台上可能更安全

func PrivateKeyFromPEM

func PrivateKeyFromPEM(pemData []byte) (*rsa.PrivateKey, error)

PrivateKeyFromPEM 从 PEM 格式加载私钥

func PublicKeyFromPEM

func PublicKeyFromPEM(pemData []byte) (*rsa.PublicKey, error)

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

func RSAEncryptOAEP(publicKey *rsa.PublicKey, plaintext []byte) ([]byte, error)

RSAEncryptOAEP 使用 OAEP 填充方式进行 RSA 加密

func RSAEncryptPKCS1v15

func RSAEncryptPKCS1v15(publicKey *rsa.PublicKey, plaintext []byte) ([]byte, error)

RSAEncryptPKCS1v15 使用 PKCS1v15 填充方式进行 RSA 加密

func RSAMaxMessageLength

func RSAMaxMessageLength(publicKey *rsa.PublicKey, padding string) (int, error)

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

func RSAVerifyPKCS1v15(publicKey *rsa.PublicKey, message []byte, signature []byte) error

RSAVerifyPKCS1v15 使用 PKCS1v15 填充方式验证数字签名

func RSAVerifyPSS

func RSAVerifyPSS(publicKey *rsa.PublicKey, message []byte, signature []byte) error

RSAVerifyPSS 使用 PSS 填充方式验证数字签名

func SHA1 deprecated

func SHA1[M string | []byte](s M) string

SHA1 计算输入字符串或字节切片的 SHA1 哈希值,并返回十六进制表示的字符串。

Deprecated: SHA1 is cryptographically broken and should not be used for security purposes. Use Sha256 or Sha512 instead.

func Sha224

func Sha224[M string | []byte](s M) string

Sha224 计算输入字符串或字节切片的 SHA-224 哈希值,并返回十六进制表示的字符串。

func Sha256

func Sha256[M string | []byte](s M) string

Sha256 计算输入字符串或字节切片的 SHA-256 哈希值,并返回十六进制表示的字符串。

func Sha384

func Sha384[M string | []byte](s M) string

Sha384 计算输入字符串或字节切片的 SHA-384 哈希值,并返回十六进制表示的字符串。

func Sha512

func Sha512[M string | []byte](s M) string

Sha512 计算输入字符串或字节切片的 SHA-512 哈希值,并返回十六进制表示的字符串。

func Sha512_224

func Sha512_224[M string | []byte](s M) string

Sha512_224 计算输入字符串或字节切片的 SHA-512/224 哈希值,并返回十六进制表示的字符串。

func Sha512_256

func Sha512_256[M string | []byte](s M) string

Sha512_256 计算输入字符串或字节切片的 SHA-512/256 哈希值,并返回十六进制表示的字符串。

func TripleDESDecryptCBC deprecated

func TripleDESDecryptCBC(key, ciphertext []byte) ([]byte, error)

TripleDESDecryptCBC 使用 3DES 在 CBC 模式下解密密文

Deprecated: 3DES is considered insecure. Use AES-based functions instead. 警告:3DES 已被认为是不安全的,仅用于兼容性目的。推荐使用 AES。 #nosec G401 - 3DES 是弱加密算法,但保留用于向后兼容

func TripleDESDecryptECB deprecated

func TripleDESDecryptECB(key, ciphertext []byte) ([]byte, error)

TripleDESDecryptECB 使用 3DES 在 ECB 模式下解密密文

Deprecated: 3DES is considered insecure. Use AES-based functions instead. 警告:ECB 模式在密码学上是不安全的,相同的明文块会产生相同的密文块。 #nosec G401 - 3DES 是相对较弱的加密算法,但保留用于向后兼容

func TripleDESEncryptCBC deprecated

func TripleDESEncryptCBC(key, plaintext []byte) ([]byte, error)

TripleDESEncryptCBC 使用 3DES 在 CBC 模式下加密明文

Deprecated: 3DES is considered insecure. Use AES-based functions instead. 警告:3DES 已被认为是不安全的,仅用于兼容性目的。推荐使用 AES。 #nosec G401 - 3DES 是弱加密算法,但保留用于向后兼容

func TripleDESEncryptECB deprecated

func TripleDESEncryptECB(key, plaintext []byte) ([]byte, error)

TripleDESEncryptECB 使用 3DES 在 ECB 模式下加密明文

Deprecated: 3DES is considered insecure. Use AES-based functions instead. 警告:ECB 模式在密码学上是不安全的,相同的明文块会产生相同的密文块。 #nosec G401 - 3DES 是相对较弱的加密算法,但保留用于向后兼容

func ULID

func ULID() string

func ULIDWithTimestamp

func ULIDWithTimestamp() (string, int64)

func UUID

func UUID() string

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

type ECDSASignature struct {
	R *big.Int
	S *big.Int
}

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 格式

Jump to

Keyboard shortcuts

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