java实现MD5加密

         作者:sundroid

       个人站点:sundroid.cn    邮箱: hfutsnjc@163.com   微博:http://weibo.com/Sundroid


package com.sundroid.utils.common;

import java.security.MessageDigest;

import java.io.*;

public class MD5Tool {
	// 0的ASCII码
	private static final int ASCII_0 = 48;
	// 9的ASCII码
	private static final int ASCII_9 = 57;
	// A的ASCII码
	private static final int ASCII_A = 65;
	// F的ASCII码
	private static final int ASCII_F = 70;
	// a的ASCII码
	private static final int ASCII_a = 97;
	// f的ASCII码
	private static final int ASCII_f = 102;

	// 可表示16进制数字的字符
	private static final char hexChar[] = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

	private static final String HASH_MD5 = "MD5";

	// private static Log log = Log.getInstance( MD5Tool.class);

	/**
	 * 获取字节数组MD5码
	 * 
	 * @param bs
	 * @return
	 */
	public final static String encoding(byte[] bs) {

		String encodingStr = null;
		try {
			MessageDigest mdTemp = MessageDigest.getInstance(HASH_MD5);
			mdTemp.update(bs);

			return toHexString(mdTemp.digest());

		} catch (Exception e) {
			// log.error(e);
			e.printStackTrace();
		}

		return encodingStr;
	}

	/**
	 * 获取字符串MD5码
	 * 
	 * @param text
	 * @return
	 */
	public final static String encoding(String text) {
		if (text == null) {
			return null;
		}
		return encoding(text.getBytes());
	}

	public final static String encodeTwice(String text) {
		if (text == null) {
			return null;
		}
		String md5Once = encoding(text.getBytes());
		return encoding(md5Once.getBytes());
	}

	/**
	 * 获取文件内容MD5码
	 * 
	 * @param filePath
	 * @return
	 */
	public final static String encodingFile(String filePath) {
		InputStream fis = null;
		try {
			fis = new FileInputStream(filePath);
			return encoding(fis);
		} catch (Exception ee) {
			return null;
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (Exception ex) {
				}
			}
		}
	}

	/**
	 * 获取输入流MD5码
	 * 
	 * @param fis
	 * @return
	 * @throws Exception
	 */
	public final static String encoding(InputStream fis) throws Exception {
		byte[] buffer = new byte[1024];
		MessageDigest md5 = MessageDigest.getInstance(HASH_MD5);
		int numRead = 0;
		while ((numRead = fis.read(buffer)) > 0) {
			md5.update(buffer, 0, numRead);
		}
		return toHexString(md5.digest());
	}

	/**
	 * 转换为用16进制字符表示的MD5
	 * 
	 * @param b
	 * @return
	 */
	public static String toHexString(byte[] b) {
		StringBuilder sb = new StringBuilder(b.length * 2);
		for (int i = 0; i < b.length; i++) {
			sb.append(hexChar[(b[i] & 0xf0) >>> 4]);
			sb.append(hexChar[b[i] & 0x0f]);
		}
		return sb.toString();
	}

	/**
	 * 检验是否是合法的MD5串
	 * 
	 * @param md5Str
	 * @return
	 */
	public static boolean validate(String md5Str) {
		if (md5Str == null || md5Str.length() != 32) {
			return false;
		}
		byte[] by = md5Str.getBytes();
		for (int i = 0; i < by.length; i++) {
			int asciiValue = (int) by[i];
			if (asciiValue < ASCII_0
					|| (asciiValue > ASCII_9 && asciiValue < ASCII_A)
					|| (asciiValue > ASCII_F && asciiValue < ASCII_a)
					|| asciiValue > ASCII_f) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {

		System.out.println(encodeTwice("sundroid"));
		System.out.println(validate(encodeTwice("sundroid")));
	}
}

  我们在做开发时一些敏感信息需要加密的MD5是个不错的选择,比如写入在数据库用户的信息,账号密码等等,如果不进行加密那么是极不安全的


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值