简体   繁体   中英

SHA-1 in Amazon Api Authorization in Javascript

I've been trying to get Authorization for Amazon's s3 rest api going. It's pretty damn complicated.

Because I'm trying to make a simple GET request from an admin page on my website, I'm just trying to do this through Javascript. Here are the instructions for constructing the Signature for the Authorization header:

Signature = Base64( HMAC-SHA1( YourSecretAccessKeyID, UTF-8-Encoding-Of( StringToSign )     ))

To keep us sane, they give us a few examples, with the following givens:

var AWSSecretAccessKey = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY,
    StringToSign = 'GET\n\n\nTue, 27 Mar 2007 19:36:42 +0000\n/johnsmith/photos/puppy.jpg;'

The output for this in their docs is bWq2s1WEIj+Ydj0vQ697zp+IXMU= . Based on the following I am getting ZGVjNzNmNTE0MGU4OWQxYTg3NTg0M2MxZDM5NjIyZDI0MGQxZGY0ZQ== :

function encode_utf8(s) {
  return unescape(encodeURIComponent(s));
}

I used code.google.com's CryptoJS.HmacSHA1 function for the SHA1 hashing. My final Signature function looks like this:

var signature = btoa( CryptoJS.HmacSHA1( aws_secret, encode_utf8( StringToSign) ) );

What is going wrong here???

I actually found the answer from an SO question with reference to google's older (2.0) CrytpoJs library. You need:

Then you create your signature as so:

Signature = btoa( Crypto.HMAC(Crypto.SHA1, encode_utf8(StringToSign), aws_secret, { asString: true }) )

I couldn't find a way to to get Strings instead of Bits in the new version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM