Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Duplicate openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat

See more OpenSSL Examples

Demonstrates how to duplicate this OpenSSL command:
openssl dgst -sha256 -sign private.pem -out sha256.sig in.dat
The in.dat file can contain text or binary data of any type. The OpenSSL command does the following:
  1. Creates a SHA256 digest of the contents of the input file
  2. Signs the SHA256 digest using the private key.

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

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Load the private key from an PEM file:
    pkey.loadPemFile("private.pem") catch {
        std.debug.print("{s}\n", .{try pkey.getLastErrorText(alloc)});
        return;
    };

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();

    // Import the private key into the RSA component:
    rsa.usePrivateKey(pkey) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // OpenSSL uses big-endian.
    rsa.setLittleEndian(false);

    // Load the file to be signed.
    const bd_file_data = try chilkat.BinData.init();
    defer bd_file_data.deinit();
    try bd_file_data.loadFile("in.dat");

    const bd_sig = try chilkat.BinData.init();
    defer bd_sig.deinit();
    rsa.signBd(bd_file_data, "sha256", bd_sig) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // Save the binary signature to a file.
    bd_sig.writeFile("signature.sig") catch {
        std.debug.print("Failed to write signature.sig.\n", .{});
        return;
    };

    std.debug.print("Success.\n", .{});
}