-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypter_test.go
54 lines (45 loc) · 1.58 KB
/
encrypter_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
package crypt
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"testing"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/require"
)
func TestHybridEncrypter(t *testing.T) {
enc := NewHybridEncrypter(testGenerateKeyPair(t, 2048))
var plain []byte
fuzz.New().NumElements(1, 1000000).Fuzz(&plain)
encoded, err := enc.Encrypt(plain)
require.NoError(t, err, "failed to encrypt")
decrypted, err := enc.Decrypt(encoded)
require.NoError(t, err, "failed to decrypt")
require.True(t, bytes.Equal(plain, decrypted), "encrypted data not equal to plain")
}
func testGenerateKeyPair(t *testing.T, bits int) (*rsa.PrivateKey, *rsa.PublicKey) {
t.Helper()
privkey, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
t.Fatalf("failed to generate rsa Key pair: %v", err)
}
return privkey, &privkey.PublicKey
}
func Test_KeyConversion(t *testing.T) {
privateKey, publicKey := testGenerateKeyPair(t, 4096)
privateKeyBytes := PrivateKeyToBytes(privateKey)
pk, err := BytesToPrivateKey(privateKeyBytes)
require.NoError(t, err, "failed to parse private key from bytes")
require.Equal(t, privateKey, pk)
publicKeyBytes, err := PublicKeyToBytes(publicKey)
require.NoError(t, err, "failed to convert public key to bytes")
pubK, err := BytesToPublicKey(publicKeyBytes)
require.NoError(t, err, "failed to parse public key from bytes")
require.Equal(t, publicKey, pubK)
}
func Test_test(t *testing.T) {
privateKey, publicKey := testGenerateKeyPair(t, 4096)
t.Logf("\n%s\n", string(PrivateKeyToBytes(privateKey)))
toBytes, _ := PublicKeyToBytes(publicKey)
t.Logf("\n%s\n", string(toBytes))
}