This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
McGamerZone/commons/src/main/java/zone/themcgamer/common/HashUtils.java

30 lines
896 B
Java
Raw Permalink Normal View History

2021-02-19 20:11:08 +00:00
package zone.themcgamer.common;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author Braydon
*/
public class HashUtils {
/**
* Encrypt the given {@link String} as SHA-256
2021-05-02 18:54:10 +00:00
*
2021-02-19 20:11:08 +00:00
* @param s the string to encrypt
* @return the encrypted string
*/
public static String encryptSha256(String s) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(s.getBytes());
2021-07-16 18:35:13 +00:00
byte[] digest = messageDigest.digest();
StringBuilder buffer = new StringBuilder();
2021-02-19 20:11:08 +00:00
for (byte b : digest)
buffer.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return buffer.toString();
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return null;
}
}