Simplify webcrypto type conversion
Previously we'd get a WordArray and convert to string before converting to array buffer. Instead, go directly to array buffer.
This commit is contained in:
@@ -32,7 +32,7 @@
|
||||
return CryptoJS.HmacSHA256(
|
||||
CryptoJS.enc.Latin1.parse(getString(input)),
|
||||
CryptoJS.enc.Latin1.parse(getString(key))
|
||||
).toString(CryptoJS.enc.Latin1);
|
||||
);
|
||||
};
|
||||
|
||||
function encryptAESCBC(plaintext, key, iv) {
|
||||
@@ -43,7 +43,7 @@
|
||||
CryptoJS.enc.Latin1.parse(getString(plaintext)),
|
||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||
).ciphertext.toString(CryptoJS.enc.Latin1);
|
||||
).ciphertext;
|
||||
};
|
||||
|
||||
function decryptAESCBC(ciphertext, key, iv) {
|
||||
@@ -54,7 +54,7 @@
|
||||
btoa(getString(ciphertext)),
|
||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||
).toString(CryptoJS.enc.Latin1);
|
||||
);
|
||||
};
|
||||
|
||||
// utility function for connecting front and back ends via promises
|
||||
@@ -63,7 +63,14 @@
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
args.shift();
|
||||
return new Promise(function(resolve) {
|
||||
resolve(toArrayBuffer(implementation.apply(this, args)));
|
||||
var wordArray = implementation.apply(this, args);
|
||||
// convert 32bit WordArray to array buffer
|
||||
var buffer = new ArrayBuffer(wordArray.sigBytes);
|
||||
var view = new DataView(buffer);
|
||||
for(var i = 0; i*4 < buffer.byteLength; i++) {
|
||||
view.setInt32(i*4, wordArray.words[i]);
|
||||
}
|
||||
resolve(buffer);
|
||||
});
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user