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

RSA Sign using Base64 Private Key

See more RSA Examples

Signs a string using a non-encrypted RSA private key in base64 encoding. Returns the RSA signature as a base64 string.

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

    const priv_key = try chilkat.PrivateKey.init();
    defer priv_key.deinit();

    const sb_pem = try chilkat.StringBuilder.init();
    defer sb_pem.deinit();
    sb_pem.appendLine("-----BEGIN RSA PRIVATE KEY-----", true) catch {};
    sb_pem.appendLine("MIIC .... j5A==", true) catch {};
    sb_pem.appendLine("-----END RSA PRIVATE KEY-----", true) catch {};

    priv_key.loadPem(try sb_pem.getAsString(alloc)) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

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

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

    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    bd.appendString("12345678", "utf-8") catch {};

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

    // Get the base64 RSA signature.
    std.debug.print("{s}\n", .{try bd.getEncoded(alloc, "base64")});

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

    const str_original = try bd.getString(alloc, "utf-8");
    std.debug.print("{s}\n", .{str_original});
}