Last active
October 26, 2023 04:04
-
-
Save getify/7325764 to your computer and use it in GitHub Desktop.
converting between Uint8Arrays and binary-encoded strings and word-arrays (for crypto purposes, with CryptoJS and NaCL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
wordArray: { words: [..], sigBytes: words.length * 4 } | |
*/ | |
// assumes wordArray is Big-Endian (because it comes from CryptoJS which is all BE) | |
// From: https://gist.github.com/creationix/07856504cf4d5cede5f9#file-encode-js | |
function convertWordArrayToUint8Array(wordArray) { | |
var len = wordArray.words.length, | |
u8_array = new Uint8Array(len << 2), | |
offset = 0, word, i | |
; | |
for (i=0; i<len; i++) { | |
word = wordArray.words[i]; | |
u8_array[offset++] = word >> 24; | |
u8_array[offset++] = (word >> 16) & 0xff; | |
u8_array[offset++] = (word >> 8) & 0xff; | |
u8_array[offset++] = word & 0xff; | |
} | |
return u8_array; | |
} | |
// create a wordArray that is Big-Endian (because it's used with CryptoJS which is all BE) | |
// From: https://gist.github.com/creationix/07856504cf4d5cede5f9#file-encode-js | |
function convertUint8ArrayToWordArray(u8Array) { | |
var words = [], i = 0, len = u8Array.length; | |
while (i < len) { | |
words.push( | |
(u8Array[i++] << 24) | | |
(u8Array[i++] << 16) | | |
(u8Array[i++] << 8) | | |
(u8Array[i++]) | |
); | |
} | |
return { | |
sigBytes: words.length * 4, | |
words: words | |
}; | |
} | |
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; | |
} |
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('');
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Big thanks for inspiration!