gcrypto

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2023 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyLength8  = 8
	KeyLength16 = 16
	KeyLength24 = 24
	KeyLength32 = 32
)

Variables

View Source
var (
	EciesAes128Sha256 = &ECIESParams{
		Hash:      sha256.New,
		hashAlgo:  crypto.SHA256,
		Cipher:    aes.NewCipher,
		BlockSize: aes.BlockSize,
		KeyLen:    16,
	}

	EciesAes256Sha384 = &ECIESParams{
		Hash:      sha512.New384,
		hashAlgo:  crypto.SHA384,
		Cipher:    aes.NewCipher,
		BlockSize: aes.BlockSize,
		KeyLen:    32,
	}

	EciesAes256Sha512 = &ECIESParams{
		Hash:      sha512.New,
		hashAlgo:  crypto.SHA512,
		Cipher:    aes.NewCipher,
		BlockSize: aes.BlockSize,
		KeyLen:    32,
	}
)
View Source
var (
	ErrCipherKey           = errors.New("the secret key is wrong and cannot be decrypted. Please check")
	ErrKeyLengthSixteen    = errors.New("a sixteen or twenty-four or thirty-two length secret key is required")
	ErrKeyLengthEight      = errors.New("a eight-length secret key is required")
	ErrKeyLengthTwentyFour = errors.New("a twenty-four-length secret key is required")
	ErrPaddingSize         = errors.New("padding size error please check the secret key or iv")
	ErrIvAes               = errors.New("a sixteen-length ivaes is required")
	ErrIvDes               = errors.New("a eight-length ivdes key is required")
	ErrRsaBits             = errors.New("bits 1024 or 2048")
	ErrAesSrcBlockSize     = errors.New("the length of src must be a multiple of the block size")
)
View Source
var (
	ErrImport                     = errors.New("ecies: failed to import key")
	ErrInvalidCurve               = errors.New("ecies: invalid elliptic curve")
	ErrInvalidParams              = errors.New("ecies: invalid ECIES parameters")
	ErrInvalidPublicKey           = errors.New("ecies: invalid public key")
	ErrSharedKeyIsPointAtInfinity = errors.New("ecies: shared key is point at infinity")
	ErrSharedKeyTooBig            = errors.New("ecies: shared key params are too big")
	ErrUnsupportedECIESParameters = errors.New("ecies: unsupported ECIES parameters")
	ErrKeyDataTooLong             = errors.New("ecies: can't supply requested key data")
	ErrSharedTooLong              = errors.New("ecies: shared secret is too long")
	ErrInvalidMessage             = errors.New("ecies: invalid message")
)

Functions

func AesCbcDecrypt added in v1.1.3

func AesCbcDecrypt(cipherText, secretKey, ivAes []byte) ([]byte, error)

AesCbcDecrypt @param cipherText 加密后的数据 @param secretKey 密钥 @param ivAes 和secretKey位数一致的偏移向量,默认的偏移量就是密钥

func AesCbcDecryptByBase64 added in v1.1.3

func AesCbcDecryptByBase64(cipherTextBase64 string, secretKey, ivAes []byte) ([]byte, error)

func AesCbcDecryptByHex added in v1.1.3

func AesCbcDecryptByHex(cipherTextHex string, secretKey, ivAes []byte) ([]byte, error)

func AesCbcEncrypt added in v1.1.3

func AesCbcEncrypt(plainText, secretKey, ivAes []byte) (cipherText []byte, err error)

AesCbcEncrypt CBC 密文分组链接模式,可以避免ECB模式的缺陷 @param plainText 待加密的数据 @param secretKey 密钥 @param ivAes 和secretKey位数一致的偏移向量,默认的偏移量就是密钥

func AesCbcEncryptBase64 added in v1.1.3

func AesCbcEncryptBase64(plainText, secretKey, ivAes []byte) (string, error)

func AesCbcEncryptHex added in v1.1.3

func AesCbcEncryptHex(plainText, secretKey, ivAes []byte) (string, error)

func AesCtrDecrypt added in v1.1.3

func AesCtrDecrypt(cipherText, secretKey, ivAes []byte) (plainText []byte, err error)

AesCtrDecrypt AES CTR模式的解密,NoPadding @param cipherText 加密后的数据 @param secretKey 密钥 @param ivAes 和secretKey位数一致的偏移向量,默认的偏移量就是密钥

