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