MD5Util 工具類2.0.1

package com.jfai.kg.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import lombok.extern.slf4j.Slf4j;

/**
  * 
  * @Package com.jfai.kg.kafkacomsu.util
  * @author wanglf
  * @Description: MD5工具類,提供字符串MD5加密(校驗),文件MD5值獲取(校驗)功能
  * @date 2018年6月30日 下午2:34:28 
  * @version 2.0.1
  */
@Slf4j
public class MD5Util {

	/**
	 * 16進制字符集
	 */
	private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	/**
	 * 
	 * 指定算法爲MD5的MessageDigest
	 */
	private static MessageDigest messageDigest = null;

	/**
	 * 
	 * 初始化messageDigest的加密算法爲MD5
	 */
	static {
		try {
			messageDigest = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			log.error("Exception :", e);
		}
	}

	/**
	 * 獲取文件的MD5值
	 * 
	 * @param file 目標文件
	 * @return MD5 字符串
	 */
	public static String getMD5String(File file) {
		String ret = "";
		FileInputStream in = null;
		FileChannel ch = null;
		try {
			in = new FileInputStream(file);
			ch = in.getChannel();
			ByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
			messageDigest.update(byteBuffer);
			ret = bytesToHex(messageDigest.digest());
		} catch (IOException e) {
			log.error("Exception :", e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					log.error("Exception :", e);
				}
			}
			if (ch != null) {
				try {
					ch.close();
				} catch (IOException e) {
					log.error("Exception :", e);
				}
			}
		}
		return ret;
	}

	/**
	 * MD5加密字符串
	 * 
	 * @param str 目標字符串
	 * @return MD5 加密後的字符串
	 */
	public static String getMD5String(String str) {
		return getMD5String(str.getBytes());
	}

	/**
	 * MD5加密以byte數組表示的字符串
	 * 
	 * @param bytes 目標byte數組
	 * @return MD5 加密後的字符串
	 */
	public static String getMD5String(byte[] bytes) {
		messageDigest.update(bytes);
		return bytesToHex(messageDigest.digest());
	}

	/**
	 * 按照mysql的函數生成md5
	 * mysql的函數生成md5,主要多加了一個.toLowerCase()
	 * @return MD5 加密後的字符串,然後大寫轉小寫
	 */
	public static String getMySQLMD5(String str) {
		return getMD5String(str).toLowerCase();
	}

	/**
	 * 校驗密碼與其MD5是否一致
	 * 
	 * @param pwd 密碼字符串
	 * @param md5 基準MD5值
	 * @return 檢驗結果
	 */
	public static boolean checkPassword(String pwd, String md5) {
		return getMD5String(pwd).equalsIgnoreCase(md5);
	}

	/**
	 * 校驗密碼與其MD5是否一致
	 * 
	 * @param pwd
	 *            以字符數組表示的密碼
	 * @param md5
	 *            基準MD5值
	 * @return 檢驗結果
	 */
	public static boolean checkPassword(char[] pwd, String md5) {
		return checkPassword(new String(pwd), md5);
	}

	/**
	 * 檢驗文件的MD5值
	 * 
	 * @param file 目標文件
	 * @param md5 基準MD5值
	 * @return 檢驗結果
	 */
	public static boolean checkFileMD5(File file, String md5) {
		return getMD5String(file).equalsIgnoreCase(md5);
	}

	/**
	 * 將字節數組轉換成16進制字符串
	 * 
	 * @param bytes 目標字節數組
	 * @return 轉換結果
	 */
	public static String bytesToHex(byte bytes[]) {
		return bytesToHex(bytes, 0, bytes.length);
	}

	/**
	 * 將字節數組中指定區間的子數組轉換成16進制字符串
	 * 
	 * @param bytes 目標字節數組
	 * @param start 起始位置(包括該位置)
	 * @param end 結束位置(不包括該位置)
	 * @return 轉換結果
	 */
	public static String bytesToHex(byte bytes[], int start, int end) {
		StringBuilder sb = new StringBuilder();
		for (int i = start; i < start + end; i++) {
			sb.append(byteToHex(bytes[i]));
		}
		return sb.toString();
	}

	/**
	 * 將單個字節碼轉換成16進制字符串
	 * 
	 * @param bt 目標字節
	 * @return 轉換結果
	 */
	public static String byteToHex(byte bt) {
		return HEX_DIGITS[(bt & 0xf0) >> 4] + "" + HEX_DIGITS[bt & 0xf];
	}

	public static void main(String[] args) throws IOException {

	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章