func AesCtrDecryptByBase64 added in v1.1.3

func AesCtrDecryptByBase64(cipherTextBase64 string, secretKey, ivAes []byte) ([]byte, error)

func AesCtrDecryptByHex added in v1.1.3

func AesCtrDecryptByHex(cipherTextHex string, secretKey, ivAes []byte) ([]byte, error)

func AesCtrEncrypt added in v1.1.3

func AesCtrEncrypt(plainText, secretKey, ivAes []byte) (cipherText []byte, err error)

AesCtrEncrypt AES CTR(计数器模式)模式的加密,NoPadding @param plainText 代加密的数据 @param secretKey 密钥 @param ivAes 和secretKey位数一致的偏移向量,默认的偏移量就是密钥

func AesCtrEncryptBase64 added in v1.1.3

func AesCtrEncryptBase64(plainText, secretKey, ivAes []byte) (string, error)

func AesCtrEncryptHex added in v1.1.3

func AesCtrEncryptHex(plainText, secretKey, ivAes []byte) (string, error)

func AesEcbDecrypt added in v1.1.3

func AesEcbDecrypt(plainText, secretKey []byte) ([]byte, error)

AesEcbDecrypt AES ECB式的解密,NoPadding @param cipherText 加密后的数据 @param secretKey 密钥

func AesEcbDecryptByBase64 added in v1.1.3

func AesEcbDecryptByBase64(cipherTextBase64 string, key []byte) ([]byte, error)

func AesEcbDecryptByHex added in v1.1.3

func AesEcbDecryptByHex(cipherTextHex string, key []byte) ([]byte, error)

func AesEcbEncrypt added in v1.1.3

func AesEcbEncrypt(plainText, secretKey []byte) ([]byte, error)

AesEcbEncrypt AES ECB式的加密,NoPadding @param cipherText 待加密的数据 @param secretKey 密钥

func AesEcbEncryptBase64 added in v1.1.3

func AesEcbEncryptBase64(plainText, key []byte) (string, error)

func AesEcbEncryptHex added in v1.1.3

func AesEcbEncryptHex(plainText, key []byte) (string, error)

func DesCbcDecrypt added in v1.1.3

func DesCbcDecrypt(cipherText, secretKey, ivDes []byte) (plainText []byte, err error)

DesCbcDecrypt DES解密

func DesCbcDecryptByBase64 added in v1.1.3

func DesCbcDecryptByBase64(cipherTextBase64 string, secretKey, ivAes []byte) (plainText []byte, err error)

func DesCbcDecryptByHex added in v1.1.3

func DesCbcDecryptByHex(cipherTextHex string, secretKey, ivAes []byte) (plainText []byte, err error)

func DesCbcEncrypt added in v1.1.3

func DesCbcEncrypt(plainText, secretKey, ivDes []byte) (cipherText []byte, err error)

DesCbcEncrypt DES加密

func DesCbcEncryptBase64 added in v1.1.3

func DesCbcEncryptBase64(plainText, secretKey, ivAes []byte) (cipherTextBase64 string, err error)

func DesCbcEncryptHex added in v1.1.3

func DesCbcEncryptHex(plainText, secretKey, ivAes []byte) (cipherTextHex string, err error)

func EccDecryptByBase64 added in v1.1.3

func EccDecryptByBase64(base64CipherText, base64PriKey string) (plainText []byte, err error)

EccDecryptByBase64 ECC使用私钥对加密后的base64字符串进行解密

func EccDecryptByHex added in v1.1.3

func EccDecryptByHex(hexCipherText, hexPriKey string) (plainText []byte, err error)

EccDecryptByHex ECC使用私钥对加密后的Hex字符串进行解密

func EccEncryptToBase64 added in v1.1.3

func EccEncryptToBase64(plainText []byte, base64PubKey string) (base64CipherText string, err error)

EccEncryptToBase64 ECC使用公钥进行加密后转base64

func EccEncryptToHex added in v1.1.3

func EccEncryptToHex(plainText []byte, hexPubKey string) (hexCipherText string, err error)

EccEncryptToHex ECC使用公钥进行加密后转Hex

func EccSignBase64 added in v1.1.3

