-
Notifications
You must be signed in to change notification settings - Fork 4
/
cryptionHandler1.js
28 lines (24 loc) · 858 Bytes
/
cryptionHandler1.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
const crypto = require('crypto');
const secret = 'pppppppppppppppppppppppppppppppp';
const Encrypt = (Roomcode) => {
const iv = Buffer.from(crypto.randomBytes(16));
const cipher = crypto.createCipheriv('aes-256-ctr', Buffer.from(secret), iv);
const encryptedRoomcode = Buffer.concat([
cipher.update(Roomcode),
cipher.final(),
]);
return { iv: iv.toString("hex"), roomcode: encryptedRoomcode.toString("hex") };
}
const Decrypt = (encryption) => {
const decipher = crypto.createDecipheriv(
'aes-256-ctr',
Buffer.from(secret),
Buffer.from(encryption.iv, "hex")
);
const decryptedPassword = Buffer.concat([
decipher.update(Buffer.from(encryption.roomcode, "hex")),
decipher.final()
])
return decryptedPassword.toString();
}
module.exports = { Encrypt, Decrypt };