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

Load PEM Public/Private Key into RSA Object

See more RSA Examples

Demonstrates how to load a PEM key into the Chilkat RSA object.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();

    // First demonstrate importing a PEM public key:
    const public_key_pem = "PEM public-key data goes here";
    const pubkey = try chilkat.PublicKey.init();
    defer pubkey.deinit();

    pubkey.loadFromString(public_key_pem) catch {
        std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
        return;
    };

    rsa.usePublicKey(pubkey) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // Demonstrate importing a PEM private key:
    const private_key_pem = "PEM private-key data goes here";
    const privkey = try chilkat.PrivateKey.init();
    defer privkey.deinit();

    // If the private key PEM is protected with a password, then
    // call LoadEncryptedPem.  Otherwise call LoadPem.
    privkey.loadPem(private_key_pem) catch {
        std.debug.print("{s}\n", .{try privkey.getLastErrorText(alloc)});
        return;
    };

    rsa.usePrivateKey(privkey) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("OK!\n", .{});
}