In my project, I have a rule that I need to sign the string of an XML that I have that is in base64 with the user's CPF ...
I have been searching, but I have not been able to understand how this signature works with .NET classes.
Well, I found this link here that subscribes to a string of an XML of NF-e. But I could not quite understand how that works, and how to adapt that code to my scenario.
What I have so far: I generate XML , I write to the file and I can generate the hash base64 of this string in>, but what I'm missing now is to sign this string with the person's CPF .The codes I have are:
//Cria o xml com as tags e faz o encoding para base64
var sb = new StringBuilder();
var settings = new XmlWriterSettings();
string cpf = "000.001.000-00";
using (var writer = XmlWriter.Create(sb, settings))
{
//Inicia o documetno xml
writer.WriteStartDocument();
//escreve o documento raiz
writer.WriteStartElement("no1");
//escreve os subelementos
writer.WriteElementString("no2", "valor");
//encerra o elemento raiz
writer.WriteEndElement();
//escreve o xml para o arquivo e encerra o objeto escritor
writer.Close();
}
//encoding do xml para base64
string s = EncodeTo64(sb.ToString());
//arquivo que vai ser usado para gerar a string base64
string caminho = parametros.Propriedades["ParPastaArquivoXML"].ToString();
caminho = caminho + "\" + "arquivo.xml";
File.WriteAllText(caminho, s);
Here you try to sign:
try
{
// Create a new CspParameters object to specify
// a key container.
CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = cpf;
// Create a new RSA signing key and save it in the container.
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
// Create a new XML document.
XmlDocument xmlDoc = new XmlDocument();
// Load an XML file into the XmlDocument object.
xmlDoc.PreserveWhitespace = false;
xmlDoc.Load(caminho);
// Sign the XML document.
SignXml(xmlDoc, rsaKey);
Console.WriteLine("XML file signed.");
// Save the document.
xmlDoc.Save(caminhoAssinado);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Here's the method to sign:
// Sign an XML file.
// This document cannot be verified unless the verifying
// code has the key with which it was signed.
public static void SignXml(XmlDocument xmlDoc, RSA Key)
{
// Check arguments.
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (Key == null)
throw new ArgumentException("Key");
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}
The example XML that is created follows:
<?xml version="1.0" encoding="utf-16"?>
<no1>
<no2>021303</no2>
</no1>
Until that time I can generate the base64 hash .
But now, how do I sign this hash with the CPF ?