From 1e15e7299b0fc547b3c5657e342f4cdd8e1bb491 Mon Sep 17 00:00:00 2001 From: Glenn Rempe Date: Sat, 24 Jan 2015 12:04:16 -0800 Subject: [PATCH 1/6] Update README.md with more complete build and test instructions. --- README.md | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6bd9c91..0e16043 100644 --- a/README.md +++ b/README.md @@ -63,10 +63,24 @@ Pass `data` and `keys` when you `decrypt` a file: __Digging In__ +`brew install node` to install NodeJS and the `npm` package manager. + `git clone https://github.com/45678/miniLockLib.git` to get the source code. +`cd miniLockLib` + +`npm install -g browserify` which is needed to run `make`. + +`npm install -g docco` which is needed to run `make`. + +`npm install` to install local packages needed to run `make` tasks. + `make` to compile [CoffeeScript](http://www.coffeescript.org/) files into [ECMAScript](http://www.ecmascript.org/) files in the `library.compiled`, `tests.compiled` and `website` folders. -`make clean` to start over. +`make clean` to remove all generated files and start over. + +`curl get.pow.cx | sh` to install the [Pow](http://pow.cx/) (OS X Only) local web application server. + +`make pow` to link the miniLockLib website into Pow for testing. -`npm run tests` to open the test suite in a web agent `window`. +`npm run test` to open the test suite in a web agent `window` using the Pow web server to host a local copy of the miniLockLib website and tests. From 3d06d2f7eee38317525fee5fb25e1cdb95afd778 Mon Sep 17 00:00:00 2001 From: Glenn Rempe Date: Thu, 4 Feb 2016 17:11:54 -0800 Subject: [PATCH 2/6] Replace Pow server with Caddy for testing. Pow is no longer working on modern OS X --- Caddyfile | 4 ++++ Makefile | 9 --------- README.md | 12 ++++++++---- package.json | 2 +- 4 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 Caddyfile diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 0000000..e6eab63 --- /dev/null +++ b/Caddyfile @@ -0,0 +1,4 @@ +localhost:8888 +tls off +root ./website + diff --git a/Makefile b/Makefile index 3a317f1..a4fc55b 100644 --- a/Makefile +++ b/Makefile @@ -52,15 +52,6 @@ clean: rm -f website/miniLockLib.js website/tests.js website/annotated_code.js website/index.js rm -rf website/annotated_code -# Establish a link with [Pow](http://pow.cx/) to serve the `website` folder at `http://minilocklib.dev/`. -pow: - mkdir -p ~/.pow/minilocklib - ln -s $(PWD)/website ~/.pow/minilocklib/public - -# Removes files added to your `~/.pow` folder by `make pow`. -unlink_pow: - rm -rf ~/.pow/minilocklib - gh-pages: website/miniLockLib.js website/tests.js website/index.js website/annotated_code.js website/annotated_code git checkout master rm -rf gh-pages diff --git a/README.md b/README.md index 0e16043..43ffa9c 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,9 @@ __Digging In__ `brew install node` to install NodeJS and the `npm` package manager. +`brew install caddy` to install [Caddy web server](https://caddyserver.com) for +simple testing. + `git clone https://github.com/45678/miniLockLib.git` to get the source code. `cd miniLockLib` @@ -79,8 +82,9 @@ __Digging In__ `make clean` to remove all generated files and start over. -`curl get.pow.cx | sh` to install the [Pow](http://pow.cx/) (OS X Only) local web application server. - -`make pow` to link the miniLockLib website into Pow for testing. +Open a new terminal, and `cd miniLockLib` and start Caddy webserver by +running `caddy` which will start server on port `8888`. -`npm run test` to open the test suite in a web agent `window` using the Pow web server to host a local copy of the miniLockLib website and tests. +`npm run test` in the original terminal to run both the NodeJS and Browser test +suites. A new browser window, using your default browser, will be opened and the +NodeJS tests will run in the console. diff --git a/package.json b/package.json index 834b45a..9459d46 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "scripts": { "start": "make; wr 'make' library/*.coffee tests/*.coffee website/*.coffee website/*.jst", "test": "npm run window.tests; npm run node.tests", - "window.tests": "make; open http://minilocklib.dev/tests.html", + "window.tests": "make; open http://localhost:8888/tests.html", "node.tests": "make; tape tests.compiled/*Tests.js" } } From 04a11c0797463c81a8c2096af3ffb428281b5234 Mon Sep 17 00:00:00 2001 From: Glenn Rempe Date: Thu, 4 Feb 2016 17:14:58 -0800 Subject: [PATCH 3/6] Upgrade npm [browserify, coffee-script, tape, tweetnacl] to current versions --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 9459d46..c3e3d56 100644 --- a/package.json +++ b/package.json @@ -9,12 +9,12 @@ "blake2s-js": "1.0.3", "entropizer": "^0.1.3", "nacl-stream": "0.3.3", - "tweetnacl": "0.12.x" + "tweetnacl": "0.13.x" }, "devDependencies": { - "browserify": "5.9.x", - "coffee-script": "1.8.x", - "tape": "2.14.x", + "browserify": "13.0.x", + "coffee-script": "1.10.x", + "tape": "4.4.x", "wr": "1.3.x" }, "scripts": { From aace259edf140675b7c80f738fe30d6ec5a0825c Mon Sep 17 00:00:00 2001 From: Glenn Rempe Date: Thu, 4 Feb 2016 17:23:50 -0800 Subject: [PATCH 4/6] Fix FileReader bug when running tests when node sniff-test fails Not all current NodeJS installs return "node" when checking `process.argv[0]`. On my system it returns: $ node -p -e 'process.argv[0]' /Users/glenn/.nvm/versions/node/v4.2.2/bin/node Do the sniff-test of whether we are running tests in a browser or in node a different way. An example of previous fail was: ``` /Users/glenn/src/miniLockLib/library.compiled/readSliceOfData.js:17 this.fileReader = new FileReader; ^ ReferenceError: FileReader is not defined at EncryptOperation.module.exports [as readSliceOfData] (/Users/glenn/src/miniLockLib/library.compiled/readSliceOfData.js:17:31) at EncryptOperation.module.exports.EncryptOperation.encryptData (/Users/glenn/src/miniLockLib/library.compiled/EncryptOperation.js:161:19) at EncryptOperation.module.exports.EncryptOperation.run (/Users/glenn/src/miniLockLib/library.compiled/EncryptOperation.js:85:19) at EncryptOperation.module.exports.EncryptOperation.start (/Users/glenn/src/miniLockLib/library.compiled/EncryptOperation.js:78:16) at EncryptOperation.start (/Users/glenn/src/miniLockLib/library.compiled/EncryptOperation.js:4:59) at Object.miniLockLib.encrypt (/Users/glenn/src/miniLockLib/library.compiled/index.js:19:22) at /Users/glenn/src/miniLockLib/tests.compiled/A Few Demo Tests.js:15:26 at /Users/glenn/src/miniLockLib/tests.compiled/fixtures.js:50:14 at Object.read.files.basic.txt (/Users/glenn/src/miniLockLib/tests.compiled/fixtures.js:56:14) at exports.read (/Users/glenn/src/miniLockLib/tests.compiled/fixtures.js:46:28) ``` --- library.compiled/readSliceOfData.js | 4 +--- library/readSliceOfData.coffee | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/library.compiled/readSliceOfData.js b/library.compiled/readSliceOfData.js index 4a43933..43b9551 100644 --- a/library.compiled/readSliceOfData.js +++ b/library.compiled/readSliceOfData.js @@ -1,8 +1,6 @@ // Generated by CoffeeScript 1.8.0 (function() { - var _ref; - - if ((typeof process !== "undefined" && process !== null ? (_ref = process.argv) != null ? _ref[0] : void 0 : void 0) === "node") { + if (typeof window === "undefined") { module.exports = function(start, end, callback) { var blob, sliceOfBytes; blob = this.data.slice(start, end); diff --git a/library/readSliceOfData.coffee b/library/readSliceOfData.coffee index 91d2bc7..fb0ff5b 100644 --- a/library/readSliceOfData.coffee +++ b/library/readSliceOfData.coffee @@ -1,4 +1,4 @@ -if process?.argv?[0] is "node" +if typeof window is "undefined" module.exports = (start, end, callback) -> blob = @data.slice(start, end) sliceOfBytes = new Uint8Array blob.buffer From 5dc20385377a0078ba6da8076b88bdb3e86bb491 Mon Sep 17 00:00:00 2001 From: Glenn Rempe Date: Thu, 4 Feb 2016 17:27:58 -0800 Subject: [PATCH 5/6] Re-generate library and test .js files with CoffeeScript 1.10.0 --- library.compiled/BLAKE2s.js | 10 +-- library.compiled/Blob.js | 8 +- library.compiled/DecryptOperation.js | 72 ++++++++--------- library.compiled/EmailAddress.js | 2 +- library.compiled/EncryptOperation.js | 34 ++++---- library.compiled/ID.js | 6 +- library.compiled/KeyPairOperation.js | 2 +- library.compiled/SecretPhrase.js | 2 +- library.compiled/index.js | 2 +- library.compiled/readSliceOfData.js | 2 +- library.compiled/scrypt-async.js | 87 +++++++++++++------- library.compiled/util.js | 10 +-- tests.compiled/A Few Demo Tests.js | 8 +- tests.compiled/Acceptability Tests.js | 8 +- tests.compiled/Decrypt Operation Tests.js | 8 +- tests.compiled/Encrypt Operation Tests.js | 96 +++++++++++------------ tests.compiled/Identification Tests.js | 8 +- tests.compiled/Make Key Pair Tests.js | 8 +- tests.compiled/Slow Operation Tests.js | 8 +- tests.compiled/fixtures.js | 2 +- tests.compiled/test_setup.js | 6 +- tests.compiled/window_test_harness.js | 10 +-- 22 files changed, 215 insertions(+), 184 deletions(-) diff --git a/library.compiled/BLAKE2s.js b/library.compiled/BLAKE2s.js index bb77e11..fc1ba02 100644 --- a/library.compiled/BLAKE2s.js +++ b/library.compiled/BLAKE2s.js @@ -1,13 +1,13 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var BLAKE2s, OriginalBLAKE2s, - __hasProp = {}.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty; OriginalBLAKE2s = require("blake2s-js"); - module.exports = BLAKE2s = (function(_super) { - __extends(BLAKE2s, _super); + module.exports = BLAKE2s = (function(superClass) { + extend(BLAKE2s, superClass); function BLAKE2s(params) { OriginalBLAKE2s.call(this, params.length); diff --git a/library.compiled/Blob.js b/library.compiled/Blob.js index d3c2c5f..3233548 100644 --- a/library.compiled/Blob.js +++ b/library.compiled/Blob.js @@ -1,10 +1,10 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var Blob; module.exports = Blob = (function() { function Blob(input, options) { - var part, _i, _len; + var i, len, part; if (options == null) { options = {}; } @@ -20,8 +20,8 @@ break; case Array: this.buffer = new Buffer(0); - for (_i = 0, _len = input.length; _i < _len; _i++) { - part = input[_i]; + for (i = 0, len = input.length; i < len; i++) { + part = input[i]; this.buffer = Buffer.concat([this.buffer, new Buffer(part)]); } } diff --git a/library.compiled/DecryptOperation.js b/library.compiled/DecryptOperation.js index 0bd2735..90fdad9 100644 --- a/library.compiled/DecryptOperation.js +++ b/library.compiled/DecryptOperation.js @@ -1,16 +1,16 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var DecryptOperation, - __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; module.exports = DecryptOperation = (function() { - var Blob, ID, NaCl, byteArrayToNumber, decodeBase64, encodeUTF8, _ref; + var Blob, ID, NaCl, byteArrayToNumber, decodeBase64, encodeUTF8, ref; NaCl = require("tweetnacl"); NaCl.stream = require("nacl-stream").stream; - _ref = NaCl.util, encodeUTF8 = _ref.encodeUTF8, decodeBase64 = _ref.decodeBase64; + ref = NaCl.util, encodeUTF8 = ref.encodeUTF8, decodeBase64 = ref.decodeBase64; ID = require("./ID"); @@ -26,7 +26,7 @@ if (params == null) { params = {}; } - this.start = __bind(this.start, this); + this.start = bind(this.start, this); this.data = params.data, this.keys = params.keys, this.callback = params.callback; this.decryptedBytes = []; if (params.start != null) { @@ -35,18 +35,18 @@ } DecryptOperation.prototype.start = function(callback) { - var _ref1, _ref2; + var ref1, ref2; if (callback != null) { this.callback = callback; } - if (((_ref1 = this.callback) != null ? _ref1.constructor : void 0) !== Function) { + if (((ref1 = this.callback) != null ? ref1.constructor : void 0) !== Function) { throw "Can’t start decrypt operation without a callback function."; } switch (false) { case this.data !== void 0: this.callback("Can’t decrypt without a Blob of data."); break; - case ((_ref2 = this.keys) != null ? _ref2.secretKey : void 0) !== void 0: + case ((ref2 = this.keys) != null ? ref2.secretKey : void 0) !== void 0: this.callback("Can’t decrypt without a set of keys."); break; default: @@ -127,15 +127,15 @@ } if (decryptedBytes = _this.streamDecryptor.decryptChunk(sliceOfBytes, false)) { nameAsBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedBytes.length; _i < _len; _i++) { - byte = decryptedBytes[_i]; + var i, len, results; + results = []; + for (i = 0, len = decryptedBytes.length; i < len; i++) { + byte = decryptedBytes[i]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); attributes = { name: encodeUTF8(nameAsBytes) @@ -172,39 +172,39 @@ if (decryptedBytes = _this.streamDecryptor.decryptChunk(sliceOfBytes, false)) { decryptedNameBytes = decryptedBytes.subarray(0, 256); nameAsBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedNameBytes.length; _i < _len; _i++) { - byte = decryptedNameBytes[_i]; + var i, len, results; + results = []; + for (i = 0, len = decryptedNameBytes.length; i < len; i++) { + byte = decryptedNameBytes[i]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); decryptedTypeBytes = decryptedBytes.subarray(256, 256 + 128); typeAsBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedTypeBytes.length; _i < _len; _i++) { - byte = decryptedTypeBytes[_i]; + var i, len, results; + results = []; + for (i = 0, len = decryptedTypeBytes.length; i < len; i++) { + byte = decryptedTypeBytes[i]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); decryptedTimeBytes = decryptedBytes.subarray(256 + 128, 256 + 128 + 24); timeAsBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedTimeBytes.length; _i < _len; _i++) { - byte = decryptedTimeBytes[_i]; + var i, len, results; + results = []; + for (i = 0, len = decryptedTimeBytes.length; i < len; i++) { + byte = decryptedTimeBytes[i]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); attributes = { name: encodeUTF8(nameAsBytes), @@ -321,11 +321,11 @@ }; DecryptOperation.prototype.findUniqueNonceAndPermit = function(header) { - var decodedEncryptedPermit, encodedEncryptedPermit, encodedUniqueNonce, ephemeral, permit, uniqueNonce, _ref1; + var decodedEncryptedPermit, encodedEncryptedPermit, encodedUniqueNonce, ephemeral, permit, ref1, uniqueNonce; ephemeral = decodeBase64(header.ephemeral); - _ref1 = header.decryptInfo; - for (encodedUniqueNonce in _ref1) { - encodedEncryptedPermit = _ref1[encodedUniqueNonce]; + ref1 = header.decryptInfo; + for (encodedUniqueNonce in ref1) { + encodedEncryptedPermit = ref1[encodedUniqueNonce]; uniqueNonce = decodeBase64(encodedUniqueNonce); decodedEncryptedPermit = decodeBase64(encodedEncryptedPermit); permit = this.decryptPermit(decodedEncryptedPermit, uniqueNonce, ephemeral); diff --git a/library.compiled/EmailAddress.js b/library.compiled/EmailAddress.js index a41a8fb..52bcb2a 100644 --- a/library.compiled/EmailAddress.js +++ b/library.compiled/EmailAddress.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var EmailAddressPattern; diff --git a/library.compiled/EncryptOperation.js b/library.compiled/EncryptOperation.js index b174ba7..7d68995 100644 --- a/library.compiled/EncryptOperation.js +++ b/library.compiled/EncryptOperation.js @@ -1,8 +1,8 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var EncryptOperation, - __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, - __slice = [].slice; + bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + slice = [].slice; module.exports = EncryptOperation = (function() { var BLAKE2s, Blob, ID, NaCl, numberToByteArray; @@ -27,8 +27,8 @@ if (params == null) { params = {}; } - this.end = __bind(this.end, this); - this.start = __bind(this.start, this); + this.end = bind(this.end, this); + this.start = bind(this.start, this); this.data = params.data, this.keys = params.keys, this.name = params.name, this.type = params.type, this.time = params.time, this.miniLockIDs = params.miniLockIDs, this.version = params.version, this.callback = params.callback; if (this.version === void 0) { this.version = 1; @@ -44,18 +44,18 @@ } EncryptOperation.prototype.start = function(callback) { - var _ref, _ref1, _ref2, _ref3; + var ref, ref1, ref2, ref3; if (callback != null) { this.callback = callback; } - if (((_ref = this.callback) != null ? _ref.constructor : void 0) !== Function) { + if (((ref = this.callback) != null ? ref.constructor : void 0) !== Function) { throw "Can’t start encrypt operation without callback function."; } switch (false) { case (this.data instanceof Blob) !== false: this.callback("Can’t encrypt without a Blob of data."); break; - case !((((_ref1 = this.keys) != null ? _ref1.publicKey : void 0) === void 0) || (((_ref2 = this.keys) != null ? _ref2.secretKey : void 0) === void 0)): + case !((((ref1 = this.keys) != null ? ref1.publicKey : void 0) === void 0) || (((ref2 = this.keys) != null ? ref2.secretKey : void 0) === void 0)): this.callback("Can’t encrypt without a set of keys."); break; case (this.miniLockIDs instanceof Array) !== false: @@ -67,7 +67,7 @@ case !(this.type && this.type.length > 128): this.callback("Can’t encrypt because media type is too long. 128-characters max please."); break; - case ((_ref3 = this.version) === 1 || _ref3 === 2) !== false: + case ((ref3 = this.version) === 1 || ref3 === 2) !== false: this.callback("Can’t encrypt because version " + this.version + " is not supported. Version 1 or 2 please."); break; default: @@ -87,7 +87,7 @@ var fileFormat; if (dataWasEncrypted != null) { _this.constructHeader(); - fileFormat = ["miniLock", _this.sizeOfHeaderIn4Bytes, _this.headerJSONBytes].concat(__slice.call(_this.ciphertextBytes)); + fileFormat = ["miniLock", _this.sizeOfHeaderIn4Bytes, _this.headerJSONBytes].concat(slice.call(_this.ciphertextBytes)); return _this.end(error, new Blob(fileFormat, { type: "application/minilock" })); @@ -235,12 +235,12 @@ }; EncryptOperation.prototype.encodedEncryptedPermits = function() { - var encodedEncryptedPermit, encodedUniqueNonce, encryptedPermit, miniLockID, permits, uniqueNonce, _i, _len, _ref, _ref1; + var encodedEncryptedPermit, encodedUniqueNonce, encryptedPermit, i, len, miniLockID, permits, ref, ref1, uniqueNonce; permits = {}; - _ref = this.miniLockIDs; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - miniLockID = _ref[_i]; - _ref1 = this.encryptedPermit(miniLockID), uniqueNonce = _ref1[0], encryptedPermit = _ref1[1]; + ref = this.miniLockIDs; + for (i = 0, len = ref.length; i < len; i++) { + miniLockID = ref[i]; + ref1 = this.encryptedPermit(miniLockID), uniqueNonce = ref1[0], encryptedPermit = ref1[1]; encodedUniqueNonce = NaCl.util.encodeBase64(uniqueNonce); encodedEncryptedPermit = NaCl.util.encodeBase64(encryptedPermit); permits[encodedUniqueNonce] = encodedEncryptedPermit; @@ -249,8 +249,8 @@ }; EncryptOperation.prototype.encryptedPermit = function(miniLockID) { - var decodedPermitJSON, encryptedPermit, permit, recipientPublicKey, uniqueNonce, _ref; - _ref = this.permit(miniLockID), uniqueNonce = _ref[0], permit = _ref[1]; + var decodedPermitJSON, encryptedPermit, permit, recipientPublicKey, ref, uniqueNonce; + ref = this.permit(miniLockID), uniqueNonce = ref[0], permit = ref[1]; decodedPermitJSON = NaCl.util.decodeUTF8(JSON.stringify(permit)); recipientPublicKey = ID.decode(miniLockID); encryptedPermit = NaCl.box(decodedPermitJSON, uniqueNonce, recipientPublicKey, this.ephemeral.secretKey); diff --git a/library.compiled/ID.js b/library.compiled/ID.js index ab20a9a..b8e014f 100644 --- a/library.compiled/ID.js +++ b/library.compiled/ID.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var BLAKE2s, Base58; @@ -7,10 +7,10 @@ BLAKE2s = require("./BLAKE2s"); exports.encode = function(publicKey) { - var index, slots, _i; + var i, index, slots; if ((publicKey != null ? publicKey.length : void 0) === 32) { slots = new Uint8Array(33); - for (index = _i = 0; _i <= 32; index = ++_i) { + for (index = i = 0; i <= 32; index = ++i) { slots[index] = publicKey[index]; } slots[32] = (new BLAKE2s({ diff --git a/library.compiled/KeyPairOperation.js b/library.compiled/KeyPairOperation.js index c078878..cba6b7c 100644 --- a/library.compiled/KeyPairOperation.js +++ b/library.compiled/KeyPairOperation.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var KeyPairOperation; diff --git a/library.compiled/SecretPhrase.js b/library.compiled/SecretPhrase.js index d6c8113..75a0e54 100644 --- a/library.compiled/SecretPhrase.js +++ b/library.compiled/SecretPhrase.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var Entropizer, entropizer; diff --git a/library.compiled/index.js b/library.compiled/index.js index 14853ed..aa0de99 100644 --- a/library.compiled/index.js +++ b/library.compiled/index.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var miniLockLib; diff --git a/library.compiled/readSliceOfData.js b/library.compiled/readSliceOfData.js index 43b9551..86bf78e 100644 --- a/library.compiled/readSliceOfData.js +++ b/library.compiled/readSliceOfData.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { if (typeof window === "undefined") { module.exports = function(start, end, callback) { diff --git a/library.compiled/scrypt-async.js b/library.compiled/scrypt-async.js index d6ba4a6..c8bd776 100644 --- a/library.compiled/scrypt-async.js +++ b/library.compiled/scrypt-async.js @@ -1,6 +1,6 @@ /*! * Fast "async" scrypt implementation in JavaScript. - * Copyright (c) 2013-2014 Dmitry Chestnykh | BSD License + * Copyright (c) 2013-2015 Dmitry Chestnykh | BSD License * https://github.com/dchest/scrypt-async-js */ @@ -9,19 +9,25 @@ */ /** - * scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encoding) + * scrypt(password, salt, logN, r, dkLen, [interruptStep], callback, [encoding]) * * Derives a key from password and salt and calls callback * with derived key as the only argument. * + * Calculations are interrupted with setImmediate (or zero setTimeout) at the + * given interruptSteps to avoid freezing the browser. If interruptStep is not + * given, it defaults to 1000. If it's zero, the callback is called immediately + * after the calculation, avoiding setImmediate. + * * @param {string|Array.} password Password. * @param {string|Array.} salt Salt. - * @param {number} logN CPU/memory cost parameter (1 to 31). - * @param {number} r Block size parameter. - * @param {number} dkLen Length of derived key. - * @param {number} interruptStep Steps to split calculation with timeouts (default 1000). - * @param {function(string)} callback Callback function. - * @param {string?} encoding Result encoding ("base64", "hex", or null). + * @param {number} logN CPU/memory cost parameter (1 to 31). + * @param {number} r Block size parameter. + * @param {number} dkLen Length of derived key. + * @param {number?} interruptStep (optional) Steps to split calculation with timeouts (default 1000). + * @param {function(string|Array.)} callback Callback function. + * @param {string?} encoding (optional) Result encoding ("base64", "hex", or null). + * */ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encoding) { 'use strict'; @@ -290,7 +296,7 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin } else { arr.push((c>>12) | 224); arr.push(((c>>6) & 63) | 128); - arr.push((c & 64) | 128); + arr.push((c & 63) | 128); } } return arr; @@ -333,7 +339,7 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin } if (len % 3 > 0) { arr[arr.length-1] = '='; - if (len % 3 == 1) arr[arr.length-2] = '='; + if (len % 3 === 1) arr[arr.length-2] = '='; } return arr.join(''); } @@ -355,9 +361,9 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin throw new Error('scrypt: parameters are too large'); // Decode strings. - if (typeof password == 'string') + if (typeof password === 'string') password = stringToUTF8Bytes(password); - if (typeof salt == 'string') + if (typeof salt === 'string') salt = stringToUTF8Bytes(salt); if (typeof Int32Array !== 'undefined') { @@ -414,36 +420,61 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin } } + var nextTick = (typeof setImmediate !== 'undefined') ? setImmediate : setTimeout; + function interruptedFor(start, end, step, fn, donefn) { (function performStep() { - setTimeout(function() { + nextTick(function() { fn(start, start + step < end ? start + step : end); start += step; if (start < end) performStep(); else donefn(); - }, 0); + }); })(); } - // Note: step argument for interruptedFor must be divisible by - // two, since smixStepX work in increments of 2. - if (!interruptStep) interruptStep = 1000; - - smixStart(); - interruptedFor(0, N, interruptStep*2, smixStep1, function() { - interruptedFor(0, N, interruptStep*2, smixStep2, function () { - smixFinish(); + function getResult(enc) { var result = PBKDF2_HMAC_SHA256_OneIter(password, B, dkLen); - if (encoding == "base64") - callback(bytesToBase64(result)); - else if (encoding == "hex") - callback(bytesToHex(result)); + if (enc === 'base64') + return bytesToBase64(result); + else if (enc === 'hex') + return bytesToHex(result); else - callback(result); + return result; + } + + if (typeof interruptStep === 'function') { + // Called as: scrypt(..., callback, [encoding]) + // shifting: scrypt(..., interruptStep, callback, [encoding]) + encoding = callback; + callback = interruptStep; + interruptStep = 1000; + } + + if (interruptStep <= 0) { + // + // Blocking async variant, calls callback. + // + smixStart(); + smixStep1(0, N); + smixStep2(0, N); + smixFinish(); + callback(getResult(encoding)); + + } else { + // + // Async variant with interruptions, calls callback. + // + smixStart(); + interruptedFor(0, N, interruptStep*2, smixStep1, function() { + interruptedFor(0, N, interruptStep*2, smixStep2, function () { + smixFinish(); + callback(getResult(encoding)); + }); }); - }); + } } if (typeof module !== 'undefined') module.exports = scrypt; diff --git a/library.compiled/util.js b/library.compiled/util.js index d30ebbe..9c42e46 100644 --- a/library.compiled/util.js +++ b/library.compiled/util.js @@ -1,9 +1,9 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { exports.numberToByteArray = function(n) { - var byteArray, index, _i; + var byteArray, i, index; byteArray = new Uint8Array(4); - for (index = _i = 0; _i <= 4; index = ++_i) { + for (index = i = 0; i <= 4; index = ++i) { byteArray[index] = n & 255; n = n >> 8; } @@ -11,9 +11,9 @@ }; exports.byteArrayToNumber = function(byteArray) { - var index, n, _i; + var i, index, n; n = 0; - for (index = _i = 3; _i >= 0; index = --_i) { + for (index = i = 3; i >= 0; index = --i) { n += byteArray[index]; if (index !== 0) { n = n << 8; diff --git a/tests.compiled/A Few Demo Tests.js b/tests.compiled/A Few Demo Tests.js index 6cb7b9e..d9b1627 100644 --- a/tests.compiled/A Few Demo Tests.js +++ b/tests.compiled/A Few Demo Tests.js @@ -1,10 +1,10 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var Alice, Bobby, miniLockLib, read, tape, _ref, _ref1; + var Alice, Bobby, miniLockLib, read, ref, ref1, tape; - _ref = require("./test_setup"), tape = _ref.tape, miniLockLib = _ref.miniLockLib; + ref = require("./test_setup"), tape = ref.tape, miniLockLib = ref.miniLockLib; - _ref1 = require("./fixtures"), Alice = _ref1.Alice, Bobby = _ref1.Bobby, read = _ref1.read; + ref1 = require("./fixtures"), Alice = ref1.Alice, Bobby = ref1.Bobby, read = ref1.read; tape("A demo of miniLockLib.encrypt & miniLockLib.decrypt", function(test) { return test.end(); diff --git a/tests.compiled/Acceptability Tests.js b/tests.compiled/Acceptability Tests.js index 4ebadbc..6f3b5aa 100644 --- a/tests.compiled/Acceptability Tests.js +++ b/tests.compiled/Acceptability Tests.js @@ -1,10 +1,10 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var Alice, Bobby, miniLockLib, tape, _ref, _ref1; + var Alice, Bobby, miniLockLib, ref, ref1, tape; - _ref = require("./test_setup"), tape = _ref.tape, miniLockLib = _ref.miniLockLib; + ref = require("./test_setup"), tape = ref.tape, miniLockLib = ref.miniLockLib; - _ref1 = require("./fixtures"), Alice = _ref1.Alice, Bobby = _ref1.Bobby; + ref1 = require("./fixtures"), Alice = ref1.Alice, Bobby = ref1.Bobby; tape("Acceptability", function(test) { return test.end(); diff --git a/tests.compiled/Decrypt Operation Tests.js b/tests.compiled/Decrypt Operation Tests.js index d2d9065..b55cf8b 100644 --- a/tests.compiled/Decrypt Operation Tests.js +++ b/tests.compiled/Decrypt Operation Tests.js @@ -1,12 +1,12 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var Alice, Blob, Bobby, miniLockLib, read, readFromNetwork, tape, _ref, _ref1; + var Alice, Blob, Bobby, miniLockLib, read, readFromNetwork, ref, ref1, tape; - _ref = require("./test_setup"), tape = _ref.tape, miniLockLib = _ref.miniLockLib; + ref = require("./test_setup"), tape = ref.tape, miniLockLib = ref.miniLockLib; Blob = (typeof window !== "undefined" && window !== null ? window.Blob : void 0) || require("../library.compiled/Blob"); - _ref1 = require("./fixtures"), Alice = _ref1.Alice, Bobby = _ref1.Bobby, read = _ref1.read, readFromNetwork = _ref1.readFromNetwork; + ref1 = require("./fixtures"), Alice = ref1.Alice, Bobby = ref1.Bobby, read = ref1.read, readFromNetwork = ref1.readFromNetwork; tape("DecryptOperation", function(test) { return test.end(); diff --git a/tests.compiled/Encrypt Operation Tests.js b/tests.compiled/Encrypt Operation Tests.js index 5eba471..c58bc38 100644 --- a/tests.compiled/Encrypt Operation Tests.js +++ b/tests.compiled/Encrypt Operation Tests.js @@ -1,12 +1,12 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var Alice, Blob, Bobby, miniLockLib, read, readFromNetwork, tape, _ref, _ref1; + var Alice, Blob, Bobby, miniLockLib, read, readFromNetwork, ref, ref1, tape; - _ref = require("./test_setup"), tape = _ref.tape, miniLockLib = _ref.miniLockLib; + ref = require("./test_setup"), tape = ref.tape, miniLockLib = ref.miniLockLib; Blob = (typeof window !== "undefined" && window !== null ? window.Blob : void 0) || require("../library.compiled/Blob"); - _ref1 = require("./fixtures"), Alice = _ref1.Alice, Bobby = _ref1.Bobby, read = _ref1.read, readFromNetwork = _ref1.readFromNetwork; + ref1 = require("./fixtures"), Alice = ref1.Alice, Bobby = ref1.Bobby, read = ref1.read, readFromNetwork = ref1.readFromNetwork; tape("EncryptOperation", function(test) { return test.end(); @@ -116,12 +116,12 @@ keys: Alice.keys, miniLockIDs: [], name: ((function() { - var _i, _results; - _results = []; - for (i = _i = 0; _i < 257; i = ++_i) { - _results.push("X"); + var j, results; + results = []; + for (i = j = 0; j < 257; i = ++j) { + results.push("X"); } - return _results; + return results; })()).join("") }); return operation.start(function(error, encrypted) { @@ -138,12 +138,12 @@ keys: Alice.keys, miniLockIDs: [], type: ((function() { - var _i, _results; - _results = []; - for (i = _i = 0; _i < 129; i = ++_i) { - _results.push("X"); + var j, results; + results = []; + for (i = j = 0; j < 129; i = ++j) { + results.push("X"); } - return _results; + return results; })()).join("") }); return operation.start(function(error, encrypted) { @@ -227,15 +227,15 @@ decodedName = operation.fixedSizeDecodedName(); test.equal(decodedName.length, 256); filteredBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decodedName.length; _i < _len; _i++) { - byte = decodedName[_i]; + var j, len, results; + results = []; + for (j = 0, len = decodedName.length; j < len; j++) { + byte = decodedName[j]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); test.same(filteredBytes.length, 0); return test.end(); @@ -273,15 +273,15 @@ decryptedBytes = decryptor.decryptChunk(operation.ciphertextBytes[0], false); test.equal(decryptedBytes.length, 256); filteredBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedBytes.length; _i < _len; _i++) { - byte = decryptedBytes[_i]; + var j, len, results; + results = []; + for (j = 0, len = decryptedBytes.length; j < len; j++) { + byte = decryptedBytes[j]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); decryptedName = miniLockLib.NaCl.util.encodeUTF8(filteredBytes); test.equal(decryptedName, "untitled.txt"); @@ -303,43 +303,43 @@ test.equal(decryptedBytes.length, 256 + 128 + 24); decryptedNameBytes = decryptedBytes.subarray(0, 256); filteredNameBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedNameBytes.length; _i < _len; _i++) { - byte = decryptedNameBytes[_i]; + var j, len, results; + results = []; + for (j = 0, len = decryptedNameBytes.length; j < len; j++) { + byte = decryptedNameBytes[j]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); decryptedName = miniLockLib.NaCl.util.encodeUTF8(filteredNameBytes); test.equal(decryptedName, "untitled.txt"); decryptedTypeBytes = decryptedBytes.subarray(256, 256 + 128); filteredTypeBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedTypeBytes.length; _i < _len; _i++) { - byte = decryptedTypeBytes[_i]; + var j, len, results; + results = []; + for (j = 0, len = decryptedTypeBytes.length; j < len; j++) { + byte = decryptedTypeBytes[j]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); decryptedType = miniLockLib.NaCl.util.encodeUTF8(filteredTypeBytes); test.equal(decryptedType, "text/plain"); decryptedTimeBytes = decryptedBytes.subarray(256 + 128, 256 + 128 + 24); filteredTimeBytes = (function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = decryptedTimeBytes.length; _i < _len; _i++) { - byte = decryptedTimeBytes[_i]; + var j, len, results; + results = []; + for (j = 0, len = decryptedTimeBytes.length; j < len; j++) { + byte = decryptedTimeBytes[j]; if (byte !== 0) { - _results.push(byte); + results.push(byte); } } - return _results; + return results; })(); decryptedTime = miniLockLib.NaCl.util.encodeUTF8(filteredTimeBytes); test.equal(decryptedTime, "2014-08-17T07:06:50.095Z"); @@ -347,11 +347,11 @@ }); tape("construct a permit to decrypt for a recipient", function(test) { - var operation, permit, uniqueNonce, _ref2; + var operation, permit, ref2, uniqueNonce; operation = new miniLockLib.EncryptOperation({ keys: Alice.keys }); - _ref2 = operation.permit(Bobby.miniLockID), uniqueNonce = _ref2[0], permit = _ref2[1]; + ref2 = operation.permit(Bobby.miniLockID), uniqueNonce = ref2[0], permit = ref2[1]; test.ok(uniqueNonce.constructor === Uint8Array); test.ok(uniqueNonce.length === 24); test.ok(permit.senderID === Alice.miniLockID); @@ -362,11 +362,11 @@ }); tape("recipient can decrypt the key, nonce and hash of the file encoded in their permit", function(test) { - var decodedFileInfo, decryptedFileInfo, fileInfo, operation, permit, uniqueNonce, _ref2; + var decodedFileInfo, decryptedFileInfo, fileInfo, operation, permit, ref2, uniqueNonce; operation = new miniLockLib.EncryptOperation({ keys: Alice.keys }); - _ref2 = operation.permit(Bobby.miniLockID), uniqueNonce = _ref2[0], permit = _ref2[1]; + ref2 = operation.permit(Bobby.miniLockID), uniqueNonce = ref2[0], permit = ref2[1]; decodedFileInfo = miniLockLib.NaCl.util.decodeBase64(permit.fileInfo); decryptedFileInfo = miniLockLib.NaCl.box.open(decodedFileInfo, uniqueNonce, Alice.publicKey, Bobby.secretKey); test.ok(decryptedFileInfo); diff --git a/tests.compiled/Identification Tests.js b/tests.compiled/Identification Tests.js index 16d2fd1..27c0a0f 100644 --- a/tests.compiled/Identification Tests.js +++ b/tests.compiled/Identification Tests.js @@ -1,10 +1,10 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var Alice, Bobby, miniLockLib, read, readFromNetwork, tape, _ref, _ref1; + var Alice, Bobby, miniLockLib, read, readFromNetwork, ref, ref1, tape; - _ref = require("./test_setup"), tape = _ref.tape, miniLockLib = _ref.miniLockLib; + ref = require("./test_setup"), tape = ref.tape, miniLockLib = ref.miniLockLib; - _ref1 = require("./fixtures"), Alice = _ref1.Alice, Bobby = _ref1.Bobby, read = _ref1.read, readFromNetwork = _ref1.readFromNetwork; + ref1 = require("./fixtures"), Alice = ref1.Alice, Bobby = ref1.Bobby, read = ref1.read, readFromNetwork = ref1.readFromNetwork; tape("Identification", function(test) { return test.end(); diff --git a/tests.compiled/Make Key Pair Tests.js b/tests.compiled/Make Key Pair Tests.js index 5299f4f..12be278 100644 --- a/tests.compiled/Make Key Pair Tests.js +++ b/tests.compiled/Make Key Pair Tests.js @@ -1,10 +1,10 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var Alice, Bobby, miniLockLib, tape, _ref, _ref1; + var Alice, Bobby, miniLockLib, ref, ref1, tape; - _ref = require("./test_setup"), tape = _ref.tape, miniLockLib = _ref.miniLockLib; + ref = require("./test_setup"), tape = ref.tape, miniLockLib = ref.miniLockLib; - _ref1 = require("./fixtures"), Alice = _ref1.Alice, Bobby = _ref1.Bobby; + ref1 = require("./fixtures"), Alice = ref1.Alice, Bobby = ref1.Bobby; tape("Make Keys", function(test) { return test.end(); diff --git a/tests.compiled/Slow Operation Tests.js b/tests.compiled/Slow Operation Tests.js index d744374..304cf60 100644 --- a/tests.compiled/Slow Operation Tests.js +++ b/tests.compiled/Slow Operation Tests.js @@ -1,12 +1,12 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var Alice, Blob, Bobby, miniLockLib, read, readFromNetwork, tape, _ref, _ref1; + var Alice, Blob, Bobby, miniLockLib, read, readFromNetwork, ref, ref1, tape; - _ref = require("./test_setup"), tape = _ref.tape, miniLockLib = _ref.miniLockLib; + ref = require("./test_setup"), tape = ref.tape, miniLockLib = ref.miniLockLib; Blob = (typeof window !== "undefined" && window !== null ? window.Blob : void 0) || require("../library.compiled/Blob"); - _ref1 = require("./fixtures"), Alice = _ref1.Alice, Bobby = _ref1.Bobby, read = _ref1.read, readFromNetwork = _ref1.readFromNetwork; + ref1 = require("./fixtures"), Alice = ref1.Alice, Bobby = ref1.Bobby, read = ref1.read, readFromNetwork = ref1.readFromNetwork; tape("Slow Operations", function(test) { return test.end(); diff --git a/tests.compiled/fixtures.js b/tests.compiled/fixtures.js index aadf769..36140bf 100644 --- a/tests.compiled/fixtures.js +++ b/tests.compiled/fixtures.js @@ -1,4 +1,4 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var Alice, Base58, Blob, Bobby, miniLockLib, read; diff --git a/tests.compiled/test_setup.js b/tests.compiled/test_setup.js index 6e09ebf..495a79d 100644 --- a/tests.compiled/test_setup.js +++ b/tests.compiled/test_setup.js @@ -1,8 +1,8 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { - var _ref; + var ref; - if ((typeof root !== "undefined" && root !== null ? (_ref = root.process) != null ? _ref.argv : void 0 : void 0) != null) { + if ((typeof root !== "undefined" && root !== null ? (ref = root.process) != null ? ref.argv : void 0 : void 0) != null) { exports.tape = require("tape"); exports.miniLockLib = require(".."); } else { diff --git a/tests.compiled/window_test_harness.js b/tests.compiled/window_test_harness.js index 4d76a40..703b518 100644 --- a/tests.compiled/window_test_harness.js +++ b/tests.compiled/window_test_harness.js @@ -1,7 +1,7 @@ -// Generated by CoffeeScript 1.8.0 +// Generated by CoffeeScript 1.10.0 (function() { var assertionTemplate, failedTests, failureTemplate, findElementForTest, fixBrokenThrowsOperatorData, idOfCurrentlyRunningTest, insertFailure, insertTestAssertion, insertTestElement, numberOfFailedTests, numberOfTests, renderBodyElement, renderTestElementEnded, renderTestElementUpdate, testTemplate, untouched, - __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; + indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; module.exports = require("tape").createHarness(); @@ -33,7 +33,7 @@ case data.operator == null: fixBrokenThrowsOperatorData(data); if (data.ok === false) { - if (__indexOf.call(failedTests, idOfCurrentlyRunningTest) < 0) { + if (indexOf.call(failedTests, idOfCurrentlyRunningTest) < 0) { failedTests.push(idOfCurrentlyRunningTest); } } @@ -94,7 +94,7 @@ containerHeight = parseInt(getComputedStyle(container)['height']); bodyHeight = parseInt(getComputedStyle(document.body)['height']); if (containerHeight > bodyHeight) { - document.body.style.height = "" + containerHeight + "px"; + document.body.style.height = containerHeight + "px"; } if (untouched) { return element.scrollIntoView(); @@ -109,7 +109,7 @@ renderTestElementEnded = function(element, data) { element.className = element.className.replace("started", "ended"); - return element.querySelector("div.duration").innerText = "" + (((Date.now() - element.startedAt) / 1000).toFixed(2)) + "s"; + return element.querySelector("div.duration").innerText = (((Date.now() - element.startedAt) / 1000).toFixed(2)) + "s"; }; insertTestAssertion = function(element, data) { From d1670d31adc4575bccda52b490f4e00fccefe85a Mon Sep 17 00:00:00 2001 From: Glenn Rempe Date: Thu, 4 Feb 2016 18:38:10 -0800 Subject: [PATCH 6/6] Don't pull in async=scrypt via curl from master branch. Use NPM. --- Makefile | 7 +- library.compiled/KeyPairOperation.js | 2 +- library.compiled/index.js | 2 +- library.compiled/scrypt-async.js | 480 --------------------------- library/KeyPairOperation.coffee | 2 +- library/index.coffee | 2 +- package.json | 7 +- 7 files changed, 9 insertions(+), 493 deletions(-) delete mode 100644 library.compiled/scrypt-async.js diff --git a/Makefile b/Makefile index a4fc55b..08ed62d 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,13 @@ default: website/miniLockLib.js website/tests.js website/index.js website/annotated_code.js website/annotated_code # Create a standalone copy of miniLockLib.js in the website folder for use in web agent windows. -website/miniLockLib.js: library/%.coffee library.compiled/scrypt-async.js +website/miniLockLib.js: library/%.coffee browserify library.compiled/index.js --standalone miniLockLib > website/miniLockLib.js # Compile CoffeeScript library files to the library.compiled folder. library/%.coffee: coffee --compile --output library.compiled library/*.coffee -# Download scrypt-async.js and save it in the library.compiled folder. -library.compiled/scrypt-async.js: - curl -s https://raw.githubusercontent.com/dchest/scrypt-async-js/master/scrypt-async.js \ - > library.compiled/scrypt-async.js - # # Tests # Compile CoffeeScript tests to the tests.compiled folder. diff --git a/library.compiled/KeyPairOperation.js b/library.compiled/KeyPairOperation.js index cba6b7c..913e3ae 100644 --- a/library.compiled/KeyPairOperation.js +++ b/library.compiled/KeyPairOperation.js @@ -9,7 +9,7 @@ NaCl = require("tweetnacl"); - scrypt = require("./scrypt-async"); + scrypt = require("scrypt-async"); EmailAddress = require("./EmailAddress"); diff --git a/library.compiled/index.js b/library.compiled/index.js index aa0de99..95d1eb2 100644 --- a/library.compiled/index.js +++ b/library.compiled/index.js @@ -47,6 +47,6 @@ miniLockLib.NaCl.stream = require("nacl-stream").stream; - miniLockLib.scrypt = require("./scrypt-async"); + miniLockLib.scrypt = require("scrypt-async"); }).call(this); diff --git a/library.compiled/scrypt-async.js b/library.compiled/scrypt-async.js deleted file mode 100644 index c8bd776..0000000 --- a/library.compiled/scrypt-async.js +++ /dev/null @@ -1,480 +0,0 @@ -/*! - * Fast "async" scrypt implementation in JavaScript. - * Copyright (c) 2013-2015 Dmitry Chestnykh | BSD License - * https://github.com/dchest/scrypt-async-js - */ - -/* - * Limitation: doesn't support parallelization parameter greater than 1. - */ - -/** - * scrypt(password, salt, logN, r, dkLen, [interruptStep], callback, [encoding]) - * - * Derives a key from password and salt and calls callback - * with derived key as the only argument. - * - * Calculations are interrupted with setImmediate (or zero setTimeout) at the - * given interruptSteps to avoid freezing the browser. If interruptStep is not - * given, it defaults to 1000. If it's zero, the callback is called immediately - * after the calculation, avoiding setImmediate. - * - * @param {string|Array.} password Password. - * @param {string|Array.} salt Salt. - * @param {number} logN CPU/memory cost parameter (1 to 31). - * @param {number} r Block size parameter. - * @param {number} dkLen Length of derived key. - * @param {number?} interruptStep (optional) Steps to split calculation with timeouts (default 1000). - * @param {function(string|Array.)} callback Callback function. - * @param {string?} encoding (optional) Result encoding ("base64", "hex", or null). - * - */ -function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encoding) { - 'use strict'; - - function SHA256(m) { - /** @const */ var K = [ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, - 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, - 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, - 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, - 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, - 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, - 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, - 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, - 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, - 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 - ]; - - var h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a, - h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19, - w = new Array(64); - - function blocks(p) { - var off = 0, len = p.length; - while (len >= 64) { - var a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7, - u, i, j, t1, t2; - - for (i = 0; i < 16; i++) { - j = off + i*4; - w[i] = ((p[j] & 0xff)<<24) | ((p[j+1] & 0xff)<<16) | - ((p[j+2] & 0xff)<<8) | (p[j+3] & 0xff); - } - - for (i = 16; i < 64; i++) { - u = w[i-2]; - t1 = ((u>>>17) | (u<<(32-17))) ^ ((u>>>19) | (u<<(32-19))) ^ (u>>>10); - - u = w[i-15]; - t2 = ((u>>>7) | (u<<(32-7))) ^ ((u>>>18) | (u<<(32-18))) ^ (u>>>3); - - w[i] = (((t1 + w[i-7]) | 0) + ((t2 + w[i-16]) | 0)) | 0; - } - - for (i = 0; i < 64; i++) { - t1 = ((((((e>>>6) | (e<<(32-6))) ^ ((e>>>11) | (e<<(32-11))) ^ - ((e>>>25) | (e<<(32-25)))) + ((e & f) ^ (~e & g))) | 0) + - ((h + ((K[i] + w[i]) | 0)) | 0)) | 0; - - t2 = ((((a>>>2) | (a<<(32-2))) ^ ((a>>>13) | (a<<(32-13))) ^ - ((a>>>22) | (a<<(32-22)))) + ((a & b) ^ (a & c) ^ (b & c))) | 0; - - h = g; - g = f; - f = e; - e = (d + t1) | 0; - d = c; - c = b; - b = a; - a = (t1 + t2) | 0; - } - - h0 = (h0 + a) | 0; - h1 = (h1 + b) | 0; - h2 = (h2 + c) | 0; - h3 = (h3 + d) | 0; - h4 = (h4 + e) | 0; - h5 = (h5 + f) | 0; - h6 = (h6 + g) | 0; - h7 = (h7 + h) | 0; - - off += 64; - len -= 64; - } - } - - blocks(m); - - var i, bytesLeft = m.length % 64, - bitLenHi = (m.length / 0x20000000) | 0, - bitLenLo = m.length << 3, - numZeros = (bytesLeft < 56) ? 56 : 120, - p = m.slice(m.length - bytesLeft, m.length); - - p.push(0x80); - for (i = bytesLeft + 1; i < numZeros; i++) p.push(0); - p.push((bitLenHi>>>24) & 0xff); - p.push((bitLenHi>>>16) & 0xff); - p.push((bitLenHi>>>8) & 0xff); - p.push((bitLenHi>>>0) & 0xff); - p.push((bitLenLo>>>24) & 0xff); - p.push((bitLenLo>>>16) & 0xff); - p.push((bitLenLo>>>8) & 0xff); - p.push((bitLenLo>>>0) & 0xff); - - blocks(p); - - return [ - (h0>>>24) & 0xff, (h0>>>16) & 0xff, (h0>>>8) & 0xff, (h0>>>0) & 0xff, - (h1>>>24) & 0xff, (h1>>>16) & 0xff, (h1>>>8) & 0xff, (h1>>>0) & 0xff, - (h2>>>24) & 0xff, (h2>>>16) & 0xff, (h2>>>8) & 0xff, (h2>>>0) & 0xff, - (h3>>>24) & 0xff, (h3>>>16) & 0xff, (h3>>>8) & 0xff, (h3>>>0) & 0xff, - (h4>>>24) & 0xff, (h4>>>16) & 0xff, (h4>>>8) & 0xff, (h4>>>0) & 0xff, - (h5>>>24) & 0xff, (h5>>>16) & 0xff, (h5>>>8) & 0xff, (h5>>>0) & 0xff, - (h6>>>24) & 0xff, (h6>>>16) & 0xff, (h6>>>8) & 0xff, (h6>>>0) & 0xff, - (h7>>>24) & 0xff, (h7>>>16) & 0xff, (h7>>>8) & 0xff, (h7>>>0) & 0xff - ]; - } - - function PBKDF2_HMAC_SHA256_OneIter(password, salt, dkLen) { - // compress password if it's longer than hash block length - password = password.length <= 64 ? password : SHA256(password); - - var i, innerLen = 64 + salt.length + 4, - inner = new Array(innerLen), - outerKey = new Array(64), - dk = []; - - // inner = (password ^ ipad) || salt || counter - for (i = 0; i < 64; i++) inner[i] = 0x36; - for (i = 0; i < password.length; i++) inner[i] ^= password[i]; - for (i = 0; i < salt.length; i++) inner[64+i] = salt[i]; - for (i = innerLen - 4; i < innerLen; i++) inner[i] = 0; - - // outerKey = password ^ opad - for (i = 0; i < 64; i++) outerKey[i] = 0x5c; - for (i = 0; i < password.length; i++) outerKey[i] ^= password[i]; - - // increments counter inside inner - function incrementCounter() { - for (var i = innerLen-1; i >= innerLen-4; i--) { - inner[i]++; - if (inner[i] <= 0xff) return; - inner[i] = 0; - } - } - - // output blocks = SHA256(outerKey || SHA256(inner)) ... - while (dkLen >= 32) { - incrementCounter(); - dk = dk.concat(SHA256(outerKey.concat(SHA256(inner)))); - dkLen -= 32; - } - if (dkLen > 0) { - incrementCounter(); - dk = dk.concat(SHA256(outerKey.concat(SHA256(inner))).slice(0, dkLen)); - } - return dk; - } - - function salsaXOR(tmp, B, bin, bout) { - var j0 = tmp[0] ^ B[bin++], - j1 = tmp[1] ^ B[bin++], - j2 = tmp[2] ^ B[bin++], - j3 = tmp[3] ^ B[bin++], - j4 = tmp[4] ^ B[bin++], - j5 = tmp[5] ^ B[bin++], - j6 = tmp[6] ^ B[bin++], - j7 = tmp[7] ^ B[bin++], - j8 = tmp[8] ^ B[bin++], - j9 = tmp[9] ^ B[bin++], - j10 = tmp[10] ^ B[bin++], - j11 = tmp[11] ^ B[bin++], - j12 = tmp[12] ^ B[bin++], - j13 = tmp[13] ^ B[bin++], - j14 = tmp[14] ^ B[bin++], - j15 = tmp[15] ^ B[bin++], - u, i; - - var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7, - x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14, - x15 = j15; - - for (i = 0; i < 8; i += 2) { - u = x0 + x12; x4 ^= u<<7 | u>>>(32-7); - u = x4 + x0; x8 ^= u<<9 | u>>>(32-9); - u = x8 + x4; x12 ^= u<<13 | u>>>(32-13); - u = x12 + x8; x0 ^= u<<18 | u>>>(32-18); - - u = x5 + x1; x9 ^= u<<7 | u>>>(32-7); - u = x9 + x5; x13 ^= u<<9 | u>>>(32-9); - u = x13 + x9; x1 ^= u<<13 | u>>>(32-13); - u = x1 + x13; x5 ^= u<<18 | u>>>(32-18); - - u = x10 + x6; x14 ^= u<<7 | u>>>(32-7); - u = x14 + x10; x2 ^= u<<9 | u>>>(32-9); - u = x2 + x14; x6 ^= u<<13 | u>>>(32-13); - u = x6 + x2; x10 ^= u<<18 | u>>>(32-18); - - u = x15 + x11; x3 ^= u<<7 | u>>>(32-7); - u = x3 + x15; x7 ^= u<<9 | u>>>(32-9); - u = x7 + x3; x11 ^= u<<13 | u>>>(32-13); - u = x11 + x7; x15 ^= u<<18 | u>>>(32-18); - - u = x0 + x3; x1 ^= u<<7 | u>>>(32-7); - u = x1 + x0; x2 ^= u<<9 | u>>>(32-9); - u = x2 + x1; x3 ^= u<<13 | u>>>(32-13); - u = x3 + x2; x0 ^= u<<18 | u>>>(32-18); - - u = x5 + x4; x6 ^= u<<7 | u>>>(32-7); - u = x6 + x5; x7 ^= u<<9 | u>>>(32-9); - u = x7 + x6; x4 ^= u<<13 | u>>>(32-13); - u = x4 + x7; x5 ^= u<<18 | u>>>(32-18); - - u = x10 + x9; x11 ^= u<<7 | u>>>(32-7); - u = x11 + x10; x8 ^= u<<9 | u>>>(32-9); - u = x8 + x11; x9 ^= u<<13 | u>>>(32-13); - u = x9 + x8; x10 ^= u<<18 | u>>>(32-18); - - u = x15 + x14; x12 ^= u<<7 | u>>>(32-7); - u = x12 + x15; x13 ^= u<<9 | u>>>(32-9); - u = x13 + x12; x14 ^= u<<13 | u>>>(32-13); - u = x14 + x13; x15 ^= u<<18 | u>>>(32-18); - } - - B[bout++] = tmp[0] = (x0 + j0) | 0; - B[bout++] = tmp[1] = (x1 + j1) | 0; - B[bout++] = tmp[2] = (x2 + j2) | 0; - B[bout++] = tmp[3] = (x3 + j3) | 0; - B[bout++] = tmp[4] = (x4 + j4) | 0; - B[bout++] = tmp[5] = (x5 + j5) | 0; - B[bout++] = tmp[6] = (x6 + j6) | 0; - B[bout++] = tmp[7] = (x7 + j7) | 0; - B[bout++] = tmp[8] = (x8 + j8) | 0; - B[bout++] = tmp[9] = (x9 + j9) | 0; - B[bout++] = tmp[10] = (x10 + j10) | 0; - B[bout++] = tmp[11] = (x11 + j11) | 0; - B[bout++] = tmp[12] = (x12 + j12) | 0; - B[bout++] = tmp[13] = (x13 + j13) | 0; - B[bout++] = tmp[14] = (x14 + j14) | 0; - B[bout++] = tmp[15] = (x15 + j15) | 0; - } - - function blockCopy(dst, di, src, si, len) { - while (len--) dst[di++] = src[si++]; - } - - function blockXOR(dst, di, src, si, len) { - while (len--) dst[di++] ^= src[si++]; - } - - function blockMix(tmp, B, bin, bout, r) { - blockCopy(tmp, 0, B, bin + (2*r-1)*16, 16); - for (var i = 0; i < 2*r; i += 2) { - salsaXOR(tmp, B, bin + i*16, bout + i*8); - salsaXOR(tmp, B, bin + i*16 + 16, bout + i*8 + r*16); - } - } - - function integerify(B, bi, r) { - return B[bi+(2*r-1)*16]; - } - - function stringToUTF8Bytes(s) { - var arr = []; - for (var i = 0; i < s.length; i++) { - var c = s.charCodeAt(i); - if (c < 128) { - arr.push(c); - } else if (c > 127 && c < 2048) { - arr.push((c>>6) | 192); - arr.push((c & 63) | 128); - } else { - arr.push((c>>12) | 224); - arr.push(((c>>6) & 63) | 128); - arr.push((c & 63) | 128); - } - } - return arr; - } - - function bytesToHex(p) { - /** @const */ - var enc = '0123456789abcdef'.split(''); - - var len = p.length, - arr = [], - i = 0; - - for (; i < len; i++) { - arr.push(enc[(p[i]>>>4) & 15]); - arr.push(enc[(p[i]>>>0) & 15]); - } - return arr.join(''); - } - - function bytesToBase64(p) { - /** @const */ - var enc = ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' + - '0123456789+/').split(''); - - var len = p.length, - arr = [], - i = 0, - a, b, c, t; - - while (i < len) { - a = i < len ? p[i++] : 0; - b = i < len ? p[i++] : 0; - c = i < len ? p[i++] : 0; - t = (a << 16) + (b << 8) + c; - arr.push(enc[(t >>> 3 * 6) & 63]); - arr.push(enc[(t >>> 2 * 6) & 63]); - arr.push(enc[(t >>> 1 * 6) & 63]); - arr.push(enc[(t >>> 0 * 6) & 63]); - } - if (len % 3 > 0) { - arr[arr.length-1] = '='; - if (len % 3 === 1) arr[arr.length-2] = '='; - } - return arr.join(''); - } - - - // Generate key. - - // Set parallelization parameter to 1. - var p = 1; - - if (logN < 1 || logN > 31) - throw new Error('scrypt: logN not be between 1 and 31'); - - var MAX_INT = (1<<31)>>>0, - N = (1<>>0, - XY, V, B, tmp; - - if (r*p >= 1<<30 || r > MAX_INT/128/p || r > MAX_INT/256 || N > MAX_INT/128/r) - throw new Error('scrypt: parameters are too large'); - - // Decode strings. - if (typeof password === 'string') - password = stringToUTF8Bytes(password); - if (typeof salt === 'string') - salt = stringToUTF8Bytes(salt); - - if (typeof Int32Array !== 'undefined') { - //XXX We can use Uint32Array, but Int32Array is faster in Safari. - XY = new Int32Array(64*r); - V = new Int32Array(32*N*r); - tmp = new Int32Array(16); - } else { - XY = []; - V = []; - tmp = new Array(16); - } - B = PBKDF2_HMAC_SHA256_OneIter(password, salt, p*128*r); - - var xi = 0, yi = 32 * r; - - function smixStart() { - for (var i = 0; i < 32*r; i++) { - var j = i*4; - XY[xi+i] = ((B[j+3] & 0xff)<<24) | ((B[j+2] & 0xff)<<16) | - ((B[j+1] & 0xff)<<8) | ((B[j+0] & 0xff)<<0); - } - } - - function smixStep1(start, end) { - for (var i = start; i < end; i += 2) { - blockCopy(V, i*(32*r), XY, xi, 32*r); - blockMix(tmp, XY, xi, yi, r); - - blockCopy(V, (i+1)*(32*r), XY, yi, 32*r); - blockMix(tmp, XY, yi, xi, r); - } - } - - function smixStep2(start, end) { - for (var i = start; i < end; i += 2) { - var j = integerify(XY, xi, r) & (N-1); - blockXOR(XY, xi, V, j*(32*r), 32*r); - blockMix(tmp, XY, xi, yi, r); - - j = integerify(XY, yi, r) & (N-1); - blockXOR(XY, yi, V, j*(32*r), 32*r); - blockMix(tmp, XY, yi, xi, r); - } - } - - function smixFinish() { - for (var i = 0; i < 32*r; i++) { - var j = XY[xi+i]; - B[i*4+0] = (j>>>0) & 0xff; - B[i*4+1] = (j>>>8) & 0xff; - B[i*4+2] = (j>>>16) & 0xff; - B[i*4+3] = (j>>>24) & 0xff; - } - } - - var nextTick = (typeof setImmediate !== 'undefined') ? setImmediate : setTimeout; - - function interruptedFor(start, end, step, fn, donefn) { - (function performStep() { - nextTick(function() { - fn(start, start + step < end ? start + step : end); - start += step; - if (start < end) - performStep(); - else - donefn(); - }); - })(); - } - - function getResult(enc) { - var result = PBKDF2_HMAC_SHA256_OneIter(password, B, dkLen); - if (enc === 'base64') - return bytesToBase64(result); - else if (enc === 'hex') - return bytesToHex(result); - else - return result; - } - - if (typeof interruptStep === 'function') { - // Called as: scrypt(..., callback, [encoding]) - // shifting: scrypt(..., interruptStep, callback, [encoding]) - encoding = callback; - callback = interruptStep; - interruptStep = 1000; - } - - if (interruptStep <= 0) { - // - // Blocking async variant, calls callback. - // - smixStart(); - smixStep1(0, N); - smixStep2(0, N); - smixFinish(); - callback(getResult(encoding)); - - } else { - // - // Async variant with interruptions, calls callback. - // - smixStart(); - interruptedFor(0, N, interruptStep*2, smixStep1, function() { - interruptedFor(0, N, interruptStep*2, smixStep2, function () { - smixFinish(); - callback(getResult(encoding)); - }); - }); - } -} - -if (typeof module !== 'undefined') module.exports = scrypt; diff --git a/library/KeyPairOperation.coffee b/library/KeyPairOperation.coffee index aeb52e3..7d00398 100644 --- a/library/KeyPairOperation.coffee +++ b/library/KeyPairOperation.coffee @@ -1,7 +1,7 @@ module.exports = class KeyPairOperation BLAKE2s = require "./BLAKE2s" NaCl = require "tweetnacl" - scrypt = require "./scrypt-async" + scrypt = require "scrypt-async" EmailAddress = require "./EmailAddress" SecretPhrase = require "./SecretPhrase" diff --git a/library/index.coffee b/library/index.coffee index 8836d62..ec8b7e0 100644 --- a/library/index.coffee +++ b/library/index.coffee @@ -34,4 +34,4 @@ miniLockLib.BLAKE2s = require "./BLAKE2s" miniLockLib.Entropizer = require "entropizer" miniLockLib.NaCl = require "tweetnacl" miniLockLib.NaCl.stream = require("nacl-stream").stream -miniLockLib.scrypt = require "./scrypt-async" +miniLockLib.scrypt = require "scrypt-async" diff --git a/package.json b/package.json index c3e3d56..3f40236 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,10 @@ "repository": "git+https://github.com/45678/miniLockLib.git", "dependencies": { "base-58": "0.0.x", - "blake2s-js": "1.0.3", - "entropizer": "^0.1.3", - "nacl-stream": "0.3.3", + "blake2s-js": "1.0.x", + "entropizer": "0.1.x", + "nacl-stream": "0.3.x", + "scrypt-async": "1.2.x", "tweetnacl": "0.13.x" }, "devDependencies": {