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

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

本文整理了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

private static Mac getMac(String algorithmName, Key key) {
 try {
  Mac mac = Mac.getInstance(algorithmName);
  mac.init(key);
  return mac;
 } catch (NoSuchAlgorithmException e) {
  throw new IllegalStateException(e);
 } catch (InvalidKeyException e) {
  throw new IllegalArgumentException(e);
 }
}

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

private static Mac initMac(byte[] key) {
  // Java forbids empty keys
  if (key.length == 0) {
    key = new byte[1];
  }
  try {
    Mac mac = Mac.getInstance("HmacSHA256");
    mac.init(new SecretKeySpec(key, "HmacSHA256"));
    return mac;
  } catch (GeneralSecurityException e) {
    throw new RuntimeException(e);
  }
}

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

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

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

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

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

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

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

/**
   * Compute HMAC of the identifier using the secret key and return the output as password.
   *
   * @param identifier the bytes of the identifier
   * @param key        the secret key
   * @return the bytes of the generated password
   */
  static byte[] createPassword(byte[] identifier, SecretKey key) {
    Mac mac = threadLocalMac.get();
    try {
      mac.init(key);
    } catch (InvalidKeyException ike) {
      throw new IllegalArgumentException("Invalid key to HMAC computation", ike);
    }
    return mac.doFinal(identifier);
  }
}

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

public static byte[] sign(byte[] data, Algorithm algorithm, SecretKey key)
{
 try
 {
   Mac mac = HMACProvider.getMAC(algorithm);
   mac.init(key);
   mac.update(data);
   return mac.doFinal();
 }
 catch (Exception e)
 {
   throw new RuntimeException(e);
 }
}
public static boolean verify(JWSInput input, SecretKey key)

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

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

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

private HashingSource(Source source, ByteString key, String algorithm) {
 super(source);
 try {
  this.mac = Mac.getInstance(algorithm);
  this.mac.init(new SecretKeySpec(key.toByteArray(), algorithm));
  this.messageDigest = null;
 } catch (NoSuchAlgorithmException e) {
  throw new AssertionError();
 } catch (InvalidKeyException e) {
  throw new IllegalArgumentException(e);
 }
}

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

private byte[] getHmac(byte[] key, byte[] input, Mac mac) {
 try {
  mac.init(new SecretKeySpec(key, "HmacSHA256"));
  return mac.doFinal(input);
 } catch (InvalidKeyException e) {
  throw new AssertionError(e);
 }
}

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

private static Mac getMac(String algorithmName, Key key) {
 try {
  Mac mac = Mac.getInstance(algorithmName);
  mac.init(key);
  return mac;
 } catch (NoSuchAlgorithmException e) {
  throw new IllegalStateException(e);
 } catch (InvalidKeyException e) {
  throw new IllegalArgumentException(e);
 }
}

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

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

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

private ByteString hmac(String algorithm, ByteString key) {
 try {
  Mac mac = Mac.getInstance(algorithm);
  mac.init(new SecretKeySpec(key.toByteArray(), algorithm));
  return ByteString.of(mac.doFinal(data));
 } catch (NoSuchAlgorithmException e) {
  throw new AssertionError(e);
 } catch (InvalidKeyException e) {
  throw new IllegalArgumentException(e);
 }
}

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

private HashingSink(Sink sink, ByteString key, String algorithm) {
 super(sink);
 try {
  this.mac = Mac.getInstance(algorithm);
  this.mac.init(new SecretKeySpec(key.toByteArray(), algorithm));
  this.messageDigest = null;
 } catch (NoSuchAlgorithmException e) {
  throw new AssertionError();
 } catch (InvalidKeyException e) {
  throw new IllegalArgumentException(e);
 }
}

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

import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class HashingUtility {
  public static String HMAC_MD5_encode(String key, String message) throws Exception {

    SecretKeySpec keySpec = new SecretKeySpec(
        key.getBytes(),
        "HmacMD5");

    Mac mac = Mac.getInstance("HmacMD5");
    mac.init(keySpec);
    byte[] rawHmac = mac.doFinal(message.getBytes());

    return Hex.encodeHexString(rawHmac);
  }
}

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

private static Mac getMac(String algorithmName, Key key) {
 try {
  Mac mac = Mac.getInstance(algorithmName);
  mac.init(key);
  return mac;
 } catch (NoSuchAlgorithmException e) {
  throw new IllegalStateException(e);
 } catch (InvalidKeyException e) {
  throw new IllegalArgumentException(e);
 }
}

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

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

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

public TheRockDigest(String secretKeyStr) {
 try {
  final SecretKey secretKey = new SecretKeySpec(secretKeyStr.getBytes(), HMAC_SHA_512);
  mac = Mac.getInstance(HMAC_SHA_512);
  mac.init(secretKey);
 } catch (Exception e) {
  throw new RuntimeException("Error initializing The Rock Signer", e);
 }
}

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

public static String encode(String key, String data) throws Exception {
 Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
 SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
 sha256_HMAC.init(secret_key);

 return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
}

public static void main(String [] args) throws Exception {
 System.out.println(encode("key", "The quick brown fox jumps over the lazy dog"));
}

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

/**
 * Creates a new {@link Mac} object.
 */
public Mac createMac() {
  try {
    Mac mac = Mac.getInstance(ALGORITHM);
    mac.init(getKey());
    return mac;
  } catch (GeneralSecurityException e) {
    // Javadoc says HmacSHA256 must be supported by every Java implementation.
    throw new Error(ALGORITHM+" not supported?",e);
  }
}

相关文章