1

Fiddle here:

var canonicalString = 'GET'+'\n'
  +'/?a=&b=c'+'\n'
  +'application/json'+'\n'
  +'\n'
  +'Mon, 26 Mar 2007 19:37:58 +0000';

console.log(canonicalString);
canonicalString = CryptoJS.HmacSHA256(canonicalString, 'bar');

console.log(CryptoJS.enc.Base64.stringify(canonicalString));
console.log(btoa(canonicalString.toString()));

I can't understand why the results are different. Where I'm wrong? By the way, the right one should be the btoa() version.

gremo
  • 47,186
  • 75
  • 257
  • 421

1 Answers1

2

Using a Base64 online decoder we can see the input was in different encodings:

7Sbw7FL9uyWDsrrW+Hi9sjdWoOtHT52Fqp9gfViFW1E=
ED26F0EC52FDBB2583B2BAD6F878BDB23756A0EB474F9D85AA9F607D58855B51

ZWQyNmYwZWM1MmZkYmIyNTgzYjJiYWQ2Zjg3OGJkYjIzNzU2YTBlYjQ3NGY5ZDg1YWE5ZjYwN2Q1ODg1NWI1MQ==
65643236663065633532666462623235383362326261643666383738626462323337353661306562343734663964383561613966363037643538383535623531

In the first case the initial original character was "E"
In the second case the initial original characters were "65"

The ASCII character "E" is "65" in hexadecimal encoding.

This the first case encoded a character representation while the decomnd case encoded a hexadecimal representations. Different Base64 implementations handle input encodings differently.

Missing from the question are the code examples, there needs to be a Minimal, Complete, and Verifiable example.

Community
  • 1
  • 1
zaph
  • 111,848
  • 21
  • 189
  • 228