Sample code for 30+ languages & platforms
Zig

IMAP Login Secure

Demonstrates how to use the LoginSecure method introduced in Chilkat v9.5.0.71.

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

    const imap = try chilkat.Imap.init();
    defer imap.deinit();

    // Connect to an IMAP server..
    imap.setSsl(true);
    imap.setPort(993);
    imap.connect("imap.mail.me.com") catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // Imagine we've previously saved our encrypted login and password within a JSON config file
    // that contains this:

    // {
    //   "imap_login": "fLkxsfnVaIWLiL/R32jo6g==",
    //   "imap_password": "/NbaFBoCftBLFf8WBU9Xtw=="
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadFile("qa_data/passwords/imap.json") catch {};

    const crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    // These are the encryption settings we previously used to encrypt the credentials within the JSON config file.
    crypt.setCryptAlgorithm("aes");
    crypt.setCipherMode("cbc");
    crypt.setKeyLength(128);
    crypt.setEncodedKey("000102030405060708090A0B0C0D0E0F", "hex");
    crypt.setEncodedIV("000102030405060708090A0B0C0D0E0F", "hex");
    crypt.setEncodingMode("base64");

    const ss_login = try chilkat.SecureString.init();
    defer ss_login.deinit();
    const ss_password = try chilkat.SecureString.init();
    defer ss_password.deinit();

    // Decrypt to the secure string.  (the strings will still held in memory encrypted, but are now encrypted using
    // a randomly generated session key.)
    crypt.decryptSecureENC(try json.stringOf(alloc, "imap_login"), ss_login) catch {};
    crypt.decryptSecureENC(try json.stringOf(alloc, "imap_password"), ss_password) catch {};

    // Pass the credentials to the LoginSecure method:
    imap.loginSecure(ss_login, ss_password) catch {
        std.debug.print("{s}\n", .{try imap.getLastErrorText(alloc)});
        return;
    };

    // ...

    imap.disconnect() catch {};
}