Sample code for 30+ languages & platforms
Zig

Generate TOTP using Base32 Secret (6 Digits, Time-based, 30-second period)

See more Encryption Examples

Generates a 6-digit time-based TOTP code using a base32 secret with a 30-second time period.

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 crypt = try chilkat.Crypt2.init();
    defer crypt.deinit();

    // Base32 secret key:
    const secret_key = "oebf ytfl qmzb p4xd 2ztf zyz4 hjrw 3uyo";

    const sb_key = try chilkat.StringBuilder.init();
    defer sb_key.deinit();
    try sb_key.append(secret_key);

    // Remove SPACE chars.
    _ = sb_key.replace(" ", "");

    // Note: A new token is generated every 30 seconds.  You must generate within the same 30 second interval to get the same result.

    // Generate 6 digits.
    const num_digits = 6;
    // 30 second time period.
    const time_period = 30;
    std.debug.print("Your token is: {s}\n", .{try crypt.totp(alloc, try sb_key.getAsString(alloc), "base32", "0", "", time_period, num_digits, -1, "sha1")});
}