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

RSA Signature using Private Key from .snk File

See more RSA Examples

A .snk file (Strong Name Key file) is a file format used in the .NET ecosystem to store an RSA public/private key pair. This key pair is typically used for strong-naming assemblies, which is a process of signing .NET assemblies to ensure their integrity and to uniquely identify them.

This example loads a private key from a .snk file and creates an RSA signature.

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

    // Load the .snk and return the private key as XML.
    const xml_str = rsa.snkToXml(alloc, "./test.snk") catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

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

    rsa.usePrivateKey(priv_key) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // Sign the SHA-256 hash of the utf-8 byte representation of the contents of sb
    // Return the signature in base64 format.
    rsa.setEncodingMode("base64");
    rsa.setCharset("utf-8");
    const sig_base64 = rsa.signStringENC(alloc, "This is the text to be hashed and signed.", "sha256") catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("RSA signature as base64: {s}\n", .{sig_base64});
}