Sample code for 30+ languages & platforms
Zig

Load a Private Key from XML Text

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.LoadXml method, which loads a private key from XML text. The only argument is the XML string. Supported roots include RSAKeyValue, DSAKeyValue, ECCKeyValue, and Ed25519KeyValue.

Background: XML key representations such as RSAKeyValue come from the .NET/XML-DSIG world, where keys are exchanged as XML elements. This loads that form — the inverse of GetXml — which is handy for interoperating with .NET code or systems built around XML signatures. It is a plaintext format, so protect the XML like any unencrypted key material.

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();

    // Demonstrates the PrivateKey.LoadXml method, which loads a private key from XML text.  The only
    // argument is the XML string.  Supported roots include RSAKeyValue, DSAKeyValue, ECCKeyValue,
    // and Ed25519KeyValue.

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();

    // The XML text of a private key (for example as produced by GetXml).
    const xml_text = "<RSAKeyValue><Modulus>...</Modulus><Exponent>AQAB</Exponent>...</RSAKeyValue>";

    priv_key.loadXml(xml_text) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Loaded a {s} private key.\n", .{try priv_key.getKeyType(alloc)});
}