views:

365

answers:

3

I am wondering if anyone has successfully generated an EC2 v2 signature for there API using php. All examples/libraries I can find online are for v1 of the signature and that has been deprecated due to insecurities. An existing library that implements the signature generation would be appreciated too.

A: 

Here's a PHP Library that supports V2. I haven't tried it though.

Ólafur Waage
A: 

Here's some code I've written and have been using:

define("AWSKEY", "Your AWS Key");
define("AWSSECRET", "Your AWS Secret");
public function get($parameters, $host) {

    // Build out the variables
    $domain = "https://$host/";
    $parameters['AWSAccessKeyId'] = AWSKEY;
    $parameters['Timestamp'] = date('c');
    $parameters['Version'] = '2007-11-07';
    $parameters['SignatureMethod'] = 'HmacSHA256';
    $parameters['SignatureVersion'] = 2;

    // Write the signature
    $signature = "GET\n";
    $signature .= "$host\n";
    $signature .= "/\n";

    $sigparams = $parameters;

    ksort($sigparams);

    $first = true;
    foreach($sigparams as $key=>$param) {
     $signature .= (!$first ? '&' : '') . rawurlencode($key) . '=' . rawurlencode($param);
     $first = false;
    }
    $signature = hash_hmac('sha256', $signature, $AWSKEY, true);
    $signature = base64_encode($signature);
    $parameters['Signature'] = $signature;

    $url = $domain . '?';
    $first = true;
    foreach($parameters as $key=>$param) {
     $url .= (!$first ? '&' : '') . rawurlencode($key) . '=' . rawurlencode($param);
     $first = false;
    }

    $ch = curl_init(trim($url));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($ch);

    return $output;

}

Here's how you'd use it:

$params = array(
    'Action' => 'ListDomains'
);
$db->get($params, 'sdb.amazonaws.com');

This would perform a ListDomains query on SimpleDB. The function itself will return Amazon's output. For more complicated commands, (i.e.: PUT, POST, etc.) there aren't any major modifications that need to be made.

mattbasta
A: 

http://mierendo.com/software/aws%5Fsigned%5Fquery/

I believe that's V2

Frank Farmer