-
Notifications
You must be signed in to change notification settings - Fork 1
/
crypto.js
50 lines (39 loc) · 1.32 KB
/
crypto.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
'use strict';
const crypto = require('node:crypto');
function decrypt (text, secret, algorithm = 'aes-256-cbc') {
const key = secret.replace(/-/gu, '').substring(0, 32);
const textParts = text.split(':');
const iv = Buffer.from(textParts.shift(), 'hex');
const encryptedText = Buffer.from(textParts.join(':'), 'hex');
const decipher = crypto.createDecipheriv(algorithm, key, iv);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([ decrypted, decipher.final() ]).toString();
return decrypted;
}
function encrypt (text, secret, algorithm = 'aes-256-cbc', ivLength = 16) {
const key = secret.replace(/-/gu, '').substring(0, 32);
const iv = crypto.randomBytes(ivLength);
const cipher = crypto.createCipheriv(algorithm, key, iv);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([ encrypted, cipher.final() ]);
encrypted = `${ iv.toString('hex') }:${ encrypted.toString('hex') }`;
return encrypted;
}
function hash (input, algorithm, encoding) {
return crypto.createHash(algorithm).
update(typeof input === 'string' ? input : JSON.stringify(input)).
digest(encoding);
}
function sha1 (input) {
return hash(input, 'sha1', 'hex');
}
function sha256 (input) {
return hash(input, 'sha256', 'hex');
}
module.exports = {
decrypt,
encrypt,
hash,
sha1,
sha256,
};