My algorithm generates a SHA512 Hash displays an unexpected result

Asked

Viewed 37 times

1

I have the following string:

<Rps><InfDeclaracaoPrestacaoServicoId="1"><Rps><IdentificacaoRps><Numero>4</Numero><Serie>aaa</Serie><Tipo>1</Tipo></IdentificacaoRps><DataEmissao>2019-07-08</DataEmissao><Status>1</Status></Rps><Competencia>2019-07-08</Competencia><Servico><Valores><ValorServicos>2358.77</ValorServicos><ValorDeducoes>0</ValorDeducoes><ValorPis>0</ValorPis><ValorCofins>0</ValorCofins><ValorInss>0</ValorInss><ValorIr>0</ValorIr><ValorCsll>0</ValorCsll><OutrasRetencoes>0</OutrasRetencoes><Aliquota>0</Aliquota><DescontoIncondicionado>0</DescontoIncondicionado><DescontoCondicionado>0</DescontoCondicionado></Valores><IssRetido>1</IssRetido><ResponsavelRetencao>1</ResponsavelRetencao><ItemListaServico>07.02</ItemListaServico><Discriminacao>TESTANDORPSDISCRIMINAO</Discriminacao><CodigoMunicipio>3526902</CodigoMunicipio></Servico><Prestador><CpfCnpj><Cnpj>88888888888888</Cnpj></CpfCnpj><InscricaoMunicipal>123456</InscricaoMunicipal></Prestador><TomadorServico><IdentificacaoTomador><CpfCnpj><Cnpj>55555555555555</Cnpj></CpfCnpj></IdentificacaoTomador><RazaoSocial>Dorotheo</RazaoSocial><Endereco><Endereco>RuaJapo</Endereco><Numero>2</Numero><Complemento>TESTERPS</Complemento><Bairro>Jaragua</Bairro><CodigoMunicipio>3550704</CodigoMunicipio><Uf>SP</Uf><Cep>11600318</Cep></Endereco><Contato><Telefone>12345678901</Telefone><Email>[email protected]</Email></Contato><AtualizaTomador>2</AtualizaTomador><TomadorExterior>2</TomadorExterior></TomadorServico><InformacoesComplementares>TESTANDORPS</InformacoesComplementares></InfDeclaracaoPrestacaoServico></Rps>TLXX4JN38KXTRNSEAJYYEA==

And I must get as Hash:

61AEC2215401D0099D85D70A56D72949860CA07C55620C37B49F8F2DA7CF9A671AFAC6C96D95BD74F9304B97CEBC6A90CDF9F7134B2A5F41A12629F7D6111BA1

(You can check on that website)

I did the following function :

public string HashIntegridade(string Dados)
{
    SHA512 shaM = SHA512.Create();
   
    var hashIntegridadeBytes = shaM.ComputeHash(Encoding.UTF8.GetBytes(Dados));
    return Encoding.UTF8.GetString(hashIntegridadeBytes);      
}

I have tested different variations of this function, trying to use Encoding.ASCII.GetBytes and other types of encoding and never get the expected result.

The result I’m getting a��!T\u0001�\t���\nV�)I�\f�|Ub\f7���-�Ϛg\u001a���m��t�0K�μj����\u0013K*_A�&)��\u0011\u001b�.

1 answer

2


The method ComputeHash returns an array of bytes.

When using Encoding.UTF8.GetString, you are trying to turn these bytes into a text, using UTF-8 encoding (related reading, if you want to understand in more detail). But that is not what should be done, because the bytes resulting from a hash will not necessarily form a coherent text (if it is, it is by coincidence).

What the site you tested returns is a hexadecimal representation of bytes. In this case, the first byte is 61 (the value 61 in hexadecimal, which in UTF-8 becomes the letter a, but this is irrelevant, because a hash results in bytes and not text, whether or not that byte corresponds to a character is something totally circumstantial). The second byte is AE (which in UTF-8 is not recognized), and so on.

Finally, to solve, just turn the byte array into a string containing its hexadecimal representation. One option is to use BitConverter:

public string HashIntegridade(string Dados)
{
    SHA512 shaM = SHA512.Create();
    
    var hashIntegridadeBytes = shaM.ComputeHash(Encoding.UTF8.GetBytes(Dados));
    return BitConverter.ToString(hashIntegridadeBytes).Replace("-","");
}

Or use a StringBuilder and go converting bytes one to one:

public string HashIntegridade(string Dados)
{
    SHA512 shaM = SHA512.Create();
    
    var hashIntegridadeBytes = shaM.ComputeHash(Encoding.UTF8.GetBytes(Dados));
    StringBuilder hex = new StringBuilder(hashIntegridadeBytes.Length * 2);
    foreach (byte b in hashIntegridadeBytes)
        hex.AppendFormat("{0:X2}", b);
    return hex.ToString();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.