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:
parent
e190582d9e
commit
1ad898a62e
3 changed files with 23 additions and 10 deletions
|
@ -32,7 +32,7 @@
|
||||||
return CryptoJS.HmacSHA256(
|
return CryptoJS.HmacSHA256(
|
||||||
CryptoJS.enc.Latin1.parse(getString(input)),
|
CryptoJS.enc.Latin1.parse(getString(input)),
|
||||||
CryptoJS.enc.Latin1.parse(getString(key))
|
CryptoJS.enc.Latin1.parse(getString(key))
|
||||||
).toString(CryptoJS.enc.Latin1);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function encryptAESCBC(plaintext, key, iv) {
|
function encryptAESCBC(plaintext, key, iv) {
|
||||||
|
@ -43,7 +43,7 @@
|
||||||
CryptoJS.enc.Latin1.parse(getString(plaintext)),
|
CryptoJS.enc.Latin1.parse(getString(plaintext)),
|
||||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||||
).ciphertext.toString(CryptoJS.enc.Latin1);
|
).ciphertext;
|
||||||
};
|
};
|
||||||
|
|
||||||
function decryptAESCBC(ciphertext, key, iv) {
|
function decryptAESCBC(ciphertext, key, iv) {
|
||||||
|
@ -54,7 +54,7 @@
|
||||||
btoa(getString(ciphertext)),
|
btoa(getString(ciphertext)),
|
||||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||||
).toString(CryptoJS.enc.Latin1);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// utility function for connecting front and back ends via promises
|
// utility function for connecting front and back ends via promises
|
||||||
|
@ -63,7 +63,14 @@
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = Array.prototype.slice.call(arguments);
|
||||||
args.shift();
|
args.shift();
|
||||||
return new Promise(function(resolve) {
|
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);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -117,8 +117,7 @@ function getStringable(thing) {
|
||||||
(thing === Object(thing) &&
|
(thing === Object(thing) &&
|
||||||
(thing.__proto__ == StaticArrayBufferProto ||
|
(thing.__proto__ == StaticArrayBufferProto ||
|
||||||
thing.__proto__ == StaticUint8ArrayProto ||
|
thing.__proto__ == StaticUint8ArrayProto ||
|
||||||
thing.__proto__ == StaticByteBufferProto ||
|
thing.__proto__ == StaticByteBufferProto)));
|
||||||
thing.__proto__ == StaticWordArrayProto)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isEqual(a, b, mayBeShort) {
|
function isEqual(a, b, mayBeShort) {
|
||||||
|
|
|
@ -2578,7 +2578,7 @@ CryptoJS.lib.Cipher || (function (undefined) {
|
||||||
return CryptoJS.HmacSHA256(
|
return CryptoJS.HmacSHA256(
|
||||||
CryptoJS.enc.Latin1.parse(getString(input)),
|
CryptoJS.enc.Latin1.parse(getString(input)),
|
||||||
CryptoJS.enc.Latin1.parse(getString(key))
|
CryptoJS.enc.Latin1.parse(getString(key))
|
||||||
).toString(CryptoJS.enc.Latin1);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
function encryptAESCBC(plaintext, key, iv) {
|
function encryptAESCBC(plaintext, key, iv) {
|
||||||
|
@ -2589,7 +2589,7 @@ CryptoJS.lib.Cipher || (function (undefined) {
|
||||||
CryptoJS.enc.Latin1.parse(getString(plaintext)),
|
CryptoJS.enc.Latin1.parse(getString(plaintext)),
|
||||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||||
).ciphertext.toString(CryptoJS.enc.Latin1);
|
).ciphertext;
|
||||||
};
|
};
|
||||||
|
|
||||||
function decryptAESCBC(ciphertext, key, iv) {
|
function decryptAESCBC(ciphertext, key, iv) {
|
||||||
|
@ -2600,7 +2600,7 @@ CryptoJS.lib.Cipher || (function (undefined) {
|
||||||
btoa(getString(ciphertext)),
|
btoa(getString(ciphertext)),
|
||||||
CryptoJS.enc.Latin1.parse(getString(key)),
|
CryptoJS.enc.Latin1.parse(getString(key)),
|
||||||
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
{ iv: CryptoJS.enc.Latin1.parse(getString(iv)) }
|
||||||
).toString(CryptoJS.enc.Latin1);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
// utility function for connecting front and back ends via promises
|
// utility function for connecting front and back ends via promises
|
||||||
|
@ -2609,7 +2609,14 @@ CryptoJS.lib.Cipher || (function (undefined) {
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = Array.prototype.slice.call(arguments);
|
||||||
args.shift();
|
args.shift();
|
||||||
return new Promise(function(resolve) {
|
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);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue