js-crypto-random
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -1,1 +0,29 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),Object.defineProperty(exports,"getRandomBytes",{enumerable:!0,get:function a(){return _random.getRandomBytes}}),Object.defineProperty(exports,"getRandomAsciiString",{enumerable:!0,get:function a(){return _random.getRandomAsciiString}}),exports.default=void 0;var _random=require("./random.js"),_default={getRandomBytes:_random.getRandomBytes,getRandomAsciiString:_random.getRandomAsciiString};exports.default=_default; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
Object.defineProperty(exports, "getRandomBytes", { | ||
enumerable: true, | ||
get: function get() { | ||
return _random.getRandomBytes; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getRandomAsciiString", { | ||
enumerable: true, | ||
get: function get() { | ||
return _random.getRandomAsciiString; | ||
} | ||
}); | ||
exports.default = void 0; | ||
var _random = require("./random.js"); | ||
/** | ||
* index.js | ||
*/ | ||
var _default = { | ||
getRandomBytes: _random.getRandomBytes, | ||
getRandomAsciiString: _random.getRandomAsciiString | ||
}; | ||
exports.default = _default; |
@@ -1,12 +0,125 @@ | ||
"use strict";var _interopRequireWildcard=require("@babel/runtime/helpers/interopRequireWildcard"),_interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:!0}),exports.getRandomAsciiString=getRandomAsciiString,exports.getRandomBytes=getRandomBytes;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator")),_asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")),util=_interopRequireWildcard(require("./util.js"));/** | ||
"use strict"; | ||
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getRandomAsciiString = getRandomAsciiString; | ||
exports.getRandomBytes = getRandomBytes; | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
var util = _interopRequireWildcard(require("./util.js")); | ||
/** | ||
* random.js | ||
*/ /** | ||
*/ | ||
/** | ||
* secure random 'ASCII' string generator based on getRandomBytes; | ||
* @param len | ||
* @return {Promise<string>} | ||
*/function getRandomAsciiString(){return _getRandomAsciiString.apply(this,arguments)}/** | ||
*/ | ||
function getRandomAsciiString(_x) { | ||
return _getRandomAsciiString.apply(this, arguments); | ||
} | ||
/** | ||
* secure random generator that returns uint 8 array filled with cryptographically secure random bytes | ||
* @param len | ||
* @return {Promise<Uint8Array>} | ||
*/function _getRandomAsciiString(){return _getRandomAsciiString=(0,_asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function a(b){var c,d,e;return _regenerator.default.wrap(function(a){var f=String.fromCharCode;for(;;)switch(a.prev=a.next){case 0:return a.next=2,getRandomBytes(b);case 2:// Ascii code excluding control characters are in 0x20 -- 0x7e | ||
for(c=a.sent,d="",e=0;e<b;e++)c[e]=c[e]%94+32,d+=f(c[e]);return a.abrupt("return",d);case 6:case"end":return a.stop();}},a,this)})),_getRandomAsciiString.apply(this,arguments)}function getRandomBytes(){return _getRandomBytes.apply(this,arguments)}function _getRandomBytes(){return _getRandomBytes=(0,_asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function a(b){var c,d,e;return _regenerator.default.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(c=util.getWebCryptoAll(),d=util.getNodeCrypto(),e=new Uint8Array(b),"undefined"==typeof c||"function"!=typeof c.getRandomValues){a.next=7;break}c.getRandomValues(e),a.next=12;break;case 7:if("undefined"==typeof d){a.next=11;break}e=new Uint8Array(d.randomBytes(b)),a.next=12;break;case 11:throw new Error("UnsupportedEnvironment");case 12:return a.abrupt("return",e);case 13:case"end":return a.stop();}},a,this)})),_getRandomBytes.apply(this,arguments)} | ||
*/ | ||
function _getRandomAsciiString() { | ||
_getRandomAsciiString = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee(len) { | ||
var array, finalString, i; | ||
return _regenerator.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
_context.next = 2; | ||
return getRandomBytes(len); | ||
case 2: | ||
array = _context.sent; | ||
finalString = ''; // Ascii code excluding control characters are in 0x20 -- 0x7e | ||
for (i = 0; i < len; i++) { | ||
array[i] = array[i] % 0x5e + 0x20; | ||
finalString += String.fromCharCode(array[i]); | ||
} | ||
return _context.abrupt("return", finalString); | ||
case 6: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, this); | ||
})); | ||
return _getRandomAsciiString.apply(this, arguments); | ||
} | ||
function getRandomBytes(_x2) { | ||
return _getRandomBytes.apply(this, arguments); | ||
} | ||
function _getRandomBytes() { | ||
_getRandomBytes = (0, _asyncToGenerator2.default)( | ||
/*#__PURE__*/ | ||
_regenerator.default.mark(function _callee2(len) { | ||
var webCrypto, nodeCrypto, array; | ||
return _regenerator.default.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
webCrypto = util.getWebCryptoAll(); // web crypto api or ms crypto | ||
nodeCrypto = util.getNodeCrypto(); // implementation on node.js | ||
array = new Uint8Array(len); | ||
if (!(typeof webCrypto !== 'undefined' && typeof webCrypto.getRandomValues === 'function')) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
webCrypto.getRandomValues(array); // for modern browsers or legacy ie 11 | ||
_context2.next = 12; | ||
break; | ||
case 7: | ||
if (!(typeof nodeCrypto !== 'undefined')) { | ||
_context2.next = 11; | ||
break; | ||
} | ||
// for node | ||
array = new Uint8Array(nodeCrypto.randomBytes(len)); | ||
_context2.next = 12; | ||
break; | ||
case 11: | ||
throw new Error('UnsupportedEnvironment'); | ||
case 12: | ||
return _context2.abrupt("return", array); | ||
case 13: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2, this); | ||
})); | ||
return _getRandomBytes.apply(this, arguments); | ||
} |
@@ -1,3 +0,20 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getWebCryptoAll=getWebCryptoAll,exports.getNodeCrypto=getNodeCrypto;/** | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getWebCryptoAll = getWebCryptoAll; | ||
exports.getNodeCrypto = getNodeCrypto; | ||
/** | ||
* util.js | ||
*/function getWebCryptoAll(){if("undefined"!=typeof window)return window.crypto?window.crypto:window.msCrypto?window.msCrypto:void 0}function getNodeCrypto(){return"undefined"==typeof window?require("crypto"):void 0} | ||
*/ | ||
function getWebCryptoAll() { | ||
if (typeof window === 'undefined') return undefined;else { | ||
if (window.crypto) return window.crypto;else if (window.msCrypto) return window.msCrypto; | ||
} | ||
} | ||
function getNodeCrypto() { | ||
if (typeof window !== 'undefined') return undefined;else return require('crypto'); | ||
} |
{ | ||
"name": "js-crypto-random", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Universal Module for Secure Random Generator in JavaScript", | ||
@@ -27,14 +27,12 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@babel/cli": "7.1.0", | ||
"@babel/core": "7.1.0", | ||
"@babel/cli": "7.1.2", | ||
"@babel/core": "7.1.2", | ||
"@babel/plugin-transform-runtime": "7.1.0", | ||
"@babel/polyfill": "7.0.0", | ||
"@babel/preset-env": "7.1.0", | ||
"@babel/register": "7.0.0", | ||
"babel-eslint": "10.0.0", | ||
"babel-loader": "8.0.2", | ||
"babel-eslint": "10.0.1", | ||
"babel-loader": "8.0.4", | ||
"babel-plugin-istanbul": "5.1.0", | ||
"babel-preset-minify": "^0.5.0", | ||
"chai": "^4.1.2", | ||
"eslint": "5.6.0", | ||
"eslint": "5.6.1", | ||
"karma": "3.0.0", | ||
@@ -50,8 +48,8 @@ "karma-chrome-launcher": "^2.2.0", | ||
"webpack": "^4.20.2", | ||
"webpack-cli": "^3.1.1" | ||
"webpack-cli": "^3.1.2" | ||
}, | ||
"dependencies": { | ||
"@babel/plugin-transform-regenerator": "7.0.0", | ||
"@babel/runtime": "7.0.0" | ||
"@babel/plugin-transform-regenerator": "^7.0.0", | ||
"@babel/runtime": "^7.1.2" | ||
} | ||
} |
Universal Module for Secure Random Generator in JavaScript | ||
-- | ||
[![CircleCI](https://circleci.com/gh/junkurihara/js-crypto-random.svg?style=svg)](https://circleci.com/gh/junkurihara/js-crypto-random) | ||
@@ -43,3 +44,13 @@ > **WARNING**: At this time this solution should be considered suitable for research and experimentation, further code and security review is needed before utilization in a production application. | ||
## 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
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 1 instance in 1 package
9639
21
139
1
55
+ Added@babel/helper-plugin-utils@7.25.9(transitive)
+ Added@babel/plugin-transform-regenerator@7.25.9(transitive)
+ Added@babel/runtime@7.26.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedregenerator-transform@0.15.2(transitive)
- Removed@babel/plugin-transform-regenerator@7.0.0(transitive)
- Removed@babel/runtime@7.0.0(transitive)
- Removedprivate@0.1.8(transitive)
- Removedregenerator-runtime@0.12.1(transitive)
- Removedregenerator-transform@0.13.4(transitive)
Updated@babel/runtime@^7.1.2