-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
53 lines (46 loc) · 1.54 KB
/
index.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
import t from 'tap'
import crypto from 'crypto'
import { createEnvelopeEncryptor } from './index.js'
const plaintextKey = crypto
.createHash('sha256')
.update('password')
.digest()
const keyService = {
getDataKey: () =>
Promise.resolve({
encryptedKey: 'foo',
plaintextKey
}),
decryptDataKey: key => Promise.resolve(plaintextKey)
}
const tests = [
'smallSecret',
'mediumSecretLength',
'superSuperSuperSuperSuperSuperSuperSuperSuperSuperSuperLongStringWithLotsOfChararctersThatWillBeEncryptedAndDecryptedProperly'
]
tests.forEach((plaintextInput) => {
t.test('envelope encryptor', async t => {
const encryptor = createEnvelopeEncryptor(keyService)
const result = await encryptor.encrypt(plaintextInput)
const { key } = result
t.equal(key, 'foo', 'encryptedKey is from keyService.getDataKey')
const secretMessage = await encryptor.decrypt(result)
t.equal(
secretMessage,
plaintextInput,
'given ciphertext, key and salt decrypt returns plaintext message'
)
})
t.test('envelope encryptor - supports changing ciphertext encoding format ', async t => {
const encryptor = createEnvelopeEncryptor(keyService, { encoding: 'base64' })
const result = await encryptor.encrypt(plaintextInput)
const { key } = result
t.equal(key, 'foo', 'encryptedKey is from keyService.getDataKey')
const secretMessage = await encryptor.decrypt(result)
t.equal(
secretMessage,
plaintextInput,
'given ciphertext, key and salt decrypt returns plaintext message'
)
})
})