Пример #1
0
 /**
  * Creates and returns a {@link MAC} instance corresponding to the given algorithm, initialized
  * with the provided secret key.
  *
  * @param algorithm the MAC algorithm.
  * @param key the secret key.
  * @return the created {@link MAC} instance.
  * @throws NullPointerException if {@code algorithm} is {@code null}.
  * @throws IllegalArgumentException if the given algorithm is unknown.
  */
 static MAC newMAC(Algorithm<MAC> algorithm, byte[] key) {
   Parameters.checkNotNull(algorithm);
   MAC mac;
   if (algorithm == Algorithm.HMAC_MD2) {
     mac = HMAC.md2(key);
   } else if (algorithm == Algorithm.HMAC_MD4) {
     mac = HMAC.md4(key);
   } else if (algorithm == Algorithm.HMAC_MD5) {
     mac = HMAC.md5(key);
   } else if (algorithm == Algorithm.HMAC_SHA1) {
     mac = HMAC.sha1(key);
   } else if (algorithm == Algorithm.HMAC_SHA256) {
     mac = HMAC.sha256(key);
   } else if (algorithm == Algorithm.HMAC_SHA512) {
     mac = HMAC.sha512(key);
   } else if (algorithm == Algorithm.HMAC_KECCAK224) {
     mac = HMAC.keccak224(key);
   } else if (algorithm == Algorithm.HMAC_KECCAK256) {
     mac = HMAC.keccak256(key);
   } else if (algorithm == Algorithm.HMAC_KECCAK384) {
     mac = HMAC.keccak384(key);
   } else if (algorithm == Algorithm.HMAC_KECCAK512) {
     mac = HMAC.keccak512(key);
   } else {
     throw new IllegalArgumentException("Unknown algorithm");
   }
   return mac;
 }
Пример #2
0
 @Test
 public void testKeccak512() {
   MAC hmac = HMAC.keccak512(ascii(EMPTY_STRING));
   assertArrayEquals(
       hex(
           "a857c9f1cd9cb25c2f24a933618abfc724d4eaebf74099dddc"
               + "5e0a1c7ab8de3865faa1fb039419de768e962bdc81a6"
               + "d85b9809d6ab84f8cc984f5d6b90f23c15"),
       hmac.digest(ascii(EMPTY_STRING)));
   hmac = HMAC.keccak512(ascii("key"));
   assertArrayEquals(
       hex(
           "22fb03b3391bc0adfc73c18e0919d9f142390e81d6cc268971"
               + "6ac53ab75458a718059d58cfbb23c6a416c32b8afa84"
               + "a9a7a9d852312a743bef0a55148e5a1b8a"),
       hmac.digest(ascii(PANGRAM)));
 }