Skip to content

Instantly share code, notes, and snippets.

@getify
Last active October 26, 2023 04:04

Revisions

  1. getify revised this gist Nov 6, 2013. 1 changed file with 29 additions and 5 deletions.
    34 changes: 29 additions & 5 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -2,16 +2,40 @@
    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 u32_array = new Uint32Array(wordArray.words);
    return new Uint8Array(u32_array.buffer);
    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 u32_array = new Uint32Array(u8Array.buffer);
    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: u32_array.length * 4,
    words: Array.prototype.slice.call(u32_array)
    sigBytes: words.length * 4,
    words: words
    };
    }

  2. getify revised this gist Nov 5, 2013. 1 changed file with 5 additions and 8 deletions.
    13 changes: 5 additions & 8 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -8,14 +8,11 @@ function convertWordArrayToUint8Array(wordArray) {
    }

    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;
    var u32_array = new Uint32Array(u8Array.buffer);
    return {
    sigBytes: u32_array.length * 4,
    words: Array.prototype.slice.call(u32_array)
    };
    }

    function convertUint8ArrayToBinaryString(u8Array) {
  3. getify created this gist Nov 5, 2013.
    35 changes: 35 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    /*
    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;
    }