Skip to content

Commit 2a0d8f0

Browse files
author
Gusted
committed
Add test function
1 parent 05f5eb0 commit 2a0d8f0

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

modules/crypto/ed25519_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package crypto
2+
3+
import (
4+
"bytes"
5+
"crypto/rand"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
const (
12+
testPublicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAOhB7/zzhC+HXDdGOdLwJln5NYwm6UNXx3chmQSVTG4\n"
13+
testPrivateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
14+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtz
15+
c2gtZWQyNTUxOQAAACADoQe/884Qvh1w3RjnS8CZZ+TWMJulDV8d3IZkElUxuAAA
16+
AIggISIjICEiIwAAAAtzc2gtZWQyNTUxOQAAACADoQe/884Qvh1w3RjnS8CZZ+TW
17+
MJulDV8d3IZkElUxuAAAAEAAAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0e
18+
HwOhB7/zzhC+HXDdGOdLwJln5NYwm6UNXx3chmQSVTG4AAAAAAECAwQF
19+
-----END OPENSSH PRIVATE KEY-----
20+
`
21+
)
22+
23+
func TestGeneratingEd25519Keypair(t *testing.T) {
24+
// Temp override the rand.Reader for deterministic testing.
25+
oldReader := rand.Reader
26+
defer func() {
27+
rand.Reader = oldReader
28+
}()
29+
30+
// Only 32 bytes needs to be provided to generate a ed25519 keypair.
31+
// And another 32 bytes are required, which is included as random value
32+
// in the OpenSSH format.
33+
b := make([]byte, 64)
34+
for i := 0; i < 64; i++ {
35+
b[i] = byte(i)
36+
}
37+
rand.Reader = bytes.NewReader(b)
38+
39+
publicKey, privateKey, err := GenerateEd25519Keypair()
40+
assert.NoError(t, err)
41+
assert.EqualValues(t, testPublicKey, string(publicKey))
42+
assert.EqualValues(t, testPrivateKey, string(privateKey))
43+
}

0 commit comments

Comments
 (0)