Skip to content

Commit 49932a5

Browse files
committed
Compare performance to native bcrypt, fixes #14
1 parent 17a693f commit 49932a5

9 files changed

Lines changed: 81 additions & 38 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ node_modules/
22
npm-debug.log
33
.idea/
44
doco/
5-
tests/bench.js

README.md

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,61 @@
1-
![bcrypt.js - bcrypt in plain JavaScript](https://raw.github.com/dcodeIO/bcrypt.js/master/bcrypt.png)
1+
![bcrypt.js - Optimized bcrypt in JavaScript with zero dependencies](https://raw.github.com/dcodeIO/bcrypt.js/master/bcrypt.png)
22
===========
3-
Optimized bcrypt in plain JavaScript with zero dependencies. Compatible to the C++ [bcrypt](https://npmjs.org/package/bcrypt)
4-
binding and also working in the browser.
5-
6-
Features ![Build Status](https://travis-ci.org/dcodeIO/bcrypt.js.png?branch=master)
7-
--------
8-
* CommonJS compatible (via [crypto](http://nodejs.org/api/crypto.html)), also available via [npm](https://npmjs.org/package/bcryptjs)
9-
* Browser compatible (via [WebCryptoAPI](http://www.w3.org/TR/WebCryptoAPI))
10-
* AMD compatible
11-
* Zero production dependencies
12-
* Small footprint
13-
* ISAAC PRNG as default fallback with bcrypt-isaac.js
14-
* Compiled with Closure Compiler using advanced optimizations, [externs included](https://github.com/dcodeIO/bcrypt.js/blob/master/externs/bcrypt.js)
3+
Optimized bcrypt in JavaScript with zero dependencies. Compatible to the C++ [bcrypt](https://npmjs.org/package/bcrypt)
4+
binding on node.js and also working in the browser.
5+
6+
[![Build Status](https://travis-ci.org/dcodeIO/bcrypt.js.svg?branch=master)](https://travis-ci.org/dcodeIO/bcrypt.js)
7+
[![Donate](https://raw.githubusercontent.com/dcodeIO/bcrypt.js/master/donate.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=info%40code-emitter.com&item_name=Open%20Source%3A%20bcrypt.js)
158

169
Security considerations
1710
-----------------------
1811
Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the
1912
iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with
2013
increasing computation power. ([see](http://en.wikipedia.org/wiki/Bcrypt))
2114

22-
While bcrypt.js is compatible to the C++ bcrypt binding, it is written in pure JavaScript and thus slower, effectively
23-
reducing the number of iterations that can be processed in an equal time span.
15+
While bcrypt.js is compatible to the C++ bcrypt binding, it is written in pure JavaScript and thus slower ([about 2.7
16+
times](https://github.com/dcodeIO/bcrypt.js/wiki/Benchmark)), effectively reducing the number of iterations that can be
17+
processed in an equal time span.
2418

2519
Usage
2620
-----
21+
The library is compatible with CommonJS and AMD loaders and is exposed globally as `dcodeIO.bcrypt` if neither is
22+
available.
23+
24+
### node.js
25+
26+
On node.js, the inbuilt [crypto module](http://nodejs.org/api/crypto.html)'s randomBytes interface is used to obtain
27+
secure random numbers.
2728

28-
#### node.js
2929
`npm install bcryptjs`
3030

31-
```javascript
31+
```js
3232
var bcrypt = require('bcryptjs');
3333
...
3434
```
3535

36-
#### RequireJS/AMD
37-
```javascript
36+
### Browser
37+
38+
In the browser, bcrypt.js by default relies on [Web Crypto API](http://www.w3.org/TR/WebCryptoAPI)'s getRandomValues
39+
interface to obtain secure random numbers. bcrypt-isaac.js additionally ships with the ISAACs PRNG used as the default
40+
fallback if the former is not available. See [bcrypt.setRandomFallback](https://github.com/dcodeIO/bcrypt.js#setrandomfallbackrandom)
41+
to set a custom fallback.
42+
43+
```js
44+
var bcrypt = dcodeIO.bcrypt;
45+
...
46+
```
47+
48+
or
49+
50+
```js
3851
require.config({
39-
"paths": {
40-
"bcrypt": "/path/to/bcrypt.js"
41-
}
52+
paths: { "bcrypt": "/path/to/bcrypt.js" }
4253
});
4354
require(["bcrypt"], function(bcrypt) {
4455
...
4556
});
4657
```
4758

48-
#### Shim/browser
49-
```html
50-
<script src="//raw.github.com/dcodeIO/bcrypt.js/master/bcrypt.min.js"></script>
51-
```
52-
```javascript
53-
var bcrypt = dcodeIO.bcrypt;
54-
...
55-
```
56-
5759
Usage - Sync
5860
------------
5961
To hash a password:
@@ -229,4 +231,4 @@ which is itself based on [javascript-bcrypt](http://code.google.com/p/javascript
229231

230232
License
231233
-------
232-
Apache License, Version 2.0 if not stated otherwise
234+
New-BSD / MIT ([see](https://github.com/dcodeIO/bcrypt.js/blob/master/LICENSE))

dist/bcrypt-isaac.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@
10181018
if (progressCallback)
10191019
progressCallback(i/rounds);
10201020
if (i < rounds) {
1021-
var start = new Date();
1021+
var start = Date.now();
10221022
for (; i < rounds;) {
10231023
i = i + 1;
10241024
_key(b, P, S);

dist/bcrypt-isaac.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/bcrypt.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@
10141014
if (progressCallback)
10151015
progressCallback(i/rounds);
10161016
if (i < rounds) {
1017-
var start = new Date();
1017+
var start = Date.now();
10181018
for (; i < rounds;) {
10191019
i = i + 1;
10201020
_key(b, P, S);

dist/bcrypt.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

donate.png

1.52 KB
Loading

src/bcrypt/impl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ function _crypt(b, salt, rounds, callback, progressCallback) {
421421
if (progressCallback)
422422
progressCallback(i/rounds);
423423
if (i < rounds) {
424-
var start = new Date();
424+
var start = Date.now();
425425
for (; i < rounds;) {
426426
i = i + 1;
427427
_key(b, P, S);

tests/bench.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var bcrypt = require("bcrypt"),
2+
bcryptjs = require("../index.js"),
3+
pass = "ä☺𠜎️☁",
4+
testRounds = [8, 9, 10, 11, 12, 13, 14, 15];
5+
6+
function testSync(name, salt, impl) {
7+
var res;
8+
console.time(name);
9+
res = impl.hashSync(pass, salt);
10+
console.timeEnd(name);
11+
console.log("`"+res+"` ");
12+
}
13+
14+
function testAsync(name, salt, impl, cb) {
15+
console.time(name);
16+
impl.hash(pass, salt, function(err, res) {
17+
console.timeEnd(name);
18+
console.log("`"+res+"` ");
19+
if (cb) cb();
20+
});
21+
}
22+
23+
console.log("## Comparing bcryptjs with bcrypt\n");
24+
25+
function next() {
26+
if (testRounds.length === 0)
27+
return;
28+
(function(rounds) {
29+
var salt = bcryptjs.genSaltSync(rounds);
30+
console.log("#### Using "+rounds+" rounds");
31+
console.log("Salt: `"+salt+"` ");
32+
testSync("* **bcrypt** sync", salt, bcrypt);
33+
testSync("* **bcrypt.js** sync", salt, bcryptjs);
34+
testAsync("* **bcrypt** async", salt, bcrypt, function() {
35+
testAsync("* **bcrypt.js** async", salt, bcryptjs, function() {
36+
console.log("");
37+
next();
38+
});
39+
});
40+
})(testRounds.shift());
41+
}
42+
next();

0 commit comments

Comments
 (0)