Skip to content

Commit

Permalink
Format with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Feb 11, 2025
1 parent 548559d commit e746547
Show file tree
Hide file tree
Showing 19 changed files with 1,181 additions and 1,128 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Test
on:
push:
branches:
- main
pull_request:
workflow_dispatch:
jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
node_version: ["current"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm ci --no-audit
- name: Lint
run: npm run lint
48 changes: 24 additions & 24 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ jobs:
if: github.repository == 'dcodeIO/bcrypt.js'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: main
- uses: actions/setup-node@v4
with:
node-version: current
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Run tests
run: npm test
- name: Publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
VERSION=$(npx aspublish --version)
if [ -z "$VERSION" ]; then
echo "Changes do not trigger a release"
else
echo "Publishing new version: $VERSION"
npx aspublish
fi
- uses: actions/checkout@v4
with:
ref: main
- uses: actions/setup-node@v4
with:
node-version: current
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Run tests
run: npm test
- name: Publish
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
VERSION=$(npx aspublish --version)
if [ -z "$VERSION" ]; then
echo "Changes do not trigger a release"
else
echo "Publishing new version: $VERSION"
npx aspublish
fi
30 changes: 15 additions & 15 deletions .github/workflows/stale.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
name: Stale
on:
schedule:
- cron: '0 0 * * *'
- cron: "0 0 * * *"
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v9
with:
stale-issue-message: 'This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!'
stale-issue-label: 'stale'
stale-pr-message: 'This PR has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!'
close-pr-message: 'This PR has been automatically closed due to lack of recent activity, but feel free to reopen it as long as you merge in the main branch afterwards.'
exempt-issue-labels: 'bug,enhancement,compatibility'
exempt-pr-labels: 'breaking change'
exempt-draft-pr: true
exempt-all-milestones: true
exempt-all-assignees: true
days-before-issue-stale: 30
days-before-pr-stale: 60
days-before-close: 7
- uses: actions/stale@v9
with:
stale-issue-message: "This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!"
stale-issue-label: "stale"
stale-pr-message: "This PR has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!"
close-pr-message: "This PR has been automatically closed due to lack of recent activity, but feel free to reopen it as long as you merge in the main branch afterwards."
exempt-issue-labels: "bug,enhancement,compatibility"
exempt-pr-labels: "breaking change"
exempt-draft-pr: true
exempt-all-milestones: true
exempt-all-assignees: true
days-before-issue-stale: 30
days-before-pr-stale: 60
days-before-close: 7
22 changes: 11 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Test
on:
push:
branches:
- main
- main
pull_request:
workflow_dispatch:
jobs:
Expand All @@ -13,13 +13,13 @@ jobs:
matrix:
node_version: ["current", "lts/*"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm ci --no-audit
- name: Build
run: npm run build
- name: Run tests
run: npm test
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_version }}
- name: Install dependencies
run: npm ci --no-audit
- name: Build
run: npm run build
- name: Run tests
run: npm test
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
109 changes: 59 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
bcrypt.js
=========
# bcrypt.js

