Skip to content

Commit 59954e1

Browse files
author
Daniel Wirtz
committed
Browser & AMD support
1 parent 37046c1 commit 59954e1

7 files changed

Lines changed: 356 additions & 52 deletions

File tree

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
11
![bcrypt.js - bcrypt in plain JavaScript](https://raw.github.com/dcodeIO/bcrypt.js/master/bcrypt.png)
22
===========
33
Optimized bcrypt in plain JavaScript with zero dependencies. Compiled through Closure Compiler using advanced
4-
optimizations, 100% typed code. Fully compatible to [bcrypt](https://npmjs.org/package/bcrypt).
4+
optimizations, 100% typed code. Fully compatible to [bcrypt](https://npmjs.org/package/bcrypt) and also working in the
5+
browser.
6+
7+
Features
8+
--------
9+
* CommonJS/node.js compatible (via [crypto](http://nodejs.org/api/crypto.html)), also available via [npm](https://npmjs.org/package/bcryptjs)
10+
* Shim/browser compatible (via [WebCryptoAPI](http://www.w3.org/TR/WebCryptoAPI))
11+
* RequireJS/AMD compatible
12+
* Zero production dependencies
13+
* Small footprint
14+
15+
Usage
16+
-----
17+
18+
#### node.js
19+
`npm install bcryptjs`
20+
21+
```javascript
22+
var bcrypt = require('bcryptjs');
23+
...
24+
```
25+
26+
#### RequireJS/AMD
27+
```javascript
28+
require.config({
29+
"paths": {
30+
"bcrypt": "/path/to/bcrypt.js"
31+
}
32+
});
33+
require(["bcrypt"], function(bcrypt) {
34+
...
35+
});
36+
```
37+
38+
#### Shim/browser
39+
```html
40+
<script src="//raw.github.com/dcodeIO/bcrypt.js/master/bcrypt.min.js"></script>
41+
```
42+
```javascript
43+
var bcrypt = dcodeIO.bcrypt;
44+
...
45+
```
546

647
Usage - Sync
748
------------

bcrypt.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
* Released under the Apache License, Version 2.0
3232
* see: https://github.com/dcodeIO/bcrypt.js for details
3333
*/
34-
module.exports = (function() {
35-
36-
var crypto = require("crypto");
34+
(function(global) {
3735

3836
/**
3937
* @type {Array.<string>}
@@ -158,7 +156,7 @@ module.exports = (function() {
158156
res.push(rs[off].charCodeAt(0));
159157
}
160158
return res;
161-
};
159+
};
162160
/**
163161
* bcrypt namespace.
164162
* @type {Object.<string,*>}
@@ -722,6 +720,26 @@ module.exports = (function() {
722720
}
723721
}
724722

723+
/**
724+
* Generates cryptographically secure random bytes.
725+
* @param {number} len Number of bytes to generate
726+
* @returns {Array.<number>}
727+
* @private
728+
*/
729+
function _randomBytes(len) {
730+
// node.js, see: http://nodejs.org/api/crypto.html
731+
if (typeof module !== 'undefined' && module.exports) {
732+
var crypto = require("crypto");
733+
return crypto.randomBytes(len);
734+
735+
// Browser, see: http://www.w3.org/TR/WebCryptoAPI/
736+
} else {
737+
var array = new Uint32Array(len);
738+
window.crypto.getRandomValues(array);
739+
return Array.prototype.slice.call(array);
740+
}
741+
}
742+
725743
/**
726744
* Internally generates a salt.
727745
* @param {number} rounds Number of rounds to use
@@ -740,7 +758,7 @@ module.exports = (function() {
740758
salt.push(rounds.toString());
741759
salt.push('$');
742760
try {
743-
salt.push(base64.encode(crypto.randomBytes(BCRYPT_SALT_LEN), BCRYPT_SALT_LEN));
761+
salt.push(base64.encode(_randomBytes(BCRYPT_SALT_LEN), BCRYPT_SALT_LEN));
744762
return salt.join('');
745763
} catch(err) {
746764
throw(err);
@@ -902,8 +920,18 @@ module.exports = (function() {
902920
}
903921
return hash.substring(0, 29);
904922
};
923+
924+
// Enable module loading if available
925+
if (typeof module != 'undefined' && module["exports"]) { // CommonJS
926+
module["exports"] = bcrypt;
927+
} else if (typeof define != 'undefined' && define["amd"]) { // AMD
928+
define("bcrypt", function() { return bcrypt; });
929+
} else { // Shim
930+
if (!global["dcodeIO"]) {
931+
global["dcodeIO"] = {};
932+
}
933+
global["dcodeIO"]["bcrypt"] = bcrypt;
934+
}
905935

906-
return bcrypt;
907-
908-
})();
936+
})(this);
909937

0 commit comments

Comments
 (0)