diff options
Diffstat (limited to 'src/znk.zig')
| -rw-r--r-- | src/znk.zig | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/znk.zig b/src/znk.zig index 1c2898b..5a5ab5e 100644 --- a/src/znk.zig +++ b/src/znk.zig | |||
| @@ -147,7 +147,7 @@ pub fn cmdGen(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !voi | |||
| 147 | 147 | ||
| 148 | try PrefixKeyGenerator.init(arena, ty.?, capitalized_prefix).generate(); | 148 | try PrefixKeyGenerator.init(arena, ty.?, capitalized_prefix).generate(); |
| 149 | } else { | 149 | } else { |
| 150 | var kp = nkeys.SeedKeyPair.generate(ty.?) catch |e| fatal("could not generate key pair: {e}", .{e}); | 150 | var kp = nkeys.SeedKeyPair.generate(ty.?); |
| 151 | defer kp.wipe(); | 151 | defer kp.wipe(); |
| 152 | try stdout.writeAll(&kp.seed); | 152 | try stdout.writeAll(&kp.seed); |
| 153 | try stdout.writeAll("\n"); | 153 | try stdout.writeAll("\n"); |
| @@ -231,7 +231,7 @@ pub fn cmdSign(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !vo | |||
| 231 | const content = file.?.readToEndAlloc(arena, std.math.maxInt(usize)) catch { | 231 | const content = file.?.readToEndAlloc(arena, std.math.maxInt(usize)) catch { |
| 232 | fatal("could not read file to generate signature for", .{}); | 232 | fatal("could not read file to generate signature for", .{}); |
| 233 | }; | 233 | }; |
| 234 | var kp = switch (readKeyFile(arena, key.?)) { | 234 | var kp = switch (readKeyFile(arena, key.?) orelse fatal("could not find a valid key", .{})) { |
| 235 | .seed_key_pair => |kp| kp, | 235 | .seed_key_pair => |kp| kp, |
| 236 | else => |*k| { | 236 | else => |*k| { |
| 237 | k.wipe(); | 237 | k.wipe(); |
| @@ -339,7 +339,7 @@ pub fn cmdVerify(gpa: *Allocator, arena: *Allocator, args: []const []const u8) ! | |||
| 339 | const signature_b64 = sig.?.readToEndAlloc(arena, std.math.maxInt(usize)) catch { | 339 | const signature_b64 = sig.?.readToEndAlloc(arena, std.math.maxInt(usize)) catch { |
| 340 | fatal("could not read signature", .{}); | 340 | fatal("could not read signature", .{}); |
| 341 | }; | 341 | }; |
| 342 | var k = readKeyFile(arena, key.?); | 342 | var k = readKeyFile(arena, key.?) orelse fatal("could not find a valid key", .{}); |
| 343 | defer k.wipe(); | 343 | defer k.wipe(); |
| 344 | 344 | ||
| 345 | const trimmed_signature_b64 = mem.trim(u8, signature_b64, " \n\t\r"); | 345 | const trimmed_signature_b64 = mem.trim(u8, signature_b64, " \n\t\r"); |
| @@ -381,7 +381,7 @@ const PrefixKeyGenerator = struct { | |||
| 381 | while (true) { | 381 | while (true) { |
| 382 | if (self.done.load(.SeqCst)) return; | 382 | if (self.done.load(.SeqCst)) return; |
| 383 | 383 | ||
| 384 | var kp = nkeys.SeedKeyPair.generate(self.ty) catch |e| fatal("could not generate key pair: {e}", .{e}); | 384 | var kp = nkeys.SeedKeyPair.generate(self.ty); |
| 385 | defer kp.wipe(); | 385 | defer kp.wipe(); |
| 386 | var public_key = kp.publicKey() catch |e| fatal("could not generate public key: {e}", .{e}); | 386 | var public_key = kp.publicKey() catch |e| fatal("could not generate public key: {e}", .{e}); |
| 387 | if (!mem.startsWith(u8, public_key[1..], self.prefix)) continue; | 387 | if (!mem.startsWith(u8, public_key[1..], self.prefix)) continue; |
| @@ -435,7 +435,7 @@ pub const Nkey = union(enum) { | |||
| 435 | pub fn publicKey(self: *const Self) !nkeys.text_public { | 435 | pub fn publicKey(self: *const Self) !nkeys.text_public { |
| 436 | return switch (self.*) { | 436 | return switch (self.*) { |
| 437 | .seed_key_pair => |*kp| try kp.publicKey(), | 437 | .seed_key_pair => |*kp| try kp.publicKey(), |
| 438 | .public_key => |*pk| try pk.publicKey(), | 438 | .public_key => |*pk| pk.publicKey(), |
| 439 | }; | 439 | }; |
| 440 | } | 440 | } |
| 441 | 441 | ||
| @@ -481,20 +481,23 @@ pub const Nkey = union(enum) { | |||
| 481 | } | 481 | } |
| 482 | }; | 482 | }; |
| 483 | 483 | ||
| 484 | pub fn readKeyFile(allocator: *Allocator, file: fs.File) Nkey { | 484 | pub fn readKeyFile(allocator: *Allocator, file: fs.File) ?Nkey { |
| 485 | var bytes = file.readToEndAlloc(allocator, std.math.maxInt(usize)) catch fatal("could not read key file", .{}); | 485 | var bytes = file.readToEndAlloc(allocator, std.math.maxInt(usize)) catch fatal("could not read key file", .{}); |
| 486 | defer { | ||
| 487 | for (bytes) |*b| b.* = 0; | ||
| 488 | allocator.free(bytes); | ||
| 489 | } | ||
| 486 | 490 | ||
| 487 | var iterator = mem.split(bytes, "\n"); | 491 | var iterator = mem.split(bytes, "\n"); |
| 488 | while (iterator.next()) |line| { | 492 | while (iterator.next()) |line| { |
| 489 | if (nkeys.isValidEncoding(line) and line.len == nkeys.text_seed_len) { | 493 | if (nkeys.isValidEncoding(line) and line.len == nkeys.text_seed_len) { |
| 490 | var k = Nkey.fromText(line) catch continue; | 494 | var k = Nkey.fromText(line) catch continue; |
| 491 | defer k.wipe(); | 495 | defer k.wipe(); |
| 492 | allocator.free(bytes); | ||
| 493 | return k; | 496 | return k; |
| 494 | } | 497 | } |
| 495 | } | 498 | } |
| 496 | 499 | ||
| 497 | fatal("could not find a valid key", .{}); | 500 | return null; |
| 498 | } | 501 | } |
| 499 | 502 | ||
| 500 | test { | 503 | test { |