-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathChacha20Poly1305StringEncryptionNodeJsCrypto.js
80 lines (68 loc) · 2.74 KB
/
Chacha20Poly1305StringEncryptionNodeJsCrypto.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var crypto = require('crypto');
console.log('ChaCha20-Poly1305 String encryption with random key full');
var plaintext = 'The quick brown fox jumps over the lazy dog';
console.log('plaintext: ', plaintext);
// generate random key
var encryptionKey = generateRandomAesKey();
var encryptionKeyBase64 = base64Encoding(encryptionKey);
console.log('encryptionKey (Base64): ', encryptionKeyBase64);
console.log('\n* * * Encryption * * *');
var ciphertextBase64 = chacha20Poly1305EncryptToBase64(encryptionKey, plaintext);
console.log('ciphertext (Base64): ' + ciphertextBase64);
console.log('output is (Base64) nonce : (Base64) ciphertext : (Base64) gcmTag');
console.log('\n* * * Decryption * * *');
var decryptionKeyBase64 = encryptionKeyBase64;
var ciphertextDecryptionBase64 = ciphertextBase64;
console.log('decryptionKey (Base64): ', decryptionKeyBase64);
console.log('ciphertext (Base64): ', ciphertextDecryptionBase64);
console.log('input is (Base64) nonce : (Base64) ciphertext : (Base64) gcmTag');
var decryptedtext = chacha20Poly1305DecryptFromBase64(encryptionKey, ciphertextBase64);
console.log('plaintext: ', decryptedtext);
function chacha20Poly1305EncryptToBase64(key, data) {
var nonce = generateRandomNonce();
const cipher = crypto.createCipheriv('chacha20-poly1305', key, nonce, { authTagLength: 16});
let encryptedBase64 = '';
cipher.setEncoding('base64');
cipher.on('data', (chunk) => encryptedBase64 += chunk);
cipher.on('end', () => {
// do nothing console.log(encryptedBase64);
// Prints: some clear text data
});
cipher.write(data);
cipher.end();
var nonceBase64 = base64Encoding(nonce);
var poly1305TagBase64 = base64Encoding(cipher.getAuthTag());
return nonceBase64 + ':' + encryptedBase64 + ':' + poly1305TagBase64;
}
function chacha20Poly1305DecryptFromBase64(key, data) {
var dataSplit = data.split(":");
var nonce = base64Decoding(dataSplit[0]);
var ciphertext = dataSplit[1];
var poly1305Tag = base64Decoding(dataSplit[2]);
const decipher = crypto.createDecipheriv('chacha20-poly1305', key, nonce, { authTagLength: 16});
decipher.setAuthTag(poly1305Tag);
let decrypted = '';
decipher.on('readable', () => {
while (null !== (chunk = decipher.read())) {
decrypted += chunk.toString('utf8');
}
});
decipher.on('end', () => {
// do nothing console.log(decrypted);
});
decipher.write(ciphertext, 'base64');
decipher.end();
return decrypted;
}
function generateRandomAesKey() {
return crypto.randomBytes(32);
}
function generateRandomNonce() {
return crypto.randomBytes(12);
}
function base64Encoding(input) {
return input.toString('base64');
}
function base64Decoding(input) {
return Buffer.from(input, 'base64')
}