Skip to content

Commit 565a584

Browse files
authored
Merge pull request #7 from FishGoddess/develop
增加 ED25519 算法的支持
2 parents 2d1a787 + 6bcb575 commit 565a584

26 files changed

+1247
-426
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ target/
1616
bin/
1717
*.test
1818
*.out
19-
*.log
19+
*.log
20+
*.key
21+
*.pub

FUTURE.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
### v0.5.x
1111

12-
* [ ] 支持 ECDSA 算法
13-
* [ ] 支持 EdDSA 算法
12+
* [ ] 支持 X25519 算法
13+
* [x] 支持 ED25519 算法
1414
* [x] 重构代码,优化使用方式
1515
* [ ] 提升覆盖率到 90% 以上
1616

HISTORY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 📜 历史版本的特性介绍 (Features in old versions)
22

3+
### v0.5.1-alpha
4+
5+
> 此版本发布于 2025-12-07
6+
7+
* 增加 ED25519 算法的支持
8+
39
### v0.5.0-alpha
410

511
> 此版本发布于 2025-12-05

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ bench:
1616
go test -v ./_examples/aes_test.go -bench=. -benchtime=1s
1717
go test -v ./_examples/rsa_test.go -bench=. -benchtime=1s
1818
go test -v ./_examples/rsa_key_test.go -bench=. -benchtime=1s
19+
go test -v ./_examples/ed25519_test.go -bench=. -benchtime=1s
20+
go test -v ./_examples/ed25519_key_test.go -bench=. -benchtime=1s

README.en.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* HMAC mixed hash supports.
1818
* DES/3DES/AES encrypt and decrypt supports.
1919
* RSA encrypt and decrypt supports.
20+
* ED25519 sign supports.
2021
* ECB/CBC/OFB/CFB/CTR/GCM mode supports.
2122
* ZERO/PKCS5/PKCS7 padding supports.
2223

@@ -35,6 +36,8 @@ $ go get -u github.com/FishGoddess/cryptox
3536
* [aes](_examples/aes.go)
3637
* [rsa](_examples/rsa.go)
3738
* [rsa_key](_examples/rsa_key.go)
39+
* [ed25519](_examples/ed25519.go)
40+
* [ed25519_key](_examples/ed25519_key.go)
3841

3942
### 🚴🏻 Benchmarks
4043

@@ -120,6 +123,11 @@ BenchmarkRSA_VerifyPSS-2 20220 56627 ns/op
120123
BenchmarkRSA_GenerateKeys1024-2 60 21398224 ns/op 283350 B/op 2851 allocs/op
121124
BenchmarkRSA_GenerateKeys2048-2 84 117753488 ns/op 600303 B/op 5459 allocs/op
122125
BenchmarkRSA_GenerateKeys4096-2 1 1432974432 ns/op 2709912 B/op 14359 allocs/op
126+
127+
BenchmarkED25519_Sign-2 32125 36469 ns/op 112 B/op 2 allocs/op
128+
BenchmarkED25519_Verify-2 13927 90989 ns/op 48 B/op 1 allocs/op
129+
130+
BenchmarkED25519_GenerateKeys-2 38296 30692 ns/op 208 B/op 4 allocs/op
123131
```
124132

125133
### 🎨 Contributing

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* 支持 HMAC 混合基础的散列算法。
1818
* 支持 DES/3DES/AES 等对称加密算法。
1919
* 支持 RSA 等非对称加密算法。
20+
* 支持 ED25519 等签名算法。
2021
* 支持 ECB/CBC/OFB/CFB/CTR/GCM 等分组模式。
2122
* 支持 ZERO/PKCS5/PKCS7 等字节填充方式。
2223

@@ -35,6 +36,8 @@ $ go get -u github.com/FishGoddess/cryptox
3536
* [aes](_examples/aes.go)
3637
* [rsa](_examples/rsa.go)
3738
* [rsa_key](_examples/rsa_key.go)
39+
* [ed25519](_examples/ed25519.go)
40+
* [ed25519_key](_examples/ed25519_key.go)
3841

3942
### 🚴🏻 性能测试
4043

@@ -120,6 +123,11 @@ BenchmarkRSA_VerifyPSS-2 20220 56627 ns/op
120123
BenchmarkRSA_GenerateKeys1024-2 60 21398224 ns/op 283350 B/op 2851 allocs/op
121124
BenchmarkRSA_GenerateKeys2048-2 84 117753488 ns/op 600303 B/op 5459 allocs/op
122125
BenchmarkRSA_GenerateKeys4096-2 1 1432974432 ns/op 2709912 B/op 14359 allocs/op
126+
127+
BenchmarkED25519_Sign-2 32125 36469 ns/op 112 B/op 2 allocs/op
128+
BenchmarkED25519_Verify-2 13927 90989 ns/op 48 B/op 1 allocs/op
129+
130+
BenchmarkED25519_GenerateKeys-2 38296 30692 ns/op 208 B/op 4 allocs/op
123131
```
124132

