crypto

package
v0.0.0-...-1da4b03 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: GPL-3.0 Imports: 42 Imported by: 0

Documentation

Overview

Package crypto 加密解密

Index

Constants

View Source
const SM4BlockSize = 16

Variables

View Source
var EmptyValue = CValue([]byte{})

Functions

func FillBase64

func FillBase64(s string) string

FillBase64 用`=`补全base64长度

func GetMD5

func GetMD5(text string) string

GetMD5 生成md5字符串

func GetRandom

func GetRandom(l int) []byte

GetRandom 获取随机数据

func GetSHA1

func GetSHA1(text string) string

GetSHA1 生成sha1字符串

func GetSHA256

func GetSHA256(text string) string

GetSHA256 生成sha256字符串

func GetSHA512

func GetSHA512(text string) string

GetSHA512 生成sha512字符串

func GetSM3

func GetSM3(text string) string

GetSM3 生成sm3字符串

func TLSConfigFromFile

func TLSConfigFromFile(certfile, keyfile, rootfile string) (*tls.Config, error)

TLSConfigFromFile 从文件载入证书

func TLSConfigFromPEM

func TLSConfigFromPEM(certpem, keypem, rootpem []byte) (*tls.Config, error)

TLSConfigFromPEM 从pem载入证书

Types

type AES

type AES struct {
	// contains filtered or unexported fields
}

AES aes算法

func NewAES

func NewAES(t AESType) *AES

NewAES 创建一个新的aes加密解密器

func (*AES) Decode

func (w *AES) Decode(b []byte) (string, error)

Decode aes解密

func (*AES) DecodeBase64

func (w *AES) DecodeBase64(s string) (string, error)

DecodeBase64 aes解密base64编码的字符串

func (*AES) Decrypt

func (w *AES) Decrypt(s string) string

Decrypt 兼容旧方法,直接解析base64字符串

func (*AES) Encode

func (w *AES) Encode(b []byte) (CValue, error)

Encode aes加密

func (*AES) Encrypt

func (w *AES) Encrypt(s string) string

Encrypt 兼容旧方法,直接返回base64字符串

func (*AES) EncryptTo

func (w *AES) EncryptTo(s string) CValue

EncryptTo 兼容旧方法,直接返回base64字符串

func (*AES) SetKeyIV

func (w *AES) SetKeyIV(key, iv string) error

SetKeyIV 设置iv和key 如果不设置iv,会生成随机iv并追加在加密结果的头部

func (*AES) SetPadding

func (w *AES) SetPadding(p Padding)

SetPadding 设置填充模式

type AESType

type AESType byte
const (
	// AES128CBC aes128cbc算法
	AES128CBC AESType = iota
	// AES192CBC aes192cbc算法
	AES192CBC
	// AES256CBC aes256cbc算法
	AES256CBC
	// AES128CFB aes128cfb算法
	AES128CFB
	// AES192CFB aes192cfb算法
	AES192CFB
	// AES256CFB aes256cfb算法
	AES256CFB
	// AES128ECB aes128ecb算法
	AES128ECB
	// AES192ECB aes192ecb算法
	AES192ECB
	// AES256ECB aes256ecb算法
	AES256ECB
)

type CValue

type CValue []byte

CValue 加密后的数据,可输出[]byte,hex string,base64string

func (CValue) Base64String

func (v CValue) Base64String() string

Base64String 加密结果以标准base64字符串形式输出

func (CValue) Base64StringNoTail

func (v CValue) Base64StringNoTail() string

Base64StringNoTail 加密结果以标准base64字符串形式输出,去除`=`

func (CValue) Bytes

func (v CValue) Bytes() []byte

Bytes 加密结果

func (CValue) HexString

func (v CValue) HexString() string

HexString 加密结果以hex字符串形式输出

func (CValue) Len

func (v CValue) Len() int

Len 加密结果长度

func (CValue) URLBase64String

func (v CValue) URLBase64String() string

URLBase64String 加密结果以URLbase64字符串形式输出

type CertOpt

type CertOpt struct {
	// 证书包含的域名清单
	DNS []string `json:"dns"`
	// 证书包含的ip清单
	IP []string `json:"ip"`
	// 根证书私钥,未指定或载入错误时,会重新生成私钥和根证书
	RootKey string `json:"root-key"`
	// 根证书,当私钥配置错误时,该参数无效
	RootCa string `json:"root-ca"`
	// 输出目录
	OutPut string `json:"-"`
}

