rexCrypto

package
v1.0.90 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 25 Imported by: 1

Documentation

Index

Constants

View Source
const (
	AES128KeyLen = 16
	AES192KeyLen = 24
	AES256KeyLen = 32
	AESGCMIvLen  = 12
	AESCCMIvLen  = 24
	AEsCBCIvLen  = 16
	AESCTRIvLen  = 16
)
View Source
const (
	DefaultArgon2Time      uint32 = 3
	DefaultArgon2MemoryKiB uint32 = 65536 // 64 MiB
	DefaultArgon2Threads   uint8  = 2
	DefaultArgon2KeyLen    uint32 = 32
	DefaultArgon2SaltLen          = Bits256Len // 128 bits
)

Variables

View Source
var (
	Argon2ErrEmptySecret   = errors.New("empty secret")
	Argon2ErrPhcFormatBad  = errors.New("bad phc format")
	Argon2ErrPhcParamsBad  = errors.New("bad phc params")
	Argon2ErrPhcVersionBad = errors.New("bad alg version")
)
View Source
var (
	ErrorInvalidECDSAPrivateKeyPEMFormat = errors.New("invalid public key PEM format")
	ErrorInvalidECDSAPublicKeyPEMFormat  = errors.New("invalid public key PEM format")
	ErrorPublicKeyNotECDSA               = errors.New("public key not ECDSA type")
)
View Source
var (
	ErrorInvalidRsaPrivateKeyPEMFormat = errors.New("invalid public key PEM format")
	ErrorInvalidRsaPublicKeyPEMFormat  = errors.New("invalid public key PEM format")
	ErrorPublicKeyNotRsa               = errors.New("public key not rsa type")
)

Functions

func AESDecryptByCBC

func AESDecryptByCBC(cipherTextBase64 string, keyBase, ivBase string) ([]byte, error)

**AES-CBC 解密**

func AESDecryptByCCM

func AESDecryptByCCM(cipherTextBase64 string, keyBase, ivBase string) ([]byte, error)

**AES-CCM 解密**

func AESDecryptByCTR

func AESDecryptByCTR(cipherTextBase64 string, keyBase, ivBase string) ([]byte, error)

**AES-CTR 解密**

func AESDecryptByGCM

func AESDecryptByGCM(cipherTextBase64 string, keyBase, ivBase string) ([]byte, error)

**AES-GCM 解密**

func AESDecryptByGCMBt added in v1.0.58

func AESDecryptByGCMBt(cipherTextBt, key, iv []byte) ([]byte, error)

**AES-GCM 解密**

func AESEncryptByCBC

func AESEncryptByCBC(plainText []byte, keyBase, ivBase string) (string, error)

**AES-CBC 加密**

func AESEncryptByCCM

func AESEncryptByCCM(plainText []byte, keyBase, ivBase string) (string, error)

**AES-CCM 加密**

func AESEncryptByCTR

func AESEncryptByCTR(plainText []byte, keyBase, ivBase string) (string, error)

**AES-CTR 加密**

func AESEncryptByGCM

func AESEncryptByGCM(plainText []byte, keyBase, ivBase string) (string, error)

**AES-GCM 加密**

func AESEncryptByGCMBt added in v1.0.57

func AESEncryptByGCMBt(plainText, key, iv []byte) ([]byte, error)

func CBCPkcs7Pad

func CBCPkcs7Pad(data []byte, blockSize int) []byte

**PKCS7 填充**

func CBCPkcs7Unpad

func CBCPkcs7Unpad(data []byte) []byte

**PKCS7 去填充**

func ECDSAGenerateECCCertificate

func ECDSAGenerateECCCertificate(curve elliptic.Curve, subject pkix.Name) (keyPem, certPem []byte, err error)

note: 生成 ECC 证书

func ECDSAGenerateKeys

func ECDSAGenerateKeys(curve elliptic.Curve) (privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, err error)

note: 生成 ECDSA 公私钥

func GenAESKeyAndIv

func GenAESKeyAndIv(KeyLen int, IvLen int) (keyBase, ivBase string, err error)

func GenRsaKey2Bt

func GenRsaKey2Bt(btLen int) (privateKeyBt, publicKeyBt []byte, err error)

note: 生成 RSA 公私钥并解析到bt

func GenRsaKey2Str

func GenRsaKey2Str(btLen int) (privateKeyStr, publicKeyStr *string, err error)

note: 生成 RSA 公私钥并解析到字符串

func HMACSha256

func HMACSha256(data string, key string) (hexStr string, baseStr string)

func HMACSha512

func HMACSha512(data string, key string) (hexStr string, baseStr string)

func ParseECDSAPrivateKeyFromPEM

func ParseECDSAPrivateKeyFromPEM(privateKeyPEM string) (*ecdsa.PrivateKey, error)

**解析 ECDSA 公钥 **

func ParseECDSAPublicKeyFromCert

func ParseECDSAPublicKeyFromCert(certPEM string) (*ecdsa.PublicKey, error)

