Zig Requires Chilkat v11.0.0+
Zig
Create a JWS Using ECDSA P-256 SHA-256
See more JSON Web Signatures (JWS) Examples
Creates and validates a JSON Web Signature (JWS) that uses ECDSA P-256 SHA-256Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Use the following ECC key loaded from JWK format.
const sb_jwk = try chilkat.StringBuilder.init();
defer sb_jwk.deinit();
sb_jwk.append("{\"kty\":\"EC\",") catch {};
sb_jwk.append("\"crv\":\"P-256\",") catch {};
sb_jwk.append("\"x\":\"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU\",") catch {};
sb_jwk.append("\"y\":\"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0\",") catch {};
sb_jwk.append("\"d\":\"jpsQnnGQmL-YBIffH1136cspYG6-0iY7X1fCE9-E9LI\"") catch {};
sb_jwk.append("}") catch {};
const ecc_key = try chilkat.PrivateKey.init();
defer ecc_key.deinit();
// Note: This example loads the ECDSA key from JWK format. Any format can be loaded
// into the private key object. (See the online reference documentation..)
ecc_key.loadJwk(try sb_jwk.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try ecc_key.getLastErrorText(alloc)});
return;
};
// Create the JWS Protected Header
const jws_prot_hdr = try chilkat.JsonObject.init();
defer jws_prot_hdr.deinit();
jws_prot_hdr.appendString("alg", "ES256") catch {};
const jws = try chilkat.Jws.init();
defer jws.deinit();
// Set the protected header:
var signature_index: i32 = 0;
jws.setProtectedHeader(signature_index, jws_prot_hdr) catch {};
// Set the ECC key:
jws.setPrivateKey(signature_index, ecc_key) catch {};
// Set the payload.
const b_include_bom = false;
const payload_str = "In our village, folks say God crumbles up the old moon into stars.";
jws.setPayload(payload_str, "utf-8", b_include_bom) catch {};
// Create the JWS
// By default, the compact serialization is used.
const jws_compact = jws.createJws(alloc) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
std.debug.print("JWS: {s}\n", .{jws_compact});
// Note: ECC signatures use random values, so the output will be different each time a signature is produced.
// sample output:
// JWS: eyJhbGciOiJFUzI1NiJ9.SW4gb3VyIHZpbGxhZ2UsIGZvbGtzIHNheSBHb2QgY3J1bWJsZXMgdXAgdGhlIG9sZCBtb29uIGludG8gc3RhcnMu.J6EGTNkeuEjehHHBu0SOKzifm8r35wbx49A5964pxzB7gannyZZPMkK7vmlaPJ-K8Xmm8cZsQySAPC12Xd8avw
// Now load the JWS, validate, and recover the original text.
const jws2 = try chilkat.Jws.init();
defer jws2.deinit();
// Load the JWS.
try jws2.loadJws(jws_compact);
const ecc_pub_key = try chilkat.PublicKey.init();
defer ecc_pub_key.deinit();
ecc_key.toPublicKey(ecc_pub_key) catch {};
// Set the ECC public key used for validation.
signature_index = 0;
jws2.setPublicKey(signature_index, ecc_pub_key) catch {};
// Validate the 1st (and only) signature at index 0..
const v = jws2.validate(signature_index);
if (v < 0) {
// Perhaps Chilkat was not unlocked or the trial expired..
std.debug.print("Method call failed for some other reason.\n", .{});
std.debug.print("{s}\n", .{try jws2.getLastErrorText(alloc)});
return;
}
if (v == 0) {
std.debug.print("Invalid signature. The ECC key was incorrect, the JWS was invalid, or both.\n", .{});
return;
}
// If we get here, the signature was validated..
std.debug.print("Signature validated.\n", .{});
// Recover the original content:
std.debug.print("{s}\n", .{try jws2.getPayload(alloc, "utf-8")});
// Examine the protected header:
const jose_header = try chilkat.JsonObject.init();
defer jose_header.deinit();
jws2.getProtectedH(signature_index, jose_header) catch {};
jose_header.setEmitCompact(false);
std.debug.print("Protected (JOSE) header:\n", .{});
std.debug.print("{s}\n", .{try jose_header.emit(alloc)});
// Output:
// Signature validated.
// In our village, folks say God crumbles up the old moon into stars.
// Protected (JOSE) header:
// {
// "alg": "ES256"
// }
}