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

RSA Sign Using Private Key from .pfx/.p12 to Base64 Signature

See more RSA Examples

Demonstrates how to RSA sign something using a private key loaded from a .pfx/.p12. The RSA signature is returned in Base64 encoded format.

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

    // Load the .pfx/.p12
    const pfx = try chilkat.Pfx.init();
    defer pfx.deinit();
    pfx.loadPfxFile("qa_data/pfx/myKey.p12", "myPassword") catch {
        std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
        return;
    };

    // Get the default private key.

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

    // Import the private key into the RSA component:
    rsa.usePrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // Get the signature in base64
    rsa.setEncodingMode("base64");

    const str_data = "This is the string to be signed.";

    // Sign the string using the sha256 hash algorithm.
    // Other valid choices are "sha384", "sha512", "sha-1", "md2" and "md5".
    const base64_sig = try rsa.signStringENC(alloc, str_data, "sha256");

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

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