forked from forgoer/openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsa_test.go
54 lines (42 loc) · 1.31 KB
/
rsa_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 openssl
import (
"bytes"
"crypto"
"encoding/base64"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRSAEncrypt(t *testing.T) {
priBuf := bytes.NewBuffer(nil)
err := RSAGenerateKey(2048, priBuf)
assert.NoError(t, err)
t.Logf("private key: %s\n", priBuf.Bytes())
pubBuf := bytes.NewBuffer(nil)
err = RSAGeneratePublicKey(priBuf.Bytes(), pubBuf)
assert.NoError(t, err)
t.Logf("public key: %s\n", pubBuf.Bytes())
src := []byte("123456")
dst, err := RSAEncrypt(src, pubBuf.Bytes())
assert.NoError(t, err)
t.Logf("encrypt out: %s\n", base64.RawStdEncoding.EncodeToString(dst))
dst, err = RSADecrypt(dst, priBuf.Bytes())
assert.NoError(t, err)
assert.Equal(t, src, dst)
t.Logf("src: %s \ndst:%s", src, dst)
}
func TestRSASign(t *testing.T) {
priBuf := bytes.NewBuffer(nil)
err := RSAGenerateKey(2048, priBuf)
assert.NoError(t, err)
t.Logf("private key: %s\n", priBuf.Bytes())
pubBuf := bytes.NewBuffer(nil)
err = RSAGeneratePublicKey(priBuf.Bytes(), pubBuf)
assert.NoError(t, err)
t.Logf("public key: %s\n", pubBuf.Bytes())
src := []byte("123456")
sign, err := RSASign(src, priBuf.Bytes(), crypto.SHA256)
assert.NoError(t, err)
t.Logf("sign out: %s\n", base64.RawStdEncoding.EncodeToString(sign))
err = RSAVerify(src, sign, pubBuf.Bytes(), crypto.SHA256)
assert.NoError(t, err)
}