-
Notifications
You must be signed in to change notification settings - Fork 1
/
seal.js
113 lines (91 loc) · 3.58 KB
/
seal.js
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
const isBuffer = require('is-buffer')
/* eslint-disable camelcase */
const {
crypto_box_PUBLICKEYBYTES,
crypto_box_SECRETKEYBYTES,
crypto_box_SEALBYTES,
} = require('sodium-universal')
const { seal, open } = require('../seal')
const { keyPair } = require('../curve25519')
const test = require('./helpers/runner')
test.cb('seal(message, opts) is a function', (t) => {
t.true('function' === typeof seal)
t.end()
})
test.cb('seal(message, opts) throws on bad input', (t) => {
t.throws(() => seal(), { instanceOf: TypeError })
t.throws(() => seal(null, null), { instanceOf: TypeError })
t.throws(() => seal(Buffer.alloc(0), null), { instanceOf: TypeError })
t.throws(() => seal(Buffer.from('hello'), null), { instanceOf: TypeError })
t.throws(() => seal(Buffer.from('hello'), {}), { instanceOf: TypeError })
t.throws(() => seal(Buffer.from('hello'), { publicKey: null }), { instanceOf: TypeError })
t.throws(() => seal(
Buffer.from('hello'),
{ publicKey: Buffer.alloc(crypto_box_PUBLICKEYBYTES - 1) }
), { instanceOf: TypeError })
t.end()
})
test.cb('seal(message, opts) seals a message', (t) => {
const message = Buffer.from('hello')
const { publicKey } = keyPair()
const sealed = seal(message, { publicKey })
t.true(isBuffer(sealed))
t.true(sealed.length === crypto_box_SEALBYTES + message.length)
t.end()
})
test.cb('open(message, opts) is a function', (t) => {
t.true('function' === typeof open)
t.true(open === seal.open)
t.end()
})
test.cb('open(message, opts) throws on bad input', (t) => {
const { publicKey, secretKey } = keyPair()
const sealed = seal(Buffer.from('hello'), { publicKey })
t.throws(() => open(), { instanceOf: TypeError })
t.throws(() => open(null, null), { instanceOf: TypeError })
t.throws(() => open(sealed), { instanceOf: TypeError })
t.throws(() => open(sealed, null), { instanceOf: TypeError })
t.throws(() => open(sealed, {}), { instanceOf: TypeError })
t.throws(() => open(sealed, { publicKey }), { instanceOf: TypeError })
t.throws(() => open(Buffer.alloc(0), null), { instanceOf: TypeError })
t.throws(() => open(sealed.slice(0, -1), {}), { instanceOf: TypeError })
t.throws(() => open(Buffer.alloc(crypto_box_SEALBYTES), {
publicKey: Buffer.alloc(0)
}), { instanceOf: TypeError })
t.throws(() => open(Buffer.alloc(crypto_box_SEALBYTES), {
publicKey: null,
}), { instanceOf: TypeError })
t.throws(() => open(Buffer.alloc(crypto_box_SEALBYTES), {
publicKey,
secretKey: Buffer.alloc(0)
}), { instanceOf: TypeError })
t.throws(() => open(Buffer.alloc(crypto_box_SEALBYTES), {
publicKey,
secretKey: Buffer.alloc(crypto_box_SECRETKEYBYTES)
}), { instanceOf: TypeError })
t.throws(() => open(Buffer.alloc(crypto_box_SEALBYTES), {
publicKey: Buffer.alloc(crypto_box_PUBLICKEYBYTES),
secretKey: Buffer.alloc(crypto_box_SECRETKEYBYTES)
}), { instanceOf: TypeError })
t.throws(() => open(Buffer.alloc(crypto_box_SEALBYTES), {
publicKey,
secretKey: Buffer.alloc(crypto_box_SECRETKEYBYTES - 1)
}), { instanceOf: TypeError })
t.throws(() => open(sealed, {
publicKey: publicKey.slice(0, -1),
secretKey,
}), { instanceOf: TypeError })
t.throws(() => open(sealed, {
publicKey,
secretKey: secretKey.slice(0, -1)
}), { instanceOf: TypeError })
t.end()
})
test.cb('open(message, opts) opens a sealed message', (t) => {
const { publicKey, secretKey } = keyPair()
const message = Buffer.from('hello')
const sealed = seal(message, { publicKey })
const opened = open(sealed, { publicKey, secretKey })
t.true(0 === Buffer.compare(message, opened))
t.end()
})