訊息摘要演算法(慕課網視訊總結與知識梳理)
阿新 • • 發佈:2018-11-09
- 訊息摘要演算法分類:
- MD(Message Digetst):訊息摘要
- SHA(Secure Hash Algorithm):安全雜湊
- MAC(Message Authentication Code):訊息認證碼
- 以上演算法主要是用來驗證資料的完整性
- .MD系列演算法:
- 特點:MD系列生成的演算法都是128位的。MD2,MD4,MD5
- 安全性:5>4>2
- SHA演算法(SHA-1,SHA-2(224,256,384,512)):
- 特點:固定長度摘要資訊
- 上述演算法中,JDK缺少演算法SHA-224,BC提供了全部的演算法
- MAC演算法:
- 定義:很有金鑰的雜湊函式演算法。包含了MD與SHA兩個系列的演算法,只是在他們兩種演算法的基礎上添加了金鑰
- 演算法分類:
- MD系列:HmacMD2,HmacMD4,HmacMD5
- SHA系列:HmacSHA1,HmacSHA224,HmacSHA256,HmacSHA384,HmacSHA512
- MD與SHA演算法的使用:
- 是與非:演算法名稱不同,但是使用方法相同
- MessageDigets.getInstence(Algorithm);獲取了物件然後加密
- 是與非:演算法名稱不同,但是使用方法相同
- MAC的使用:
- 使用方式:
- MAC是含有金鑰的雜湊函式演算法,既然這樣肯定是要新增金鑰的。那麼首先得生成金鑰
- // 初始化KeyGenerator
KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
// 產生金鑰
SecretKey secretKey = keyGenerator.generateKey();
// 獲取金鑰
// byte[] key = secretKey.getEncoded();
byte[] key = Hex.decodeHex(new char[] { '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e' });
// 還原金鑰,HmacMD5是演算法的名字
SecretKey restoreSecretKey = new SecretKeySpec(key, "HmacMD5");
// 例項化MAC
Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm());
// 初始化MAC
mac.init(restoreSecretKey);
// 執行訊息摘要
byte[] hmacMD5Bytes = mac.doFinal(src.getBytes());
System.out.println("jdk hmacMD5:"
+ Hex.encodeHexString(hmacMD5Bytes));
- 使用方式:
- 以上是對訊息摘要演算法的簡單的描述,並不涉及操作。