package com.tz.ssspm.util;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.Validate;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.security.SecureRandom;
/**
* 封装各种格式的编码解码工具类.
* 1.Commons-Codec的 hex/base64 编码
* 3.Commons-Lang的xml/html escape
* 4.JDK提供的URLEncoder
*/
public class EncryptUtil {
public static final String DEFAULT_URL_ENCODING = "UTF-8";
public static final String SHA1 = "SHA-1";
public static final String MD5 = "MD5";
private static SecureRandom random = new SecureRandom();
/**
* Hex编码.
*/
public static String encodeHex(byte[] input) {
return new String(Hex.encodeHex(input));
}
/**
* Hex解码.
*/
public static byte[] decodeHex(String input) {
try {
return Hex.decodeHex(input.toCharArray());
} catch (DecoderException e) {
e.printStackTrace();
}
return null;
}
/**
* Base64编码.
*/
public static String encodeBase64(byte[] input) {
return new String(Base64.encodeBase64(input));
}
/**
* Base64编码.
*/
public static String encodeBase64(String input) {
try {
return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* Base64解码.
*/
public static byte[] decodeBase64(String input) {
return Base64.decodeBase64(input.getBytes());
}
/**
* Base64解码.
*/
public static String decodeBase64String(String input) {
try {
return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* Html 转码.
*/
public static String escapeHtml(String html) {
return StringEscapeUtils.escapeHtml4(html);
}
/**
* Html 解码.
*/
public static String unescapeHtml(String htmlEscaped) {
return StringEscapeUtils.unescapeHtml4(htmlEscaped);
}
/**
* Xml 转码.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 解码.
*/
public static String unescapeXml(String xmlEscaped) {
return StringEscapeUtils.unescapeXml(xmlEscaped);
}
/**
* URL 编码, Encode默认为UTF-8.
*/
public static String urlEncode(String part) {
try {
return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* URL 解码, Encode默认为UTF-8.
*/
public static String urlDecode(String part) {
try {
return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/**
* 对输入字符串进行md5散列.
*/
public static byte[] md5(byte[] input) {
return digest(input, MD5, null, 1);
}
public static byte[] md5(byte[] input, int iterations) {
return digest(input, MD5, null, iterations);
}
/**
* 对输入字符串进行sha1散列.
*/
public static byte[] sha1(byte[] input) {
return digest(input, SHA1, null, 1);
}
public static byte[] sha1(byte[] input, byte[] salt) {
return digest(input, SHA1, salt, 1);
}
public static byte[] sha1(byte[] input, byte[] salt, int iterations) {
return digest(input, SHA1, salt, iterations);
}
/**
* 对字符串进行散列, 支持md5与sha1算法.
*/
private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) {
try {
MessageDigest digest = MessageDigest.getInstance(algorithm);
if (salt != null) {
digest.update(salt);
}
byte[] result = digest.digest(input);
for (int i = 1; i < iterations; i++) {
digest.reset();
result = digest.digest(result);
}
return result;
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return null;
}
/**
* 生成随机的Byte[]作为salt.
*
* @param numBytes byte数组的大小
*/
public static byte[] generateSalt(int numBytes) {
Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);
byte[] bytes = new byte[numBytes];
random.nextBytes(bytes);
return bytes;
}
/**
* 对文件进行md5散列.
*/
public static byte[] md5(InputStream input) throws IOException {
return digest(input, MD5);
}
/**
* 对文件进行sha1散列.
*/
public static byte[] sha1(InputStream input) throws IOException {
return digest(input, SHA1);
}
private static byte[] digest(InputStream input, String algorithm) throws IOException {
try {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
int bufferLength = 8 * 1024;
byte[] buffer = new byte[bufferLength];
int read = input.read(buffer, 0, bufferLength);
while (read > -1) {
messageDigest.update(buffer, 0, read);
read = input.read(buffer, 0, bufferLength);
}
return messageDigest.digest();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return null;
}
/**
*
* 加密密码并返回
* 方法名:encryptPwd
* 创建人: shubiao
* 时间:2016年11月13日 -下午12:14:48
* @param pwd
* @return
* return: String
* @exception
* @since 1.0.0
*/
public String encryptPwd(String pwd){
//生成8位随机数作为盐
byte random[] = EncryptUtil. generateSalt(8);
//用可逆的加密算法加密随机数
String randomHex = EncryptUtil. encodeHex(random );
//
String encryptPwd = EncryptUtil. encodeHex(EncryptUtil. sha1(pwd.getBytes(), random, 1024));
String newpwd = randomHex+ encryptPwd;
return newpwd;
}
/**
*
* 密码验证
* 两个参数:第一个文本框输入密码,第二个加密后的密码
* 方法名:encryptPwd
* 创建人: shubiao
* 时间:2016年11月13日 -下午12:37:32
* @param inputpwd
* @param encryptpwd
* @return
* return: String
* @exception
* @since 1.0.0
*/
public String encryptPwd(String inputpwd,String encryptpwd){
inputpwd = "123456";
encryptpwd = "91820075a5775d36a20d6a16e6647eead20f291ce8729f881f883d30" ;
//先将密文逆转出来
byte salt[] = EncryptUtil.decodeHex(encryptpwd.substring(0, 16));
//重新拼凑盐+密码进行不可逆加密sha1
byte haspwd[] = EncryptUtil. sha1(inputpwd.getBytes(), salt, 1024);
//在对盐和haspwd进行可逆加密
String newpwd = EncryptUtil.encodeHex(salt)+EncryptUtil.encodeHex(haspwd );
return newpwd;
}
}