func EccSignBase64(msg []byte, base64PriKey string) (base64rSign, base64sSign string, err error)

EccSignBase64 ECC签名转base64

func EccSignHex added in v1.1.3

func EccSignHex(msg []byte, hexPriKey string) (hexrSign, hexsSign string, err error)

EccSignHex ECC签名转Hex

func EccVerifySignBase64 added in v1.1.3

func EccVerifySignBase64(msg []byte, base64rSign, base64sSign, base64PubKey string) bool

EccVerifySignBase64 ECC验证base64格式的签名

func EccVerifySignHex added in v1.1.3

func EccVerifySignHex(msg []byte, hexrSign, hexsSign, hexPubKey string) bool

EccVerifySignHex ECC验证hex格式的签名

func Encrypt added in v1.1.3

func Encrypt(rand io.Reader, pub *PublicKey, m, s1, s2 []byte) (ct []byte, err error)

Encrypt encrypts a message using ECIES as specified in SEC 1, 5.1.

s1 and s2 contain shared information that is not part of the resulting ciphertext. s1 is fed into key derivation, s2 is fed into the MAC. If the shared information parameters aren't being used, they should be nil.

func GenerateRsaKey added in v1.1.3

func GenerateRsaKey(bits int) (*rsa.PrivateKey, error)

GenerateRsaKey 生成公钥和私钥

func HmacSha256 added in v1.1.3

func HmacSha256(key []byte, body string) []byte

func HmacSha256Hex added in v1.1.3

func HmacSha256Hex(key []byte, body string) string

func HmacSha512 added in v1.1.3

func HmacSha512(key []byte, body string) []byte

func HmacSha512Hex added in v1.1.3

func HmacSha512Hex(key []byte, body string) string

func MD5To16Upper added in v1.2.0

func MD5To16Upper(value string) string

MD5To16Upper MD5大写加密16位

func MD5Upper added in v1.2.0

func MD5Upper(value string) string

MD5Upper MD5大写加密32位

func MaxSharedKeyLength added in v1.1.3

func MaxSharedKeyLength(pub *PublicKey) int

MaxSharedKeyLength returns the maximum length of the shared key the public key can produce.

func Md5

func Md5(str string) string

Md5 32位md5加密后字符串

func Md5SaltMulti

func Md5SaltMulti(str string, salt string, iteration int) string

Md5SaltMulti 加盐并多次加密

func Md5To16

func Md5To16(str string) string

Md5To16 16位md5加密后字符串

func Md5WithSalt

func Md5WithSalt(str string, salt string) string

Md5WithSalt 加盐加密

func Murmur128Hash added in v1.1.3

func Murmur128Hash(data []byte) (uint64, uint64)

func Murmur128HashWithSeed added in v1.1.3

func Murmur128HashWithSeed(seed uint32, data []byte) (uint64, uint64)

func Murmur32Hash added in v1.1.3

func Murmur32Hash(data []byte) uint32

func Murmur32HashWithSeed added in v1.1.3

func Murmur32HashWithSeed(seed uint32, data []byte) uint32

func Murmur64Hash added in v1.1.3

func Murmur64Hash(data []byte) uint64

func Murmur64HashWithSeed added in v1.1.3

func Murmur64HashWithSeed(seed uint32, data []byte) uint64

func New32 added in v1.1.3

func New32() hash.Hash32

New32 returns new 32-bit hasher

func New32WithSeed added in v1.1.3

func New32WithSeed(seed uint32) hash.Hash32

New32WithSeed returns new 32-bit hasher set with explicit seed value

func New64 added in v1.1.3

func New64() hash.Hash64

New64 returns a 64-bit hasher

func New64WithSeed added in v1.1.3

func New64WithSeed(seed uint32) hash.Hash64

New64WithSeed returns a 64-bit hasher set with explicit seed value

func PKCS5Padding added in v1.1.3

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

func PKCS5UnPadding added in v1.1.3

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

func RsaDecryptByBase64 added in v1.1.3

func RsaDecryptByBase64(base64CipherText, base64PriKey string) (plainText []byte, err error)

RsaDecryptByBase64 使用私钥解密密文的base64格式

func RsaDecryptByHex added in v1.1.3

func RsaDecryptByHex(hexCipherText, hexPriKey string) (plainText []byte, err error)

