This repository has been archived on 2024-02-27. You can view files and clone it, but cannot push or open issues or pull requests.
gootp/cmd/gootp/decrypt/decrypt.go

68 lines
1.3 KiB
Go

package decrypt
import (
"fmt"
"os"
"syscall"
"github.com/Kichiyaki/gootp/internal/andotp"
"golang.org/x/term"
"github.com/urfave/cli/v2"
)
func NewCommand() *cli.Command {
return &cli.Command{
Name: "decrypt",
Usage: "decrypt backup file generated by andotp",
Action: func(c *cli.Context) error {
var err error
password := []byte(c.String("password"))
if len(password) == 0 {
password, err = getPassword()
}
if err != nil {
return err
}
b, err := os.ReadFile(c.String("path"))
if err != nil {
return fmt.Errorf("something went wrong while reading file: %w", err)
}
result, err := andotp.Decrypt(password, b)
if err != nil {
return fmt.Errorf("something went wrong while decrypting file: %w", err)
}
fmt.Print("\n")
fmt.Print(string(result))
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "path",
Usage: "path to backup file",
Required: true,
},
&cli.StringFlag{
Name: "password",
Usage: "encryption password",
Required: false,
},
},
}
}
func getPassword() ([]byte, error) {
fmt.Print("Password: ")
pass, err := term.ReadPassword(syscall.Stdin)
if err != nil {
return nil, fmt.Errorf("term.ReadPassword: %w", err)
}
return pass, nil
}