I am signing the following string: 339575410000100000000000120150413NI00000000000005000000000000002502917N207293716000260
Using the following code:
' Obtem o certificado
Dim CertificadoDig As X509Certificate2 = ObtemCertificado("")
' Converte os dados ASCII para Bytes
Dim data() As Byte = System.Text.Encoding.ASCII.GetBytes(String_AssADC)
Dim csp As RSACryptoServiceProvider = DirectCast(CertificadoDig.PrivateKey, RSACryptoServiceProvider)
'Gerando Hash(array de bytes) utilizando SHA1
Dim sha As New SHA1Managed()
Dim hash() As Byte = sha.ComputeHash(data)
'Assinando o HASH(array de bytes) utilizando RSA-SHA1
Dim encrypted As Byte() = csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"))
'Verifica se a Assinatura é Valida
Dim isValid As Boolean = csp.VerifyData(data, "SHA1", encrypted)
Return Convert.ToBase64String(encrypted)
I wonder if when I generate Hash, does it already sign? Well I have seen that it can happen, and if this happens I will be signing 2 times and I believe that there must be the error, the WebService of the City of São Paulo, returns with the following message:
<?xml version="1.0" encoding="UTF-8" ?>
<RetornoEnvioRPS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.prefeitura.sp.gov.br/nfe">
<Cabecalho Versao="1" xmlns="">
<Sucesso>false</Sucesso>
</Cabecalho>
<Erro xmlns="">
<Codigo>1057</Codigo>
<Descricao>Rejeição: Assinatura difere do calculado.</Descricao>
</Erro>
</RetornoEnvioRPS>