not throw NoSuchAlgorithmException in Digest
This commit is contained in:
@ -15,73 +15,89 @@ 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) {
|
||||||
if (data == null || data.length == 0) {
|
try {
|
||||||
return null;
|
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);
|
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) {
|
||||||
MessageDigest sha = MessageDigest.getInstance("SHA");
|
try {
|
||||||
sha.update(bytes);
|
MessageDigest sha = MessageDigest.getInstance("SHA");
|
||||||
return Text.byteToHex(sha.digest());
|
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);
|
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) {
|
||||||
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
try {
|
||||||
sha.update(bytes);
|
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||||
return Text.byteToHex(sha.digest());
|
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);
|
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) {
|
||||||
MessageDigest sha = MessageDigest.getInstance("SHA-512");
|
try {
|
||||||
BigInteger number = new BigInteger(1, sha.digest(bytes));
|
MessageDigest sha = MessageDigest.getInstance("SHA-512");
|
||||||
StringBuilder result = new StringBuilder(number.toString(16));
|
BigInteger number = new BigInteger(1, sha.digest(bytes));
|
||||||
while (result.length() < 64) {
|
StringBuilder result = new StringBuilder(number.toString(16));
|
||||||
result.insert(0, "0");
|
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) {
|
public static String argon2id(String password) {
|
||||||
|
|||||||
Reference in New Issue
Block a user