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

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

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

Mac.getInstance介绍

[英]Creates a new Mac instance that provides the specified MAC algorithm.
[中]创建提供指定Mac算法的新Mac实例。

代码示例

代码示例来源: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: 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: twosigma/beakerx

public String signBytes(List<byte[]> msg) {
 try {
  final Mac mac = Mac.getInstance(TYPE);
  mac.init(spec);
  msg.forEach(it -> mac.update(it));
  byte[] digest = mac.doFinal();
  return toHex(digest);
 } catch (InvalidKeyException e) {
  throw new RuntimeException(INVALID_HMAC_EXCEPTION, e);
 } catch (NoSuchAlgorithmException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源: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: 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: spring-projects/spring-security-oauth

Mac mac = Mac.getInstance(MAC_NAME);
mac.init(key);
byte[] text = signatureBaseString.getBytes("UTF-8");
byte[] signatureBytes = mac.doFinal(text);
signatureBytes = Base64.encodeBase64(signatureBytes);
String signature = new String(signatureBytes, "UTF-8");

代码示例来源: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: 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: signalapp/Signal-Server

private byte[] getCiphertext(byte[] plaintext, SecretKeySpec cipherKey, SecretKeySpec macKey)
  throws CryptoEncodingException
{
 try {
  Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
  Mac hmac = Mac.getInstance("HmacSHA256");
  hmac.init(macKey);
  hmac.update(VERSION);
  byte[] ivBytes = cipher.getIV();
  hmac.update(ivBytes);
  byte[] ciphertext   = cipher.doFinal(plaintext);
  byte[] mac          = hmac.doFinal(ciphertext);
  byte[] truncatedMac = new byte[MAC_SIZE];
  System.arraycopy(mac, 0, truncatedMac, 0, truncatedMac.length);
  return Util.combine(VERSION, ivBytes, ciphertext, truncatedMac);
 } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
  throw new AssertionError(e);
 } catch (InvalidKeyException e) {
  logger.warn("Invalid Key", e);
  throw new CryptoEncodingException("Invalid key!");
 }
}

代码示例来源: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: spring-projects/spring-security-oauth

Mac mac = Mac.getInstance(MAC_NAME);
mac.init(key);
byte[] text = signatureBaseString.getBytes("UTF-8");
byte[] calculatedBytes = mac.doFinal(text);
if (!safeArrayEquals(calculatedBytes, signatureBytes)) {
 throw new InvalidSignatureException("Invalid signature for signature method " + getName());

代码示例来源: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);
  }
}

代码示例来源:origin: hs-web/hsweb-framework

/**
 * This method uses the JCE to provide the crypto algorithm.
 * HMAC computes a Hashed Message Authentication Code with the
 * crypto hash algorithm as a parameter.
 *
 * @param crypto:   the crypto algorithm (HmacSHA1, HmacSHA256,
 *                  HmacSHA512)
 * @param keyBytes: the bytes to use for the HMAC key
 * @param text:     the message or text to be authenticated
 */
private static byte[] hmac_sha(String crypto, byte[] keyBytes,
                byte[] text) {
  try {
    Mac hmac;
    hmac = Mac.getInstance(crypto);
    SecretKeySpec macKey =
        new SecretKeySpec(keyBytes, "RAW");
    hmac.init(macKey);
    return hmac.doFinal(text);
  } catch (GeneralSecurityException gse) {
    throw new UndeclaredThrowableException(gse);
  }
}

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

private CoinoneHmacDigest(String secretKeyBase) throws IllegalArgumentException {
 try {
  SecretKey secretKey = new SecretKeySpec(secretKeyBase.getBytes(), HMAC_SHA_512);
  mac = Mac.getInstance(HMAC_SHA_512);
  mac.init(secretKey);
 } catch (InvalidKeyException e) {
  throw new IllegalArgumentException("Invalid key for hmac initialization.", e);
 } catch (NoSuchAlgorithmException e) {
  throw new RuntimeException(
    "Illegal algorithm for post body digest. Check the implementation.");
 }
}

代码示例来源: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: wildfly/wildfly

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);
 }
}

相关文章