type CompressType

type CompressType byte

CompressType 压缩编码类型

const (
	CompressZlib CompressType = iota
	CompressGZip
	CompressSnappy
	CompressZstd
)

type Compressor

type Compressor struct {
	// contains filtered or unexported fields
}

func NewCompressor

func NewCompressor(t CompressType) *Compressor

func (*Compressor) Decode

func (z *Compressor) Decode(src []byte) ([]byte, error)

func (*Compressor) Encode

func (z *Compressor) Encode(src []byte) ([]byte, error)

type ECC

type ECC struct {
	// contains filtered or unexported fields
}

ECC ecc算法

func NewECC

func NewECC() *ECC

NewECC 创建一个新的ecc算法器

签名算法采用sha256
支持 openssl ecparam -name prime256v1/secp384r1 格式的密钥

func (*ECC) CreateCert

func (w *ECC) CreateCert(opt *CertOpt) error

CreateCert 创建基于ecc算法的数字证书,opt.RootKey无效时,会重新创建私钥和根证书

func (*ECC) Decode

func (w *ECC) Decode(b []byte) (string, error)

Decode ecc解密

func (*ECC) DecodeBase64

func (w *ECC) DecodeBase64(s string) (string, error)

DecodeBase64 从base64字符串解码

func (*ECC) Decrypt

func (w *ECC) Decrypt(s string) string

Decrypt 兼容旧方法,直接解析base64字符串

func (*ECC) Encode

func (w *ECC) Encode(b []byte) (CValue, error)

Encode ecc加密

func (*ECC) Encrypt

func (w *ECC) Encrypt(s string) string

Encrypt 加密,兼容旧方法,直接返回base64字符串

func (*ECC) EncryptTo

func (w *ECC) EncryptTo(s string) CValue

EncryptTo 加密字符串

func (*ECC) GenerateKey

func (w *ECC) GenerateKey(ec ECShortName) (CValue, CValue, error)

GenerateKey 创建ecc密钥对

返回,pubkey,prikey,error

func (*ECC) Keys

func (w *ECC) Keys() (CValue, CValue)

Keys 返回公钥和私钥

func (*ECC) SetPrivateKey

func (w *ECC) SetPrivateKey(key string) error

SetPrivateKey 设置base64编码的私钥

func (*ECC) SetPrivateKeyFromFile

func (w *ECC) SetPrivateKeyFromFile(keyPath string) error

SetPrivateKeyFromFile 从文件获取私钥

func (*ECC) SetPublicKey

func (w *ECC) SetPublicKey(key string) error

SetPublicKey 设置base64编码的公钥

func (*ECC) SetPublicKeyFromFile

func (w *ECC) SetPublicKeyFromFile(keyPath string) error

SetPublicKeyFromFile 从文件获取公钥

func (*ECC) Sign

func (w *ECC) Sign(b []byte) (CValue, error)

Sign 签名

func (*ECC) ToFile

func (w *ECC) ToFile(pubfile, prifile string) error

ToFile 创建ecc密钥到文件

func (*ECC) VerifySign

func (w *ECC) VerifySign(signature, data []byte) (bool, error)

VerifySign 验证签名

func (*ECC) VerifySignFromBase64

func (w *ECC) VerifySignFromBase64(signature string, data []byte) (bool, error)

VerifySignFromBase64 验证base64格式的签名

func (*ECC) VerifySignFromHex

func (w *ECC) VerifySignFromHex(signature string, data []byte) (bool, error)

VerifySignFromHex 验证hexstring格式的签名

type ECShortName

type ECShortName byte
var (
	// ECPrime256v1 as elliptic.P256() and openssl ecparam -name prime256v1
	ECPrime256v1 ECShortName = 1
	// ECSecp384r1 as elliptic.P384() and openssl ecparam -name secp384r1
	ECSecp384r1 ECShortName = 2
)

type HASH

type HASH struct {
	// contains filtered or unexported fields
}

HASH hash算法

func NewHash

func NewHash(t HashType, hmacKey []byte) *HASH

NewHash creates a new hash algorithm instance based on the provided hash type and HMAC key. It uses a sync.Pool to reuse hash instances, improving performance.

