diff options
Began adding browser-native encryptor module
Added new Qunit build process that will browserify the contents of `test/integration/lib` into the QUnit browser, allowing much more modular testing, including unit testing of our modules in our target browsers.
Made a basic unit test file of this form for the new encryptor module, which fails miserably because I've only just begun to work with it.
I've started with this blog post as a starting point, and will be adjusting it to our needs from there:
http://qnimate.com/passphrase-based-encryption-using-web-cryptography-api/
Diffstat (limited to 'app/scripts/lib/encryptor.js')
-rw-r--r-- | app/scripts/lib/encryptor.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/app/scripts/lib/encryptor.js b/app/scripts/lib/encryptor.js new file mode 100644 index 000000000..607825764 --- /dev/null +++ b/app/scripts/lib/encryptor.js @@ -0,0 +1,51 @@ +var vector = global.crypto.getRandomValues(new Uint8Array(16)) +var key = null + +module.exports = { + encrypt, + decrypt, + convertArrayBufferViewtoString, + keyFromPassword, +} + +// Takes a Pojo, returns encrypted text. +function encrypt (password, dataObj) { + var data = JSON.stringify(dataObj) + global.crypto.subtle.encrypt({name: 'AES-CBC', iv: vector}, key, convertStringToArrayBufferView(data)).then(function(result){ + const encryptedData = new Uint8Array(result) + return encryptedData + }, + function(e){ + console.log(e.message) + }) +} + +// Takes encrypted text, returns the restored Pojo. +function decrypt (password, text) { + +} + +function convertStringToArrayBufferView (str) { + var bytes = new Uint8Array(str.length) + for (var i = 0; i < str.length; i++) { + bytes[i] = str.charCodeAt(i) + } + + return bytes +} + +function convertArrayBufferViewtoString (buffer) { + var str = '' + for (var i = 0; i < buffer.byteLength; i++) { + str += String.fromCharCode(buffer[i]) + } + + return str +} + +function keyFromPassword (password) { + global.crypto.subtle.digest({name: 'SHA-256'}, convertStringToArrayBufferView(password)).then(function(result){ + return global.crypto.subtle.importKey('raw', result, {name: 'AES-CBC'}, false, ['encrypt', 'decrypt']) + }) +} + |