xcrypto

package
v0.1.65 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Iterations PBKDF2 迭代次数
	Iterations = 600000
	// DefaultKeyLen 默认密钥长度
	DefaultKeyLen = 32
)

Variables

This section is empty.

Functions

func Base64Decode added in v0.1.62

func Base64Decode(src []byte) ([]byte, error)

func Base64Encode added in v0.1.62

func Base64Encode(src []byte) []byte

func CertToText added in v0.1.41

func CertToText(certDER []byte) ([]byte, error)

CertToText 证书 DER 转换成 PEM

func ComparePassword added in v0.1.56

func ComparePassword(hashPassword []byte, password []byte) bool

ComparePassword 比较密码

func ComparePasswordKDFHash added in v0.1.56

func ComparePasswordKDFHash(a, b []byte) bool

ComparePasswordKDFHash 对比密码 KDF 哈希

func CreateCert added in v0.1.41

func CreateCert(template *x509.Certificate) (*x509.Certificate, *rsa.PrivateKey, error)

CreateCert 创建 x509 证书

func CreateCertDER added in v0.1.52

func CreateCertDER(template *x509.Certificate) ([]byte, *rsa.PrivateKey, error)

CreateCertDER 创建 x509 证书 DER

func CreateCertToTLS added in v0.1.41

func CreateCertToTLS(template *x509.Certificate) (*tls.Config, error)

CreateCertToTLS 创建一个 x509 证书并转换为 TLS 配置

func CreateCertToText added in v0.1.41

func CreateCertToText(template *x509.Certificate) ([]byte, []byte, error)

CreateCertToText 创建一个 x509 证书并转换为 PEM

func CreatePasswordHash added in v0.1.56

func CreatePasswordHash(password []byte) (hashPassword []byte)

CreatePasswordHash 创建密码哈希值

func DecryptCFB

func DecryptCFB(ciphertext, key []byte, opts ...CryptoOption) (plaintext []byte, err error)

DecryptCFB CFB 解密

func DecryptGCM added in v0.1.51

func DecryptGCM(ciphertext, key []byte, opts ...CryptoOption) (plaintext []byte, err error)

DecryptGCM GCM 解密

func DecryptOAEP added in v0.1.49

func DecryptOAEP(base64Ciphertext string, key *rsa.PrivateKey) (string, error)

DecryptOAEP OAEP 解密

func DecryptPKCSRSA added in v0.1.14

func DecryptPKCSRSA(base64Ciphertext string, key *rsa.PrivateKey) (string, error)

DecryptPKCSRSA PKCS 解密

func DecryptToStruct added in v0.1.51

func DecryptToStruct(x any, key []byte, decryptFunc DecryptFunc, inDecoder CryptoOption) error

DecryptToStruct 将结构体中的加密字段转换为明文

func DecryptVault added in v0.1.56

func DecryptVault(v *Vault, masterKey []byte) (data []byte, err error)

DecryptVault 解密 Vault

func EncryptCFB

func EncryptCFB(plaintext, key []byte, opts ...CryptoOption) (ciphertext []byte, err error)

EncryptCFB CFB 加密

func EncryptGCM added in v0.1.51

func EncryptGCM(plaintext, key []byte, opts ...CryptoOption) (ciphertext []byte, err error)

EncryptGCM GCM 加密

func EncryptOAEP added in v0.1.49

func EncryptOAEP(plaintext string, key *rsa.PublicKey) (string, error)

EncryptOAEP OAEP 加密

func EncryptPKCSRSA added in v0.1.14

func EncryptPKCSRSA(plaintext string, key *rsa.PublicKey) (string, error)

EncryptPKCSRSA PKCS 加密

func GenDEK added in v0.1.56

func GenDEK(opts ...KeyOption) []byte

GenDEK 生成 DEK(Data Encryption Key,数据加密密钥)

func GenDeriveKey added in v0.1.59

func GenDeriveKey(password, salt []byte, opts ...KeyOption) []byte

GenDeriveKey 使用 KDF 生成派生密钥

func GenExtendKey added in v0.1.59

func GenExtendKey(key []byte, salt, info []byte, opts ...KeyOption) ([]byte, error)

GenExtendKey 使用 HKDF 生成扩展密钥

func GenKey added in v0.1.60

func GenKey(opts ...KeyOption) []byte

GenKey 生成随机密钥

func GenMasterKey added in v0.1.56

func GenMasterKey(password, salt []byte, opts ...KeyOption) []byte

GenMasterKey 生成主密钥

