-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaesrw_test.go
49 lines (43 loc) · 1.39 KB
/
aesrw_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 aesrw
import "fmt"
import "math/rand"
import "testing"
//Characters to use in random strings
const CHRB = "!\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f"
//Max string length to test
const MAXL = 999
//Generate a slice of random bytes of length n
//using characters in CHRB
func RandomBytes(n int) []byte {
s := make([]byte, n)
l64 := int64(len(CHRB))
for i := range s {
s[i] = CHRB[rand.Int63() % l64]
}
return s
}
//Generate a slice of random string of length n
//using characters in CHRB
func RandomString(n int) string {
return string(RandomBytes(n))
}
//Test generating random strings, encrypting them, decrypting them
//and verifying the result is same as the start
func TestString(t *testing.T) {
var keyLen = []int { 16, 24, 32 }
for i := 0; i < 999; i++ {
k := RandomBytes(keyLen[i % len(keyLen)]) //Random encryption key of length 16, 24 or 32
s1 := RandomString(rand.Int() % MAXL) //Random start
s2, e := EncryptString(s1, k) //Encrypted
if e != nil {
t.Error(fmt.Sprintf("%s", e))
}
s3, e := DecryptString(s2, k) //Decrypted
if e != nil {
t.Error(fmt.Sprintf("%s", e))
}
if s1 != s3 {
t.Error("Original and decrypted strings do not match!")
}
}
}