Sample code for 30+ languages & platforms
Zig

Get RSA Key Modulus from .cer or .key

See more Certificates Examples

Demonstrates how to get the RSA key modulus from either the certificate (.cer) or RSA key (.key). OpenSSL commands to do the same would be:
openssl x509 -inform DER -in "test.cer"  -modulus -noout 
or
openssl pkcs8 -inform DER -in​ "test.key"​ -outform PEM -passin pass:"12345​678a​"
   | openssl rsa -inform PEM -modulus -noout 

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();

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

    const password = "12345678a";
    priv_key.loadPkcs8EncryptedFile("qa_data/certs/test_12345678a.key", password) catch {
        std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
        return;
    };

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(try priv_key.getXml(alloc)) catch {};

    // The XML contains the parts of the key in base64.
    std.debug.print("Private Key XML:\n", .{});
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // We can get the base64 modulus like this:
    var modulus: [:0]const u8 = try xml.getChildContent(alloc, "Modulus");
    std.debug.print("base64 modulus = {s}\n", .{modulus});

    // To convert to hex:
    const bin_dat = try chilkat.BinData.init();
    defer bin_dat.deinit();
    bin_dat.appendEncoded(modulus, "base64") catch {};
    var hex_modulus: [:0]const u8 = try bin_dat.getEncoded(alloc, "hex");
    std.debug.print("hex modulus = {s}\n", .{hex_modulus});

    // Now get the modulus from the cert:
    const cert = try chilkat.Cert.init();
    defer cert.deinit();

    cert.loadFromFile("qa_data/certs/test_12345678a.cer") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // The cert contains the public key, which is composed of the
    // modulus + exponent (for RSA keys).
    const pub_key = try chilkat.PublicKey.init();
    defer pub_key.deinit();
    cert.getPublicKey(pub_key) catch {};

    xml.loadXml(try pub_key.getXml(alloc)) catch {};
    std.debug.print("Public Key XML:\n", .{});
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Proceed in the same way as before....
    modulus = try xml.getChildContent(alloc, "Modulus");
    std.debug.print("base64 modulus = {s}\n", .{modulus});

    // To convert to hex:
    bin_dat.clear() catch {};
    bin_dat.appendEncoded(modulus, "base64") catch {};
    hex_modulus = try bin_dat.getEncoded(alloc, "hex");
    std.debug.print("hex modulus = {s}\n", .{hex_modulus});
}