forked from gtank/isaac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isaac_test.go
124 lines (107 loc) · 2.65 KB
/
isaac_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
package isaac
import (
"bufio"
"bytes"
"fmt"
"os"
"testing"
)
/* This tests against the official randvect.txt, which outputs two rounds with no key (zeros) */
func TestZeros(t *testing.T) {
vect, err := os.Open("randvect.txt")
if err != nil {
t.Error("could not find text vector file")
}
defer vect.Close()
scanner := bufio.NewScanner(vect)
scanner.Scan()
var rng ISAAC
rng.randInit(true)
var buf bytes.Buffer
for i := 0; i < 2; i++ {
rng.isaac()
for j := 0; j < 256; j++ {
buf.WriteString(fmt.Sprintf("%.8x", rng.randrsl[j]))
if (j & 7) == 7 {
var output = buf.String()
if scanner.Text() == output {
scanner.Scan()
buf.Reset()
} else {
fmt.Printf("o: " + output + "\n" + "v: " + scanner.Text() + "\n")
t.Fail()
return
}
}
}
}
}
/* This tests against the randrsl state of randtest.c after randInit()
* to make sure that my seeding procedure works the same as the reference */
func TestSeed(t *testing.T) {
keyVector, _ := os.Open("keytest.txt")
defer keyVector.Close()
keyScan := bufio.NewScanner(keyVector)
keyScan.Scan()
var rng ISAAC
var key = "This is <i>not</i> the right mytext."
rng.Seed(key)
var buf bytes.Buffer
for i := 0; i < 256; i++ {
buf.WriteString(fmt.Sprintf("%.8x ", rng.randrsl[i]))
if (i & 7) == 7 {
var output = buf.String()
if keyScan.Text() == output {
keyScan.Scan()
buf.Reset()
} else {
fmt.Printf("index: %d\n", i)
fmt.Printf("o: " + output + "\n" + "v: " + keyScan.Text() + "\n")
t.Fail()
return
}
}
}
}
/* This tests output against the official randseed.txt */
func TestRand(t *testing.T) {
testVector, _ := os.Open("randseed.txt")
defer testVector.Close()
outScan := bufio.NewScanner(testVector)
outScan.Scan()
var rng ISAAC
var key = "This is <i>not</i> the right mytext."
rng.Seed(key)
var buf bytes.Buffer
k := 0
for i := 0; i < 10; i++ {
for j := 0; j < 256; j++ {
buf.WriteString(fmt.Sprintf("%.8x ", rng.Rand()))
k++
if k == 8 {
k = 0
if outScan.Text() == buf.String() {
outScan.Scan()
buf.Reset()
} else {
fmt.Printf("o: " + outScan.Text() + "\n" + "v: " + buf.String() + "\n")
t.Fail()
return
}
}
}
}
}
func TestStream(t *testing.T) {
var key = "This is <i>not</i> the right mytext."
var plaintext = []byte("Hello, world")
var ciphertext = make([]byte, len(plaintext))
var decrypted = make([]byte, len(plaintext))
enc := NewISAACStream(key)
enc.XORKeyStream(ciphertext, plaintext)
dec := NewISAACStream(key)
dec.XORKeyStream(decrypted, ciphertext)
if !bytes.Equal(plaintext, decrypted) {
t.Errorf("Plaintext not equal to decrypted ciphertext")
}
}