-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto_test.go
123 lines (100 loc) · 2.02 KB
/
crypto_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
package crypto_test
import (
"strings"
"testing"
"github.com/gomig/crypto"
)
const algo crypto.HashAlgo = crypto.SHA3256
func cr() crypto.Crypto {
return crypto.NewCryptography("S0Me SeCure KeY")
}
func TestHash(t *testing.T) {
_, err := cr().Hash("Some Data", algo)
if err != nil {
t.Fatal(err)
}
}
func TestHashFilename(t *testing.T) {
hash, err := cr().HashFilename("file.jpg", algo)
if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(hash, ".jpg") {
t.Errorf("failed to generate hash file name!")
}
}
func TestHashSize(t *testing.T) {
if cr().HashSize(algo) != 64 {
t.Error("invalid hash size!")
}
}
func TestCheck(t *testing.T) {
hash, err := cr().Hash("My Password", algo)
if err != nil {
t.Fatal(err)
}
ok, err := cr().Check("My Password", hash, algo)
if err != nil {
t.Fatal(err)
}
if !ok {
t.Error("failed to check hash!")
}
}
func TestEncrypt(t *testing.T) {
_, err := cr().Encrypt([]byte("Some Data"))
if err != nil {
t.Fatal(err)
}
}
func TestDecrypt(t *testing.T) {
enc, err := cr().Encrypt([]byte("Data To Enc"))
if err != nil {
t.Fatal(err)
}
dec, err := cr().Decrypt(enc)
if err != nil {
t.Fatal(err)
}
if string(dec) != "Data To Enc" {
t.Error("failed to decrypt data")
}
}
func TestEncryptHEX(t *testing.T) {
_, err := cr().EncryptHEX([]byte("Some Data"))
if err != nil {
t.Fatal(err)
}
}
func TestDecryptHex(t *testing.T) {
enc, err := cr().EncryptHEX([]byte("Data To Enc"))
if err != nil {
t.Fatal(err)
}
dec, err := cr().DecryptHex(enc)
if err != nil {
t.Fatal(err)
}
if string(dec) != "Data To Enc" {
t.Error("failed to decrypt data")
}
}
func TestEncryptBase64(t *testing.T) {
_, err := cr().EncryptBase64([]byte("Some Data"))
if err != nil {
t.Fatal(err)
}
}
func TestDecryptBase64(t *testing.T) {
enc, err := cr().EncryptBase64([]byte("Data To Enc"))
if err != nil {
t.Fatal(err)
}
dec, err := cr().DecryptBase64(enc)
if err != nil {
t.Fatal(err)
}
if string(dec) != "Data To Enc" {
t.Error("failed to decrypt data")
}
}