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

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

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

  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: 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: twosigma/beakerx

  1. public String signBytes(List<byte[]> msg) {
  2. try {
  3. final Mac mac = Mac.getInstance(TYPE);
  4. mac.init(spec);
  5. msg.forEach(it -> mac.update(it));
  6. byte[] digest = mac.doFinal();
  7. return toHex(digest);
  8. } catch (InvalidKeyException e) {
  9. throw new RuntimeException(INVALID_HMAC_EXCEPTION, e);
  10. } catch (NoSuchAlgorithmException e) {
  11. throw new RuntimeException(e);
  12. }
  13. }

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

  1. Mac mac = Mac.getInstance(MAC_NAME);
  2. mac.init(key);
  3. byte[] text = signatureBaseString.getBytes("UTF-8");
  4. byte[] signatureBytes = mac.doFinal(text);
  5. signatureBytes = Base64.encodeBase64(signatureBytes);
  6. String signature = new String(signatureBytes, "UTF-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: 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: signalapp/Signal-Server

  1. private byte[] getCiphertext(byte[] plaintext, SecretKeySpec cipherKey, SecretKeySpec macKey)
  2. throws CryptoEncodingException
  3. {
  4. try {
  5. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  6. cipher.init(Cipher.ENCRYPT_MODE, cipherKey);
  7. Mac hmac = Mac.getInstance("HmacSHA256");
  8. hmac.init(macKey);
  9. hmac.update(VERSION);
  10. byte[] ivBytes = cipher.getIV();
  11. hmac.update(ivBytes);
  12. byte[] ciphertext = cipher.doFinal(plaintext);
  13. byte[] mac = hmac.doFinal(ciphertext);
  14. byte[] truncatedMac = new byte[MAC_SIZE];
  15. System.arraycopy(mac, 0, truncatedMac, 0, truncatedMac.length);
  16. return Util.combine(VERSION, ivBytes, ciphertext, truncatedMac);
  17. } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
  18. throw new AssertionError(e);
  19. } catch (InvalidKeyException e) {
  20. logger.warn("Invalid Key", e);
  21. throw new CryptoEncodingException("Invalid key!");
  22. }
  23. }

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

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

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

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

  1. /**
  2. * This method uses the JCE to provide the crypto algorithm.
  3. * HMAC computes a Hashed Message Authentication Code with the
  4. * crypto hash algorithm as a parameter.
  5. *
  6. * @param crypto: the crypto algorithm (HmacSHA1, HmacSHA256,
  7. * HmacSHA512)
  8. * @param keyBytes: the bytes to use for the HMAC key
  9. * @param text: the message or text to be authenticated
  10. */
  11. private static byte[] hmac_sha(String crypto, byte[] keyBytes,
  12. byte[] text) {
  13. try {
  14. Mac hmac;
  15. hmac = Mac.getInstance(crypto);
  16. SecretKeySpec macKey =
  17. new SecretKeySpec(keyBytes, "RAW");
  18. hmac.init(macKey);
  19. return hmac.doFinal(text);
  20. } catch (GeneralSecurityException gse) {
  21. throw new UndeclaredThrowableException(gse);
  22. }
  23. }

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

  1. private CoinoneHmacDigest(String secretKeyBase) throws IllegalArgumentException {
  2. try {
  3. SecretKey secretKey = new SecretKeySpec(secretKeyBase.getBytes(), HMAC_SHA_512);
  4. mac = Mac.getInstance(HMAC_SHA_512);
  5. mac.init(secretKey);
  6. } catch (InvalidKeyException e) {
  7. throw new IllegalArgumentException("Invalid key for hmac initialization.", e);
  8. } catch (NoSuchAlgorithmException e) {
  9. throw new RuntimeException(
  10. "Illegal algorithm for post body digest. Check the implementation.");
  11. }
  12. }

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

  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. }

相关文章