package foundation.util;
|
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
|
import com.alibaba.druid.util.Base64;
|
|
public class EncipherUtil {
|
|
public static String sha1Decode(String value) {
|
String hashedData = null;
|
try {
|
MessageDigest digest = MessageDigest.getInstance("SHA-1");
|
byte[] bytes = value.getBytes();
|
|
digest.update(bytes);
|
byte[] hashedBytes = digest.digest();
|
|
StringBuilder hexString = new StringBuilder();
|
for (byte b : hashedBytes) {
|
String hex = Integer.toHexString(0xFF & b);
|
if (hex.length() == 1) {
|
hexString.append('0');
|
}
|
hexString.append(hex);
|
}
|
|
hashedData = hexString.toString();
|
} catch (NoSuchAlgorithmException e) {
|
e.printStackTrace();
|
}
|
|
return hashedData;
|
}
|
|
public static String encode(String value) {
|
if (Util.isEmpty(value)) {
|
return value;
|
}
|
|
if (value.startsWith("H") && value.endsWith("DS")) {
|
return value;
|
}
|
|
value = Base64.byteArrayToBase64(value.getBytes());
|
return "H" + value + "DS";
|
}
|
|
public static String decode(String value) {
|
if (Util.isEmpty(value)) {
|
return value;
|
}
|
|
if (!value.startsWith("H") || !value.endsWith("DS")) {
|
return value;
|
}
|
|
value = value.substring(1, value.length() - 2);
|
value = new String(Base64.base64ToByteArray(value));
|
|
return value;
|
}
|
}
|