/
cryptr.ts
41 lines (31 loc) · 1.16 KB
/
cryptr.ts
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
import crypto from 'crypto';
const algorithm = 'aes-256-ctr';
export default class Cryptr {
private key: Buffer;
constructor(secret: string) {
if (!secret || typeof secret !== 'string') {
throw new Error('Cryptr: secret must be a non-0-length string');
}
this.key = crypto.createHash('sha256').update(String(secret)).digest();
}
public encrypt(value: string): string {
if (value == null) {
throw new Error('value must not be null or undefined');
}
const iv = crypto.randomBytes(16).toString('hex').slice(0, 16);
const cipher = crypto.createCipheriv(algorithm, this.key, iv);
const encrypted =
cipher.update(String(value), 'utf8', 'hex') + cipher.final('hex');
return iv + encrypted;
}
public decrypt(value: string | Buffer): string {
if (value == null) {
throw new Error('value must not be null or undefined');
}
const stringValue = String(value);
const iv = stringValue.slice(0, 16);
const encrypted = stringValue.slice(16);
const decipher = crypto.createDecipheriv(algorithm, this.key, iv);
return decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');
}
}