FirmaSAT Namespace

A .NET interface for FirmaSAT

Classes

Class Description
General General utilities.
Sat SAT-related methods.
Tfd Methods for Timbre Fiscal Digital (TFD).

Enumerations

Enumeration Description
KeyFormat Format for saved key files.
KeyOption Options for key output in Sat.GetKeyAsString().
PfxFormat Options for PKCS12 (PFX) output in Sat.WritePfxFile().
Query Options for certificate query.
SignOptions Options for signing XML.
XmlOption Options for XML validation.

General Class

General utilities.

Inheritance Hierarchy

System.Object
FirmaSAT.General

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public class General

The General type exposes the following members.

Methods

Name Description
Comments Get additional information about the core DLL module.
CompileTime Get date and time the core DLL module was last compiled.
ErrorLookup Look up error code.
FormatErrorMessage Return an error message string for the last error.
LastError Retrieve the last error message.
LicenceType Get licence type.
ModuleName Get full path name of core diFirmaSat DLL module.
Platform Get the platform for which the core native DLL was compiled.
Version Return version number of core diFirmaSat DLL.

See Also

FirmaSAT Namespace

General.Comments Method

Get additional information about the core DLL module.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string Comments()

Return Value

String
Comments string

Example

Console.WriteLine(General.Comments());
// Licensed Developer Edition | Edicion de Desarrollador Licenciado.

See Also

General Class
FirmaSAT Namespace

General.CompileTime Method

Get date and time the core DLL module was last compiled.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string CompileTime()

Return Value

String
Date and time string.

Example

Console.WriteLine(General.CompileTime());  // Feb 28 2021 18:29:46

See Also

General Class
FirmaSAT Namespace

General.ErrorLookup Method

Look up error code.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string ErrorLookup(
    int errCode
)

Parameters

errCode Int32

Code number (may be positive or negative)

Return Value

String
Corresponding error message

Example

Console.WriteLine(General.ErrorLookup(-10));
// Required data not found/Datos necesarios no encontrados (MISSING_ERROR)

See Also

General Class
FirmaSAT Namespace

General.FormatErrorMessage Method

Return an error message string for the last error.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string FormatErrorMessage(
    int errCode = 0
)

Parameters

errCode Int32 (Optional)

Error code returned by last call (optional)

Return Value

String
Error message as a string Error ({errCode}): {errorlookup}[: {lasterror}]

Example

// Error (-28): XML restriction is violated/XML restriccion es violada (XML_FACET_ERROR):
// Bad attribute/atributo mal [iedu:instEducativas/@CURP] (line 30):
// 'JUAN01010101GTOHMD0' is too long/es demasiado largo, maximum length/longitud maxima=18

See Also

General Class
FirmaSAT Namespace

General.LastError Method

Retrieve the last error message.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string LastError()

Return Value

String
Error message from last call (may be empty)

Remarks

Call this immediately when an error is indicated. Calling another method will overwrite the message. This is the same as Sat.LastError().

Example

int n = Sat.ValidateXml("cfdv33a-badspec.xml");
Console.WriteLine("Sat.ValidateXml returns {0}", n);
// Sat.ValidateXml returns -27
Console.WriteLine(General.ErrorLookup(n));
// Invalid XML format/No valido formato XML (BAD_XML_ERROR)
Console.WriteLine(General.LastError());
// XML validation error/Error al validar XML: Required attribute 'Importe' missing for element 'cfdi:Concepto' (Line/numero de linea: 12); 
// XML validation error/Error al validar XML: Required attribute 'Importe' missing for element 'cfdi:Concepto' (Line/numero de linea: 13)

See Also

General Class
FirmaSAT Namespace
FormatErrorMessage(Int32)

Sat.LastError Method

Retrieve the last error message.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string LastError()

Return Value

String
Error message from last call (may be empty)

Remarks

Call this immediately when an error is indicated. Calling another method will overwrite the message. This is the same as General.LastError().

Example

// Attempt to sign but input file does not comply with CFDi specifications
int n = satSignXml("cfdv33a-out.xml", "cfdv33a-badspec.xml", "emisor.key", "12345678a", "emisor.cer");
Console.WriteLine("Error {0}: {1}", n, Sat.ErrorLookup(n));
// Error -10: Required data not found/Datos necesarios no encontrados (MISSING_ERROR)
Console.WriteLine(Sat.LastError());
// Attribute 'Importe' is mandatory/Atributo es obligatorio

See Also

Sat Class
FirmaSAT Namespace
FormatErrorMessage(Int32)

General.LicenceType Method

Get licence type.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static char LicenceType()

Return Value

Char
D=Developer T=Trial

See Also

General Class
FirmaSAT Namespace

General.ModuleName Method

Get full path name of core diFirmaSat DLL module.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string ModuleName()

Return Value

String
File path.

Example

Console.WriteLine(General.ModuleName());  // C:\WINDOWS\SYSTEM32\diFirmaSAT2.dll

See Also

General Class
FirmaSAT Namespace

General.Platform Method

Get the platform for which the core native DLL was compiled.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string Platform()

Return Value

String
"Win32" or "Win64"

See Also

General Class
FirmaSAT Namespace

General.Version Method

Return version number of core diFirmaSat DLL.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int Version()

Return Value

Int32
Version number in form Major*10000 + Minor*100 + Release

Remarks

For example, version 9.2.15 returns 90215

Example

int n = General.Version();
Console.WriteLine("Version = {0}", n);  // Version = 90215

See Also

General Class
FirmaSAT Namespace

Sat Class

SAT-related methods.

Inheritance Hierarchy

System.Object
FirmaSAT.Sat

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public class Sat

The Sat type exposes the following members.

Methods