func ParseRSAPrivateKey

func ParseRSAPrivateKey(pemData string) (*rsa.PrivateKey, error)

解析 PEM 格式的私钥

func ParseRSAPublicKey

func ParseRSAPublicKey(pemData string) (*rsa.PublicKey, error)

解析 PEM 格式的公钥

func RSADecrypt

func RSADecrypt(method hash.Hash, encryptedBase64 string, privateKey *rsa.PrivateKey) ([]byte, error)

RSA + SHA-512 进行解密

func RSADecryptBySha256

func RSADecryptBySha256(encryptedBase64 string, privateKey *rsa.PrivateKey) ([]byte, error)

RSA + SHA-256 进行解密

func RSADecryptBySha512

func RSADecryptBySha512(encryptedBase64 string, privateKey *rsa.PrivateKey) ([]byte, error)

RSA + SHA-512 进行解密

func RSAEncrypt

func RSAEncrypt(method hash.Hash, plainText []byte, publicKey *rsa.PublicKey) (string, error)

RSA + SHA-512 进行加密

func RSAEncryptBySha256

func RSAEncryptBySha256(plainText []byte, publicKey *rsa.PublicKey) (string, error)

RSA + SHA-256 进行加密

func RSAEncryptBySha512

func RSAEncryptBySha512(plainText []byte, publicKey *rsa.PublicKey) (string, error)

RSA + SHA-512 进行加密

func RSAGenerateKeys

func RSAGenerateKeys(bits int) (privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey, err error)

note: 生成 RSA 公私钥

func RSAParsePrivateKey2Bt

func RSAParsePrivateKey2Bt(privateKey *rsa.PrivateKey) (prvKeyBt []byte, err error)

note: 解析私钥到字节

func RSAParsePublicKey2Bt

func RSAParsePublicKey2Bt(publicKey *rsa.PublicKey) (pubKeyBt []byte, err error)

note: 解析公钥到字节

func Sha256

func Sha256(data string) (hexStr string, baseStr string)

func Sha512

func Sha512(data string) (hexStr string, baseStr string)

Types

type Argon2Config added in v1.0.54

type Argon2Config struct {
	Time      uint32 // Time cose
	MemoryKiB uint32 // Memory cost
	Threads   uint8  // Parallelism degree
	KeyLen    uint32 // KeyLen
	SaltLen   BitLen // Salt
}

func DefaultArgon2Config added in v1.0.54

func DefaultArgon2Config() *Argon2Config

type Argon2Tool added in v1.0.56

type Argon2Tool interface {
	GetConfig() *Argon2Config
	HashToPHC(secret, pepper []byte) (SelfContained string, err error)
	VerifyPhc(phc string, secret, pepper []byte) (result bool, err error)
	VerifyWithPepperSet(phc string, secret []byte, pepperCurrent, pepperOld []byte) (ok bool, rehashWith []byte, err error)
	NeedsRehash(phc string) (bool, error)
	ParsePHC(phc string) (algo, ver string, m uint32, t uint32, p uint8, salt, hash []byte, err error)
}

func NewArgon2Tool added in v1.0.54

func NewArgon2Tool(conf *Argon2Config) Argon2Tool

type BitLen added in v1.0.54

type BitLen int
const (
	Bits16Len    BitLen = 2
	Bits32Len    BitLen = 4
	Bits64Len    BitLen = 8
	Bits128Len   BitLen = 16
	Bits256Len   BitLen = 32
	Bits512Len   BitLen = 64
	Bits1024Len  BitLen = 128
	Bits2048Len  BitLen = 256
	Bits4096Len  BitLen = 512
	Bits8192Len  BitLen = 1024
	Bits16384Len BitLen = 2048
)

type HashTool added in v1.0.62

type HashTool interface {
	Sha256(data []byte) []byte
	Sha512(data []byte) []byte
	HMACSha256(data []byte, key []byte) []byte
	HMACSha512(data []byte, key []byte) []byte
}

func NewHash added in v1.0.61

func NewHash() HashTool

type RandTool added in v1.0.66

type RandTool interface {
	GetAnyBtLen(num int) BitLen
	RandInt(min, max int) int
	RandInt64(min, max int64) int64
	RandLowerString(stringLen int) string
	RandStringRunes(n int) string
	GenValidateCode(width int) string
	RandBytesHexNoErr(btLen BitLen) string
	RandBytesHex(btLen BitLen) (string, error)
	RandBytesBase(btLen BitLen) (string, error)
	RandBytesBaseNoErr(btLen BitLen) string
	RandBytesUrlBase(btLen BitLen) (string, error)
	RandBytesUrlBaseNoErr(btLen BitLen) string
	RandBytes(btLen BitLen) ([]byte, error)
	RandBytesNoErr(btLen BitLen) []byte
}

func NewRand added in v1.0.54

func NewRand() RandTool

Jump to

Keyboard shortcuts

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