Add some extra hashing functions to the API
This commit is contained in:
parent
0bbb7ae630
commit
d0b6beb014
1 changed files with 83 additions and 0 deletions
|
@ -19,6 +19,89 @@ public class Hashing
|
||||||
|
|
||||||
byte[] byteData = md.digest();
|
byte[] byteData = md.digest();
|
||||||
|
|
||||||
|
// Convert the byte array to a hexadecimal string
|
||||||
|
StringBuilder hexString = new StringBuilder();
|
||||||
|
for (byte aByteData : byteData) {
|
||||||
|
String hex = Integer.toHexString(0xff & aByteData);
|
||||||
|
if (hex.length() == 1) {
|
||||||
|
hexString.append('0');
|
||||||
|
}
|
||||||
|
hexString.append(hex);
|
||||||
|
}
|
||||||
|
return hexString.toString();
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A md5 hashing function that is compatible with literally every other hashing function out there
|
||||||
|
* @param input The bytes to hash
|
||||||
|
* @return The hash
|
||||||
|
*/
|
||||||
|
public static String md5(byte[] input) {
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||||
|
md.update(input);
|
||||||
|
|
||||||
|
byte[] byteData = md.digest();
|
||||||
|
|
||||||
|
// Convert the byte array to a hexadecimal string
|
||||||
|
StringBuilder hexString = new StringBuilder();
|
||||||
|
for (byte aByteData : byteData) {
|
||||||
|
String hex = Integer.toHexString(0xff & aByteData);
|
||||||
|
if (hex.length() == 1) {
|
||||||
|
hexString.append('0');
|
||||||
|
}
|
||||||
|
hexString.append(hex);
|
||||||
|
}
|
||||||
|
return hexString.toString();
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A sha256 hashing function that is compatible with literally every other hashing function out there
|
||||||
|
* @param input The string to hash
|
||||||
|
* @return The hash
|
||||||
|
*/
|
||||||
|
public static String sha256(String input) {
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("SHA256");
|
||||||
|
md.update(input.getBytes());
|
||||||
|
|
||||||
|
byte[] byteData = md.digest();
|
||||||
|
|
||||||
|
// Convert the byte array to a hexadecimal string
|
||||||
|
StringBuilder hexString = new StringBuilder();
|
||||||
|
for (byte aByteData : byteData) {
|
||||||
|
String hex = Integer.toHexString(0xff & aByteData);
|
||||||
|
if (hex.length() == 1) {
|
||||||
|
hexString.append('0');
|
||||||
|
}
|
||||||
|
hexString.append(hex);
|
||||||
|
}
|
||||||
|
return hexString.toString();
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A sha256 hashing function that is compatible with literally every other hashing function out there
|
||||||
|
* @param input The bytes to hash
|
||||||
|
* @return The hash
|
||||||
|
*/
|
||||||
|
public static String sha256(byte[] input) {
|
||||||
|
try {
|
||||||
|
MessageDigest md = MessageDigest.getInstance("SHA256");
|
||||||
|
md.update(input);
|
||||||
|
|
||||||
|
byte[] byteData = md.digest();
|
||||||
|
|
||||||
// Convert the byte array to a hexadecimal string
|
// Convert the byte array to a hexadecimal string
|
||||||
StringBuilder hexString = new StringBuilder();
|
StringBuilder hexString = new StringBuilder();
|
||||||
for (byte aByteData : byteData) {
|
for (byte aByteData : byteData) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue