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

Export Digital Certificate's Public Key

See more Certificates Examples

The ExportPublicKey method can be called to get a certificate's public key. It can then be saved to any of a number of formats: (1) OpenSSL DER, (2) OpenSSL PEM, (3) RSA DER, (4) XML.

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

    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    // LoadFromFile will load virtually any certificate format file.
    // It will auto-recognize the format and load appropiately.
    cert.loadFromFile("/Users/chilkat/testData/cer/chilkat.cer") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Get the public key:
    const pubkey = try chilkat.PublicKey.init();
    defer pubkey.deinit();
    cert.getPublicKey(pubkey) catch {};

    // Save to various formats:
    pubkey.saveDerFile(false, "/Users/chilkat/testData/pubkeys/chilkat_pkcs8.der") catch {
        std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
        return;
    };

    pubkey.savePemFile(false, "/Users/chilkat/testData/pubkeys/chilkat.pem") catch {
        std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
        return;
    };

    pubkey.saveDerFile(true, "/Users/chilkat/testData/pubkeys/chilkat_pkcs1.der") catch {
        std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
        return;
    };

    pubkey.saveXmlFile("/Users/chilkat/testData/pubkeys/chilkat.xml") catch {
        std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Public key exported to all file formats.\n", .{});
}