package com.qianwen.core.tool.utils;
|
|
import java.security.InvalidKeyException;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
import javax.crypto.Mac;
|
import javax.crypto.SecretKey;
|
import javax.crypto.spec.SecretKeySpec;
|
import org.springframework.lang.Nullable;
|
import org.springframework.util.DigestUtils;
|
|
/* loaded from: blade-core-tool-9.3.0.0-SNAPSHOT.jar:org/springblade/core/tool/utils/DigestUtil.class */
|
public class DigestUtil extends DigestUtils {
|
private static final char[] HEX_CODE = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
|
|
public static String md5Hex(final String data) {
|
return DigestUtils.md5DigestAsHex(data.getBytes(Charsets.UTF_8));
|
}
|
|
public static String md5Hex(final byte[] bytes) {
|
return DigestUtils.md5DigestAsHex(bytes);
|
}
|
|
public static String sha1Hex(String data) {
|
return sha1Hex(data.getBytes(Charsets.UTF_8));
|
}
|
|
public static String sha1Hex(final byte[] bytes) {
|
return digestHex("SHA-1", bytes);
|
}
|
|
public static String sha224Hex(String data) {
|
return sha224Hex(data.getBytes(Charsets.UTF_8));
|
}
|
|
public static String sha224Hex(final byte[] bytes) {
|
return digestHex("SHA-224", bytes);
|
}
|
|
public static String sha256Hex(String data) {
|
return sha256Hex(data.getBytes(Charsets.UTF_8));
|
}
|
|
public static String sha256Hex(final byte[] bytes) {
|
return digestHex("SHA-256", bytes);
|
}
|
|
public static String sha384Hex(String data) {
|
return sha384Hex(data.getBytes(Charsets.UTF_8));
|
}
|
|
public static String sha384Hex(final byte[] bytes) {
|
return digestHex("SHA-384", bytes);
|
}
|
|
public static String sha512Hex(String data) {
|
return sha512Hex(data.getBytes(Charsets.UTF_8));
|
}
|
|
public static String sha512Hex(final byte[] bytes) {
|
return digestHex("SHA-512", bytes);
|
}
|
|
public static String digestHex(String algorithm, byte[] bytes) {
|
try {
|
MessageDigest md = MessageDigest.getInstance(algorithm);
|
return encodeHex(md.digest(bytes));
|
} catch (NoSuchAlgorithmException e) {
|
throw Exceptions.unchecked(e);
|
}
|
}
|
|
public static String hmacMd5Hex(String data, String key) {
|
return hmacMd5Hex(data.getBytes(Charsets.UTF_8), key);
|
}
|
|
public static String hmacMd5Hex(final byte[] bytes, String key) {
|
return digestHMacHex("HmacMD5", bytes, key);
|
}
|
|
public static String hmacSha1Hex(String data, String key) {
|
return hmacSha1Hex(data.getBytes(Charsets.UTF_8), key);
|
}
|
|
public static String hmacSha1Hex(final byte[] bytes, String key) {
|
return digestHMacHex("HmacSHA1", bytes, key);
|
}
|
|
public static String hmacSha224Hex(String data, String key) {
|
return hmacSha224Hex(data.getBytes(Charsets.UTF_8), key);
|
}
|
|
public static String hmacSha224Hex(final byte[] bytes, String key) {
|
return digestHMacHex("HmacSHA224", bytes, key);
|
}
|
|
public static byte[] hmacSha256(String data, String key) {
|
return hmacSha256(data.getBytes(Charsets.UTF_8), key);
|
}
|
|
public static byte[] hmacSha256(final byte[] bytes, String key) {
|
return digestHMac("HmacSHA256", bytes, key);
|
}
|
|
public static String hmacSha256Hex(String data, String key) {
|
return hmacSha256Hex(data.getBytes(Charsets.UTF_8), key);
|
}
|
|
public static String hmacSha256Hex(final byte[] bytes, String key) {
|
return digestHMacHex("HmacSHA256", bytes, key);
|
}
|
|
public static String hmacSha384Hex(String data, String key) {
|
return hmacSha384Hex(data.getBytes(Charsets.UTF_8), key);
|
}
|
|
public static String hmacSha384Hex(final byte[] bytes, String key) {
|
return digestHMacHex("HmacSHA384", bytes, key);
|
}
|
|
public static String hmacSha512Hex(String data, String key) {
|
return hmacSha512Hex(data.getBytes(Charsets.UTF_8), key);
|
}
|
|
public static String hmacSha512Hex(final byte[] bytes, String key) {
|
return digestHMacHex("HmacSHA512", bytes, key);
|
}
|
|
public static String digestHMacHex(String algorithm, final byte[] bytes, String key) {
|
SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm);
|
try {
|
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
|
mac.init(secretKey);
|
return encodeHex(mac.doFinal(bytes));
|
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
|
throw Exceptions.unchecked(e);
|
}
|
}
|
|
public static byte[] digestHMac(String algorithm, final byte[] bytes, String key) {
|
SecretKey secretKey = new SecretKeySpec(key.getBytes(Charsets.UTF_8), algorithm);
|
try {
|
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
|
mac.init(secretKey);
|
return mac.doFinal(bytes);
|
} catch (InvalidKeyException | NoSuchAlgorithmException e) {
|
throw Exceptions.unchecked(e);
|
}
|
}
|
|
public static String encodeHex(byte[] bytes) {
|
StringBuilder r = new StringBuilder(bytes.length * 2);
|
for (byte b : bytes) {
|
r.append(HEX_CODE[(b >> 4) & 15]);
|
r.append(HEX_CODE[b & 15]);
|
}
|
return r.toString();
|
}
|
|
public static byte[] decodeHex(final String hexStr) {
|
return DatatypeConverterUtil.parseHexBinary(hexStr);
|
}
|
|
public static boolean slowEquals(@Nullable String a, @Nullable String b) {
|
if (a == null || b == null) {
|
return false;
|
}
|
return slowEquals(a.getBytes(Charsets.UTF_8), b.getBytes(Charsets.UTF_8));
|
}
|
|
public static boolean slowEquals(@Nullable byte[] a, @Nullable byte[] b) {
|
if (a == null || b == null || a.length != b.length) {
|
return false;
|
}
|
int diff = a.length ^ b.length;
|
for (int i = 0; i < a.length; i++) {
|
diff |= a[i] ^ b[i];
|
}
|
return diff == 0;
|
}
|
|
public static String hex(String data) {
|
if (StringUtil.isBlank(data)) {
|
return StringPool.EMPTY;
|
}
|
return sha1Hex(data);
|
}
|
|
public static String encrypt(String data) {
|
if (StringUtil.isBlank(data)) {
|
return StringPool.EMPTY;
|
}
|
return sha1Hex(md5Hex(data));
|
}
|
}
|