Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.0.0+

Validate a JWS Using ECDSA P-521 SHA-512

See more JSON Web Signatures (JWS) Examples

Validates a JSON Web Signature (JWS) that uses ECDSA P-521 SHA-512

Chilkat Zig Downloads

Zig
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-521",
    //       "x":"AekpBQ8ST8a8VcfVOTNl353vSrDCLLJXmPk06wTjxrrjcBpXp5EOnYG_NjFZ6OvLFV1jSfS9tsz4qUxcWceqwQGk",
    //       "y":"ADSmRA43Z1DSNx_RvcLI87cdL07l6jQyyBXMoxVg_l2Th-x3S1WDhjDly79ajL4Kkd0AZMaZmh9ubmf63e3kyMj2"
    //      }

    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-521\",") catch {};
    sb_pub_key.append("\"x\":\"AekpBQ8ST8a8VcfVOTNl353vSrDCLLJXmPk06wTjxrrjcBpXp5EOnYG_NjFZ6OvLFV1jSfS9tsz4qUxcWceqwQGk\",") catch {};
    sb_pub_key.append("\"y\":\"ADSmRA43Z1DSNx_RvcLI87cdL07l6jQyyBXMoxVg_l2Th-x3S1WDhjDly79ajL4Kkd0AZMaZmh9ubmf63e3kyMj2\"") 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("eyJhbGciOiJFUzUxMiJ9") catch {};
    sb_jws.append(".") catch {};
    sb_jws.append("UGF5bG9hZA") catch {};
    sb_jws.append(".") catch {};
    sb_jws.append("AdwMgeerwtHoh-l192l60hp9wAHZFVJbLfD_UxMi70cwnZOYaRI1bKPWROc-mZZq") catch {};
    sb_jws.append("wqT2SI-KGDKB34XO0aw_7XdtAG8GaSwFKdCAPZgoXD2YBJZCPEX3xKpRwcdOO8Kp") catch {};
    sb_jws.append("EHwJjyqOgzDO7iKvU8vcnwNrmxYbSW9ERBXukOXolLzeO_Jn") 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:
    // (the string "Payload" was the content that was signed.)

    //     Signature validated.
    //     --
    //     Recovered content:
    //     Payload
    //     --
    //     Protected (JOSE) header:
    //     {
    //       "alg": "ES512"
    //     }
}