js-crypto-hash
Advanced tools
Comparing version 0.1.1 to 0.1.2
142
dist/hash.js
@@ -1,4 +0,25 @@ | ||
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports.compute=compute;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")),util=_interopRequireWildcard(require("./util.js")),_params=_interopRequireDefault(require("./params.js"));/** | ||
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.compute = compute; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
var util = _interopRequireWildcard(require("./util.js")); | ||
var _params = _interopRequireDefault(require("./params.js")); | ||
/** | ||
* hash.js | ||
*/ /** | ||
*/ | ||
/** | ||
* Compute Hash | ||
@@ -8,2 +29,117 @@ * @param hash | ||
* @return {Promise<Uint8Array>} | ||
*/function compute(){return _compute.apply(this,arguments)}function _compute(){return _compute=(0,_asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function a(b){var c,d,e,f,g,h,i,j,k=arguments;return _regenerator.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(c=1<k.length&&void 0!==k[1]?k[1]:"SHA-256",!(0>Object.keys(_params.default.hashes).indexOf(c))){a.next=3;break}throw new Error("UnsupportedHashAlgorithm");case 3:if(b instanceof Uint8Array){a.next=5;break}throw new Error("UnsupportedMessageType");case 5:if(d=util.getWebCrypto(),e=util.getNodeCrypto(),f=util.getMsCrypto(),"undefined"==typeof d||"function"!=typeof d.digest){a.next=14;break}return a.next=11,d.digest(c,b);case 11:g=a.sent,a.next=29;break;case 14:if("undefined"==typeof e){a.next=21;break}h=_params.default.hashes[c].nodeName,i=e.createHash(h),i.update(b),g=i.digest(),a.next=29;break;case 21:if("undefined"==typeof f||"function"!=typeof f.digest){a.next=28;break}return j=function(a,b){return new Promise(function(c,d){var e=window.msCrypto.subtle.digest(a,b);e.oncomplete=function(a){c(a.target.result)},e.onerror=function(a){d(a)}})},a.next=25,j(c,b);case 25:g=a.sent,a.next=29;break;case 28:throw new Error("UnsupportedEnvironment");case 29:return a.abrupt("return",new Uint8Array(g));case 30:case"end":return a.stop();}},a,this)})),_compute.apply(this,arguments)} | ||
*/ | ||
function compute(_x) { | ||
return _compute.apply(this, arguments); | ||
} | ||
function _compute() { | ||
_compute = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee(msg) { | ||
var hash, | ||
webCrypto, | ||
nodeCrypto, | ||
msCrypto, | ||
msgHash, | ||
alg, | ||
hashFunc, | ||
msdigest, | ||
_args = arguments; | ||
return _regenerator.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
hash = _args.length > 1 && _args[1] !== undefined ? _args[1] : 'SHA-256'; | ||
if (!(Object.keys(_params.default.hashes).indexOf(hash) < 0)) { | ||
_context.next = 3; | ||
break; | ||
} | ||
throw new Error('UnsupportedHashAlgorithm'); | ||
case 3: | ||
if (msg instanceof Uint8Array) { | ||
_context.next = 5; | ||
break; | ||
} | ||
throw new Error('UnsupportedMessageType'); | ||
case 5: | ||
webCrypto = util.getWebCrypto(); | ||
nodeCrypto = util.getNodeCrypto(); | ||
msCrypto = util.getMsCrypto(); | ||
if (!(typeof webCrypto !== 'undefined' && typeof webCrypto.digest === 'function')) { | ||
_context.next = 14; | ||
break; | ||
} | ||
_context.next = 11; | ||
return webCrypto.digest(hash, msg); | ||
case 11: | ||
msgHash = _context.sent; | ||
_context.next = 29; | ||
break; | ||
case 14: | ||
if (!(typeof nodeCrypto !== 'undefined')) { | ||
_context.next = 21; | ||
break; | ||
} | ||
// for node | ||
alg = _params.default.hashes[hash].nodeName; | ||
hashFunc = nodeCrypto.createHash(alg); | ||
hashFunc.update(msg); | ||
msgHash = hashFunc.digest(); | ||
_context.next = 29; | ||
break; | ||
case 21: | ||
if (!(typeof msCrypto !== 'undefined' && typeof msCrypto.digest === 'function')) { | ||
_context.next = 28; | ||
break; | ||
} | ||
// for legacy ie 11 | ||
// WTF IE!!! | ||
msdigest = function msdigest(alg, m) { | ||
return new Promise(function (resolve, reject) { | ||
var op = window.msCrypto.subtle.digest(alg, m); | ||
op.oncomplete = function (evt) { | ||
resolve(evt.target.result); | ||
}; | ||
op.onerror = function (e) { | ||
reject(e); | ||
}; | ||
}); | ||
}; | ||
_context.next = 25; | ||
return msdigest(hash, msg); | ||
case 25: | ||
msgHash = _context.sent; | ||
_context.next = 29; | ||
break; | ||
case 28: | ||
throw new Error('UnsupportedEnvironment'); | ||
case 29: | ||
return _context.abrupt("return", new Uint8Array(msgHash)); | ||
case 30: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
return _compute.apply(this, arguments); | ||
} |
@@ -1,1 +0,22 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"compute",{enumerable:!0,get:function a(){return _hash.compute}}),exports.default=void 0;var _hash=require("./hash.js"),_default={compute:_hash.compute};exports.default=_default; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "compute", { | ||
enumerable: true, | ||
get: function get() { | ||
return _hash.compute; | ||
} | ||
}); | ||
exports.default = void 0; | ||
var _hash = require("./hash.js"); | ||
/** | ||
* index.js | ||
*/ | ||
var _default = { | ||
compute: _hash.compute | ||
}; | ||
exports.default = _default; |
@@ -1,3 +0,27 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;/** | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
/** | ||
* params. js | ||
*/var _default={hashes:{"SHA-256":{nodeName:"sha256",hashSize:32},"SHA-384":{nodeName:"sha384",hashSize:48},"SHA-512":{nodeName:"sha512",hashSize:64}}};exports.default=_default; | ||
*/ | ||
var _default = { | ||
hashes: { | ||
'SHA-256': { | ||
nodeName: 'sha256', | ||
hashSize: 32 | ||
}, | ||
'SHA-384': { | ||
nodeName: 'sha384', | ||
hashSize: 48 | ||
}, | ||
'SHA-512': { | ||
nodeName: 'sha512', | ||
hashSize: 64 | ||
} | ||
} | ||
}; | ||
exports.default = _default; |
@@ -1,3 +0,28 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getWebCrypto=getWebCrypto,exports.getNodeCrypto=getNodeCrypto,exports.getMsCrypto=getMsCrypto;/** | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getWebCrypto = getWebCrypto; | ||
exports.getNodeCrypto = getNodeCrypto; | ||
exports.getMsCrypto = getMsCrypto; | ||
/** | ||
* util.js | ||
*/function getWebCrypto(){if("undefined"!=typeof window)return window.crypto?window.crypto.subtle:void 0}function getNodeCrypto(){return"undefined"==typeof window?require("crypto"):void 0}function getMsCrypto(){if("undefined"!=typeof window)return window.crypto?void 0:window.msCrypto?window.msCrypto.subtle:void 0} | ||
*/ | ||
function getWebCrypto() { | ||
if (typeof window === 'undefined') return undefined;else { | ||
if (window.crypto) return window.crypto.subtle; | ||
} | ||
} | ||
function getNodeCrypto() { | ||
if (typeof window !== 'undefined') return undefined;else return require('crypto'); | ||
} | ||
function getMsCrypto() { | ||
if (typeof window === 'undefined') return undefined;else { | ||
if (window.crypto) return undefined; | ||
if (window.msCrypto) return window.msCrypto.subtle; | ||
} | ||
} |
{ | ||
"name": "js-crypto-hash", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Universal Module for Hash Function in JavaScript", | ||
"main": "dist/index.js", | ||
"main": "dist/jschash.bundle.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha --require @babel/register --recursive $(find test -name '*.spec.js')", | ||
"karma": "./node_modules/.bin/karma start", | ||
"build": "NODE_ENV=production ./node_modules/.bin/babel src -d dist/ --keep-file-extension" | ||
"webpack": "./node_modules/.bin/webpack --optimize-minimize --mode production", | ||
"build": "NODE_ENV=production ./node_modules/.bin/babel src -d dist/ --keep-file-extension && npm run webpack" | ||
}, | ||
@@ -11,0 +12,0 @@ "author": "Jun Kurihara", |
@@ -39,13 +39,3 @@ Universal Module for Hash Function in JavaScript | ||
## How to bundle scripts importing this module via Webpack | ||
When you bundle files importing this module via Webpack for web, the bundler tries to simultaneously bundle modules that are compatible to some native modules of Node.js, i.e., those in`node-libs-browser` module. But this module automatically chooses the native `crypto` module in Node.js and `crypto.subtle` of Web API in browsers by checking its running environment, and hence the bundled modules of `node-libs-browser` are redundant. From this observation, you should exclude them wen you create bundle scripts importing this module. In particular, the `externals` option of `webpack.config.js` is useful as follows. | ||
```javascript | ||
{ | ||
externals: { | ||
'crypto': true | ||
} | ||
} | ||
``` | ||
# License | ||
Licensed under the MIT license, see `LICENSE` file. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 2 instances in 1 package
19482
8
179
2
40
2