package br.com.elotech.base.crypto;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;

public final class Crypto {

    private static String text;
    private final static String salt = "Elo!Tech3000";

    public synchronized static String setHash(String text)
    {
        if (text.equals(Crypto.text))
        {
            return text;
        }

        if (text == null || text.equals(""))
        {
            text = RandomStringUtils.randomAscii(20);
        }

        Crypto.text = DigestUtils.sha512Hex(text + salt);
        return Crypto.text;
    }

    /**
     * Check if a given password is correct.
     *
     * @param text
     * @return True is correct, else false.
     */
    public synchronized static boolean checkHash(String text,String hash)
    {
        return (hash.equals(DigestUtils.sha512Hex(text + salt)));
    }

}
 
