Zig Requires Chilkat v11.0.0+
Zig
Validate a JWS Using ECDSA P-256 SHA-256
See more JSON Web Signatures (JWS) Examples
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.
// This example takes a JSON signature in compact serialization format,
// and uses an ECDSA public key to validate and recover the protected header and payload.
// We only need a public key for signature validation. This is the ECDSA public key
// that is used:
// {"kty":"EC",
// "crv":"P-256",
// "x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU",
// "y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0"
// }
const sb_pub_key = try chilkat.StringBuilder.init();
defer sb_pub_key.deinit();
sb_pub_key.append("{\"kty\":\"EC\",") catch {};
sb_pub_key.append("\"crv\":\"P-256\",") catch {};
sb_pub_key.append("\"x\":\"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU\",") catch {};
sb_pub_key.append("\"y\":\"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0\"") catch {};
sb_pub_key.append("}") catch {};
const pub_key = try chilkat.PublicKey.init();
defer pub_key.deinit();
pub_key.loadFromString(try sb_pub_key.getAsString(alloc)) catch {
std.debug.print("{s}\n", .{try pub_key.getLastErrorText(alloc)});
return;
};
const jws = try chilkat.Jws.init();
defer jws.deinit();
// Set the ECC public key:
const signature_index = 0;
jws.setPublicKey(signature_index, pub_key) catch {};
// Load the JWS.
const sb_jws = try chilkat.StringBuilder.init();
defer sb_jws.deinit();
sb_jws.append("eyJhbGciOiJFUzI1NiJ9") catch {};
sb_jws.append(".") catch {};
sb_jws.append("eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt") catch {};
sb_jws.append("cGxlLmNvbS9pc19yb290Ijp0cnVlfQ") catch {};
sb_jws.append(".") catch {};
sb_jws.append("DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8ISlSA") catch {};
sb_jws.append("pmWQxfKTUJqPP3-Kg6NU1Q") catch {};
jws.loadJwsSb(sb_jws) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
// Validate the 1st (and only) signature at index 0..
const v = jws.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 jws.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", .{});
std.debug.print("--\n", .{});
// Recover the original content:
std.debug.print("Recovered content:\n", .{});
std.debug.print("{s}\n", .{try jws.getPayload(alloc, "utf-8")});
std.debug.print("--\n", .{});
// Examine the protected header:
const jose_header = try chilkat.JsonObject.init();
defer jose_header.deinit();
jws.getProtectedH(signature_index, jose_header) catch {
std.debug.print("{s}\n", .{try jws.getLastErrorText(alloc)});
return;
};
jose_header.setEmitCompact(false);
std.debug.print("Protected (JOSE) header:\n", .{});
std.debug.print("{s}\n", .{try jose_header.emit(alloc)});
// Output:
// Signature validated.
// --
// Recovered content:
// {"iss":"joe",
// "exp":1300819380,
// "http://example.com/is_root":true}
// --
// Protected (JOSE) header:
// {
// "alg": "ES256"
// }
}