Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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.
    const http = try chilkat.Http.init();
    defer http.deinit();
    const zip_file = try chilkat.BinData.init();
    defer zip_file.deinit();
    const key_url = "https://www.chilkatsoft.com/exampleData/secp256r1-key.zip";

    http.quickGetBd(key_url, zip_file) catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    const zip = try chilkat.Zip.init();
    defer zip.deinit();
    try zip.openBd(zip_file);

    const entry = try chilkat.ZipEntry.init();
    defer entry.deinit();
    zip.entryMatching("*.pem", entry) catch {
        std.debug.print("{s}\n", .{try zip.getLastErrorText(alloc)});
        return;
    };

    const ec_key = try chilkat.PrivateKey.init();
    defer ec_key.deinit();
    ec_key.loadPem(try entry.unzipToString(alloc, 0, "utf-8")) catch {
        std.debug.print("{s}\n", .{try ec_key.getLastErrorText(alloc)});
        return;
    };

    // ----------------------------------------------------------------------------
    const gen = try chilkat.XmlDSigGen.init();
    defer gen.deinit();

    // Provide the ECDSA key to the XML Digital Signature generator
    gen.setPrivateKey(ec_key) catch {};

    // Add an enveloped reference to the content to be signed.
    const sb_content = try chilkat.StringBuilder.init();
    defer sb_content.deinit();
    sb_content.append("This is the content that is signed.") catch {};
    gen.addEnvelopedRef("abc123", sb_content, "sha256", "C14N", "") catch {};

    // 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.
    const sb_xml = try chilkat.StringBuilder.init();
    defer sb_xml.deinit();
    gen.createXmlDSigSb(sb_xml) catch {
        std.debug.print("{s}\n", .{try gen.getLastErrorText(alloc)});
        return;
    };

    // Examine the enveloped signature, where the data is contained within the XML Signature
    std.debug.print("{s}\n", .{try sb_xml.getAsString(alloc)});

    // 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>
    //
}