cryptoutil

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: MIT Imports: 26 Imported by: 0

README

cryptoutil 加密相关函数

  1. rsa、ecdsa、sm2 非对称加密算法,加密、解密和签名
  2. 秘钥对生成

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlockDecrypt

func BlockDecrypt(block cipher.Block, mode BlockStreamMode, value []byte) ([]byte, error)

func BlockEncrypt

func BlockEncrypt(block cipher.Block, mode BlockStreamMode, value []byte) ([]byte, error)

func DecodePemHexBase64

func DecodePemHexBase64(data []byte) ([]byte, error)

DecodePemHexBase64 解析pem或者hex或者base64编码成der编码.

func DecryptByAesCBC

func DecryptByAesCBC(encrypted, key, iv []byte) (decrypted []byte, err error)

DecryptByAesCBC cbc模式解密.

func DecryptByAesCTR

func DecryptByAesCTR(origData, key []byte) (encrypted []byte, err error)

func DecryptByRSA

func DecryptByRSA(privateKey *rsa.PrivateKey, ciphertext []byte) ([]byte, error)

DecryptByRSA 使用RSA私钥解密.

func DecryptByRSABytes

func DecryptByRSABytes(privateKey []byte, ciphertext []byte) ([]byte, error)

DecryptByRSABytes 使用RSA私钥解密.

func DecryptBySM2

func DecryptBySM2(privateKey *sm2.PrivateKey, ciphertext []byte) ([]byte, error)

DecryptBySM2 使用SM2私钥解密.

func DecryptBySM2Bytes

func DecryptBySM2Bytes(privateKey []byte, ciphertext []byte) (text []byte, err error)

func EncryptByAesCBC

func EncryptByAesCBC(origData, key, iv []byte) (encrypted []byte, err error)

EncryptByAesCBC 加密 AES-128 key长度:16, 24, 32 bytes 对应 AES-128, AES-192, AES-256. CBC比EBC更安全,但不可并行.

func EncryptByAesCTR

func EncryptByAesCTR(origData, key []byte) (encrypted []byte, err error)

func EncryptByRSA

func EncryptByRSA(publicKey *rsa.PublicKey, content []byte) ([]byte, error)

EncryptByRSA 使用RSA公钥加密.

func EncryptByRSABytes

func EncryptByRSABytes(publicKey, content []byte) ([]byte, error)

EncryptByRSABytes 使用RSA公钥加密.

func GenerateBase64Key

func GenerateBase64Key(secretLength core.SecretKeyLengthType,
	secretFormat core.SecretKeyFormatType) (pkStr, pbkStr string, err error)

GenerateBase64Key 生成base64编码的公私钥.

func GenerateECDSAKey

func GenerateECDSAKey(privateWriter, publicWriter io.Writer, c elliptic.Curve) error

GenerateECDSAKey 生成PEM格式ECDSA公私钥,写入到io.Writer中.

func GenerateECDSAKeyToMemory

func GenerateECDSAKeyToMemory(c elliptic.Curve) (privateBytes []byte, publicBytes []byte, err error)

GenerateECDSAKeyToMemory 生成PEM格式ECDSA公私钥,返回字节格式.

func GenerateRSAKey

func GenerateRSAKey(privateWriter, publicWriter io.Writer, bits RSABit) error

GenerateRSAKey 生成PEM格式PKCS1的RSA公私钥,写入到io.Writer中.

func GenerateRSAKeyToMemory

func GenerateRSAKeyToMemory(bits RSABit) (privateBytes []byte, publicBytes []byte, err error)

GenerateRSAKeyToMemory 生成PEM格式PKCS1的RSA公私钥,返回字节格式.

func GenerateSSHKey

func GenerateSSHKey(bits RSABit) (pkBytes []byte, pbkBytes []byte, err error)

GenerateSSHKey 生成ssh密钥队.

func HMacMD5

func HMacMD5(data []byte, salt ...[]byte) string

HMacMD5 md5摘要算法,使用hmac算法生成hash,参数salt是多参数,实现多态的行为. 参考github.com/dxvgef/gommon/encrypt/sha.go中的hash算法.

func HMacSha256

func HMacSha256(key []byte, value []byte) string

HMacSha256 生成hmac256摘要

func MD5

func MD5(str []byte, salt ...[]byte) string

MD5 md5摘要算法,可以加盐,参数salt是多参数,实现多态的行为.

func ParseRsaPrivateKey

func ParseRsaPrivateKey(derBytes []byte) (privateKey *rsa.PrivateKey, err error)

ParseRsaPrivateKey 解析私钥,derBytes可以使用DecodePemHexBase64函数获取.

func ParseRsaPublicKey

func ParseRsaPublicKey(derBytes []byte) (publicKey *rsa.PublicKey, err error)

ParseRsaPublicKey 解析公钥,derBytes可以使用DecodePemHexBase64函数获取.

func ParseSM2PublicKey

func ParseSM2PublicKey(derBytes []byte) (publicKey *sm2.PublicKey, err error)

ParseSM2PublicKey 解析公钥,derBytes可以使用DecodePemHexBase64函数获取.

func RsaPrivateDecrypt

func RsaPrivateDecrypt(privateKeyBytes, cipherBytes []byte) (textBytes []byte, err error)

RsaPrivateDecrypt 解析rsa私钥,参数privateKeyStr必须是hex、base64或者是pem编码.

func RsaPublicEncrypt

func RsaPublicEncrypt(publicKeyBytes, textBytes []byte) ([]byte, error)

RsaPublicEncrypt Rsa公钥加密,参数publicKeyStr必须是hex、base64或者是pem编码.

func SM2PublicEncrypt

func SM2PublicEncrypt(publicKeyBytes, textBytes []byte) ([]byte, error)

SM2PublicEncrypt sm2公钥加密,参数publicKeyStr必须是hex、base64或者是pem编码.

func SignByRSA

func SignByRSA(key *rsa.PrivateKey, licenseBytes []byte) (license string, err error)

SignByRSA 使用rsa私钥对象指针签名字符串,返回base64编码的license.

func SignByRSABytes

func SignByRSABytes(key, licenseBytes []byte) (string, error)

SignByRSABytes 使用rsa私钥签名字符串,返回base64编码的license.

func SignBySM2

func SignBySM2(privateKey *sm2.PrivateKey, licenseBytes []byte) (license string, err error)

SignBySM2 使用sm2私钥对象指针签名字符串,返回base64编码的license.

func SignBySM2Bytes

func SignBySM2Bytes(privateKey, licenseBytes []byte) (license string, err error)

SignBySM2Bytes 使用sm2私钥签名字符串,返回base64编码的license.

func VerifyByRSA

func VerifyByRSA(publicKeyBase64, licenseCode string) (license string, valid bool, err error)

VerifyByRSA 使用rsa公钥验证签名的license.

func VerifyBySM2

func VerifyBySM2(publicKeyBase64, licenseCode string) (license string, valid bool, err error)

VerifyBySM2 使用sm2公钥验证签名的license.

Types

type BlockStreamMode

type BlockStreamMode string
const (
	CTR BlockStreamMode = "CTR"
	CFB BlockStreamMode = "CFB"
	OFB BlockStreamMode = "OFB"
	RC4 BlockStreamMode = "RC4"
)

type RSABit

type RSABit int
const (
	RSA1024 RSABit = 1024
	RSA2048 RSABit = 2048
)

Jump to

Keyboard shortcuts

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