func GenPasswordKDFHash added in v0.1.56

func GenPasswordKDFHash(password, salt []byte) []byte

GenPasswordKDFHash 生成密码 KDF 哈希

func GenSalt added in v0.1.56

func GenSalt(opts ...KeyOption) []byte

GenSalt 生成随机盐

func GenerateHmacSha1 added in v0.1.23

func GenerateHmacSha1(message []byte, key []byte) ([]byte, error)

GenerateHmacSha1 生成 sha1 消息摘要,密钥长度必须为16

func GenerateHmacSha256 added in v0.1.21

func GenerateHmacSha256(message []byte, key []byte) ([]byte, error)

GenerateHmacSha256 生成 sha256 消息摘要,密钥长度必须为32

func GenerateRSAKeyPair added in v0.1.14

func GenerateRSAKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey, error)

GenerateRSAKeyPair 生成公钥私钥对

func GenerateRandomString added in v0.1.13

func GenerateRandomString(length int, containsNumbers, containsUppercase, containsLowercase, containsSpecial bool) []byte

GenerateRandomString 生成随机字符串

func IsPasswordComplexity added in v0.1.56

func IsPasswordComplexity(s string, minLength int, minIncludeCase int) bool

IsPasswordComplexity 检查密码是否符合复杂度

func RSAPrivateKeyToText added in v0.1.14

func RSAPrivateKeyToText(key *rsa.PrivateKey) ([]byte, error)

RSAPrivateKeyToText 私钥转成 PEM

func RSAPublicKeyToText added in v0.1.14

func RSAPublicKeyToText(key *rsa.PublicKey) ([]byte, error)

RSAPublicKeyToText 公钥转成 PEM

func TextToCert added in v0.1.50

func TextToCert(certText []byte) (*x509.Certificate, error)

TextToCert PEM 转换成证书

func TextToCertDER added in v0.1.52

func TextToCertDER(certText []byte) ([]byte, error)

TextToCertDER PEM 转换成证书 DER

func TextToRSAPrivateKey added in v0.1.14

func TextToRSAPrivateKey(key []byte) (*rsa.PrivateKey, error)

TextToRSAPrivateKey PEM 转为私钥

func TextToRSAPublicKey added in v0.1.14

func TextToRSAPublicKey(key []byte) (*rsa.PublicKey, error)

TextToRSAPublicKey PEM 转为公钥

func UpdateVaultKEK added in v0.1.58

func UpdateVaultKEK(v *Vault, oldMasterKey, newMasterKey []byte, isUpdateDEK bool) (err error)

UpdateVaultKEK 更新 Vault KEK

func ValidAesKey added in v0.1.51

func ValidAesKey(key []byte) error

ValidAesKey 验证 AES 密钥长度是否合法,合法的密钥长度为16(aes-128) 24(aes-192) 32(aes-256)

Types

type CryptoOption added in v0.1.62

type CryptoOption func(*cryptoConfig)

func WithDecryptDecoder added in v0.1.62

func WithDecryptDecoder(decoder Decoder) CryptoOption

WithDecryptDecoder 用于解密前数据解码

func WithEncryptEncoder added in v0.1.62

func WithEncryptEncoder(encoder Encoder) CryptoOption

WithEncryptEncoder 用于加密后数据编码

type Decoder added in v0.1.62

type Decoder func(src []byte) ([]byte, error)

type DecryptFunc added in v0.1.56

type DecryptFunc func(ciphertext, key []byte, opts ...CryptoOption) (plaintext []byte, err error)

type Encoder added in v0.1.62

type Encoder func(src []byte) []byte

type KeyOption added in v0.1.61

type KeyOption func(*keyConfig)

func WithKeyEncoder added in v0.1.62

func WithKeyEncoder(encoder Encoder) KeyOption

WithKeyEncoder 用于秘钥生成后编码

func WithKeyHash added in v0.1.62

func WithKeyHash(hash func() hash.Hash) KeyOption

func WithKeyIterations added in v0.1.62

func WithKeyIterations(iterations int) KeyOption

func WithKeyLen added in v0.1.61

func WithKeyLen(keyLen int) KeyOption

type Vault added in v0.1.56

type Vault struct {
	Salt   string `json:"salt"`
	EncDEK string `json:"enc_dek"`
	Data   string `json:"data"`
}

func CreateVault added in v0.1.56

func CreateVault(masterKey, plaintext []byte) (vault *Vault, err error)

CreateVault 创建 Vault

Jump to

Keyboard shortcuts

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