Sample code for 30+ languages & platforms
Zig

Get a Private Key's Raw Hex Value

See more Private Key Examples

Demonstrates the Chilkat PrivateKey.GetRawHex method, which returns the raw private value as lowercase hexadecimal and replaces the contents of the supplied StringBuilder with the corresponding raw public key.

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: This drops below the structured container formats to the bare key bytes as hex, which is meaningful chiefly for fixed-size keys such as EC and Ed25519 where a key is essentially a fixed byte string. It is the export to use when another system wants raw values rather than PEM or JWK — the counterpart to LoadEd25519. Both the returned private hex and the public value are produced in one call.

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 private key to export.
    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadPemFile("qa_data/private.pem") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    // GetRawHex returns the raw private value as lowercase hex, and replaces the contents of the
    // StringBuilder argument with the corresponding raw public key.  This is most meaningful for
    // fixed-size keys such as EC and Ed25519.
    const sb_public = try chilkat.StringBuilder.init();
    defer sb_public.deinit();
    const priv_hex = priv_key.getRawHex(alloc, sb_public) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Raw private value: {s}\n", .{priv_hex});
    std.debug.print("Raw public key: {s}\n", .{try sb_public.getAsString(alloc)});
}