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
2021-07-16 14:35:13 -04:00

30 lines
896 B
Java

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
*
* @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());
byte[] digest = messageDigest.digest();
StringBuilder buffer = new StringBuilder();
for (byte b : digest)
buffer.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return buffer.toString();
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return null;
}
}