com.app.common.encrypt.StringCoder Maven / Gradle / Ivy
The newest version!
package com.app.common.encrypt;
import java.io.UnsupportedEncodingException;
/**
* @Description
* @Author
* @Date
**/
public class StringCoder {
public StringCoder() {
}
public static byte[] toByteArray(String str) {
try {
byte[] buf = str.getBytes("UTF-8");
return buf;
} catch (UnsupportedEncodingException var2) {
var2.printStackTrace();
return null;
}
}
public static String fromByteArray(byte[] array) {
try {
String result = new String(array, "UTF-8");
return result;
} catch (UnsupportedEncodingException var2) {
var2.printStackTrace();
return null;
}
}
public static String toBase64(byte[] array) {
return new String(Base64Coder.encode(array));
}
public static byte[] fromBase64(String str) {
return Base64Coder.decode(str);
}
public static String md5Hash(String str) {
byte[] array = toByteArray(str);
byte[] buf = Hash.md5(array);
return toBase64(buf);
}
public static String shaHash(String str) {
byte[] array = toByteArray(str);
byte[] buf = Hash.sha(array);
return toBase64(buf);
}
public static String zipToBase64(String str) {
String result = str;
byte[] buf = toByteArray(str);
if (buf != null) {
byte[] zip = GZip.zip(buf);
if (zip != null) {
result = toBase64(zip);
}
}
return result;
}
public static String unzipFromBase64(String base64) {
String result = base64;
byte[] array = fromBase64(base64);
if (array != null) {
byte[] buf = GZip.unzip(array);
if (buf != null) {
result = fromByteArray(buf);
}
}
return result;
}
}