耀灵 2 anni fa
parent
commit
2b35105c6e

+ 105
- 0
src/manager/cn/com/yskj/zfdm/model/organizationpersonnel/MD5Util.java Vedi File

@@ -0,0 +1,105 @@
package cn.com.yskj.zfdm.model.organizationpersonnel;

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

public class MD5Util {

public final static String MD5(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = s.getBytes();
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
*
* @time 2017年10月9日10:07:49
* @author Lxm
* @param data
* @param charset
* @return
*/
public final static String MD5(String data, String charset) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = data.getBytes(charset);
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 获取32,16位md5
*
* @param sourceStr
* @param bit
* @return
*/
public static String MD5Plus(String sourceStr, int bit) {
String result = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(sourceStr.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
result = buf.toString();
if (bit == 16) {
result = buf.toString().substring(8, 24);
}
// System.out.println("MD5(" + sourceStr + ",32) = " + result);
// System.out.println("MD5(" + sourceStr + ",16) = "
// +buf.toString().substring(8, 24));
} catch (NoSuchAlgorithmException e) {
System.out.println(e);
}
return result;
}


}

Loading…
Annulla
Salva