-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.go
156 lines (142 loc) · 4 KB
/
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/kahlys/webcrypto/aes"
wecdsa "github.com/kahlys/webcrypto/ecdsa"
"github.com/kahlys/webcrypto/sha"
)
func testSha1() error {
expected, _ := hex.DecodeString("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed")
actual, err := sha.Sum1([]byte("hello world"))
if err != nil {
return fmt.Errorf("sum1 error: %s", err)
}
if bytes.Compare(expected, actual) != 0 {
return fmt.Errorf("not expected value")
}
return nil
}
func testSha256() error {
expected, _ := hex.DecodeString("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9")
actual, err := sha.Sum256([]byte("hello world"))
if err != nil {
return fmt.Errorf("sum256 error: %s", err)
}
if bytes.Compare(expected, actual) != 0 {
return fmt.Errorf("not expected value")
}
return nil
}
func testSha384() error {
expected, _ := hex.DecodeString("fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd")
actual, err := sha.Sum384([]byte("hello world"))
if err != nil {
return fmt.Errorf("sum384 error: %s", err)
}
if bytes.Compare(expected, actual) != 0 {
return fmt.Errorf("not expected value")
}
return nil
}
func testSha512() error {
expected, _ := hex.DecodeString("309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f")
actual, err := sha.Sum512([]byte("hello world"))
if err != nil {
return fmt.Errorf("sum512 error: %s", err)
}
if bytes.Compare(expected, actual) != 0 {
return fmt.Errorf("not expected value")
}
return nil
}
func testAesCbc() error {
expected, _ := hex.DecodeString("3647f8768f5198c9c60d1c2ce248b463290fe64907d38b339f649b9beb12e133")
data := []byte("yellow submarine")
iv, _ := hex.DecodeString("52cbbf25804213a7cdecfef9d22dac30")
key, _ := hex.DecodeString("f4a08eef65d0be7082c2f7dcef2b9439")
actual, err := aes.EncryptCBC(key, iv, data)
if err != nil {
return fmt.Errorf("encryption error: %s", err)
}
if bytes.Compare(expected, actual) != 0 {
return fmt.Errorf("not expected ciphertext value")
}
actual, err = aes.DecryptCBC(key, iv, actual)
if err != nil {
return fmt.Errorf("encryption error: %s", err)
}
if bytes.Compare(data, actual) != 0 {
return fmt.Errorf("not expected plaintext value")
}
return nil
}
func testAesGcm() error {
// expected, _ := hex.DecodeString("TBD")
data := []byte("yellow submarine")
iv, _ := hex.DecodeString("52cbbf25804213a7cdecfef9d22dac30")
key, _ := hex.DecodeString("f4a08eef65d0be7082c2f7dcef2b9439")
actual, err := aes.EncryptGCM(key, iv, data, nil)
if err != nil {
return fmt.Errorf("encryption error: %s", err)
}
// if bytes.Compare(expected, actual) != 0 {
// return fmt.Errorf("not expected ciphertext value")
// }
actual, err = aes.DecryptGCM(key, iv, actual, nil)
if err != nil {
return fmt.Errorf("encryption error: %s", err)
}
if bytes.Compare(data, actual) != 0 {
return fmt.Errorf("not expected plaintext value")
}
return nil
}
func testEcdsa() error {
text := []byte("yellow submarine")
priv, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
sig, err := wecdsa.Sign(priv, text)
if err != nil {
return fmt.Errorf("unable to sign: %v", err)
}
err = wecdsa.Verify(&priv.PublicKey, sig, text)
if err != nil {
return fmt.Errorf("unable to verify: %v", err)
}
return nil
}
func main() {
register(
testfunc{"SHA-1", testSha1},
testfunc{"SHA-256", testSha256},
testfunc{"SHA-384", testSha384},
testfunc{"SHA-512", testSha512},
testfunc{"AES-CBC", testAesCbc},
testfunc{"AES-GCM", testAesGcm},
testfunc{"ECDSA", testEcdsa},
)
run()
}
type testfunc struct {
name string
test func() error
}
var testset []testfunc
func register(tests ...testfunc) {
testset = append(testset, tests...)
}
func run() {
status := "SUCCESS"
for _, t := range testset {
fmt.Println("==", t.name)
if err := t.test(); err != nil {
fmt.Println(err)
status = "FAILED"
}
}
fmt.Printf("\nTESTS STATUS: %s\n", status)
}