This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
aead.test.js
95 lines (73 loc) · 2.18 KB
/
aead.test.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
const expect = require('chai').expect
const sodium = require('./index')
describe('aead Suite', () => {
before(() => {
sodium.SecBuf.setLockLevel(sodium.SecBuf.LOCK_NONE)
})
it('should fail with bad enc message', () => {
expect(() => {
sodium.aead.enc()
}).throws()
})
it('should fail with bad enc secret', () => {
expect(() => {
sodium.aead.enc(Buffer.alloc(0))
}).throws()
})
it('should fail with bad enc adata', () => {
expect(() => {
sodium.aead.enc(Buffer.alloc(0), new sodium.SecBuf(1), 'hello')
}).throws()
})
it('should fail with bad dec nonce', () => {
expect(() => {
sodium.aead.dec()
}).throws()
})
it('should fail with bad dec cipher', () => {
expect(() => {
sodium.aead.dec(Buffer.alloc(0))
}).throws()
})
it('should fail with bad dec secret', () => {
expect(() => {
sodium.aead.dec(Buffer.alloc(0), Buffer.alloc(0))
}).throws()
})
it('should fail with bad dec adata', () => {
expect(() => {
sodium.aead.dec(
Buffer.alloc(0), Buffer.alloc(0), new sodium.SecBuf(1), 'hello')
}).throws()
})
it('should encrypt and decrypt', () => {
const secret = new sodium.SecBuf(32)
secret.randomize()
const { nonce, cipher } = sodium.aead.enc(
Buffer.from('hello'), secret)
const message = sodium.aead.dec(nonce, cipher, secret).toString()
secret.free()
expect(message).equals('hello')
})
it('should encrypt and decrypt with auth aead', () => {
const secret = new sodium.SecBuf(32)
secret.randomize()
const { nonce, cipher } = sodium.aead.enc(
Buffer.from('hello'), secret, Buffer.from('aead'))
const message = sodium.aead.dec(
nonce, cipher, secret, Buffer.from('aead')).toString()
secret.free()
expect(message).equals('hello')
})
it('should fail with bad aead', () => {
const secret = new sodium.SecBuf(32)
secret.randomize()
const { nonce, cipher } = sodium.aead.enc(
Buffer.from('hello'), secret, Buffer.from('aead'))
expect(() => {
sodium.aead.dec(
nonce, cipher, secret, Buffer.from('aead-bad')).toString()
}).throws()
secret.free()
})
})