Zig
Zig
ECDSA Sign Data and Verify Signature
See more ECC Examples
Demonstrates using the Elliptic Curve Digital Signature Algorithm to hash data and sign it. Also demonstrates how to verify the ECDSA signature.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// To create an ECDSA signature, the data first needs to be hashed. Then the hash
// is signed.
// Use Chilkat Crypt2 to generate a hash for any of the following
// hash algorithms: SHA256, SHA384, SHA512, SHA1, MD5, MD2, HAVAL, RIPEMD128/160/256/320
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setHashAlgorithm("SHA256");
crypt.setCharset("utf-8");
crypt.setEncodingMode("base64");
// Hash a string.
const hash1 = try crypt.hashStringENC(alloc, "The quick brown fox jumps over the lazy dog");
std.debug.print("hash1 = {s}\n", .{hash1});
// Or hash a file..
const hash2 = try crypt.hashFileENC(alloc, "qa_data/hamlet.xml");
std.debug.print("hash2 = {s}\n", .{hash2});
// (The Crypt2 API provides many other ways to hash data..)
// -----------------------------------------------------------
// An ECDSA private key is used for signing. The public key is for signature verification.
// Load our ECC private key.
// Our private key file contains this:
// // -----BEGIN PRIVATE KEY-----
// MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg3J8q/24D1sEKGdP9
// 72MGYElLGpw/a56Y3t6pfON3uhShRANCAATlSmoizyhAwoYZAOuFBATl07/1RR54
// a1Dzfm16grxJe666AGKR+bSs24hk7TEpaeCTvT8YOOM3l+xKFg7zq6Q9
// -----END PRIVATE KEY-----
const priv_key = try chilkat.PrivateKey.init();
defer priv_key.deinit();
priv_key.loadPemFile("qa_data/ecc/secp256r1-key-pkcs8.pem") catch {
std.debug.print("{s}\n", .{try priv_key.getLastErrorText(alloc)});
return;
};
// We'll need a PRNG source for random number generation.
// Use Chilkat's PRNG (for the Fortuna PRNG algorithm).
const prng = try chilkat.Prng.init();
defer prng.deinit();
// Sign the hash..
const ecdsa = try chilkat.Ecc.init();
defer ecdsa.deinit();
const ecdsa_sig_base64 = ecdsa.signHashENC(alloc, hash1, "base64", priv_key, prng) catch {
std.debug.print("{s}\n", .{try ecdsa.getLastErrorText(alloc)});
return;
};
std.debug.print("ECDSA signature = {s}\n", .{ecdsa_sig_base64});
// -----------------------------------------------------------
// Now let's verify the signature using the public key.
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
pub_key.loadFromFile("qa_data/ecc/secp256r1-pubkey.pem") catch {
std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
return;
};
const result = ecdsa.verifyHashENC(hash1, ecdsa_sig_base64, "base64", pub_key);
if (result == 1) {
std.debug.print("Signature is valid.\n", .{});
return;
}
if (result == 0) {
std.debug.print("Signature is invalid.\n", .{});
return;
}
if (result < 0) {
std.debug.print("{s}\n", .{try ecdsa.getLastErrorText(alloc)});
std.debug.print("The VerifyHashENC method call failed.\n", .{});
return;
}
}