https://github.com/sequencemedia/pbkdf2
https://github.com/sequencemedia/pbkdf2
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sequencemedia/pbkdf2
- Owner: sequencemedia
- Created: 2023-01-27T19:28:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-05-23T04:20:37.000Z (about 2 years ago)
- Last Synced: 2024-05-23T05:29:43.215Z (about 2 years ago)
- Language: JavaScript
- Size: 1.69 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @sequencemedia/pbkdf2
## Compare a given password to a `hash` and `salt`
Exposes the crypto `pbkdf2` utilities as `async` functions for Node and the shell
ES functions are exported from the package root alongside a TS definition
## Password validation by `hash` and `salt` comparison
A password can be compared with a `hash` and `salt` (and the number of `iterations` that were used to compute it) to determine whether it is valid
The ES functions expect the same argument types as the underlying Node utilities
- The `hash` is a `Buffer`
- The `salt` is a `Buffer`
- `iterations` is a `Number`
- `keylen` is a `Number`
- `digest` is a `String`
Etc.
### `compare`
The password is expected as the first argument, while the others are fields on a params object
```javascript
const isValid = await compare(password, {
hash,
salt,
iterations,
keylen,
digest
})
```
### `hash`
As with `compare`, the password is expected as the first argument, while the others are fields on a params object (except for `hash` which can of course be omitted)
```javascript
const value = await hash(password, {
hash,
iterations,
keylen,
digest
})
```
### `salt`
An `async` wrapper around `crypto.randomBytes()` to _generate_ a salt of the salt size
```javascript
const value = await salt(size)
```
## In the shell
Scripts are exposed to `npm` in the package and each script can of course be invoked from the command line directly in the shell
```json
{
"compare": "node scripts/compare.mjs",
"hash": "node scripts/hash.mjs",
"salt": "node scripts/salt.mjs"
}
```
Where required, both the `hash` and `salt` arguments are expected from the command line to be strings in _Base64_ format. Both `iterations` and `keylen` are coerced from strings to numbers
```bash
npm run compare -- \
--password \
--hash \
--salt \
--keylen \
--iterations \
--digest
```
```bash
node ./scripts/compare.mjs \
--password \
--hash \
--salt \
--keylen \
--iterations \
--digest
```
Etc.
- The `hash` is decoded from a _Base64_ `String` to a `Buffer`
- The `salt` is decoded from a _Base64_ `String` to a `Buffer`
## Tests
```bash
npm test
```
```bash
./compare.sh
```