forked from schollz/croc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_test.go
49 lines (46 loc) · 1.1 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
package main
import (
"io/ioutil"
"os"
"testing"
)
func TestEncrypt(t *testing.T) {
key := GetRandomName()
encrypted, salt, iv := Encrypt([]byte("hello, world"), key)
decrypted, err := Decrypt(encrypted, key, salt, iv)
if err != nil {
t.Error(err)
}
if string(decrypted) != "hello, world" {
t.Error("problem decrypting")
}
_, err = Decrypt(encrypted, "wrong passphrase", salt, iv)
if err == nil {
t.Error("should not work!")
}
}
func TestEncryptFiles(t *testing.T) {
key := GetRandomName()
if err := ioutil.WriteFile("temp", []byte("hello, world!"), 0644); err != nil {
t.Error(err)
}
if err := EncryptFile("temp", "temp.enc", key); err != nil {
t.Error(err)
}
if err := DecryptFile("temp.enc", "temp.dec", key); err != nil {
t.Error(err)
}
data, err := ioutil.ReadFile("temp.dec")
if string(data) != "hello, world!" {
t.Errorf("Got something weird: " + string(data))
}
if err != nil {
t.Error(err)
}
if err := DecryptFile("temp.enc", "temp.dec", key+"wrong password"); err == nil {
t.Error("should throw error!")
}
os.Remove("temp.dec")
os.Remove("temp.enc")
os.Remove("temp")
}