io.lsn.spring.auth.HashGenerator Maven / Gradle / Ivy
package io.lsn.spring.auth;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author Patryk Szlagowski
*/
public class HashGenerator {
/**
* encode plain string by md5
* @param input
* @return
* @throws NoSuchAlgorithmException
*/
public static String md5(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
return hash(md, input);
}
/**
* encode plain string by sha256
* @param input
* @return
* @throws NoSuchAlgorithmException
*/
public static String sha256(String input) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
return hash(md, input);
}
private static String hash(MessageDigest md, String input) {
byte[] digest = md.digest(input.getBytes());
return new BigInteger(1, digest).toString(16);
}
}