From aca2d8010461b3ddea393430188b3eda0cc13e7b Mon Sep 17 00:00:00 2001 From: David Humphrey Date: Tue, 3 Dec 2013 15:23:47 -0500 Subject: [PATCH] Trying to fix encoding issue in crypto.js, not working yet --- src/adapters/crypto.js | 71 +++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/src/adapters/crypto.js b/src/adapters/crypto.js index 71c5ded..291f849 100644 --- a/src/adapters/crypto.js +++ b/src/adapters/crypto.js @@ -7,6 +7,38 @@ define(function(require) { // Rabbit, see http://code.google.com/p/crypto-js/#Rabbit require("crypto-js/rollups/rabbit"); + // Move back and forth from Uint8Arrays and CryptoJS' WordArray + // source: https://groups.google.com/forum/#!topic/crypto-js/TOb92tcJlU0 + Uint8ArrayFormatter = { + fromWordArray: function (wordArray) { + // Shortcuts + var words = wordArray.words; + var sigBytes = wordArray.sigBytes; + + // Convert + var u8 = new Uint8Array(sigBytes); + for (var i = 0; i < sigBytes; i++) { + var byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; + u8[i]=byte; + } +console.log("stringify", wordArray, u8); + return u8; + }, + + toWordArray: function (u8arr) { + // Shortcut + var len = u8arr.length; + + // Convert + var words = []; + for (var i = 0; i < len; i++) { + words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8); + } +console.log("parse", u8arr, CryptoJS.lib.WordArray.create(words, len)); + return CryptoJS.lib.WordArray.create(words, len); + } + }; + function CryptoContext(context, encrypt, decrypt) { this.context = context; @@ -24,7 +56,7 @@ define(function(require) { return; } if(value) { - value = decrypt(value); + value = Uint8ArrayFormatter.fromWordArray(decrypt(value)); } callback(null, value); }); @@ -37,37 +69,6 @@ define(function(require) { this.context.delete(key, callback); }; - // Move back and forth from Uint8Arrays and CryptoJS' WordArray - // source: https://groups.google.com/forum/#!topic/crypto-js/TOb92tcJlU0 - Uint8ArrayFormatter = { - stringify: function (wordArray) { - // Shortcuts - var words = wordArray.words; - var sigBytes = wordArray.sigBytes; - - // Convert - var u8 = new Uint8Array(sigBytes); - for (var i = 0; i < sigBytes; i++) { - var byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; - u8[i]=byte; - } - - return u8; - }, - - parse: function (u8arr) { - // Shortcut - var len = u8arr.length; - - // Convert - var words = []; - for (var i = 0; i < len; i++) { - words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8); - } - - return CryptoJS.lib.WordArray.create(words, len); - } - }; function buildCryptoAdapter(encryptionType) { // It is up to the app using this wrapper how the passphrase is acquired, probably by @@ -76,13 +77,13 @@ define(function(require) { this.provider = provider; this.encrypt = function(plain) { return CryptoJS[encryptionType] - .encrypt(plain, passphrase, {format: Uint8ArrayFormatter}) + .encrypt(Uint8ArrayFormatter.toWordArray(plain), passphrase) .toString(); }; this.decrypt = function(encrypted) { return CryptoJS[encryptionType] - .decrypt(encrypted, passphrase, {format: Uint8ArrayFormatter}) - .toString(); //CryptoJS.enc.Utf8); + .decrypt(encrypted, passphrase) + .toString(); }; } CryptoAdapter.isSupported = function() {