Skip to content

Commit

Permalink
继续优化代码,完善使用案例
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Jun 23, 2023
1 parent 913c35b commit 5a00a59
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 85 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 📜 历史版本的特性介绍 (Features in old versions)

### v0.3.1-alpha

> 此版本发布于 2023-06-23
* 继续优化代码,完善使用案例

### v0.3.0-alpha

> 此版本发布于 2023-06-22
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bench:
go test -v ./_examples/des_test.go -bench=. -benchtime=1s
go test -v ./_examples/triple_des_test.go -bench=. -benchtime=1s
go test -v ./_examples/aes_test.go -bench=. -benchtime=1s
go test -v ./_examples/rsa_test.go -bench=. -benchtime=1s
go test -v ./_examples/rsa_key_test.go -bench=. -benchtime=1s
go test -v ./_examples/rsa_test.go -bench=. -benchtime=1s

all: fmt test bench
62 changes: 31 additions & 31 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

### 💡 Features

* HEX/BASE64 Supports.
* MD5/SHA1/SHA256/SHA512/HMAC Supports.
* CRC/FNV Supports.
* DES/3DES/AES Supports.
* RSA/ECC Supports.
* ECB/CBC/OFB/CFB/CTR Supports.
* PKCS5/PKCS7/ZERO/NO Supports.
* MD5/SHA1/SHA256/SHA512/HMAC Supports.
* CRC/FNV Supports.
* HEX/BASE64 Supports.

_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about more information._

Expand All @@ -27,10 +27,10 @@ _Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about mor
$ go get -u github.com/FishGoddess/cryptox
```

* [hash](_examples/hash.go)
* [des](_examples/des.go)
* [triple_des](_examples/triple_des.go)
* [aes](_examples/aes.go)
* [hash](_examples/hash.go)
* [rsa_key](_examples/rsa_key.go)
* [rsa](_examples/rsa.go)

Expand All @@ -47,16 +47,23 @@ 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
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
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
Expand All @@ -77,23 +84,16 @@ BenchmarkTripleDESDecryptWithCBC-12 117877 9483 ns/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
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
BenchmarkRSAEncryptPKCS1v15-12 23575 51665 ns/op 5119 B/op 12 allocs/op
BenchmarkRSAEncryptOAEP-12 23125 54832 ns/op 5475 B/op 18 allocs/op
Expand Down
71 changes: 36 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

### 💡 功能特性

* 支持 HEX/BASE64 等编解码算法。
* 支持 MD5/SHA1/SHA256/SHA512/HMAC 等散列算法。
* 支持 CRC/FNV 等散列算法。
* 支持 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](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_

Expand All @@ -27,10 +27,10 @@ _历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新
$ go get -u github.com/FishGoddess/cryptox
```

* [hash](_examples/hash.go)
* [des](_examples/des.go)
* [triple_des](_examples/triple_des.go)
* [aes](_examples/aes.go)
* [hash](_examples/hash.go)
* [rsa_key](_examples/rsa_key.go)
* [rsa](_examples/rsa.go)

Expand All @@ -47,16 +47,23 @@ 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
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
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
Expand All @@ -77,23 +84,16 @@ BenchmarkTripleDESDecryptWithCBC-12 117877 9483 ns/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
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
BenchmarkRSAEncryptPKCS1v15-12 23575 51665 ns/op 5119 B/op 12 allocs/op
BenchmarkRSAEncryptOAEP-12 23125 54832 ns/op 5475 B/op 18 allocs/op
Expand All @@ -108,10 +108,11 @@ BenchmarkRSADecryptOAEP-12 812 1377677 ns/op

### 💪 使用 cryptox 的项目

| 项目 | 作者 | 描述 | 链接 |
|-----|-----|-----|---------------------|
| | | | [Github]() / [码云]() |
| 项目 | 作者 | 描述 | 链接 |
|----|----|----|---------------------|
| | | | [Github]() / [码云]() |

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

