Skip to content

Commit e55eb39

Browse files
committed
Added sync version and bumped major version.
1 parent ea5ade0 commit e55eb39

File tree

6 files changed

+886
-268
lines changed

6 files changed

+886
-268
lines changed

Diff for: README.md

+52-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
scrypt
22
======
33

4-
The [scrypt](https://en.wikipedia.org/wiki/Scrypt) password-base key derivation function (pbkdf) is an algorithm designed to be brute-force resistant that converts human readable passwords into fixed length arrays of bytes, which can then be used as a key for symmetric block ciphers, private keys, et cetera.
4+
The [scrypt](https://en.wikipedia.org/wiki/Scrypt) password-base key derivation
5+
function (pbkdf) is an algorithm designed to be brute-force resistant that
6+
converts human readable passwords into fixed length arrays of bytes, which can
7+
then be used as a key for symmetric block ciphers, private keys, et cetera.
58

69
### Features:
710
- **Non-blocking** - Gives other events in the event loop opportunities to run (asynchronous)
@@ -40,12 +43,28 @@ npm install scrypt-js
4043
API
4144
---
4245

46+
**scrypt . scrypt ( password , salt , N , r , p , dkLen [ , progressCallback ] )** *=> Promise<Uint8Array>*
47+
48+
Compute the scrypt PBKDF asynchronously using a Promise. If *progressCallback* is
49+
provided, it is periodically called with a single parameter, a number between 0 and
50+
1 (inclusive) indicating the completion progress; it will **always** emit 0 at the
51+
beginning and 1 at the end, and numbers between may repeat.
52+
53+
**scrypt . syncScrypt ( password , salt , N , r , p , dkLen )** *=> Uint8Array*
54+
55+
Compute the scrypt PBKDF synchronously. Keep in mind this may stall UI and other tasks and the
56+
asynchronous version is highly preferred.
57+
58+
59+
Example
60+
-------
61+
4362
```html
4463
<html>
4564
<body>
65+
<div><span id="progress"></span>% complete...</div>
4666
<!-- These two libraries are highly recommended for encoding password/salt -->
4767
<script src="libs/buffer.js" type="text/javascript"></script>
48-
<script src="libs/unorm.js" type="text/javascript"></script>
4968

5069
<!-- This shim library greatly improves performance of the scrypt algorithm -->
5170
<script src="libs/setImmediate.js" type="text/javascript"></script>
@@ -54,24 +73,26 @@ API
5473
<script type="text/javascript">
5574
5675
// See the section below: "Encoding Notes"
57-
var password = new buffer.SlowBuffer("anyPassword".normalize('NFKC'));
58-
var salt = new buffer.SlowBuffer("someSalt".normalize('NFKC'));
76+
const password = new buffer.SlowBuffer("anyPassword".normalize('NFKC'));
77+
const salt = new buffer.SlowBuffer("someSalt".normalize('NFKC'));
5978
60-
var N = 1024, r = 8, p = 1;
61-
var dkLen = 32;
79+
const N = 1024, r = 8, p = 1;
80+
const dkLen = 32;
6281
63-
scrypt(password, salt, N, r, p, dkLen, function(error, progress, key) {
64-
if (error) {
65-
console.log("Error: " + error);
82+
function updateInterface(progress) {
83+
document.getElementById("progress").textContent = Math.trunc(100 * progress);
84+
}
6685
67-
} else if (key) {
68-
console.log("Found: " + key);
86+
// Async
87+
const keyPromise = scrypt.scrypt(password, salt, N, r, p, dkLen, updateInterface);
6988
70-
} else {
71-
// update UI with progress complete
72-
updateInterface(progress);
73-
}
89+
keyPromise.then(function(key) {
90+
console.log("Derived Key (async): ", key);
7491
});
92+
93+
// Sync
94+
const key = scrypt.syncScrypt(password, salt, N, r, p, dkLen);
95+
console.log("Derived Key (sync): ", key);
7596
</script>
7697
</body>
7798
</html>
@@ -131,14 +152,22 @@ true
131152

132153
**Normalizing**
133154

134-
The `normalize()` method of a string can be used to convert a string to a specific form. Without going into too much detail, I generally recommend `NFKC`, however if you wish to dive deeper into this, a nice short summary can be found in Pythons [unicodedata module](https://docs.python.org/2/library/unicodedata.html#unicodedata.normalize)'s documentation.
155+
The `normalize()` method of a string can be used to convert a string to a
156+
specific form. Without going into too much detail, I generally recommend
157+
`NFKC`, however if you wish to dive deeper into this, a nice short summary
158+
can be found in Pythons [unicodedata module](https://docs.python.org/2/library/unicodedata.html#unicodedata.normalize)'s
159+
documentation.
135160

136-
For browsers without `normalize()` support, the [npm unorm module](https://www.npmjs.com/package/unorm) can be used to polyfill strings.
161+
For browsers without `normalize()` support, the [npm unorm module](https://www.npmjs.com/package/unorm)
162+
can be used to polyfill strings.
137163

138164

139165
**Another example of encoding woes**
140166

141-
One quick story I will share is a project which used the `SHA256(encodeURI(password))` as a key, which (ignoring [rainbow table attacks](https://en.wikipedia.org/wiki/Rainbow_table)) had an unfortunate consequence of old web browsers replacing spaces with `+` while on new web browsers, replacing it with a `%20`, causing issues for anyone who used spaces in their password.
167+
One quick story I will share is a project which used the `SHA256(encodeURI(password))` as
168+
a key, which (ignoring [rainbow table attacks](https://en.wikipedia.org/wiki/Rainbow_table))
169+
had an unfortunate consequence of old web browsers replacing spaces with `+` while on new web
170+
browsers, replacing it with a `%20`, causing issues for anyone who used spaces in their password.
142171

143172

144173
### Suggestions
@@ -161,7 +190,8 @@ npm test
161190
Special Thanks
162191
--------------
163192

164-
I would like to thank @dchest for his [scrypt-async](https://github.com/dchest/scrypt-async-js) library and for his assistance providing feedback and optimization suggestions.
193+
I would like to thank @dchest for his [scrypt-async](https://github.com/dchest/scrypt-async-js)
194+
library and for his assistance providing feedback and optimization suggestions.
165195

166196

167197
License
@@ -183,8 +213,7 @@ References
183213
Donations
184214
---------
185215

186-
Obviously, it's all licensed under the MIT license, so use it as you wish; but if you'd like to buy me a coffee, I won't complain. =)
216+
Obviously, it's all licensed under the MIT license, so use it as you wish;
217+
but if you'd like to buy me a coffee, I won't complain. =)
187218

188-
- Bitcoin - `1LsxZkCZpQXyiGsoAnAW9nRRfck3Nvv7QS`
189-
- Dogecoin - `DF1VMTgyPsew619hwq5tT2RP8BNh2ZpzWA`
190-
- Testnet3 - `muf7Vak4ZCVgtYZCnGStDXuoEdmZuo2nhA`
219+
- Ethereum - `ricmoo.eth`

0 commit comments

Comments
 (0)