对消息经过一个单向Hash算法进行计算,获得一个惟一的结果。每一个消息对应一个惟一结果,全部结果的长度固定。java
(1)MD5(Message Digest)消息摘要算法
(2)SHA(Secure Hash Algorithm)安全散列算法
(3)MAC(Message Authentication Code)消息认证码算法,结合了MD5和SHA的优点,同时用密钥对摘要进行加密,是一种更为安全的消息摘要算法。算法
校验数据的完整性,防止在传输途中被篡改编程
import java.io.File; import java.io.FileInputStream; import java.security.DigestInputStream; import java.security.MessageDigest; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class MessageDigestUtil { /** * 计算MD5值 */ public static String encryptMD5(byte[] data) throws Exception { MessageDigest messageDigest = MessageDigest.getInstance("MD5");//MD5,MD2 messageDigest.update(data); return BytesToHex.fromBytesToHex(messageDigest.digest()); } /** * 计算文件的MD5值(指纹签名) */ public static String getFileMD5(String path) throws Exception { FileInputStream fis = new FileInputStream(new File(path)); DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5")); try { byte[] buffer = new byte[1024]; int read = dis.read(buffer, 0, 1024); while (read != -1) { read = dis.read(buffer, 0, 1024); } MessageDigest md = dis.getMessageDigest(); return BytesToHex.fromBytesToHex(md.digest()); } finally { dis.close(); fis.close(); } } /** * 计算SHA值 */ public static String encryptSHA(byte[] data) throws Exception { MessageDigest messageDigest = MessageDigest.getInstance("SHA");//SHA,SHA1,SHA-1,SHA-256,SHA-384,SHA-512 messageDigest.update(data); return BytesToHex.fromBytesToHex(messageDigest.digest()); } /** * 初始化HMAC密钥 */ public static byte[] initHmacKey() throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512");//HmacMD5,HmacSHA1,HmacSHA256,HmacSHA384,HmacSHA512 return keyGenerator.generateKey().getEncoded(); } /** * 使用Hmac生成的密钥对数据进行加密 */ public static String encryptHmac(byte[] data, byte[] key) throws Exception { SecretKey secretKey = new SecretKeySpec(key, "HmacSHA512"); Mac mac = Mac.getInstance("HmacSHA512"); mac.init(secretKey); return BytesToHex.fromBytesToHex(mac.doFinal(data)); } public static void main(String[] args) throws Exception { String data = "123456"; String path = "/Users/admin/Downloads/httpd-2.4.20.tar.gz"; System.out.println(data + " 的MD5值: " + MessageDigestUtil.encryptMD5(data.getBytes())); System.out.println(path + " 的文件MD5签名: " + MessageDigestUtil.getFileMD5(path)); System.out.println(data + " 的SHA值: " + MessageDigestUtil.encryptSHA(data.getBytes())); byte[] hmacKey = initHmacKey(); System.out.println("初始化Hmac密钥:" + BytesToHex.fromBytesToHex(hmacKey)); System.out.println(data + " 是的Hmac加密值为: " + MessageDigestUtil.encryptHmac(data.getBytes(), hmacKey)); } }
public class BytesToHex { public static String fromBytesToHex(byte[] resultBytes) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < resultBytes.length; i++) { if (Integer.toHexString(0xFF & resultBytes[i]).length() == 1) { builder.append("0").append( Integer.toHexString(0xFF & resultBytes[i])); } else { builder.append(Integer.toHexString(0xFF & resultBytes[i])); } } return builder.toString(); } }