Trying to sort out encodings to/from Uint8Array, not working
This commit is contained in:
parent
aca2d80104
commit
82f4648ef5
|
@ -9,36 +9,30 @@ define(function(require) {
|
||||||
|
|
||||||
// Move back and forth from Uint8Arrays and CryptoJS' WordArray
|
// Move back and forth from Uint8Arrays and CryptoJS' WordArray
|
||||||
// source: https://groups.google.com/forum/#!topic/crypto-js/TOb92tcJlU0
|
// source: https://groups.google.com/forum/#!topic/crypto-js/TOb92tcJlU0
|
||||||
Uint8ArrayFormatter = {
|
var WordArray = CryptoJS.lib.WordArray;
|
||||||
fromWordArray: function (wordArray) {
|
function fromWordArray(wordArray) {
|
||||||
// Shortcuts
|
|
||||||
var words = wordArray.words;
|
var words = wordArray.words;
|
||||||
var sigBytes = wordArray.sigBytes;
|
var sigBytes = wordArray.sigBytes;
|
||||||
|
|
||||||
// Convert
|
|
||||||
var u8 = new Uint8Array(sigBytes);
|
var u8 = new Uint8Array(sigBytes);
|
||||||
for (var i = 0; i < sigBytes; i++) {
|
for (var i = 0; i < sigBytes; i++) {
|
||||||
var byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
var byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
||||||
u8[i]=byte;
|
u8[i]=byte;
|
||||||
}
|
}
|
||||||
console.log("stringify", wordArray, u8);
|
|
||||||
return u8;
|
return u8;
|
||||||
},
|
}
|
||||||
|
function toWordArray(u8arr) {
|
||||||
toWordArray: function (u8arr) {
|
|
||||||
// Shortcut
|
|
||||||
var len = u8arr.length;
|
var len = u8arr.length;
|
||||||
|
|
||||||
// Convert
|
|
||||||
var words = [];
|
var words = [];
|
||||||
for (var i = 0; i < len; i++) {
|
for (var i = 0; i < len; i++) {
|
||||||
words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8);
|
words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8);
|
||||||
}
|
}
|
||||||
console.log("parse", u8arr, CryptoJS.lib.WordArray.create(words, len));
|
return WordArray.create(words, len);
|
||||||
return CryptoJS.lib.WordArray.create(words, len);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
CryptoJS.enc.Uint8Array = {
|
||||||
|
stringify: fromWordArray,
|
||||||
|
parse: toWordArray
|
||||||
|
};
|
||||||
|
|
||||||
function CryptoContext(context, encrypt, decrypt) {
|
function CryptoContext(context, encrypt, decrypt) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
@ -56,7 +50,7 @@ console.log("parse", u8arr, CryptoJS.lib.WordArray.create(words, len));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(value) {
|
if(value) {
|
||||||
value = Uint8ArrayFormatter.fromWordArray(decrypt(value));
|
value = decrypt(value);
|
||||||
}
|
}
|
||||||
callback(null, value);
|
callback(null, value);
|
||||||
});
|
});
|
||||||
|
@ -75,15 +69,30 @@ console.log("parse", u8arr, CryptoJS.lib.WordArray.create(words, len));
|
||||||
// prompting the user to enter it when the file system is being opened.
|
// prompting the user to enter it when the file system is being opened.
|
||||||
function CryptoAdapter(passphrase, provider) {
|
function CryptoAdapter(passphrase, provider) {
|
||||||
this.provider = provider;
|
this.provider = provider;
|
||||||
this.encrypt = function(plain) {
|
this.encrypt = function(buffer) {
|
||||||
return CryptoJS[encryptionType]
|
var wordArray = toWordArray(buffer);
|
||||||
.encrypt(Uint8ArrayFormatter.toWordArray(plain), passphrase)
|
// return CryptoJS[encryptionType]
|
||||||
.toString();
|
// .encrypt(wordArray, passphrase)
|
||||||
|
// .toString(CryptoJS.enc.Uint8Array);
|
||||||
|
|
||||||
|
var e = CryptoJS[encryptionType].encrypt(wordArray, passphrase);
|
||||||
|
var e2 = e.ciphertext.toString(CryptoJS.enc.Uint8Array);
|
||||||
|
console.log("encrypt", e, e2);
|
||||||
|
return e2;
|
||||||
};
|
};
|
||||||
this.decrypt = function(encrypted) {
|
this.decrypt = function(encrypted) {
|
||||||
return CryptoJS[encryptionType]
|
debugger;
|
||||||
.decrypt(encrypted, passphrase)
|
var wordArray = toWordArray(encrypted);
|
||||||
.toString();
|
// return CryptoJS[encryptionType]
|
||||||
|
// .decrypt(wordArray, passphrase)
|
||||||
|
// .toString(CryptoJS.enc.Uint8Array);
|
||||||
|
|
||||||
|
// var cipherParams = CryptoJS.lib.CipherParams.create({
|
||||||
|
// ciphertext: CryptoJS.enc.Base64.parse(jsonObj.ct)
|
||||||
|
// });
|
||||||
|
|
||||||
|
var result = CryptoJS[encryptionType].decrypt({ciphertext:wordArray}, passphrase);
|
||||||
|
return result.toString(CryptoJS.enc.Uint8Array);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
CryptoAdapter.isSupported = function() {
|
CryptoAdapter.isSupported = function() {
|
||||||
|
|
Loading…
Reference in New Issue