I want to know if a string with integer data can be converted to a CryptoJS word array correctly? Example. Can I convert "175950736337895418" into a word array the same way I can create a word array out of 175950736337895418 (int value).
I have some code that converts integer values to word array
// Converts integer to byte array
function getInt64Bytes( x ){
var bytes = [];
for(var i = 7;i>=0;i--){
bytes[i] = x & 0xff;
x = x>>8;
}
return bytes;
}
//converts the byte array to hex string
function bytesToHexStr(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
}
// Main function to convert integer values to word array
function intToWords(counter){
var bytes = getInt64Bytes(counter);
var hexstr = bytesToHexStr(bytes);
var words = CryptoJS.enc.Hex.parse(hexstr);
return words;
}
Even this code doesn't work correctly as very large integer numbers (exceeding javascript limit of numbers 2^53 - 1) get rounded off. Hence I wanted a solution that could take the integer value as string and convert it to a word array correctly.
PS. I need this word array to calculate the HMAC value using the following code
CryptoJS.HmacSHA512(intToWords(counter), CryptoJS.enc.Hex.parse(key))