All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.lsn.spring.auth.HashGenerator Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy