crypto

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2022 License: MIT Imports: 6 Imported by: 0

README

go-crypto

根据基础加解密算法包做出的一定性能优化

优化方向

  • 减少了字符串与字节数组转化的内存损耗
  • 对加解密过程中申请的字节空间使用sync.Pool进行管理

Benchmark 基准测试

基准测试命令:go test -bench=. -benchtime=10s -benchmem -cpu=4 -timeout=100m

--- dataSize: 10
goos: darwin
goarch: amd64
pkg: github.com/frank-yf/go-crypto
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkAesECBCrypto-4                          7462260              1608 ns/op            1112 B/op         40 allocs/op
BenchmarkAesECBCrypto_WithPool-4                 4858204              2447 ns/op            1496 B/op         53 allocs/op
BenchmarkAesECBCrypto_WithBase64-4               3404462              3520 ns/op            1912 B/op         60 allocs/op
BenchmarkAesECBCrypto_WithBase64_WithPool-4      2129854              5618 ns/op            2856 B/op         99 allocs/op
BenchmarkAesECBCrypto_WithHex-4                  3260694              3668 ns/op            2264 B/op         60 allocs/op
BenchmarkAesECBCrypto_WithHex_WithPool-4         1979689              6013 ns/op            3208 B/op         99 allocs/op
BenchmarkNone-4                                  1690008              7094 ns/op           10072 B/op        120 allocs/op
PASS
ok      github.com/frank-yf/go-crypto   114.702s

--- dataSize: 10
goos: darwin
goarch: amd64
pkg: github.com/frank-yf/go-crypto
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkAesECBCrypto-4                           211598             58158 ns/op           84792 B/op        400 allocs/op
BenchmarkAesECBCrypto_WithPool-4                  172875             69764 ns/op           88818 B/op        525 allocs/op
BenchmarkAesECBCrypto_WithBase64-4                 91771            130877 ns/op          135544 B/op        600 allocs/op
BenchmarkAesECBCrypto_WithBase64_WithPool-4        77180            156315 ns/op          127207 B/op        999 allocs/op
BenchmarkAesECBCrypto_WithHex-4                    73759            165117 ns/op          169784 B/op        600 allocs/op
BenchmarkAesECBCrypto_WithHex_WithPool-4           62107            192252 ns/op          161457 B/op        999 allocs/op
BenchmarkNone-4                                   111423            110126 ns/op          174392 B/op       1200 allocs/op
PASS
ok      github.com/frank-yf/go-crypto   94.274s

--- dataSize: 10
goos: darwin
goarch: amd64
pkg: github.com/frank-yf/go-crypto
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkAesECBCrypto-4                             2990           4037089 ns/op         7070559 B/op       4000 allocs/op
BenchmarkAesECBCrypto_WithPool-4                    2905           4166075 ns/op         7113762 B/op       5260 allocs/op
BenchmarkAesECBCrypto_WithBase64-4                  1269           9184116 ns/op        12067350 B/op       6001 allocs/op
BenchmarkAesECBCrypto_WithBase64_WithPool-4         1285           9496406 ns/op        10070904 B/op      10016 allocs/op
BenchmarkAesECBCrypto_WithHex-4                      969          12509609 ns/op        15620470 B/op       6002 allocs/op
BenchmarkAesECBCrypto_WithHex_WithPool-4             952          12481948 ns/op        13632622 B/op      10024 allocs/op
BenchmarkNone-4                                     2650           4529414 ns/op         7966664 B/op      12000 allocs/op
PASS
ok      github.com/frank-yf/go-crypto   90.392s

  • 线性增加数据长度以及元素数量后,基准结果也呈线性增长;
  • 当数据长度超过一定范围后,hex/base64等编码函数更耗资源;

欢迎提出意见或提交 PR。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MD5

func MD5(data []byte) []byte

func MD5Hex

func MD5Hex(data string) []byte

func MD5HexToString

func MD5HexToString(data string) string

Types

type AesECB

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

func NewAesECB

func NewAesECB(key string, opts ...AesECBOptions) (c *AesECB, err error)

func (*AesECB) Decrypt

func (c *AesECB) Decrypt(encrypted []byte) []byte

func (*AesECB) DecryptFrom

func (c *AesECB) DecryptFrom(encrypted string, decoder StringDecoder) (decrypted string, err error)

func (*AesECB) DecryptFromBase64

func (c *AesECB) DecryptFromBase64(encrypted string) (decrypted string, err error)

func (*AesECB) DecryptFromHex

func (c *AesECB) DecryptFromHex(encrypted string) (decrypted string, err error)

func (*AesECB) Encrypt

func (c *AesECB) Encrypt(data []byte) (encrypted []byte)

func (*AesECB) EncryptTo

func (c *AesECB) EncryptTo(data string, encoder StringEncoder) string

func (*AesECB) EncryptToBase64

func (c *AesECB) EncryptToBase64(data string) string

func (*AesECB) EncryptToHex

func (c *AesECB) EncryptToHex(data string) string

type AesECBOptions

type AesECBOptions func(*AesECB)

func WithPool

func WithPool(preSize int) AesECBOptions

type Crypto

type Crypto interface {
	Encrypt([]byte) []byte

	EncryptTo(string, StringEncoder) string

	Decrypt([]byte) []byte

	DecryptFrom(string, StringDecoder) (string, error)
}

type Hash

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

func NewHash

func NewHash(f func() hash.Hash) *Hash

func (*Hash) Sum

func (h *Hash) Sum(data []byte) []byte

func (*Hash) SumString

func (h *Hash) SumString(data string, encoder StringEncoder) string

type StringDecoder

type StringDecoder func(string) ([]byte, error)

type StringEncoder

type StringEncoder func([]byte) string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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