Trying to fix encoding issue in crypto.js, not working yet

This commit is contained in:
David Humphrey 2013-12-03 15:23:47 -05:00
parent a3a1c50b4e
commit aca2d80104
1 changed files with 36 additions and 35 deletions

View File

@ -7,6 +7,38 @@ define(function(require) {
// Rabbit, see http://code.google.com/p/crypto-js/#Rabbit // Rabbit, see http://code.google.com/p/crypto-js/#Rabbit
require("crypto-js/rollups/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) { function CryptoContext(context, encrypt, decrypt) {
this.context = context; this.context = context;
@ -24,7 +56,7 @@ define(function(require) {
return; return;
} }
if(value) { if(value) {
value = decrypt(value); value = Uint8ArrayFormatter.fromWordArray(decrypt(value));
} }
callback(null, value); callback(null, value);
}); });
@ -37,37 +69,6 @@ define(function(require) {
this.context.delete(key, callback); 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) { function buildCryptoAdapter(encryptionType) {
// It is up to the app using this wrapper how the passphrase is acquired, probably by // 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.provider = provider;
this.encrypt = function(plain) { this.encrypt = function(plain) {
return CryptoJS[encryptionType] return CryptoJS[encryptionType]
.encrypt(plain, passphrase, {format: Uint8ArrayFormatter}) .encrypt(Uint8ArrayFormatter.toWordArray(plain), passphrase)
.toString(); .toString();
}; };
this.decrypt = function(encrypted) { this.decrypt = function(encrypted) {
return CryptoJS[encryptionType] return CryptoJS[encryptionType]
.decrypt(encrypted, passphrase, {format: Uint8ArrayFormatter}) .decrypt(encrypted, passphrase)
.toString(); //CryptoJS.enc.Utf8); .toString();
}; };
} }
CryptoAdapter.isSupported = function() { CryptoAdapter.isSupported = function() {