Converting A 64 Bit Number String To Word Array Using Cryptojs
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 c
Solution 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:
functionintToWords(num, lengthInBytes) {
var bigInt = newBigInteger();
bigInt.fromString(num, 10); // radix is 10var hexNum = bigInt.toString(16); // radix is 16if (lengthInBytes && lengthInBytes * 2 >= hexNum.length) {
hexNum = Array(lengthInBytes * 2 - hexNum.length + 1).join("0") + hexNum;
}
returnCryptoJS.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();
<scriptsrc="https://cdn.rawgit.com/jasondavies/jsbn/master/jsbn.js"></script><scriptsrc="https://cdn.rawgit.com/jasondavies/jsbn/master/jsbn2.js"></script><scriptsrc="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/hmac-sha512.js"></script><divid="hexInt"></div><divid="hexIntShort"></div><divid="result"></div>
If you need the result in a specific length, then you can pass the number of required bytes as the second argument.
Post a Comment for "Converting A 64 Bit Number String To Word Array Using Cryptojs"