Name Description
Asciify(Byte[]) Replace any non-ASCII characters in an XML document (as a byte array) with XML character references (where permitted).
Asciify(String) Replace non-ASCII characters in an XML document with XML numeric character references (where permitted).
CheckKeyAndCert Verify that the public key in an X.509 certificate matches the private key.
DefaultDigestAlg Return default digest (hash) algorithm for the XML document type.
ExtractDigestFromSignature Extract message digest from the signature (sello) in an XML file.
FixBom Add UTF-8 byte order mark (BOM) to a file if not already present.
GetCertAsString Get certificate data as a base64 string.
GetCertExpiry Get expiry date of the X.509 certificate in ISO time format.
GetCertNumber Get serial number of the X.509 certificate in “special” SAT format.
GetCertStart Get start date of the X.509 certificate in ISO time format.
GetKeyAsString Get private key data as a base64 string suitable for a llaveCertificado element in a Cancelacion XML document
GetXmlAttribute Extract attribute data from an XML file.
InsertCert Insert certificate information into an XML document and output to a new file.
InsertCertToBytes(Byte[], String) Insert certificate information into an XML document (as a byte array) and output to memory.
InsertCertToBytes(String, String) Insert certificate information into an XML document (as a file) and output to memory.
LastError Retrieve the last error message.
MakeDigestFromXml Form message digest of piped string (cadena) from an XML file.
MakePipeStringFromXml Create the piped string (cadena) from an XML file.
MakeSignatureFromXml Create signature as a base64 string from data in an XML file ready to be inserted as a sello node.
NewKeyFile Save keyfile with a new password.
QueryCert Query an X.509 certificate.
SetXmlNoMatch Modify the string returned when GetXmlAttribute(String, String, String) fails to find a match.
SignXml Sign an XML file
SignXmlEx Obsolete.
Sign an XML file with extended options [deprecated].
SignXmlToBytes(Byte[], String, String, String, SignOptions) Sign XML data writing output to a byte array.
SignXmlToBytes(String, String, String, String, SignOptions) Sign XML file writing output to a byte array.
Uuid Generate a Universally Unique IDentifier (UUID) compliant with RFC 4122.
ValidateXml Validate an XML file against SAT specifications.
VerifySignature Verify the signature (sello) in an XML file.
WritePfxFile Create PFX (PKCS-12) file in PEM format suitable for a Cancelación.
XmlNoMatch Error message returned when GetXmlAttribute(String, String, String) fails to find a match
XmlReceiptVersion Find version number of Comprobante element or ID number for other document types.

See Also

FirmaSAT Namespace

Sat.Asciify(Byte[]) Method

Replace any non-ASCII characters in an XML document (as a byte array) with XML character references (where permitted).

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Overloads

Name Description
Asciify(Byte[]) Replace any non-ASCII characters in an XML document (as a byte array) with XML character references (where permitted).
Asciify(String) Replace non-ASCII characters in an XML document with XML numeric character references (where permitted).

Syntax

public static string Asciify(
    byte[] xmlData
)

Parameters

xmlData Byte[]

Byte array containing XML data

Return Value

String
XML document as a string with non-ASCII characters replaced by XML character references.

Remarks

(bytes) –> (string)

Example

byte[] b = File.ReadAllBytes("cfdv40-ejemplo.xml");
string s = Sat.Asciify(b);
Console.WriteLine(s);
// <?xml version="1.0"?>
// <cfdi:Comprobante ... >
//   <cfdi:Emisor Rfc=" AAA010101AAA" Nombre="Esta es una demostraci&#xF3;n" RegimenFiscal="622"></cfdi:Emisor>
// ... [etc]

See Also

Sat Class
FirmaSAT Namespace

Sat.Asciify(String) Method

Replace non-ASCII characters in an XML document with XML numeric character references (where permitted).

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Overloads

Name Description
Asciify(Byte[]) Replace any non-ASCII characters in an XML document (as a byte array) with XML character references (where permitted).
Asciify(String) Replace non-ASCII characters in an XML document with XML numeric character references (where permitted).

Syntax

public static string Asciify(
    string xmlFile
)

Parameters

xmlFile String

Name of XML file (or a string containing XML data)

Return Value

String
XML document as a string with non-ASCII characters replaced by XML numeric character references.

Remarks

In almost all cases, the output contains only US-ASCII characters and can safely be used as input to other functions without concern for character encoding issues. For example, the character “ó” (U+00F3 LATIN SMALL LETTER O WITH ACUTE) is replaced by the XML character reference “ó”.

In certain cases, some characters in an XML document cannot be replaced by a numeric character reference, for example where they are used in an element or attribute name, such as Año="2016". In these cases, they are left as UTF-8-encoded characters.

Example

