Skip to content

Commit e3ca4cf

Browse files
committed
test
1 parent a9a5fd5 commit e3ca4cf

File tree

5 files changed

+119
-34
lines changed

5 files changed

+119
-34
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

_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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
}
1717

1818
// Generate a key with seed.
19-
seed := byte("12345678876543211234567887654321")
19+
seed := []byte("12345678876543211234567887654321")
2020

2121
privateKey, publicKey, err = ed25519.GenerateKeys(ed25519.WithKeySeed(seed))
2222
if err != nil {

ed25519/ed25519_test.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ed25519
66

77
import (
88
"bytes"
9+
"slices"
910
"testing"
1011
)
1112

@@ -27,7 +28,9 @@ func (testRandomReader) Read(p []byte) (n int, err error) {
2728
}
2829

2930
func newTestPrivateKey() PrivateKey {
30-
reader := bytes.NewReader([]byte(``))
31+
reader := bytes.NewReader([]byte(`-----BEGIN PRIVATE KEY-----
32+
MC4CAQAwBQYDK2VwBCIEIDEyMzQ1Njc4ODc2NTQzMjExMjM0NTY3ODg3NjU0MzIx
33+
-----END PRIVATE KEY-----`))
3134

3235
privateKey, err := ReadPrivateKey(reader)
3336
if err != nil {
@@ -38,7 +41,9 @@ func newTestPrivateKey() PrivateKey {
3841
}
3942

4043
func newTestPublicKey() PublicKey {
41-
reader := bytes.NewReader([]byte(``))
44+
reader := bytes.NewReader([]byte(`-----BEGIN PUBLIC KEY-----
45+
MCowBQYDK2VwAyEAjmI9pwiSpBdVpKQjZ+pDGbzYSx/ceC1DW45+DUzgyyM=
46+
-----END PUBLIC KEY-----`))
4247

4348
publicKey, err := ReadPublicKey(reader)
4449
if err != nil {
@@ -56,21 +61,21 @@ func TestSignVerify(t *testing.T) {
5661
testCases := []signTestCase{
5762
{
5863
Data: []byte(""),
59-
SignData: []byte(""),
60-
SignDataHex: []byte(""),
61-
SignDataBase64: []byte(""),
64+
SignData: []byte{151, 147, 60, 76, 20, 163, 37, 105, 38, 95, 170, 9, 135, 36, 234, 8, 193, 238, 213, 20, 68, 50, 39, 245, 4, 57, 88, 11, 36, 213, 251, 151, 99, 17, 253, 149, 187, 131, 208, 92, 161, 182, 89, 55, 79, 155, 15, 107, 105, 149, 241, 35, 56, 160, 255, 125, 233, 178, 32, 145, 205, 180, 139, 1},
65+
SignDataHex: []byte("97933c4c14a32569265faa098724ea08c1eed514443227f50439580b24d5fb976311fd95bb83d05ca1b659374f9b0f6b6995f12338a0ff7de9b22091cdb48b01"),
66+
SignDataBase64: []byte("l5M8TBSjJWkmX6oJhyTqCMHu1RREMif1BDlYCyTV+5djEf2Vu4PQXKG2WTdPmw9raZXxIzig/33psiCRzbSLAQ=="),
6267
},
6368
{
6469
Data: []byte("123"),
65-
SignData: []byte(""),
66-
SignDataHex: []byte(""),
67-
SignDataBase64: []byte(""),
70+
SignData: []byte{183, 71, 38, 235, 113, 179, 74, 4, 80, 180, 145, 181, 86, 153, 198, 216, 56, 131, 47, 17, 182, 193, 32, 115, 55, 234, 54, 115, 51, 233, 1, 32, 143, 47, 134, 74, 34, 230, 226, 223, 148, 144, 123, 68, 12, 216, 138, 226, 237, 65, 234, 71, 229, 183, 69, 29, 125, 144, 189, 93, 61, 220, 7, 3},
71+
SignDataHex: []byte("b74726eb71b34a0450b491b55699c6d838832f11b6c1207337ea367333e901208f2f864a22e6e2df94907b440cd88ae2ed41ea47e5b7451d7d90bd5d3ddc0703"),
72+
SignDataBase64: []byte("t0cm63GzSgRQtJG1VpnG2DiDLxG2wSBzN+o2czPpASCPL4ZKIubi35SQe0QM2Iri7UHqR+W3RR19kL1dPdwHAw=="),
6873
},
6974
{
7075
Data: []byte("你好,世界"),
71-
SignData: []byte(""),
72-
SignDataHex: []byte(""),
73-
SignDataBase64: []byte(""),
76+
SignData: []byte{58, 139, 184, 49, 247, 17, 190, 45, 156, 70, 236, 70, 184, 147, 248, 17, 193, 200, 151, 37, 104, 85, 120, 70, 189, 174, 24, 213, 190, 231, 23, 3, 113, 235, 186, 66, 8, 228, 77, 122, 251, 137, 161, 2, 70, 217, 98, 244, 89, 197, 53, 22, 239, 44, 219, 192, 127, 15, 40, 13, 190, 40, 99, 11},
77+
SignDataHex: []byte("3a8bb831f711be2d9c46ec46b893f811c1c8972568557846bdae18d5bee7170371ebba4208e44d7afb89a10246d962f459c53516ef2cdbc07f0f280dbe28630b"),
78+
SignDataBase64: []byte("Oou4MfcRvi2cRuxGuJP4EcHIlyVoVXhGva4Y1b7nFwNx67pCCORNevuJoQJG2WL0WcU1Fu8s28B/DygNvihjCw=="),
7479
},
7580
}
7681

@@ -83,6 +88,10 @@ func TestSignVerify(t *testing.T) {
8388
t.Fatal(err)
8489
}
8590

91+
if !slices.Equal(sign, testCase.SignData) {
92+
t.Fatalf("data %q: got %+v != expect %+v", testCase.Data, sign, testCase.SignData)
93+
}
94+
8695
// Hex
8796
sign = privateKey.Sign(testCase.Data, WithHex())
8897

@@ -91,12 +100,20 @@ func TestSignVerify(t *testing.T) {
91100
t.Fatal(err)
92101
}
93102

103+
if !slices.Equal(sign, testCase.SignDataHex) {
104+
t.Fatalf("data %q: got %+v != expect %+v", testCase.Data, sign, testCase.SignDataHex)
105+
}
106+
94107
// Base64
95108
sign = privateKey.Sign(testCase.Data, WithBase64())
96109

97110
err = publicKey.Verify(testCase.Data, sign, WithBase64())
98111
if err != nil {
99112
t.Fatal(err)
100113
}
114+
115+
if !slices.Equal(sign, testCase.SignDataBase64) {
116+
t.Fatalf("data %q: got %+v != expect %+v", testCase.Data, sign, testCase.SignDataBase64)
117+
}
101118
}
102119
}

0 commit comments

Comments
 (0)