<a href="https://www.jetbrains.com/?from=cryptox" target="_blank"><img src="./_icons/jetbrains.png" width="250"/></a>
27 changes: 26 additions & 1 deletion _examples/rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package main
import (
"fmt"

"github.com/FishGoddess/cryptox"
"github.com/FishGoddess/cryptox/hash"
"github.com/FishGoddess/cryptox/rsa"
)

Expand All @@ -19,20 +21,43 @@ var (
)

func main() {
msg := []byte("戴上头箍,爱不了你;不戴头箍,救不了你。")
msg := cryptox.FromString("戴上头箍,爱不了你;不戴头箍,救不了你。")
fmt.Printf("Msg: %s\n", msg)

// Use public key to encrypt msg.
encrypted, err := publicKey.EncryptPKCS1v15(msg)
if err != nil {
panic(err)
}

fmt.Printf("Encrypted: %s\n", encrypted.Base64())

// Use private key to decrypt msg.
decrypted, err := privateKey.DecryptPKCS1v15(encrypted)
if err != nil {
panic(err)
}

fmt.Printf("Decrypted: %s\n", decrypted)

msg, err = hash.SHA256(msg)
if err != nil {
panic(err)
}

// Use private key to sign msg.
signed, err := privateKey.SignPKCS1v15(msg)
if err != nil {
panic(err)
}

fmt.Printf("Signed: %s\n", signed)

// Use public key to verify msg.
err = publicKey.VerifyPKCS1v15(msg, signed)
if err != nil {
panic(err)
}

fmt.Println("Verified.")
}
10 changes: 5 additions & 5 deletions rsa/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func GenerateKeys(bits int, opts ...KeyOption) (PrivateKey, PublicKey, error) {

// GeneratePrivateKey generates a private key of bits.
func GeneratePrivateKey(bits int, opts ...KeyOption) (PrivateKey, error) {
cfg := fromKeyOptions(opts)

privateKey, err := rsa.GenerateMultiPrimeKey(rand.Reader, Primes, bits)
if err != nil {
return PrivateKey{}, err
}

cfg := fromKeyOptions(opts...)

privateKeyBytes, err := cfg.privateKeyEncoder.Encode(privateKey)
if err != nil {
return PrivateKey{}, err
Expand All @@ -53,8 +53,8 @@ func GeneratePrivateKey(bits int, opts ...KeyOption) (PrivateKey, error) {

// GeneratePublicKey generates a public key from private key.
func GeneratePublicKey(privateKey PrivateKey, opts ...KeyOption) (PublicKey, error) {
cfg := fromKeyOptions(opts)
publicKey := &(privateKey.Key().PublicKey)
cfg := fromKeyOptions(opts...)

publicKeyBytes, err := cfg.publicKeyEncoder.Encode(publicKey)
if err != nil {
Expand All @@ -66,7 +66,7 @@ func GeneratePublicKey(privateKey PrivateKey, opts ...KeyOption) (PublicKey, err

// ParsePrivateKey parses private key from pem bytes.
func ParsePrivateKey(keyBytes cryptox.Bytes, opts ...KeyOption) (PrivateKey, error) {
cfg := fromKeyOptions(opts...)
cfg := fromKeyOptions(opts)

privateKey, err := cfg.privateKeyDecoder.Decode(keyBytes)
if err != nil {
Expand All @@ -78,7 +78,7 @@ func ParsePrivateKey(keyBytes cryptox.Bytes, opts ...KeyOption) (PrivateKey, err

// ParsePublicKey parses public key from pem bytes.
func ParsePublicKey(keyBytes cryptox.Bytes, opts ...KeyOption) (PublicKey, error) {
cfg := fromKeyOptions(opts...)
cfg := fromKeyOptions(opts)

publicKey, err := cfg.publicKeyDecoder.Decode(keyBytes)
if err != nil {
Expand Down
Loading

0 comments on commit 5a00a59

Please sign in to comment.