Sample code for 30+ languages & platforms
Zig

X509Certificate GetCertHash SHA256

See more Certificates Examples

Shows how to get the SHA256 hash of a certificate.

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 cert = try chilkat.Cert.init();
    defer cert.deinit();

    // Load a certificate object with a certificate.
    // The certificate object could have come from any source,
    // or perhaps was obtained from another Chilkat function.
    // The source does not matter.  What matters is that we have
    // a cert object loaded with the certificate..
    cert.loadFromFile("qa_data/certs/cert_test123.cer") catch {
        std.debug.print("{s}\n", .{try cert.getLastErrorText(alloc)});
        return;
    };

    // Get the binary DER of the certificae, and compute the SHA256 hash:
    const bd_der = try chilkat.BinData.init();
    defer bd_der.deinit();
    cert.exportCertDerBd(bd_der) catch {};

    // Get the base64 representation of the SHA256 hash.
    const cert_hash = try bd_der.getHash(alloc, "sha256", "base64");

    std.debug.print("Certificate hash in base64 = {s}\n", .{cert_hash});
}