Skip to content

Commit

Permalink
ncrypt: read password from file and fix return code when failed (#32)
Browse files Browse the repository at this point in the history
This commit adds a password CLI flag `-p` which allows specifying a password directly.
Further it fixes a wrong error return code.
  • Loading branch information
pjw91 authored and Andreas Auernhammer committed Jan 15, 2019
1 parent d86b148 commit ff852fa
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions cmd/ncrypt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ var (
)

var (
listFlag bool
decryptFlag bool
cipherFlag string
listFlag bool
decryptFlag bool
cipherFlag string
passwordFlag string
)

func init() {
flag.BoolVar(&listFlag, "list", false, fmt.Sprintf("%-8s List supported algorithms", ""))
flag.BoolVar(&decryptFlag, "d", false, fmt.Sprintf("%-8s Decrypt", ""))

flag.StringVar(&cipherFlag, "cipher", "", fmt.Sprintf("%-8s Specify cipher - default: platform depended", "string"))
flag.StringVar(&passwordFlag, "p", "", fmt.Sprintf("%-8s Specify the password - default: prompt for password", "string"))

flag.Usage = func() {
printFlag := func(f *flag.Flag) {
Expand Down Expand Up @@ -217,14 +219,16 @@ func readPassword(src *os.File) []byte {
fmt.Fprintln(src, "")
if len(password) == 0 {
fmt.Fprintln(os.Stderr, "Failed to read password: No password")
exit(codeOK)
exit(codeError)
}
return password
}

func deriveKey(dst, src *os.File) []byte {
password, salt := []byte{}, make([]byte, 32)
if src == os.Stdin {
if passwordFlag != "" {
password = []byte(passwordFlag)
} else if src == os.Stdin {
password = readPassword(os.Stderr)
} else {
password = readPassword(os.Stdin)
Expand Down

0 comments on commit ff852fa

Please sign in to comment.