Fix for UTF-8/binary handling in hashing operations. Added tests to prevent future breakages. Closes #249.

This commit is contained in:
n1474335 2018-03-04 17:39:53 +00:00
parent 8360c9e9f9
commit f47a408755
4 changed files with 339 additions and 69 deletions

View file

@ -495,15 +495,16 @@ const Utils = {
* Converts an ArrayBuffer to a string.
*
* @param {ArrayBuffer} arrayBuffer
* @param {boolean} [utf8=true] - Whether to attempt to decode the buffer as UTF-8
* @returns {string}
*
* @example
* // returns "hello"
* Utils.arrayBufferToStr(Uint8Array.from([104,101,108,108,111]).buffer);
*/
arrayBufferToStr: function(arrayBuffer) {
arrayBufferToStr: function(arrayBuffer, utf8=true) {
const byteArray = Array.prototype.slice.call(new Uint8Array(arrayBuffer));
return Utils.byteArrayToUtf8(byteArray);
return utf8 ? Utils.byteArrayToUtf8(byteArray) : Utils.byteArrayToChars(byteArray);
},