gocrypto

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2022 License: Apache-2.0 Imports: 14 Imported by: 5

README

GoCrypto使用说明

Go Reference GoCrypto CI GitHub GitHub go.mod Go version (branch)

特别说明

based on GoRSA, GoCrypt Optimized the following points for packaging:

  • Optimization of public and private keys requires registration and initialization in advance.
  • The encryption machine does not perform base64 processing, and avoids secondary encapsulation of base64 during cross-program transfer or storage
  • The incoming return uses the string type uniformly to avoid conversion trouble
  • Supports RSAWithSHA1 and RSAWithSHA256 signature verification algorithms

此版本新增了AES、DES、3DES、HMAC、HASH等常见加/解密及hash获取方式,结构作了些调整,由之前的版本更新到此版本时一定要注意引用方法包名的变化。 AES/DES块加密时,填充默认采用PKCS7 padding( 如果块大小为64 位(8字节)时,此时PKCS7与PKCS5结果一致).

get & import

go get -u -v github.com/houseme/gocrypto

使用方法

1.构建需要加解密的类型handle
    handleRSA := rsa.NewRSACrypt(secretInfo) // RSA
    handleDES := des.NewDESCrypt(key) // des
    handle3DES := des.NewTripleDESCrypt(key) // 3des
    handleAES := aes.NewAESCrypt(key) // aes
    handleHash := hash.NewHash(hashType) // common hash
    handleHMAC := hash.NewHMAC(hashType, key) // hmac
2.加密、解密、hash、hmac
(1)加密

加密指定字符串,并以指定编码格式输出结果

    encrypt, err := handle.Encrypt(src) // des/3des/aes
(2)解密

解密指定格式编码后的加密串,返回原字符串

    decrypt, err := handle.Decrypt(src) // des/3des/aes
(3)hash/hmac

哈希运算/密钥相关的哈希运算。

    handle.Get([]byte("123456"))// 输出[]byte
    handle.EncodeToString([]byte("123456"), gocrypto.HEX) // 输出为hex格式的字符串

RSA加密、解密、签名、验签

1.设置公私钥信息
secretInfo := rsa.SecretInfo{
    PublicKey:          "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyoiAraTnAbCoqGVOKugFDM2/ms2szXmb3zTOU3ByicH/XPZqy7Eougbs8OQQIoNW4xKw8PNyWf0lf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73O8CTJ0bB24wAKZZEOK3eJeqG/N0q+CiD83ygr8pSZzpE1xvqQp32IgXtgvm7/UmT8cfAp05Z3bF4jcA8uXwodBz4NsVGijlO78PsCooLsArM0CgYEA4lz5pXDEN3w5JwkbspLnUSUS738hne8YM0PchCaww+8sXLS9GLL2CHcvwh6Tv9Mee7r6SdbDI73x118y68WEDDhidiYZCLhXJN2v12ezJOMqH5m9wVJzQOGNv6kPV1EW1WlWxoJQGxCdzbZMLxtTbyTZe3+iAVG++8u6NWMV3dUCgYA1dm1rnQto321kGy+6Z/2OMXTNBeufGwDDDfilzZdTkNwASMhEAW7trLuXcV8bahcsymMUTUevQawOFBnYupq/lAEluSOtq5vZBAF+huAdLJptFiJT6rKFkM5j+z2jW3DJnyMz6UmXT7GTDTVqCWoaBqIFfbsY60NjXlK92YhJzQKBgQDWfQjktbSHasLw9RV0oPRklD+cBhfBgfOpZ+0En3CxR+j+MxhW1gSBQwZS5wxTIGXrEeHlo4UmUe5diExE0dRsi+ToVPM1qw6P1SuwbQd3tXSNmu0NyOWCnfblm/j4YNLFB1p9IK9s5dLRQKJxpG/ribw15FuK6n2QM5vOyIPIvQKBgE5PUzRUCCVsjKAxZOfaZQatMbSzAUSB3bNmUw+F3pDq8ibs6XXvtySowG2femlPDNL7mDMuUc9kYrtTFTQNrEsQGB55wBopX3UxzRjpXJoAQ/d+RPdrSJC7xJyu+URoFI6ae0I3bx1BzjctYU0Rv5DUh+j9leMH5N2S9vHb+vqu",
    PrivateKeyType:     gocrypto.PKCS1,
    PrivateKeyDataType: gocrypto.Base64,
    HashType:           gocrypto.SHA256,
}
handle := rsa.NewRSACrypt(secretInfo)
加密、解密、签名、验签
(1)RSA加密

加密指定字符串,并以指定编码格式输出结果

encrypt, err := handle.Encrypt("test", gocrypto.HEX)
if err != nil {
    fmt.Println("encrypt error :", err)
    return
}
fmt.Println("encrypt data :", encrypt)
(2)RSA解密

解密指定格式编码后的加密串,返回原字符串

decrypt, err := handle.Decrypt(encrypt, gocrypto.HEX)
if err != nil {
    fmt.Println("decrypt error :", err)
    return
}
fmt.Println("decrypt data :", decrypt)
(3)RSA签名

以指定摘要算法签名,并以指定编码格式输出结果,仅适用于RSA。

