Zig Requires Chilkat v11.0.0+
Zig
Create PKCS1 RSA Signature with PEM Private Key
See more RSA Examples
Demonstrates how to load a private key from a PEM file and create a PKCS1 RSA digital signature.Chilkat Zig Downloads
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 pkey = try chilkat.PrivateKey.init();
defer pkey.deinit();
// Load the private key from an RSA PEM file:
pkey.loadPemFile("pvkey2_rsa.pem") catch {
std.debug.print("{s}\n", .{try pkey.getLastErrorText(alloc)});
return;
};
const rsa = try chilkat.Rsa.init();
defer rsa.deinit();
// Import the private key into the RSA object:
rsa.usePrivateKey(pkey) catch {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
return;
};
// Get the signature as hex.
rsa.setEncodingMode("hex");
const str_data = "This is the string to be signed.";
// Sign the string using the sha-1 hash algorithm.
// Other valid choices are "md2", "md5", "sha256",
// "sha384", and "sha512".
const hex_sig = try rsa.signStringENC(alloc, str_data, "sha-1");
std.debug.print("{s}\n", .{hex_sig});
std.debug.print("Success!\n", .{});
}