Zig Requires Chilkat v11.0.0+
Zig
How to Generate an Elliptic Curve Shared Secret
See more ECC Examples
Demonstrates how to generate an ECC (Elliptic Curve Cryptography) shared secret. Imagine a cilent has one ECC private key, the server has another. A shared secret is computed by each side providing it's public key to the other. The private keys are kept private.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.
// This example includes both client-side and server-side code.
// Each code segment is marked as client-side or server-side.
// Imagine these segments are running on separate computers...
// -----------------------------------------------------------------
// (Client-Side) Generate an ECC key, save the public part to a file.
// -----------------------------------------------------------------
const prng_client = try chilkat.Prng.init();
defer prng_client.deinit();
const ecc_client = try chilkat.Ecc.init();
defer ecc_client.deinit();
const priv_key_client = try chilkat.PrivateKey.init();
defer priv_key_client.deinit();
ecc_client.genKey("secp256r1", prng_client, priv_key_client) catch {
std.debug.print("{s}\n", .{try ecc_client.getLastErrorText(alloc)});
return;
};
const pub_key_client = try chilkat.PublicKey.init();
defer pub_key_client.deinit();
priv_key_client.toPublicKey(pub_key_client) catch {};
pub_key_client.savePemFile(false, "qa_output/eccClientPub.pem") catch {};
// -----------------------------------------------------------------
// (Server-Side) Generate an ECC key, save the public part to a file.
// -----------------------------------------------------------------
const prng_server = try chilkat.Prng.init();
defer prng_server.deinit();
const ecc_server = try chilkat.Ecc.init();
defer ecc_server.deinit();
const priv_key_server = try chilkat.PrivateKey.init();
defer priv_key_server.deinit();
ecc_server.genKey("secp256r1", prng_server, priv_key_server) catch {};
const pub_key_server = try chilkat.PublicKey.init();
defer pub_key_server.deinit();
priv_key_server.toPublicKey(pub_key_server) catch {};
pub_key_server.savePemFile(false, "qa_output/eccServerPub.pem") catch {};
// -----------------------------------------------------------------
// (Client-Side) Generate the shared secret using our private key, and the other's public key.
// -----------------------------------------------------------------
// Imagine that the server sent the public key PEM to the client.
// (This is simulated by loading the server's public key from the file.
const pub_key_from_server = try chilkat.PublicKey.init();
defer pub_key_from_server.deinit();
pub_key_from_server.loadFromFile("qa_output/eccServerPub.pem") catch {};
const shared_secret1 = try ecc_client.sharedSecretENC(alloc, priv_key_client, pub_key_from_server, "base64");
// -----------------------------------------------------------------
// (Server-Side) Generate the shared secret using our private key, and the other's public key.
// -----------------------------------------------------------------
// Imagine that the client sent the public key PEM to the server.
// (This is simulated by loading the client's public key from the file.
const pub_key_from_client = try chilkat.PublicKey.init();
defer pub_key_from_client.deinit();
pub_key_from_client.loadFromFile("qa_output/eccClientPub.pem") catch {};
const shared_secret2 = try ecc_server.sharedSecretENC(alloc, priv_key_server, pub_key_from_client, "base64");
// ---------------------------------------------------------
// Examine the shared secrets. They should be the same.
// Both sides now have a secret that only they know.
// ---------------------------------------------------------
std.debug.print("{s}\n", .{shared_secret1});
std.debug.print("{s}\n", .{shared_secret2});
}