cryptox

package module
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2024 License: MIT Imports: 8 Imported by: 3

README

🔒 Cryptox

Go Doc License Coverage Test

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

Read me in English

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

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

⚙ 使用方式
$ go get -u github.com/FishGoddess/cryptox
🚴🏻 性能测试
$ make bench

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

goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor

BenchmarkGenerateBytes-2        11454386               102.6 ns/op            16 B/op          1 allocs/op
BenchmarkGenerateString-2       11392647               106.1 ns/op            16 B/op          1 allocs/op

BenchmarkMD5-2           8933902               133.6 ns/op             0 B/op          0 allocs/op
BenchmarkSHA1-2          7997222               148.7 ns/op             0 B/op          0 allocs/op
BenchmarkSHA224-2       12905275                93.82 ns/op            0 B/op          0 allocs/op
BenchmarkSHA256-2       13305572                90.11 ns/op            0 B/op          0 allocs/op
BenchmarkSHA384-2        4501677               266.8 ns/op             0 B/op          0 allocs/op
BenchmarkSHA512-2        4457496               272.0 ns/op             0 B/op          0 allocs/op
BenchmarkCRC32IEEE-2    37977817                26.99 ns/op            0 B/op          0 allocs/op
BenchmarkCRC64ISO-2     49464211                23.77 ns/op            0 B/op          0 allocs/op
BenchmarkCRC64ECMA-2    47987559                23.85 ns/op            0 B/op          0 allocs/op
BenchmarkFnv32-2        88136509                13.91 ns/op            0 B/op          0 allocs/op
BenchmarkFnv32a-2       92693382                13.53 ns/op            0 B/op          0 allocs/op
BenchmarkFnv64-2        78227923                15.31 ns/op            0 B/op          0 allocs/op
BenchmarkFnv64a-2       79213070                15.35 ns/op            0 B/op          0 allocs/op
BenchmarkFnv128-2       15003325                77.31 ns/op           16 B/op          1 allocs/op
BenchmarkFnv128a-2      15473437                76.73 ns/op           16 B/op          1 allocs/op

BenchmarkHMACUsingMD5-2          1418788               837.4 ns/op           432 B/op          6 allocs/op
BenchmarkHMACUsingSHA1-2         1304995               922.9 ns/op           472 B/op          6 allocs/op
BenchmarkHMACUsingSHA224-2       1879555               651.9 ns/op           512 B/op          6 allocs/op
BenchmarkHMACUsingSHA256-2       1880856               656.8 ns/op           512 B/op          6 allocs/op
BenchmarkHMACUsingSHA384-2        702820              1679 ns/op             848 B/op          6 allocs/op
BenchmarkHMACUsingSHA512-2        691454              1629 ns/op             864 B/op          6 allocs/op

BenchmarkDESEncryptECB-2          321910              3601 ns/op             656 B/op          4 allocs/op
BenchmarkDESEncryptCBC-2          307196              3911 ns/op             752 B/op          7 allocs/op
BenchmarkDESEncryptCFB-2          319731              3689 ns/op             480 B/op          6 allocs/op
BenchmarkDESEncryptOFB-2          126572              9045 ns/op             984 B/op          6 allocs/op
BenchmarkDESEncryptCTR-2          125204              9471 ns/op             984 B/op          6 allocs/op
BenchmarkDESDecryptECB-2          340486              3461 ns/op             272 B/op          2 allocs/op
BenchmarkDESDecryptCBC-2          321182              3730 ns/op             368 B/op          5 allocs/op
BenchmarkDESDecryptCFB-2          329288              3607 ns/op             352 B/op          5 allocs/op
BenchmarkDESDecryptOFB-2          131550              9054 ns/op             856 B/op          5 allocs/op
BenchmarkDESDecryptCTR-2          125089              9539 ns/op             856 B/op          5 allocs/op

BenchmarkDESEncryptECBTriple-2            124927              9474 ns/op             912 B/op          4 allocs/op
BenchmarkDESEncryptCBCTriple-2            121834              9701 ns/op            1008 B/op          7 allocs/op
BenchmarkDESEncryptCFBTriple-2            127020              9334 ns/op             736 B/op          6 allocs/op
BenchmarkDESEncryptOFBTriple-2             50922             23598 ns/op            1240 B/op          6 allocs/op
BenchmarkDESEncryptCTRTriple-2             49735             23873 ns/op            1240 B/op          6 allocs/op
BenchmarkDESDecryptECBTriple-2            128005              9328 ns/op             528 B/op          2 allocs/op
BenchmarkDESDecryptCBCTriple-2            122860              9558 ns/op             624 B/op          5 allocs/op
BenchmarkDESDecryptCFBTriple-2            126201              9334 ns/op             608 B/op          5 allocs/op
BenchmarkDESDecryptOFBTriple-2             50688             23291 ns/op            1112 B/op          5 allocs/op
BenchmarkDESDecryptCTRTriple-2             50391             23913 ns/op            1112 B/op          5 allocs/op

