syareen syareen - 2 months ago 23
JSON Question

Javascript: SHA512 value convert to JSON object

I already crypt the variable '

getShaValue
' to sha512. Then combine it with many variable like 'name','ic' using
JSON.stringify
. But when I debug my JSON object, the value of SHA512 didn't show the right value. If I only debug the value before convert it into JSON, it show the right value.

Here is my function to crypt the value

self.sha512 = function () {
var value = self.generateSHAvalue();
var getShaValue= CryptoJS.SHA512(value);
return getShaValue;
};


I combine it with many variable

var authToken = SHA.sha512();
var requestData = JSON.stringify({
name: "Test",
authToken: authToken
})
console.log("requestData: " + JSON.stringify(requestData));


The result of the console is

{
"name": "Test",
"authToken": '"$super":{"$super":{}},"words":[1157899753,2720090447,1588426441,2244605341,2288345873,3771352114,2976397435,3171064119,-130018106,2601059156,3822838925,2519334849,1988499628,2785343384,-556559616,-1270654637],"sigBytes":64'
}


But it should be like this:

{
"name": "Test",
"authToken": "21507C7061D3F45058A95751E2FB332DD68F6A2ADC2039DE4341199643E12ADEFB8DF603C3F 34E71FB447F46B82BC5DC7BD2B81B83B389D8950583BEFB424676"
}


Can anyone help me. Thanks.

Answer

You included the binary digest in the json. Try converting it to hex first:

self.sha512 = function () {
  var value = self.generateSHAvalue();
  var shaHex = CryptoJS.SHA512(value).toString(CryptoJS.enc.Hex);
  return shaHex ;
};