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

RSASSA-PSS Sign String to Create Base64 PCKS7 Signature

See more Digital Signatures Examples

Signs a string to create a PKCS7 signature in the base64 encoding. The signature algorithm is RSASSA-PSS with SHA256.

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

    var success: bool = false;

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

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    // Get a digital certificate with private key from a .pfx
    // (Chilkat has many different ways to provide a cert + private key for siging.
    // Using a PFX is just one possible option.)
    const pfx = try chilkat.Pfx.init();
    defer pfx.deinit();
    success = if (pfx.loadPfxFile("qa_data/rsassa-pss/privatekey.pfx", "PFX_PASSWORD")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
        return;
    }

    // Get the certificate to be used for signing.
    // (The typical case for a PFX is that it contains a cert with an associated private key,
    // as well as other certificates in the chain of authentication.  The cert with the private
    // key should be in the first position at index 0.)

    const cert = try chilkat.Cert.init();
    defer cert.deinit();
    success = if (pfx.certAt(0, cert)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try pfx.getLastErrorText(alloc)});
        return;
    }

    crypt.setSigningCert(cert) catch {};

    // Indicate that RSASSA-PSS with SHA256 should be used.
    crypt.setSigningAlg("pss");
    crypt.setHashAlgorithm("sha256");

    crypt.setEncodingMode("base64");

    // Sign a string and return the base64 PKCS7 detached signature
    const original_text = "This is a test";
    const pkcs7sig = try crypt.signStringENC(alloc, original_text);
    std.debug.print("Detached Signature:\n", .{});
    std.debug.print("{s}\n", .{pkcs7sig});

    // This signature looks like this:
    // MIIG5wYJKoZIhvcNAQcCoIIG2DCCBtQCAQExDzANBgl .. YToLqEwTdU87ox5g7rvw==

    // The ASN.1 of the signature can be examined by browsing to https://lapo.it/asn1js/ ,
    // then copy-and-paste the Base64 signature into the form and decode..

    // The signature can be verified against the original data like this:
    success = if (crypt.verifyStringENC(original_text, pkcs7sig)) true else |_| false;
    std.debug.print("Signature verified: {}\n", .{success});
    success = if (crypt.verifyStringENC("Not the original text", pkcs7sig)) true else |_| false;
    std.debug.print("Signature verified: {}\n", .{success});

    // Now we'll create an opaque signature (the opposite of a detached signature).
    // An opaque signature is a PKCS7 message that contains both the original data and
    // the signature.  The verification process extracts the original data.
    const opaque_sig = try crypt.opaqueSignStringENC(alloc, original_text);
    std.debug.print("Opaque Signature:\n", .{});
    std.debug.print("{s}\n", .{opaque_sig});

    // The ASN.1 of the signature can be examined by browsing to https://lapo.it/asn1js/ ,
    // then copy-and-paste the Base64 signature into the form and decode..

    // We can verify and extract the original data:
    const orig_txt = crypt.opaqueVerifyStringENC(alloc, opaque_sig) catch {
        std.debug.print("Signature verification failed.\n", .{});
        std.debug.print("{s}\n", .{try crypt.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Signature verified.\n", .{});
    std.debug.print("Extracted text:{s}\n", .{orig_txt});
}