/**
*
*/
package com.util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
/**
* @author Administrator
*
*/
public class MessageDigestUtil {
/**
* 先进行MD5摘要再进行Base64编码获取摘要字符串
*
* @param str
* @return
*/
public static String base64AndMD5(String str) {
if (str == null) {
throw new IllegalArgumentException("inStr can not be null");
}
return base64AndMD5(toBytes(str));
}
/**
* 先进行MD5摘要再进行Base64编码获取摘要字符串
*
* @return
*/
public static String base64AndMD5(byte[] bytes) {
if (bytes == null) {
throw new IllegalArgumentException("bytes can not be null");
}
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(bytes);
final Base64 base64 = new Base64();
final byte[] enbytes = base64.encode(md.digest());
return new String(enbytes);
} catch (final NoSuchAlgorithmException e) {
throw new IllegalArgumentException("unknown algorithm MD5");
}
}
/**
* UTF-8编码转换为ISO-9959-1
*
* @param str
* @return
*/
public static String utf8ToIso88591(String str) {
if (str == null) {
return str;
}
try {
return new String(str.getBytes("UTF-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* ISO-9959-1编码转换为UTF-8
*
* @param str
* @return
*/
public static String iso88591ToUtf8(String str) {
if (str == null) {
return str;
}
try {
return new String(str.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* String转换为字节数组
*
* @param str
* @return
*/
private static byte[] toBytes(final String str) {
if (str == null) {
return null;
}
try {
return str.getBytes("UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* @Title: generate
* @Description: 加盐MD5
* @author lzg
* @date 2018-6-9 下午3:32:54
* @return String
* @throws
*/
public static String generate(String salt,String password) {
return md5Hex(salt + password);
/* password = md5Hex(salt + password);
char[] cs = new char[48];
for (int i = 0; i < 48; i += 3) {
cs[i] = password.charAt(i / 3 * 2);
char c = salt.charAt(i / 3);
cs[i + 1] = c;
cs[i + 2] = password.charAt(i / 3 * 2 + 1);
}
return new String(cs);*/
}
/**
* @Title: md5Hex
* @Description: 获取十六进制字符串形式的MD5摘要
* @author lzg
* @date 2018-6-11 上午11:03:52
* @return String
* @throws
*/
private static String md5Hex(String str) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bs = md5.digest(str.getBytes());
return new String(new Hex().encode(bs));
} catch (Exception e) {
return null;
}
}
/**
* @Title: base64Decode
* @Description: base64解码
* @author lzg
* @date 2018-6-11 下午02:23:34
* @return String
* @throws
*/
public static String base64Decode(String str){
final Base64 base64 = new Base64();
try {
return new String(base64.decode(str),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* @Title: base64Eecode
* @Description: base64编码
* @author lzg
* @date 2018-6-11 下午02:23:52
* @return String
* @throws
*/
public static String base64Encode(String str){
final Base64 base64 = new Base64();
try {
byte [] array = str.getBytes("UTF-8");
return base64.encodeToString(array);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* @Title: checkStrong
* @Description: 检查密码强度
* @author lzg
* @date 2018-6-11 上午11:05:41
* @return int
* @throws
*/
public static int checkStrong(String pwd){
if (pwd.length() < 8 || pwd.length() > 25){
return -1;//密码不符合要求
}
int modes = 0;
for (int i = 0; i < pwd.length(); i++) {
//密码模式
modes |= charMode(pwd.charAt(i));
}
return bitTotal(modes);
}
/**
* @Title: checkPwd
* @Description: 检查密码是否相等
* @author lzg
* @date 2018-6-11 上午11:11:17
* @return boolean
* @throws
*/
public static boolean checkPwd(String pwd1,String pwd2){
if(pwd1.length() <= 25){
return MD5.MD5Encode(pwd1).equals(pwd2);
}
return pwd1.substring(32).equals(pwd2);
}
/**
* @Title: bitTotal
* @Description: 密码强度等级
* @author lzg
* @date 2018-6-11 上午11:06:03
* @return int
* @throws
*/
private static int bitTotal (int num) {
int modes = 0;
for (int i = 0; i < 4; i++) {
if((num & 1) == 1){
modes++;
}
num >>>= 1;
}
return modes;
}
/**
* @Title: charMode
* @Description: 判断密码类型
* @author lzg
* @date 2018-6-11 上午11:06:14
* @return int
* @throws
*/
private static int charMode(int iN){
if (iN >= 48 && iN <= 57){ //数字
return 1;
}else if(iN >= 65 && iN <= 90){ //大写
return 2;
}else if(iN >= 97 && iN <= 122){ //小写
return 4;
}else{
return 8;
}
}
}
reminis_hy
- 粉丝: 1
- 资源: 5
最新资源
- 贵阳市五险一金办事指南.docx
- 三亚市五险一金办事指南.docx
- 秦皇岛市五险一金办事指南.docx
- 张北市五险一金办事指南.docx
- 焦作市五险一金办事指南.docx
- Erlang26.2.5.4+RabbitMQ3.13.7及4.0.2
- 通化市五险一金办事指南.docx
- 昆山市五险一金办事指南.docx
- 常熟市五险一金办事指南.docx
- python作业资料代码文件.zip
- java项目,课程设计-springboot学生综合测评系统
- ChristmasTree.html
- 营口市五险一金办事指南.docx
- 济南市五险一金办事指南.docx
- 潍坊市五险一金办事指南.docx
- 晋中市五险一金办事指南.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