1. 程式人生 > 其它 >Nginx的配置與使用

Nginx的配置與使用

public final class Md5Util {
private Md5Util(){}
/**
* 將明文密碼轉成MD5密碼
*/
public static String encodeByMd5(String password) throws Exception{
//Java中MessageDigest類封裝了MD5和SHA演算法,今天我們只要MD5演算法
MessageDigest md5 = MessageDigest.getInstance("MD5");
//呼叫MD5演算法,即返回16個byte型別的值
byte[] byteArray = md5.digest(password.getBytes());
//注意:MessageDigest只能將String轉成byte[],接下來的事情,由我們程式設計師來完成
return byteArrayToHexString(byteArray);
}
/**
* 將byte[]轉在16進位制字串
*/
private static String byteArrayToHexString(byte[] byteArray) {
StringBuffer sb = new StringBuffer();
//遍歷
for(byte b : byteArray){//16次
//取出每一個byte型別,進行轉換
String hex = byteToHexString(b);
//將轉換後的值放入StringBuffer中
sb.append(hex);
}
return sb.toString();
}
/**
* 將byte轉在16進位制字串
*/
private static String byteToHexString(byte b) {//-31轉成e1,10轉成0a,。。。
//將byte型別賦給int型別
int n = b;
//如果n是負數
if(n < 0){
//轉正數
//-31的16進位制數,等價於求225的16進位制數
n = 256 + n;
}
//商(14),陣列的下標
int d1 = n / 16;
//餘(1),陣列的下標
int d2 = n % 16;
//通過下標取值
return hex[d1] + hex[d2];
}
private static String[] hex = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
/**
* 測試
*/
public static void main(String[] args) throws Exception{
String password = "123456";
String passwordMD5 = Md5Util.encodeByMd5(password);
System.out.println(password);
System.out.println(passwordMD5);
}
}