JAVA加密算法(2)- 消息摘要算法(MD5、SHA、HMAC)

消息摘要的概念

对消息通过一个单向Hash算法进行计算,得到一个唯一的结果。每个消息对应一个唯一结果,所有结果的长度固定。

消息摘要的分类

(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));
  }

}
字节码数组转成16进制字符串工具类
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();
    }

}
    原文作者:加密算法
    原文地址: https://segmentfault.com/a/1190000007275064
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