MD5加密类方法
package com.shkj.android.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
?* md5加密
?* @author admin
?*/
public class 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) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
?}
?private MD5() {
?}
?
?//調(diào)用此方法,直接傳字符串即可加密
?final public static String encode(String source) {
??byte[] btyes = source.getBytes();
??byte[] encodedBytes = sDigest.digest(btyes);
??return hexString(encodedBytes);
?}
?
?public static String hexString(byte[] source) {
??if (source == null || source.length <= 0) {
???return "";
??}
??final int size = source.length;
??final char str[] = new char[size * 2];
??int index = 0;
??byte b;
??for (int i = 0; i < size; i++) {
???b = source[i];
???str[index++] = sHexDigits[b >>> 4 & 0xf];
???str[index++] = sHexDigits[b & 0xf];
??}
??return new String(str);
?}
}
轉(zhuǎn)載于:https://www.cnblogs.com/qgc88/p/3210394.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
- 上一篇: 静态库与动态库(一):概述
- 下一篇: Windows phone listbo