Java:MD5 Hash

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {

	private MD5() {
	}

	public static String md5(String s) {
		if (s == null) {
			return null;
		}
		MessageDigest digest;
		StringBuffer hasHexString;
		try {
			digest = MessageDigest.getInstance("MD5");
			digest.update(s.getBytes(), 0, s.length());
			byte messageDigest[] = digest.digest();
			hasHexString = new StringBuffer();
			for (int i = 0; i < messageDigest.length; i++) {
				String hex = Integer.toHexString(0xFF & messageDigest[i]);
				if (hex.length() == 1)
					hasHexString.append('0');
				hasHexString.append(hex);
			}
			return hasHexString.toString();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}
}

你可能感兴趣的:(Java:MD5 Hash)