diff options
| author | Rutger Broekhoff | 2021-05-25 15:25:43 +0200 |
|---|---|---|
| committer | Rutger Broekhoff | 2021-05-25 15:25:43 +0200 |
| commit | 60c08f0a418df148793870cdbde4b2a2cec73bd0 (patch) | |
| tree | 67041a5a59a234fe71d154c73b31855086f0c636 /src | |
| parent | 7914eed7b4a37b9c0b0da9aa9481d9666353b45e (diff) | |
| download | zig-nkeys-60c08f0a418df148793870cdbde4b2a2cec73bd0.tar.gz zig-nkeys-60c08f0a418df148793870cdbde4b2a2cec73bd0.zip | |
Rename tagged union fields in znk
Diffstat (limited to 'src')
| -rw-r--r-- | src/nkeys.zig | 16 | ||||
| -rw-r--r-- | src/znk.zig | 32 |
2 files changed, 30 insertions, 18 deletions
diff --git a/src/nkeys.zig b/src/nkeys.zig index ac5ba1b..42a781f 100644 --- a/src/nkeys.zig +++ b/src/nkeys.zig | |||
| @@ -26,10 +26,10 @@ pub const KeyTypePrefixByte = enum(u8) { | |||
| 26 | private = 15 << 3, // P | 26 | private = 15 << 3, // P |
| 27 | 27 | ||
| 28 | fn char(self: Self) u8 { | 28 | fn char(self: Self) u8 { |
| 29 | switch (self) { | 29 | return switch (self) { |
| 30 | .seed => 'S', | 30 | .seed => 'S', |
| 31 | .private => 'P', | 31 | .private => 'P', |
| 32 | } | 32 | }; |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | fn fromChar(c: u8) InvalidPrefixByteError!Self { | 35 | fn fromChar(c: u8) InvalidPrefixByteError!Self { |
| @@ -61,6 +61,16 @@ pub const PublicPrefixByte = enum(u8) { | |||
| 61 | }; | 61 | }; |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | fn char(self: Self) u8 { | ||
| 65 | return switch (self) { | ||
| 66 | .account => 'A', | ||
| 67 | .cluster => 'C', | ||
| 68 | .operator => 'O', | ||
| 69 | .server => 'N', | ||
| 70 | .user => 'U', | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 64 | fn fromChar(c: u8) InvalidPrefixByteError!Self { | 74 | fn fromChar(c: u8) InvalidPrefixByteError!Self { |
| 65 | return switch (c) { | 75 | return switch (c) { |
| 66 | 'A' => .account, | 76 | 'A' => .account, |
| @@ -464,6 +474,8 @@ pub fn parseDecoratedUserNkey(contents: []const u8) (NoNkeySeedFoundError || NoN | |||
| 464 | 474 | ||
| 465 | test { | 475 | test { |
| 466 | testing.refAllDecls(@This()); | 476 | testing.refAllDecls(@This()); |
| 477 | testing.refAllDecls(KeyTypePrefixByte); | ||
| 478 | testing.refAllDecls(PublicPrefixByte); | ||
| 467 | testing.refAllDecls(SeedKeyPair); | 479 | testing.refAllDecls(SeedKeyPair); |
| 468 | testing.refAllDecls(PublicKey); | 480 | testing.refAllDecls(PublicKey); |
| 469 | testing.refAllDecls(PrivateKey); | 481 | testing.refAllDecls(PrivateKey); |
diff --git a/src/znk.zig b/src/znk.zig index 4ab3077..b088dc4 100644 --- a/src/znk.zig +++ b/src/znk.zig | |||
| @@ -232,7 +232,7 @@ pub fn cmdSign(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !vo | |||
| 232 | fatal("could not read file to generate signature for", .{}); | 232 | fatal("could not read file to generate signature for", .{}); |
| 233 | }; | 233 | }; |
| 234 | var nkey = readKeyFile(arena, key.?) orelse fatal("could not find a valid key", .{}); | 234 | var nkey = readKeyFile(arena, key.?) orelse fatal("could not find a valid key", .{}); |
| 235 | if (nkey == .public_key) fatal("key was provided but is not a seed or private key", .{}); | 235 | if (nkey == .PublicKey) fatal("key was provided but is not a seed or private key", .{}); |
| 236 | defer nkey.wipe(); | 236 | defer nkey.wipe(); |
| 237 | 237 | ||
| 238 | const sig = nkey.sign(content) catch fatal("could not generate signature", .{}); | 238 | const sig = nkey.sign(content) catch fatal("could not generate signature", .{}); |
| @@ -424,15 +424,15 @@ fn toUpper(allocator: *Allocator, slice: []const u8) ![]u8 { | |||
| 424 | pub const Nkey = union(enum) { | 424 | pub const Nkey = union(enum) { |
| 425 | const Self = @This(); | 425 | const Self = @This(); |
| 426 | 426 | ||
| 427 | seed_key_pair: nkeys.SeedKeyPair, | 427 | SeedKeyPair: nkeys.SeedKeyPair, |
| 428 | public_key: nkeys.PublicKey, | 428 | PublicKey: nkeys.PublicKey, |
| 429 | private_key: nkeys.PrivateKey, | 429 | PrivateKey: nkeys.PrivateKey, |
| 430 | 430 | ||
| 431 | pub fn wipe(self: *Self) void { | 431 | pub fn wipe(self: *Self) void { |
| 432 | switch (self.*) { | 432 | switch (self.*) { |
| 433 | .seed_key_pair => |*kp| kp.wipe(), | 433 | .SeedKeyPair => |*kp| kp.wipe(), |
| 434 | .public_key => |*pk| pk.wipe(), | 434 | .PublicKey => |*pk| pk.wipe(), |
| 435 | .private_key => |*pk| pk.wipe(), | 435 | .PrivateKey => |*pk| pk.wipe(), |
| 436 | } | 436 | } |
| 437 | } | 437 | } |
| 438 | 438 | ||
| @@ -442,9 +442,9 @@ pub const Nkey = union(enum) { | |||
| 442 | sig: [std.crypto.sign.Ed25519.signature_length]u8, | 442 | sig: [std.crypto.sign.Ed25519.signature_length]u8, |
| 443 | ) !void { | 443 | ) !void { |
| 444 | return switch (self.*) { | 444 | return switch (self.*) { |
| 445 | .seed_key_pair => |*kp| try kp.verify(msg, sig), | 445 | .SeedKeyPair => |*kp| try kp.verify(msg, sig), |
| 446 | .public_key => |*pk| try pk.verify(msg, sig), | 446 | .PublicKey => |*pk| try pk.verify(msg, sig), |
| 447 | .private_key => |*pk| try pk.verify(msg, sig), | 447 | .PrivateKey => |*pk| try pk.verify(msg, sig), |
| 448 | }; | 448 | }; |
| 449 | } | 449 | } |
| 450 | 450 | ||
| @@ -453,9 +453,9 @@ pub const Nkey = union(enum) { | |||
| 453 | msg: []const u8, | 453 | msg: []const u8, |
| 454 | ) ![std.crypto.sign.Ed25519.signature_length]u8 { | 454 | ) ![std.crypto.sign.Ed25519.signature_length]u8 { |
| 455 | return switch (self.*) { | 455 | return switch (self.*) { |
| 456 | .seed_key_pair => |*kp| try kp.sign(msg), | 456 | .SeedKeyPair => |*kp| try kp.sign(msg), |
| 457 | .private_key => |*pk| try pk.sign(msg), | 457 | .PrivateKey => |*pk| try pk.sign(msg), |
| 458 | .public_key => return error.CantSign, | 458 | .PublicKey => return error.CantSign, |
| 459 | }; | 459 | }; |
| 460 | } | 460 | } |
| 461 | 461 | ||
| @@ -465,17 +465,17 @@ pub const Nkey = union(enum) { | |||
| 465 | 'S' => { | 465 | 'S' => { |
| 466 | // It's a seed. | 466 | // It's a seed. |
| 467 | if (text.len != nkeys.text_seed_len) return error.InvalidSeed; | 467 | if (text.len != nkeys.text_seed_len) return error.InvalidSeed; |
| 468 | return Self{ .seed_key_pair = try nkeys.SeedKeyPair.fromTextSeed(text[0..nkeys.text_seed_len]) }; | 468 | return Self{ .SeedKeyPair = try nkeys.SeedKeyPair.fromTextSeed(text[0..nkeys.text_seed_len]) }; |
| 469 | }, | 469 | }, |
| 470 | 'P' => { | 470 | 'P' => { |
| 471 | // It's a private key. | 471 | // It's a private key. |
| 472 | if (text.len != nkeys.text_private_len) return error.InvalidPrivateKey; | 472 | if (text.len != nkeys.text_private_len) return error.InvalidPrivateKey; |
| 473 | return Self{ .private_key = try nkeys.PrivateKey.fromTextPrivateKey(text[0..nkeys.text_private_len]) }; | 473 | return Self{ .PrivateKey = try nkeys.PrivateKey.fromTextPrivateKey(text[0..nkeys.text_private_len]) }; |
| 474 | }, | 474 | }, |
| 475 | else => { | 475 | else => { |
| 476 | // It should be a public key. | 476 | // It should be a public key. |
| 477 | if (text.len != nkeys.text_public_len) return error.InvalidEncoding; | 477 | if (text.len != nkeys.text_public_len) return error.InvalidEncoding; |
| 478 | return Self{ .public_key = try nkeys.PublicKey.fromTextPublicKey(text[0..nkeys.text_public_len]) }; | 478 | return Self{ .PublicKey = try nkeys.PublicKey.fromTextPublicKey(text[0..nkeys.text_public_len]) }; |
| 479 | }, | 479 | }, |
| 480 | } | 480 | } |
| 481 | } | 481 | } |