Parameters: - t: The hash type, which can be one of the following: HashMD5, HashHMACSHA1, HashHMACSHA256, HashSHA1, HashSHA256, HashSHA512, HashSM3. - hmacKey: The HMAC key to be used for HashHMACSHA1 and HashHMACSHA256 hash types. If not required, an empty slice can be passed.

Returns: - A pointer to the newly created HASH instance.

func (*HASH) Hash

func (w *HASH) Hash(b []byte) CValue

Hash 计算哈希值

type HashType

type HashType byte
const (
	// HashMD5 md5算法
	HashMD5 HashType = iota
	// HashSHA256 sha256算法
	HashSHA256
	// HashSHA512 sha512算法
	HashSHA512
	// HashHMACSHA1 hmacsha1摘要算法
	HashHMACSHA1
	// HashHMACSHA256 hmacsha256摘要算法
	HashHMACSHA256
	// HashSHA1 sha1算法
	HashSHA1
	// HashSM3 国密sm3
	HashSM3
)

type Padding

type Padding byte
var (
	NoPadding    Padding = 0
	Pkcs5Padding Padding = 1
	Pkcs7Padding Padding = 2
	ZeroPadding  Padding = 3
)

type RSA

type RSA struct {
	// contains filtered or unexported fields
}

RSA rsa算法

func NewRSA

func NewRSA() *RSA

NewRSA 创建一个新的rsa算法器

签名算法采用sha256

func (*RSA) CreateCert

func (w *RSA) CreateCert(opt *CertOpt) error

CreateCert 创建基于rsa算法的数字证书,opt.RootKey无效时,会重新创建私钥和根证书

func (*RSA) Decode

func (w *RSA) Decode(b []byte) (string, error)

Decode 解码

func (*RSA) DecodeBase64

func (w *RSA) DecodeBase64(s string) (string, error)

DecodeBase64 从base64字符串解码

func (*RSA) Decrypt

func (w *RSA) Decrypt(s string) string

Decrypt 兼容旧方法,直接解析base64字符串

func (*RSA) Encode

func (w *RSA) Encode(b []byte) (CValue, error)

Encode 编码

func (*RSA) Encrypt

func (w *RSA) Encrypt(s string) string

Encrypt 兼容旧方法,直接返回base64字符串

func (*RSA) EncryptTo

func (w *RSA) EncryptTo(s string) CValue

EncryptTo 兼容旧方法,直接返回base64字符串

func (*RSA) GenerateKey

func (w *RSA) GenerateKey(bits RSABits) (CValue, CValue, error)

GenerateKey 创建rsa密钥对

返回,pubkey,prikey,error

func (*RSA) Keys

func (w *RSA) Keys() (CValue, CValue)

Keys 返回公钥和私钥

func (*RSA) SetPrivateKey

func (w *RSA) SetPrivateKey(key string) error

SetPrivateKey 设置base64编码的私钥

func (*RSA) SetPrivateKeyFromFile

func (w *RSA) SetPrivateKeyFromFile(keyPath string) error

SetPrivateKeyFromFile 从文件获取私钥

func (*RSA) SetPublicKey

func (w *RSA) SetPublicKey(key string) error

SetPublicKey 设置base64编码的公钥

func (*RSA) SetPublicKeyFromFile

func (w *RSA) SetPublicKeyFromFile(keyPath string) error

SetPublicKeyFromFile 从文件获取公钥

func (*RSA) Sign

func (w *RSA) Sign(b []byte) (CValue, error)

Sign 签名,返回签名,hash值

func (*RSA) ToFile

func (w *RSA) ToFile(pubfile, prifile string) error

ToFile 创建rsa密钥到文件

func (*RSA) VerifySign

func (w *RSA) VerifySign(signature, data []byte) (bool, error)

VerifySign 验证签名

func (*RSA) VerifySignFromBase64

func (w *RSA) VerifySignFromBase64(signature string, data []byte) (bool, error)

VerifySignFromBase64 验证base64格式的签名

func (*RSA) VerifySignFromHex

func (w *RSA) VerifySignFromHex(signature string, data []byte) (bool, error)

VerifySignFromHex 验证hexstring格式的签名

type RSABits

type RSABits byte
var (
	RSA2048 RSABits = 1
	RSA4096 RSABits = 2
)

type SM2

type SM2 struct {
	// contains filtered or unexported fields
}

func NewSM2

func NewSM2() *SM2

NewSM2 创建一个新的sm2算法器

func (*SM2) CreateCert

