not throw NoSuchAlgorithmException in Digest
This commit is contained in:
@ -15,73 +15,89 @@ import java.security.NoSuchAlgorithmException;
|
||||
*/
|
||||
public class Digest {
|
||||
|
||||
public static String md5(String data) throws NoSuchAlgorithmException {
|
||||
public static String md5(String data) {
|
||||
return md5(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String md5(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String md5(String data, Charset charset) {
|
||||
return md5(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String md5(byte[] data) throws NoSuchAlgorithmException {
|
||||
if (data == null || data.length == 0) {
|
||||
return null;
|
||||
public static String md5(byte[] data) {
|
||||
try {
|
||||
if (data == null || data.length == 0) {
|
||||
return null;
|
||||
}
|
||||
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||||
md5.update(data);
|
||||
byte[] bytes = md5.digest();
|
||||
char[] chars = new char[bytes.length * 2];
|
||||
for (int i = 0, j = 0; i < bytes.length; i++) {
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] >>> 4 & 0xF];
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] & 0xF];
|
||||
}
|
||||
return new String(chars);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported md5 digest");
|
||||
}
|
||||
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||||
md5.update(data);
|
||||
byte[] bytes = md5.digest();
|
||||
char[] chars = new char[bytes.length * 2];
|
||||
for (int i = 0, j = 0; i < bytes.length; i++) {
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] >>> 4 & 0xF];
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] & 0xF];
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
public static String sha1(String data) throws NoSuchAlgorithmException {
|
||||
public static String sha1(String data) {
|
||||
return sha1(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String sha1(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String sha1(String data, Charset charset) {
|
||||
return sha1(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String sha1(byte[] bytes) throws NoSuchAlgorithmException {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
public static String sha1(byte[] bytes) {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha1 digest");
|
||||
}
|
||||
}
|
||||
|
||||
public static String sha256(String data) throws NoSuchAlgorithmException {
|
||||
public static String sha256(String data) {
|
||||
return sha256(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String sha256(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String sha256(String data, Charset charset) {
|
||||
return sha256(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String sha256(byte[] bytes) throws NoSuchAlgorithmException {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
public static String sha256(byte[] bytes) {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha256 digest");
|
||||
}
|
||||
}
|
||||
|
||||
public static String sha512(String data) throws NoSuchAlgorithmException {
|
||||
public static String sha512(String data) {
|
||||
return sha512(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String sha512(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String sha512(String data, Charset charset) {
|
||||
return sha512(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String sha512(byte[] bytes) throws NoSuchAlgorithmException {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-512");
|
||||
BigInteger number = new BigInteger(1, sha.digest(bytes));
|
||||
StringBuilder result = new StringBuilder(number.toString(16));
|
||||
while (result.length() < 64) {
|
||||
result.insert(0, "0");
|
||||
public static String sha512(byte[] bytes) {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-512");
|
||||
BigInteger number = new BigInteger(1, sha.digest(bytes));
|
||||
StringBuilder result = new StringBuilder(number.toString(16));
|
||||
while (result.length() < 64) {
|
||||
result.insert(0, "0");
|
||||
}
|
||||
return result.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha512 digest");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static String argon2id(String password) {
|
||||
|
||||
Reference in New Issue
Block a user