package br.com.elotech.util;

import br.com.elotech.model.Registro;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

public abstract class Crypto {

    private static final String SALT = "Elo!Tech3000";
    public static String geraHash(Registro registro){
        String hash = null;
        if(registro != null){
            hash = DigestUtils.sha512Hex(calculaHash(registro));
        }
        return hash;
    }

    public static String calculaHash(Registro registro){
        StringBuilder builder = new StringBuilder();
        builder.append(registro.getValidade().getTime())
               .append(registro.getModulo())
                .append(StringUtils.deleteWhitespace(registro.getNome()))
                .append(registro.getCodigo())
                .append(SALT);
        return builder.toString();
    }

    public static synchronized  boolean checkHash(Registro registro){
        return geraHash(registro).equals(registro.getHash());
    }
}
