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

Load JWK into PublicKey Object

See more JSON Web Signatures (JWS) Examples

Demonstrates how to load a Java Web Key (JWK) into a Chilkat public key object.

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();

    // Imagine we have this JWK:

    // {
    // "kty":"RSA",
    // "kid":"bilbo.baggins@hobbiton.example",
    // "use":"sig",
    // "n":"26381066103266289976070039679240841999340706859156341823971393693544846472963839705790075117105560154606562987481050889935247188300178764426769543782451174831475924241862817098997865881169498995546309260357488285676768114714548754913374731868063116787903918383610614912211576698081222844057810710088656983077876282582286966085572172471849443305250452393766960762340311496052418952315081937000716222751229644449475751932222180738210988190709824209639002026650048655399108647971010890335494074025258268035713167380057822555485524738518699437692145859633592396626258745090144803752145540471688039673495588314804430463791",
    // "e":"65537"
    // }
    //

    const sb_jwk = try chilkat.StringBuilder.init();
    defer sb_jwk.deinit();
    sb_jwk.loadFile("qa_data/jwk/simple_jwk_rsa.json", "utf-8") catch {
        std.debug.print("Failed to load simple JWK file.\n", .{});
        return;
    };

    // Loading it into a Chilkat public key object is easy:
    const pubkey = try chilkat.PublicKey.init();
    defer pubkey.deinit();
    pubkey.loadFromString(try sb_jwk.getAsString(alloc)) catch {
        std.debug.print("{s}\n", .{try pubkey.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Simple JWK loaded into public key object.\n", .{});

    // ----------------------------------------------------------------------------------------------------------
    // Demonstrate how to load a JWK that is contained within JSON into a public key object.

    // {
    //   "alg":"RS256",
    //   "jwk":  {
    //           "kty":"RSA",
    //           "kid":"bilbo.baggins@hobbiton.example",
    //           "use":"sig",
    //           "n":"26381066103266289976070039679240841999340706859156341823971393693544846472963839705790075117105560154606562987481050889935247188300178764426769543782451174831475924241862817098997865881169498995546309260357488285676768114714548754913374731868063116787903918383610614912211576698081222844057810710088656983077876282582286966085572172471849443305250452393766960762340311496052418952315081937000716222751229644449475751932222180738210988190709824209639002026650048655399108647971010890335494074025258268035713167380057822555485524738518699437692145859633592396626258745090144803752145540471688039673495588314804430463791",
    //           "e":"65537"
    //           }
    // }

    const sb_jwk2 = try chilkat.StringBuilder.init();
    defer sb_jwk2.deinit();
    sb_jwk2.loadFile("qa_data/jwk/contained_jwk_rsa.json", "utf-8") catch {
        std.debug.print("{s}\n", .{try sb_jwk2.getLastErrorText(alloc)});
        return;
    };

    // Get the "jwk" part of the JSON..
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.load(try sb_jwk2.getAsString(alloc)) catch {};

    const json_jwk = try chilkat.JsonObject.init();
    defer json_jwk.deinit();
    json.objectOf2("jwk", json_jwk) catch {
        std.debug.print("{s}\n", .{try json.getLastErrorText(alloc)});
        return;
    };

    const pubkey2 = try chilkat.PublicKey.init();
    defer pubkey2.deinit();
    pubkey2.loadFromString(try json_jwk.emit(alloc)) catch {
        std.debug.print("{s}\n", .{try pubkey2.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("Contained JWK loaded into public key object.\n", .{});
}