Zig Requires Chilkat v11.0.0+
Zig
Get a Certificate's Key Size
See more Certificates Examples
Demonstrates how to get the RSA key size of a certificate (for example, 1024-bit, 2048-bit, etc.)Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// For this example, I have a certificate in raw base64 format (not PEM),
// that looks like this: "MIIGkDCCBHigAwIBAgIUMDA ... s/iqLsLA=="
const sb_cert_base64 = try chilkat.StringBuilder.init();
defer sb_cert_base64.deinit();
try sb_cert_base64.loadFile("qa_data/certs/base64Cert.txt", "utf-8");
const cert = try chilkat.Cert.init();
defer cert.deinit();
cert.loadFromBase64(try sb_cert_base64.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
return;
};
// Get the public key.
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
cert.getPublicKey(pub_key) catch {};
var num_bits: i32 = pub_key.getKeySize();
std.debug.print("Number of bits = {d}\n", .{num_bits});
// If using an older version of Chilkat, the key size can be obtained like this:
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadXml(try pub_key.getXml(alloc)) catch {};
const bin_dat = try chilkat.BinData.init();
defer bin_dat.deinit();
bin_dat.appendEncoded(try xml.getChildContent(alloc, "Modulus"), "base64") catch {};
num_bits = 8 * bin_dat.getNumBytes();
std.debug.print("Number of bits = {d}\n", .{num_bits});
}