-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (34 loc) · 820 Bytes
/
index.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
/*
* Wraps `node-fpe`, https://www.npmjs.com/package/node-fpe.
*
* Authors:
*
* - Nicholas Kyriakides, @nicholaswmin
*/
'use strict';
const fpe = require('node-fpe');
class Encryptor {
constructor(opts = {password: null}) {
if (!opts.password) {
throw new Error('You need to pass a valid password to use for encrypting/decrypting');
}
this._encryptor = fpe({
password: opts.password,
domain: this._getAllASCIICharacters()
});
}
encrypt(plaintext) {
return this._encryptor.encrypt(plaintext);
}
decrypt(ciphertext) {
return this._encryptor.decrypt(ciphertext);
}
_getAllASCIICharacters() {
const chars = [];
for (let i = 32; i < 127; ++i) {
chars.push(String.fromCharCode(i));
}
return chars;
}
}
module.exports = Encryptor;