-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneratekey.js
65 lines (51 loc) · 1.92 KB
/
generatekey.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
54
55
56
57
58
59
60
61
62
63
64
65
import fs from 'fs';
import crypto from 'crypto';
function generateAes256CbcKey() {
// Generate a random 32-byte key.
const key = crypto.randomBytes(32);
// Return the key in binary format.
return key.toString("hex");
}
const appKey = generateAes256CbcKey();
// Note to self: this file should generate a key for communication purposes for our apps that key should be stored in an .env file to be hidden from the public
// Function to generate a random initialization vector (IV)
function generateIV() {
return crypto.randomBytes(16); // 16 bytes for AES-256-CBC
}
// Function to perform AES-256-CBC encryption
function encrypt(plaintext, key, iv) {
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(plaintext, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
// Function to perform AES-256-CBC decryption
function decrypt(encryptedMessage, key, iv) {
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
let decrypted = decipher.update(encryptedMessage, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
}
// Example usage
const plaintext = 'Hello, World!';
const key = crypto.randomBytes(32); // 32 bytes for AES-256 (256 bits)
const iv = generateIV();
const encryptedMessage = encrypt(plaintext, key, iv);
console.log('Encrypted Message:', encryptedMessage);
const decryptedMessage = decrypt(encryptedMessage, key, iv);
console.log('Decrypted Message:', decryptedMessage);
function generateEnvFile(key, value, dir1, dir2) {
// Create a new file called `.env` in the current directory.
for(dir of [dir1, dir2]) {
fs.writeFileSync(`${dir}/.env`, "");
console.log(`environment variable ${key}:${value} generated.`);
fs.appendFileSync("dir/.env", `${key}=${value}\n`, {
encoding: "utf-8",
});
}
}
// generateEnvFile('AES_256_CBC_KEY', appKey);
export {
generateAes256CbcKey,
generateEnvFile,
};