Home > OS >  Delphi - Amazon MWS API - How do derive the Base64 HMAC from the SHA 256 HMAC?
Delphi - Amazon MWS API - How do derive the Base64 HMAC from the SHA 256 HMAC?

Time:11-01

Using:

  • Delphi 10.2.3 Tokyo
  • IPWorks SSL, IPWorks Encrypt

I'm writing a Delphi app to get order list from Amazon MWS API.

I've followed their instructions here:

  • Amazon MWS Scratchpad

    CodePudding user response:

    That's because you don't understand the whole point of Base64 at all, including how to write it correctly. Its main purpose is to carry 8bit data (i.e. whole bytes) safely thru 7bit (i.e. ASCII):

    • When encoding 6bits are taken and displayed as one letter.
    • When decoding one letter is taken and 6bit of original data are restored.

    Which is also the reason why encoding inflates the size by 1/3. When sending attachments in emails the former are stored in Base64, because emails are only 7bit safe. Which is the reason why sending a 4 MiB big picture ends up producing an email of at least 5.2 MiB.

    No, it makes no sense to Base64 encode something that is already ASCII and as such 7bit safe. Everybody should be alarmed when someone wants him to Base64 encode the text 9660152e55a7178db9a9521cd80b7f4872f4be2290d1dd0f32205708f2e06589.

    You want to encode bytes, not text. What you see is the hex representation of those bytes. You actually want this:

    var
      input: Array of Byte;
      output: String;
    begin
      SetLength( input, 32 );  // Represented in hexadecimal it would be a text of 64 characters
      input[0]:= $96;
      input[1]:= $60;
      input[2]:= $15;
      ...
      output:= TNetEncoding.Base64.Encode( input );
      if output<> 'lmAVLlWnF425qVIc2At/SHL0viKQ0d0PMiBXCPLgZYk=' then Halt();  // Expected output
    

    It's even more obvious when just trying to decode what you expect and just looking straight at it: once as if it's text, and once which byte values every character actually has (because you'll see it's not text at all). Text is not bytes, and vice versa. That's why Base64 exists. Not just for fun.

  • Related