1 // Copyright 2012 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 "golang.org/x/crypto/curve25519"
16 func TestSealOpen(t *testing.T) {
17 publicKey1, privateKey1, _ := GenerateKey(rand.Reader)
18 publicKey2, privateKey2, _ := GenerateKey(rand.Reader)
20 if *privateKey1 == *privateKey2 {
21 t.Fatalf("private keys are equal!")
23 if *publicKey1 == *publicKey2 {
24 t.Fatalf("public keys are equal!")
26 message := []byte("test message")
29 box := Seal(nil, message, &nonce, publicKey1, privateKey2)
30 opened, ok := Open(nil, box, &nonce, publicKey2, privateKey1)
32 t.Fatalf("failed to open box")
35 if !bytes.Equal(opened, message) {
36 t.Fatalf("got %x, want %x", opened, message)
41 _, ok := Open(nil, box, &nonce, publicKey2, privateKey1)
43 t.Fatalf("opened box with byte %d corrupted", i)
49 func TestBox(t *testing.T) {
50 var privateKey1, privateKey2 [32]byte
51 for i := range privateKey1[:] {
54 for i := range privateKey2[:] {
58 var publicKey1 [32]byte
59 curve25519.ScalarBaseMult(&publicKey1, &privateKey1)
61 for i := range message[:] {
66 for i := range nonce[:] {
70 box := Seal(nil, message[:], &nonce, &publicKey1, &privateKey2)
72 // expected was generated using the C implementation of NaCl.
73 expected, _ := hex.DecodeString("78ea30b19d2341ebbdba54180f821eec265cf86312549bea8a37652a8bb94f07b78a73ed1708085e6ddd0e943bbdeb8755079a37eb31d86163ce241164a47629c0539f330b4914cd135b3855bc2a2dfc")
75 if !bytes.Equal(box, expected) {
76 t.Fatalf("box didn't match, got\n%x\n, expected\n%x", box, expected)