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

Duplicating OpenSSL rsautl (creating RSA signatures)

See more RSA Examples

Demonstrates how to duplicate OpenSSL rsautil RSA signatures.

The Chilkat RSA component's methods for creating RSA signatures (SignBytes, SignBytesENC, SignString, and SignStringENC) are very different from OpenSSL's rsautl command. First, we'll explain what Chilkat's signing methods do, and then what OpenSSL's rsautl does. New signing methods have been added to Chilkat RSA to duplicate OpenSSL rsautl: OpenSslSignBytes, OpenSslSignBytesENC, OpenSslSignString, and OpenSslSignStringENC.

Here's what Chilkat's RSA Sign* methods do:
(Note: Chilkat RSA's Sign* methods generate signatures according to RSA Laboratories' "PKCS #1 v2.0: RSA Cryptography Standard".)

  1. Input data is hashed using the hashing algorithm specified.
  2. The hash is padded using either PKCS1 v1.5 or PKCS1 PSS padding. PKCS v1.5 involves encoding to ASN.1 before padding.
  3. The padded hash is finally RSA signed (i.e. modular exponentiation) and the signature is returned.

OpenSSL rsautl is very different. Here's what it does:

  1. The input data is not hashed. It must be a small enough amount of data such that it can be padded and signed. PKCS v1.5 padding is always used. The data is not ASN.1 encoded before padding.
  2. The PKCS v1.5 padded data is RSA signed and the signature is returned.
The new OpenSsl* methods are designed to duplicate OpenSSL's rsautil signature functionality.

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 assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

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

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();
    rsa.usePrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    const str_data = "secret";
    const bd = try chilkat.BinData.init();
    defer bd.deinit();
    bd.appendString(str_data, "utf-8") catch {};

    try rsa.signRawBd(bd);
    const hex_sig = try bd.getEncoded(alloc, "hex");
    std.debug.print("{s}\n", .{hex_sig});

    // Recover the data using the corresponding public key:
    const pub_key = try chilkat.PublicKey.init();
    defer pub_key.deinit();

    // Load the public key from a PEM file:
    pub_key.loadFromFile("public.pem") catch {
        std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
        return;
    };

    const rsa2 = try chilkat.Rsa.init();
    defer rsa2.deinit();
    rsa2.usePublicKey(pub_key) catch {
        std.debug.print("{s}\n", .{try rsa2.getLastErrorText(alloc)});
        return;
    };

    const bd2 = try chilkat.BinData.init();
    defer bd2.deinit();
    bd2.appendEncoded(hex_sig, "hex") catch {};

    // Recover the original data.
    rsa2.verifyRawBd(bd2) catch {
        std.debug.print("{s}\n", .{try rsa2.getLastErrorText(alloc)});
        return;
    };

    const original_data = try bd2.getString(alloc, "utf-8");
    std.debug.print("{s}\n", .{original_data});
}