BenchmarkAESEncryptECB-2         1960758               578.4 ns/op          1104 B/op          7 allocs/op
BenchmarkAESEncryptCBC-2         1413351               842.1 ns/op          1216 B/op         10 allocs/op
BenchmarkAESEncryptCFB-2         1657416               726.3 ns/op           944 B/op          9 allocs/op
BenchmarkAESEncryptOFB-2          924843              1206 ns/op            1440 B/op          9 allocs/op
BenchmarkAESEncryptCTR-2          808509              1381 ns/op            1440 B/op          9 allocs/op
BenchmarkAESEncryptGCM-2         1784593               661.2 ns/op          1168 B/op          7 allocs/op
BenchmarkAESDecryptECB-2         2590693               475.5 ns/op           720 B/op          5 allocs/op
BenchmarkAESDecryptCBC-2         1715998               711.6 ns/op           832 B/op          8 allocs/op
BenchmarkAESDecryptCFB-2         1809044               656.0 ns/op           816 B/op          8 allocs/op
BenchmarkAESDecryptOFB-2          956439              1152 ns/op            1312 B/op          8 allocs/op
BenchmarkAESDecryptCTR-2          841315              1348 ns/op            1312 B/op          8 allocs/op
BenchmarkAESDecryptGCM-2         1944147               633.5 ns/op          1040 B/op          6 allocs/op

BenchmarkRSAEncryptPKCS1v15-2                       1252            948252 ns/op           95488 B/op        155 allocs/op
BenchmarkRSAEncryptOAEP-2                           1315            909433 ns/op           95592 B/op        160 allocs/op
BenchmarkRSADecryptPKCS1v15-2                        166           7219740 ns/op            7600 B/op         16 allocs/op
BenchmarkRSADecryptPKCS1v15SessionKey-2              165           7154349 ns/op            7600 B/op         16 allocs/op
BenchmarkRSADecryptOAEP-2                            166           7150228 ns/op            7704 B/op         21 allocs/op
BenchmarkRSASignPSS-2                                154           7670215 ns/op           67168 B/op        100 allocs/op
BenchmarkRSASignPKCS1v15-2                           156           7637694 ns/op           66992 B/op         95 allocs/op
BenchmarkRSAVerifyPSS-2                             1308            901882 ns/op           95184 B/op        159 allocs/op
BenchmarkRSAVerifyPKCS1v15-2                        1339            895115 ns/op           94976 B/op        154 allocs/op

BenchmarkRSAGenerateKey1024PKCS1PKIX-2                93          20882099 ns/op         1048351 B/op       4090 allocs/op
BenchmarkRSAGenerateKey2048PKCS1PKIX-2                12         179918684 ns/op         3038840 B/op       8000 allocs/op
BenchmarkRSAGenerateKey4096PKCS1PKIX-2                 1        3257891003 ns/op        15509888 B/op      25112 allocs/op
BenchmarkRSAGenerateKey1024PKCS8PKIX-2                51          22485111 ns/op         1148367 B/op       4450 allocs/op
BenchmarkRSAGenerateKey2048PKCS8PKIX-2                10         149631216 ns/op         2446195 B/op       6550 allocs/op
BenchmarkRSAGenerateKey4096PKCS8PKIX-2                 1        1617318757 ns/op         7521280 B/op      12421 allocs/op
BenchmarkRSAGenerateKey1024PKCS1PKCS1-2               57          22467728 ns/op         1144077 B/op       4387 allocs/op
BenchmarkRSAGenerateKey2048PKCS1PKCS1-2               25         176744406 ns/op         2982597 B/op       7835 allocs/op
BenchmarkRSAGenerateKey4096PKCS1PKCS1-2                1        1577239084 ns/op         7272704 B/op      11896 allocs/op
BenchmarkRSAGenerateKey1024PKCS8PKCS1-2               61          20029226 ns/op         1000116 B/op       3942 allocs/op
BenchmarkRSAGenerateKey2048PKCS8PKCS1-2               21         178710269 ns/op         2998867 B/op       7896 allocs/op
BenchmarkRSAGenerateKey4096PKCS8PKCS1-2                1        2782582400 ns/op        13284080 B/op      21606 allocs/op
🎨 贡献者

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateString added in v0.4.3

func GenerateString(n int) string

GenerateString generates a string including n random bytes and returns it.

Types

type Bytes

type Bytes []byte

func AppendBytes added in v0.4.3

func AppendBytes(bs Bytes, n int) Bytes

AppendBytes appends n random bytes to bs.

func GenerateBytes added in v0.3.2

func GenerateBytes(n int) Bytes

GenerateBytes generates n random bytes and returns them. It usually used to generate an iv and store it to an safe database. It means each encrypted data has an different iv so your encrypted data is safer than using a fixed iv. However, you should know that the encrypted data of same data will be different if the iv is different.

func ParseBase64

func ParseBase64(base64String string) (Bytes, error)

ParseBase64 a base64 string to bytes.

func ParseHex

func ParseHex(hexString string) (Bytes, error)

ParseHex a hex string to bytes.

func (Bytes) Base64

func (bs Bytes) Base64() string

Base64 returns Bytes as base64.

func (Bytes) Clone

func (bs Bytes) Clone() Bytes

Clone clones bs and returns a new slice.

func (Bytes) Hex

func (bs Bytes) Hex() string

Hex returns Bytes as hex.

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 Padding

type Padding interface {
	Padding(bs Bytes, blockSize int) Bytes
	UndoPadding(bs Bytes, blockSize int) (Bytes, error)
}

Padding paddings and undo paddings to a byte slice.

var (
	PaddingNone  Padding = paddingNone{}
	PaddingZero  Padding = paddingZero{}
	PaddingPKCS5 Padding = paddingPKCS7{} // PKCS5 is actually the same as pkcs7.
	PaddingPKCS7 Padding = paddingPKCS7{}
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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