func (w *SM2) CreateCert(opt *CertOpt) error

CreateCert 创建基于sm2算法的数字证书,opt.RootKey无效时,会重新创建私钥和根证书

func (*SM2) Decode

func (w *SM2) Decode(b []byte) (string, error)

Decode sm2解密

func (*SM2) DecodeBase64

func (w *SM2) DecodeBase64(s string) (string, error)

DecodeBase64 从base64字符串解码

func (*SM2) Decrypt

func (w *SM2) Decrypt(s string) string

Decrypt 兼容旧方法,直接解析base64字符串

func (*SM2) Encode

func (w *SM2) Encode(b []byte) (CValue, error)

Encode sm2加密

func (*SM2) EncodeAsn1

func (w *SM2) EncodeAsn1(b []byte) (CValue, error)

EncodeAsn1 sm2加密

func (*SM2) Encrypt

func (w *SM2) Encrypt(s string) string

Encrypt 兼容旧方法,直接返回base64字符串

func (*SM2) EncryptTo

func (w *SM2) EncryptTo(s string) CValue

EncryptTo 兼容旧方法,直接返回base64字符串

func (*SM2) GenerateKey

func (w *SM2) GenerateKey() (CValue, CValue, error)

GenerateKey 创建sm2密钥对

返回,pubkey,prikey,error

func (*SM2) Keys

func (w *SM2) Keys() (CValue, CValue)

Keys 返回公钥和私钥

func (*SM2) SetPrivateKey

func (w *SM2) SetPrivateKey(key string) error

SetPrivateKey 设置base64编码的私钥

func (*SM2) SetPrivateKeyFromFile

func (w *SM2) SetPrivateKeyFromFile(keyPath string) error

SetPrivateKeyFromFile 从文件获取私钥

func (*SM2) SetPublicKey

func (w *SM2) SetPublicKey(key string) error

SetPublicKey 设置base64编码的公钥

func (*SM2) SetPublicKeyFromFile

func (w *SM2) SetPublicKeyFromFile(keyPath string) error

SetPublicKeyFromFile 从文件获取公钥

func (*SM2) Sign

func (w *SM2) Sign(b []byte) (CValue, error)

Sign 签名

func (*SM2) ToFile

func (w *SM2) ToFile(pubfile, prifile string) error

ToFile 创建ecc密钥到文件

func (*SM2) VerifySign

func (w *SM2) VerifySign(signature, data []byte) (bool, error)

VerifySign 验证签名

func (*SM2) VerifySignFromBase64

func (w *SM2) VerifySignFromBase64(signature string, data []byte) (bool, error)

VerifySignFromBase64 验证base64格式的签名

func (*SM2) VerifySignFromHex

func (w *SM2) VerifySignFromHex(signature string, data []byte) (bool, error)

VerifySignFromHex 验证hexstring格式的签名

type SM4

type SM4 struct {
	// contains filtered or unexported fields
}

sm4

func NewSM4

func NewSM4(t SM4Type) *SM4

NewSM4 创建一个新的sm4算法器

func (*SM4) Decode

func (w *SM4) Decode(b []byte) (string, error)

Decode sm4解密

func (*SM4) DecodeBase64

func (w *SM4) DecodeBase64(s string) (string, error)

DecodeBase64 解密base64编码的字符串

func (*SM4) Decrypt

func (w *SM4) Decrypt(s string) string

Decrypt 兼容旧方法,直接解析base64字符串

func (*SM4) Encode

func (w *SM4) Encode(b []byte) (CValue, error)

Encode sm4加密

func (*SM4) Encrypt

func (w *SM4) Encrypt(s string) string

Encrypt 兼容旧方法,直接返回base64字符串

func (*SM4) EncryptTo

func (w *SM4) EncryptTo(s string) CValue

EncryptTo 兼容旧方法,直接返回base64字符串

func (*SM4) SetKeyIV

func (w *SM4) SetKeyIV(key, iv []byte) error

SetKeyIV 设置iv和key 如果不设置iv,会生成随机iv并追加在加密结果的头部

type SM4Type

type SM4Type byte
const (
	// SM4CBC SM4 CBC算法
	SM4CBC SM4Type = iota
	// SM4CFB SM4 CFB算法
	SM4CFB
	// SM4OFB SM4 CBC算法
	SM4OFB
	// SM4ECB SM4 CFB算法
	SM4ECB
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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