Last active
April 27, 2018 16:38
-
-
Save bluthen/c0fa4d95a77089e61000ee4a36e854e7 to your computer and use it in GitHub Desktop.
Node aes-gcm-stream and Web Crypto API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var aesgcm = require('aes-gcm-stream'); | |
var fs = require('fs'); | |
var encryptToB64 = function (config, message) { | |
return new Promise(function (resolve, reject) { | |
var encrypt = aesgcm.encrypt(config); | |
var buffer = []; | |
encrypt.on('data', function (chunk) { | |
buffer.push(chunk); | |
}); | |
var eData; | |
encrypt.on('end', function () { | |
eData = Buffer.concat(buffer); | |
resolve(eData.toString('base64')); | |
}); | |
encrypt.write(message); | |
encrypt.end(); | |
}); | |
}; | |
// Wait | |
var decryptFromB64 = function (config, encryptedMessage) { | |
return new Promise(function (resolve, reject) { | |
var eData = Buffer.from(encryptedMessage, 'base64'); | |
var decrypt = aesgcm.decrypt(config); | |
var dData; | |
var buffer = []; | |
decrypt.on('data', function (chunk) { | |
buffer.push(chunk); | |
}); | |
decrypt.on('end', function () { | |
resolve(Buffer.concat(buffer)); | |
}); | |
decrypt.write(eData); | |
decrypt.end(); | |
}); | |
}; | |
var config = {key: aesgcm.createKeyBuffer()}; | |
console.log('key:', config.key.toString('base64')); | |
var eMsg = encryptToB64(config, 'Hello World').then(function (eMsg) { | |
console.log('eMsg:', eMsg); | |
return decryptFromB64(config, eMsg); | |
}).then(function (msgBuffer) { | |
console.log('decrypted: ', msgBuffer.toString()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Example of decoding base64 string encrypted with node aes-gcm-stream using webcrypto | |
var b64ToArray = function (base64Data, cb) { | |
var promise = new Promise(function(resolve, reject) { | |
var req = new XMLHttpRequest; | |
req.open('GET', "data:application/octet;base64," + base64Data); | |
req.responseType = 'arraybuffer'; | |
req.onload = function fileLoaded(e) { | |
var byteArray = new Int8Array(e.target.response); | |
resolve(byteArray); | |
} | |
req.send(); | |
}); | |
return promise; | |
} | |
var makeImportKey = function (b64key, cb) { | |
return b64ToArray(b64key).then(function (keyArray) { | |
return window.crypto.subtle.importKey( | |
"raw", | |
keyArray, | |
{ | |
name: "AES-GCM", | |
} | |
, | |
false, | |
["encrypt", "decrypt"] | |
); | |
}); | |
} | |
var decrypt = function (b64Key, b64msg, cb) { | |
var key; | |
return makeImportKey(b64Key).then( | |
function (k) { | |
key = k; | |
return b64ToArray(b64msg); | |
} | |
).then( | |
function (dataArray) { | |
iv = dataArray.slice(0, 12); | |
dataArray = dataArray.slice(12); | |
return window.crypto.subtle.decrypt( | |
{ | |
name: "AES-GCM", | |
iv: iv, | |
}, | |
key, | |
dataArray | |
); | |
} | |
); | |
} | |
//Strings from aes-gcm-stream, see node_aes_gcm_stream_base64.js | |
var b64key = 'RU8FrL/KxRsOEKZ6a/zZLgpF8Cq8s9Ud4fR4rdnzpCw='; | |
var eDataB64 = '8Ldk+zjB6OSr7d72EpQTtsQCKCABI+eA2hjEq8Dhz+o4usB1BNBf'; | |
decrypt(b64key, eDataB64).then(function (dd) { | |
console.log(String.fromCharCode.apply(null, new Uint8Array(dd))); | |
}).catch(function(err) { console.log(err); }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment