javax.crypto.Mac.init()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(317)

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

Mac.init介绍

[英]Initializes this Mac instance with the specified key.
[中]

代码示例

代码示例来源:origin: google/guava

  1. private static Mac getMac(String algorithmName, Key key) {
  2. try {
  3. Mac mac = Mac.getInstance(algorithmName);
  4. mac.init(key);
  5. return mac;
  6. } catch (NoSuchAlgorithmException e) {
  7. throw new IllegalStateException(e);
  8. } catch (InvalidKeyException e) {
  9. throw new IllegalArgumentException(e);
  10. }
  11. }

代码示例来源:origin: com.h2database/h2

  1. private static Mac initMac(byte[] key) {
  2. // Java forbids empty keys
  3. if (key.length == 0) {
  4. key = new byte[1];
  5. }
  6. try {
  7. Mac mac = Mac.getInstance("HmacSHA256");
  8. mac.init(new SecretKeySpec(key, "HmacSHA256"));
  9. return mac;
  10. } catch (GeneralSecurityException e) {
  11. throw new RuntimeException(e);
  12. }
  13. }

代码示例来源:origin: apache/kafka

  1. public byte[] hmac(byte[] key, byte[] bytes) throws InvalidKeyException {
  2. mac.init(new SecretKeySpec(key, mac.getAlgorithm()));
  3. return mac.doFinal(bytes);
  4. }

代码示例来源:origin: alibaba/canal

  1. /**
  2. * 使用 HMAC-SHA1 签名方法对对encryptText进行签名
  3. *
  4. * @param encryptText 被签名的字符串
  5. * @param encryptKey 密钥
  6. * @return
  7. * @throws Exception
  8. */
  9. private byte[] HmacSHA1Encrypt(String encryptText, String encryptKey) throws Exception {
  10. byte[] data = encryptKey.getBytes(ENCODING);
  11. // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
  12. SecretKey secretKey = new SecretKeySpec(data, MAC_NAME);
  13. // 生成一个指定 Mac 算法 的 Mac 对象
  14. Mac mac = Mac.getInstance(MAC_NAME);
  15. // 用给定密钥初始化 Mac 对象
  16. mac.init(secretKey);
  17. byte[] text = encryptText.getBytes(ENCODING);
  18. // 完成 Mac 操作
  19. return mac.doFinal(text);
  20. }

代码示例来源:origin: google/guava

  1. public void testEmptyInputs() throws Exception {
  2. String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";
  3. Mac mac = Mac.getInstance("HmacMD5");
  4. mac.init(MD5_KEY);
  5. assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
  6. assertEquals(knownOutput, Hashing.hmacMd5(MD5_KEY).newHasher().hash().toString());
  7. }

代码示例来源:origin: apache/storm

  1. /**
  2. * Compute HMAC of the identifier using the secret key and return the output as password.
  3. *
  4. * @param identifier the bytes of the identifier
  5. * @param key the secret key
  6. * @return the bytes of the generated password
  7. */
  8. static byte[] createPassword(byte[] identifier, SecretKey key) {
  9. Mac mac = threadLocalMac.get();
  10. try {
  11. mac.init(key);
  12. } catch (InvalidKeyException ike) {
  13. throw new IllegalArgumentException("Invalid key to HMAC computation", ike);
  14. }
  15. return mac.doFinal(identifier);
  16. }
  17. }

代码示例来源:origin: resteasy/Resteasy

  1. public static byte[] sign(byte[] data, Algorithm algorithm, SecretKey key)
  2. {
  3. try
  4. {
  5. Mac mac = HMACProvider.getMAC(algorithm);
  6. mac.init(key);
  7. mac.update(data);
  8. return mac.doFinal();
  9. }
  10. catch (Exception e)
  11. {
  12. throw new RuntimeException(e);
  13. }
  14. }
  15. public static boolean verify(JWSInput input, SecretKey key)

代码示例来源:origin: rapidoid/rapidoid

  1. public static byte[] hmac(byte[] data, byte[] secret, byte[] salt) throws Exception {
  2. Mac m = Mac.getInstance(HMAC_SHA_256);
  3. m.init(new SecretKeySpec(secret, HMAC_SHA_256));
  4. return m.doFinal(data);
  5. }

代码示例来源:origin: prestodb/presto

  1. private HashingSource(Source source, ByteString key, String algorithm) {
  2. super(source);
  3. try {
  4. this.mac = Mac.getInstance(algorithm);
  5. this.mac.init(new SecretKeySpec(key.toByteArray(), algorithm));
  6. this.messageDigest = null;
  7. } catch (NoSuchAlgorithmException e) {
  8. throw new AssertionError();
  9. } catch (InvalidKeyException e) {
  10. throw new IllegalArgumentException(e);
  11. }
  12. }

代码示例来源:origin: signalapp/Signal-Server

  1. private byte[] getHmac(byte[] key, byte[] input, Mac mac) {
  2. try {
  3. mac.init(new SecretKeySpec(key, "HmacSHA256"));
  4. return mac.doFinal(input);
  5. } catch (InvalidKeyException e) {
  6. throw new AssertionError(e);
  7. }
  8. }

代码示例来源:origin: prestodb/presto

  1. private static Mac getMac(String algorithmName, Key key) {
  2. try {
  3. Mac mac = Mac.getInstance(algorithmName);
  4. mac.init(key);
  5. return mac;
  6. } catch (NoSuchAlgorithmException e) {
  7. throw new IllegalStateException(e);
  8. } catch (InvalidKeyException e) {
  9. throw new IllegalArgumentException(e);
  10. }
  11. }

代码示例来源:origin: google/guava

  1. public void testEmptyInputs_mixedAlgorithms() throws Exception {
  2. String knownOutput = "8cbf764cbe2e4623d99a41354adfd390";
  3. Mac mac = Mac.getInstance("HmacMD5");
  4. mac.init(SHA1_KEY);
  5. assertEquals(knownOutput, HashCode.fromBytes(mac.doFinal()).toString());
  6. assertEquals(knownOutput, Hashing.hmacMd5(SHA1_KEY).newHasher().hash().toString());
  7. }

代码示例来源:origin: prestodb/presto

  1. private ByteString hmac(String algorithm, ByteString key) {
  2. try {
  3. Mac mac = Mac.getInstance(algorithm);
  4. mac.init(new SecretKeySpec(key.toByteArray(), algorithm));
  5. return ByteString.of(mac.doFinal(data));
  6. } catch (NoSuchAlgorithmException e) {
  7. throw new AssertionError(e);
  8. } catch (InvalidKeyException e) {
  9. throw new IllegalArgumentException(e);
  10. }
  11. }

代码示例来源:origin: prestodb/presto

  1. private HashingSink(Sink sink, ByteString key, String algorithm) {
  2. super(sink);
  3. try {
  4. this.mac = Mac.getInstance(algorithm);
  5. this.mac.init(new SecretKeySpec(key.toByteArray(), algorithm));
  6. this.messageDigest = null;
  7. } catch (NoSuchAlgorithmException e) {
  8. throw new AssertionError();
  9. } catch (InvalidKeyException e) {
  10. throw new IllegalArgumentException(e);
  11. }
  12. }

代码示例来源:origin: stackoverflow.com

  1. import org.apache.commons.codec.binary.Hex;
  2. import javax.crypto.Mac;
  3. import javax.crypto.spec.SecretKeySpec;
  4. public class HashingUtility {
  5. public static String HMAC_MD5_encode(String key, String message) throws Exception {
  6. SecretKeySpec keySpec = new SecretKeySpec(
  7. key.getBytes(),
  8. "HmacMD5");
  9. Mac mac = Mac.getInstance("HmacMD5");
  10. mac.init(keySpec);
  11. byte[] rawHmac = mac.doFinal(message.getBytes());
  12. return Hex.encodeHexString(rawHmac);
  13. }
  14. }

代码示例来源:origin: google/j2objc

  1. private static Mac getMac(String algorithmName, Key key) {
  2. try {
  3. Mac mac = Mac.getInstance(algorithmName);
  4. mac.init(key);
  5. return mac;
  6. } catch (NoSuchAlgorithmException e) {
  7. throw new IllegalStateException(e);
  8. } catch (InvalidKeyException e) {
  9. throw new IllegalArgumentException(e);
  10. }
  11. }

代码示例来源:origin: spring-cloud/spring-cloud-config

  1. private static byte[] hmacSha256(String data, byte[] key) throws Exception {
  2. String algorithm = HMAC_SHA256;
  3. Mac mac = Mac.getInstance(algorithm);
  4. mac.init(new SecretKeySpec(key, algorithm));
  5. return mac.doFinal(data.getBytes(UTF8));
  6. }

代码示例来源:origin: knowm/XChange

  1. public TheRockDigest(String secretKeyStr) {
  2. try {
  3. final SecretKey secretKey = new SecretKeySpec(secretKeyStr.getBytes(), HMAC_SHA_512);
  4. mac = Mac.getInstance(HMAC_SHA_512);
  5. mac.init(secretKey);
  6. } catch (Exception e) {
  7. throw new RuntimeException("Error initializing The Rock Signer", e);
  8. }
  9. }

代码示例来源:origin: stackoverflow.com

  1. public static String encode(String key, String data) throws Exception {
  2. Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
  3. SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
  4. sha256_HMAC.init(secret_key);
  5. return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
  6. }
  7. public static void main(String [] args) throws Exception {
  8. System.out.println(encode("key", "The quick brown fox jumps over the lazy dog"));
  9. }

代码示例来源:origin: jenkinsci/jenkins

  1. /**
  2. * Creates a new {@link Mac} object.
  3. */
  4. public Mac createMac() {
  5. try {
  6. Mac mac = Mac.getInstance(ALGORITHM);
  7. mac.init(getKey());
  8. return mac;
  9. } catch (GeneralSecurityException e) {
  10. // Javadoc says HmacSHA256 must be supported by every Java implementation.
  11. throw new Error(ALGORITHM+" not supported?",e);
  12. }
  13. }

相关文章