RSS LinkedIn Twitter

SQS: Queue Length / Auth Signature

November 10th, 2007 Categories: Amazon Web Services, CS, CS 462, EC2, PHP, Scalability, School, SQS, Virtualization

To get the queue length, as well as the visibility timeout, you make a request using the GetQueueAttributes action. The PHP library I’m using to make my calls to SQS doesn’t support this call (must have been written before the 2007-05-01 release of SQS) so my options are to find a new library, or to write my own function to do this.

I decided to try writing my own first, and while researching this I found something I was looking for while doing lab 4. How to compute the authorization header, or Signature.

The process is as follows, you take the query parameters and concatenate them all end to end (key preceding value). Don’t include the ?, &, or = signs. Then you calculate the HMAC-SHA1 signature of that string (using your secret access key). Then convert it to base64.

Here’s the example Amazon gives on their site.

The following request:

[bash]
?Action=CreateQueue
&QueueName=queue2
&AWSAccessKeyId=0A8BDF2G9KCB3ZNKFA82
&SignatureVersion=1
&Expires=2007-01-12T12:00:00Z
&Version=2006-04-01
[/bash]

translates into the following string:

[bash]ActionCreateQueueAWSAccessKeyId0A8BDF2G9KCB3ZNKFA82Expires2007-01-12T12:00:00ZQueueNamequeue2SignatureVersion1Version2006-04-01[/bash]

which when hashed with the secret key (fake-secret-key, used in this example) yields:

[bash]
wlv84EOcHQk800Yq6QHgX4AdJfk=
(URL encoded version: wlv84EOcHQk800Yq6QHgX4AdJfk%3D)
[/bash]

I looked at my PHP library, and sure enough here are the methods that create the signature. They require the PEAR Crypt_HMAC package.

[php]
function hex2b64($str) {
$raw = ”;
for ($i=0; $i < strlen($str); $i+=2) {
$raw .= chr(hexdec(substr($str, $i, 2)));
}
return base64_encode($raw);
}
[/php]

[php][/php]
function constructSig($str) {
$hasher =& new Crypt_HMAC($this->secretKey, “sha1”);
$signature = $this->hex2b64($hasher->hash($str));
return($signature);
}
[php][/php]

Tags:
No comments yet.

Leave a Comment

*