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

RSASSA-PSS Algorithm with SHA256 Hashing

See more RSA Examples

RSA encrypt a SHA256 hash with OAEP padding.

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

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

    // Load the private key object from a PEM file.
    // (To load from a PEM string, call LoadPem instead.)
    privkey.loadPemFile("somePath/myPrivateKey.pem") catch {
        std.debug.print("{s}\n", .{try privkey.getLastErrorText(alloc)});
        return;
    };

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();
    // Use RSA-PSS by setting PkcsPadding = false
    rsa.setPkcsPadding(false);
    // Use SHA256
    rsa.setOaepHash("SHA-256");

    rsa.usePrivateKey(privkey) catch {};

    // Generate a base64 signature.
    rsa.setEncodingMode("base64");

    const sig_str = rsa.signStringENC(alloc, "String to be signed", "SHA-256") catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Signature: {s}\n", .{sig_str});
}