Ejemplo n.º 1
0
 @Test
 public void testMD2() {
   MAC hmac = HMAC.md2(ascii(EMPTY_STRING));
   assertArrayEquals(hex("6f6e031223b36cd2a997787a03d16bf5"), hmac.digest(ascii(EMPTY_STRING)));
   hmac = HMAC.md2(ascii("key"));
   assertArrayEquals(hex("13758b9534bfb38d850457814613b0c1"), hmac.digest(ascii(PANGRAM)));
 }
Ejemplo n.º 2
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;
 }