RsaDecryptByHex 使用私钥解密密文的hex格式

func RsaEncryptToBase64 added in v1.1.3

func RsaEncryptToBase64(plainText []byte, base64PubKey string) (base64CipherText string, err error)

RsaEncryptToBase64 公钥加密后转base64

func RsaEncryptToHex added in v1.1.3

func RsaEncryptToHex(plainText []byte, hexPubKey string) (hexCipherText string, err error)

RsaEncryptToHex 公钥加密后转hex

func RsaSignBase64 added in v1.1.3

func RsaSignBase64(msg []byte, base64PriKey string) (base64Sign string, err error)

RsaSignBase64 私钥生成签名转base64

func RsaSignHex added in v1.1.3

func RsaSignHex(msg []byte, hexPriKey string) (hexSign string, err error)

RsaSignHex 私钥生成签名转hex

func RsaVerifySignBase64 added in v1.1.3

func RsaVerifySignBase64(msg []byte, base64Sign, base64PubKey string) bool

RsaVerifySignBase64 公钥验证base64格式签名

func RsaVerifySignHex added in v1.1.3

func RsaVerifySignHex(msg []byte, hexSign, hexPubKey string) bool

RsaVerifySignHex 公钥验证hex格式签名

func Sha1 added in v1.1.3

func Sha1(data []byte) []byte

func Sha1Hex added in v1.1.3

func Sha1Hex(data []byte) string

func Sha256 added in v1.1.3

func Sha256(data []byte) []byte

func Sha256Hex added in v1.1.3

func Sha256Hex(data []byte) string

func Sha512 added in v1.1.3

func Sha512(data []byte) []byte

func Sha512Hex added in v1.1.3

func Sha512Hex(data []byte) string

func Sum128 added in v1.1.3

func Sum128(data []byte) (h1 uint64, h2 uint64)

Sum128 returns the MurmurHash3 sum of data. It is equivalent to the following sequence (without the extra burden and the extra allocation):

hasher := New128()
hasher.Write(data)
return hasher.Sum128()

func Sum128WithSeed added in v1.1.3

func Sum128WithSeed(data []byte, seed uint32) (h1 uint64, h2 uint64)

Sum128WithSeed returns the MurmurHash3 sum of data. It is equivalent to the following sequence (without the extra burden and the extra allocation):

hasher := New128WithSeed(seed)
hasher.Write(data)
return hasher.Sum128()

func Sum32 added in v1.1.3

func Sum32(data []byte) uint32

Sum32 使用默认的注重生成哈希值

func Sum32WithSeed added in v1.1.3

func Sum32WithSeed(data []byte, seed uint32) uint32

Sum32WithSeed 使用种子生成哈希值

func Sum64 added in v1.1.3

func Sum64(data []byte) uint64

Sum64 returns the MurmurHash3 sum of data. It is equivalent to the following sequence (without the extra burden and the extra allocation):

hasher := New64()
hasher.Write(data)
return hasher.Sum64()

func Sum64WithSeed added in v1.1.3

func Sum64WithSeed(data []byte, seed uint32) uint64

Sum64WithSeed returns the MurmurHash3 sum of data. It is equivalent to the following sequence (without the extra burden and the extra allocation):

hasher := New64WithSeed(seed)
hasher.Write(data)
return hasher.Sum64()

func TripleDesDecrypt added in v1.1.3

func TripleDesDecrypt(cipherText, secretKey, ivDes []byte) ([]byte, error)

func TripleDesDecryptByBase64 added in v1.1.3

func TripleDesDecryptByBase64(cipherTextBase64 string, secretKey, ivAes []byte) ([]byte, error)

func TripleDesDecryptByHex added in v1.1.3

func TripleDesDecryptByHex(cipherTextHex string, secretKey, ivAes []byte) ([]byte, error)

func TripleDesEncrypt added in v1.1.3

func TripleDesEncrypt(plainText, secretKey, ivDes []byte) ([]byte, error)

func TripleDesEncryptBase64 added in v1.1.3

func TripleDesEncryptBase64(plainText, secretKey, ivAes []byte) (string, error)

func TripleDesEncryptHex added in v1.1.3

func TripleDesEncryptHex(plainText, secretKey, ivAes []byte) (string, error)

