java工具类实现MD5加密、SHA数字签名

MD5:一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致
SHA:该SHA(安全散列算法)是加密散列函数族。它与MD5非常相似,只不过它会产生更强的哈希值。然而,这些散列并不总是唯一的,这意味着对于两个不同的输入,我们可以具有相等的散列。当发生这种情况时,它被称为“碰撞”。SHA中的碰撞几率小于MD5。但是,不要担心这些碰撞,因为它们真的非常罕见。
代码:

package com.liziguo.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigitalSignature {

	public static String MD5_16(String str) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(str.getBytes("UTF-8"));
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.substring(8, 24);
	}

	public static String MD5_32(String str) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(str.getBytes("UTF-8"));
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.toString();
	}

	public static String MD5_32(byte[] bytes) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("MD5");
			m.update(bytes);
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.toString();
	}

	public static String SHA_1(String str) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("SHA-1");
			m.update(str.getBytes("UTF-8"));
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.toString();
	}

	public static String SHA_224(String str) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("SHA-224");
			m.update(str.getBytes("UTF-8"));
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.toString();
	}

	public static String SHA_256(String str) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("SHA-256");
			m.update(str.getBytes("UTF-8"));
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.toString();
	}

	public static String SHA_384(String str) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("SHA-384");
			m.update(str.getBytes("UTF-8"));
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.toString();
	}

	public static String SHA_512(String str) {
		StringBuilder strbui = null;
		try {
			MessageDigest m = MessageDigest.getInstance("SHA-512");
			m.update(str.getBytes("UTF-8"));
			byte[] by = m.digest();
			strbui = new StringBuilder();
			for (int i = 0; i < by.length; i++) {
				strbui.append(Integer.toHexString((0x000000FF & by[i]) | 0xFFFFFF00).substring(6));
			}
		} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (strbui == null) return null;
		return strbui.toString();
	}

}

运行结果:
java工具类实现MD5加密、SHA数字签名_第1张图片

你可能感兴趣的:(java工具类实现MD5加密、SHA数字签名)