package util;
/**
* @author 子夕
* @version 1.0
* @description: TODO
* @date 2021/12/21 11:45
*/
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 常用加密算法 md5 sha256
* @author 江雄
*/
@Slf4j
public class DigestUtil {
public static final BASE64Encoder BASE64ENCODER = new BASE64Encoder();
public static final BASE64Decoder BASE64DECODER = new BASE64Decoder();
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
String str="123456";
String md5= md5(str);
String base64 = base64encode("123456");
System.out.println(base64);
System.out.println(base64decode(base64));
}
/**
* md5
* @param str
* @return
*/
public static String md5(String str) throws NoSuchAlgorithmException {
BigInteger bigInteger=null;
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] inputData = str.getBytes(StandardCharsets.UTF_8);
md.update(inputData);
bigInteger = new BigInteger(1,md.digest());
return bigInteger.toString(16);
}
/**
* sha256
* @param str
* @return
*/
public static String sha256(String str) throws NoSuchAlgorithmException {
MessageDigest messageDigest;
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
byte[] digest = messageDigest.digest();
return String.format("%064x", new java.math.BigInteger(1, digest));
}
/**
* HmacSHA256
* @param key
* @param data
* @return
* @throws Exception
*/
public static byte[] hmacSha256(byte[] key, String data) throws Exception {
String algorithm = "HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes(StandardCharsets.UTF_8));
}
/**
* base64加密
* @param data 加密的字符串
* @return 解密后的字符串
*/
public static String base64encode(String data){
String s = BASE64ENCODER.encodeBuffer(data.getBytes(StandardCharsets.UTF_8));
return s.replace("\n","");
}
/**
* base64解码
* @param base64 解密的字符串
* @return 解密之后的字符串
*/
public static String base64decode(String base64) throws IOException {
String s = new String(BASE64DECODER.decodeBuffer(base64));
return s.replace("\n","");
}
/**
* UTF-8编码
* @param param
* @return
*/
private String urlEncoder(String param){
try {
return URLEncoder.encode(param, "UTF-8");
} catch (Exception e) {
return null;
}
}
/**
* 字节转16进制
* @param bytes
* @return
*/
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
private String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars).toLowerCase();
}
}
java常用工具类整理-加密算法
最新推荐文章于 2023-06-25 11:55:46 发布