diff --git a/cmd/age/age.go b/cmd/age/age.go index e5d17e2b..275e17b2 100644 --- a/cmd/age/age.go +++ b/cmd/age/age.go @@ -455,10 +455,20 @@ func decryptNotPass(flags identityFlags, in io.Reader, out io.Writer) { } func decryptPass(in io.Reader, out io.Writer) { + passphrase := passphrasePromptForDecryption + + if in != os.Stdin && !term.IsTerminal(int(os.Stdin.Fd())) { + passphrase = func() (string, error) { + b, err := io.ReadAll(os.Stdin) + b = bytes.TrimRight(b, "\n") + return string(b), err + } + } + identities := []age.Identity{ // If there is an scrypt recipient (it will have to be the only one and) // this identity will be invoked. - &LazyScryptIdentity{passphrasePromptForDecryption}, + &LazyScryptIdentity{passphrase}, } decrypt(identities, in, out) diff --git a/cmd/age/testdata/output_file.txt b/cmd/age/testdata/output_file.txt index 5b16d654..24b5219f 100644 --- a/cmd/age/testdata/output_file.txt +++ b/cmd/age/testdata/output_file.txt @@ -54,7 +54,7 @@ cmp inputcopy input # https://github.com/FiloSottile/age/issues/159 ttyin terminal age -p -a -o test.age input -ttyin terminalwrong +ttyin -stdin terminalwrong ! age -o test.out -d test.age ttyout 'Enter passphrase' stderr 'incorrect passphrase' diff --git a/cmd/age/testdata/scrypt.txt b/cmd/age/testdata/scrypt.txt index 93298855..abc26895 100644 --- a/cmd/age/testdata/scrypt.txt +++ b/cmd/age/testdata/scrypt.txt @@ -10,14 +10,14 @@ ttyout 'Enter passphrase' ! stdout . # decrypt with a provided passphrase -ttyin terminal +ttyin -stdin terminal age -d test.age ttyout 'Enter passphrase' ! stderr . cmp stdout input # decrypt with the wrong passphrase -ttyin wrong +ttyin -stdin wrong ! age -d test.age stderr 'incorrect passphrase' @@ -27,7 +27,7 @@ ttyin empty age -p -o test.age ! stderr . ! stdout . -ttyin autogenerated +ttyin -stdin autogenerated age -d test.age cmp stdout input diff --git a/cmd/age/testdata/terminal.txt b/cmd/age/testdata/terminal.txt index b2cf0078..8923b7d5 100644 --- a/cmd/age/testdata/terminal.txt +++ b/cmd/age/testdata/terminal.txt @@ -34,7 +34,12 @@ age -p -a -o test.age ttyout 'Enter passphrase' ! stderr . # check the file was encrypted correctly -ttyin terminal +ttyin -stdin terminal +age -d test.age +cmp stdout input + +# read passphrase from stdin +stdin password age -d test.age cmp stdout input @@ -53,5 +58,7 @@ test -- terminal -- password password +-- password -- +password -- empty --