forked from tidwall/secret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_test.go
55 lines (47 loc) · 1.11 KB
/
secret_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
package secret
import (
"bytes"
"encoding/hex"
"math/rand"
"testing"
"time"
)
func TestEncrypt(t *testing.T) {
rand.Seed(time.Now().UnixNano())
start := time.Now()
for time.Since(start) < time.Second*2 {
key := make([]byte, 32)
rand.Read(key[:])
data := make([]byte, rand.Intn(256))
rand.Read(data[:])
encdata, err := Encrypt(string(key), data)
if err != nil {
t.Fatalf("encrypt: %s: key: %s, data: %s",
err.Error(), hex.EncodeToString(key), hex.EncodeToString(data))
}
decdata, err := Decrypt(string(key), encdata)
if err != nil {
t.Fatalf("decrypt: %s: key: %s, data: %s",
err.Error(), hex.EncodeToString(key), hex.EncodeToString(data))
}
if !bytes.Equal(decdata, data) {
t.Fatalf("mismatch: key: %s, data: %s",
hex.EncodeToString(key), hex.EncodeToString(data))
}
}
}
func TestSimple(t *testing.T) {
key := "hello world"
data := []byte("hello jello")
encdata, err := Encrypt(key, data)
if err != nil {
panic(err)
}
decdata, err := Decrypt("hello world", encdata)
if err != nil {
panic(err)
}
if string(decdata) != string(data) {
panic("mismatch")
}
}