import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.http.impl.auth.UnsupportedDigestAlgorithmException; import android.util.Log; public final class MD5 { private static final String LOG_TAG = "MD5"; private static final String ALGORITHM = "MD5"; private static char sHexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; private static MessageDigest sDigest; static { try { sDigest = MessageDigest.getInstance(ALGORITHM); } catch (NoSuchAlgorithmException e) { // Log.e(LOG_TAG, "Get MD5 Digest failed."); throw new UnsupportedDigestAlgorithmException(ALGORITHM, e); } } private MD5() { } final public static String encode(String source) { byte[] btyes = source.getBytes(); byte[] encodedBytes = sDigest.digest(btyes); return Utility.hexString(encodedBytes); } public static void main(String[] args) { System.out.println(MD5.encode("Today i'm not busy!")); } }
//关于 MessageDigest 类为应用程序提供信息摘要算法的功能,如 MD5 或 SHA 算法。信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。
//个人举得这篇博文还是不错的 http://blog.sina.com.cn/s/blog_4f36423201000c1e.html