MAC加密组件 @author SeanDragon
| 14 | * @author SeanDragon |
| 15 | */ |
| 16 | public final class ToolMAC { |
| 17 | |
| 18 | private ToolMAC() { |
| 19 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * 初始化HmacMD5密钥 |
| 24 | * |
| 25 | * @return |
| 26 | * |
| 27 | * @throws Exception |
| 28 | */ |
| 29 | public static byte[] initHmacMD5Key() throws NoSuchAlgorithmException { |
| 30 | |
| 31 | // 初始化KeyGenerator |
| 32 | KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5"); |
| 33 | |
| 34 | // 产生秘密密钥 |
| 35 | SecretKey secretKey = keyGenerator.generateKey(); |
| 36 | |
| 37 | // 获得密钥 |
| 38 | return secretKey.getEncoded(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * HmacMD5加密 |
| 43 | * |
| 44 | * @param data |
| 45 | * 待加密数据 |
| 46 | * @param key |
| 47 | * 密钥 |
| 48 | * |
| 49 | * @return byte[] 消息摘要 |
| 50 | * |
| 51 | * @throws Exception |
| 52 | */ |
| 53 | public static byte[] encodeHmacMD5(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException { |
| 54 | |
| 55 | // 还原密钥 |
| 56 | SecretKey secretKey = new SecretKeySpec(key, "HmacMD5"); |
| 57 | |
| 58 | // 实例化Mac "SslMacMD5" |
| 59 | Mac mac = Mac.getInstance("SslMacMD5");//secretKey.getAlgorithm()); |
| 60 | |
| 61 | // 初始化Mac |
| 62 | mac.init(secretKey); |
| 63 | |
| 64 | // 执行消息摘要 |
| 65 | return mac.doFinal(data); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * 初始化HmacSHA1密钥 |
| 70 | * |
| 71 | * @return |
| 72 | * |
| 73 | * @throws Exception |
nothing calls this directly
no outgoing calls
no test coverage detected