forked from glycerine/goq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
djbnacl_test.go
65 lines (49 loc) · 1.76 KB
/
djbnacl_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
package main
import (
"fmt"
"testing"
"time"
cv "github.com/glycerine/goconvey/convey"
nacl "golang.org/x/crypto/nacl/secretbox"
)
func TestDjbNaclSealOpen(t *testing.T) {
var key [32]byte
start := time.Now()
orig := []byte("12345678901234567890123456789012")
hashed := HashAlotSha256(orig)
copy(key[:], hashed[:])
fmt.Printf("that took: %v\n", time.Since(start)) // 80 usec
cv.Convey("So that we can use the encyprtion of DJB's NaCl (salt) suite, we wrap with Seal/Open from crypto/nacl/secretbox.", t, func() {
message := []byte("I'm a little teapot")
var nonce, nonce2 [LEN_NONCE_BYTES]byte
generateNonce24(&nonce)
generateNonce24(&nonce2)
// fmt.Printf("nonce = '%#v'\n", nonce)
// fmt.Printf("nonce2 = '%#v'\n", nonce2)
cv.Convey("nonce should not be all zeros: the nonce should have been intialized by generateNonce24()", func() {
cv.So(nonce[:], cv.ShouldNotResemble, make([]byte, LEN_NONCE_BYTES))
})
cv.Convey("two different nonces should be different", func() {
cv.So(nonce[:], cv.ShouldNotResemble, nonce2[:])
})
box := NaClEncryptWithRandomNoncePrepended(message, &key)
opened, ok := NaclDecryptWithNoncePrepended(box, &key)
cv.Convey("When a NaCl box is Sealed and then re-Opened, we should be able to open the box without error", func() {
cv.So(ok, cv.ShouldBeTrue)
})
cv.Convey(" ... and should return the same plaintext", func() {
cv.So(opened, cv.ShouldResemble, message)
})
cv.Convey("Integrity checking should catch any single bit modification of the cyphertext", func() {
for i := range box {
box[i] ^= 0x10
_, ok := nacl.Open(nil, box, &nonce, &key)
if ok {
t.Fatalf("opened box with byte %d corrupted", i)
}
cv.So(ok, cv.ShouldBeFalse)
box[i] ^= 0x10
}
})
})
}