-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoreValidation.js
More file actions
51 lines (30 loc) · 1.22 KB
/
scoreValidation.js
File metadata and controls
51 lines (30 loc) · 1.22 KB
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
const crypto = require('crypto');
function encryptScore(scoreString){
// Generate a random secret key
const secretKey = crypto.randomBytes(32);
// Generate a random initialization vector
const iv = crypto.randomBytes(16);
// Encrypt the data on the first computer
const cipher = crypto.createCipheriv('aes-256-cbc', secretKey, iv);
let encrypted = cipher.update(scoreString, 'utf8', 'hex');
encrypted += cipher.final('hex');
return [encrypted, secretKey.toString('hex'), iv.toString('hex')];
}
function decryptScore(scoreString, secret, vect){
const decipher = crypto.createDecipheriv('aes-256-cbc',
Buffer.from(secret, 'hex'),
Buffer.from(vect, 'hex'));
let decrypted = decipher.update(scoreString, 'hex', 'utf8');
decrypted += decipher.final('utf8');
// The decrypted data is returned as a string
return decrypted;
}
exports.encryptScore = encryptScore;
exports.decryptScore = decryptScore;
//TESTING
/*
encryptedMessage = encryptScore("Hello-World");
console.log(encryptedMessage);
decryptedMessage = decryptScore(encryptedMessage[0],encryptedMessage[1],encryptedMessage[2]);
console.log(decryptedMessage);
*/