骑士李四记录
Base64Util
import org.apache.commons.codec.binary.Base64;
public class Base64Util {
public static String encode(String input) {
if (null == input) {
input = "";
}
byte[] base64 = Base64.encodeBase64(input.getBytes());
try{
return new String(base64,"UTF-8");
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
public static String decode(String input) {
if (null == input) {
input = "";
}
byte[] base64 = Base64.decodeBase64(input.getBytes());
try{
return new String(base64,"UTF-8");
}
catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
MD5Util
import org.apache.commons.lang3.StringUtils;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
public class MD5Util {
public static String MD5(String s) {
if (StringUtils.isEmpty(s) || StringUtils.isBlank(s))
{
return "";
}
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] btInput = s.getBytes("utf-8");
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) {
return null;
}
}
public static String getHashCode(Object object) throws IOException {
if(object == null) return "";
String ss = null;
ObjectOutputStream s = null;
ByteArrayOutputStream bo = new ByteArrayOutputStream();
try {
s = new ObjectOutputStream(bo);
s.writeObject(object);
s.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(s != null) {
s.close();
s = null;
}
}
ss = MD5(bo.toString());
return ss;
}
}