-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer_test.go
43 lines (32 loc) · 1.06 KB
/
signer_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
package sshsign_test
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"io"
"testing"
"github.com/SierraSoftworks/sshsign-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
)
func TestRoundTrip(t *testing.T) {
pk, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err, "private key should be generated correctly")
ss, err := ssh.NewSignerFromKey(pk)
require.NoError(t, err, "signer should be generated correctly")
signer := sshsign.DefaultSigner("test", "sha512", ss)
data := getRandomData(1024)
sig, err := signer.Sign(bytes.NewBuffer(data))
require.NoError(t, err, "signature should be generated correctly")
assert.NotNil(t, sig, "signature should not be nil")
assert.Equal(t, "sha512", sig.HashAlgorithm)
assert.Equal(t, "test", sig.Namespace)
verifier := sshsign.DefaultVerifier("test", "sha512")
assert.NoError(t, verifier.Verify(bytes.NewBuffer(data), sig))
}
func getRandomData(n int) []byte {
data := bytes.NewBuffer(nil)
io.CopyN(data, rand.Reader, 1024)
return data.Bytes()
}