sign, err := handle.Sign("test", gocrypto.HEX)
if err != nil {
    fmt.Println("sign error :", err)
    return
}
fmt.Println("sign data :", sign)
(4)RSA验签

验证字符串是否是以指定摘要算法编码的签名串的原始字符串,仅适用于RSA。

verifySign, err := handle.VerifySign("test", sign, gocrypto.HEX)
if err != nil {
    fmt.Println("verifySign error :", err)
    return
}
fmt.Println("verifySign result :", verifySign)
(5)RSA私钥加密

加密指定字符串,并以指定编码格式输出结果

encrypt, err := handle.EncryptByPriKey("test", gocrypto.HEX)
if err != nil {
    fmt.Println("encrypt error :", err)
    return
}
fmt.Println("encrypt data :", encrypt)
(6)RSA公钥解密

解密指定格式编码后的加密串,返回原字符串

decrypt, err := handle.DecryptByPublic(encrypt, gocrypto.HEX)
if err != nil {
    fmt.Println("decrypt error :", err)
    return
}
fmt.Println("decrypt data :", decrypt)

License

Licensed under either of Apache License, Version 2.0, LICENSE at your option.

Documentation

Overview

Package gocrypto is a simple encryption and decryption package

Index

Constants

View Source
const (
	PaddingPKCS5 = iota
	PaddingPKCS7
)
View Source
const (
	ECB = iota
	CBC
	CFB
	OFB
)

Variables

This section is empty.

Functions

func DecodeString

func DecodeString(data string, encodedType Encode) (keyDecoded []byte, err error)

DecodeString decodes string data to bytes in designed encoded type

func EncodeToString

func EncodeToString(data []byte, encodeType Encode) (string, error)

EncodeToString encodes data to string with encode types

func GetHash

func GetHash(data []byte, hashType Hash) (h crypto.Hash, hashed []byte, err error)

GetHash gets the crypto hash type & hashed data in different hash types

func GetHashFunc

func GetHashFunc(hashType Hash) (f func() hash.Hash, h crypto.Hash)

GetHashFunc gets the crypto hash func & type in different hash types

func PKCS7Padding

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

PKCS7Padding adds padding data using pkcs7 rules , adapt to PKCS5 &PKCS7

func ParsePrivateKey

func ParsePrivateKey(privateKeyDecoded []byte, keyType Secret) (*rsa.PrivateKey, error)

ParsePrivateKey parses private key bytes to rsa privateKey

func UnPaddingPKCS7

func UnPaddingPKCS7(src []byte, blockSize int) []byte

UnPaddingPKCS7 un-padding src data to original data , adapt to PKCS5 & PKCS7

Types

type Cipher

type Cipher uint

Cipher defines cipher types

type CipherCrypt

type CipherCrypt struct {
	Block cipher.Block
}

CipherCrypt is a simple encryption and decryption package

func (*CipherCrypt) Decrypt

func (cc *CipherCrypt) Decrypt(src []byte, c Cipher, ivs ...[]byte) (dst []byte, err error)

Decrypt decrypts src to dst with cipher & iv, if failed return error src the original encrypted bytes c the defined cipher type,now support CBC, CFB, OFB, ECB ivs the iv for CBC, CFB, OFB mode dst the decrypted bytes

func (*CipherCrypt) DecryptToString

func (cc *CipherCrypt) DecryptToString(encodeType Encode, src []byte, c Cipher, ivs ...[]byte) (dst string, err error)

DecryptToString decrypts src then encodes return data to string encodeType now support String, HEX, Base64

func (*CipherCrypt) Encrypt

func (cc *CipherCrypt) Encrypt(src []byte, c Cipher, ivs ...[]byte) (dst []byte, err error)

Encrypt encrypts src to dst with cipher & iv, if failed return error src the original source bytes c the defined cipher type,now support CBC, CFB, OFB, ECB ivs the iv for CBC, CFB, OFB mode dst the encrypted bytes

func (*CipherCrypt) EncryptToString

func (cc *CipherCrypt) EncryptToString(encodeType Encode, src []byte, c Cipher, ivs ...[]byte) (dst string, err error)

EncryptToString encrypts src then encodes data returned to string encodeType now support String, HEX, Base64

type Crypt

type Crypt uint

Crypt defines crypt types

const (
	RSA Crypt = iota
)

type Encode

type Encode uint

Encode defines the type of bytes encoded to string

const (
	String Encode = iota
	HEX
	Base64
)

type Hash

type Hash uint

Hash for crypto Hash

const (
	MD5 Hash = iota
	SHA1
	SHA224
	SHA256
	SHA384
	SHA512
	Sha512224
	Sha512256
)

type Padding

type Padding uint

Padding defines padding types

type Secret

type Secret uint

Secret defines the private key type

const (
	PKCS1 Secret = iota
	PKCS8
)

Directories

Path Synopsis
Package aes implements the AES encryption algorithm.
Package aes implements the AES encryption algorithm.
Package des implements the DES encryption algorithm.
Package des implements the DES encryption algorithm.
Package hash for crypto Hash
Package hash for crypto Hash
Package rsa provides RSA encryption and decryption, signature and verification functions.
Package rsa provides RSA encryption and decryption, signature and verification functions.

Jump to

Keyboard shortcuts

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