Console.WriteLine(Sat.Asciify("cfdv40-ejemplo.xml");
// <?xml version="1.0" ?>
// <cfdi:Comprobante ... >
//   <cfdi:Emisor Rfc=" AAA010101AAA" Nombre="Esta es una demostraci&#xF3;n" RegimenFiscal="622"></cfdi:Emisor>
// ... [etc]

See Also

Sat Class
FirmaSAT Namespace

Sat.CheckKeyAndCert Method

Verify that the public key in an X.509 certificate matches the private key.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int CheckKeyAndCert(
    string keyFile,
    string password,
    string certFile
)

Parameters

keyFile String

Name of private key file

password String

Password for encrypted private key

certFile String

X.509 certificate file or XML document with embedded certificado node

Return Value

Int32
Zero (0) if keys match or a negative error code.

Example

int n = Sat.CheckKeyAndCert("emisor.key", "12345678a", "emisor.cer");
Console.WriteLine("Sat.CheckKeyAndCert returns {0}", n);
// Sat.CheckKeyAndCert returns 0

// Cert and key do not match ...
n = Sat.CheckKeyAndCert("emisor.key", "12345678a", "pac.cer");
Console.WriteLine("Sat.CheckKeyAndCert returns {0}", n);
// Sat.CheckKeyAndCert returns -21
Console.WriteLine(General.ErrorLookup(n));
// Match not found/No se pudo encontrar datos coincidente (NO_MATCH_ERROR)
Console.WriteLine(General.LastError());
// Private key does not match key in certificate/La clave privada no coincide con la clave del certificado

See Also

Sat Class
FirmaSAT Namespace

Sat.DefaultDigestAlg Method

Return default digest (hash) algorithm for the XML document type.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string DefaultDigestAlg(
    string xmlFile
)

Parameters

xmlFile String

Name of XML file

Return Value

String
“SHA-1” or “SHA-256”; or “!ERROR!” if an error occurred.

Example

Console.WriteLine(Sat.DefaultDigestAlg("cfdv40-ejemplo.xml"));  // SHA-256

See Also

Sat Class
FirmaSAT Namespace

Sat.ExtractDigestFromSignature Method

Extract message digest from the signature (sello) in an XML file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string ExtractDigestFromSignature(
    string xmlFile,
    string certFile = ""
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

certFile String (Optional)

X.509 certificate file (optional) to override embedded Certificado node.

Return Value

String
Message digest in hex format or empty string on error

Remarks

This is the actual digest value computed over the original document and used to sign it. To re-compute the digest value over the current document, use MakeDigestFromXml(String).

Example

Console.WriteLine(Sat.ExtractDigestFromSignature("cfdv40-ejemplo-signed-tfd.xml"));
// 0FF1274E51FBB090489588D832BB1B5B36543302DECBB0A5490839B8C99E8755

See Also

Sat Class
FirmaSAT Namespace

Sat.FixBom Method

Add UTF-8 byte order mark (BOM) to a file if not already present.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int FixBom(
    string outputFile,
    string inputFile
)

Parameters

outputFile String

Name of output file to be created with BOM.

inputFile String

Name of input file.

Return Value

Int32
Zero (0) on success or a negative error code.

Remarks

It is an error if the input file contains invalid UTF-8 characters.

Example

int n = Sat.FixBom("cfdv40_new-signed-with-BOM.xml", "cfdv40-signed-nobom.xml");
Console.WriteLine("Sat.FixBom returns {0}", n);
// Read in new file as bytes, truncate, and display first 3 bytes: expecting EF-BB-BF
byte[] b = File.ReadAllBytes("cfdv40_new-signed-with-BOM.xml");
Array.Resize(ref b, 3);
Console.WriteLine(BitConverter.ToString(b));
// EF-BB-BF

See Also

Sat Class
FirmaSAT Namespace

Sat.GetCertAsString Method

Get certificate data as a base64 string.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string GetCertAsString(
    string fileName
)

Parameters

fileName String

X.509 certificate file or XML file with Certificado node

Return Value

String
Certificate data as a string of base64 characters

Remarks

Use to obtain the value for the Certificado node from a .CER file. If input is an XML file, this is equivalent to Sat.GetXmlAttribute(fileName, "Certificado", "Comprobante")

Example

Console.WriteLine(Sat.GetCertAsString("emisor.cer"));
// MIIF+TCCA+GgAwIBAgIUMzAwMDEwMDAwMDAzMDAw ...

See Also

Sat Class
FirmaSAT Namespace

Sat.GetCertExpiry Method

Get expiry date of the X.509 certificate in ISO time format.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string GetCertExpiry(
    string fileName
)

Parameters

fileName String

X.509 certificate file or XML file with certificado node

Return Value

String
Certificate expiry date

Example

Console.WriteLine(Sat.GetCertExpiry("emisor.cer"));
// 2021-05-18T03:54:56Z

See Also

Sat Class
FirmaSAT Namespace

Sat.GetCertNumber Method

Get serial number of the X.509 certificate in “special” SAT format.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string GetCertNumber(
    string fileName
)

Parameters

fileName String

X.509 certificate file or XML file with certificado node

Return Value

String
Certificate serial number

Remarks

Expected format is 20 decimal digits encoded in ASCII format

Example

// From certificate file directly
Console.WriteLine(Sat.GetCertNumber("emisor.cer"));
// 30001000000300023708
// From embedded Certificado attribute
Console.WriteLine(Sat.GetCertNumber("cfdv40-ejemplo-signed-tfd.xml"));
// 30001000000300023708

See Also

Sat Class
FirmaSAT Namespace

Sat.GetCertStart Method

Get start date of the X.509 certificate in ISO time format.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string GetCertStart(
    string fileName
)

Parameters

fileName String

X.509 certificate file or XML file with certificado node

Return Value

String
Certificate start date

Example

Console.WriteLine(Sat.GetCertStart("emisor.cer"));
// 2017-05-18T03:54:56Z

See Also

Sat Class
FirmaSAT Namespace

Sat.GetXmlAttribute Method

Extract attribute data from an XML file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string GetXmlAttribute(
    string xmlFile,
    string attributeName,
    string elementName
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

attributeName String

Name of attribute

elementName String

Name of element or xpath expression (see remarks)

Return Value

String
Attribute value, if found; or "!NO MATCH!" if not found (see XmlNoMatch()); or empty string on error (the empty string may be the correct result if attr="", so check for an error using LastError()).

Remarks

For a simple element name, the default behaviour is to get the attribute from the first element found with the given name. Specify the element name in the form "element[N]" to extract the attribute for the N’th element found in the XML document, where N is a positive decimal integer (N=1,2,3,...). Setting elementName="" will output the value of the named attribute from the root element of the XML document. Setting both elementName="" and attributeName="" will output the name of the root element itself.

XPath expression: Alternatively, specify an path expression in elementName using the "/" and "//" operators and optional predicate [N] where N is a positive integer. For example "/Comprobante/Emisor" or "//Concepto[2]//Retencion[3]". This is a simplified form of XPath that selects the first occurrence of the element matching the path expression (whereas XPath would select all matching elements). Path expressions must start with a "/" or "//" and must not end with a "/". No other XPath function or operator is accepted. Do not use namespace prefixes (e.g. "cfdi:") in the path expression.

Simplified Xpath syntax: /e1 – selects the first <e1> document element (child element of the document node)./e1/e2 – selects the first <e2> child element of the first <e1> document element./e1[2]/e2[3] – selects the third <e2> child element of the second <e1> document element./e1[1]/e2[1] – same as /e1/e2.//e2 – the first <e2> element found anywhere (same as simple e2).//e2[3] – the third <e2> element found anywhere (same as simple e2[3])./e1//e2 – the first <e2> element found anywhere inside the <e1> element. To test for the existence of an element, set attributeName="". This will return the name of the element if it exists, or "!NO MATCH!" if not found. This message can be changed using SetXmlNoMatch(String).

Example

Console.WriteLine(Sat.GetXmlAttribute("cfdv40-ejemplo.xml", "Nombre", "cfdi:Emisor"));
// Esta es una demostración

See Also

Sat Class
FirmaSAT Namespace
XmlNoMatch()
SetXmlNoMatch(String)

Sat.InsertCert Method

Insert certificate information into an XML document and output to a new file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int InsertCert(
    string newFile,
    string baseFile,
    string certFile
)

Parameters

newFile String

Name of new file to be created.

baseFile String

Name of input XML file to be processed (or a string containing XML data).

certFile String

X.509 certificate file.

Return Value

Int32
0 if successful or non-zero error code if failed

Example

// Take an XML file without a NoCertificado...
string fname = cfdv40-ejemplo-nocertnum.xml";
Console.WriteLine("Start file '{0}'.NoCertificado=[{1}]", fname, Sat.GetXmlAttribute(fname, "NoCertificado", "cfdi:Comprobante"));
Console.WriteLine("Start file '{0}'.Certificado={1} bytes", fname, Sat.GetXmlAttribute(fname, "Certificado", "cfdi:Comprobante").Length);
// Insert certificate details into intermediate file...
string interfile = "cfdv40_new-base-pluscert.xml";
int n = Sat.InsertCert(interfile, fname, "emisor.cer");
Console.WriteLine("Sat.InsertCert() returns {0} (expecting 0)", n);
Debug.Assert(0 == n, "Sat.InsertCert failed");
Console.WriteLine("Inter file '{0}'.NoCertificado=[{1}]", interfile, Sat.GetXmlAttribute(interfile, "NoCertificado", "cfdi:Comprobante"));
Console.WriteLine("Inter file '{0}'.Certificado={1} bytes", interfile, Sat.GetXmlAttribute(interfile, "Certificado", "cfdi:Comprobante").Length);
// Start file 'cfdv40-ejemplo-nocertnum.xml'.NoCertificado=[]
// Start file 'cfdv40-ejemplo-nocertnum.xml'.Certificado=0 bytes
// Sat.InsertCert() returns 0 (expecting 0)
// Inter file 'cfdv40_new-base-pluscert.xml'.NoCertificado=[30001000000300023708]
// Inter file 'cfdv40_new-base-pluscert.xml'.Certificado=1836 bytes

See Also

Sat Class
FirmaSAT Namespace

Sat.InsertCertToBytes(Byte[], String) Method

Insert certificate information into an XML document (as a byte array) and output to memory.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Overloads

Name Description
InsertCertToBytes(Byte[], String) Insert certificate information into an XML document (as a byte array) and output to memory.
InsertCertToBytes(String, String) Insert certificate information into an XML document (as a file) and output to memory.

Syntax

public static byte[] InsertCertToBytes(
    byte[] xmlData,
    string certFile
)

Parameters

xmlData Byte[]

Byte array containing XML data

certFile String

X.509 certificate file

Return Value

Byte[]
XML data as a byte array.

Remarks

(bytes) –> (bytes)

Example

// Pass input XML data as a byte array
byte[] xmlArr = File.ReadAllBytes("cfdv40-ejemplo-nocertnum.xml");
string cerStr = Sat.GetCertAsString("emisor.cer");
byte[] b = Sat.InsertCertToBytes(xmlArr, cerStr);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(b));
// <cfdi:Comprobante 
// ...
// NoCertificado="30001000000300023708" Sello="" Certificado="MIIF+TCCA+GgAwIBAgIU ...

See Also

Sat Class
FirmaSAT Namespace

Sat.InsertCertToBytes(String, String) Method

Insert certificate information into an XML document (as a file) and output to memory.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Overloads

Name Description
InsertCertToBytes(Byte[], String) Insert certificate information into an XML document (as a byte array) and output to memory.
InsertCertToBytes(String, String) Insert certificate information into an XML document (as a file) and output to memory.

Syntax

public static byte[] InsertCertToBytes(
    string xmlFile,
    string certFile
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

certFile String

X.509 certificate file

Return Value

Byte[]
XML data as a byte array.

Remarks

(file) –> (bytes)

Example

// Pass input XML data as a string
string xmlStr = File.ReadAllText("cfdv40-ejemplo-nocertnum.xml");
string cerStr = Sat.GetCertAsString("emisor.cer");
byte[] b = Sat.InsertCertToBytes(xmlStr, cerStr);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(b)); 
// <cfdi:Comprobante 
// ...
// NoCertificado="30001000000300023708" Sello="" Certificado="MIIF+TCCA+GgAwIBAgIU ...

See Also

Sat Class
FirmaSAT Namespace

Sat.MakeDigestFromXml Method

Form message digest of piped string (cadena) from an XML file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string MakeDigestFromXml(
    string xmlFile
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

Return Value

String
Message digest in hex format or empty string on error

Example

Console.WriteLine(Sat.MakeDigestFromXml("cfdv40-ejemplo-signed-tfd.xml"));
// 0ff1274e51fbb090489588d832bb1b5b36543302decbb0a5490839b8c99e8755

See Also

Sat Class
FirmaSAT Namespace

Sat.MakePipeStringFromXml Method

Create the piped string (cadena) from an XML file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string MakePipeStringFromXml(
    string xmlFile
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

Return Value

String
Piped string.

Example

string s = Sat.MakePipeStringFromXml("cfdv40-ejemplo.xml");
Console.WriteLine(s);
// ||3.3|A|123ABC|2017-12-04T01:23:59|02|...[cut]...||

See Also

Sat Class
FirmaSAT Namespace

Sat.MakeSignatureFromXml Method

Create signature as a base64 string from data in an XML file ready to be inserted as a sello node.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string MakeSignatureFromXml(
    string xmlFile,
    string keyFile,
    string password = ""
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

keyFile String

Name of private key file

password String (Optional)

Password

Return Value

String
Signature in base64 format or empty string on error

Example

string s = Sat.MakeSignatureFromXml("cfdv40-ejemplo.xml", "emisor.key", "12345678a");
Console.WriteLine(s);
// JQJd6rbiMZj1tZVb1Ta8l88bE7pTDm/aAl ...

See Also

Sat Class
FirmaSAT Namespace

Sat.NewKeyFile Method

Save keyfile with a new password.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int NewKeyFile(
    string newFile,
    string newPassword,
    string keyFile,
    string keyPassword,
    KeyFormat format
)

Parameters

newFile String

Name of new output file to be created.

newPassword String

Password for new key file.

keyFile String

Name of input key file (or a string containing the key in PEM form).

keyPassword String

Password for existing key file.

format KeyFormat

Format to save file [default = DER binary].

Return Value

Int32
Zero (0) if output file is successfully created, or a negative error code.

Example

string password = "12345678a";
string newpassword = "password123";
int n = Sat.NewKeyFile("emisor_new.pem", newpassword, "emisor.key", password, KeyFormat.PEM);
Debug.Assert(n == 0, "Sat.NewKeyFile failed");
Console.WriteLine(File.ReadAllText("emisor_new.pem"));
// -----BEGIN ENCRYPTED PRIVATE KEY-----
// MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIn2sl+Cj3VtgCAggA ...
// -----END ENCRYPTED PRIVATE KEY-----

See Also

Sat Class
FirmaSAT Namespace

Sat.QueryCert Method

Query an X.509 certificate.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string QueryCert(
    string fileName,
    Query query
)

Parameters

fileName String

X.509 certificate file or XML file with certificado node

query Query

Query

Return Value

String
Result of query or an empty string on error

See Also

Sat Class
FirmaSAT Namespace

Sat.SetXmlNoMatch Method

Modify the string returned when GetXmlAttribute(String, String, String) fails to find a match.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static void SetXmlNoMatch(
    string value
)

Parameters

value String

New string value.

Example

// Show default NoMatch string
Console.WriteLine(Sat.XmlNoMatch());
// !NO MATCH!
string fname = "cfdv40-ejemplo-signed-tfd.xml";
// Look for element that isn't there
Console.WriteLine(Sat.GetXmlAttribute(fname, "", "/Comprobante/notthere"));
// !NO MATCH!
// Set a new NoMatch string
Sat.SetXmlNoMatch("##No coinciden##");
Console.WriteLine(Sat.GetXmlAttribute(fname, "", "/Comprobante/notthere"));
// ##No coinciden##

See Also

Sat Class
FirmaSAT Namespace
XmlNoMatch()

Sat.SignXml Method

Sign an XML file

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int SignXml(
    string newFile,
    string baseFile,
    string keyFile,
    string password = "",
    string certFile = "",
    SignOptions signOpts = SignOptions.Default
)

Parameters

newFile String

Name of new file to be created

baseFile String

Name of base XML file to be signed (or a string containing XML data)

keyFile String

Name of private key file

password String (Optional)

Password

certFile String (Optional)

Name of X.509 certificate file to be included in output XML (optional)

signOpts SignOptions (Optional)

Options for output format (optional)

Return Value

Int32
0 if successful or non-zero error code if failed

Remarks

This will create an output XML document copied from the input with the Sello node overwritten by a new signature value. Any existing file called newFile will be overwritten without warning; however, the input and output files can be the same.

If a certificate file szCertFile is specified then the Certificado and NoCertificado nodes will be overwritten in the output file with the values in the certificate file. If a certificate file is not specified then the Certificado value in the XML will be used. A version 4 CFDi document to be signed must use the “cfdi:” namespace prefix. For CFD v4 the NoCertificado attribute in the input must be set to the correct certificate serial number before signing. In a Retenciones document you must set the CertNum attribute before signing. In a ControlesVolumetricos document you must set both the noCertificado and certificado attributes before signing.

Example

int n = Sat.SignXml("cfdv40-signed_new.xml", "cfdv40-ejemplo.xml", "emisor.key", "12345678a", "emisor.cer");

See Also

Sat Class
FirmaSAT Namespace

Sat.SignXmlEx Method

Note: This API is now obsolete. Sign an XML file with extended options [deprecated].

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

[ObsoleteAttribute("Use Sat.SignXml with optional SignOptions parameter")]
public static int SignXmlEx(
    string newFile,
    string xmlFile,
    string keyFile,
    string password,
    string certFile,
    SignOptions signOpts
)

Parameters

newFile String

Name of new file to be created

xmlFile String

Name of base XML file to be signed

keyFile String

Name of private key file

password String

Password for private key file

certFile String

(optional) name of X.509 certificate file to be included in output XML

signOpts SignOptions

Options for output format

Return Value

Int32
0 if successful or non-zero error code if failed

Remarks

See the remarks in SignXml(String, String, String, String, String, SignOptions)

Example

int n = Sat.SignXmlEx(newname, fname, keyfile, password, certfile, SignOptions.BigFile);

See Also

Sat Class
FirmaSAT Namespace

Sat.SignXmlToBytes(Byte[], String, String, String, SignOptions) Method

Sign XML data writing output to a byte array.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Overloads

Name Description
SignXmlToBytes(Byte[], String, String, String, SignOptions) Sign XML data writing output to a byte array.
SignXmlToBytes(String, String, String, String, SignOptions) Sign XML file writing output to a byte array.

Syntax

public static byte[] SignXmlToBytes(
    byte[] xmlData,
    string keyFile,
    string password = "",
    string certFile = "",
    SignOptions signOpts = SignOptions.Default
)

Parameters

xmlData Byte[]

Byte array containing XML data

keyFile String

Name of private key file (or string containing key data in PEM format)

password String (Optional)

Password for key file

certFile String (Optional)

(optional) name of X.509 certificate file to be included in output XML (or string containing certificate data in base64 or PEM format)

signOpts SignOptions (Optional)

Options for output format

Return Value

Byte[]
Signed XML data in a byte array

Remarks

Output XML is always UTF-8 encoded

Example

string password = "12345678a";
string keyStr = Sat.GetKeyAsString("emisor.key", password, KeyOption.EncryptedPEM);
string cerStr = Sat.GetCertAsString("emisor.cer");
byte[] xmlArr = File.ReadAllBytes("cfdv40-ejemplo.xml");
byte[] xmlArrSigned = Sat.SignXmlToBytes(xmlArr, keyStr, password, cerStr, 0);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(xmlArrSigned));
// <?xml version="1.0" encoding="UTF-8"?>
// <cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" ...

See Also

Sat Class
FirmaSAT Namespace

Sat.SignXmlToBytes(String, String, String, String, SignOptions) Method

Sign XML file writing output to a byte array.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Overloads

Name Description
SignXmlToBytes(Byte[], String, String, String, SignOptions) Sign XML data writing output to a byte array.
SignXmlToBytes(String, String, String, String, SignOptions) Sign XML file writing output to a byte array.

Syntax

public static byte[] SignXmlToBytes(
    string xmlFile,
    string keyFile,
    string password = "",
    string certFile = "",
    SignOptions signOpts = SignOptions.Default
)

Parameters

xmlFile String

Name of base XML file to be signed

keyFile String

Name of private key file (or string containing key data in PEM format)

password String (Optional)

Password for key file

certFile String (Optional)

(optional) name of X.509 certificate file to be included in output XML (or string containing certificate data in base64 or PEM format)

signOpts SignOptions (Optional)

Options for output format

Return Value

Byte[]
Signed XML data in a byte array

Remarks

Output XML is always UTF-8 encoded

Example

byte[] b = Sat.SignXmlToBytes("cfdv40-ejemplo.xml", "emisor.key", "12345678a", "emisor.cer", SignOptions.Default);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(b));
// <?xml version="1.0" encoding="UTF-8"?>
// <cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" ...

See Also

Sat Class
FirmaSAT Namespace

Sat.Uuid Method

Generate a Universally Unique IDentifier (UUID) compliant with RFC 4122.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string Uuid()

Return Value

String
UUID string of exactly 36 characters

Remarks

The output value will be different each time.

Example

Console.WriteLine(Sat.Uuid());
// 343c6b13-4f69-4d2a-97e6-64cb770c7677

See Also

Sat Class
FirmaSAT Namespace

Sat.ValidateXml Method

Validate an XML file against SAT specifications.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int ValidateXml(
    string xmlFile,
    XmlOption xmlOpt = XmlOption.Default
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

xmlOpt XmlOption (Optional)

Option for strict or loose XML restrictions (optional, default=Strict)

Return Value

Int32
0 if successful or non-zero error code

Remarks

This just validates the XML structure, not the signature.

Example

int n = Sat.ValidateXml("cfdv40-ejemplo.xml");
Console.WriteLine("Sat.ValidateXml returns {0}", n);
// Sat.ValidateXml returns 0

int n = Sat.ValidateXml("cfdv40-iedu-badcurp.xml");
Console.WriteLine("Sat.ValidateXml returns {0}", n);
// Sat.ValidateXml returns -28
Console.WriteLine(General.ErrorLookup(n));
// XML restriction is violated/XML restriccion es violada (XML_FACET_ERROR)
Console.WriteLine(General.LastError());
// Bad attribute/atributo mal [iedu:instEducativas/@CURP] (line 30): 
// 'JUAN01010101GTOHMD0' is too long/es demasiado largo, maximum length/longitud maxima=18
// Input has valid XML form but an invalid facet error against SAT specifications
// Using the "Loose" option validates that the XML form is still correct.
int n = Sat.ValidateXml("cfdv40-iedu-badcurp.xml", XmlOption.Loose);
Console.WriteLine("Sat.ValidateXml(Loose) returns {0}", n);
// Sat.ValidateXml(Loose) returns 0

See Also

Sat Class
FirmaSAT Namespace

Sat.VerifySignature Method

Verify the signature (sello) in an XML file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int VerifySignature(
    string xmlFile,
    string certFile = ""
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

certFile String (Optional)

X.509 certificate file (optional)

Return Value

Int32
Zero if signature is verified or nonzero error code if failed

Remarks

If no certFile is specified, the public key is obtained from the certificado node in the XML file.

Example

Console.WriteLine(Sat.VerifySignature("cfdv40-ejemplo-signed.xml"));
// 0
Console.WriteLine(Sat.VerifySignature("cfdv40-badsig.xml"));
// -15
Console.WriteLine(General.ErrorLookup(-15));
// Decryption error/De error de descifrado (DECRYPT_ERROR)

See Also

Sat Class
FirmaSAT Namespace

Sat.XmlNoMatch Method

Error message returned when GetXmlAttribute(String, String, String) fails to find a match

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string XmlNoMatch()

Return Value

String
Error message (default = "!NO MATCH!")

Remarks

Use to test for existence of an element. The message can be changed using SetXmlNoMatch(String).

Example

Console.WriteLine(Sat.XmlNoMatch());
// !NO MATCH!

See Also

Sat Class
FirmaSAT Namespace
SetXmlNoMatch(String)

Sat.XmlReceiptVersion Method

Find version number of Comprobante element or ID number for other document types.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int XmlReceiptVersion(
    string xmlFile
)

Parameters

xmlFile String

Name of XML file

Return Value

Int32
Version number or ID number, or a negative error code. 40 = Comprobante document with Version=“4.0”33 = Comprobante document with version=“3.3”1010/1020 = Retenciones document with Version=“1.0”/“2.0”2011/2013 = CatalogoCuentas document with Version=“1.1”/“1.3”2111/2113 = BalanzaComprobacion document with Version=“1.1”/“1.3”2211/2213 = PolizasPeriodo document with Version=“1.1”/“1.3”2312/2313 = AuxiliarFolios document with Version=“1.2”/“1.3”2411/2413 = AuxiliarCtas document with Version=“1.1”/“1.3”2511 = SelloDigitalContElec document with Version=“1.1”4011 = ControlesVolumetricos document with Version=“1.1”

Example

Console.WriteLine(Sat.XmlReceiptVersion("cfdv40-ejemplo.xml"));  // 40

See Also

Sat Class
FirmaSAT Namespace

Tfd Class

Methods for Timbre Fiscal Digital (TFD).

Inheritance Hierarchy

System.Object
FirmaSAT.Tfd

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public class Tfd

The Tfd type exposes the following members.

Methods

Name Description
AddSignedTfd Add a signed Timbre Fiscal Digital (TFD) element to a CFDI document.
ExtractDigestFromSignature Extract message digest from the selloSAT node in Timbre Fiscal Digital of CFDI document.
MakeDigestFromXml Form message digest of cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.
MakePipeStringFromXml Create the cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.
MakeSignatureFromXml Create the selloSAT signature as a base64 string from TFD data in CFDI XML document.
VerifySignature Verify the selloSAT signature in CFDI XML document.

See Also

FirmaSAT Namespace

Tfd.AddSignedTfd Method

Add a signed Timbre Fiscal Digital (TFD) element to a CFDI document.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int AddSignedTfd(
    string newFile,
    string inputFile,
    string keyFile,
    string password,
    string certFile
)

Parameters

newFile String

Name of new file to be created

inputFile String

Name of existing CFDI file

keyFile String

Name of PAC’s private key file

password String

Password for private key

certFile String

Name of PAC’s X.509 certificate that matches the keyFile

Return Value

Int32
Zero (0) if successful or non-zero error code if failed

Remarks

The inputFile must be a version 4.0/3.3 CFDi document already signed with a sello field and no existing TFD element. The TFD will be timestamped using the system clock and a fresh UUID will be generated. No other XML processing is carried out except inserting the TFD element.

Example

int n = Tfd.AddSignedTfd(newname, fname, keyfile, password, certfile);
Console.WriteLine("Tfd.AddSignedTfd returns {0}", n);  // 0
string s = Sat.GetXmlAttribute("cfdv40-ejemplo_signed.xml", "SelloSAT", "TimbreFiscalDigital");
// SelloSat=Qncw19SZ0w/uxkwCYkf/7V3DF3j28Jp1XyNVyqiyOreq0S ...

See Also

Tfd Class
FirmaSAT Namespace

Tfd.ExtractDigestFromSignature Method

Extract message digest from the selloSAT node in Timbre Fiscal Digital of CFDI document.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string ExtractDigestFromSignature(
    string xmlFile,
    string certFile
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

certFile String

X.509 certificate file of PAC who signed the TFD (required)

Return Value

String
Message digest in hex format or empty string on error

See Also

Tfd Class
FirmaSAT Namespace

Tfd.MakeDigestFromXml Method

Form message digest of cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string MakeDigestFromXml(
    string xmlFile
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

Return Value

String
Message digest in hex format or empty string on error

See Also

Tfd Class
FirmaSAT Namespace

Tfd.MakePipeStringFromXml Method

Create the cadena original del Timbre Fiscal Digital del SAT (TFD piped string) from CFDI XML file.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string MakePipeStringFromXml(
    string xmlFile
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

Return Value

String
Piped string in UTF-8 encoding

See Also

Tfd Class
FirmaSAT Namespace

Tfd.MakeSignatureFromXml Method

Create the selloSAT signature as a base64 string from TFD data in CFDI XML document.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string MakeSignatureFromXml(
    string xmlFile,
    string keyFile,
    string password
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

keyFile String

Name of private key file

password String

Password

Return Value

String
Signature in base64 format or empty string on error

Remarks

Assumes you are a PAC with a valid SAT signing key.

See Also

Tfd Class
FirmaSAT Namespace

Tfd.VerifySignature Method

Verify the selloSAT signature in CFDI XML document.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int VerifySignature(
    string xmlFile,
    string certFile
)

Parameters

xmlFile String

Name of input XML file to be processed (or a string containing XML data)

certFile String

X.509 certificate file of PAC who signed the TFD (required)

Return Value

Int32
0 if signature is verified or non-zero error code if failed

See Also

Tfd Class
FirmaSAT Namespace

KeyFormat Enumeration

Format for saved key files.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public enum KeyFormat

Members

Member name Description
Default Default = Binary
Binary Binary DER-encoded
PEM PEM Format

See Also

FirmaSAT Namespace

KeyOption Enumeration

Options for key output in Sat.GetKeyAsString().

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public enum KeyOption

Members

Member name Description
Default Default (unencrypted base64 string).
UnencryptedBase64 Key as unencrypted base64 string suitable for Cancelacion element.
EncryptedPEM Key as encrypted private key in PEM format suitable for input as a keyFile parameter.

See Also

FirmaSAT Namespace

Sat.GetKeyAsString Method

Get private key data as a base64 string suitable for a llaveCertificado element in a Cancelacion XML document

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static string GetKeyAsString(
    string fileName,
    string password,
    KeyOption keyOpt = KeyOption.Default
)

Parameters

fileName String

Encrypted private key file

password String

Password for encrypted private key

keyOpt KeyOption (Optional)

Output format (optional, default=UnencryptedBase64)

Return Value

String
Private key data as a string or empty string on error

Remarks

CAUTION: the unencrypted default option reveals your private key in unsecured form. Use with care!

Example

Console.WriteLine(Sat.GetKeyAsString("emisor.key", "12345678a"));
// PFJTQUtleVZhbHVlPjxNb2R1bHVzP ... WYWx1ZT4=
Console.WriteLine(Sat.GetKeyAsString("emisor.key", "12345678a", KeyOption.EncryptedPEM));
// -----BEGIN ENCRYPTED PRIVATE KEY-----
// MIIFDjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIAgEAAoIBAQACAggA
// ... rbw=
// -----END ENCRYPTED PRIVATE KEY-----

See Also

Sat Class
FirmaSAT Namespace

PfxFormat Enumeration

Options for PKCS12 (PFX) output in Sat.WritePfxFile().

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public enum PfxFormat

Members

Member name Description
Default Default (plain base64 suitable for Cancelacion element).
Base64 PFX data as plain base64.
PEM PKCS12 PEM textual form = same as Base64 but with PEM encapsulation
-----BEGIN PKCS12-----
...
-----END PKCS12-----
Binary Binary DER form.

See Also

FirmaSAT Namespace

Sat.WritePfxFile Method

Create PFX (PKCS-12) file in PEM format suitable for a Cancelación.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public static int WritePfxFile(
    string pfxFile,
    string pfxPassword,
    string keyFile,
    string keyPassword,
    string certFile,
    PfxFormat pfxFormat = PfxFormat.Default
)

Parameters

pfxFile String

Name of output PFX file to be created

pfxPassword String

Password to open new PFX file

keyFile String

Name of encrypted key file

keyPassword String

Password for encrypted key file

certFile String

Name of X.509 certificate file

pfxFormat PfxFormat (Optional)

Output format for PFX file (optional, default=plain base64)

Return Value

Int32
Zero (0) if output file is successfully created, or a negative error code.

Example

string password = "12345678a";
string newpassword = "clavedesalida";
int n = Sat.WritePfxFile("archivo_new-pfx.txt", newpassword, "emisor.key", password, "emisor.cer");
Debug.Assert(n == 0, "Sat.WritePfxFile failed");
Console.WriteLine(File.ReadAllText("archivo_new-pfx.txt"));
// MIIMOQIBAzCCC/8GCSqGSIb3DQEHAaCCC/AEggvsMIIL6DCCBp8GCSqGSIb3DQEH ...

See Also

Sat Class
FirmaSAT Namespace

Query Enumeration

Options for certificate query.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public enum Query

Members

Member name Description
notAfter Get certificate expiry date.
notBefore Get certificate start date.
organizationName Get organization name of issuer. Expecting “Servicio de Administración Tributaria” if issued by SAT
companyName Get organization name of subject. This should be your company name.
rfc Get RFC of subject. Expecting 12 or 13 characters if issued by SAT.
serialNumber Get decoded serial number. Expecting 20 decimal digits.
sigAlg Get algorithm used to sign certificate (e.g. “sha256WithRSAEncryption”).
keySize Get size in bits of certificate’s public key (e.g. “2048”).

See Also

FirmaSAT Namespace

SignOptions Enumeration

Options for signing XML.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

[FlagsAttribute]
public enum SignOptions

Members

Member name Description
Default Default (add BOM, empty elements in form <foo></foo>).
NoBOM Do not add byte-order mark (BOM) to file.
OverrideReqd Override strict checks for required nodes (advanced users).
UseEmptyElements Output empty elements in empty-element tag form <foo /> (default is start-end tag pair form <foo></foo>).
BigFile Speed up the processing of large files.

See Also

FirmaSAT Namespace

XmlOption Enumeration

Options for XML validation.

Namespace: FirmaSAT
Assembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.36515 (10.70.0.0)

Syntax

public enum XmlOption

Members

Member name Description
Default Default value (strict)
Strict Enforce stricter XML restrictions (default in v5.0 and above).
Loose Use looser restrictions on data types (default before v5.0).

See Also

FirmaSAT Namespace


Created: 2025-10-20 20:18:00
Copyright (C) 2025 D.I. Management Services Pty Ltd t/a CryptoSys https://cryptosys.net