125133
### 🎨 贡献者

_examples/ed25519.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/FishGoddess/cryptox/ed25519"
11+
)
12+
13+
func main() {
14+
// Load the private key and the public key from file.
15+
// Check ed25519.Option for more information about file encoding.
16+
privateKey, err := ed25519.LoadPrivateKey("ed25519.key")
17+
if err != nil {
18+
panic(err)
19+
}
20+
21+
publicKey, err := ed25519.LoadPublicKey("ed25519.pub")
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
data := []byte("戴上头箍,爱不了你;不戴头箍,救不了你。")
27+
fmt.Printf("data: %s\n", data)
28+
29+
// Use the private key to sign data.
30+
sign := privateKey.Sign(data, ed25519.WithHex())
31+
fmt.Printf("sign: %s\n", sign)
32+
33+
// Use the public key to verify the sign.
34+
err = publicKey.Verify(data, sign, ed25519.WithHex())
35+
if err != nil {
36+
panic(err)
37+
}
38+
39+
fmt.Printf("verify: %s\n", data)
40+
}

_examples/ed25519_key.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"github.com/FishGoddess/cryptox/ed25519"
9+
)
10+
11+
func main() {
12+
// Generate a key without seed.
13+
privateKey, publicKey, err := ed25519.GenerateKeys()
14+
if err != nil {
15+
panic(err)
16+
}
17+
18+
// Generate a key with seed.
19+
seed := []byte("12345678876543211234567887654321")
20+
21+
privateKey, publicKey, err = ed25519.GenerateKeys(ed25519.WithKeySeed(seed))
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
// Store the private key and the public key to file.
27+
err = ed25519.StorePrivateKey("ed25519.key", privateKey)
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
err = ed25519.StorePublicKey("ed25519.pub", publicKey)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
// Load the private key and the public key from file.
38+
privateKey, err = ed25519.LoadPrivateKey("ed25519.key")
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
publicKey, err = ed25519.LoadPublicKey("ed25519.pub")
44+
if err != nil {
45+
panic(err)
46+
}
47+
}

_examples/ed25519_key_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"testing"
9+
10+
"github.com/FishGoddess/cryptox/ed25519"
11+
)
12+
13+
// go test -v -bench=^BenchmarkED25519_GenerateKeys$ -benchtime=1s ed25519_key_test.go
14+
func BenchmarkED25519_GenerateKeys(b *testing.B) {
15+
b.ReportAllocs()
16+
b.ResetTimer()
17+
18+
for i := 0; i < b.N; i++ {
19+
_, _, err := ed25519.GenerateKeys()
20+
if err != nil {
21+
b.Fatal(err)
22+
}
23+
}
24+
}

_examples/ed25519_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2025 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package main
6+
7+
import (
8+
"testing"
9+
10+
"github.com/FishGoddess/cryptox/ed25519"
11+
)
12+
13+
var (
14+
ed25519BenchData = []byte("你好,世界")
15+
)
16+
17+
// go test -v -bench=^BenchmarkED25519_Sign$ -benchtime=1s ed25519_test.go
18+
func BenchmarkED25519_Sign(b *testing.B) {
19+
privateKey, err := ed25519.LoadPrivateKey("ed25519.key")
20+
if err != nil {
21+
b.Fatal(err)
22+
}
23+
24+
b.ReportAllocs()
25+
b.ResetTimer()
26+
27+
for i := 0; i < b.N; i++ {
28+
privateKey.Sign(ed25519BenchData)
29+
}
30+
}
31+
32+
// go test -v -bench=^BenchmarkED25519_Verify$ -benchtime=1s ed25519_test.go
33+
func BenchmarkED25519_Verify(b *testing.B) {
34+
privateKey, err := ed25519.LoadPrivateKey("ed25519.key")
35+
if err != nil {
36+
b.Fatal(err)
37+
}
38+
39+
publicKey, err := ed25519.LoadPublicKey("ed25519.pub")
40+
if err != nil {
41+
b.Fatal(err)
42+
}
43+
44+
sign := privateKey.Sign(ed25519BenchData)
45+
46+
b.ReportAllocs()
47+
b.ResetTimer()
48+
49+
for i := 0; i < b.N; i++ {
50+
err = publicKey.Verify(ed25519BenchData, sign)
51+
if err != nil {
52+
b.Fatal(err)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)