Skip to content

Commit

Permalink
Confirm password
Browse files Browse the repository at this point in the history
  • Loading branch information
angelsolaorbaiceta committed Sep 5, 2024
1 parent 237393e commit 18e7b00
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 29 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Encrypting an archive:
```bash
$ aar encrypt -f archive.aarch
Password: <password>
Confirm password: <password>
```

Where `<password>` is the password you want to use to encrypt the archive, with a minimum length of 8 characters.
Expand All @@ -85,6 +86,7 @@ Decrypting an archive:
```bash
$ aar decrypt -f archive.aarch.enc
Password: <password>
Confirm password: <password>
```

Where `<password>` is the password you used to encrypt the archive.
Expand Down
31 changes: 2 additions & 29 deletions cmd/aar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"flag"
"fmt"
"os"
"syscall"

"github.com/angelsolaorbaiceta/aar/cmd"
"golang.org/x/term"
)

func main() {
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}
}
49 changes: 49 additions & 0 deletions cmd/password.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 18e7b00

Please sign in to comment.