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 @@...@@ -3,7 +3,7 @@
3![loc](https://sloc.xyz/github/nektro/zig-totp)3![loc](https://sloc.xyz/github/nektro/zig-totp)
4[![license](https://img.shields.io/github/license/nektro/zig-totp.svg)](https://github.com/nektro/zig-totp/blob/master/LICENSE)4[![license](https://img.shields.io/github/license/nektro/zig-totp.svg)](https://github.com/nektro/zig-totp/blob/master/LICENSE)
5[![nektro @ github sponsors](https://img.shields.io/badge/sponsors-nektro-purple?logo=github)](https://github.com/sponsors/nektro)5[![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/)
7[![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod)7[![Zigmod](https://img.shields.io/badge/Zigmod-latest-f7a41d)](https://github.com/nektro/zigmod)
88
9RFC 6238: TOTP: Time-Based One-Time Password Algorithm9RFC 6238: TOTP: Time-Based One-Time Password Algorithm
build.zig+1
...@@ -14,6 +14,7 @@ pub fn build(b: *std.Build) void {...@@ -14,6 +14,7 @@ pub fn build(b: *std.Build) void {
14 }),14 }),
15 });15 });
16 deps.addAllTo(tests);16 deps.addAllTo(tests);
17 tests.root_module.link_libc = true;
17 tests.use_llvm = !disable_llvm;18 tests.use_llvm = !disable_llvm;
18 tests.use_lld = !disable_llvm;19 tests.use_lld = !disable_llvm;
19 b.getInstallStep().dependOn(&tests.step);20 b.getInstallStep().dependOn(&tests.step);
totp.zig+15-14
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const extras = @import("extras");2const extras = @import("extras");
3const url = @import("url");3const url = @import("url");
4const nio = @import("nio");
45
5pub fn totp(digits: comptime_int, Hash: type, epoch: u64, X: u64, K: *const [Hash.digest_length]u8, time_now_s: u64) [digits]u8 {6pub fn totp(digits: comptime_int, Hash: type, epoch: u64, X: u64, K: *const [Hash.digest_length]u8, time_now_s: u64) [digits]u8 {
6 const T = (time_now_s - epoch) / X;7 const T = (time_now_s - epoch) / X;
...@@ -56,32 +57,32 @@ pub fn generateUrl(allocator: std.mem.Allocator, issuer: []const u8, account: []...@@ -56,32 +57,32 @@ pub fn generateUrl(allocator: std.mem.Allocator, issuer: []const u8, account: []
56 std.debug.assert(secret_raw.len <= algo.digest_length());57 std.debug.assert(secret_raw.len <= algo.digest_length());
57 std.debug.assert(digits == 6 or digits == 7 or digits == 8);58 std.debug.assert(digits == 6 or digits == 7 or digits == 8);
58 std.debug.assert(period == 15 or period == 30 or period == 60);59 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);
60 errdefer list.deinit();61 errdefer list.deinit();
61 try list.appendSlice("otpauth://");62 try list.writeAll("otpauth://");
62 try list.appendSlice("totp/");63 try list.writeAll("totp/");
63 try url.percentEncodeAL(&list, issuer, url.is_path_percent_char);64 try url.percentEncodeAL(&list, issuer, url.is_path_percent_char);
64 try list.append(':');65 try list.writeAll(":");
65 try url.percentEncodeAL(&list, account, url.is_path_percent_char);66 try url.percentEncodeAL(&list, account, url.is_path_percent_char);
66 try list.appendSlice("?secret=");67 try list.writeAll("?secret=");
67 try encodeBase32(&list, secret_raw);68 try encodeBase32(&list, secret_raw);
68 try list.appendSlice("&algorithm=");69 try list.writeAll("&algorithm=");
69 try list.appendSlice(@tagName(algo));70 try list.writeAll(@tagName(algo));
70 try list.appendSlice("&digits=");71 try list.writeAll("&digits=");
71 try list.writer().print("{d}", .{digits});72 try list.print("{d}", .{digits});
72 try list.appendSlice("&period=");73 try list.writeAll("&period=");
73 try list.writer().print("{d}", .{period});74 try list.print("{d}", .{period});
74 try list.appendSlice("&issuer=");75 try list.writeAll("&issuer=");
75 try url.percentEncodeAL(&list, issuer, url.is_query_percent_char);76 try url.percentEncodeAL(&list, issuer, url.is_query_percent_char);
76 return list.toOwnedSlice();77 return list.toOwnedSlice();
77}78}
7879
79// RFC3548 base3280// RFC3548 base32
80// input.len is gonna be 64 | 32 | 6481// 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 {
82 const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";83 const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
83 var iter = BufBiterator.init(input);84 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]);
85}86}
8687
87const BufBiterator = struct {88const BufBiterator = struct {
zigmod.yml+1
...@@ -6,6 +6,7 @@ description: "RFC 6238: TOTP: Time-Based One-Time Password Algorithm"...@@ -6,6 +6,7 @@ description: "RFC 6238: TOTP: Time-Based One-Time Password Algorithm"
6dependencies:6dependencies:
7 - src: git https://github.com/nektro/zig-extras7 - src: git https://github.com/nektro/zig-extras
8 - src: git https://github.com/nektro/zig-whatwg-url8 - src: git https://github.com/nektro/zig-whatwg-url
9 - src: git https://github.com/nektro/zig-nio
9root_dependencies:10root_dependencies:
10 - src: git https://github.com/nektro/zig-extras11 - src: git https://github.com/nektro/zig-extras
11 - src: git https://github.com/nektro/zig-expect12 - src: git https://github.com/nektro/zig-expect