Adds a hashing function helper to be compatible with other languages
This commit is contained in:
parent
615049ab7b
commit
05970bdefc
1 changed files with 37 additions and 0 deletions
37
src/main/java/dev/zontreck/ariaslib/util/Hashing.java
Normal file
37
src/main/java/dev/zontreck/ariaslib/util/Hashing.java
Normal file
|
@ -0,0 +1,37 @@
|
|||
package dev.zontreck.ariaslib.util;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class Hashing
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* A md5 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 md5(String input) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue