Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Create XML Digital Signature using a DSA Key

Demonstrates how to create an XML digital signature using a DSA key.

This example requires Chilkat v9.5.0.69 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    String sUrl
    Handle hoHttp
    Variant vSbXml
    Handle hoSbXml
    Boolean iSuccess
    Variant vDsaKey
    Handle hoDsaKey
    Handle hoXmlSigGen
    Handle hoVerifier
    Boolean iBVerified
    String sTemp1

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // The XML to be signed in this example contains the following:

    // <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    // <Envelope>
    //   <Header>
    //     <Security>
    //     </Security>
    //   </Header>
    //   <Body Id="abc">
    //     <z:FooBar xmlns:z="https://www.example-code.com"/>
    //   </Body>
    // </Envelope>

    // The above XML is available at https://www.chilkatsoft.com/exampleData/xmlToSign.xml
    // Fetch the XML and then sign it..

    Move "https://www.chilkatsoft.com/exampleData/xmlToSign.xml" To sUrl
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComQuickGetSb Of hoHttp sUrl vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // This example uses a DSA private key for signing.  
    // There are many ways of getting an DSA private key using Chilkat.  This example
    // will load it from an encrypted PEM file.

    // In case you would like to use it, I put the DSA private key PEM file here:
    // https://www.chilkatsoft.com/exampleData/dsa1024_secret.zip
    // The password is "secret".
    Get Create (RefClass(cComChilkatPrivateKey)) To hoDsaKey
    If (Not(IsComObjectCreated(hoDsaKey))) Begin
        Send CreateComObject of hoDsaKey
    End
    Get ComLoadEncryptedPemFile Of hoDsaKey "qa_data/dsa/dsa1024_secret.pem" "secret" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoDsaKey To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // To create the XML digital signature (i.e. embed the signature within
    // the XML), first specify what is desired, then call the method to 
    // create the XML signature.
    // 
    // For example, the application must provide the following:
    //     - Where to put the signature.
    //     - What to sign.
    //     - The algorithms to be used.
    //     - The key to be used for signing.
    // 

    Get Create (RefClass(cComChilkatXmlDSigGen)) To hoXmlSigGen
    If (Not(IsComObjectCreated(hoXmlSigGen))) Begin
        Send CreateComObject of hoXmlSigGen
    End

    // In this example, we're going to put the signature within the Security element.
    // To specify the location, set the SigLocation property to the XML path to this element,
    // using vertical bar characters to separate tags.
    Set ComSigLocation Of hoXmlSigGen To "Envelope|Header|Security"

    // An XML digital signature contains one or more references.  These can be references to the parts
    // of the XML document to be signed (a same document reference), or can be external references.
    // This example will add a single same-document reference.  We'll add a reference to the XML fragment
    // at Body, which is indicated by providing the value of the "ID" attribute (where "ID" is case
    // insensitive).  In this case, we specify "abc" because Body has an Id="abc".
    // This causes the <Body> ... </Body> XML fragment to be signed.
    // 
    // For each same-document reference, we must also indicate the hash algorithm and XML canonicalization
    // algorithm to be used.  For this example we'll choose SHA1 and Exclusive XML Canonicalization.
    Get ComAddSameDocRef Of hoXmlSigGen "abc" "sha1" "EXCL_C14N" "" "" To iSuccess

    // Provide the DSA key to be used for signing:
    Get pvComObject of hoDsaKey to vDsaKey
    Get ComSetPrivateKey Of hoXmlSigGen vDsaKey To iSuccess

    // We're leaving the following properties at their default values:
    // 
    //    - SigNamespacePrefix (default is "ds")
    //    - SignedInfoCanonAlg  (default is EXCL_C14N)
    //    - KeyInfoType (default is "KeyValue", where the DSA public key is included in the Signature)

    // Note: Each Reference has it's own specified algorithms for XML canonicalization and hashing,
    // and the actual signature part (the SignedInfo) has it's own hash/canonicalization algorithm.
    // They may or may not be the same.  In this example, we use Exclusive XML Canonicalization and SHA1 throughout.

    // The default SignedInfoDigestMethod is sha256, which is a good choice.  This example will
    // change it to sha1.
    Set ComSignedInfoDigestMethod Of hoXmlSigGen To "sha1"

    // OK, everything's specified, so let's create the XML digital signature:
    // This in-place signs the XML.  If successful, sbXml will contain the 
    // XML with the digital signature at the specified location.
    Get pvComObject of hoSbXml to vSbXml
    Get ComCreateXmlDSigSb Of hoXmlSigGen vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoXmlSigGen To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Examine the signed XML:
    Get ComGetAsString Of hoSbXml To sTemp1
    Showln sTemp1

    // Below is the signed XML that is produced.
    // Chilkat emits the Signature in compact form on a single line.  Whitespace in XML signatures
    // matters.  Chilkat's opinion is that writing the Signature without whitespace minimizes the chance
    // for problems with whatever software might be verifying the signature.

    // <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    // <Envelope>
    //   <Header>
    //     <Security>
    //     <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/2000/09/xmldsig#dsa-sha1"/><ds:Reference URI="#abc"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><ds:DigestValue>77X3L6odxTBow1W1dIiqZW1foow=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>Ci/KiAqoQLvNyK94n8TJwdJ6dVpIIy8tJg+9s8utmB92cW8lTd+U7g==</ds:SignatureValue><ds:KeyInfo><ds:KeyValue><ds:DSAKeyValue><ds:P>AN+Af9FvnqYl84Own8m/MHza9vVFEbpySOiWyeIM8diDjaES9i+UykaX9BGc0oaRJHtrHhe+gboMBGHfP+gYcgbqzeyVCi2kHghRD/TK7uI4jN8Yv8tfM6AO0QrDFfxFq4IV+qTZbSoCkbWaopQ+kEOY6B4btFxJ1YuE48mExMZ3</ds:P><ds:Q>AOCv5pwf4kvMW37Yzt0iH6URKOlD</ds:Q><ds:G>FO3eoDcpuU9Z25OQGFtC+M2ifczcKyRkFwTauGR3mIw0wePPiu3Ocr7A81SA8GQ9x3hWO5jyP//jL3jxYy8fgBg2RXwiic3nwT2rZfu50g6tPUpz1stKz/+Mxjulo6XuaMSOWwjU021IxVMzxQ1/L8QiwiIrwxvfQOaMY8HJgvw=</ds:G><ds:Y>JJmZQvWtrZ+Nb2YV47SfGAave8uOIedGlmg+CseoqKZC2aFcWM6kR0giUwQx834dhxQScRhqzxFxQPk/x+5hyG8LQbXW+BELmT1BiyodobYUAet4rSIt4EU4KU0iYw1drpcWT1jqI07l5xNi28heYSfSF5Odv2qwXxVKDSEeEkM=</ds:Y></ds:DSAKeyValue></ds:KeyValue></ds:KeyInfo></ds:Signature></Security>
    //   </Header>
    //   <Body Id="abc">
    //     <z:FooBar xmlns:z="https://www.example-code.com"/>
    //   </Body>
    // </Envelope>

    // The above signed XML contains the DSA public key in the DSAKeyValue fragment (within the ds:KeyInfo).
    // The signature can easily be verified like this:

    Get Create (RefClass(cComChilkatXmlDSig)) To hoVerifier
    If (Not(IsComObjectCreated(hoVerifier))) Begin
        Send CreateComObject of hoVerifier
    End

    // First load the XML containing the signature(s) to be verified.
    Get pvComObject of hoSbXml to vSbXml
    Get ComLoadSignatureSb Of hoVerifier vSbXml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComVerifySignature Of hoVerifier True To iBVerified
    If (iBVerified <> True) Begin
        Get ComLastErrorText Of hoVerifier To sTemp1
        Showln sTemp1
    End

    Showln "Signature verified = " iBVerified


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.