aboutsummaryrefslogtreecommitdiffstats
path: root/src/znk.zig
diff options
context:
space:
mode:
authorLibravatar Rutger Broekhoff2021-05-25 17:41:21 +0200
committerLibravatar Rutger Broekhoff2021-05-25 17:41:21 +0200
commitd8597fe575644ddf5f1f8ef8cf901a446bc7acda (patch)
treef7f16966a74e60be6334ea10f1a277a434136de4 /src/znk.zig
parent9d03b59a409596d12c3ec7b49066e4eaf0f463e7 (diff)
downloadzig-nkeys-d8597fe575644ddf5f1f8ef8cf901a446bc7acda.tar.gz
zig-nkeys-d8597fe575644ddf5f1f8ef8cf901a446bc7acda.zip
Clean up janky prefix byte API
Diffstat (limited to 'src/znk.zig')
-rw-r--r--src/znk.zig36
1 files changed, 18 insertions, 18 deletions
diff --git a/src/znk.zig b/src/znk.zig
index 4ab3077..8be7713 100644
--- a/src/znk.zig
+++ b/src/znk.zig
@@ -74,9 +74,9 @@ pub fn mainArgs(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
74} 74}
75 75
76const usage_gen = 76const usage_gen =
77 \\Usage: znk gen [options] <type> 77 \\Usage: znk gen [options] <role>
78 \\ 78 \\
79 \\Supported Types: 79 \\Supported Roles:
80 \\ 80 \\
81 \\ account 81 \\ account
82 \\ cluster 82 \\ cluster
@@ -98,7 +98,7 @@ const usage_gen =
98pub fn cmdGen(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !void { 98pub fn cmdGen(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !void {
99 const stdout = io.getStdOut(); 99 const stdout = io.getStdOut();
100 100
101 var ty: ?nkeys.PublicPrefixByte = null; 101 var role: ?nkeys.Role = null;
102 var pub_out: bool = false; 102 var pub_out: bool = false;
103 var prefix: ?[]const u8 = null; 103 var prefix: ?[]const u8 = null;
104 104
@@ -120,34 +120,34 @@ pub fn cmdGen(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !voi
120 } else { 120 } else {
121 fatal("unrecognized parameter: '{s}'", .{arg}); 121 fatal("unrecognized parameter: '{s}'", .{arg});
122 } 122 }
123 } else if (ty != null) { 123 } else if (role != null) {
124 fatal("more than one type to generate provided", .{}); 124 fatal("more than one role to generate for provided", .{});
125 } else if (mem.eql(u8, arg, "account")) { 125 } else if (mem.eql(u8, arg, "account")) {
126 ty = .account; 126 role = .account;
127 } else if (mem.eql(u8, arg, "cluster")) { 127 } else if (mem.eql(u8, arg, "cluster")) {
128 ty = .cluster; 128 role = .cluster;
129 } else if (mem.eql(u8, arg, "operator")) { 129 } else if (mem.eql(u8, arg, "operator")) {
130 ty = .operator; 130 role = .operator;
131 } else if (mem.eql(u8, arg, "server")) { 131 } else if (mem.eql(u8, arg, "server")) {
132 ty = .server; 132 role = .server;
133 } else if (mem.eql(u8, arg, "user")) { 133 } else if (mem.eql(u8, arg, "user")) {
134 ty = .user; 134 role = .user;
135 } else { 135 } else {
136 fatal("unrecognized extra parameter: '{s}'", .{arg}); 136 fatal("unrecognized extra parameter: '{s}'", .{arg});
137 } 137 }
138 } 138 }
139 139
140 if (ty == null) { 140 if (role == null) {
141 info("{s}", .{usage_gen}); 141 info("{s}", .{usage_gen});
142 fatal("no type to generate seed for provided", .{}); 142 fatal("no role to generate seed for provided", .{});
143 } 143 }
144 144
145 if (prefix != null) { 145 if (prefix != null) {
146 const capitalized_prefix = try toUpper(arena, prefix.?); 146 const capitalized_prefix = try toUpper(arena, prefix.?);
147 147
148 try PrefixKeyGenerator.init(arena, ty.?, capitalized_prefix).generate(); 148 try PrefixKeyGenerator.init(arena, role.?, capitalized_prefix).generate();
149 } else { 149 } else {
150 var kp = try nkeys.SeedKeyPair.generate(ty.?); 150 var kp = try nkeys.SeedKeyPair.generate(role.?);
151 defer kp.wipe(); 151 defer kp.wipe();
152 try stdout.writeAll(&kp.seedText()); 152 try stdout.writeAll(&kp.seedText());
153 try stdout.writeAll("\n"); 153 try stdout.writeAll("\n");
@@ -356,16 +356,16 @@ pub fn cmdVerify(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !
356} 356}
357 357
358const PrefixKeyGenerator = struct { 358const PrefixKeyGenerator = struct {
359 ty: nkeys.PublicPrefixByte, 359 role: nkeys.Role,
360 prefix: []const u8, 360 prefix: []const u8,
361 allocator: *Allocator, 361 allocator: *Allocator,
362 done: std.atomic.Bool, 362 done: std.atomic.Bool,
363 363
364 const Self = @This(); 364 const Self = @This();
365 365
366 pub fn init(allocator: *Allocator, ty: nkeys.PublicPrefixByte, prefix: []const u8) Self { 366 pub fn init(allocator: *Allocator, role: nkeys.Role, prefix: []const u8) Self {
367 return .{ 367 return .{
368 .ty = ty, 368 .role = role,
369 .prefix = prefix, 369 .prefix = prefix,
370 .allocator = allocator, 370 .allocator = allocator,
371 .done = std.atomic.Bool.init(false), 371 .done = std.atomic.Bool.init(false),
@@ -376,7 +376,7 @@ const PrefixKeyGenerator = struct {
376 while (true) { 376 while (true) {
377 if (self.done.load(.SeqCst)) return; 377 if (self.done.load(.SeqCst)) return;
378 378
379 var kp = try nkeys.SeedKeyPair.generate(self.ty); 379 var kp = try nkeys.SeedKeyPair.generate(self.role);
380 defer kp.wipe(); 380 defer kp.wipe();
381 var public_key = kp.publicKeyText(); 381 var public_key = kp.publicKeyText();
382 if (!mem.startsWith(u8, public_key[1..], self.prefix)) continue; 382 if (!mem.startsWith(u8, public_key[1..], self.prefix)) continue;