Calculate the HMAC/SHA1 on a string. @return Signature @throws java.security.NoSuchAlgorithmException If the algorithm does not exist. Unlikely @throws java.security.InvalidKeyException If the key is invalid.
(String awsSecretAccessKey, String canonicalString,
boolean urlencode)
| 1463 | * If the key is invalid. |
| 1464 | */ |
| 1465 | static String encode(String awsSecretAccessKey, String canonicalString, |
| 1466 | boolean urlencode) { |
| 1467 | // The following HMAC/SHA1 code for the signature is taken from the |
| 1468 | // AWS Platform's implementation of RFC2104 (amazon.webservices.common.Signature) |
| 1469 | // |
| 1470 | // Acquire an HMAC/SHA1 from the raw key bytes. |
| 1471 | SecretKeySpec signingKey= |
| 1472 | new SecretKeySpec(awsSecretAccessKey.getBytes(), HMAC_SHA1_ALGORITHM); |
| 1473 | |
| 1474 | // Acquire the MAC instance and initialize with the signing key. |
| 1475 | Mac mac=null; |
| 1476 | try { |
| 1477 | mac=Mac.getInstance(HMAC_SHA1_ALGORITHM); |
| 1478 | } |
| 1479 | catch(NoSuchAlgorithmException e) { |
| 1480 | // should not happen |
| 1481 | throw new RuntimeException("Could not find sha1 algorithm", e); |
| 1482 | } |
| 1483 | try { |
| 1484 | mac.init(signingKey); |
| 1485 | } |
| 1486 | catch(InvalidKeyException e) { |
| 1487 | // also should not happen |
| 1488 | throw new RuntimeException("Could not initialize the MAC algorithm", e); |
| 1489 | } |
| 1490 | |
| 1491 | // Compute the HMAC on the digest, and set it. |
| 1492 | String b64=Base64.encodeBytes(mac.doFinal(canonicalString.getBytes())); |
| 1493 | |
| 1494 | if(urlencode) { |
| 1495 | return urlencode(b64); |
| 1496 | } |
| 1497 | else { |
| 1498 | return b64; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | static Map paramsForListOptions(String prefix, String marker, Integer maxKeys) { |
| 1503 | return paramsForListOptions(prefix, marker, maxKeys, null); |
no test coverage detected