-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_rsa_test.go
111 lines (99 loc) · 2.67 KB
/
crypto_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"log"
"testing"
)
func TestRSAOAEP(t *testing.T) {
pk, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
t.Logf("E: %v\nD: %v\nN: %v\n", pk.E, pk.D, pk.N)
plaintext := []byte{0x00, 0x00, 0x00, 0x01}
ciphertext, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, &pk.PublicKey, plaintext, nil)
if err != nil {
t.Fatal(err)
}
t.Run("decrypt", func(t *testing.T) {
dec, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, pk, ciphertext, nil)
if err != nil {
t.Fatalf("解密失败:%v", err)
}
t.Logf("密文:%x\n", ciphertext)
t.Logf("解密:%x\n", string(dec))
})
t.Run("modify-single-byte", func(t *testing.T) {
_enc := sliceCopy(ciphertext)
_enc[0] += 1
if _, err := rsa.DecryptOAEP(sha512.New(), rand.Reader, pk, _enc, nil); err != nil {
t.Logf("解密失败:%v", err)
}
})
}
func TestRSAPKCS1v15(t *testing.T) {
pk, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
t.Logf("E: %v\nD: %v\nN: %v\n", pk.E, pk.D, pk.N)
plaintext := []byte{0x00, 0x00, 0x00, 0x01}
ciphertext, err := rsa.EncryptPKCS1v15(rand.Reader, &pk.PublicKey, plaintext)
if err != nil {
t.Fatal(err)
}
t.Run("decrypt", func(t *testing.T) {
dec, err := rsa.DecryptPKCS1v15(rand.Reader, pk, ciphertext)
if err != nil {
t.Fatalf("解密失败:%v", err)
}
t.Logf("密文:%x\n", ciphertext)
t.Logf("解密:%x\n", string(dec))
})
t.Run("modify-single-byte", func(t *testing.T) {
_enc := sliceCopy(ciphertext)
_enc[0] += 1
if _, err := rsa.DecryptPKCS1v15(rand.Reader, pk, _enc); err != nil {
t.Logf("解密失败:%v", err)
}
})
}
func TestRSASignPSS(t *testing.T) {
pk, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
t.Logf("E: %v\nD: %v\nN: %v\n", pk.E, pk.D, pk.N)
msg := []byte{0x00, 0x00, 0x00, 0x01}
hash := sha512.New()
hash.Sum(msg)
sum := hash.Sum(nil)
sign, err := rsa.SignPSS(rand.Reader, pk, crypto.SHA512, sum, &rsa.PSSOptions{})
if err != nil {
t.Fatal(err)
}
t.Run("verify", func(t *testing.T) {
log.Print(rsa.VerifyPSS(&pk.PublicKey, crypto.SHA512, sum, sign, &rsa.PSSOptions{}))
})
}
func TestRSASignPKCS1v15(t *testing.T) {
pk, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatal(err)
}
t.Logf("E: %v\nD: %v\nN: %v\n", pk.E, pk.D, pk.N)
msg := []byte{0x00, 0x00, 0x00, 0x01}
hash := sha512.New()
hash.Sum(msg)
sum := hash.Sum(nil)
sign, err := rsa.SignPKCS1v15(rand.Reader, pk, crypto.SHA512, sum)
if err != nil {
t.Fatal(err)
}
t.Run("verify", func(t *testing.T) {
log.Print(rsa.VerifyPKCS1v15(&pk.PublicKey, crypto.SHA512, sum, sign))
})
}