Zig Requires Chilkat v11.0.0+
Zig
RSA Signature with Certificate's Private Key from PFX
See more RSA Examples
Demonstrates how to use a certificate's private key from a PFX file to create an RSA 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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
const cert_store = try chilkat.CertStore.init();
defer cert_store.deinit();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
cert_store.loadPfxFile("chilkat.pfx", "test") catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
// Find the certificate by the subject common name:
const json_cn = try chilkat.JsonObject.init();
defer json_cn.deinit();
json_cn.updateString("CN", "cert common name") catch {};
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert_store.findCert(json_cn, cert) catch {
std.debug.print("{s}\n", .{try cert_store.getLastErrorText(alloc)});
return;
};
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
cert.getPrivateKey(priv_key) catch {
std.debug.print("{s}\n", .{try cert.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;
};
// Encode the signature as a hex string
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 "sha-256", "md2" and "md5".
const hex_sig = try rsa.signStringENC(alloc, str_data, "sha-1");
std.debug.print("{s}\n", .{hex_sig});
std.debug.print("Success!\n", .{});
}