cryptox

package module
v0.2.2-alpha Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2022 License: MIT Imports: 14 Imported by: 3

README

🔒 Cryptox

Go Doc License License Test

Cryptox 是使用 Go 开发的安全套件,包括了常用的对称加密和非对称加密算法,还有散列算法、编解码常用算法等,主要是为了方便相关场景使用。

Read me in English

💡 功能特性
  • 支持 DES/3DES/AES 等对称加密算法。
  • 支持 RSA/ECC 等非对称加密算法。
  • 支持 ECB/CBC/OFB/CFB/CTR 等分组模式。
  • 支持 PKCS5/PKCS7/ZERO/NO 等填充方式。
  • 支持 MD5/SHA1/SHA256/SHA512/HMAC 等散列算法。
  • 支持 CRC/FNV 等散列算法。
  • 支持 HEX/BASE64 等编解码算法。

历史版本的特性请查看 HISTORY.md。未来版本的新特性和计划请查看 FUTURE.md

⚙ 使用方式
$ go get -u github.com/FishGoddess/cryptox
🚴🏻 性能测试
$ go test -v -bench=. -benchtime=1s _examples/*_test.go

注:数据为 128 字节,ecb/cbc 为 pkcs7 填充,cfb/ofb/ctr 为不填充。

goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz

BenchmarkAESEncryptWithECB-12            2376924               506 ns/op             960 B/op          6 allocs/op
BenchmarkAESEncryptWithCBC-12            1704799               699 ns/op            1072 B/op          9 allocs/op
BenchmarkAESEncryptWithCFB-12            1890339               632 ns/op             816 B/op          8 allocs/op
BenchmarkAESEncryptWithOFB-12            1000000              1029 ns/op            1312 B/op          8 allocs/op
BenchmarkAESEncryptWithCTR-12             979789              1186 ns/op            1312 B/op          8 allocs/op
BenchmarkAESDecryptWithECB-12            2811610               429 ns/op             720 B/op          5 allocs/op
BenchmarkAESDecryptWithCBC-12            2013831               613 ns/op             832 B/op          8 allocs/op
BenchmarkAESDecryptWithCFB-12            1935090               625 ns/op             816 B/op          8 allocs/op
BenchmarkAESDecryptWithOFB-12            1000000              1025 ns/op            1312 B/op          8 allocs/op
BenchmarkAESDecryptWithCTR-12            1000000              1173 ns/op            1312 B/op          8 allocs/op
BenchmarkDESEncryptWithECB-12             351558              3444 ns/op             512 B/op          3 allocs/op
BenchmarkDESEncryptWithCBC-12             334164              3668 ns/op             608 B/op          6 allocs/op
BenchmarkDESEncryptWithCFB-12             352360              3566 ns/op             352 B/op          5 allocs/op
BenchmarkDESEncryptWithOFB-12             148867              8146 ns/op             856 B/op          5 allocs/op
BenchmarkDESEncryptWithCTR-12             138829              8547 ns/op             856 B/op          5 allocs/op
BenchmarkDESDecryptWithECB-12             356511              3427 ns/op             272 B/op          2 allocs/op
BenchmarkDESDecryptWithCBC-12             327507              3664 ns/op             368 B/op          5 allocs/op
BenchmarkDESDecryptWithCFB-12             345454              3552 ns/op             352 B/op          5 allocs/op
BenchmarkDESDecryptWithOFB-12             145267              8183 ns/op             856 B/op          5 allocs/op
BenchmarkDESDecryptWithCTR-12             144640              8451 ns/op             856 B/op          5 allocs/op
BenchmarkTripleDESEncryptWithECB-12       131902              9259 ns/op             768 B/op          3 allocs/op
BenchmarkTripleDESEncryptWithCBC-12       126849              9468 ns/op             864 B/op          6 allocs/op
BenchmarkTripleDESEncryptWithCFB-12       130987              9276 ns/op             608 B/op          5 allocs/op
BenchmarkTripleDESEncryptWithOFB-12        57150             20805 ns/op            1112 B/op          5 allocs/op
BenchmarkTripleDESEncryptWithCTR-12        55443             21344 ns/op            1112 B/op          5 allocs/op
BenchmarkTripleDESDecryptWithECB-12       132991              9236 ns/op             528 B/op          2 allocs/op
BenchmarkTripleDESDecryptWithCBC-12       117877              9483 ns/op             624 B/op          5 allocs/op
BenchmarkTripleDESDecryptWithCFB-12       129415              9064 ns/op             608 B/op          5 allocs/op
BenchmarkTripleDESDecryptWithOFB-12        56397             20979 ns/op            1112 B/op          5 allocs/op
BenchmarkTripleDESDecryptWithCTR-12        56103             21694 ns/op            1112 B/op          5 allocs/op

BenchmarkMD5-12                  7447408               156.3 ns/op           112 B/op          2 allocs/op
BenchmarkSHA1-12                 6629499               184.2 ns/op           136 B/op          2 allocs/op
BenchmarkSHA224-12               4768708               249.6 ns/op           160 B/op          2 allocs/op
BenchmarkSHA256-12               4755806               256.2 ns/op           160 B/op          2 allocs/op
BenchmarkSHA384-12               3717706               329.8 ns/op           272 B/op          2 allocs/op
BenchmarkSHA512-12               3679125               325.5 ns/op           288 B/op          2 allocs/op
BenchmarkHMAC-12                 1215033               988.4 ns/op           512 B/op          6 allocs/op
BenchmarkCRC32IEEE-12           17037747                70.4 ns/op            24 B/op          2 allocs/op
BenchmarkCRC64ISO-12            26904604                44.8 ns/op             8 B/op          1 allocs/op
BenchmarkCRC64ECMA-12           26632101                44.4 ns/op             8 B/op          1 allocs/op
BenchmarkFnv32-12               41738200                28.2 ns/op             8 B/op          1 allocs/op
BenchmarkFnv32a-12              42062208                29.0 ns/op             8 B/op          1 allocs/op
BenchmarkFnv64-12               39065052                29.9 ns/op             8 B/op          1 allocs/op
BenchmarkFnv64a-12              39740802                29.9 ns/op             8 B/op          1 allocs/op
BenchmarkFnv128-12              23474830                50.2 ns/op            16 B/op          1 allocs/op
BenchmarkFnv128a-12             24201123                50.3 ns/op            16 B/op          1 allocs/op

BenchmarkRSAEncryptPKCS1v15-12                     23575             51665 ns/op            5119 B/op         12 allocs/op
BenchmarkRSAEncryptOAEP-12                         23125             54832 ns/op            5475 B/op         18 allocs/op
BenchmarkRSADecryptPKCS1v15-12                       806           1388847 ns/op           26180 B/op        102 allocs/op
BenchmarkRSADecryptPKCS1v15SessionKey-12             856           1367107 ns/op           26179 B/op        102 allocs/op
BenchmarkRSADecryptOAEP-12                           812           1377677 ns/op           26284 B/op        107 allocs/op
🎨 贡献者

如果您觉得 cryptox 缺少您需要的功能,请不要犹豫,马上参与进来,发起一个 issue

💪 使用 cryptox 的项目
项目 作者 描述 链接
Github / 码云

最后,我想感谢 JetBrains 公司的 free JetBrains Open Source license(s),因为 cryptox 是用该计划下的 Idea / GoLand 完成开发的。

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// FileFlag is the flag of file.
	FileFlag = os.O_CREATE | os.O_APPEND | os.O_WRONLY

	// FileMode is the mode of file.
	FileMode os.FileMode = 0644
)
View Source
var (
	// Base64 is the encoding of base64.
	Base64 = base64.StdEncoding
)

Functions

func AES

func AES(key []byte) (cipher.Block, error)

AES return a cipher.Block in aes and an error if failed.

func DES

func DES(key []byte) (cipher.Block, error)

DES return a cipher.Block in des and an error if failed.

func DecryptCBC

func DecryptCBC(block cipher.Block, iv []byte, src []byte, dst []byte) error

DecryptCBC is cbc decrypting mode.

func DecryptCFB

func DecryptCFB(block cipher.Block, iv []byte, src []byte, dst []byte) error

DecryptCFB is cfb decrypting mode.

func DecryptCTR

func DecryptCTR(block cipher.Block, iv []byte, src []byte, dst []byte) error

DecryptCTR is ctr decrypting mode.

func DecryptECB

func DecryptECB(block cipher.Block, _ []byte, src []byte, dst []byte) error

DecryptECB is ecb decrypting mode.

func DecryptOFB

func DecryptOFB(block cipher.Block, iv []byte, src []byte, dst []byte) error

DecryptOFB is ofb decrypting mode.

func EncryptCBC

func EncryptCBC(block cipher.Block, iv []byte, src []byte, dst []byte) error

EncryptCBC is cbc encrypting mode.

func EncryptCFB

func EncryptCFB(block cipher.Block, iv []byte, src []byte, dst []byte) error

EncryptCFB is cfb encrypting mode.

func EncryptCTR

func EncryptCTR(block cipher.Block, iv []byte, src []byte, dst []byte) error

EncryptCTR is ctr encrypting mode.

func EncryptECB

func EncryptECB(block cipher.Block, _ []byte, src []byte, dst []byte) error

EncryptECB is ecb encrypting mode.

func EncryptOFB

func EncryptOFB(block cipher.Block, iv []byte, src []byte, dst []byte) error

EncryptOFB is ofb encrypting mode.

func MD5

func MD5() hash.Hash

MD5 returns md5 hash.

func PaddingNone

func PaddingNone(data []byte, _ int) []byte

PaddingNone won't padding anything to data.

func PaddingPKCS5

func PaddingPKCS5(data []byte, blockSize int) []byte

PaddingPKCS5 paddings data using pkcs5.

func PaddingPKCS7

func PaddingPKCS7(data []byte, blockSize int) []byte

PaddingPKCS7 paddings data using pkcs7.

func PaddingZero

func PaddingZero(data []byte, blockSize int) []byte

PaddingZero paddings zero to data.

func SHA1

func SHA1() hash.Hash

SHA1 returns sha1 hash.

func SHA224

func SHA224() hash.Hash

SHA224 returns sha224 hash.

func SHA256

func SHA256() hash.Hash

SHA256 returns sha256 hash.

func SHA384

func SHA384() hash.Hash

SHA384 returns sha384 hash.

func SHA512

func SHA512() hash.Hash

SHA512 returns sha512 hash.

func TripleDES

func TripleDES(key []byte) (cipher.Block, error)

TripleDES return a cipher.Block in 3des and an error if failed.

func UnPaddingNone

func UnPaddingNone(data []byte, _ int) ([]byte, error)

UnPaddingNone won't unPadding anything from data.

func UnPaddingPKCS5

func UnPaddingPKCS5(data []byte, blockSize int) ([]byte, error)

UnPaddingPKCS5 unPaddings data using pkcs5.

func UnPaddingPKCS7

func UnPaddingPKCS7(data []byte, blockSize int) ([]byte, error)

UnPaddingPKCS7 unPaddings data using pkcs7.

func UnPaddingZero

func UnPaddingZero(data []byte, blockSize int) ([]byte, error)

UnPaddingZero unPaddings zero from data.

Types

type Bytes

type Bytes []byte

Bytes is an alias of []byte.

func ParseBase64

func ParseBase64(str string) (Bytes, error)

ParseBase64 uses base64 to parse string to Bytes.

func ParseHex

func ParseHex(str string) (Bytes, error)

ParseHex uses hex to parse string to Bytes.

func RandomBytes

func RandomBytes(n int) (Bytes, error)

RandomBytes returns a byte slice filled with random byte. It usually used to generate an iv and install iv to crypted data. For example, you use this method to generate a byte slice and pass it to encrypter as iv. After encrypting, you append this iv slice encoded to hex or base64 to crypted slice as they are one part. When you need to decrypt data, parse iv from the "crypted" slice including raw-crypted slice and iv slice first. Then you can pass this iv to decrypter and decrypt data as usual. However, you should know that the crypted data of the same plain data will be different every time because of different ivs.

func (Bytes) Base64

func (bs Bytes) Base64() string

Base64 returns Bytes in base64.

func (Bytes) Bytes

func (bs Bytes) Bytes() []byte

Bytes returns Bytes in []byte.

func (Bytes) Clone

func (bs Bytes) Clone() Bytes

Clone clones bs to new slice.

func (Bytes) Hex

func (bs Bytes) Hex() string

Hex returns Bytes in hex.

func (Bytes) String

func (bs Bytes) String() string

String returns Bytes in string.

func (Bytes) WriteTo

func (bs Bytes) WriteTo(writer io.Writer) (n int64, err error)

WriteTo writes bytes to writer.

func (Bytes) WriteToFile

func (bs Bytes) WriteToFile(path string) (n int64, err error)

WriteToFile writes bytes to file.

type Cipher

type Cipher func(key []byte) (cipher.Block, error)

Cipher is a function returning a cipher.Block and an error if failed.

type DecryptMode

type DecryptMode func(block cipher.Block, iv []byte, src []byte, dst []byte) error

DecryptMode is a function using one mode to encrypt src to dst.

func (DecryptMode) Crypt

func (dm DecryptMode) Crypt(block cipher.Block, iv []byte, src []byte, dst []byte) error

Crypt crypts src to dst.

type Decrypter

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

Decrypter decrypts data from bytes, hex, and base64.

func NewDecrypter

func NewDecrypter(cipher Cipher, key []byte, mode DecryptMode, iv []byte, unPadding UnPadding) Decrypter

NewDecrypter returns a new decrypter.

func (Decrypter) Decrypt

func (d Decrypter) Decrypt(crypted Bytes) (Bytes, error)

Decrypt decrypts data to bytes.

func (Decrypter) DecryptBase64

func (d Decrypter) DecryptBase64(crypted string) (Bytes, error)

DecryptBase64 decrypts base64 in hex to bytes.

func (Decrypter) DecryptHex

func (d Decrypter) DecryptHex(crypted string) (Bytes, error)

DecryptHex decrypts data in hex to bytes.

type EncryptMode

type EncryptMode func(block cipher.Block, iv []byte, src []byte, dst []byte) error

EncryptMode is a function using one mode to encrypt src to dst.

func (EncryptMode) Crypt

func (em EncryptMode) Crypt(block cipher.Block, iv []byte, src []byte, dst []byte) error

Crypt crypts src to dst.

type Encrypter

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

Encrypter encrypts data to bytes, hex, and base64.

func NewEncrypter

func NewEncrypter(cipher Cipher, key []byte, mode EncryptMode, iv []byte, padding Padding) Encrypter

NewEncrypter returns a new encrypter.

func (Encrypter) Encrypt

func (e Encrypter) Encrypt(plain Bytes) (Bytes, error)

Encrypt encrypts data to bytes.

func (Encrypter) EncryptBase64

func (e Encrypter) EncryptBase64(plain Bytes) (string, error)

EncryptBase64 encrypts data to string in base64.

func (Encrypter) EncryptHex

func (e Encrypter) EncryptHex(plain Bytes) (string, error)

EncryptHex encrypts data to string in hex.

type Hash

type Hash func() hash.Hash

Hash is a function returning hash.Hash.

type Hasher

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

Hasher hashes data with inside hash.

func NewHasher

func NewHasher(h Hash) Hasher

NewHasher returns a new hasher.

func (Hasher) Hash

func (h Hasher) Hash(data Bytes) (Bytes, error)

Hash hashes data to bytes. Although it has an error in returning values, it should be nil forever.

type Padding

type Padding func(data []byte, blockSize int) []byte

Padding paddings data with blockSize.

type UnPadding

type UnPadding func(data []byte, blockSize int) ([]byte, error)

UnPadding unPaddings data with blockSize.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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