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:


translates into the following string:


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

(URL encoded version: wlv84EOcHQk800Yq6QHgX4AdJfk%3D)

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

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

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

No comments yet.

Leave a Comment