Пример #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 testSHA512() {
   MAC hmac = HMAC.sha512(ascii(EMPTY_STRING));
   assertArrayEquals(
       hex(
           "b936cee86c9f87aa5d3c6f2e84cb5a4239a5fe50480a6ec66b"
               + "70ab5b1f4ac6730c6c515421b327ec1d69402e53dfb4"
               + "9ad7381eb067b338fd7b0cb22247225d47"),
       hmac.digest(ascii(EMPTY_STRING)));
   hmac = HMAC.sha512(ascii("key"));
   assertArrayEquals(
       hex(
           "b42af09057bac1e2d41708e48a902e09b5ff7f12ab428a4fe8"
               + "6653c73dd248fb82f948a549f7b791a5b41915ee4d1e"
               + "c3935357e4e2317250d0372afa2ebeeb3a"),
       hmac.digest(ascii(PANGRAM)));
 }