Zig
Zig
ECDSA Sign and Verify
See more ECC Examples
Demonstrates how to create an ECDSA signature on the SHA256 hash of some data, and then verify.Chilkat Zig Downloads
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.
// First load an ECDSA private key to be used for signing.
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
priv_key.loadEncryptedPemFile("qa_data/ecc/secp256r1-key-pkcs8-secret.pem", "secret") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
// Sign the SHA256 hash of some data.
const bd = try chilkat.BinData.init();
defer bd.deinit();
bd.loadFile("qa_data/hamlet.xml") catch {
std.debug.print("Failed to load file to be hashed.\n", .{});
return;
};
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setHashAlgorithm("sha256");
crypt.setEncodingMode("base64");
const hash_str = try crypt.hashBdENC(alloc, bd);
const ecdsa = try chilkat.Ecc.init();
defer ecdsa.deinit();
const prng = try chilkat.Prng.init();
defer prng.deinit();
// Returns ASN.1 signature as a base64 string.
const sig = try ecdsa.signHashENC(alloc, hash_str, "base64", priv_key, prng);
std.debug.print("sig = {s}\n", .{sig});
// The signature is in ASN.1 format (which may be described as the "encoded DSS signature").
// SEQUENCE (2 elem)
// INTEGER (255 bit) 4849395540832462044300553275435608522154141569743642905628579547100940...
// INTEGER (255 bit) 3680701124244788134409868118208591399799457104230118295614152238560005...
// If you wish, you can get the r and s components of the signature like this:
const asn = try chilkat.Asn.init();
defer asn.deinit();
asn.loadEncoded(sig, "base64") catch {};
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadXml(try asn.asnToXml(alloc)) catch {};
std.debug.print("{s}\n", .{try xml.getXml(alloc)});
// We now have this:
// <?xml version="1.0" encoding="utf-8"?>
// <sequence>
// <int>6650D422D86BA4A228B5617604E59052591B9B2C32EF324C44D09EF67E5F0060</int>
// <int>0CFD9F6AC85042FC70F672C141BA6B2A4CAFBB906C3D907BCCC1BED62B28326F</int>
// </sequence>
// Get the "r" and "s" as hex strings
const r = try xml.getChildContentByIndex(alloc, 0);
const s = try xml.getChildContentByIndex(alloc, 1);
std.debug.print("r = {s}\n", .{r});
std.debug.print("s = {s}\n", .{s});
// --------------------------------------------------------------------
// Now verify against the hash of the original data.
// Get the corresponding public key.
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
pub_key.loadFromFile("qa_data/ecc/secp256r1-pub.pem") catch {
std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
return;
};
// We already have the SHA256 hash of the original data (hashStr) so no need to re-do it..
const ecc2 = try chilkat.Ecc.init();
defer ecc2.deinit();
const result = ecc2.verifyHashENC(hash_str, sig, "base64", pub_key);
if (result != 1) {
std.debug.print("{s}\n", .{try ecc2.getLastErrorText(alloc)});
return;
}
std.debug.print("Verified!\n", .{});
// Note: If we have only r,s and wish to reconstruct the ASN.1 signature, we do it like this:
const xml2 = try chilkat.Xml.init();
defer xml2.deinit();
xml2.setTag("sequence");
xml2.newChild2("int", r);
xml2.newChild2("int", s);
const asn2 = try chilkat.Asn.init();
defer asn2.deinit();
asn2.loadAsnXml(try xml2.getXml(alloc)) catch {};
const encoded_sig = try asn2.getEncodedDer(alloc, "base64");
std.debug.print("encoded DSS signature: {s}\n", .{encoded_sig});
// You can go to https://lapo.it/asn1js/ and copy/paste the base64 encodedSig into the online tool, then press the "decode" button.
// You will see the ASN.1 such as this:
// SEQUENCE (2 elem)
// INTEGER (255 bit) 4849395540832462044300553275435608522154141569743642905628579547100940...
// INTEGER (255 bit) 3680701124244788134409868118208591399799457104230118295614152238560005...
}