SecretKeySpec signingKey = null;
try {
signingKey = new SecretKeySpec(awsSecretKey.getBytes(Constants.DEFAULT_ENCODING),
Constants.HMAC_SHA1_ALGORITHM);
} catch (UnsupportedEncodingException e) {
throw new S3ServiceException("Unable to get bytes from secret string", e);
}
// Acquire the MAC instance and initialize with the signing key.
Mac mac = null;
try {
mac = Mac.getInstance(Constants.HMAC_SHA1_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
// should not happen
throw new RuntimeException("Could not find sha1 algorithm", e);
}
try {
mac.init(signingKey);
} catch (InvalidKeyException e) {
// also should not happen
throw new RuntimeException("Could not initialize the MAC algorithm", e);
}
// Compute the HMAC on the digest, and set it.
try {
byte[] b64 = Base64.encodeBase64(mac.doFinal(
canonicalString.getBytes(Constants.DEFAULT_ENCODING)));
return new String(b64);
} catch (UnsupportedEncodingException e) {
throw new S3ServiceException("Unable to get bytes from canonical string", e);
}
}