diff --git a/README.md b/README.md index 05450ea..b29e994 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ Encrypting an archive: ```bash $ aar encrypt -f archive.aarch Password: +Confirm password: ``` Where `` is the password you want to use to encrypt the archive, with a minimum length of 8 characters. @@ -85,6 +86,7 @@ Decrypting an archive: ```bash $ aar decrypt -f archive.aarch.enc Password: +Confirm password: ``` Where `` is the password you used to encrypt the archive. diff --git a/cmd/aar/main.go b/cmd/aar/main.go index 1db0d0c..b7caf2d 100644 --- a/cmd/aar/main.go +++ b/cmd/aar/main.go @@ -4,10 +4,8 @@ import ( "flag" "fmt" "os" - "syscall" "github.com/angelsolaorbaiceta/aar/cmd" - "golang.org/x/term" ) func main() { @@ -59,14 +57,14 @@ func main() { case "encrypt": encryptCmd.Parse(os.Args[2:]) validateFileName(*encryptFileNameFlag) - password := promptPassword() + password := cmd.PromptPassword() cmd.EncryptArchive(*encryptFileNameFlag, password) case "decrypt": decryptCmd.Parse(os.Args[2:]) validateFileName(*decryptFileNameFlag) - password := promptPassword() + password := cmd.PromptPassword() cmd.DecryptArchive(*decryptFileNameFlag, password) @@ -91,28 +89,3 @@ func createArchive(fileName string, fileNames []string) { cmd.CreateArchive(fileName, fileNames) } - -func promptPassword() string { - fmt.Print("Password: ") - - // Disable input echoing - passwordBytes, err := term.ReadPassword(int(syscall.Stdin)) - if err != nil { - fmt.Fprintf(os.Stderr, "Error reading password: %v\n", err) - os.Exit(1) - } - - fmt.Println() // Move to the next line after password input - - password := string(passwordBytes) - validatePassword(password) - - return password -} - -func validatePassword(password string) { - if len(password) < 8 { - fmt.Fprintf(os.Stderr, "The password must be at least 8 characters long.\n") - os.Exit(1) - } -} diff --git a/cmd/password.go b/cmd/password.go new file mode 100644 index 0000000..d7ec18a --- /dev/null +++ b/cmd/password.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "fmt" + "os" + "syscall" + + "golang.org/x/term" +) + +func PromptPassword() string { + fmt.Print("Password: ") + passwordBytes, err := term.ReadPassword(int(syscall.Stdin)) + if err != nil { + fmt.Fprintf(os.Stderr, "Error reading password: %v\n", err) + os.Exit(1) + } + + // Move to the next line after password input + fmt.Println() + + password := string(passwordBytes) + validatePassword(password) + + fmt.Print("Confirm password: ") + passwordConfirmationBytes, err := term.ReadPassword(int(syscall.Stdin)) + if err != nil { + fmt.Fprintf(os.Stderr, "Error reading password: %v\n", err) + os.Exit(1) + } + + // Move to the next line after password input + fmt.Println() + + passwordConfirmation := string(passwordConfirmationBytes) + if password != passwordConfirmation { + fmt.Fprintf(os.Stderr, "Passwords do not match.\n") + os.Exit(1) + } + + return password +} + +func validatePassword(password string) { + if len(password) < 8 { + fmt.Fprintf(os.Stderr, "The password must be at least 8 characters long.\n") + os.Exit(1) + } +}