Sample code for 30+ languages & platforms
Zig

Apple Keychain - Import RSA Private Key

See more Apple Keychain Examples

Imports an RSA private key to the Apple Keychain.

Note: This example requires Chilkat v10.0.0 or greater.

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 pem_path = "/Users/chilkat/qa_data/rsa/rsaPrivKey_pkcs8.pem";
    // Provide a password if your PEM is encrypted. Otherwise pass the empty string.
    const pem_password = "";

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();
    priv_key.loadAnyFormatFile(pem_path, pem_password) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    // In Apple Keychain, the label for an item such as a private key is a user-friendly identifier that helps distinguish one item from
    // another in the Keychain. It is typically a brief name or description associated with the item.
    // The label is displayed in the Keychain Access application and is useful for quickly identifying the purpose
    // or source of a specific key, certificate, or password entry.

    // The label does not affect the functionality or security of the item; it is purely for organizational purposes.
    // You can edit or assign labels to make your Keychain easier to navigate.

    // On MacOS, you can save the private key to the Keychain by calling SavePkcs8File with a "filename" in the format"keychain:<label>"
    priv_key.savePkcs8File("keychain:MyRsaKey123") catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

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

    // Here you can see:
    //
    // image
}