0

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))
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
grane2212
  • 764
  • 1
  • 10
  • 29

1 Answers1

1

What you want is to parse big numbers from strings. Since this is necessary for RSA, you can use Tom Wu's JSBN to get that functionality. Be sure to include jsbn.js and jsbn2.js. Then you can use it like this:

function intToWords(num, lengthInBytes) {
    var bigInt = new BigInteger();
    bigInt.fromString(num, 10); // radix is 10
    var hexNum = bigInt.toString(16); // radix is 16
    
    if (lengthInBytes && lengthInBytes * 2 >= hexNum.length) {
        hexNum = Array(lengthInBytes * 2 - hexNum.length + 1).join("0") + hexNum;
    }

    return CryptoJS.enc.Hex.parse(hexNum);
}

var num = "175950736337895418";
numWords = intToWords(num);

document.querySelector("#hexInt").innerHTML = "hexNum: " + numWords.toString();
document.querySelector("#hexIntShort").innerHTML = "hexNumShort: " + intToWords("15646513", 8).toString();

var key = CryptoJS.enc.Hex.parse("11223344ff");

document.querySelector("#result").innerHTML = "hexHMAC: " + 
        CryptoJS.HmacSHA512(numWords, key).toString();
<script src="https://cdn.rawgit.com/jasondavies/jsbn/master/jsbn.js"></script>
<script src="https://cdn.rawgit.com/jasondavies/jsbn/master/jsbn2.js"></script>
<script src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/hmac-sha512.js"></script>
<div id="hexInt"></div>
<div id="hexIntShort"></div>
<div id="result"></div>

If you need the result in a specific length, then you can pass the number of required bytes as the second argument.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • Is the code snippet working for you? I don't see any results being displayed! – grane2212 Aug 06 '15 at 15:15
  • Yes, it's working. Add the necessary noscript XSS rules. – Artjom B. Aug 06 '15 at 15:18
  • The code works fine! There is one issue I am trying to figure out. The code that I have creates a 8 byte hex value (with zeros padded in the beginning) but your code gives me the exact hex value. Do you know how could I get the padding in front of the hex value. For example if I pass 15646513 to the intToWords function, I get "eebf31" from your function and I get "0000000000eebf31" from my function. I need to ensure I get the padding since I will be messing up my further calculations. – grane2212 Aug 06 '15 at 17:24
  • I've extended it by utilizing string concatenation of a "0" in hex. – Artjom B. Aug 06 '15 at 18:00
  • If this solved your problem, you may [accept](http://meta.stackexchange.com/q/5234/266187) this answer. Please look at your older question to see if you can accept its answer. Additionally, you can also cast votes on posts if you found them useful (or not). – Artjom B. Aug 06 '15 at 18:04
  • I observed that if the counter value is negative, then the bigInt doesn't convert correctly to the hex value. I was trying to set the counter value as -52 and in that case the BigInt converts it to -34 (which it ideally should be FFFFFFFFFFFFFFCC). Could you help me understand what could be going wrong? – grane2212 Aug 10 '15 at 18:59
  • You can ask another question and this time specify your requirements completely. Make sure to include what you have tried in order to prevent it from being closed as too broad. – Artjom B. Aug 10 '15 at 18:59