Version 9.2 of FirmaSAT has improvements that make it easier to work entirely in memory.
The improvements provide work-arounds to solve problems storing UTF-8-encoded data in .NET and VBA UTF-16 strings (called "Unicode" strings in older Microsoft documentation).
Read in file from disk to a string | Read in key and certificate data to a string | Signing XML data entirely in memory | Example code | What's the issue here? | Contact us
The following code now works properly, allowing you to read in an XML file directly into a string and then pass that string as a parameter to other FirmaSAT functions.
string xmlStr = File.ReadAllText(xmlFile, Encoding.UTF8); n = Sat.Validate(xmlStr);
Improvement in v9.2: In earlier versions, if xmlFile contains characters like "ó" in Nombre="Esta es una demostración"
,
the string read in by File.ReadAllText would cause an "encoding error" when passed to a FirmaSAT function. This is now fixed in version 9.2.
In VBA/VB6, use the satAsciify
function to avoid UTF-8 encoding issues.
(satAsciify
is a wrapper around the function SAT_Asciify
.)
Dim fname As String Dim xmlstring As String Dim n As Long fname = "cfdv33a-nomina12B.xml" xmlstring = satAsciify(fname) n = SAT_ValidateXml(xmlstring, 0) Debug.Assert (0 = n)
Improvement in v9.2: In earlier versions, the SAT_Asciify
function
would work for almost all input files, converting all non-ASCII characters to XML numeric character references.
This would not work for cases like Año="2016"
and
Antigüedad="P3Y2M23D"
where the attribute name contained a non-ASCII character like "ñ" or "ü".
This is now fixed in version 9.2.
Here is how to read in private key and X.509 certificate files to strings that can be used directly in FirmaSAT functions.
string keyStr = Sat.GetKeyAsString("emisor.key", password, KeyOption.EncryptedPEM); string cerStr = Sat.GetCertAsString("emisor.cer");
Dim keyfiledata As String Dim certfiledata As String keyfiledata = satGetKeyAsPEMString("emisor.key", password) certfiledata = satGetCertAsString("emisor.cer")
Alternatively, you can hard-code the data.
string keyStr = "-----BEGIN ENCRYPTED PRIVATE KEY-----" + "MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQI5qDMtGWYa2wCAggA" + "MBQGCCqGSIb3DQMHBAhFAqj+c0f8JASCBMhNUpNUp57vMu8L3LHBKRBTFl0VE3oq" + ...[cut]... "EsYulVdcXWzCF58HFQjUoDon0e/QMukS0eNgq9ipmoKAWKyy7+TQw7Xx3MmqkGlL" + "HGM=" + "-----END ENCRYPTED PRIVATE KEY-----"; string cerStr = "-----BEGIN CERTIFICATE-----" + "MIIF+TCCA+GgAwIBAgIUMzAwMDEwMDAwMDAzMDAwMjM3MDgwDQYJKoZIhvcNAQELBQAwggFmMSAwHgY" + "DVQQDDBdBLkMuIDIgZGUgcHJ1ZWJhcyg0MDk2KTEvMC0GA1UECgwmU2VydmljaW8gZGUgQWRtaW5pc3" + ...[cut]... "DsUb0qP42QCGBiTUseGugAzqOP6EYpVPC73gFourmdBQgfayaEvi3xjNanFkPlW1XEYNrYJB4yNjphF" + "rvWwTY86vL2o8gZN0Utmc5fnoBTfM9r2zVKmEi6FUeJ1iaDaVNv47te9iS1ai4V4vBY8r" + "-----END CERTIFICATE-----";
Note that the private key data stored in the string beginning "BEGIN ENCRYPTED PRIVATE KEY"
is still safely encrypted.
With all the necessary data now stored in memory, we can sign the XML document. Use the .NET method Sat.SignXmlToBytes or the VBA/VB6 function satSignXmlToBytes (the preferred wrapper for SAT_SignXmlToString).
byte[] xmlArr = System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(xmlStr); // Sign XML document with all parameters passed in memory byte[] xmlArrSigned = Sat.SignXmlToBytes(xmlArr, keyStr, password, cerStr, 0);
Dim xmlbytes() As Byte xmlbytes = satSignXmlToBytes(xmlstring, keyfiledata, password, certfiledata, 0)
You can see the above examples used in the .NET test code TestFirmaSat.cs and TestFirmaSat.vb, and the VBA/VB6 test code TestFirmaSat.bas. The test code source files are provided in the distribution.
byte[] arr;
Dim arr() as Byte
File.ReadAllText()
and will automatically correct it if passed as an argument.System.Text.Encoding.UTF8.GetBytes()
and System.Text.Encoding.UTF8.GetString()
to convert between a .NET Unicode string and an array of bytes encoded in UTF-8.
To contact us or comment on this page, please send us a message.
This page last updated 15 August 2025