Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra tweaks to CLI #72

Merged
merged 8 commits into from
Jan 9, 2024
Merged
3 changes: 3 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ cradle:
component: "password-instances:test:doctests"
- path: "./password-instances/test/tasty"
component: "password-instances:test:password-instances-tasty"

- path: "./password-cli/app"
component: "password-cli:exe:password-cli"
26 changes: 25 additions & 1 deletion password-cli/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
## 0.1.1.0

- Small refactor and quality of life additions.
Thanks to [@Vlix](https://github.com/Vlix)
[#72](https://github.com/cdepillabout/password/pull/72)

- Changes include:
- More complete explanation of the CLI in the README.
- Added more description of commands and options.
- Added option to read literal contents of a file.
- Hash output now adds a newline when using the CLI interactively. (on Unix)
- Added `--version` to only output the version of the CLI.

## 0.1.0.0

- Initial version.
- First minimal working CLI to hash passwords and verify hashes.
Thanks to [@blackheaven](https://github.com/blackheaven)
[#70](https://github.com/cdepillabout/password/pull/70)

- Functionality includes:
- Hashing (`Argon2`, `bcrypt`, `PBKDF2`, `scrypt`) interactively,
piped to `stdin`, or from the first line in a provided file.
- Checking a hash (`Argon2`, `bcrypt`, `PBKDF2`, `scrypt`) that is
provided through a CLI option, or from a provided file. The password
can be entered interactively, piped to `stdin` or from the first
line in a provided file.
- Option to disable logging to stdout or stderr. `-q|--quiet`
91 changes: 84 additions & 7 deletions password-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,99 @@
# password-cli
# `password-cli`

[![Build Status](https://github.com/cdepillabout/password/workflows/password/badge.svg)](http://github.com/cdepillabout/password)
[![Hackage](https://img.shields.io/hackage/v/password-cli.svg)](https://hackage.haskell.org/package/password-cli)
[![Stackage LTS](http://stackage.org/package/password-cli/badge/lts)](http://stackage.org/lts/package/password-cli)
[![Stackage Nightly](http://stackage.org/package/password-cli/badge/nightly)](http://stackage.org/nightly/package/password-cli)
[![BSD3 license](https://img.shields.io/badge/license-BSD3-blue.svg)](./LICENSE)

This package provides a simple CLI for [password](https://hackage.haskell.org/package/password) package.
This package provides a simple CLI for the [`password`](https://hackage.haskell.org/package/password) package.
As such it supports all the algorithms that the [`password`](https://hackage.haskell.org/package/password)
package supports, which at the time of writing are `Argon2`, `brypt`, `PBKDF2` and `scrypt`.

Pipeline usage:
At the moment, the default settings are used for each algorithm, but this will probably become configurable in
a later version of the CLI.

## Example usage

The following sections give examples of how the CLI can be used.

### Hashing a password interactively

Hashing a password interactively is as easy as
```
user@computer $ password-cli hash bcrypt
Enter password:
Vlix marked this conversation as resolved.
Show resolved Hide resolved
```
where the input is then hidden and the hash is printed to the screen, resulting in
```
user@computer $ password-cli hash bcrypt
Enter password:
$2b$10$JuNbIWqVQD2EldT481zEEuaVKROrYhsHXLjM/Tx3e7ahJQxVw7N4y
user@computer $
```
Vlix marked this conversation as resolved.
Show resolved Hide resolved

### Hashing a password with pipes

When piping in the password from a file or other program:
```
user@computer $ cat password.txt | password-cli hash pbkdf2
Enter password:
sha512:25000:8ZJ1T55Y0sPRwltXNe/2fA==:aA0BT1WlTg+t2pSr8E6+l2zJW88rmUiDlKeohSOnzS0nLOumDSyK0FfsiNJBvWvWVkB2r6IMxRqelk4LZR33ow==user@computer $
```
$ password-cli check argon2 --quiet --hash $(password-cli hash argon2 --quiet)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```
user@computer $ cat password.txt | password-cli hash pbkdf2
Enter password:
sha512:25000:8ZJ1T55Y0sPRwltXNe/2fA==:aA0BT1WlTg+t2pSr8E6+l2zJW88rmUiDlKeohSOnzS0nLOumDSyK0FfsiNJBvWvWVkB2r6IMxRqelk4LZR33ow==user@computer $
```
```console
$ cat password.txt | password-cli hash pbkdf2
Enter password:
sha512:25000:8ZJ1T55Y0sPRwltXNe/2fA==:aA0BT1WlTg+t2pSr8E6+l2zJW88rmUiDlKeohSOnzS0nLOumDSyK0FfsiNJBvWvWVkB2r6IMxRqelk4LZR33ow==
```

I'd just remove the user@computer $ at the end of the line here, since you explain it in the next paragraph.

Although I see why you originally wrote it like this. It may be easier to understand as you have it in this current PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but without the user@computer it's difficult to convey it obviously.
I removed it and hope it's obvious from the text and any testing people would do with the CLI.

You'll notice the output has no newline, so you can easily pipe the resulting
hash into a file or other program. When piping the result to a file, you'll
probably want to use `--quiet` or `-q` to make sure the `Enter password:` prompt
isn't also saved to the file.
```
user@computer $ cat password.txt | password-cli hash pbkdf2 --quiet > password.hash
user@computer $ cat password.hash && echo
sha512:25000:iFYCOgfOgMPp0NuPXhyucw==:XUMDNnqZo2LH08CIZr+1nbTke3N6pE95FcbZA+4A1Ng4dWHnnl4SMUTn3KXFtB0uZRrEhArLatLAH1Oo8brcVw==
user@computer $
```
When piping in the password, the first line of the file (i.e. up to the first newline)
Vlix marked this conversation as resolved.
Show resolved Hide resolved
is read and taken as the password. This is also the case if the password is provided
from a file, though you can set the `--literal-contents` flag to use the entire literal
file contents as the password.

### Hashing a password from a file

Instead of piping in the contents of a file, you can also just provide the path
to the file.
```
user@computer $ password-cli hash scrypt --password-file password.txt
14|8|1|mdSECCGuEMf7GQOp9EX5EYLMW9Jwe6Dma7fwbxuNwvs=|KSh5jxOEiQPMjfng2D05/G1baiF2LyluWgg3Cfzh5arJUF3K7irRIBXoKAT/xCO11oPmsgDD7TT6l6FQth9f4g==user@computer $
```
Here you don't have to pass in the `--quiet` option, since the password is already provided
Vlix marked this conversation as resolved.
Show resolved Hide resolved
so the CLI doesn't print `Enter password:` to the screen.

Interactive mode (default):
### Verifying a password hash

Just like when hashing a password, you can input the password manually, through pipes, or
by providing a `--password-file`.
```
user@computer $ # Interactively check password
user@computer $ password-cli check argon2 --hash "SOME-HASH"
Enter password:
Password matches provided hash
user@computer $ echo $?
0
```
If the provided hash doesn't match the password, `Password does not match provided hash`
Vlix marked this conversation as resolved.
Show resolved Hide resolved
will be shown and the exit code will be `1` to indicate a failed match.
```
user@computer $ # Pipe in the password.
user@computer $ cat password.txt | password-cli check argon2 --hash "SOME-HASH" --quiet
user@computer $ echo $?
0
user@computer $ # Give the WRONG password file.
user@computer $ password-cli check argon2 --hash "SOME-HASH" --password-file password.txt.wrong --quiet
user@computer $ echo $?
1
```

Vlix marked this conversation as resolved.
Show resolved Hide resolved
You can also provide the hash from file contents by providing the path to the `--hash-file`
option. Just like the default of the `--password-file` option, this will only read up to the
first newline.
```
$ password-cli hash argon2
Vlix marked this conversation as resolved.
Show resolved Hide resolved
$ password-cli check argon2 --hash "SOME-HASH"
user@computer $ password-cli check argon2 --hash-file password.hash
```
Vlix marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading