~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
29 lines · 966 B Raw History
const std = @import("std");
const store = @import("../storage/store.zig");

pub fn makeCode(out: *[11]u8) []const u8 {
    const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
    var random: [10]u8 = undefined;
    store.randomBytes(&random);
    for (random, 0..) |byte, idx| {
        const write_idx = if (idx < 5) idx else idx + 1;
        out[write_idx] = alphabet[byte & 31];
    }
    out[5] = '-';
    return out[0..];
}

test "email codes use social-app compatible base32 shape" {
    try store.init(std.Options.debug_io, ":memory:");
    defer store.close();

    const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
    var out: [11]u8 = undefined;
    const code = makeCode(&out);
    try std.testing.expectEqual(@as(usize, 11), code.len);
    try std.testing.expectEqual(@as(u8, '-'), code[5]);
    for (code, 0..) |byte, idx| {
        if (idx == 5) continue;
        try std.testing.expect(std.mem.indexOfScalar(u8, alphabet, byte) != null);
    }
}