@Test public void testEncrypt() { byte[] key = Base64.getDecoder().decode("aDoeS0jpEa7R6YssPU7gZvf95RYH4slqbQgr2gpijhviXyOa16xxOAYmlg0VqBKTE0QPYB26wySLruNJNsbO3A=="); byte[] data = "aaaa".getBytes(); byte[] encryptData = HMAC.encrypt(data, key); String result = Base64.getEncoder().encodeToString(encryptData); assertEquals(result, "omXf1QfFYGlZ+SshA2twjw=="); }
public static byte[] encrypt(byte[] data, byte[] key) { try { SecretKey secretKey = new SecretKeySpec(key, ISecurity.HMAC_ALGORITHM); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return mac.doFinal(data); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("无此算法错误", e); } catch (InvalidKeyException e) { throw new RuntimeException("无效密钥错误", e); } }
HMAC { public static byte[] encrypt(byte[] data, byte[] key) { try { SecretKey secretKey = new SecretKeySpec(key, ISecurity.HMAC_ALGORITHM); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return mac.doFinal(data); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("无此算法错误", e); } catch (InvalidKeyException e) { throw new RuntimeException("无效密钥错误", e); } } }
HMAC { public static byte[] encrypt(byte[] data, byte[] key) { try { SecretKey secretKey = new SecretKeySpec(key, ISecurity.HMAC_ALGORITHM); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return mac.doFinal(data); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("无此算法错误", e); } catch (InvalidKeyException e) { throw new RuntimeException("无效密钥错误", e); } }  }
HMAC { public static byte[] encrypt(byte[] data, byte[] key) { try { SecretKey secretKey = new SecretKeySpec(key, ISecurity.HMAC_ALGORITHM); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return mac.doFinal(data); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("无此算法错误", e); } catch (InvalidKeyException e) { throw new RuntimeException("无效密钥错误", e); } }  static byte[] encrypt(byte[] data, byte[] key); static byte[] initMacKey(); }
HMAC { public static byte[] encrypt(byte[] data, byte[] key) { try { SecretKey secretKey = new SecretKeySpec(key, ISecurity.HMAC_ALGORITHM); Mac mac = Mac.getInstance(secretKey.getAlgorithm()); mac.init(secretKey); return mac.doFinal(data); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("无此算法错误", e); } catch (InvalidKeyException e) { throw new RuntimeException("无效密钥错误", e); } }  static byte[] encrypt(byte[] data, byte[] key); static byte[] initMacKey();  }