Sample code for 30+ languages & platforms
Zig

Export a Public Key as DER into a BinData

Demonstrates the Chilkat PublicKey.GetDerBd method, which writes the public key as binary DER into a BinData. The first argument selects the representation (as for GetPem) and the second is the BinData that receives the bytes (its contents are replaced).

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: The in-memory binary export: the raw DER lands in a BinData ready to hash, transmit, or hand to another API without a temporary file. It is the BinData counterpart to the text exporters, and the first argument picks the same PKCS #1 versus SubjectPublicKeyInfo structure.

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

    // Load a public key to export.  LoadFromFile inspects the content, so it accepts DER, PEM, XML,
    // JWK, and other supported representations.
    const pub_key = try chilkat.PublicKey.init();
    defer pub_key.deinit();
    pub_key.loadFromFile("qa_data/public.pem") catch {
        std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
        return;
    };

    // Write the public key as binary DER into a BinData.  The 1st argument selects the representation
    // (as for GetPem) and the 2nd is the BinData that receives the bytes (its contents are replaced).
    const prefer_pkcs1 = false;
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    pub_key.getDerBd(prefer_pkcs1, bd) catch {
        std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Exported {d} DER bytes.\n", .{bd.getNumBytes()});
}