Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #92 from jimni1222/scrypt
Browse files Browse the repository at this point in the history
Change library scrypt.js to scryptsy
  • Loading branch information
jimni1222 authored Sep 3, 2019
2 parents 3c09efe + 642bcfc commit 0d163bd
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"oboe": "2.1.3",
"request": "2.87.0",
"requestretry": "^2.0.2",
"scrypt.js": "0.2.0",
"scryptsy": "2.1.0",
"underscore": "^1.9.1",
"utf8": "2.1.1",
"uuid": "2.0.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/caver-klay/caver-klay-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var RLP = require("eth-lib/lib/rlp");
var Nat = require("eth-lib/lib/nat");
var Bytes = require("eth-lib/lib/bytes");
var cryp = (typeof global === 'undefined') ? require('crypto-browserify') : require('crypto');
var scryptsy = require('scrypt.js');
var scrypt = require('./scrypt');
var uuid = require('uuid');
var utils = require('../../../caver-utils');
var helpers = require('../../../caver-core-helpers');
Expand Down Expand Up @@ -470,7 +470,7 @@ Accounts.prototype.decrypt = function (v3Keystore, password, nonStrict) {
kdfparams = json.crypto.kdfparams;

// FIXME: support progress reporting callback
derivedKey = scryptsy(new Buffer(password), new Buffer(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
derivedKey = scrypt(new Buffer(password), new Buffer(kdfparams.salt, 'hex'), kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
} else if (json.crypto.kdf === 'pbkdf2') {
kdfparams = json.crypto.kdfparams;

Expand Down Expand Up @@ -589,7 +589,7 @@ Accounts.prototype.encrypt = function (privateKey, password, options) {
kdfparams.n = options.n || 4096; // 2048 4096 8192 16384
kdfparams.r = options.r || 8;
kdfparams.p = options.p || 1;
derivedKey = scryptsy(new Buffer(password), salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
derivedKey = scrypt(new Buffer(password), salt, kdfparams.n, kdfparams.r, kdfparams.p, kdfparams.dklen);
} else {
throw new Error('Unsupported kdf');
}
Expand Down
92 changes: 92 additions & 0 deletions packages/caver-klay/caver-klay-accounts/src/scrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Modifications copyright 2019 The caver-js Authors
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
This file is derived from web3.js/packages/web3-eth-accounts/src/scrpyt.js (2019/09/03).
Modified and improved for the caver-js development.
*/

var scryptsy = require('scryptsy');

var scrypt;

var isNode = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
if (isNode) {
var NODE_MIN_VER_WITH_BUILTIN_SCRYPT = '10.5.0';
var NODE_MIN_VER_INCOMPAT_SCRYPT_PKG = '12.0.0';
var semver = require('semver');
var useNodeBuiltin = isNode && semver.Range('>=' + NODE_MIN_VER_WITH_BUILTIN_SCRYPT).test(process.version);

var tryScryptPkg = (function() {
var scryptPkg;
return function() {
if (scryptPkg !== undefined) { return scryptPkg; }
try {
scryptPkg = (function(m) { return require(m); })('scrypt');
} catch (e) {
if (/was compiled against a different/.test(e.message)) {
throw e;
}
scryptPkg = null;
}
return scryptPkg;
};
})();

var canImprove = function(nodeVer) {
return 'can improve web3\'s peformance when running Node.js versions older than ' + nodeVer + ' by installing the (deprecated) scrypt package in your project';
};

if (useNodeBuiltin) {
var crypto = require('crypto');
var fallbackCount = 0;
scrypt = function(key, salt, N, r, p, dkLen) {
try {
return crypto.scryptSync(key, salt, dkLen, {N: N, r: r, p: p});
} catch (e) {
if (/scrypt:memory limit exceeded/.test(e.message)) {
var scryptPkg = tryScryptPkg();
if (scryptPkg) {
return scryptPkg.hashSync(key, {N: N, r: r, p: p}, dkLen, salt);
}
fallbackCount += 1;
console.warn(
'\x1b[33m%s\x1b[0m',
'Memory limit exceeded for Node\'s built-in crypto.scrypt, falling back to scryptsy (times: ' + fallbackCount + '), if this happens frequently you ' + canImprove(NODE_MIN_VER_INCOMPAT_SCRYPT_PKG)
);
return scryptsy(key, salt, N, r, p, dkLen);
}
throw e;
}
};
} else {
var scryptPkg = tryScryptPkg();
if (scryptPkg) {
scrypt = function(key, salt, N, r, p, dkLen) {
return scryptPkg.hashSync(key, {N: N, r: r, p: p}, dkLen, salt);
};
} else {
console.warn(
'\x1b[33m%s\x1b[0m',
'You ' + canImprove(NODE_MIN_VER_WITH_BUILTIN_SCRYPT)
);
}
}
}

scrypt = scrypt || scryptsy;

module.exports = scrypt;

0 comments on commit 0d163bd

Please sign in to comment.