-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlegacy.js
85 lines (67 loc) · 2 KB
/
legacy.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
import test from 'tape'
import { Cryptor } from '../'
const secret = 'mysecretpassword'
const data = 'sensitive data to encrypt'
test('Encrypts and decrypts data', t => {
t.plan(1)
const cryptor = new Cryptor(secret)
const encrypted = cryptor.encrypt(data)
const decrypted = cryptor.decrypt(encrypted)
t.equal(decrypted, data)
})
test('Another cryptor instance can decrypt data', t => {
t.plan(1)
const cryptor1 = new Cryptor(secret)
const encrypted = cryptor1.encrypt(data)
const cryptor2 = new Cryptor(secret)
const decrypted = cryptor2.decrypt(encrypted)
t.equal(decrypted, data)
})
test('Encryptor generates a different value each time', t => {
t.plan(1)
const cryptor = new Cryptor(secret)
const encrypted1 = cryptor.encrypt(data)
const encrypted2 = cryptor.encrypt(data)
t.notEqual(encrypted1, encrypted2)
})
test('Throws an error if no secret is set for Cryptor', t => {
t.plan(5)
t.throws(() => new Cryptor(), 'Cryptor: secret must be a non-empty string')
t.throws(
() => new Cryptor(null),
'Cryptor: secret must be a non-empty string'
)
t.throws(() => new Cryptor(0), 'Cryptor: secret must be a non-empty string')
t.throws(
() => new Cryptor(Integer(12345678)),
'Cryptor: secret must be a non-empty string'
)
t.throws(
() => new Cryptor(Buffer.from('12345678')),
'Cryptor: secret must be a non-empty string'
)
})
test('Throws an error if encryption value is null or undefined', t => {
t.plan(2)
const cryptor = new Cryptor(secret)
t.throws(
() => cryptor.encrypt(),
'Cryptor: secret must be a non-empty string'
)
t.throws(
() => cryptor.encrypt(null),
'Cryptor: secret must be a non-empty string'
)
})
test('Throws an error if decryption value is null or undefined', t => {
t.plan(2)
const cryptor = new Cryptor(secret)
t.throws(
() => cryptor.decrypt(),
'Cryptor: secret must be a non-empty string'
)
t.throws(
() => cryptor.decrypt(null),
'Cryptor: secret must be a non-empty string'
)
})