Skip to content

Commit

Permalink
工具类优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wustrive2008 committed Sep 20, 2018
1 parent 399b26f commit b7d9051
Showing 1 changed file with 69 additions and 65 deletions.
134 changes: 69 additions & 65 deletions src/main/java/com/wustrive/aesrsa/util/EncryptionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,78 @@

import java.security.MessageDigest;

/**
* Description: 加密工具类
*
* @author: wubaoguo
* @email: [email protected]
* @date: 2018/9/20 14:28
* @Copyright: 2017-2018 dgztc Inc. All rights reserved.
*/
public class EncryptionUtil {

// 对字符串进行md5加密
public static String md5(String str) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());

byte[] b = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int v = (int) b[i];
v = v < 0 ? 0x100 + v : v;
String cc = Integer.toHexString(v);
if (cc.length() == 1)
sb.append('0');
sb.append(cc);
}
/**
* 对字符串进行md5加密
*
* @param str
* @return
*/
public static String md5(String str) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(str.getBytes());
return byteToHex(md.digest());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

return sb.toString();
} catch (Exception e) {
}
return "";
}

// 对字符串进行sha256加密
public static String sha256(String str) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(str.getBytes());
/**
* 对字符串进行sha256加密
*
* @param str
* @return
*/
public static String sha256(String str) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(str.getBytes());
return byteToHex(md.digest());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

byte[] b = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int v = (int) b[i];
v = v < 0 ? 0x100 + v : v;
String cc = Integer.toHexString(v);
if (cc.length() == 1)
sb.append('0');
sb.append(cc);
}
/**
* 对字符串进行sha1加密
*
* @param str
* @return
*/
public static String sha1(String str) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
return byteToHex(md.digest());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}

return sb.toString();
} catch (Exception e) {
}
return "";
}
// 对字符串进行sha1加密
public static String sha1(String str) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());

byte[] b = md.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int v = (int) b[i];
v = v < 0 ? 0x100 + v : v;
String cc = Integer.toHexString(v);
if (cc.length() == 1)
sb.append('0');
sb.append(cc);
}

return sb.toString();
} catch (Exception e) {
}
return "";
}

/**
* 字节数组转16进制字符串
*
* @param data
* @return
*/
public static String byteToHex(byte[] data) {
final StringBuilder builder = new StringBuilder();
for(byte b : data) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
}

0 comments on commit b7d9051

Please sign in to comment.