forked from keys-pub/keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_test.go
71 lines (58 loc) · 2.28 KB
/
encode_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package keys_test
import (
"testing"
"github.com/keys-pub/keys"
"github.com/stretchr/testify/require"
)
func TestEncodeKeyToSaltpack(t *testing.T) {
sk := keys.GenerateEdX25519Key()
msg, err := keys.EncodeSaltpackKey(sk, "testpassword")
require.NoError(t, err)
_, err = keys.DecodeSaltpackKey(msg, "invalidpassword", false)
require.EqualError(t, err, "failed to decrypt saltpack encoded key: failed to decrypt with a password: secretbox open failed")
skOut, err := keys.DecodeSaltpackKey(msg, "testpassword", false)
require.NoError(t, err)
require.Equal(t, sk.Type(), skOut.Type())
require.Equal(t, sk.Bytes(), skOut.Bytes())
}
func TestEncodeKeyDecodeKey(t *testing.T) {
sk := keys.GenerateEdX25519Key()
// Saltpack (password)
msg, err := keys.EncodeKey(sk, keys.SaltpackEncoding, "testpassword")
require.NoError(t, err)
out, err := keys.DecodeKey(msg, keys.SaltpackEncoding, "testpassword")
require.NoError(t, err)
require.Equal(t, sk.Type(), out.Type())
require.Equal(t, sk.Bytes(), out.Bytes())
// Saltpack (no password)
msg, err = keys.EncodeKey(sk, keys.SaltpackEncoding, "")
require.NoError(t, err)
out, err = keys.DecodeKey(msg, keys.SaltpackEncoding, "")
require.NoError(t, err)
require.Equal(t, sk.Type(), out.Type())
require.Equal(t, sk.Bytes(), out.Bytes())
// Saltpack (public)
_, err = keys.EncodeKey(sk.PublicKey(), keys.SaltpackEncoding, "")
require.EqualError(t, err, "failed to encode to saltpack: unsupported key ed25519-public")
// SSH (public)
msg, err = keys.EncodeKey(sk.PublicKey(), keys.SSHEncoding, "")
require.NoError(t, err)
out, err = keys.DecodeKey(msg, keys.SSHEncoding, "")
require.NoError(t, err)
require.Equal(t, sk.PublicKey().Type(), out.Type())
require.Equal(t, sk.PublicKey().Bytes(), out.Bytes())
// SSH (password)
msg, err = keys.EncodeKey(sk, keys.SSHEncoding, "testpassword")
require.NoError(t, err)
out, err = keys.DecodeKey(msg, keys.SSHEncoding, "testpassword")
require.NoError(t, err)
require.Equal(t, sk.Type(), out.Type())
require.Equal(t, sk.Bytes(), out.Bytes())
// SSH (no password)
msg, err = keys.EncodeKey(sk, keys.SSHEncoding, "")
require.NoError(t, err)
out, err = keys.DecodeKey(msg, keys.SSHEncoding, "")
require.NoError(t, err)
require.Equal(t, sk.Type(), out.Type())
require.Equal(t, sk.Bytes(), out.Bytes())
}