Zig Requires Chilkat v11.0.0+
Zig
Verfies an RSA Signature
See more Apple Keychain Examples
Verifies an RSA signature against the original data.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
var success: bool = false;
// The following data was signed by the following example:
// RSA Sign using a Private Key on a USB Token or Smartcard
const bd = try chilkat.BinData.init();
defer bd.deinit();
var i: i32 = 0;
i = 0;
while (i <= 100) : (i += 1) {
bd.appendEncoded("000102030405060708090A0B0C0D0E0F", "hex") catch {};
}
// Load the signature
const bd_sig = try chilkat.BinData.init();
defer bd_sig.deinit();
success = if (bd_sig.loadFile("rsaSignatures/test1.sig")) true else |_| false;
if (!success) {
std.debug.print("Failed to load the RSA signature\n", .{});
return;
}
// Get the public key to be used for signature verification.
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
success = if (pub_key.loadFromFile("rsaKeys/chilkat-rsa-2048.pem")) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
return;
}
const rsa = try chilkat.Rsa.init();
defer rsa.deinit();
success = if (rsa.usePublicKey(pub_key)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
return;
}
// Verify the hash of the data against the signature.
// We pass in the original data. Internally, the hash is generated
// and used to validate the signature.
// Validating the RSA signature means two things:
// (1) the original data is exactly what was signed, and
// (2) it was signed by the owner of the RSA private key.
success = if (rsa.verifyBd(bd, "sha256", bd_sig)) true else |_| false;
if (!success) {
std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
std.debug.print("Signature invalid.\n", .{});
} else {
std.debug.print("Signature valid.\n", .{});
}
}