crypto

package
v0.6.8 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2025 License: MIT Imports: 21 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BcryptHash

func BcryptHash(str string) (string, error)

Deprecated

BcryptHash 对传入字符串进行bcrypt哈希

func BcryptHash2 added in v0.6.3

func BcryptHash2(password []byte) ([]byte, error)

func BcryptMatch

func BcryptMatch(hash string, str string) bool

Deprecated

BcryptMatch 对传入字符串和哈希字符串进行比对,str为明文

func BcryptMatch2 added in v0.6.3

func BcryptMatch2(hashedPassword []byte, password []byte) error

func GenCertificate

func GenCertificate() (cert tls.Certificate, err error)

func GenerateKeyPair

func GenerateKeyPair() (rawCert, rawKey []byte, err error)

func GenerateRSAKey

func GenerateRSAKey(bits int) (Private []byte, Public []byte, err error)

生成RSA私钥和公钥,pem格式

func GenerateRandomSeed

func GenerateRandomSeed() int64

GenerateRandomSeed 生成随机种子

func GenerateSM2Key

func GenerateSM2Key() (pubKey, priKey []byte, err error)

func GenerateTLSConfig

func GenerateTLSConfig() (*tls.Config, error)

Setup a bare-bones TLS config for the server

例如:添加quic协议支持
config.NextProtos = append(config.NextProtos, "quic")

func LoadX509KeyPairTLSConfig

func LoadX509KeyPairTLSConfig(certFile, keyFile string) (*tls.Config, error)

func PKCS7Padding

func PKCS7Padding(plainText []byte, blockSize int) []byte

func PKCS7UnPadding

func PKCS7UnPadding(plainText []byte, blockSize int) ([]byte, error)

func RSA_DecryptOAEP

func RSA_DecryptOAEP(cipherText []byte, privateKey []byte, label []byte) ([]byte, error)

RSA解密,填充模式OAEP,private为私钥的pem格式

func RSA_DecryptPKCS1v15

func RSA_DecryptPKCS1v15(cipherText []byte, privateKey []byte) ([]byte, error)

RSA解密,填充模式PKCS1v15,private为私钥的pem格式

func RSA_EncryptOAEP

func RSA_EncryptOAEP(plainText []byte, publicKey []byte, label []byte) ([]byte, error)

RSA加密,填充方式为OAEP,publicKey为公钥的pem格式。 对结果进行base64编码可以提高可读性。

The random parameter is used as a source of entropy to ensure that encrypting the same message twice doesn't result in the same ciphertext.

The label parameter may contain arbitrary data that will not be encrypted, but which gives important context to the message. For example, if a given public key is used to encrypt two types of messages then distinct label values could be used to ensure that a ciphertext for one purpose cannot be used for another by an attacker. If not required it can be empty.

func RSA_EncryptPKCS1v15

func RSA_EncryptPKCS1v15(plainText []byte, publicKey []byte) ([]byte, error)

RSA加密,填充方式为PKCS1v15,publicKey为公钥的pem格式。 对结果进行base64编码可以提高可读性。

The message must be no longer than the length of the public modulus minus 11 bytes.

WARNING: use of this function to encrypt plaintexts other than session keys is dangerous. Use RSA OAEP in new protocols.

在RSA攻击中,存在着“小明文攻击“的方式;
在明文够小时,密文也够小,直接开e次方即可;
在明文有点小时,如果e也较小,可用pow(m,e)=n*k+c穷举k尝试爆破,所以,比如说,在选择明文攻击中,单纯的RSA非常容易被破解。
于是,我们就像将密文进行一下填充,最好让密文都等长。
但是填充方式也是很讲究的;不好的填充规则往往仅仅有限的增加了攻击的难度,或者难以实现等长密文。
于是我们就查到了OAEP——最优非对称加密填充。

func RandNByte

func RandNByte(n int) []byte

RandNByte returns a slice of n random bytes.

func RsaSignPKCS1v15

func RsaSignPKCS1v15(src []byte, privateKey []byte) ([]byte, error)

rsa数字签名,private为私钥的pem格式

func RsaVerifyPKCS1v15

func RsaVerifyPKCS1v15(src []byte, sign []byte, publicKey []byte) error

rsa数字验签,publicKey为公钥的pem格式

func SM2DecryptAsn1

func SM2DecryptAsn1(priKey, cipherText []byte) (plainText []byte, err error)

func SM2EncryptAsn1

func SM2EncryptAsn1(pubKey, plainText []byte) (cipherText []byte, err error)

返回asn.1编码格式的密文内容

Types

type AESCBC added in v0.6.2

type AESCBC struct {
	cipher.Block
	// contains filtered or unexported fields
}

func NewAESCBC added in v0.6.2

func NewAESCBC(key []byte) (AESCBC, error)

func NewAESCBCFromHex added in v0.6.2

func NewAESCBCFromHex(hexKey string) (AESCBC, error)

