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