29.07.2020 Views

sqs-dg-2009-02-01

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Amazon Simple Queue Service Developer Guide

Query Request Authentication

/**

* This class defines common routines for generating

* authentication signatures for AWS requests.

*/

public class Signature {

private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

/**

* Computes RFC 2104-compliant HMAC signature.

* * @param data

* The data to be signed.

* @param key

* The signing key.

* @return

* The Base64-encoded RFC 2104-compliant HMAC signature.

* @throws

* java.security.SignatureException when signature generation fails

*/

public static String calculateRFC2104HMAC(String data, String key)

throws java.security.SignatureException

{

String result;

try {

// get an hmac_sha1 key from the raw key bytes

SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),

HMAC_SHA1_ALGORITHM);

// get an hmac_sha1 Mac instance and initialize with the signing key

Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);

mac.init(signingKey);

// compute the hmac on input data bytes

byte[] rawHmac = mac.doFinal(data.getBytes());

// base64-encode the hmac

result = Encoding.EncodeBase64(rawHmac);

} catch (Exception e) {

throw new SignatureException("Failed to generate HMAC : " + e.getMessage());

}

return result;

}

}

Query Request Authentication

You can send Query requests over either HTTP or HTTPS. Regardless of which protocol you use, you

must include a signature in every Query request. This section describes how to create the signature. The

method described in the following procedure is known as signature version 2.

Caution

If you are currently using signature version 1: Version 1 is deprecated, and you should move to

signature version 2 immediately. For information about the deprecation schedule and the

API Version 2009-02-01

23

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!