xcrypto

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2021 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPaddingSize indicates bad padding size
	ErrPaddingSize = errors.New("padding size error")
	// ErrNotFullPadding indicates bad padding size
	ErrNotFullPadding = errors.New("need a multiple of the block size")
)
View Source
var (
	// ErrPrivateKey indicates the invalid private key.
	ErrPrivateKey = errors.New("failed to parse private PEM block containing the key")
	// ErrPublicKey indicates the invalid public key.
	ErrPublicKey = errors.New("failed to parse public PEM block containing the key")
	// ErrNotRsaKey indicates the invalid RSA key.
	ErrNotRsaKey = errors.New("key type is not RSA")
)
View Source
var ErrInvalidEncodeMode = errors.New("unsupported encode mode")

ErrInvalidEncodeMode unsupported encode mode

Functions

func DecodeString

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

DecodeString decodes string data to bytes in designed encoded type

func EncodeToString

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

EncodeToString encodes data to string with encode type

func GenRsaKey

func GenRsaKey(bits int) (string, string, error)

GenRsaKey return publicKey, privateKey, error bits: 1024, 2048, 3072, 4096

func Hash

func Hash(src string, mode HashMode) (dst []byte)

Hash hash input data using assigned hash mode

func HashToString

func HashToString(src string, mode HashMode, encode Encode) (dst string, err error)

HashToString hash input to assigned encode mode.

func HmacSHA256

func HmacSHA256(key []byte, src string) (dst []byte)

HmacSHA256 returns HMAC bytes for src with the given key

func HmacSHA256ToString

func HmacSHA256ToString(key []byte, src string, encode Encode) (dst string, err error)

HmacSHA256ToString returns hmac hash string with the given key and assigned encode mode hash mode: md5, sha1, sha256, sha512, default md5

Types

type Aes

type Aes interface {
	Encrypt(src []byte, ivs ...[]byte) (dst []byte, err error)
	EncryptToString(src []byte, mode Encode, ivs ...[]byte) (dst string, err error)
	Decrypt(src []byte, ivs ...[]byte) (dst []byte, err error)
	DecryptFromString(src string, mode Encode, ivs ...[]byte) (dst []byte, err error)
}

Aes aes crypt interface definition

type AesCbcCrypto

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

AesCbcCrypt aes cbc mode

func NewAesCbcCrypto

func NewAesCbcCrypto(secret string, iv ...byte) (*AesCbcCrypto, error)

NewAesCbcCrypto returns a aes cbc mode crypto

func (*AesCbcCrypto) Decrypt

func (acc *AesCbcCrypto) Decrypt(src []byte, ivs ...[]byte) (dst []byte, err error)

Decrypt decrypt input slice using assigned ivs

func (*AesCbcCrypto) DecryptFromString

func (acc *AesCbcCrypto) DecryptFromString(src string, mode Encode, ivs ...[]byte) (dst []byte, err error)

DecryptFromString decrypt input string using assigned encode mode encode mode: Base64,HEX or Plain String

func (*AesCbcCrypto) Encrypt

func (acc *AesCbcCrypto) Encrypt(src []byte, ivs ...[]byte) (dst []byte, err error)

Encrypt encrypt input slice using assigned ivs

func (*AesCbcCrypto) EncryptToString

func (acc *AesCbcCrypto) EncryptToString(src []byte, mode Encode, ivs ...[]byte) (dst string, err error)

EncryptToString encrypt input slice using assigned ivs and return encode encrypt string encode mode: Base64,HEX or Plain String

type Encode

type Encode uint

Encode defines the type of bytes encoded to string

const (
	PlainText Encode = iota // plain text, no encode just string
	HEX                     // hex encode
	Base64                  // base64 encode
)

type HashMode

type HashMode uint

Hash for crypto Hash

const (
	MD5 HashMode = iota
	SHA1
	SHA256
	SHA512
)

type RsaConfig

type RsaConfig struct {
	PublicKeyPath  string
	PrivateKeyPath string
}

type RsaCrypt

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

func NewRsaCrypt

func NewRsaCrypt(config *RsaConfig) (*RsaCrypt, error)

NewRsaCrypt init with the RSA config

func (*RsaCrypt) Decrypt

func (rc *RsaCrypt) Decrypt(src []byte) (dst []byte, err error)

Decrypt decrypts input slice using private key and return byte slice src the encrypted data with public key

func (*RsaCrypt) DecryptFromString

func (rc *RsaCrypt) DecryptFromString(src string, mode Encode) (dst []byte, err error)

DecryptFromString decrypts a plaintext using private key and return string src the encrypted data with public key mode the encode type of encrypted data,such as Base64,HEX or Plain String

func (*RsaCrypt) Encrypt

func (rc *RsaCrypt) Encrypt(src []byte) (dst []byte, err error)

Encrypt encrypts the given message with public key and return byte slice

func (*RsaCrypt) EncryptToString

func (rc *RsaCrypt) EncryptToString(src []byte, mode Encode) (dst string, err error)

EncryptToString encrypts the given message with public key and return string

Jump to

Keyboard shortcuts

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