func ZeroPadding added in v1.1.3

func ZeroPadding(ciphertext []byte, blockSize int) []byte

func ZeroUnPadding added in v1.1.3

func ZeroUnPadding(origData []byte) []byte

Types

type ECIESParams added in v1.1.3

type ECIESParams struct {
	Hash func() hash.Hash // hash function

	Cipher    func([]byte) (cipher.Block, error) // symmetric cipher
	BlockSize int                                // block size of symmetric cipher
	KeyLen    int                                // length of symmetric key
	// contains filtered or unexported fields
}

func ParamsFromCurve added in v1.1.3

func ParamsFromCurve(curve elliptic.Curve) (params *ECIESParams)

ParamsFromCurve 以下为从以太坊源码/crypt/ecies/params.go中截取过来

type EccKey added in v1.1.3

type EccKey struct {
	PrivateKey string
	PublicKey  string
}

func GenerateEccKeyBase64 added in v1.1.3

func GenerateEccKeyBase64() (EccKey, error)

GenerateEccKeyBase64 生成ECC的私钥和公钥的base64形式

func GenerateEccKeyHex added in v1.1.3

func GenerateEccKeyHex() (EccKey, error)

GenerateEccKeyHex 生成ECC的私钥和公钥的Hex形式

type Hash128 added in v1.1.3

type Hash128 interface {
	hash.Hash
	Sum128() (uint64, uint64)
}

Hash128 represents a 128-bit hasher Hack: the standard api doesn't define any Hash128 interface.

func New128 added in v1.1.3

func New128() Hash128

New128 returns a 128-bit hasher

func New128WithSeed added in v1.1.3

func New128WithSeed(seed uint32) Hash128

New128WithSeed returns a 128-bit hasher set with explicit seed value

type PrivateKey added in v1.1.3

type PrivateKey struct {
	PublicKey
	D *big.Int
}

PrivateKey is a representation of an elliptic curve private key.

func GenerateKey added in v1.1.3

func GenerateKey(rand io.Reader, curve elliptic.Curve, params *ECIESParams) (prv *PrivateKey, err error)

GenerateKey Generate an elliptic curve public / private keypair. If params is nil, the recommended default parameters for the key will be chosen.

func ImportECDSA added in v1.1.3

func ImportECDSA(prv *ecdsa.PrivateKey) *PrivateKey

ImportECDSA Import an ECDSA private key as an ECIES private key.

func (*PrivateKey) Decrypt added in v1.1.3

func (prv *PrivateKey) Decrypt(c, s1, s2 []byte) (m []byte, err error)

Decrypt decrypts an ECIES ciphertext.

func (*PrivateKey) ExportECDSA added in v1.1.3

func (prv *PrivateKey) ExportECDSA() *ecdsa.PrivateKey

ExportECDSA Export an ECIES private key as an ECDSA private key.

func (*PrivateKey) GenerateShared added in v1.1.3

func (prv *PrivateKey) GenerateShared(pub *PublicKey, skLen, macLen int) (sk []byte, err error)

GenerateShared ECDH key agreement method used to establish secret keys for encryption.

type PublicKey added in v1.1.3

type PublicKey struct {
	X *big.Int
	Y *big.Int
	elliptic.Curve
	Params *ECIESParams
}

PublicKey is a representation of an elliptic curve public key.

func ImportECDSAPublic added in v1.1.3

func ImportECDSAPublic(pub *ecdsa.PublicKey) *PublicKey

ImportECDSAPublic Import an ECDSA public key as an ECIES public key.

func (*PublicKey) ExportECDSA added in v1.1.3

func (pub *PublicKey) ExportECDSA() *ecdsa.PublicKey

ExportECDSA Export an ECIES public key as an ECDSA public key.

type RsaKey added in v1.1.3

type RsaKey struct {
	PrivateKey string
	PublicKey  string
}

func GenerateRsaKeyBase64 added in v1.1.3

func GenerateRsaKeyBase64(bits int) (RsaKey, error)

GenerateRsaKeyBase64 生成公钥和私钥的base64格式

func GenerateRsaKeyHex added in v1.1.3

func GenerateRsaKeyHex(bits int) (RsaKey, error)

GenerateRsaKeyHex 生成公钥和私钥的hex格式

Jump to

Keyboard shortcuts

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