PowerBuilder
An enveloping signature is where the signed data is contained within the Signature within an Object element.
This example signs non-XML text data where the XML signature constitutes the entire output.
PowerBuilder
Create Enveloping XML Digital Signature
See more XML Digital Signatures Examples
This example creates an enveloping digital signature.An enveloping signature is where the signed data is contained within the Signature within an Object element.
This example signs non-XML text data where the XML signature constitutes the entire output.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Http
oleobject loo_ZipFile
string ls_KeyUrl
oleobject loo_Zip
oleobject loo_Entry
oleobject loo_EcKey
oleobject loo_Gen
oleobject loo_SbContent
oleobject loo_SbXml
li_Success = 0
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Let's use the ECDSA private key at https://www.chilkatsoft.com/exampleData/secp256r1-key.zip
// for signing.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_ZipFile = create oleobject
li_rc = loo_ZipFile.ConnectToNewObject("Chilkat.BinData")
ls_KeyUrl = "https://www.chilkatsoft.com/exampleData/secp256r1-key.zip"
li_Success = loo_Http.QuickGetBd(ls_KeyUrl,loo_ZipFile)
if li_Success = 0 then
Write-Debug loo_Http.LastErrorText
destroy loo_Http
destroy loo_ZipFile
return
end if
loo_Zip = create oleobject
li_rc = loo_Zip.ConnectToNewObject("Chilkat.Zip")
li_Success = loo_Zip.OpenBd(loo_ZipFile)
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.ZipEntry")
li_Success = loo_Zip.EntryMatching("*.pem",loo_Entry)
if li_Success = 0 then
Write-Debug loo_Zip.LastErrorText
destroy loo_Http
destroy loo_ZipFile
destroy loo_Zip
destroy loo_Entry
return
end if
loo_EcKey = create oleobject
li_rc = loo_EcKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_EcKey.LoadPem(loo_Entry.UnzipToString(0,"utf-8"))
if li_Success <> 1 then
Write-Debug loo_EcKey.LastErrorText
destroy loo_Http
destroy loo_ZipFile
destroy loo_Zip
destroy loo_Entry
destroy loo_EcKey
return
end if
// ----------------------------------------------------------------------------
loo_Gen = create oleobject
li_rc = loo_Gen.ConnectToNewObject("Chilkat.XmlDSigGen")
// Provide the ECDSA key to the XML Digital Signature generator
loo_Gen.SetPrivateKey(loo_EcKey)
// Add an enveloped reference to the content to be signed.
loo_SbContent = create oleobject
li_rc = loo_SbContent.ConnectToNewObject("Chilkat.StringBuilder")
loo_SbContent.Append("This is the content that is signed.")
loo_Gen.AddEnvelopedRef("abc123",loo_SbContent,"sha256","C14N","")
// Generate the XML digital signature.
// Notice that in other examples, the sbXml passed to CreateXmlDSigSb
// already contains XML, and the XML signature is inserted at the location
// specified by the SigLocation property. In this case, both SigLocation
// and sbXml are empty. The result is that sbXml will contain just the Signature.
loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_Gen.CreateXmlDSigSb(loo_SbXml)
if li_Success = 0 then
Write-Debug loo_Gen.LastErrorText
destroy loo_Http
destroy loo_ZipFile
destroy loo_Zip
destroy loo_Entry
destroy loo_EcKey
destroy loo_Gen
destroy loo_SbContent
destroy loo_SbXml
return
end if
// Examine the enveloped signature, where the data is contained within the XML Signature
Write-Debug loo_SbXml.GetAsString()
// The Signature returned is compact and in a single line, like this:
// <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256"/><ds:Reference URI="#abc123"><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>tEVrbXXjeTXjF3tIojul4/sgeEGN49E1dxr/GMs8GNE=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>/pILUshwrzgdGc4bPgp85TDfbUiM9pn8EIPNRVWKuoVEtPsv4XRthUrv9aDDvajmyl2okLwTakANgtaxO1ULMw==</ds:SignatureValue><ds:KeyInfo><ds:KeyValue><ds:ECKeyValue xmlns="http://www.w3.org/2009/xmldsig11#"><ds:NamedCurve URI="urn:oid:1.2.840.10045.3.1.7" /><ds:PublicKey>BOVKaiLPKEDChhkA64UEBOXTv/VFHnhrUPN+bXqCvEl7rroAYpH5tKzbiGTtMSlp4JO9Pxg44zeX7EoWDvOrpD0=</ds:PublicKey></ds:ECKeyValue></ds:KeyValue></ds:KeyInfo><ds:Object Id="abc123">This is the content that is signed.</ds:Object></ds:Signature>
// XML pretty-printed, the signature is as follows, but pretty-printing introductes whitespace that breaks the signature..
// <?xml version="1.0" encoding="utf-8" ?>
// <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
// <ds:SignedInfo>
// <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
// <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#ecdsa-sha256" />
// <ds:Reference URI="#abc123">
// <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
// <ds:DigestValue>tEVrbXXjeTXjF3tIojul4/sgeEGN49E1dxr/GMs8GNE=</ds:DigestValue>
// </ds:Reference>
// </ds:SignedInfo>
// <ds:SignatureValue>/pILUshwrzgdGc4bPgp85TDfbUiM9pn8EIPNRVWKuoVEtPsv4XRthUrv9aDDvajmyl2okLwTakANgtaxO1ULMw==</ds:SignatureValue>
// <ds:KeyInfo>
// <ds:KeyValue>
// <ds:ECKeyValue xmlns="http://www.w3.org/2009/xmldsig11#">
// <ds:NamedCurve URI="urn:oid:1.2.840.10045.3.1.7" />
// <ds:PublicKey>BOVKaiLPKEDChhkA64UEBOXTv/VFHnhrUPN+bXqCvEl7rroAYpH5tKzbiGTtMSlp4JO9Pxg44zeX7EoWDvOrpD0=</ds:PublicKey>
// </ds:ECKeyValue>
// </ds:KeyValue>
// </ds:KeyInfo>
// <ds:Object Id="abc123">This is the content that is signed.</ds:Object>
// </ds:Signature>
//
destroy loo_Http
destroy loo_ZipFile
destroy loo_Zip
destroy loo_Entry
destroy loo_EcKey
destroy loo_Gen
destroy loo_SbContent
destroy loo_SbXml