fix panic (#12)

* c.Args().Len() - 1 might be negative value

* fix getStdinLinesOrFirstArgument

* fix getStdinLinesOrFirstArgument
This commit is contained in:
mattn
2024-02-03 22:03:32 +09:00
committed by GitHub
parent 0b9e861f90
commit 01e1f52a70
5 changed files with 35 additions and 18 deletions

32
key.go
View File

@@ -39,7 +39,7 @@ var public = &cli.Command{
Description: ``,
ArgsUsage: "[secret]",
Action: func(c *cli.Context) error {
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c) {
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c, c.Args().First()) {
pubkey, err := nostr.GetPublicKey(sec)
if err != nil {
lineProcessingError(c, "failed to derive public key: %s", err)
@@ -65,11 +65,20 @@ var encrypt = &cli.Command{
},
},
Action: func(c *cli.Context) error {
password := c.Args().Get(c.Args().Len() - 1)
var content string
var password string
switch c.Args().Len() {
case 1:
content = ""
password = c.Args().Get(0)
case 2:
content = c.Args().Get(0)
password = c.Args().Get(1)
}
if password == "" {
return fmt.Errorf("no password given")
}
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c) {
for sec := range getSecretKeyFromStdinLinesOrFirstArgument(c, content) {
ncryptsec, err := nip49.Encrypt(sec, password, uint8(c.Int("logn")), 0x02)
if err != nil {
lineProcessingError(c, "failed to encrypt: %s", err)
@@ -87,11 +96,20 @@ var decrypt = &cli.Command{
Description: `uses the NIP-49 standard.`,
ArgsUsage: "<ncryptsec-code> <password>",
Action: func(c *cli.Context) error {
password := c.Args().Get(c.Args().Len() - 1)
var content string
var password string
switch c.Args().Len() {
case 1:
content = ""
password = c.Args().Get(0)
case 2:
content = c.Args().Get(0)
password = c.Args().Get(1)
}
if password == "" {
return fmt.Errorf("no password given")
}
for ncryptsec := range getStdinLinesOrFirstArgument(c) {
for ncryptsec := range getStdinLinesOrFirstArgument(content) {
sec, err := nip49.Decrypt(ncryptsec, password)
if err != nil {
lineProcessingError(c, "failed to decrypt: %s", err)
@@ -104,10 +122,10 @@ var decrypt = &cli.Command{
},
}
func getSecretKeyFromStdinLinesOrFirstArgument(c *cli.Context) chan string {
func getSecretKeyFromStdinLinesOrFirstArgument(c *cli.Context, content string) chan string {
ch := make(chan string)
go func() {
for sec := range getStdinLinesOrFirstArgument(c) {
for sec := range getStdinLinesOrFirstArgument(content) {
if sec == "" {
continue
}