@thangnm.nip/arouter 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.next/standalone/.next/BUILD_ID +1 -1
- package/.next/standalone/.next/app-path-routes-manifest.json +5 -5
- package/.next/standalone/.next/build-manifest.json +2 -2
- package/.next/standalone/.next/prerender-manifest.json +3 -3
- package/.next/standalone/.next/server/app/(dashboard)/mitm/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/(dashboard)/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/_global-error.html +2 -2
- package/.next/standalone/.next/server/app/_global-error.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/_not-found.html +1 -1
- package/.next/standalone/.next/server/app/_not-found.rsc +3 -3
- package/.next/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +3 -3
- package/.next/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +3 -3
- package/.next/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/antigravity-mitm/alias/route.js +2 -2
- package/.next/standalone/.next/server/app/api/cli-tools/antigravity-mitm/alias/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/antigravity-mitm/console-logs/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/antigravity-mitm/route.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/antigravity-mitm/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/antigravity-mitm/settings/route.js +2 -2
- package/.next/standalone/.next/server/app/api/cli-tools/antigravity-mitm/settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/claude-settings/route.js +2 -2
- package/.next/standalone/.next/server/app/api/cli-tools/claude-settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/codex-settings/route.js +2 -2
- package/.next/standalone/.next/server/app/api/cli-tools/codex-settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/copilot-settings/route.js +2 -2
- package/.next/standalone/.next/server/app/api/cli-tools/copilot-settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/droid-settings/route.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/droid-settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/openclaw-settings/route.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/openclaw-settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/opencode-settings/route.js +1 -1
- package/.next/standalone/.next/server/app/api/cli-tools/opencode-settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/keys/[id]/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/keys/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/locale/route.js +2 -2
- package/.next/standalone/.next/server/app/api/locale/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/models/alias/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/models/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/providers/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/settings/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/api/version/route_client-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/app/index.html +1 -1
- package/.next/standalone/.next/server/app/index.rsc +4 -4
- package/.next/standalone/.next/server/app/index.segments/!KGRhc2hib2FyZCk/__PAGE__.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/index.segments/!KGRhc2hib2FyZCk.segment.rsc +2 -2
- package/.next/standalone/.next/server/app/index.segments/_full.segment.rsc +4 -4
- package/.next/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/index.segments/_index.segment.rsc +3 -3
- package/.next/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/mitm.html +1 -1
- package/.next/standalone/.next/server/app/mitm.rsc +5 -5
- package/.next/standalone/.next/server/app/mitm.segments/!KGRhc2hib2FyZCk/mitm/__PAGE__.segment.rsc +2 -2
- package/.next/standalone/.next/server/app/mitm.segments/!KGRhc2hib2FyZCk/mitm.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/mitm.segments/!KGRhc2hib2FyZCk.segment.rsc +2 -2
- package/.next/standalone/.next/server/app/mitm.segments/_full.segment.rsc +5 -5
- package/.next/standalone/.next/server/app/mitm.segments/_head.segment.rsc +1 -1
- package/.next/standalone/.next/server/app/mitm.segments/_index.segment.rsc +3 -3
- package/.next/standalone/.next/server/app/mitm.segments/_tree.segment.rsc +1 -1
- package/.next/standalone/.next/server/app-paths-manifest.json +5 -5
- package/.next/standalone/.next/server/chunks/411.js +1 -1
- package/.next/standalone/.next/server/middleware.js +2 -2
- package/.next/standalone/.next/server/pages/404.html +1 -1
- package/.next/standalone/.next/server/pages/500.html +2 -2
- package/.next/standalone/.next/server/server-reference-manifest.js +1 -1
- package/.next/standalone/.next/server/server-reference-manifest.json +1 -1
- package/.next/standalone/.next/static/chunks/{702-90a93bbee38dba8f.js → 702-0864796c00fc702e.js} +1 -1
- package/.next/standalone/node_modules/node-forge/LICENSE +331 -0
- package/.next/standalone/node_modules/node-forge/README.md +2071 -0
- package/.next/standalone/node_modules/node-forge/dist/forge.all.min.js +2 -0
- package/.next/standalone/node_modules/node-forge/dist/forge.all.min.js.map +1 -0
- package/.next/standalone/node_modules/node-forge/dist/forge.min.js +2 -0
- package/.next/standalone/node_modules/node-forge/dist/forge.min.js.map +1 -0
- package/.next/standalone/node_modules/node-forge/dist/prime.worker.min.js +2 -0
- package/.next/standalone/node_modules/node-forge/dist/prime.worker.min.js.map +1 -0
- package/.next/standalone/node_modules/node-forge/flash/swf/SocketPool.swf +0 -0
- package/.next/standalone/node_modules/node-forge/lib/aes.js +1091 -0
- package/.next/standalone/node_modules/node-forge/lib/aesCipherSuites.js +282 -0
- package/.next/standalone/node_modules/node-forge/lib/asn1-validator.js +91 -0
- package/.next/standalone/node_modules/node-forge/lib/asn1.js +1503 -0
- package/.next/standalone/node_modules/node-forge/lib/baseN.js +186 -0
- package/.next/standalone/node_modules/node-forge/lib/cipher.js +230 -0
- package/.next/standalone/node_modules/node-forge/lib/cipherModes.js +999 -0
- package/.next/standalone/node_modules/node-forge/lib/des.js +496 -0
- package/.next/standalone/node_modules/node-forge/lib/ed25519.js +1072 -0
- package/.next/standalone/node_modules/node-forge/lib/forge.js +13 -0
- package/.next/standalone/node_modules/node-forge/lib/form.js +149 -0
- package/.next/standalone/node_modules/node-forge/lib/hmac.js +146 -0
- package/.next/standalone/node_modules/node-forge/lib/http.js +1346 -0
- package/.next/standalone/node_modules/node-forge/lib/index.all.js +16 -0
- package/.next/standalone/node_modules/node-forge/lib/index.js +33 -0
- package/.next/standalone/node_modules/node-forge/lib/jsbn.js +1264 -0
- package/.next/standalone/node_modules/node-forge/lib/kem.js +168 -0
- package/.next/standalone/node_modules/node-forge/lib/log.js +319 -0
- package/.next/standalone/node_modules/node-forge/lib/md.all.js +13 -0
- package/.next/standalone/node_modules/node-forge/lib/md.js +11 -0
- package/.next/standalone/node_modules/node-forge/lib/md5.js +289 -0
- package/.next/standalone/node_modules/node-forge/lib/mgf.js +12 -0
- package/.next/standalone/node_modules/node-forge/lib/mgf1.js +57 -0
- package/.next/standalone/node_modules/node-forge/lib/oids.js +179 -0
- package/.next/standalone/node_modules/node-forge/lib/pbe.js +1023 -0
- package/.next/standalone/node_modules/node-forge/lib/pbkdf2.js +211 -0
- package/.next/standalone/node_modules/node-forge/lib/pem.js +237 -0
- package/.next/standalone/node_modules/node-forge/lib/pkcs1.js +276 -0
- package/.next/standalone/node_modules/node-forge/lib/pkcs12.js +1078 -0
- package/.next/standalone/node_modules/node-forge/lib/pkcs7.js +1260 -0
- package/.next/standalone/node_modules/node-forge/lib/pkcs7asn1.js +410 -0
- package/.next/standalone/node_modules/node-forge/lib/pki.js +102 -0
- package/.next/standalone/node_modules/node-forge/lib/prime.js +297 -0
- package/.next/standalone/node_modules/node-forge/lib/prime.worker.js +168 -0
- package/.next/standalone/node_modules/node-forge/lib/prng.js +419 -0
- package/.next/standalone/node_modules/node-forge/lib/pss.js +241 -0
- package/.next/standalone/node_modules/node-forge/lib/random.js +191 -0
- package/.next/standalone/node_modules/node-forge/lib/rc2.js +410 -0
- package/.next/standalone/node_modules/node-forge/lib/rsa.js +1949 -0
- package/.next/standalone/node_modules/node-forge/lib/sha1.js +319 -0
- package/.next/standalone/node_modules/node-forge/lib/sha256.js +327 -0
- package/.next/standalone/node_modules/node-forge/lib/sha512.js +561 -0
- package/.next/standalone/node_modules/node-forge/lib/socket.js +287 -0
- package/.next/standalone/node_modules/node-forge/lib/ssh.js +236 -0
- package/.next/standalone/node_modules/node-forge/lib/tls.js +4282 -0
- package/.next/standalone/node_modules/node-forge/lib/tlssocket.js +249 -0
- package/.next/standalone/node_modules/node-forge/lib/util.js +2652 -0
- package/.next/standalone/node_modules/node-forge/lib/x509.js +3242 -0
- package/.next/standalone/node_modules/node-forge/lib/xhr.js +738 -0
- package/.next/standalone/node_modules/node-forge/package.json +123 -0
- package/.next/standalone/node_modules/node-machine-id/.babelrc +26 -0
- package/.next/standalone/node_modules/node-machine-id/.eslintignore +2 -0
- package/.next/standalone/node_modules/node-machine-id/.eslintrc +140 -0
- package/.next/standalone/node_modules/node-machine-id/.flowconfig +9 -0
- package/.next/standalone/node_modules/node-machine-id/LICENSE +21 -0
- package/.next/standalone/node_modules/node-machine-id/README.md +84 -0
- package/.next/standalone/node_modules/node-machine-id/dist/index.js +1 -0
- package/.next/standalone/node_modules/node-machine-id/index.js +80 -0
- package/.next/standalone/node_modules/node-machine-id/package.json +74 -0
- package/.next/standalone/node_modules/node-machine-id/tests/index.js +45 -0
- package/.next/standalone/node_modules/node-machine-id/types/index.d.ts +17 -0
- package/.next/standalone/node_modules/node-machine-id/webpack.config.babel.js +59 -0
- package/.next/standalone/package.json +1 -1
- package/.next/standalone/src/mitm/cert/generate.js +32 -0
- package/.next/standalone/src/mitm/cert/install.js +176 -0
- package/.next/standalone/src/mitm/cert/rootCA.js +173 -0
- package/.next/standalone/src/mitm/dns/dnsConfig.js +216 -0
- package/.next/standalone/src/mitm/logger.js +8 -0
- package/.next/standalone/src/mitm/manager.js +603 -0
- package/.next/standalone/src/mitm/paths.js +16 -0
- package/package.json +1 -1
- /package/.next/standalone/.next/static/{Ws7kclTEFgfHRwd7pk9Ly → DkVxg6bby4SUixuq1vdO8}/_buildManifest.js +0 -0
- /package/.next/standalone/.next/static/{Ws7kclTEFgfHRwd7pk9Ly → DkVxg6bby4SUixuq1vdO8}/_ssgManifest.js +0 -0
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prime number generation API.
|
|
3
|
+
*
|
|
4
|
+
* @author Dave Longley
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2014 Digital Bazaar, Inc.
|
|
7
|
+
*/
|
|
8
|
+
var forge = require('./forge');
|
|
9
|
+
require('./util');
|
|
10
|
+
require('./jsbn');
|
|
11
|
+
require('./random');
|
|
12
|
+
|
|
13
|
+
(function() {
|
|
14
|
+
|
|
15
|
+
// forge.prime already defined
|
|
16
|
+
if(forge.prime) {
|
|
17
|
+
module.exports = forge.prime;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/* PRIME API */
|
|
22
|
+
var prime = module.exports = forge.prime = forge.prime || {};
|
|
23
|
+
|
|
24
|
+
var BigInteger = forge.jsbn.BigInteger;
|
|
25
|
+
|
|
26
|
+
// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
|
|
27
|
+
var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
|
|
28
|
+
var THIRTY = new BigInteger(null);
|
|
29
|
+
THIRTY.fromInt(30);
|
|
30
|
+
var op_or = function(x, y) {return x|y;};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Generates a random probable prime with the given number of bits.
|
|
34
|
+
*
|
|
35
|
+
* Alternative algorithms can be specified by name as a string or as an
|
|
36
|
+
* object with custom options like so:
|
|
37
|
+
*
|
|
38
|
+
* {
|
|
39
|
+
* name: 'PRIMEINC',
|
|
40
|
+
* options: {
|
|
41
|
+
* maxBlockTime: <the maximum amount of time to block the main
|
|
42
|
+
* thread before allowing I/O other JS to run>,
|
|
43
|
+
* millerRabinTests: <the number of miller-rabin tests to run>,
|
|
44
|
+
* workerScript: <the worker script URL>,
|
|
45
|
+
* workers: <the number of web workers (if supported) to use,
|
|
46
|
+
* -1 to use estimated cores minus one>.
|
|
47
|
+
* workLoad: the size of the work load, ie: number of possible prime
|
|
48
|
+
* numbers for each web worker to check per work assignment,
|
|
49
|
+
* (default: 100).
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* @param bits the number of bits for the prime number.
|
|
54
|
+
* @param options the options to use.
|
|
55
|
+
* [algorithm] the algorithm to use (default: 'PRIMEINC').
|
|
56
|
+
* [prng] a custom crypto-secure pseudo-random number generator to use,
|
|
57
|
+
* that must define "getBytesSync".
|
|
58
|
+
*
|
|
59
|
+
* @return callback(err, num) called once the operation completes.
|
|
60
|
+
*/
|
|
61
|
+
prime.generateProbablePrime = function(bits, options, callback) {
|
|
62
|
+
if(typeof options === 'function') {
|
|
63
|
+
callback = options;
|
|
64
|
+
options = {};
|
|
65
|
+
}
|
|
66
|
+
options = options || {};
|
|
67
|
+
|
|
68
|
+
// default to PRIMEINC algorithm
|
|
69
|
+
var algorithm = options.algorithm || 'PRIMEINC';
|
|
70
|
+
if(typeof algorithm === 'string') {
|
|
71
|
+
algorithm = {name: algorithm};
|
|
72
|
+
}
|
|
73
|
+
algorithm.options = algorithm.options || {};
|
|
74
|
+
|
|
75
|
+
// create prng with api that matches BigInteger secure random
|
|
76
|
+
var prng = options.prng || forge.random;
|
|
77
|
+
var rng = {
|
|
78
|
+
// x is an array to fill with bytes
|
|
79
|
+
nextBytes: function(x) {
|
|
80
|
+
var b = prng.getBytesSync(x.length);
|
|
81
|
+
for(var i = 0; i < x.length; ++i) {
|
|
82
|
+
x[i] = b.charCodeAt(i);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
if(algorithm.name === 'PRIMEINC') {
|
|
88
|
+
return primeincFindPrime(bits, rng, algorithm.options, callback);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
throw new Error('Invalid prime generation algorithm: ' + algorithm.name);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
function primeincFindPrime(bits, rng, options, callback) {
|
|
95
|
+
if('workers' in options) {
|
|
96
|
+
return primeincFindPrimeWithWorkers(bits, rng, options, callback);
|
|
97
|
+
}
|
|
98
|
+
return primeincFindPrimeWithoutWorkers(bits, rng, options, callback);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function primeincFindPrimeWithoutWorkers(bits, rng, options, callback) {
|
|
102
|
+
// initialize random number
|
|
103
|
+
var num = generateRandom(bits, rng);
|
|
104
|
+
|
|
105
|
+
/* Note: All primes are of the form 30k+i for i < 30 and gcd(30, i)=1. The
|
|
106
|
+
number we are given is always aligned at 30k + 1. Each time the number is
|
|
107
|
+
determined not to be prime we add to get to the next 'i', eg: if the number
|
|
108
|
+
was at 30k + 1 we add 6. */
|
|
109
|
+
var deltaIdx = 0;
|
|
110
|
+
|
|
111
|
+
// get required number of MR tests
|
|
112
|
+
var mrTests = getMillerRabinTests(num.bitLength());
|
|
113
|
+
if('millerRabinTests' in options) {
|
|
114
|
+
mrTests = options.millerRabinTests;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// find prime nearest to 'num' for maxBlockTime ms
|
|
118
|
+
// 10 ms gives 5ms of leeway for other calculations before dropping
|
|
119
|
+
// below 60fps (1000/60 == 16.67), but in reality, the number will
|
|
120
|
+
// likely be higher due to an 'atomic' big int modPow
|
|
121
|
+
var maxBlockTime = 10;
|
|
122
|
+
if('maxBlockTime' in options) {
|
|
123
|
+
maxBlockTime = options.maxBlockTime;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
_primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function _primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback) {
|
|
130
|
+
var start = +new Date();
|
|
131
|
+
do {
|
|
132
|
+
// overflow, regenerate random number
|
|
133
|
+
if(num.bitLength() > bits) {
|
|
134
|
+
num = generateRandom(bits, rng);
|
|
135
|
+
}
|
|
136
|
+
// do primality test
|
|
137
|
+
if(num.isProbablePrime(mrTests)) {
|
|
138
|
+
return callback(null, num);
|
|
139
|
+
}
|
|
140
|
+
// get next potential prime
|
|
141
|
+
num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
|
|
142
|
+
} while(maxBlockTime < 0 || (+new Date() - start < maxBlockTime));
|
|
143
|
+
|
|
144
|
+
// keep trying later
|
|
145
|
+
forge.util.setImmediate(function() {
|
|
146
|
+
_primeinc(num, bits, rng, deltaIdx, mrTests, maxBlockTime, callback);
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// NOTE: This algorithm is indeterminate in nature because workers
|
|
151
|
+
// run in parallel looking at different segments of numbers. Even if this
|
|
152
|
+
// algorithm is run twice with the same input from a predictable RNG, it
|
|
153
|
+
// may produce different outputs.
|
|
154
|
+
function primeincFindPrimeWithWorkers(bits, rng, options, callback) {
|
|
155
|
+
// web workers unavailable
|
|
156
|
+
if(typeof Worker === 'undefined') {
|
|
157
|
+
return primeincFindPrimeWithoutWorkers(bits, rng, options, callback);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// initialize random number
|
|
161
|
+
var num = generateRandom(bits, rng);
|
|
162
|
+
|
|
163
|
+
// use web workers to generate keys
|
|
164
|
+
var numWorkers = options.workers;
|
|
165
|
+
var workLoad = options.workLoad || 100;
|
|
166
|
+
var range = workLoad * 30 / 8;
|
|
167
|
+
var workerScript = options.workerScript || 'forge/prime.worker.js';
|
|
168
|
+
if(numWorkers === -1) {
|
|
169
|
+
return forge.util.estimateCores(function(err, cores) {
|
|
170
|
+
if(err) {
|
|
171
|
+
// default to 2
|
|
172
|
+
cores = 2;
|
|
173
|
+
}
|
|
174
|
+
numWorkers = cores - 1;
|
|
175
|
+
generate();
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
generate();
|
|
179
|
+
|
|
180
|
+
function generate() {
|
|
181
|
+
// require at least 1 worker
|
|
182
|
+
numWorkers = Math.max(1, numWorkers);
|
|
183
|
+
|
|
184
|
+
// TODO: consider optimizing by starting workers outside getPrime() ...
|
|
185
|
+
// note that in order to clean up they will have to be made internally
|
|
186
|
+
// asynchronous which may actually be slower
|
|
187
|
+
|
|
188
|
+
// start workers immediately
|
|
189
|
+
var workers = [];
|
|
190
|
+
for(var i = 0; i < numWorkers; ++i) {
|
|
191
|
+
// FIXME: fix path or use blob URLs
|
|
192
|
+
workers[i] = new Worker(workerScript);
|
|
193
|
+
}
|
|
194
|
+
var running = numWorkers;
|
|
195
|
+
|
|
196
|
+
// listen for requests from workers and assign ranges to find prime
|
|
197
|
+
for(var i = 0; i < numWorkers; ++i) {
|
|
198
|
+
workers[i].addEventListener('message', workerMessage);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/* Note: The distribution of random numbers is unknown. Therefore, each
|
|
202
|
+
web worker is continuously allocated a range of numbers to check for a
|
|
203
|
+
random number until one is found.
|
|
204
|
+
|
|
205
|
+
Every 30 numbers will be checked just 8 times, because prime numbers
|
|
206
|
+
have the form:
|
|
207
|
+
|
|
208
|
+
30k+i, for i < 30 and gcd(30, i)=1 (there are 8 values of i for this)
|
|
209
|
+
|
|
210
|
+
Therefore, if we want a web worker to run N checks before asking for
|
|
211
|
+
a new range of numbers, each range must contain N*30/8 numbers.
|
|
212
|
+
|
|
213
|
+
For 100 checks (workLoad), this is a range of 375. */
|
|
214
|
+
|
|
215
|
+
var found = false;
|
|
216
|
+
function workerMessage(e) {
|
|
217
|
+
// ignore message, prime already found
|
|
218
|
+
if(found) {
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
--running;
|
|
223
|
+
var data = e.data;
|
|
224
|
+
if(data.found) {
|
|
225
|
+
// terminate all workers
|
|
226
|
+
for(var i = 0; i < workers.length; ++i) {
|
|
227
|
+
workers[i].terminate();
|
|
228
|
+
}
|
|
229
|
+
found = true;
|
|
230
|
+
return callback(null, new BigInteger(data.prime, 16));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// overflow, regenerate random number
|
|
234
|
+
if(num.bitLength() > bits) {
|
|
235
|
+
num = generateRandom(bits, rng);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// assign new range to check
|
|
239
|
+
var hex = num.toString(16);
|
|
240
|
+
|
|
241
|
+
// start prime search
|
|
242
|
+
e.target.postMessage({
|
|
243
|
+
hex: hex,
|
|
244
|
+
workLoad: workLoad
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
num.dAddOffset(range, 0);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Generates a random number using the given number of bits and RNG.
|
|
254
|
+
*
|
|
255
|
+
* @param bits the number of bits for the number.
|
|
256
|
+
* @param rng the random number generator to use.
|
|
257
|
+
*
|
|
258
|
+
* @return the random number.
|
|
259
|
+
*/
|
|
260
|
+
function generateRandom(bits, rng) {
|
|
261
|
+
var num = new BigInteger(bits, rng);
|
|
262
|
+
// force MSB set
|
|
263
|
+
var bits1 = bits - 1;
|
|
264
|
+
if(!num.testBit(bits1)) {
|
|
265
|
+
num.bitwiseTo(BigInteger.ONE.shiftLeft(bits1), op_or, num);
|
|
266
|
+
}
|
|
267
|
+
// align number on 30k+1 boundary
|
|
268
|
+
num.dAddOffset(31 - num.mod(THIRTY).byteValue(), 0);
|
|
269
|
+
return num;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Returns the required number of Miller-Rabin tests to generate a
|
|
274
|
+
* prime with an error probability of (1/2)^80.
|
|
275
|
+
*
|
|
276
|
+
* See Handbook of Applied Cryptography Chapter 4, Table 4.4.
|
|
277
|
+
*
|
|
278
|
+
* @param bits the bit size.
|
|
279
|
+
*
|
|
280
|
+
* @return the required number of iterations.
|
|
281
|
+
*/
|
|
282
|
+
function getMillerRabinTests(bits) {
|
|
283
|
+
if(bits <= 100) return 27;
|
|
284
|
+
if(bits <= 150) return 18;
|
|
285
|
+
if(bits <= 200) return 15;
|
|
286
|
+
if(bits <= 250) return 12;
|
|
287
|
+
if(bits <= 300) return 9;
|
|
288
|
+
if(bits <= 350) return 8;
|
|
289
|
+
if(bits <= 400) return 7;
|
|
290
|
+
if(bits <= 500) return 6;
|
|
291
|
+
if(bits <= 600) return 5;
|
|
292
|
+
if(bits <= 800) return 4;
|
|
293
|
+
if(bits <= 1250) return 3;
|
|
294
|
+
return 2;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
})();
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RSA Key Generation Worker.
|
|
3
|
+
*
|
|
4
|
+
* @author Dave Longley
|
|
5
|
+
*
|
|
6
|
+
* Copyright (c) 2013 Digital Bazaar, Inc.
|
|
7
|
+
*/
|
|
8
|
+
// worker is built using CommonJS syntax to include all code in one worker file
|
|
9
|
+
//importScripts('jsbn.js');
|
|
10
|
+
var forge = require('./forge');
|
|
11
|
+
require('./jsbn');
|
|
12
|
+
|
|
13
|
+
// prime constants
|
|
14
|
+
var LOW_PRIMES = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997];
|
|
15
|
+
var LP_LIMIT = (1 << 26) / LOW_PRIMES[LOW_PRIMES.length - 1];
|
|
16
|
+
|
|
17
|
+
var BigInteger = forge.jsbn.BigInteger;
|
|
18
|
+
var BIG_TWO = new BigInteger(null);
|
|
19
|
+
BIG_TWO.fromInt(2);
|
|
20
|
+
|
|
21
|
+
self.addEventListener('message', function(e) {
|
|
22
|
+
var result = findPrime(e.data);
|
|
23
|
+
self.postMessage(result);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// start receiving ranges to check
|
|
27
|
+
self.postMessage({found: false});
|
|
28
|
+
|
|
29
|
+
// primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
|
|
30
|
+
var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
|
|
31
|
+
|
|
32
|
+
function findPrime(data) {
|
|
33
|
+
// TODO: abstract based on data.algorithm (PRIMEINC vs. others)
|
|
34
|
+
|
|
35
|
+
// create BigInteger from given random bytes
|
|
36
|
+
var num = new BigInteger(data.hex, 16);
|
|
37
|
+
|
|
38
|
+
/* Note: All primes are of the form 30k+i for i < 30 and gcd(30, i)=1. The
|
|
39
|
+
number we are given is always aligned at 30k + 1. Each time the number is
|
|
40
|
+
determined not to be prime we add to get to the next 'i', eg: if the number
|
|
41
|
+
was at 30k + 1 we add 6. */
|
|
42
|
+
var deltaIdx = 0;
|
|
43
|
+
|
|
44
|
+
// find nearest prime
|
|
45
|
+
var workLoad = data.workLoad;
|
|
46
|
+
for(var i = 0; i < workLoad; ++i) {
|
|
47
|
+
// do primality test
|
|
48
|
+
if(isProbablePrime(num)) {
|
|
49
|
+
return {found: true, prime: num.toString(16)};
|
|
50
|
+
}
|
|
51
|
+
// get next potential prime
|
|
52
|
+
num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {found: false};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function isProbablePrime(n) {
|
|
59
|
+
// divide by low primes, ignore even checks, etc (n already aligned properly)
|
|
60
|
+
var i = 1;
|
|
61
|
+
while(i < LOW_PRIMES.length) {
|
|
62
|
+
var m = LOW_PRIMES[i];
|
|
63
|
+
var j = i + 1;
|
|
64
|
+
while(j < LOW_PRIMES.length && m < LP_LIMIT) {
|
|
65
|
+
m *= LOW_PRIMES[j++];
|
|
66
|
+
}
|
|
67
|
+
m = n.modInt(m);
|
|
68
|
+
while(i < j) {
|
|
69
|
+
if(m % LOW_PRIMES[i++] === 0) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return runMillerRabin(n);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// HAC 4.24, Miller-Rabin
|
|
78
|
+
function runMillerRabin(n) {
|
|
79
|
+
// n1 = n - 1
|
|
80
|
+
var n1 = n.subtract(BigInteger.ONE);
|
|
81
|
+
|
|
82
|
+
// get s and d such that n1 = 2^s * d
|
|
83
|
+
var s = n1.getLowestSetBit();
|
|
84
|
+
if(s <= 0) {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
var d = n1.shiftRight(s);
|
|
88
|
+
|
|
89
|
+
var k = _getMillerRabinTests(n.bitLength());
|
|
90
|
+
var prng = getPrng();
|
|
91
|
+
var a;
|
|
92
|
+
for(var i = 0; i < k; ++i) {
|
|
93
|
+
// select witness 'a' at random from between 1 and n - 1
|
|
94
|
+
do {
|
|
95
|
+
a = new BigInteger(n.bitLength(), prng);
|
|
96
|
+
} while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0);
|
|
97
|
+
|
|
98
|
+
/* See if 'a' is a composite witness. */
|
|
99
|
+
|
|
100
|
+
// x = a^d mod n
|
|
101
|
+
var x = a.modPow(d, n);
|
|
102
|
+
|
|
103
|
+
// probably prime
|
|
104
|
+
if(x.compareTo(BigInteger.ONE) === 0 || x.compareTo(n1) === 0) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
var j = s;
|
|
109
|
+
while(--j) {
|
|
110
|
+
// x = x^2 mod a
|
|
111
|
+
x = x.modPowInt(2, n);
|
|
112
|
+
|
|
113
|
+
// 'n' is composite because no previous x == -1 mod n
|
|
114
|
+
if(x.compareTo(BigInteger.ONE) === 0) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
// x == -1 mod n, so probably prime
|
|
118
|
+
if(x.compareTo(n1) === 0) {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 'x' is first_x^(n1/2) and is not +/- 1, so 'n' is not prime
|
|
124
|
+
if(j === 0) {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// get pseudo random number generator
|
|
133
|
+
function getPrng() {
|
|
134
|
+
// create prng with api that matches BigInteger secure random
|
|
135
|
+
return {
|
|
136
|
+
// x is an array to fill with bytes
|
|
137
|
+
nextBytes: function(x) {
|
|
138
|
+
for(var i = 0; i < x.length; ++i) {
|
|
139
|
+
x[i] = Math.floor(Math.random() * 0xFF);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Returns the required number of Miller-Rabin tests to generate a
|
|
147
|
+
* prime with an error probability of (1/2)^80.
|
|
148
|
+
*
|
|
149
|
+
* See Handbook of Applied Cryptography Chapter 4, Table 4.4.
|
|
150
|
+
*
|
|
151
|
+
* @param bits the bit size.
|
|
152
|
+
*
|
|
153
|
+
* @return the required number of iterations.
|
|
154
|
+
*/
|
|
155
|
+
function _getMillerRabinTests(bits) {
|
|
156
|
+
if(bits <= 100) return 27;
|
|
157
|
+
if(bits <= 150) return 18;
|
|
158
|
+
if(bits <= 200) return 15;
|
|
159
|
+
if(bits <= 250) return 12;
|
|
160
|
+
if(bits <= 300) return 9;
|
|
161
|
+
if(bits <= 350) return 8;
|
|
162
|
+
if(bits <= 400) return 7;
|
|
163
|
+
if(bits <= 500) return 6;
|
|
164
|
+
if(bits <= 600) return 5;
|
|
165
|
+
if(bits <= 800) return 4;
|
|
166
|
+
if(bits <= 1250) return 3;
|
|
167
|
+
return 2;
|
|
168
|
+
}
|