Optimized bcrypt in JavaScript with zero dependencies, with TypeScript support. Compatible to the C++
[bcrypt](https://npmjs.org/package/bcrypt) binding on Node.js and also working in the browser.

[![Build Status](https://img.shields.io/github/actions/workflow/status/dcodeIO/bcrypt.js/test.yml?branch=main&label=test&logo=github)](https://github.com/dcodeIO/bcrypt.js/actions/workflows/test.yml) [![Publish Status](https://img.shields.io/github/actions/workflow/status/dcodeIO/bcrypt.js/publish.yml?branch=main&label=publish&logo=github)](https://github.com/dcodeIO/bcrypt.js/actions/workflows/publish.yml) [![npm](https://img.shields.io/npm/v/bcryptjs.svg?label=npm&color=007acc&logo=npm)](https://www.npmjs.com/package/bcryptjs)

Security considerations
-----------------------
## Security considerations

Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the
iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with
Expand All @@ -19,8 +17,7 @@ processed in an equal time span.
The maximum input length is 72 bytes (note that UTF8 encoded characters use up to 4 bytes) and the length of generated
hashes is 60 characters.

Usage
-----
## Usage

The package exports an ECMAScript module with an UMD fallback.

Expand All @@ -29,93 +26,93 @@ $> npm install bcryptjs
```

```ts
import bcrypt from 'bcryptjs'
import bcrypt from "bcryptjs";
```

### Usage with a CDN

* From GitHub via [jsDelivr](https://www.jsdelivr.com):<br />
- From GitHub via [jsDelivr](https://www.jsdelivr.com):<br />
`https://cdn.jsdelivr.net/gh/dcodeIO/bcrypt.js@TAG/index.js` (ESM)
* From npm via [jsDelivr](https://www.jsdelivr.com):<br />
- From npm via [jsDelivr](https://www.jsdelivr.com):<br />
`https://cdn.jsdelivr.net/npm/bcryptjs@VERSION/index.js` (ESM)<br />
`https://cdn.jsdelivr.net/npm/bcryptjs@VERSION/umd/index.js` (UMD)
* From npm via [unpkg](https://unpkg.com):<br />
- From npm via [unpkg](https://unpkg.com):<br />
`https://unpkg.com/bcryptjs@VERSION/index.js` (ESM)<br />
`https://unpkg.com/bcryptjs@VERSION/umd/index.js` (UMD)

Replace `TAG` respectively `VERSION` with a [specific version](https://github.com/dcodeIO/bcrypt.js/releases) or omit it (not recommended in production) to use latest.

### Usage - Sync

To hash a password:
To hash a password:

```ts
const salt = bcrypt.genSaltSync(10)
const hash = bcrypt.hashSync('B4c0/\/', salt)
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync("B4c0/\/", salt);
// Store hash in your password DB
```

To check a password:

```ts
// Load hash from your password DB
bcrypt.compareSync('B4c0/\/', hash) // true
bcrypt.compareSync('not_bacon', hash) // false
bcrypt.compareSync("B4c0/\/", hash); // true
bcrypt.compareSync("not_bacon", hash); // false
```

Auto-gen a salt and hash:

```ts
const hash = bcrypt.hashSync('bacon', 10)
const hash = bcrypt.hashSync("bacon", 10);
```

### Usage - Async

To hash a password:

```ts
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash('B4c0/\/', salt)
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash("B4c0/\/", salt);
// Store hash in your password DB
```

```ts
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash('B4c0/\/', salt, function(err, hash) {
// Store hash in your password DB
})
})
bcrypt.hash("B4c0/\/", salt, function (err, hash) {
// Store hash in your password DB
});
});
```

To check a password:
To check a password:

```ts
// Load hash from your password DB
await bcrypt.compare('B4c0/\/', hash) // true
await bcrypt.compare('not_bacon', hash) // false
await bcrypt.compare("B4c0/\/", hash); // true
await bcrypt.compare("not_bacon", hash); // false
```

```ts
// Load hash from your password DB
bcrypt.compare('B4c0/\/', hash, (err, res) => {
// res === true
})
bcrypt.compare('not_bacon', hash, (err, res) => {
// res === false
})
bcrypt.compare("B4c0/\/", hash, (err, res) => {
// res === true
});
bcrypt.compare("not_bacon", hash, (err, res) => {
// res === false
});
```

Auto-gen a salt and hash:

```ts
await bcrypt.hash('B4c0/\/', 10)
await bcrypt.hash("B4c0/\/", 10);
// Store hash in your password DB
```

```ts
bcrypt.hash('B4c0/\/', 10, (err, hash) => {
// Store hash in your password DB
})
bcrypt.hash("B4c0/\/", 10, (err, hash) => {
// Store hash in your password DB
});
```

**Note:** Under the hood, asynchronous APIs split an operation into small chunks. After the completion of a chunk, the execution of the next chunk is placed on the back of the [JS event queue](https://developer.mozilla.org/en/docs/Web/JavaScript/EventLoop), efficiently yielding for other computation to execute.
Expand All @@ -126,50 +123,62 @@ bcrypt.hash('B4c0/\/', 10, (err, hash) => {
Usage: bcrypt <input> [rounds|salt]
```

API
---
## API

### Callback types

* type **Callback<`T`>**: `(err: Error | null, result?: T) => void`
- type **Callback<`T`>**: `(err: Error | null, result?: T) => void`

* type **ProgressCallback**: `(percentage: number) => void`
- type **ProgressCallback**: `(percentage: number) => void`

### Functions

* bcrypt.**setRandomFallback**(random: `(length: number) => number[]`): `void`<br />
- bcrypt.**setRandomFallback**(random: `(length: number) => number[]`): `void`<br />
Sets the pseudo random number generator to use as a fallback if neither Node's [crypto](http://nodejs.org/api/crypto.html) module nor the [Web Crypto API](http://www.w3.org/TR/WebCryptoAPI/) is available. Please note: It is highly important that the PRNG used is cryptographically secure and that it is seeded properly!

* bcrypt.**genSaltSync**(rounds?: `number`): `string`<br />
- bcrypt.**genSaltSync**(rounds?: `number`): `string`<br />
Synchronously generates a salt. Number of rounds defaults to 10 when omitted.

* bcrypt.**genSalt**(rounds?: `number`): `Promise<string>`<br />
- bcrypt.**genSalt**(rounds?: `number`): `Promise<string>`<br />
bcrypt.**genSalt**(rounds: `number`, callback: `Callback<string>`): `void`<br />
bcrypt.**genSalt**(callback: `Callback<string>`): `void`<br />
Asynchronously generates a salt. Number of rounds defaults to 10 when omitted.

* bcrypt.**hashSync**(s: `string`, salt?: `number | string`): `string`
- bcrypt.**hashSync**(s: `string`, salt?: `number | string`): `string`
Synchronously generates a hash for the given string. Number of rounds defaults to 10 when omitted.

* bcrypt.**hash**(s: `string`, salt: `number | string`): `Promise<string>`<br />
- bcrypt.**hash**(s: `string`, salt: `number | string`): `Promise<string>`<br />
bcrypt.**hash**(s: `string`, salt: `number | string`, callback: `Callback<string>`, progressCallback?: `ProgressCallback`): `void`<br />
Asynchronously generates a hash for the given string. Optionally calls a progress callback with the percentage of rounds completed (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.

* bcrypt.**compareSync**(s: `string`, hash: `string`): `boolean`<br />
- bcrypt.**compareSync**(s: `string`, hash: `string`): `boolean`<br />
Synchronously tests a string against a hash.

* bcrypt.**compare**(s: `string`, hash: `string`): `Promise<boolean>`<br />
- bcrypt.**compare**(s: `string`, hash: `string`): `Promise<boolean>`<br />
bcrypt.**compare**(s: `string`, hash: `string`, callback: `Callback<boolean>`, progressCallback?: `ProgressCallback`)<br />
Asynchronously compares a string against a hash. Optionally calls a progress callback with the percentage of rounds completed (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.

* bcrypt.**getRounds**(hash: `string`): `number`<br />
- bcrypt.**getRounds**(hash: `string`): `number`<br />
Gets the number of rounds used to encrypt the specified hash.

* bcrypt.**getSalt**(hash: `string`): `string`<br />
- bcrypt.**getSalt**(hash: `string`): `string`<br />
Gets the salt portion from a hash. Does not validate the hash.

Credits
-------
## Building

Building the UMD fallback:

```
$> npm run build
```

Running the [tests](./tests):

```
$> npm test
```

## Credits

Based on work started by Shane Girish at [bcrypt-nodejs](https://github.com/shaneGirish/bcrypt-nodejs), which is itself
based on [javascript-bcrypt](http://code.google.com/p/javascript-bcrypt/) (New BSD-licensed).
Loading

0 comments on commit e746547

Please sign in to comment.