Skip to content

Instantly share code, notes, and snippets.

@nickretallack
Created March 3, 2015 03:49
Show Gist options
  • Save nickretallack/7a130ce0efa44c332eac to your computer and use it in GitHub Desktop.
Save nickretallack/7a130ce0efa44c332eac to your computer and use it in GitHub Desktop.
Converting the WordArray into 8-bit character values.
function to_char(number){
return [
(number & 0xff000000) >>> 24,
(number & 0x00ff0000) >>> 16,
(number & 0x0000ff00) >>> 8,
(number & 0x000000ff) >>> 0
];
}
function to_char_array(wordarray){
result = [];
for (var index in wordarray.words) {
word = wordarray.words[index];
result = result.concat(to_char(word));
}
return result;
}
// bonus: populating Node.JS's Buffer type
function to_buffer(wordarray) {
words = wordarray.words;
buffer = Buffer(wordarray.sigBytes);
for (var x = 0; x < words.length; x++) {
word = to_char(words[x]);
for (var y = 0; y < 4; y ++){
buffer[x*4+y] = word[y];
}
}
return buffer;
}
// test
var wordarray = {
sigBytes: 16,
"words": [
3150126731,
5845652552,
7032008983,
1211931708
]
};
console.log(to_buffer(wordarray));
console.log(to_char_array(wordarray));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment