Created
April 11, 2019 18:18
-
-
Save daniloguimaraes/bfe93a270b42d1456b4ecdddf5ec078b to your computer and use it in GitHub Desktop.
Geração da tag 'hashCSRT' do Responsável Técnico da NF-e (NT 2018-005 v1.20)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.oobj.responsaveltecnico; | |
// https://mvnrepository.com/artifact/commons-codec/commons-codec | |
import org.apache.commons.codec.digest.DigestUtils; | |
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 | |
import org.apache.commons.lang3.StringUtils; | |
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.Base64; | |
/** | |
* Utilitario para calcular a String em Base64 do hashing do Responsável Tecnico, conforme NT 2018-005. | |
* | |
* @author Danilo Guimaraes | |
* @since 20/03/2019 | |
*/ | |
public final class ResponsavelTecnico { | |
private static final Logger LOG = Logger.getLogger(ResponsavelTecnico.class); | |
private ResponsavelTecnico() { | |
// Classe utilitaria | |
} | |
/** | |
* Gera a String em Base64, com base no CSRT + Chave de Acesso, que devera ser preenchida na tag {@code hashCSRT}. | |
* | |
* @param csrt o CSRT cadastrado na Sefaz | |
* @param chaveAcesso a chave de acesso do documento | |
* @return a String Base64 que devera ser preenchida na tag {@code hashCSRT}. | |
*/ | |
public static String calcularHashCsrtBase64(String csrt, String chaveAcesso) { | |
if (StringUtils.isBlank(csrt)) { | |
throw new IllegalArgumentException("O CSRT nao pode ser em branco ou nulo"); | |
} | |
if (StringUtils.isBlank(chaveAcesso)) { | |
throw new IllegalArgumentException("A Chave de Acesso nao pode ser nula"); | |
} | |
LOG.info("Gerando hashCSRT para a Chave de Acesso {}", chaveAcesso); | |
final String data = csrt + chaveAcesso; | |
final String base64Result = new String(Base64.getEncoder().encode(DigestUtils.sha1(data))); | |
LOG.info("Resultado do hashCSRT (Base64): {}", base64Result); | |
return base64Result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.oobj.responsaveltecnico; | |
// https://mvnrepository.com/artifact/junit/junit | |
import static org.junit.Assert.assertEquals; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.ExpectedException; | |
/** | |
* Testes unitarios da classe {@link ResponsavelTecnico}. | |
* | |
* @author Danilo Guimaraes | |
* @since 20/03/2019 | |
*/ | |
public class ResponsavelTecnicoTest { | |
private String csrt = "G8063VRTNDMO886SFNK5LDUDEI24XJ22YIPO"; | |
private String chaveAcesso = "41180678393592000146558900000006041028190697"; | |
@Test | |
public void testGeracaoHashCsrtBase64ConformeExemploDaSefazDeveRetornarOValorEsperado() { | |
String expected = "aWv6LeEM4X6u4+qBI2OYZ8grigw="; | |
String actual = ResponsavelTecnico.calcularHashCsrtBase64(csrt, chaveAcesso); | |
assertEquals(expected, actual); | |
} | |
@Rule | |
public ExpectedException expectedException = ExpectedException.none(); | |
@Test | |
public void testGeracaoHashCsrtBase64PassandoCsrtNuloDeveLancarIllegalArgumentException() { | |
expectedException.expect(IllegalArgumentException.class); | |
expectedException.expectMessage("O ID do CSRT nao pode ser em branco ou nulo"); | |
ResponsavelTecnico.calcularHashCsrtBase64(null, chaveAcesso); | |
} | |
@Test | |
public void testGeracaoHashCsrtBase64PassandoChaveAcessoNuloDeveLancarIllegalArgumentException() { | |
expectedException.expect(IllegalArgumentException.class); | |
expectedException.expectMessage("A Chave de Acesso nao pode ser nula"); | |
ResponsavelTecnico.calcularHashCsrtBase64(csrt, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment