-
-
Save getify/7325764 to your computer and use it in GitHub Desktop.
/* | |
wordArray: { words: [..], sigBytes: words.length * 4 } | |
*/ | |
function convertWordArrayToUint8Array(wordArray) { | |
var u32_array = new Uint32Array(wordArray.words); | |
return new Uint8Array(u32_array.buffer); | |
} | |
function convertUint8ArrayToWordArray(u8Array) { | |
var u32_array = new Uint32Array(u8Array.buffer), | |
i, len = u32_array.length, | |
w_array = { sigBytes: len, words: [] } | |
; | |
for (i=0; i<len; i++) { | |
w_array.words.push(u32_array[i]); | |
} | |
return w_array; | |
} | |
function convertUint8ArrayToBinaryString(u8Array) { | |
var i, len = u8Array.length, b_str = ""; | |
for (i=0; i<len; i++) { | |
b_str += String.fromCharCode(u8Array[i]); | |
} | |
return b_str; | |
} | |
function convertBinaryStringToUint8Array(bStr) { | |
var i, len = bStr.length, u8_array = new Uint8Array(len); | |
for (var i = 0; i < len; i++) { | |
u8_array[i] = bStr.charCodeAt(i); | |
} | |
return u8_array; | |
} |
@creationix are you talking about these lines?
Right.
w_array = {
sigBytes: len,
words: Array.prototype.slice.call(u32_array)
};
@creationix thanks, updated!
NOTE: had to update the gist to account for Big-Endianess coming out of CryptoJS lib (in its WordArray
instances). Uint32Array
instances subjected that data to a platform-dependent assumption of endianess, which was possibly unsafe. The above now goes directly from the explicitly (not assumed) BE of the WordArray
to a non-endian Uint8Array
(and vice versa).
convertWordArrayToUint8Array was creating an array that was slightly longer than the original wordarray for me. I replaced -
var len = wordArray.words.length, u8_array = new Uint8Array(len << 2),
with
u8_array = new Uint8Array(wordArray.sigBytes)
and this seems to have corrected the issue.
CryptoJS's WordArray has sigBytes for cases when length of uint8array is not divisible by 4. It should be the length of the original array and signals how much of the last word should be used.
This was also the source of @hygroovy's problem. Funfact: we're discussing this under a three-year-old topic :P
Thanks , Mr. Simpson. these help me a lot when play with msgpack and axios.
@pawelabrams Right, this caused troubles for me and should be changed to sigBytes: len
Big thanks for inspiration!
const formatU8Afrom = Uint8Array.from.bind(Uint8Array);
const formatFromCC = String.fromCharCode.bind(String);
export function stringToUint8Array(str: string) {
return formatU8Afrom(str, (c) => c.charCodeAt(0));
}
export function uint8ArrayToString(u8a: Uint8Array) {
// cf. https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/12713326#12713326
const maxargs = 0x1000;
const strs = [];
for (let i = 0, l = u8a.length; i < l; i += maxargs) {
strs.push(formatFromCC(...u8a.subarray(i, i + maxargs)));
}
return strs.join('');
}
For converting a
Uint32Array
to a normalArray
, you can use the oldArray.prototype.slice.call(arrayLike)
trick.