authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-04 19:42:30 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-04 19:42:30 -07:00
log1748cbd38deb9d7642b333365e4b6ea60bcb2b3c
tree82e14d10a7c83f4d5c69c8f260a13c6957fb8969
parenteb9a281086922860f35ae7a7037b97965083da6b
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

update to zig 0.16.0


4 files changed, 18 insertions(+), 15 deletions(-)

README.md+1-1
......@@ -3,7 +3,7 @@
33![loc](https://sloc.xyz/github/nektro/zig-totp)
44[![license](https://img.shields.io/github/license/nektro/zig-totp.svg)](https://github.com/nektro/zig-totp/blob/master/LICENSE)
55[![nektro @ github sponsors](https://img.shields.io/badge/sponsors-nektro-purple?logo=github)](https://github.com/sponsors/nektro)
6[![Zig](https://img.shields.io/badge/Zig-0.15-f7a41d)](https://ziglang.org/)
6[![Zig](https://img.shields.io/badge/Zig-0.16-f7a41d)](https://ziglang.org/)
77[![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod)
88
99RFC 6238: TOTP: Time-Based One-Time Password Algorithm
build.zig+1
......@@ -14,6 +14,7 @@ pub fn build(b: *std.Build) void {
1414 }),
1515 });
1616 deps.addAllTo(tests);
17 tests.root_module.link_libc = true;
1718 tests.use_llvm = !disable_llvm;
1819 tests.use_lld = !disable_llvm;
1920 b.getInstallStep().dependOn(&tests.step);
totp.zig+15-14
......@@ -1,6 +1,7 @@
11const std = @import("std");
22const extras = @import("extras");
33const url = @import("url");
4const nio = @import("nio");
45
56pub fn totp(digits: comptime_int, Hash: type, epoch: u64, X: u64, K: *const [Hash.digest_length]u8, time_now_s: u64) [digits]u8 {
67 const T = (time_now_s - epoch) / X;
......@@ -56,32 +57,32 @@ pub fn generateUrl(allocator: std.mem.Allocator, issuer: []const u8, account: []
5657 std.debug.assert(secret_raw.len <= algo.digest_length());
5758 std.debug.assert(digits == 6 or digits == 7 or digits == 8);
5859 std.debug.assert(period == 15 or period == 30 or period == 60);
59 var list: std.array_list.Managed(u8) = .init(allocator);
60 var list: nio.AllocatingWriter = .init(allocator);
6061 errdefer list.deinit();
61 try list.appendSlice("otpauth://");
62 try list.appendSlice("totp/");
62 try list.writeAll("otpauth://");
63 try list.writeAll("totp/");
6364 try url.percentEncodeAL(&list, issuer, url.is_path_percent_char);
64 try list.append(':');
65 try list.writeAll(":");
6566 try url.percentEncodeAL(&list, account, url.is_path_percent_char);
66 try list.appendSlice("?secret=");
67 try list.writeAll("?secret=");
6768 try encodeBase32(&list, secret_raw);
68 try list.appendSlice("&algorithm=");
69 try list.appendSlice(@tagName(algo));
70 try list.appendSlice("&digits=");
71 try list.writer().print("{d}", .{digits});
72 try list.appendSlice("&period=");
73 try list.writer().print("{d}", .{period});
74 try list.appendSlice("&issuer=");
69 try list.writeAll("&algorithm=");
70 try list.writeAll(@tagName(algo));
71 try list.writeAll("&digits=");
72 try list.print("{d}", .{digits});
73 try list.writeAll("&period=");
74 try list.print("{d}", .{period});
75 try list.writeAll("&issuer=");
7576 try url.percentEncodeAL(&list, issuer, url.is_query_percent_char);
7677 return list.toOwnedSlice();
7778}
7879
7980// RFC3548 base32
8081// input.len is gonna be 64 | 32 | 64
81fn encodeBase32(list: *std.array_list.Managed(u8), input: []const u8) !void {
82fn encodeBase32(list: *nio.AllocatingWriter, input: []const u8) !void {
8283 const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
8384 var iter = BufBiterator.init(input);
84 while (iter.nextInt(u5)) |idx| try list.append(alphabet[idx]);
85 while (iter.nextInt(u5)) |idx| try list.writeAll(alphabet[idx..][0..1]);
8586}
8687
8788const BufBiterator = struct {
zigmod.yml+1
......@@ -6,6 +6,7 @@ description: "RFC 6238: TOTP: Time-Based One-Time Password Algorithm"
66dependencies:
77 - src: git https://github.com/nektro/zig-extras
88 - src: git https://github.com/nektro/zig-whatwg-url
9 - src: git https://github.com/nektro/zig-nio
910root_dependencies:
1011 - src: git https://github.com/nektro/zig-extras
1112 - src: git https://github.com/nektro/zig-expect