Skip to content

Commit 318838b

Browse files
committed
key: a command for expanding partial keys by left-padding with zeroes.
1 parent b811396 commit 318838b

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

key.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var key = &cli.Command{
2323
Commands: []*cli.Command{
2424
generate,
2525
public,
26+
expand,
2627
encryptKey,
2728
decryptKey,
2829
combine,
@@ -67,6 +68,37 @@ var public = &cli.Command{
6768
},
6869
}
6970

71+
var expand = &cli.Command{
72+
Name: "expand",
73+
Usage: "left-pads a hex key to 64 characters",
74+
Description: ``,
75+
ArgsUsage: "[key]",
76+
DisableSliceFlagSeparator: true,
77+
Action: func(ctx context.Context, c *cli.Command) error {
78+
for keyhex := range getStdinLinesOrArgumentsFromSlice(c.Args().Slice()) {
79+
keyhex = strings.TrimSpace(keyhex)
80+
if keyhex == "" {
81+
continue
82+
}
83+
if len(keyhex) > 64 {
84+
ctx = lineProcessingError(ctx, "invalid hex key: length %d", len(keyhex))
85+
continue
86+
}
87+
check := keyhex
88+
if len(check)%2 == 1 {
89+
check = "0" + check
90+
}
91+
if _, err := hex.DecodeString(check); err != nil {
92+
ctx = lineProcessingError(ctx, "invalid hex key: %s", err)
93+
continue
94+
}
95+
keyhex = strings.ToLower(keyhex)
96+
stdout(strings.Repeat("0", 64-len(keyhex)) + keyhex)
97+
}
98+
return nil
99+
},
100+
}
101+
70102
var encryptKey = &cli.Command{
71103
Name: "encrypt",
72104
Usage: "encrypts a secret key and prints an ncryptsec code",

0 commit comments

Comments
 (0)