Here is my dilemma. I'm familiar with reading Java code, but no good at writing it. I have several examples from Amazon documentation, but I'm doing something wrong. I'm using Eclipse. I have the AWS Java SDK, Apache Commons Codec & Logging, and Base64. I have all the code in the proper classpaths inside my project.
I understand how to form the request (the order of elements, timestamp, etc.), but I don't know how to send this info to the java code to create the signed request. So I'll start with the code I am using from the documentation.
Code for Signature:
import java.security.SignatureException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
/**
* 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;
}
}
Code for Encoding:
/**
* This class defines common routines for encoding * data in AWS requests.
*/
public class Encoding {
/**
* Performs base64-encoding of input bytes.
*
* @param rawData * Array of bytes to be encoded.
* @return * The base64 encoded string representation of rawData.
*/
public static String EncodeBase64(byte[] rawData) {
return Base64.encodeBytes(rawData);
}
}
Code I came up with to sign request:
import java.security.SignatureException;
public class SignatureTest {
/**
* @param args
* @throws SignatureException
*/
public static void main( String[] args ) throws SignatureException {
// data is the URL parameters and time stamp to be encoded
String data = "<data-to-encode>";
// key is the secret key
String key = "<my-secret-key>";
Signature.calculateRFC2104HMAC(data, key);
}
}
At first, I was getting errors related to classpath, so I added those to my project. Now when I run the code, I get no errors and no response, I just return to a command prompt. I know this is probably a simple solution. I've spent a week trying to figure this out and have had no success finding any answers. Can someone point me in the right direction?
Note: I didn't include the Base64 code here because of the size, but I do have that in my project.
The line in my code:
Signature.calculateRFC2104HMAC(data, key);
is missing a print statement to display the results. Changing it to
System.out.println(Signature.calculateRFC2104HMAC(data, key));
gives me the result I was expecting.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.