23.06.2023 Views

AWS

4 weeks subscription

4 weeks subscription

SHOW MORE
SHOW LESS

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<br />

Query Request Authentication<br />

/**<br />

* This class defines common routines for generating<br />

* authentication signatures for <strong>AWS</strong> requests.<br />

*/<br />

public class Signature {<br />

private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";<br />

/**<br />

* Computes RFC 2104-compliant HMAC signature.<br />

* * @param data<br />

* The data to be signed.<br />

* @param key<br />

* The signing key.<br />

* @return<br />

* The Base64-encoded RFC 2104-compliant HMAC signature.<br />

* @throws<br />

* java.security.SignatureException when signature generation fails<br />

*/<br />

public static String calculateRFC2104HMAC(String data, String key)<br />

throws java.security.SignatureException<br />

{<br />

String result;<br />

try {<br />

// get an hmac_sha1 key from the raw key bytes<br />

SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),<br />

HMAC_SHA1_ALGORITHM);<br />

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

Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);<br />

mac.init(signingKey);<br />

// compute the hmac on input data bytes<br />

byte[] rawHmac = mac.doFinal(data.getBytes());<br />

// base64-encode the hmac<br />

result = Encoding.EncodeBase64(rawHmac);<br />

} catch (Exception e) {<br />

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

}<br />

return result;<br />

}<br />

}<br />

Query Request Authentication<br />

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

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

method described in the following procedure is known as signature version 2.<br />

Caution<br />

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

signature version 2 immediately. For information about the deprecation schedule and the<br />

API Version 2009-02-01<br />

23

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

Saved successfully!

Ooh no, something went wrong!