|
1 |
| -var tape = require('tape') |
2 |
| -var sodium = require('../') |
| 1 | +const test = require('brittle') |
| 2 | +const sodium = require('..') |
3 | 3 |
|
4 |
| -tape('crypto_box_seed_keypair', function (t) { |
5 |
| - var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
6 |
| - var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
7 |
| - var seed = Buffer.alloc(sodium.crypto_box_SEEDBYTES, 'lo') |
| 4 | +test('crypto_box_seed_keypair', function (t) { |
| 5 | + const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
| 6 | + const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
| 7 | + const seed = Buffer.alloc(sodium.crypto_box_SEEDBYTES, 'lo') |
8 | 8 |
|
9 |
| - t.throws(function () { |
| 9 | + t.exception.all(function () { |
10 | 10 | sodium.crypto_box_seed_keypair()
|
11 | 11 | }, 'should validate input')
|
12 | 12 |
|
13 |
| - t.throws(function () { |
| 13 | + t.exception.all(function () { |
14 | 14 | sodium.crypto_box_seed_keypair(Buffer.alloc(0), Buffer.alloc(0), Buffer.alloc(0))
|
15 | 15 | }, 'should validate input length')
|
16 | 16 |
|
17 | 17 | sodium.crypto_box_seed_keypair(pk, sk, seed)
|
18 | 18 |
|
19 |
| - var eSk = '8661a95d21b134adc02881022ad86d37f32a230d537b525b997bce27aa745afc' |
20 |
| - var ePk = '425c5ba523e70411c77300bb48dd846562e6c1fcf0142d81d2567d650ce76c3b' |
| 19 | + const eSk = '8661a95d21b134adc02881022ad86d37f32a230d537b525b997bce27aa745afc' |
| 20 | + const ePk = '425c5ba523e70411c77300bb48dd846562e6c1fcf0142d81d2567d650ce76c3b' |
21 | 21 |
|
22 |
| - t.same(pk.toString('hex'), ePk, 'seeded public key') |
23 |
| - t.same(sk.toString('hex'), eSk, 'seeded secret key') |
24 |
| - t.end() |
| 22 | + t.alike(pk.toString('hex'), ePk, 'seeded public key') |
| 23 | + t.alike(sk.toString('hex'), eSk, 'seeded secret key') |
25 | 24 | })
|
26 | 25 |
|
27 |
| -tape('crypto_box_keypair', function (t) { |
28 |
| - var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
29 |
| - var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
| 26 | +test('crypto_box_keypair', function (t) { |
| 27 | + const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
| 28 | + const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
30 | 29 |
|
31 | 30 | sodium.crypto_box_keypair(pk, sk)
|
32 | 31 |
|
33 |
| - t.notEqual(pk, Buffer.alloc(pk.length), 'made public key') |
34 |
| - t.notEqual(sk, Buffer.alloc(sk.length), 'made secret key') |
| 32 | + t.not(pk, Buffer.alloc(pk.length), 'made public key') |
| 33 | + t.not(sk, Buffer.alloc(sk.length), 'made secret key') |
35 | 34 |
|
36 |
| - t.throws(function () { |
| 35 | + t.exception.all(function () { |
37 | 36 | sodium.crypto_box_keypair()
|
38 | 37 | }, 'should validate input')
|
39 | 38 |
|
40 |
| - t.throws(function () { |
| 39 | + t.exception.all(function () { |
41 | 40 | sodium.crypto_box_keypair(Buffer.alloc(0), Buffer.alloc(0))
|
42 | 41 | }, 'should validate input length')
|
43 |
| - |
44 |
| - t.end() |
45 | 42 | })
|
46 | 43 |
|
47 |
| -tape('crypto_box_detached', function (t) { |
48 |
| - var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
49 |
| - var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
50 |
| - var nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES) |
| 44 | +test('crypto_box_detached', function (t) { |
| 45 | + const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
| 46 | + const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
| 47 | + const nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES) |
51 | 48 |
|
52 | 49 | sodium.crypto_box_keypair(pk, sk)
|
53 | 50 |
|
54 |
| - var message = Buffer.from('Hello, World!') |
55 |
| - var mac = Buffer.alloc(sodium.crypto_box_MACBYTES) |
56 |
| - var cipher = Buffer.alloc(message.length) |
| 51 | + const message = Buffer.from('Hello, World!') |
| 52 | + const mac = Buffer.alloc(sodium.crypto_box_MACBYTES) |
| 53 | + const cipher = Buffer.alloc(message.length) |
57 | 54 |
|
58 | 55 | sodium.crypto_box_detached(cipher, mac, message, nonce, pk, sk)
|
59 | 56 |
|
60 |
| - t.notEqual(cipher, Buffer.alloc(cipher.length), 'not blank') |
| 57 | + t.not(cipher, Buffer.alloc(cipher.length), 'not blank') |
61 | 58 |
|
62 |
| - var plain = Buffer.alloc(cipher.length) |
63 |
| - t.notOk(sodium.crypto_box_open_detached(plain, cipher, Buffer.alloc(mac.length), nonce, pk, sk), 'does not decrypt') |
| 59 | + const plain = Buffer.alloc(cipher.length) |
| 60 | + t.absent(sodium.crypto_box_open_detached(plain, cipher, Buffer.alloc(mac.length), nonce, pk, sk), 'does not decrypt') |
64 | 61 | t.ok(sodium.crypto_box_open_detached(plain, cipher, mac, nonce, pk, sk), 'decrypts')
|
65 |
| - t.same(plain, message, 'same message') |
66 |
| - |
67 |
| - t.end() |
| 62 | + t.alike(plain, message, 'same message') |
68 | 63 | })
|
69 | 64 |
|
70 |
| -tape('crypto_box_easy', function (t) { |
71 |
| - var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
72 |
| - var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
73 |
| - var nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES) |
| 65 | +test('crypto_box_easy', function (t) { |
| 66 | + const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
| 67 | + const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
| 68 | + const nonce = Buffer.alloc(sodium.crypto_box_NONCEBYTES) |
74 | 69 |
|
75 | 70 | sodium.crypto_box_keypair(pk, sk)
|
76 | 71 |
|
77 |
| - var message = Buffer.from('Hello, World!') |
78 |
| - var cipher = Buffer.alloc(message.length + sodium.crypto_box_MACBYTES) |
| 72 | + const message = Buffer.from('Hello, World!') |
| 73 | + const cipher = Buffer.alloc(message.length + sodium.crypto_box_MACBYTES) |
79 | 74 |
|
80 | 75 | sodium.crypto_box_easy(cipher, message, nonce, pk, sk)
|
81 | 76 |
|
82 |
| - t.notEqual(cipher, Buffer.alloc(cipher.length), 'not blank') |
| 77 | + t.not(cipher, Buffer.alloc(cipher.length), 'not blank') |
83 | 78 |
|
84 |
| - var plain = Buffer.alloc(cipher.length - sodium.crypto_box_MACBYTES) |
85 |
| - t.notOk(sodium.crypto_box_open_easy(plain, Buffer.alloc(cipher.length), nonce, pk, sk), 'does not decrypt') |
| 79 | + const plain = Buffer.alloc(cipher.length - sodium.crypto_box_MACBYTES) |
| 80 | + t.absent(sodium.crypto_box_open_easy(plain, Buffer.alloc(cipher.length), nonce, pk, sk), 'does not decrypt') |
86 | 81 | t.ok(sodium.crypto_box_open_easy(plain, cipher, nonce, pk, sk), 'decrypts')
|
87 |
| - t.same(plain, message, 'same message') |
88 |
| - |
89 |
| - t.end() |
| 82 | + t.alike(plain, message, 'same message') |
90 | 83 | })
|
91 | 84 |
|
92 |
| -tape('crypto_box_seal', function (t) { |
93 |
| - var pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
94 |
| - var sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
| 85 | +test('crypto_box_seal', function (t) { |
| 86 | + const pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
| 87 | + const sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
95 | 88 |
|
96 | 89 | sodium.crypto_box_keypair(pk, sk)
|
97 | 90 |
|
98 |
| - var pk2 = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
99 |
| - var sk2 = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
| 91 | + const pk2 = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) |
| 92 | + const sk2 = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) |
100 | 93 |
|
101 | 94 | sodium.crypto_box_keypair(pk2, sk2)
|
102 | 95 |
|
103 |
| - var message = Buffer.from('Hello, sealed World!') |
104 |
| - var cipher = Buffer.alloc(message.length + sodium.crypto_box_SEALBYTES) |
| 96 | + const message = Buffer.from('Hello, sealed World!') |
| 97 | + const cipher = Buffer.alloc(message.length + sodium.crypto_box_SEALBYTES) |
105 | 98 |
|
106 | 99 | sodium.crypto_box_seal(cipher, message, pk)
|
107 |
| - t.notEqual(cipher, message, 'did not encrypt!') |
| 100 | + t.not(cipher, message, 'did not encrypt!') |
108 | 101 |
|
109 |
| - t.notEqual(cipher, Buffer.alloc(cipher.length), 'not blank') |
| 102 | + t.not(cipher, Buffer.alloc(cipher.length), 'not blank') |
110 | 103 |
|
111 |
| - var plain = Buffer.alloc(cipher.length - sodium.crypto_box_SEALBYTES) |
112 |
| - t.notOk(sodium.crypto_box_seal_open(plain, cipher, pk2, sk2), 'does not decrypt') |
| 104 | + const plain = Buffer.alloc(cipher.length - sodium.crypto_box_SEALBYTES) |
| 105 | + t.absent(sodium.crypto_box_seal_open(plain, cipher, pk2, sk2), 'does not decrypt') |
113 | 106 | t.ok(sodium.crypto_box_seal_open(plain, cipher, pk, sk), 'decrypts')
|
114 |
| - t.same(plain, message, 'same message') |
115 |
| - |
116 |
| - t.end() |
| 107 | + t.alike(plain, message, 'same message') |
117 | 108 | })
|
0 commit comments