func (AESCBC) Decrypt added in v0.6.2

func (c AESCBC) Decrypt(data []byte) (plaintext []byte, err error)

func (AESCBC) Encrypt added in v0.6.2

func (c AESCBC) Encrypt(data []byte) ([]byte, error)

type AESCBC2 added in v0.6.2

type AESCBC2 struct {
	cipher.Block
	// contains filtered or unexported fields
}

AESCBC2 puts iv at the end of the ciphertext

func NewAESCBC2 added in v0.6.2

func NewAESCBC2(key []byte) (AESCBC2, error)

func NewAESCBCFromHex2 added in v0.6.2

func NewAESCBCFromHex2(hexKey string) (AESCBC2, error)

func (AESCBC2) Decrypt added in v0.6.2

func (c AESCBC2) Decrypt(data []byte) (plaintext []byte, err error)

func (AESCBC2) Encrypt added in v0.6.2

func (c AESCBC2) Encrypt(data []byte) ([]byte, error)

type AESGCM added in v0.6.3

type AESGCM struct {
	cipher.AEAD
	// contains filtered or unexported fields
}

func NewAESGCM added in v0.6.3

func NewAESGCM(key []byte) (AESGCM, error)

func NewAESGCMFromHex added in v0.6.3

func NewAESGCMFromHex(hexKey string) (AESGCM, error)

func (AESGCM) Decrypt added in v0.6.3

func (c AESGCM) Decrypt(data []byte) ([]byte, error)

Decrypt decrypts data with a nonce, the nonce is the first part of the plaintext(12 bytes).

Data storage will be reused, so the data will be modified.

func (AESGCM) DecryptAuth added in v0.6.3

func (c AESGCM) DecryptAuth(data []byte, additionalData ...[]byte) ([]byte, error)

DecryptAuth decrypts data with a nonce, the nonce is the first part of the plaintext(12 bytes).

Data storage will be reused, so the data will be modified.

func (AESGCM) Encrypt added in v0.6.3

func (c AESGCM) Encrypt(data []byte) ([]byte, error)

func (AESGCM) EncryptAuth added in v0.6.3

func (c AESGCM) EncryptAuth(data []byte, additionalData ...[]byte) ([]byte, error)

func (AESGCM) EncryptWithNonce added in v0.6.3

func (c AESGCM) EncryptWithNonce(data []byte, additionalData ...[]byte) ([]byte, error)

EncryptWithNonce encrypts data with a nonce, the nonce is the first part of the plaintext(12 bytes).

Data storage will not be reused, so the data will not be modified. At least in go1.24, this is the case.

type CbcAESCrypt

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

初始化向量(IV,Initialization Vector)是许多任务作模式中用于将加密随机化的一个位块, 由此即使同样的明文被多次加密也会产生不同的密文,避免了较慢的重新产生密钥的过程。 对于CBC和CFB,重用IV会导致泄露明文首个块的某些信息,亦包括两个不同消息中相同的前缀。 对于OFB和CTR而言,重用IV会导致完全失去安全性。另外,在CBC模式中,IV在加密时必须是无法预测的;

AES CBC加密时,用iv和key去加密第一个块,然后用第一个块的加密数据作为下一个块的iv,依次迭代。 解密时,用n-1个块的加密数据作为iv和key去解密第n个块(n>1),只有第一个块才会用加密时的iv去解密第一个块。 所以如果解密时,使用了错误的iv,出问题的数据只有第一个块。

分组加密在对明文加密的时候,并不是把整个明文一股脑加密成一整段密文, 而是把明文拆分成一个个独立的明文块, AES的每一个明文块长度128bit(16字节),则记块长度为BlockSize = 16。 初始化向量IV是一个长度为BlockSize的随机字节数组,对应一个伪随机数。

func NewAESCrypt

func NewAESCrypt(SecretKey []byte) (*CbcAESCrypt, error)

Deprecated

NewAESCryptFromHex 创建AES加密器。 第三方库使用 CBC模式,PKCS5填充。

func NewAESCryptFromHex

func NewAESCryptFromHex(HexSecretKey string) (*CbcAESCrypt, error)

Deprecated

NewAESCryptFromHex 创建AES加密器, HexSecretKey为16进制字符串 CBC模式,PKCS5填充

func (*CbcAESCrypt) Decrypt

func (a *CbcAESCrypt) Decrypt(cipherText []byte) ([]byte, error)

Deprecated

Decrypt 解密,cipherText 为 密文+16字节IV。

func (*CbcAESCrypt) Encrypt

func (a *CbcAESCrypt) Encrypt(plainText []byte) ([]byte, error)

Deprecated

Encrypt 加密后返回 密文+16字节IV。

type SymmetricCipher added in v0.6.2

type SymmetricCipher interface {
	Encrypt(data []byte) ([]byte, error)
	Decrypt(data []byte) ([]byte, error)
}

Jump to

Keyboard shortcuts

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