Sample code for 30+ languages & platforms
Zig

DSA R,S Signature Values

See more DSA Examples

Creates a DSA signature. Gets r,s values from the signature. Re-creates the DSA signature ASN.1 from the r,s values. Then verifies the signature using the re-created ASN.1 DSA 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();

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

    crypt.setEncodingMode("hex");
    crypt.setHashAlgorithm("sha-1");

    const hash_str = try crypt.hashFileENC(alloc, "qa_data/hamlet.xml");
    std.debug.print("hash to sign: {s}\n", .{hash_str});

    const dsa = try chilkat.Dsa.init();
    defer dsa.deinit();

    const pem_private_key = try dsa.loadText(alloc, "qa_data/dsa/dsaPrivKey2.pem");
    success = if (dsa.fromPem(pem_private_key)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try dsa.getLastErrorText(alloc)});
        return;
    }

    // Load the hash to be signed into the DSA object:
    success = if (dsa.setEncodedHash("hex", hash_str)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try dsa.getLastErrorText(alloc)});
        return;
    }

    // Sign the hash.
    success = if (dsa.signHash()) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try dsa.getLastErrorText(alloc)});
        return;
    }

    // Get the ASN.1 signature.
    var asn_sig: [:0]const u8 = try dsa.getEncodedSignature(alloc, "base64");
    std.debug.print("Signature: {s}\n", .{asn_sig});

    // Examine the details of the ASN.1 signature.
    // We want to get the r,s values as hex strings..
    const asn = try chilkat.Asn.init();
    defer asn.deinit();
    success = if (asn.loadEncoded(asn_sig, "base64")) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try asn.getLastErrorText(alloc)});
        return;
    }

    // Get the ASN.1 as XML.
    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    success = if (xml.loadXml(try asn.asnToXml(alloc))) true else |_| false;
    std.debug.print("Signature as XML: \n", .{});
    std.debug.print("{s}\n", .{try xml.getXml(alloc)});

    // Sample XML shown here.
    // The r and s values are the two hex strings in the XML.

    // <?xml version="1.0" encoding="utf-8"?>
    // <sequence>
    //     <int>2C187F3AB6E47A66497B86CE97BB39E2133810F5</int>
    //     <int>588E53D3F7B69636B48FD7175E99A3961BD7D775</int>
    // </sequence>

    // Pretend we're starting with r,s
    const r = "2C187F3AB6E47A66497B86CE97BB39E2133810F5";
    const s = "588E53D3F7B69636B48FD7175E99A3961BD7D775";

    // Build the XML that will be converted to ASN.1
    xml.clear();
    xml.setTag("sequence");
    xml.newChild2("int", r);
    xml.newChild2("int", s);

    // Convert the XML to ASN.1
    success = if (asn.loadAsnXml(try xml.getXml(alloc))) true else |_| false;

    // Emit the signature as DER encoded ASN.1 (base64)
    asn_sig = try asn.getEncodedDer(alloc, "base64");

    // --------------------------------------------------------------------
    // Verify the signature using the asnSig we built from the r,s values
    // --------------------------------------------------------------------

    const dsa2 = try chilkat.Dsa.init();
    defer dsa2.deinit();

    // Load the DSA public key to be used for verification:
    const pem_public_key = try dsa2.loadText(alloc, "qa_data/dsa/dsaPubKey2.pem");
    success = if (dsa2.fromPublicPem(pem_public_key)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try dsa2.getLastErrorText(alloc)});
        return;
    }

    // Load the hash to be verified.
    success = if (dsa2.setEncodedHash("hex", hash_str)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try dsa2.getLastErrorText(alloc)});
        return;
    }

    // Load the ASN.1 signature:
    success = if (dsa2.setEncodedSignature("base64", asn_sig)) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try dsa2.getLastErrorText(alloc)});
        return;
    }

    // Verify:
    success = if (dsa2.verify()) true else |_| false;
    if (!success) {
        std.debug.print("{s}\n", .{try dsa2.getLastErrorText(alloc)});
    } else {
        std.debug.print("DSA Signature Verified!\n", .{});
    }
}