PowerBuilder
PowerBuilder
Sign Mexico Pedimento
See more Misc Examples
Add a signature to a Mexico pedimento file.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
integer li_BCRLF
oleobject loo_Sb
string ls_Md5_base64
oleobject loo_PrivKey
oleobject loo_Xml
oleobject loo_Asn
oleobject loo_Rsa
oleobject loo_BdSig
oleobject loo_Cert
string ls_SerialHex
oleobject loo_SbSerial
integer li_NumReplaced
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This is the contents before signing:
// 500|1|3621|4199800|400||
// 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
// 507|4199800|IM|2006-7888">
// 507|4199800|MS|2">
// 800|4199800|1">
// 801|M3621037.222|1|5|011|
// This is the contents after signing
// 500|1|3621|4199800|400||
// 601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||
// 507|4199800|IM|2006-7888">
// 507|4199800|MS|2">
// 800|4199800|1|fhP2Ker54D2+3+UZch23F0E72 .... 9qNSPIuAqpj524qLZbbA==|30001000000500003416|
// 801|M3621037.222|1|5|011|
// First create the text to be signed.
li_BCRLF = 1
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_Sb
MessageBox("Error","Connecting to COM object failed")
return
end if
// Use CRLF line endings.
loo_Sb.AppendLine("500|1|3621|4199800|400||",li_BCRLF)
loo_Sb.AppendLine("601|3621|4199800|400|IN|1||EKU9003173C9|EKU9003173C9FRNN09|1||",li_BCRLF)
loo_Sb.AppendLine("507|4199800|IM|2006-7888">",li_BCRLF)
loo_Sb.AppendLine("507|4199800|MS|2">",li_BCRLF)
// Generate the MD5 hash of what we have so far..
ls_Md5_base64 = loo_Sb.GetHash("md5","base64","utf-8")
Write-Debug "MD5 hash = " + ls_Md5_base64
// Complete the original file.
// After signing, we'll update the BASE64_SIGNATURE and CERT_SERIAL
loo_Sb.AppendLine("800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL|",li_BCRLF)
loo_Sb.AppendLine("801|M3621037.222|1|5|011|",li_BCRLF)
// We're going to sign the MD5 hash using the private key.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_PrivKey.LoadAnyFormatFile("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.key","12345678a")
if li_Success = 0 then
Write-Debug loo_PrivKey.LastErrorText
destroy loo_Sb
destroy loo_PrivKey
return
end if
// Generate the ASN.1 to be signed.
// <sequence>
// <sequence>
// <oid>1.2.840.113549.2.5</oid>
// <null/>
// </sequence>
// <octets>SwxHfaJhG+N3pPqay6UzVA==</octets>
// </sequence>
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
loo_Xml.Tag = "sequence"
loo_Xml.UpdateChildContent("sequence|oid","1.2.840.113549.2.5")
loo_Xml.UpdateChildContent("sequence|null","")
loo_Xml.UpdateChildContent("octets",ls_Md5_base64)
loo_Asn = create oleobject
li_rc = loo_Asn.ConnectToNewObject("Chilkat.Asn")
loo_Asn.LoadAsnXml(loo_Xml.GetXml())
Write-Debug "ASN.1 = " + loo_Asn.GetEncodedDer("base64")
// Sign with the private key.
loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")
li_Success = loo_Rsa.UsePrivateKey(loo_PrivKey)
if li_Success = 0 then
Write-Debug loo_Rsa.LastErrorText
destroy loo_Sb
destroy loo_PrivKey
destroy loo_Xml
destroy loo_Asn
destroy loo_Rsa
return
end if
// Create the opaque signature.
loo_BdSig = create oleobject
li_rc = loo_BdSig.ConnectToNewObject("Chilkat.BinData")
loo_BdSig.AppendEncoded(loo_Asn.GetEncodedDer("base64"),"base64")
li_Success = loo_Rsa.SignRawBd(loo_BdSig)
if li_Success = 0 then
Write-Debug loo_Rsa.LastErrorText
destroy loo_Sb
destroy loo_PrivKey
destroy loo_Xml
destroy loo_Asn
destroy loo_Rsa
destroy loo_BdSig
return
end if
// bd now contains the opaque signature, which embeds the ASN.1, which contains the MD5 hash.
// We're going to add this line:
// 800|4199800|1|BASE64_SIGNATURE|CERT_SERIAL_NUM|
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadFromFile("qa_data/certs/mexico_test/Certificados_de_Prueba/Certificados_Pruebas/Personas Morales/EKU9003173C9_20230517223532/CSD_EKU9003173C9_20230517223903/CSD_Sucursal_1_EKU9003173C9_20230517_223850.cer")
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Sb
destroy loo_PrivKey
destroy loo_Xml
destroy loo_Asn
destroy loo_Rsa
destroy loo_BdSig
destroy loo_Cert
return
end if
ls_SerialHex = loo_Cert.SerialNumber
// The serial in hex form looks like this: 3330303031303030303030353030303033343136
// Decode to us-ascii.
loo_SbSerial = create oleobject
li_rc = loo_SbSerial.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbSerial.DecodeAndAppend(ls_SerialHex,"hex","us-ascii")
Write-Debug "serial number in us-ascii: " + loo_SbSerial.GetAsString()
li_NumReplaced = loo_Sb.Replace("CERT_SERIAL",loo_SbSerial.GetAsString())
li_NumReplaced = loo_Sb.Replace("BASE64_SIGNATURE",loo_BdSig.GetEncoded("base64"))
Write-Debug "------------------------------------"
Write-Debug "Result:"
Write-Debug loo_Sb.GetAsString()
destroy loo_Sb
destroy loo_PrivKey
destroy loo_Xml
destroy loo_Asn
destroy loo_Rsa
destroy loo_BdSig
destroy loo_Cert
destroy loo_SbSerial