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

Everyware API RSA Encrypt JSON

See more RSA Examples

Demonstrates how to RSA encrypt JSON using everyware.com's RSA public key.

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 example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // First build the JSON to be encrypted.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("provider_key", "USER GUID HERE") catch {};
    json.updateString("menu_item", "payment") catch {};
    const dt = try chilkat.DateTime.init();
    defer dt.deinit();
    dt.setFromCurrentSystemTime() catch {};
    json.updateString("date_time", try dt.getAsUnixTimeStr(alloc, false)) catch {};

    // This build JSON like the following:

    // {
    //     "provider_key": "USER GUID HERE",
    //     "menu_item": "payment",
    //     "date_time": "1588163411"
    // }

    // When we sign, we'll want to sign the most compact JSON possible
    json.setEmitCompact(true);

    // Everyware's RSA public key is at:  https://docs.everyware.com/docs/everyware-public-rsa-key
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    const b_crlf = true;
    sb.appendLine("-----BEGIN PUBLIC KEY-----", b_crlf) catch {};
    sb.appendLine("MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxNbflxUSWQ3XJ1N9dAoh", b_crlf) catch {};
    sb.appendLine("k+uaiFsg3wkPi9LGS/mH8DtHBgZxKyz+oQBDtnDd9FDEo0ql7MMgCMsTAv27W5vk", b_crlf) catch {};
    sb.appendLine("Pu0rm6zhcTeYquWEuVCS7VtVsyTATr0Z9WhqNeZlIRurovJAXl2jRDX6QeY5dayC", b_crlf) catch {};
    sb.appendLine("ubwyG4lBWE4fCakGY6zlh+oaElK0rvblqjYoEg3dn4KPRCYGof8OFxLptHThD4cE", b_crlf) catch {};
    sb.appendLine("T30j+utVafhO0HRyJ4iR3Pigb4GXdWBtJEEEWddZJizMkjFQkyUAoYLOT8EJ2TW3", b_crlf) catch {};
    sb.appendLine("Tz8SvAuHBUEFcPWTSTMAG/bSp5wrYBTXaeEhx+wrYa60OruHuzgmhzKyQVuYlCNJ", b_crlf) catch {};
    sb.appendLine("HdbnassuIRjjSNo25o4AdSlWwpGfBZjAiyEInR+KGpHdhKTxSekJxiwiXUS0UfSG", b_crlf) catch {};
    sb.appendLine("prOpd5PzWaAR7DvjLsdmR9XffxvJCVxC735gLK7hDJKjCajDPHVDr8FSL8xMlrq0", b_crlf) catch {};
    sb.appendLine("nKxtsHeRl1yzoGrRr12+9MiQnHtpqROTNXcXdwe3v+Vh8V5k8v8oIrcgh1+/N7Bd", b_crlf) catch {};
    sb.appendLine("NiRsy1gFHBdu/he/KcDRT/9/acQFMPLQueGfZxUvU5As6pEONjtKX2MUg2fMF6Rc", b_crlf) catch {};
    sb.appendLine("sQVVrLzg0g7EcuHGfuPeKfD/716MvS8NU7rX+2soijCSQv/e18PJPMVDlcMXjnup", b_crlf) catch {};
    sb.appendLine("PPx1tStemesavFlj1okhS6UCAwEAAQ==", b_crlf) catch {};
    sb.appendLine("-----END PUBLIC KEY-----", b_crlf) catch {};

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

    const rsa = try chilkat.Rsa.init();
    defer rsa.deinit();
    rsa.usePublicKey(pubkey) catch {
        std.debug.print("{s}\n", .{try rsa.getLastErrorText(alloc)});
        return;
    };

    // We probably need a base64Url encoded encrypted key.
    // Using straight-up base64 would potenially include chars that are not URL safe (i.e. have special meanings in URLs)
    rsa.setEncodingMode("base64url");

    const encrypted_json = try rsa.encryptStringENC(alloc, try json.emit(alloc), false);

    // Build the URL
    // Such as:  https://portal.everyware.com/Account/LoginMenu?data={Base64Url_encrypted_JSON}
    const sb_url = try chilkat.StringBuilder.init();
    defer sb_url.deinit();
    sb_url.append("https://portal.everyware.com/Account/LoginMenu?data=") catch {};
    sb_url.append(encrypted_json) catch {};

    std.debug.print("{s}\n", .{try sb_url.getAsString(alloc)});
}