-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
37 lines (32 loc) · 1.18 KB
/
util.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
const readline = require('readline');
const crypto = require('crypto');
function prompt(question) {
return new Promise((resolve) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Ask the question
rl.question(question, (answer) => {
resolve(answer); // Resolve the promise with the answer
rl.close(); // Close the readline interface
});
});
}
// Function to derive an encryption key from the hashed password
function deriveKey(hashedPassword) {
// You can use a unique salt if necessary
const salt = 'pepper#$@I)(U$)U@)$#JFOIJQ$#)IFH#$J(PFIHJ#Q(PFHJ@Q(#FHQJ#$(FH#$(FH#$(QFH#$(F*H#$(*FH(EQFHQOEIWFNWOEQJFQOEFNWEKOFNOEW'; // Use a consistent salt for key derivation
const key = crypto.pbkdf2Sync(hashedPassword, salt, 100000, 32, 'sha512'); // 32 bytes for AES-256
return key;
}
// Function to hash the password using SHA-512
function hashPassword(password) {
return crypto.createHash('sha512').update(password).digest('hex');
}
// Export the functions using module.exports
module.exports = {
prompt,
deriveKey,
hashPassword
};