2

I am facing problem in encoding in JavaScript, I am not getting expected result here

here are few things that I want to list down - 1) when I am doing encoding in ruby then getting expected result. - 2) but when I am tring to follow same steps in JavaScript then not getting expected result


1) please find below the code of ruby which is working fine.

require 'openssl'
require "base64"
key =  Base64.decode64("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0=")
data = "<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>"
result  = OpenSSL::HMAC.digest('sha256', key , data)
final_result = Base64.encode64(result)

ruby output: received expected output

Key =  "\xC9\x94.\x9E\x05\xC3^\xD9,\e\x86)r]\x1F\xEAj\x11]d\xC2\x1E\x1F\xC2`5b^vol-"
result = "\xC4\x9B\x94\x9C\anQT\xF9';\xE9$\x1C\x98k\xEE)\xD77\xFD\xCA\a\xD1L\xBB\x9B\xD2r\xE9\x1A\xA8"
final_result = "xJuUnAduUVT5JzvpJByYa+4p1zf9ygfRTLub0nLpGqg=\n"

2) please find below the code of JavaScript which is not working as expected

  • Used crypo.js for encoding and decoding

<html>
  <head>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>

    <script>
      key = window.atob("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0=");
      data = "<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>";
      console.log("key");
      console.log(key);
      result = CryptoJS.HmacSHA256(data, key);
      console.log("result");
      console.log(result.toString());
      final_result = CryptoJS.enc.Base64.stringify(result);
      console.log("final_result");
      console.log(final_result.toString());
    </script>
  </head>
  <body>
  </body>
</html>

Javascript output: received expected output

Key =  "É.Ã^Ù,)r]êj]dÂÂ`5b^vol-"
result = "035a028de6bea2c7843b4310b28b57f5193d7597840ea2f23c255cb889d77d60"
final_result = "A1oCjea+oseEO0MQsotX9Rk9dZeEDqLyPCVcuInXfWA="

so here I am not understanding, why i am getting key, result and final_result different in ruby and JavaScript

Please share your experience, it would help me alot

Thanks

2 Answers 2

0

I have tried a lot and find the solution below.

<html>
  <head>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script>
    <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script>
    <script src="jquery.base64.js"></script>
    <script>
      var key = Base64.decode("yZQungXDXtksG4Ypcl0f6moRXWTCHh/CYDViXnZvbC0=");
      console.log(key)
      words = CryptoJS.enc.Latin1.parse(key);
      words = CryptoJS.enc.Hex.parse(words.toString());
     result = CryptoJS.HmacSHA256("<content><app-id>9cd73144-46ca-4a02-852b-fa452b1d8bdc</app-id><hmac>HMACSHA256</hmac><signing-time>2013-11-19T12:06:11.409Z</signing-time></content>", words);
     console.log(result);
     final_result = CryptoJS.enc.Base64.stringify(result);
     console.log(final_result);
   </script>
 </head>
</html>

Now I am getting expected result.

key = "É.Ã^Ù,)r]êj]dÂÂ`5b^vol-"
result = "c49b949c076e5154f9273be9241c986bee29d737fdca07d14cbb9bd272e91aa8"
final_result = "xJuUnAduUVT5JzvpJByYa+4p1zf9ygfRTLub0nLpGqg="
-1

you may need to use Base64.strict_encode64 (http://ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html#method-i-strict_encode64) strict_encode is fully RFC compliant, whereas .encode is not. Most implementations don't care about this specific issue (has to do w/new line characters), but some do.

3
  • My concern is that why I am not getting same 'final_result' of JavaScript as ruby's final result. Can you explain why this discrepancy? Dec 13, 2013 at 6:44
  • Well the outputs aren't identical because ruby is printing the output using hex because not all of the characters are ascii printable. JavaScript apparently doesn't detect that and is printing the output no matter what. However, just by looking, I can tell that the output aren't equivalent, even tho they are being printed differently. If they were equivalent, you would still get the same answer for final_result since base64 encoded data is always ascii printable. Dec 14, 2013 at 0:27
  • The root of the problem is that the Key's are not equivalent. Even though they are being printed differently, you can tell they are likely not the same just by comparing the first character. Hex C9 (\xC9) is not ascii printable (nothing over 7f is). If you treat it as unicode, then the first char would be \xC9\x94 which is a "backwards c" according to this which is not the first char in the js version. My guess is still the same, that you need to use Base64.strict_decode64 when creating the Key Dec 14, 2013 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.