org.apache.commons.codec.digest.HmacUtils类的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(12.3k)|赞(0)|评价(0)|浏览(1055)

本文整理了Java中org.apache.commons.codec.digest.HmacUtils类的一些代码示例,展示了HmacUtils类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HmacUtils类的具体详情如下:
包路径:org.apache.commons.codec.digest.HmacUtils
类名称:HmacUtils

HmacUtils介绍

[英]Simplifies common javax.crypto.Mac tasks. This class is immutable and thread-safe.

Note: Not all JCE implementations supports all algorithms. If not supported, an IllegalArgumentException is thrown.
[中]简化了普通javax。加密。Mac任务。这个类是不可变的,并且是线程安全的。
注意:并非所有JCE实现都支持所有算法。如果不受支持,将抛出IllegalArgumentException。

代码示例

代码示例来源:origin: commons-codec/commons-codec

/**
 * Creates an instance using the provided algorithm type.
 *
 * @param algorithm to use
 * @param  key the key to use
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 * @since 1.11
 */
public HmacUtils(final String algorithm, final byte[] key) {
  this(getInitializedMac(algorithm, key));
}

代码示例来源:origin: commons-codec/commons-codec

@Test(expected = IllegalArgumentException.class)
public void testSha1HMacFail() throws IOException {
  HmacUtils.hmacSha1((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES);
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testGetHMac() throws IOException {
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
      HmacUtils.getHmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
      HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA256_RESULT_BYTES,
      HmacUtils.getHmacSha256(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA384_RESULT_BYTES,
      HmacUtils.getHmacSha384(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA512_RESULT_BYTES,
      HmacUtils.getHmacSha512(HmacAlgorithmsTest.STANDARD_KEY_BYTES).doFinal(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
}

代码示例来源:origin: commons-codec/commons-codec

/**
 * Returns a HmacSHA512 Message Authentication Code (MAC) as hex string (lowercase) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest (maybe empty or null)
 * @return HmacSHA512 MAC for the given key and value as hex string (lowercase)
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 * @deprecated (1.11) Use {@code new HmacUtils(HmacAlgorithms.HMAC_SHA_512, String).hmacHex(String)}
 */
@Deprecated
public static String hmacSha512Hex(final String key, final String valueToDigest) {
  return new HmacUtils(HmacAlgorithms.HMAC_SHA_512, key).hmacHex(valueToDigest);
}

代码示例来源:origin: commons-codec/commons-codec

@Test(expected = IllegalArgumentException.class)
public void testHmacHexFailString() throws IOException {
  new HmacUtils(hmacAlgorithm, (String) null).hmac(STANDARD_PHRASE_STRING);
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testInitializedMac() throws IOException {
  final Mac md5Mac = HmacUtils.getInitializedMac(HmacAlgorithms.HMAC_MD5, HmacAlgorithmsTest.STANDARD_KEY_BYTES);
  final Mac md5Mac2 = HmacUtils.getInitializedMac("HmacMD5", HmacAlgorithmsTest.STANDARD_KEY_BYTES);
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)
      .doFinal());
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES, HmacUtils.updateHmac(md5Mac2, HmacAlgorithmsTest.STANDARD_PHRASE_STRING)
      .doFinal());
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testHmacSha1UpdateWithInpustream() throws IOException {
  final Mac mac = HmacUtils.getHmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES);
  HmacUtils.updateHmac(mac, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING, Hex.encodeHexString(mac.doFinal()));
  HmacUtils.updateHmac(mac, new ByteArrayInputStream("".getBytes()));
  assertEquals("f42bb0eeb018ebbd4597ae7213711ec60760843f", Hex.encodeHexString(mac.doFinal()));
}

代码示例来源:origin: ibinti/bugvm

/**
 * Returns a HmacSHA512 Message Authentication Code (MAC) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest
 *            <p>
 *            The InputStream must not be null and will not be closed
 *            </p>
 * @return HmacSHA512 MAC for the given key and value
 * @throws IOException
 *             If an I/O error occurs.
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 */
public static byte[] hmacSha512(final byte[] key, final InputStream valueToDigest) throws IOException {
  return updateHmac(getHmacSha512(key), valueToDigest).doFinal();
}

代码示例来源:origin: commons-codec/commons-codec

@Test(expected = IllegalArgumentException.class)
public void testMd5HMacFail() throws IOException {
  HmacUtils.hmacMd5((byte[]) null, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES);
}

代码示例来源:origin: ibinti/bugvm

/**
 * Returns a HmacMD5 Message Authentication Code (MAC) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest
 *            <p>
 *            The InputStream must not be null and will not be closed
 *            </p>
 * @return HmacMD5 MAC for the given key and value
 * @throws IOException
 *             If an I/O error occurs.
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 */
public static byte[] hmacMd5(final byte[] key, final InputStream valueToDigest) throws IOException {
  return updateHmac(getHmacMd5(key), valueToDigest).doFinal();
}

代码示例来源:origin: ibinti/bugvm

/**
 * Returns a HmacSHA384 Message Authentication Code (MAC) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest
 *            <p>
 *            The InputStream must not be null and will not be closed
 *            </p>
 * @return HmacSHA384 MAC for the given key and value
 * @throws IOException
 *             If an I/O error occurs.
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 */
public static byte[] hmacSha384(final byte[] key, final InputStream valueToDigest) throws IOException {
  return updateHmac(getHmacSha384(key), valueToDigest).doFinal();
}

代码示例来源:origin: ibinti/bugvm

/**
   * Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value.
   *
   * @param key
   *            They key for the keyed digest (must not be null)
   * @param valueToDigest
   *            The value (data) which should to digest
   *            <p>
   *            The InputStream must not be null and will not be closed
   *            </p>
   * @return HmacSHA256 MAC for the given key and value
   * @throws IOException
   *             If an I/O error occurs.
s     * @throws IllegalArgumentException
   *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
   */
  public static byte[] hmacSha256(final byte[] key, final InputStream valueToDigest) throws IOException {
    return updateHmac(getHmacSha256(key), valueToDigest).doFinal();
  }

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testSha1HMac() throws IOException {
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
      HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
      HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_BYTES,
      HmacUtils.hmacSha1(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
  Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING,
      HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING,
      HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
  Assert.assertEquals(HmacAlgorithmsTest.STANDARD_SHA1_RESULT_STRING,
      HmacUtils.hmacSha1Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
}

代码示例来源:origin: ibinti/bugvm

/**
 * Returns a HmacSHA512 Message Authentication Code (MAC) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest (maybe empty or null)
 * @return HmacSHA512 MAC for the given key and value
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 */
public static byte[] hmacSha512(final byte[] key, final byte[] valueToDigest) {
  try {
    return getHmacSha512(key).doFinal(valueToDigest);
  } catch (final IllegalStateException e) {
    // cannot happen
    throw new IllegalArgumentException(e);
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test(expected = IllegalArgumentException.class)
public void testEmptyKey() {
  HmacUtils.getHmacMd5(new byte[] {});
}

代码示例来源:origin: com.impetus.fabric/fabric-jdbc-driver-shaded

/**
 * Returns a HmacSHA256 Message Authentication Code (MAC) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest (maybe empty or null)
 * @return HmacSHA256 MAC for the given key and value
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 */
public static byte[] hmacSha256(final byte[] key, final byte[] valueToDigest) {
  try {
    return getHmacSha256(key).doFinal(valueToDigest);
  } catch (final IllegalStateException e) {
    // cannot happen
    throw new IllegalArgumentException(e);
  }
}

代码示例来源:origin: ibinti/bugvm

/**
 * Returns a HmacSHA384 Message Authentication Code (MAC) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest (maybe empty or null)
 * @return HmacSHA384 MAC for the given key and value
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 */
public static byte[] hmacSha384(final byte[] key, final byte[] valueToDigest) {
  try {
    return getHmacSha384(key).doFinal(valueToDigest);
  } catch (final IllegalStateException e) {
    // cannot happen
    throw new IllegalArgumentException(e);
  }
}

代码示例来源:origin: ibinti/bugvm

/**
 * Returns a HmacSHA1 Message Authentication Code (MAC) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest (maybe empty or null)
 * @return HmacSHA1 MAC for the given key and value
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 */
public static byte[] hmacSha1(final byte[] key, final byte[] valueToDigest) {
  try {
    return getHmacSha1(key).doFinal(valueToDigest);
  } catch (final IllegalStateException e) {
    // cannot happen
    throw new IllegalArgumentException(e);
  }
}

代码示例来源:origin: commons-codec/commons-codec

@Test
public void testMd5HMac() throws IOException {
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
      HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
      HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
  Assert.assertArrayEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_BYTES,
      HmacUtils.hmacMd5(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
  Assert.assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING, HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, HmacAlgorithmsTest.STANDARD_PHRASE_BYTES));
  Assert.assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING,
      HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_BYTES, new ByteArrayInputStream(HmacAlgorithmsTest.STANDARD_PHRASE_BYTES)));
  Assert.assertEquals(HmacAlgorithmsTest.STANDARD_MD5_RESULT_STRING,
      HmacUtils.hmacMd5Hex(HmacAlgorithmsTest.STANDARD_KEY_STRING, HmacAlgorithmsTest.STANDARD_PHRASE_STRING));
}

代码示例来源:origin: commons-codec/commons-codec

/**
 * Returns a HmacMD5 Message Authentication Code (MAC) as a hex string (lowercase) for the given key and value.
 *
 * @param key
 *            They key for the keyed digest (must not be null)
 * @param valueToDigest
 *            The value (data) which should to digest (maybe empty or null)
 * @return HmacMD5 MAC for the given key and value as a hex string (lowercase)
 * @throws IllegalArgumentException
 *             when a {@link NoSuchAlgorithmException} is caught or key is null or key is invalid.
 * @deprecated (1.11) Use {@code new HmacUtils(HmacAlgorithms.HMAC_MD5, String).hmacHex(String)}
 */
@Deprecated
public static String hmacMd5Hex(final String key, final String valueToDigest) {
  return new HmacUtils(HmacAlgorithms.HMAC_MD5, key).hmacHex(valueToDigest);
}

相关文章