1 |
|
2 | const globalThis = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | const SymbolPolyfill = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol' ?
|
9 | Symbol :
|
10 | description => `Symbol(${description})`;
|
11 |
|
12 |
|
13 | function noop() {
|
14 |
|
15 | }
|
16 |
|
17 |
|
18 |
|
19 | const NumberIsNaN = Number.isNaN || function (x) {
|
20 |
|
21 | return x !== x;
|
22 | };
|
23 |
|
24 | const rethrowAssertionErrorRejection = noop;
|
25 |
|
26 | function typeIsObject(x) {
|
27 | return (typeof x === 'object' && x !== null) || typeof x === 'function';
|
28 | }
|
29 | function createArrayFromList(elements) {
|
30 |
|
31 |
|
32 | return elements.slice();
|
33 | }
|
34 | function ArrayBufferCopy(dest, destOffset, src, srcOffset, n) {
|
35 | new Uint8Array(dest).set(new Uint8Array(src, srcOffset, n), destOffset);
|
36 | }
|
37 | function IsFiniteNonNegativeNumber(v) {
|
38 | if (IsNonNegativeNumber(v) === false) {
|
39 | return false;
|
40 | }
|
41 | if (v === Infinity) {
|
42 | return false;
|
43 | }
|
44 | return true;
|
45 | }
|
46 | function IsNonNegativeNumber(v) {
|
47 | if (typeof v !== 'number') {
|
48 | return false;
|
49 | }
|
50 | if (NumberIsNaN(v)) {
|
51 | return false;
|
52 | }
|
53 | if (v < 0) {
|
54 | return false;
|
55 | }
|
56 | return true;
|
57 | }
|
58 | function Call(F, V, args) {
|
59 | if (typeof F !== 'function') {
|
60 | throw new TypeError('Argument is not a function');
|
61 | }
|
62 | return Function.prototype.apply.call(F, V, args);
|
63 | }
|
64 | function CreateAlgorithmFromUnderlyingMethod(underlyingObject, methodName, algoArgCount, extraArgs) {
|
65 | const method = underlyingObject[methodName];
|
66 | if (method !== undefined) {
|
67 | if (typeof method !== 'function') {
|
68 | throw new TypeError(`${method} is not a method`);
|
69 | }
|
70 | switch (algoArgCount) {
|
71 | case 0: {
|
72 | return () => {
|
73 | return PromiseCall(method, underlyingObject, extraArgs);
|
74 | };
|
75 | }
|
76 | case 1: {
|
77 | return arg => {
|
78 | const fullArgs = [arg].concat(extraArgs);
|
79 | return PromiseCall(method, underlyingObject, fullArgs);
|
80 | };
|
81 | }
|
82 | }
|
83 | }
|
84 | return () => promiseResolvedWith(undefined);
|
85 | }
|
86 | function InvokeOrNoop(O, P, args) {
|
87 | const method = O[P];
|
88 | if (method === undefined) {
|
89 | return undefined;
|
90 | }
|
91 | return Call(method, O, args);
|
92 | }
|
93 | function PromiseCall(F, V, args) {
|
94 | try {
|
95 | return promiseResolvedWith(Call(F, V, args));
|
96 | }
|
97 | catch (value) {
|
98 | return promiseRejectedWith(value);
|
99 | }
|
100 | }
|
101 |
|
102 | function TransferArrayBuffer(O) {
|
103 | return O;
|
104 | }
|
105 |
|
106 | function IsDetachedBuffer(O) {
|
107 | return false;
|
108 | }
|
109 | function ValidateAndNormalizeHighWaterMark(highWaterMark) {
|
110 | highWaterMark = Number(highWaterMark);
|
111 | if (NumberIsNaN(highWaterMark) || highWaterMark < 0) {
|
112 | throw new RangeError('highWaterMark property of a queuing strategy must be non-negative and non-NaN');
|
113 | }
|
114 | return highWaterMark;
|
115 | }
|
116 | function MakeSizeAlgorithmFromSizeFunction(size) {
|
117 | if (size === undefined) {
|
118 | return () => 1;
|
119 | }
|
120 | if (typeof size !== 'function') {
|
121 | throw new TypeError('size property of a queuing strategy must be a function');
|
122 | }
|
123 | return chunk => size(chunk);
|
124 | }
|
125 | const originalPromise = Promise;
|
126 | const originalPromiseThen = Promise.prototype.then;
|
127 | const originalPromiseResolve = Promise.resolve.bind(originalPromise);
|
128 | const originalPromiseReject = Promise.reject.bind(originalPromise);
|
129 | function newPromise(executor) {
|
130 | return new originalPromise(executor);
|
131 | }
|
132 | function promiseResolvedWith(value) {
|
133 | return originalPromiseResolve(value);
|
134 | }
|
135 | function promiseRejectedWith(reason) {
|
136 | return originalPromiseReject(reason);
|
137 | }
|
138 | function PerformPromiseThen(promise, onFulfilled, onRejected) {
|
139 |
|
140 |
|
141 | return originalPromiseThen.call(promise, onFulfilled, onRejected);
|
142 | }
|
143 | function uponPromise(promise, onFulfilled, onRejected) {
|
144 | PerformPromiseThen(PerformPromiseThen(promise, onFulfilled, onRejected), undefined, rethrowAssertionErrorRejection);
|
145 | }
|
146 | function uponFulfillment(promise, onFulfilled) {
|
147 | uponPromise(promise, onFulfilled);
|
148 | }
|
149 | function uponRejection(promise, onRejected) {
|
150 | uponPromise(promise, undefined, onRejected);
|
151 | }
|
152 | function transformPromiseWith(promise, fulfillmentHandler, rejectionHandler) {
|
153 | return PerformPromiseThen(promise, fulfillmentHandler, rejectionHandler);
|
154 | }
|
155 | function setPromiseIsHandledToTrue(promise) {
|
156 | PerformPromiseThen(promise, undefined, rethrowAssertionErrorRejection);
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 | const QUEUE_MAX_ARRAY_SIZE = 16384;
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | class SimpleQueue {
|
169 | constructor() {
|
170 | this._cursor = 0;
|
171 | this._size = 0;
|
172 |
|
173 | this._front = {
|
174 | _elements: [],
|
175 | _next: undefined
|
176 | };
|
177 | this._back = this._front;
|
178 |
|
179 |
|
180 |
|
181 | this._cursor = 0;
|
182 |
|
183 | this._size = 0;
|
184 | }
|
185 | get length() {
|
186 | return this._size;
|
187 | }
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | push(element) {
|
193 | const oldBack = this._back;
|
194 | let newBack = oldBack;
|
195 | if (oldBack._elements.length === QUEUE_MAX_ARRAY_SIZE - 1) {
|
196 | newBack = {
|
197 | _elements: [],
|
198 | _next: undefined
|
199 | };
|
200 | }
|
201 |
|
202 |
|
203 | oldBack._elements.push(element);
|
204 | if (newBack !== oldBack) {
|
205 | this._back = newBack;
|
206 | oldBack._next = newBack;
|
207 | }
|
208 | ++this._size;
|
209 | }
|
210 |
|
211 |
|
212 | shift() {
|
213 | const oldFront = this._front;
|
214 | let newFront = oldFront;
|
215 | const oldCursor = this._cursor;
|
216 | let newCursor = oldCursor + 1;
|
217 | const elements = oldFront._elements;
|
218 | const element = elements[oldCursor];
|
219 | if (newCursor === QUEUE_MAX_ARRAY_SIZE) {
|
220 | newFront = oldFront._next;
|
221 | newCursor = 0;
|
222 | }
|
223 |
|
224 | --this._size;
|
225 | this._cursor = newCursor;
|
226 | if (oldFront !== newFront) {
|
227 | this._front = newFront;
|
228 | }
|
229 |
|
230 | elements[oldCursor] = undefined;
|
231 | return element;
|
232 | }
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 | forEach(callback) {
|
242 | let i = this._cursor;
|
243 | let node = this._front;
|
244 | let elements = node._elements;
|
245 | while (i !== elements.length || node._next !== undefined) {
|
246 | if (i === elements.length) {
|
247 | node = node._next;
|
248 | elements = node._elements;
|
249 | i = 0;
|
250 | if (elements.length === 0) {
|
251 | break;
|
252 | }
|
253 | }
|
254 | callback(elements[i]);
|
255 | ++i;
|
256 | }
|
257 | }
|
258 |
|
259 |
|
260 | peek() {
|
261 | const front = this._front;
|
262 | const cursor = this._cursor;
|
263 | return front._elements[cursor];
|
264 | }
|
265 | }
|
266 |
|
267 | function ReadableStreamCreateReadResult(value, done, forAuthorCode) {
|
268 | let prototype = null;
|
269 | if (forAuthorCode === true) {
|
270 | prototype = Object.prototype;
|
271 | }
|
272 | const obj = Object.create(prototype);
|
273 | obj.value = value;
|
274 | obj.done = done;
|
275 | return obj;
|
276 | }
|
277 | function ReadableStreamReaderGenericInitialize(reader, stream) {
|
278 | reader._forAuthorCode = true;
|
279 | reader._ownerReadableStream = stream;
|
280 | stream._reader = reader;
|
281 | if (stream._state === 'readable') {
|
282 | defaultReaderClosedPromiseInitialize(reader);
|
283 | }
|
284 | else if (stream._state === 'closed') {
|
285 | defaultReaderClosedPromiseInitializeAsResolved(reader);
|
286 | }
|
287 | else {
|
288 | defaultReaderClosedPromiseInitializeAsRejected(reader, stream._storedError);
|
289 | }
|
290 | }
|
291 |
|
292 |
|
293 | function ReadableStreamReaderGenericCancel(reader, reason) {
|
294 | const stream = reader._ownerReadableStream;
|
295 | return ReadableStreamCancel(stream, reason);
|
296 | }
|
297 | function ReadableStreamReaderGenericRelease(reader) {
|
298 | if (reader._ownerReadableStream._state === 'readable') {
|
299 | defaultReaderClosedPromiseReject(reader, new TypeError('Reader was released and can no longer be used to monitor the stream\'s closedness'));
|
300 | }
|
301 | else {
|
302 | defaultReaderClosedPromiseResetToRejected(reader, new TypeError('Reader was released and can no longer be used to monitor the stream\'s closedness'));
|
303 | }
|
304 | reader._ownerReadableStream._reader = undefined;
|
305 | reader._ownerReadableStream = undefined;
|
306 | }
|
307 |
|
308 | function readerLockException(name) {
|
309 | return new TypeError('Cannot ' + name + ' a stream using a released reader');
|
310 | }
|
311 |
|
312 | function defaultReaderClosedPromiseInitialize(reader) {
|
313 | reader._closedPromise = newPromise((resolve, reject) => {
|
314 | reader._closedPromise_resolve = resolve;
|
315 | reader._closedPromise_reject = reject;
|
316 | });
|
317 | }
|
318 | function defaultReaderClosedPromiseInitializeAsRejected(reader, reason) {
|
319 | defaultReaderClosedPromiseInitialize(reader);
|
320 | defaultReaderClosedPromiseReject(reader, reason);
|
321 | }
|
322 | function defaultReaderClosedPromiseInitializeAsResolved(reader) {
|
323 | defaultReaderClosedPromiseInitialize(reader);
|
324 | defaultReaderClosedPromiseResolve(reader);
|
325 | }
|
326 | function defaultReaderClosedPromiseReject(reader, reason) {
|
327 | setPromiseIsHandledToTrue(reader._closedPromise);
|
328 | reader._closedPromise_reject(reason);
|
329 | reader._closedPromise_resolve = undefined;
|
330 | reader._closedPromise_reject = undefined;
|
331 | }
|
332 | function defaultReaderClosedPromiseResetToRejected(reader, reason) {
|
333 | defaultReaderClosedPromiseInitializeAsRejected(reader, reason);
|
334 | }
|
335 | function defaultReaderClosedPromiseResolve(reader) {
|
336 | reader._closedPromise_resolve(undefined);
|
337 | reader._closedPromise_resolve = undefined;
|
338 | reader._closedPromise_reject = undefined;
|
339 | }
|
340 |
|
341 | const CancelSteps = SymbolPolyfill('[[CancelSteps]]');
|
342 | const PullSteps = SymbolPolyfill('[[PullSteps]]');
|
343 |
|
344 |
|
345 | function AcquireReadableStreamDefaultReader(stream, forAuthorCode = false) {
|
346 | const reader = new ReadableStreamDefaultReader(stream);
|
347 | reader._forAuthorCode = forAuthorCode;
|
348 | return reader;
|
349 | }
|
350 |
|
351 | function ReadableStreamAddReadRequest(stream) {
|
352 | const promise = newPromise((resolve, reject) => {
|
353 | const readRequest = {
|
354 | _resolve: resolve,
|
355 | _reject: reject
|
356 | };
|
357 | stream._reader._readRequests.push(readRequest);
|
358 | });
|
359 | return promise;
|
360 | }
|
361 | function ReadableStreamFulfillReadRequest(stream, chunk, done) {
|
362 | const reader = stream._reader;
|
363 | const readRequest = reader._readRequests.shift();
|
364 | readRequest._resolve(ReadableStreamCreateReadResult(chunk, done, reader._forAuthorCode));
|
365 | }
|
366 | function ReadableStreamGetNumReadRequests(stream) {
|
367 | return stream._reader._readRequests.length;
|
368 | }
|
369 | function ReadableStreamHasDefaultReader(stream) {
|
370 | const reader = stream._reader;
|
371 | if (reader === undefined) {
|
372 | return false;
|
373 | }
|
374 | if (!IsReadableStreamDefaultReader(reader)) {
|
375 | return false;
|
376 | }
|
377 | return true;
|
378 | }
|
379 | class ReadableStreamDefaultReader {
|
380 | constructor(stream) {
|
381 | if (IsReadableStream(stream) === false) {
|
382 | throw new TypeError('ReadableStreamDefaultReader can only be constructed with a ReadableStream instance');
|
383 | }
|
384 | if (IsReadableStreamLocked(stream) === true) {
|
385 | throw new TypeError('This stream has already been locked for exclusive reading by another reader');
|
386 | }
|
387 | ReadableStreamReaderGenericInitialize(this, stream);
|
388 | this._readRequests = new SimpleQueue();
|
389 | }
|
390 | get closed() {
|
391 | if (!IsReadableStreamDefaultReader(this)) {
|
392 | return promiseRejectedWith(defaultReaderBrandCheckException('closed'));
|
393 | }
|
394 | return this._closedPromise;
|
395 | }
|
396 | cancel(reason) {
|
397 | if (!IsReadableStreamDefaultReader(this)) {
|
398 | return promiseRejectedWith(defaultReaderBrandCheckException('cancel'));
|
399 | }
|
400 | if (this._ownerReadableStream === undefined) {
|
401 | return promiseRejectedWith(readerLockException('cancel'));
|
402 | }
|
403 | return ReadableStreamReaderGenericCancel(this, reason);
|
404 | }
|
405 | read() {
|
406 | if (!IsReadableStreamDefaultReader(this)) {
|
407 | return promiseRejectedWith(defaultReaderBrandCheckException('read'));
|
408 | }
|
409 | if (this._ownerReadableStream === undefined) {
|
410 | return promiseRejectedWith(readerLockException('read from'));
|
411 | }
|
412 | return ReadableStreamDefaultReaderRead(this);
|
413 | }
|
414 | releaseLock() {
|
415 | if (!IsReadableStreamDefaultReader(this)) {
|
416 | throw defaultReaderBrandCheckException('releaseLock');
|
417 | }
|
418 | if (this._ownerReadableStream === undefined) {
|
419 | return;
|
420 | }
|
421 | if (this._readRequests.length > 0) {
|
422 | throw new TypeError('Tried to release a reader lock when that reader has pending read() calls un-settled');
|
423 | }
|
424 | ReadableStreamReaderGenericRelease(this);
|
425 | }
|
426 | }
|
427 |
|
428 | function IsReadableStreamDefaultReader(x) {
|
429 | if (!typeIsObject(x)) {
|
430 | return false;
|
431 | }
|
432 | if (!Object.prototype.hasOwnProperty.call(x, '_readRequests')) {
|
433 | return false;
|
434 | }
|
435 | return true;
|
436 | }
|
437 | function ReadableStreamDefaultReaderRead(reader) {
|
438 | const stream = reader._ownerReadableStream;
|
439 | stream._disturbed = true;
|
440 | if (stream._state === 'closed') {
|
441 | return promiseResolvedWith(ReadableStreamCreateReadResult(undefined, true, reader._forAuthorCode));
|
442 | }
|
443 | if (stream._state === 'errored') {
|
444 | return promiseRejectedWith(stream._storedError);
|
445 | }
|
446 | return stream._readableStreamController[PullSteps]();
|
447 | }
|
448 |
|
449 | function defaultReaderBrandCheckException(name) {
|
450 | return new TypeError(`ReadableStreamDefaultReader.prototype.${name} can only be used on a ReadableStreamDefaultReader`);
|
451 | }
|
452 |
|
453 |
|
454 | let AsyncIteratorPrototype;
|
455 | if (typeof SymbolPolyfill.asyncIterator === 'symbol') {
|
456 |
|
457 |
|
458 | AsyncIteratorPrototype = {
|
459 |
|
460 |
|
461 | [SymbolPolyfill.asyncIterator]() {
|
462 | return this;
|
463 | }
|
464 | };
|
465 | Object.defineProperty(AsyncIteratorPrototype, SymbolPolyfill.asyncIterator, { enumerable: false });
|
466 | }
|
467 |
|
468 |
|
469 | const ReadableStreamAsyncIteratorPrototype = {
|
470 | next() {
|
471 | if (IsReadableStreamAsyncIterator(this) === false) {
|
472 | return promiseRejectedWith(streamAsyncIteratorBrandCheckException('next'));
|
473 | }
|
474 | const reader = this._asyncIteratorReader;
|
475 | if (reader._ownerReadableStream === undefined) {
|
476 | return promiseRejectedWith(readerLockException('iterate'));
|
477 | }
|
478 | return transformPromiseWith(ReadableStreamDefaultReaderRead(reader), result => {
|
479 | const done = result.done;
|
480 | if (done) {
|
481 | ReadableStreamReaderGenericRelease(reader);
|
482 | }
|
483 | const value = result.value;
|
484 | return ReadableStreamCreateReadResult(value, done, true);
|
485 | });
|
486 | },
|
487 | return(value) {
|
488 | if (IsReadableStreamAsyncIterator(this) === false) {
|
489 | return promiseRejectedWith(streamAsyncIteratorBrandCheckException('next'));
|
490 | }
|
491 | const reader = this._asyncIteratorReader;
|
492 | if (reader._ownerReadableStream === undefined) {
|
493 | return promiseRejectedWith(readerLockException('finish iterating'));
|
494 | }
|
495 | if (reader._readRequests.length > 0) {
|
496 | return promiseRejectedWith(new TypeError('Tried to release a reader lock when that reader has pending read() calls un-settled'));
|
497 | }
|
498 | if (this._preventCancel === false) {
|
499 | const result = ReadableStreamReaderGenericCancel(reader, value);
|
500 | ReadableStreamReaderGenericRelease(reader);
|
501 | return transformPromiseWith(result, () => ReadableStreamCreateReadResult(value, true, true));
|
502 | }
|
503 | ReadableStreamReaderGenericRelease(reader);
|
504 | return promiseResolvedWith(ReadableStreamCreateReadResult(value, true, true));
|
505 | }
|
506 | };
|
507 | if (AsyncIteratorPrototype !== undefined) {
|
508 | Object.setPrototypeOf(ReadableStreamAsyncIteratorPrototype, AsyncIteratorPrototype);
|
509 | }
|
510 | Object.defineProperty(ReadableStreamAsyncIteratorPrototype, 'next', { enumerable: false });
|
511 | Object.defineProperty(ReadableStreamAsyncIteratorPrototype, 'return', { enumerable: false });
|
512 |
|
513 | function AcquireReadableStreamAsyncIterator(stream, preventCancel = false) {
|
514 | const reader = AcquireReadableStreamDefaultReader(stream);
|
515 | const iterator = Object.create(ReadableStreamAsyncIteratorPrototype);
|
516 | iterator._asyncIteratorReader = reader;
|
517 | iterator._preventCancel = Boolean(preventCancel);
|
518 | return iterator;
|
519 | }
|
520 | function IsReadableStreamAsyncIterator(x) {
|
521 | if (!typeIsObject(x)) {
|
522 | return false;
|
523 | }
|
524 | if (!Object.prototype.hasOwnProperty.call(x, '_asyncIteratorReader')) {
|
525 | return false;
|
526 | }
|
527 | return true;
|
528 | }
|
529 |
|
530 | function streamAsyncIteratorBrandCheckException(name) {
|
531 | return new TypeError(`ReadableStreamAsyncIterator.${name} can only be used on a ReadableSteamAsyncIterator`);
|
532 | }
|
533 |
|
534 | function DequeueValue(container) {
|
535 | const pair = container._queue.shift();
|
536 | container._queueTotalSize -= pair.size;
|
537 | if (container._queueTotalSize < 0) {
|
538 | container._queueTotalSize = 0;
|
539 | }
|
540 | return pair.value;
|
541 | }
|
542 | function EnqueueValueWithSize(container, value, size) {
|
543 | size = Number(size);
|
544 | if (!IsFiniteNonNegativeNumber(size)) {
|
545 | throw new RangeError('Size must be a finite, non-NaN, non-negative number.');
|
546 | }
|
547 | container._queue.push({ value, size });
|
548 | container._queueTotalSize += size;
|
549 | }
|
550 | function PeekQueueValue(container) {
|
551 | const pair = container._queue.peek();
|
552 | return pair.value;
|
553 | }
|
554 | function ResetQueue(container) {
|
555 | container._queue = new SimpleQueue();
|
556 | container._queueTotalSize = 0;
|
557 | }
|
558 |
|
559 | const AbortSteps = SymbolPolyfill('[[AbortSteps]]');
|
560 | const ErrorSteps = SymbolPolyfill('[[ErrorSteps]]');
|
561 | class WritableStream {
|
562 | constructor(underlyingSink = {}, strategy = {}) {
|
563 | InitializeWritableStream(this);
|
564 | const size = strategy.size;
|
565 | let highWaterMark = strategy.highWaterMark;
|
566 | const type = underlyingSink.type;
|
567 | if (type !== undefined) {
|
568 | throw new RangeError('Invalid type is specified');
|
569 | }
|
570 | const sizeAlgorithm = MakeSizeAlgorithmFromSizeFunction(size);
|
571 | if (highWaterMark === undefined) {
|
572 | highWaterMark = 1;
|
573 | }
|
574 | highWaterMark = ValidateAndNormalizeHighWaterMark(highWaterMark);
|
575 | SetUpWritableStreamDefaultControllerFromUnderlyingSink(this, underlyingSink, highWaterMark, sizeAlgorithm);
|
576 | }
|
577 | get locked() {
|
578 | if (IsWritableStream(this) === false) {
|
579 | throw streamBrandCheckException('locked');
|
580 | }
|
581 | return IsWritableStreamLocked(this);
|
582 | }
|
583 | abort(reason) {
|
584 | if (IsWritableStream(this) === false) {
|
585 | return promiseRejectedWith(streamBrandCheckException('abort'));
|
586 | }
|
587 | if (IsWritableStreamLocked(this) === true) {
|
588 | return promiseRejectedWith(new TypeError('Cannot abort a stream that already has a writer'));
|
589 | }
|
590 | return WritableStreamAbort(this, reason);
|
591 | }
|
592 | close() {
|
593 | if (IsWritableStream(this) === false) {
|
594 | return promiseRejectedWith(streamBrandCheckException('close'));
|
595 | }
|
596 | if (IsWritableStreamLocked(this) === true) {
|
597 | return promiseRejectedWith(new TypeError('Cannot close a stream that already has a writer'));
|
598 | }
|
599 | if (WritableStreamCloseQueuedOrInFlight(this) === true) {
|
600 | return promiseRejectedWith(new TypeError('Cannot close an already-closing stream'));
|
601 | }
|
602 | return WritableStreamClose(this);
|
603 | }
|
604 | getWriter() {
|
605 | if (IsWritableStream(this) === false) {
|
606 | throw streamBrandCheckException('getWriter');
|
607 | }
|
608 | return AcquireWritableStreamDefaultWriter(this);
|
609 | }
|
610 | }
|
611 |
|
612 | function AcquireWritableStreamDefaultWriter(stream) {
|
613 | return new WritableStreamDefaultWriter(stream);
|
614 | }
|
615 |
|
616 | function CreateWritableStream(startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark = 1, sizeAlgorithm = () => 1) {
|
617 | const stream = Object.create(WritableStream.prototype);
|
618 | InitializeWritableStream(stream);
|
619 | const controller = Object.create(WritableStreamDefaultController.prototype);
|
620 | SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm);
|
621 | return stream;
|
622 | }
|
623 | function InitializeWritableStream(stream) {
|
624 | stream._state = 'writable';
|
625 |
|
626 |
|
627 | stream._storedError = undefined;
|
628 | stream._writer = undefined;
|
629 |
|
630 |
|
631 | stream._writableStreamController = undefined;
|
632 |
|
633 |
|
634 | stream._writeRequests = new SimpleQueue();
|
635 |
|
636 |
|
637 | stream._inFlightWriteRequest = undefined;
|
638 |
|
639 |
|
640 | stream._closeRequest = undefined;
|
641 |
|
642 |
|
643 | stream._inFlightCloseRequest = undefined;
|
644 |
|
645 | stream._pendingAbortRequest = undefined;
|
646 |
|
647 | stream._backpressure = false;
|
648 | }
|
649 | function IsWritableStream(x) {
|
650 | if (!typeIsObject(x)) {
|
651 | return false;
|
652 | }
|
653 | if (!Object.prototype.hasOwnProperty.call(x, '_writableStreamController')) {
|
654 | return false;
|
655 | }
|
656 | return true;
|
657 | }
|
658 | function IsWritableStreamLocked(stream) {
|
659 | if (stream._writer === undefined) {
|
660 | return false;
|
661 | }
|
662 | return true;
|
663 | }
|
664 | function WritableStreamAbort(stream, reason) {
|
665 | const state = stream._state;
|
666 | if (state === 'closed' || state === 'errored') {
|
667 | return promiseResolvedWith(undefined);
|
668 | }
|
669 | if (stream._pendingAbortRequest !== undefined) {
|
670 | return stream._pendingAbortRequest._promise;
|
671 | }
|
672 | let wasAlreadyErroring = false;
|
673 | if (state === 'erroring') {
|
674 | wasAlreadyErroring = true;
|
675 |
|
676 | reason = undefined;
|
677 | }
|
678 | const promise = newPromise((resolve, reject) => {
|
679 | stream._pendingAbortRequest = {
|
680 | _promise: undefined,
|
681 | _resolve: resolve,
|
682 | _reject: reject,
|
683 | _reason: reason,
|
684 | _wasAlreadyErroring: wasAlreadyErroring
|
685 | };
|
686 | });
|
687 | stream._pendingAbortRequest._promise = promise;
|
688 | if (wasAlreadyErroring === false) {
|
689 | WritableStreamStartErroring(stream, reason);
|
690 | }
|
691 | return promise;
|
692 | }
|
693 | function WritableStreamClose(stream) {
|
694 | const state = stream._state;
|
695 | if (state === 'closed' || state === 'errored') {
|
696 | return promiseRejectedWith(new TypeError(`The stream (in ${state} state) is not in the writable state and cannot be closed`));
|
697 | }
|
698 | const promise = newPromise((resolve, reject) => {
|
699 | const closeRequest = {
|
700 | _resolve: resolve,
|
701 | _reject: reject
|
702 | };
|
703 | stream._closeRequest = closeRequest;
|
704 | });
|
705 | const writer = stream._writer;
|
706 | if (writer !== undefined && stream._backpressure === true && state === 'writable') {
|
707 | defaultWriterReadyPromiseResolve(writer);
|
708 | }
|
709 | WritableStreamDefaultControllerClose(stream._writableStreamController);
|
710 | return promise;
|
711 | }
|
712 |
|
713 | function WritableStreamAddWriteRequest(stream) {
|
714 | const promise = newPromise((resolve, reject) => {
|
715 | const writeRequest = {
|
716 | _resolve: resolve,
|
717 | _reject: reject
|
718 | };
|
719 | stream._writeRequests.push(writeRequest);
|
720 | });
|
721 | return promise;
|
722 | }
|
723 | function WritableStreamDealWithRejection(stream, error) {
|
724 | const state = stream._state;
|
725 | if (state === 'writable') {
|
726 | WritableStreamStartErroring(stream, error);
|
727 | return;
|
728 | }
|
729 | WritableStreamFinishErroring(stream);
|
730 | }
|
731 | function WritableStreamStartErroring(stream, reason) {
|
732 | const controller = stream._writableStreamController;
|
733 | stream._state = 'erroring';
|
734 | stream._storedError = reason;
|
735 | const writer = stream._writer;
|
736 | if (writer !== undefined) {
|
737 | WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, reason);
|
738 | }
|
739 | if (WritableStreamHasOperationMarkedInFlight(stream) === false && controller._started === true) {
|
740 | WritableStreamFinishErroring(stream);
|
741 | }
|
742 | }
|
743 | function WritableStreamFinishErroring(stream) {
|
744 | stream._state = 'errored';
|
745 | stream._writableStreamController[ErrorSteps]();
|
746 | const storedError = stream._storedError;
|
747 | stream._writeRequests.forEach(writeRequest => {
|
748 | writeRequest._reject(storedError);
|
749 | });
|
750 | stream._writeRequests = new SimpleQueue();
|
751 | if (stream._pendingAbortRequest === undefined) {
|
752 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
753 | return;
|
754 | }
|
755 | const abortRequest = stream._pendingAbortRequest;
|
756 | stream._pendingAbortRequest = undefined;
|
757 | if (abortRequest._wasAlreadyErroring === true) {
|
758 | abortRequest._reject(storedError);
|
759 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
760 | return;
|
761 | }
|
762 | const promise = stream._writableStreamController[AbortSteps](abortRequest._reason);
|
763 | uponPromise(promise, () => {
|
764 | abortRequest._resolve();
|
765 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
766 | }, (reason) => {
|
767 | abortRequest._reject(reason);
|
768 | WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream);
|
769 | });
|
770 | }
|
771 | function WritableStreamFinishInFlightWrite(stream) {
|
772 | stream._inFlightWriteRequest._resolve(undefined);
|
773 | stream._inFlightWriteRequest = undefined;
|
774 | }
|
775 | function WritableStreamFinishInFlightWriteWithError(stream, error) {
|
776 | stream._inFlightWriteRequest._reject(error);
|
777 | stream._inFlightWriteRequest = undefined;
|
778 | WritableStreamDealWithRejection(stream, error);
|
779 | }
|
780 | function WritableStreamFinishInFlightClose(stream) {
|
781 | stream._inFlightCloseRequest._resolve(undefined);
|
782 | stream._inFlightCloseRequest = undefined;
|
783 | const state = stream._state;
|
784 | if (state === 'erroring') {
|
785 |
|
786 | stream._storedError = undefined;
|
787 | if (stream._pendingAbortRequest !== undefined) {
|
788 | stream._pendingAbortRequest._resolve();
|
789 | stream._pendingAbortRequest = undefined;
|
790 | }
|
791 | }
|
792 | stream._state = 'closed';
|
793 | const writer = stream._writer;
|
794 | if (writer !== undefined) {
|
795 | defaultWriterClosedPromiseResolve(writer);
|
796 | }
|
797 | }
|
798 | function WritableStreamFinishInFlightCloseWithError(stream, error) {
|
799 | stream._inFlightCloseRequest._reject(error);
|
800 | stream._inFlightCloseRequest = undefined;
|
801 |
|
802 | if (stream._pendingAbortRequest !== undefined) {
|
803 | stream._pendingAbortRequest._reject(error);
|
804 | stream._pendingAbortRequest = undefined;
|
805 | }
|
806 | WritableStreamDealWithRejection(stream, error);
|
807 | }
|
808 |
|
809 | function WritableStreamCloseQueuedOrInFlight(stream) {
|
810 | if (stream._closeRequest === undefined && stream._inFlightCloseRequest === undefined) {
|
811 | return false;
|
812 | }
|
813 | return true;
|
814 | }
|
815 | function WritableStreamHasOperationMarkedInFlight(stream) {
|
816 | if (stream._inFlightWriteRequest === undefined && stream._inFlightCloseRequest === undefined) {
|
817 | return false;
|
818 | }
|
819 | return true;
|
820 | }
|
821 | function WritableStreamMarkCloseRequestInFlight(stream) {
|
822 | stream._inFlightCloseRequest = stream._closeRequest;
|
823 | stream._closeRequest = undefined;
|
824 | }
|
825 | function WritableStreamMarkFirstWriteRequestInFlight(stream) {
|
826 | stream._inFlightWriteRequest = stream._writeRequests.shift();
|
827 | }
|
828 | function WritableStreamRejectCloseAndClosedPromiseIfNeeded(stream) {
|
829 | if (stream._closeRequest !== undefined) {
|
830 | stream._closeRequest._reject(stream._storedError);
|
831 | stream._closeRequest = undefined;
|
832 | }
|
833 | const writer = stream._writer;
|
834 | if (writer !== undefined) {
|
835 | defaultWriterClosedPromiseReject(writer, stream._storedError);
|
836 | }
|
837 | }
|
838 | function WritableStreamUpdateBackpressure(stream, backpressure) {
|
839 | const writer = stream._writer;
|
840 | if (writer !== undefined && backpressure !== stream._backpressure) {
|
841 | if (backpressure === true) {
|
842 | defaultWriterReadyPromiseReset(writer);
|
843 | }
|
844 | else {
|
845 | defaultWriterReadyPromiseResolve(writer);
|
846 | }
|
847 | }
|
848 | stream._backpressure = backpressure;
|
849 | }
|
850 | class WritableStreamDefaultWriter {
|
851 | constructor(stream) {
|
852 | if (IsWritableStream(stream) === false) {
|
853 | throw new TypeError('WritableStreamDefaultWriter can only be constructed with a WritableStream instance');
|
854 | }
|
855 | if (IsWritableStreamLocked(stream) === true) {
|
856 | throw new TypeError('This stream has already been locked for exclusive writing by another writer');
|
857 | }
|
858 | this._ownerWritableStream = stream;
|
859 | stream._writer = this;
|
860 | const state = stream._state;
|
861 | if (state === 'writable') {
|
862 | if (WritableStreamCloseQueuedOrInFlight(stream) === false && stream._backpressure === true) {
|
863 | defaultWriterReadyPromiseInitialize(this);
|
864 | }
|
865 | else {
|
866 | defaultWriterReadyPromiseInitializeAsResolved(this);
|
867 | }
|
868 | defaultWriterClosedPromiseInitialize(this);
|
869 | }
|
870 | else if (state === 'erroring') {
|
871 | defaultWriterReadyPromiseInitializeAsRejected(this, stream._storedError);
|
872 | defaultWriterClosedPromiseInitialize(this);
|
873 | }
|
874 | else if (state === 'closed') {
|
875 | defaultWriterReadyPromiseInitializeAsResolved(this);
|
876 | defaultWriterClosedPromiseInitializeAsResolved(this);
|
877 | }
|
878 | else {
|
879 | const storedError = stream._storedError;
|
880 | defaultWriterReadyPromiseInitializeAsRejected(this, storedError);
|
881 | defaultWriterClosedPromiseInitializeAsRejected(this, storedError);
|
882 | }
|
883 | }
|
884 | get closed() {
|
885 | if (IsWritableStreamDefaultWriter(this) === false) {
|
886 | return promiseRejectedWith(defaultWriterBrandCheckException('closed'));
|
887 | }
|
888 | return this._closedPromise;
|
889 | }
|
890 | get desiredSize() {
|
891 | if (IsWritableStreamDefaultWriter(this) === false) {
|
892 | throw defaultWriterBrandCheckException('desiredSize');
|
893 | }
|
894 | if (this._ownerWritableStream === undefined) {
|
895 | throw defaultWriterLockException('desiredSize');
|
896 | }
|
897 | return WritableStreamDefaultWriterGetDesiredSize(this);
|
898 | }
|
899 | get ready() {
|
900 | if (IsWritableStreamDefaultWriter(this) === false) {
|
901 | return promiseRejectedWith(defaultWriterBrandCheckException('ready'));
|
902 | }
|
903 | return this._readyPromise;
|
904 | }
|
905 | abort(reason) {
|
906 | if (IsWritableStreamDefaultWriter(this) === false) {
|
907 | return promiseRejectedWith(defaultWriterBrandCheckException('abort'));
|
908 | }
|
909 | if (this._ownerWritableStream === undefined) {
|
910 | return promiseRejectedWith(defaultWriterLockException('abort'));
|
911 | }
|
912 | return WritableStreamDefaultWriterAbort(this, reason);
|
913 | }
|
914 | close() {
|
915 | if (IsWritableStreamDefaultWriter(this) === false) {
|
916 | return promiseRejectedWith(defaultWriterBrandCheckException('close'));
|
917 | }
|
918 | const stream = this._ownerWritableStream;
|
919 | if (stream === undefined) {
|
920 | return promiseRejectedWith(defaultWriterLockException('close'));
|
921 | }
|
922 | if (WritableStreamCloseQueuedOrInFlight(stream) === true) {
|
923 | return promiseRejectedWith(new TypeError('Cannot close an already-closing stream'));
|
924 | }
|
925 | return WritableStreamDefaultWriterClose(this);
|
926 | }
|
927 | releaseLock() {
|
928 | if (IsWritableStreamDefaultWriter(this) === false) {
|
929 | throw defaultWriterBrandCheckException('releaseLock');
|
930 | }
|
931 | const stream = this._ownerWritableStream;
|
932 | if (stream === undefined) {
|
933 | return;
|
934 | }
|
935 | WritableStreamDefaultWriterRelease(this);
|
936 | }
|
937 | write(chunk) {
|
938 | if (IsWritableStreamDefaultWriter(this) === false) {
|
939 | return promiseRejectedWith(defaultWriterBrandCheckException('write'));
|
940 | }
|
941 | if (this._ownerWritableStream === undefined) {
|
942 | return promiseRejectedWith(defaultWriterLockException('write to'));
|
943 | }
|
944 | return WritableStreamDefaultWriterWrite(this, chunk);
|
945 | }
|
946 | }
|
947 |
|
948 | function IsWritableStreamDefaultWriter(x) {
|
949 | if (!typeIsObject(x)) {
|
950 | return false;
|
951 | }
|
952 | if (!Object.prototype.hasOwnProperty.call(x, '_ownerWritableStream')) {
|
953 | return false;
|
954 | }
|
955 | return true;
|
956 | }
|
957 |
|
958 | function WritableStreamDefaultWriterAbort(writer, reason) {
|
959 | const stream = writer._ownerWritableStream;
|
960 | return WritableStreamAbort(stream, reason);
|
961 | }
|
962 | function WritableStreamDefaultWriterClose(writer) {
|
963 | const stream = writer._ownerWritableStream;
|
964 | return WritableStreamClose(stream);
|
965 | }
|
966 | function WritableStreamDefaultWriterCloseWithErrorPropagation(writer) {
|
967 | const stream = writer._ownerWritableStream;
|
968 | const state = stream._state;
|
969 | if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {
|
970 | return promiseResolvedWith(undefined);
|
971 | }
|
972 | if (state === 'errored') {
|
973 | return promiseRejectedWith(stream._storedError);
|
974 | }
|
975 | return WritableStreamDefaultWriterClose(writer);
|
976 | }
|
977 | function WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, error) {
|
978 | if (writer._closedPromiseState === 'pending') {
|
979 | defaultWriterClosedPromiseReject(writer, error);
|
980 | }
|
981 | else {
|
982 | defaultWriterClosedPromiseResetToRejected(writer, error);
|
983 | }
|
984 | }
|
985 | function WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, error) {
|
986 | if (writer._readyPromiseState === 'pending') {
|
987 | defaultWriterReadyPromiseReject(writer, error);
|
988 | }
|
989 | else {
|
990 | defaultWriterReadyPromiseResetToRejected(writer, error);
|
991 | }
|
992 | }
|
993 | function WritableStreamDefaultWriterGetDesiredSize(writer) {
|
994 | const stream = writer._ownerWritableStream;
|
995 | const state = stream._state;
|
996 | if (state === 'errored' || state === 'erroring') {
|
997 | return null;
|
998 | }
|
999 | if (state === 'closed') {
|
1000 | return 0;
|
1001 | }
|
1002 | return WritableStreamDefaultControllerGetDesiredSize(stream._writableStreamController);
|
1003 | }
|
1004 | function WritableStreamDefaultWriterRelease(writer) {
|
1005 | const stream = writer._ownerWritableStream;
|
1006 | const releasedError = new TypeError('Writer was released and can no longer be used to monitor the stream\'s closedness');
|
1007 | WritableStreamDefaultWriterEnsureReadyPromiseRejected(writer, releasedError);
|
1008 |
|
1009 |
|
1010 | WritableStreamDefaultWriterEnsureClosedPromiseRejected(writer, releasedError);
|
1011 | stream._writer = undefined;
|
1012 | writer._ownerWritableStream = undefined;
|
1013 | }
|
1014 | function WritableStreamDefaultWriterWrite(writer, chunk) {
|
1015 | const stream = writer._ownerWritableStream;
|
1016 | const controller = stream._writableStreamController;
|
1017 | const chunkSize = WritableStreamDefaultControllerGetChunkSize(controller, chunk);
|
1018 | if (stream !== writer._ownerWritableStream) {
|
1019 | return promiseRejectedWith(defaultWriterLockException('write to'));
|
1020 | }
|
1021 | const state = stream._state;
|
1022 | if (state === 'errored') {
|
1023 | return promiseRejectedWith(stream._storedError);
|
1024 | }
|
1025 | if (WritableStreamCloseQueuedOrInFlight(stream) === true || state === 'closed') {
|
1026 | return promiseRejectedWith(new TypeError('The stream is closing or closed and cannot be written to'));
|
1027 | }
|
1028 | if (state === 'erroring') {
|
1029 | return promiseRejectedWith(stream._storedError);
|
1030 | }
|
1031 | const promise = WritableStreamAddWriteRequest(stream);
|
1032 | WritableStreamDefaultControllerWrite(controller, chunk, chunkSize);
|
1033 | return promise;
|
1034 | }
|
1035 | class WritableStreamDefaultController {
|
1036 |
|
1037 | constructor() {
|
1038 | throw new TypeError('WritableStreamDefaultController cannot be constructed explicitly');
|
1039 | }
|
1040 | error(e) {
|
1041 | if (IsWritableStreamDefaultController(this) === false) {
|
1042 | throw new TypeError('WritableStreamDefaultController.prototype.error can only be used on a WritableStreamDefaultController');
|
1043 | }
|
1044 | const state = this._controlledWritableStream._state;
|
1045 | if (state !== 'writable') {
|
1046 |
|
1047 |
|
1048 | return;
|
1049 | }
|
1050 | WritableStreamDefaultControllerError(this, e);
|
1051 | }
|
1052 |
|
1053 | [AbortSteps](reason) {
|
1054 | const result = this._abortAlgorithm(reason);
|
1055 | WritableStreamDefaultControllerClearAlgorithms(this);
|
1056 | return result;
|
1057 | }
|
1058 |
|
1059 | [ErrorSteps]() {
|
1060 | ResetQueue(this);
|
1061 | }
|
1062 | }
|
1063 |
|
1064 | function IsWritableStreamDefaultController(x) {
|
1065 | if (!typeIsObject(x)) {
|
1066 | return false;
|
1067 | }
|
1068 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledWritableStream')) {
|
1069 | return false;
|
1070 | }
|
1071 | return true;
|
1072 | }
|
1073 | function SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm) {
|
1074 | controller._controlledWritableStream = stream;
|
1075 | stream._writableStreamController = controller;
|
1076 |
|
1077 | controller._queue = undefined;
|
1078 | controller._queueTotalSize = undefined;
|
1079 | ResetQueue(controller);
|
1080 | controller._started = false;
|
1081 | controller._strategySizeAlgorithm = sizeAlgorithm;
|
1082 | controller._strategyHWM = highWaterMark;
|
1083 | controller._writeAlgorithm = writeAlgorithm;
|
1084 | controller._closeAlgorithm = closeAlgorithm;
|
1085 | controller._abortAlgorithm = abortAlgorithm;
|
1086 | const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
|
1087 | WritableStreamUpdateBackpressure(stream, backpressure);
|
1088 | const startResult = startAlgorithm();
|
1089 | const startPromise = promiseResolvedWith(startResult);
|
1090 | uponPromise(startPromise, () => {
|
1091 | controller._started = true;
|
1092 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
1093 | }, r => {
|
1094 | controller._started = true;
|
1095 | WritableStreamDealWithRejection(stream, r);
|
1096 | });
|
1097 | }
|
1098 | function SetUpWritableStreamDefaultControllerFromUnderlyingSink(stream, underlyingSink, highWaterMark, sizeAlgorithm) {
|
1099 | const controller = Object.create(WritableStreamDefaultController.prototype);
|
1100 | function startAlgorithm() {
|
1101 | return InvokeOrNoop(underlyingSink, 'start', [controller]);
|
1102 | }
|
1103 | const writeAlgorithm = CreateAlgorithmFromUnderlyingMethod(underlyingSink, 'write', 1, [controller]);
|
1104 | const closeAlgorithm = CreateAlgorithmFromUnderlyingMethod(underlyingSink, 'close', 0, []);
|
1105 | const abortAlgorithm = CreateAlgorithmFromUnderlyingMethod(underlyingSink, 'abort', 1, []);
|
1106 | SetUpWritableStreamDefaultController(stream, controller, startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, highWaterMark, sizeAlgorithm);
|
1107 | }
|
1108 |
|
1109 | function WritableStreamDefaultControllerClearAlgorithms(controller) {
|
1110 | controller._writeAlgorithm = undefined;
|
1111 | controller._closeAlgorithm = undefined;
|
1112 | controller._abortAlgorithm = undefined;
|
1113 | controller._strategySizeAlgorithm = undefined;
|
1114 | }
|
1115 | function WritableStreamDefaultControllerClose(controller) {
|
1116 | EnqueueValueWithSize(controller, 'close', 0);
|
1117 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
1118 | }
|
1119 | function WritableStreamDefaultControllerGetChunkSize(controller, chunk) {
|
1120 | try {
|
1121 | return controller._strategySizeAlgorithm(chunk);
|
1122 | }
|
1123 | catch (chunkSizeE) {
|
1124 | WritableStreamDefaultControllerErrorIfNeeded(controller, chunkSizeE);
|
1125 | return 1;
|
1126 | }
|
1127 | }
|
1128 | function WritableStreamDefaultControllerGetDesiredSize(controller) {
|
1129 | return controller._strategyHWM - controller._queueTotalSize;
|
1130 | }
|
1131 | function WritableStreamDefaultControllerWrite(controller, chunk, chunkSize) {
|
1132 | const writeRecord = { chunk };
|
1133 | try {
|
1134 | EnqueueValueWithSize(controller, writeRecord, chunkSize);
|
1135 | }
|
1136 | catch (enqueueE) {
|
1137 | WritableStreamDefaultControllerErrorIfNeeded(controller, enqueueE);
|
1138 | return;
|
1139 | }
|
1140 | const stream = controller._controlledWritableStream;
|
1141 | if (WritableStreamCloseQueuedOrInFlight(stream) === false && stream._state === 'writable') {
|
1142 | const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
|
1143 | WritableStreamUpdateBackpressure(stream, backpressure);
|
1144 | }
|
1145 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
1146 | }
|
1147 |
|
1148 | function WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller) {
|
1149 | const stream = controller._controlledWritableStream;
|
1150 | if (controller._started === false) {
|
1151 | return;
|
1152 | }
|
1153 | if (stream._inFlightWriteRequest !== undefined) {
|
1154 | return;
|
1155 | }
|
1156 | const state = stream._state;
|
1157 | if (state === 'erroring') {
|
1158 | WritableStreamFinishErroring(stream);
|
1159 | return;
|
1160 | }
|
1161 | if (controller._queue.length === 0) {
|
1162 | return;
|
1163 | }
|
1164 | const writeRecord = PeekQueueValue(controller);
|
1165 | if (writeRecord === 'close') {
|
1166 | WritableStreamDefaultControllerProcessClose(controller);
|
1167 | }
|
1168 | else {
|
1169 | WritableStreamDefaultControllerProcessWrite(controller, writeRecord.chunk);
|
1170 | }
|
1171 | }
|
1172 | function WritableStreamDefaultControllerErrorIfNeeded(controller, error) {
|
1173 | if (controller._controlledWritableStream._state === 'writable') {
|
1174 | WritableStreamDefaultControllerError(controller, error);
|
1175 | }
|
1176 | }
|
1177 | function WritableStreamDefaultControllerProcessClose(controller) {
|
1178 | const stream = controller._controlledWritableStream;
|
1179 | WritableStreamMarkCloseRequestInFlight(stream);
|
1180 | DequeueValue(controller);
|
1181 | const sinkClosePromise = controller._closeAlgorithm();
|
1182 | WritableStreamDefaultControllerClearAlgorithms(controller);
|
1183 | uponPromise(sinkClosePromise, () => {
|
1184 | WritableStreamFinishInFlightClose(stream);
|
1185 | }, reason => {
|
1186 | WritableStreamFinishInFlightCloseWithError(stream, reason);
|
1187 | });
|
1188 | }
|
1189 | function WritableStreamDefaultControllerProcessWrite(controller, chunk) {
|
1190 | const stream = controller._controlledWritableStream;
|
1191 | WritableStreamMarkFirstWriteRequestInFlight(stream);
|
1192 | const sinkWritePromise = controller._writeAlgorithm(chunk);
|
1193 | uponPromise(sinkWritePromise, () => {
|
1194 | WritableStreamFinishInFlightWrite(stream);
|
1195 | const state = stream._state;
|
1196 | DequeueValue(controller);
|
1197 | if (WritableStreamCloseQueuedOrInFlight(stream) === false && state === 'writable') {
|
1198 | const backpressure = WritableStreamDefaultControllerGetBackpressure(controller);
|
1199 | WritableStreamUpdateBackpressure(stream, backpressure);
|
1200 | }
|
1201 | WritableStreamDefaultControllerAdvanceQueueIfNeeded(controller);
|
1202 | }, reason => {
|
1203 | if (stream._state === 'writable') {
|
1204 | WritableStreamDefaultControllerClearAlgorithms(controller);
|
1205 | }
|
1206 | WritableStreamFinishInFlightWriteWithError(stream, reason);
|
1207 | });
|
1208 | }
|
1209 | function WritableStreamDefaultControllerGetBackpressure(controller) {
|
1210 | const desiredSize = WritableStreamDefaultControllerGetDesiredSize(controller);
|
1211 | return desiredSize <= 0;
|
1212 | }
|
1213 |
|
1214 | function WritableStreamDefaultControllerError(controller, error) {
|
1215 | const stream = controller._controlledWritableStream;
|
1216 | WritableStreamDefaultControllerClearAlgorithms(controller);
|
1217 | WritableStreamStartErroring(stream, error);
|
1218 | }
|
1219 |
|
1220 | function streamBrandCheckException(name) {
|
1221 | return new TypeError(`WritableStream.prototype.${name} can only be used on a WritableStream`);
|
1222 | }
|
1223 |
|
1224 | function defaultWriterBrandCheckException(name) {
|
1225 | return new TypeError(`WritableStreamDefaultWriter.prototype.${name} can only be used on a WritableStreamDefaultWriter`);
|
1226 | }
|
1227 | function defaultWriterLockException(name) {
|
1228 | return new TypeError('Cannot ' + name + ' a stream using a released writer');
|
1229 | }
|
1230 | function defaultWriterClosedPromiseInitialize(writer) {
|
1231 | writer._closedPromise = newPromise((resolve, reject) => {
|
1232 | writer._closedPromise_resolve = resolve;
|
1233 | writer._closedPromise_reject = reject;
|
1234 | writer._closedPromiseState = 'pending';
|
1235 | });
|
1236 | }
|
1237 | function defaultWriterClosedPromiseInitializeAsRejected(writer, reason) {
|
1238 | defaultWriterClosedPromiseInitialize(writer);
|
1239 | defaultWriterClosedPromiseReject(writer, reason);
|
1240 | }
|
1241 | function defaultWriterClosedPromiseInitializeAsResolved(writer) {
|
1242 | defaultWriterClosedPromiseInitialize(writer);
|
1243 | defaultWriterClosedPromiseResolve(writer);
|
1244 | }
|
1245 | function defaultWriterClosedPromiseReject(writer, reason) {
|
1246 | setPromiseIsHandledToTrue(writer._closedPromise);
|
1247 | writer._closedPromise_reject(reason);
|
1248 | writer._closedPromise_resolve = undefined;
|
1249 | writer._closedPromise_reject = undefined;
|
1250 | writer._closedPromiseState = 'rejected';
|
1251 | }
|
1252 | function defaultWriterClosedPromiseResetToRejected(writer, reason) {
|
1253 | defaultWriterClosedPromiseInitializeAsRejected(writer, reason);
|
1254 | }
|
1255 | function defaultWriterClosedPromiseResolve(writer) {
|
1256 | writer._closedPromise_resolve(undefined);
|
1257 | writer._closedPromise_resolve = undefined;
|
1258 | writer._closedPromise_reject = undefined;
|
1259 | writer._closedPromiseState = 'resolved';
|
1260 | }
|
1261 | function defaultWriterReadyPromiseInitialize(writer) {
|
1262 | writer._readyPromise = newPromise((resolve, reject) => {
|
1263 | writer._readyPromise_resolve = resolve;
|
1264 | writer._readyPromise_reject = reject;
|
1265 | });
|
1266 | writer._readyPromiseState = 'pending';
|
1267 | }
|
1268 | function defaultWriterReadyPromiseInitializeAsRejected(writer, reason) {
|
1269 | defaultWriterReadyPromiseInitialize(writer);
|
1270 | defaultWriterReadyPromiseReject(writer, reason);
|
1271 | }
|
1272 | function defaultWriterReadyPromiseInitializeAsResolved(writer) {
|
1273 | defaultWriterReadyPromiseInitialize(writer);
|
1274 | defaultWriterReadyPromiseResolve(writer);
|
1275 | }
|
1276 | function defaultWriterReadyPromiseReject(writer, reason) {
|
1277 | setPromiseIsHandledToTrue(writer._readyPromise);
|
1278 | writer._readyPromise_reject(reason);
|
1279 | writer._readyPromise_resolve = undefined;
|
1280 | writer._readyPromise_reject = undefined;
|
1281 | writer._readyPromiseState = 'rejected';
|
1282 | }
|
1283 | function defaultWriterReadyPromiseReset(writer) {
|
1284 | defaultWriterReadyPromiseInitialize(writer);
|
1285 | }
|
1286 | function defaultWriterReadyPromiseResetToRejected(writer, reason) {
|
1287 | defaultWriterReadyPromiseInitializeAsRejected(writer, reason);
|
1288 | }
|
1289 | function defaultWriterReadyPromiseResolve(writer) {
|
1290 | writer._readyPromise_resolve(undefined);
|
1291 | writer._readyPromise_resolve = undefined;
|
1292 | writer._readyPromise_reject = undefined;
|
1293 | writer._readyPromiseState = 'fulfilled';
|
1294 | }
|
1295 |
|
1296 | function isAbortSignal(value) {
|
1297 | if (typeof value !== 'object' || value === null) {
|
1298 | return false;
|
1299 | }
|
1300 | try {
|
1301 | return typeof value.aborted === 'boolean';
|
1302 | }
|
1303 | catch (_a) {
|
1304 |
|
1305 | return false;
|
1306 | }
|
1307 | }
|
1308 |
|
1309 |
|
1310 | const NativeDOMException = typeof DOMException !== 'undefined' ? DOMException : undefined;
|
1311 |
|
1312 |
|
1313 | function isDOMExceptionConstructor(ctor) {
|
1314 | if (!(typeof ctor === 'function' || typeof ctor === 'object')) {
|
1315 | return false;
|
1316 | }
|
1317 | try {
|
1318 | new ctor();
|
1319 | return true;
|
1320 | }
|
1321 | catch (_a) {
|
1322 | return false;
|
1323 | }
|
1324 | }
|
1325 | function createDOMExceptionPolyfill() {
|
1326 | const ctor = function DOMException(message, name) {
|
1327 | this.message = message || '';
|
1328 | this.name = name || 'Error';
|
1329 | if (Error.captureStackTrace) {
|
1330 | Error.captureStackTrace(this, this.constructor);
|
1331 | }
|
1332 | };
|
1333 | ctor.prototype = Object.create(Error.prototype);
|
1334 | Object.defineProperty(ctor.prototype, 'constructor', { value: ctor, writable: true, configurable: true });
|
1335 | return ctor;
|
1336 | }
|
1337 | const DOMException$1 = isDOMExceptionConstructor(NativeDOMException) ? NativeDOMException : createDOMExceptionPolyfill();
|
1338 |
|
1339 | function ReadableStreamPipeTo(source, dest, preventClose, preventAbort, preventCancel, signal) {
|
1340 | const reader = AcquireReadableStreamDefaultReader(source);
|
1341 | const writer = AcquireWritableStreamDefaultWriter(dest);
|
1342 | source._disturbed = true;
|
1343 | let shuttingDown = false;
|
1344 |
|
1345 | let currentWrite = promiseResolvedWith(undefined);
|
1346 | return newPromise((resolve, reject) => {
|
1347 | let abortAlgorithm;
|
1348 | if (signal !== undefined) {
|
1349 | abortAlgorithm = () => {
|
1350 | const error = new DOMException$1('Aborted', 'AbortError');
|
1351 | const actions = [];
|
1352 | if (preventAbort === false) {
|
1353 | actions.push(() => {
|
1354 | if (dest._state === 'writable') {
|
1355 | return WritableStreamAbort(dest, error);
|
1356 | }
|
1357 | return promiseResolvedWith(undefined);
|
1358 | });
|
1359 | }
|
1360 | if (preventCancel === false) {
|
1361 | actions.push(() => {
|
1362 | if (source._state === 'readable') {
|
1363 | return ReadableStreamCancel(source, error);
|
1364 | }
|
1365 | return promiseResolvedWith(undefined);
|
1366 | });
|
1367 | }
|
1368 | shutdownWithAction(() => Promise.all(actions.map(action => action())), true, error);
|
1369 | };
|
1370 | if (signal.aborted === true) {
|
1371 | abortAlgorithm();
|
1372 | return;
|
1373 | }
|
1374 | signal.addEventListener('abort', abortAlgorithm);
|
1375 | }
|
1376 |
|
1377 |
|
1378 |
|
1379 | function pipeLoop() {
|
1380 | return newPromise((resolveLoop, rejectLoop) => {
|
1381 | function next(done) {
|
1382 | if (done) {
|
1383 | resolveLoop();
|
1384 | }
|
1385 | else {
|
1386 |
|
1387 |
|
1388 | PerformPromiseThen(pipeStep(), next, rejectLoop);
|
1389 | }
|
1390 | }
|
1391 | next(false);
|
1392 | });
|
1393 | }
|
1394 | function pipeStep() {
|
1395 | if (shuttingDown === true) {
|
1396 | return promiseResolvedWith(true);
|
1397 | }
|
1398 | return PerformPromiseThen(writer._readyPromise, () => {
|
1399 | return PerformPromiseThen(ReadableStreamDefaultReaderRead(reader), result => {
|
1400 | if (result.done === true) {
|
1401 | return true;
|
1402 | }
|
1403 | currentWrite = PerformPromiseThen(WritableStreamDefaultWriterWrite(writer, result.value), undefined, noop);
|
1404 | return false;
|
1405 | });
|
1406 | });
|
1407 | }
|
1408 |
|
1409 | isOrBecomesErrored(source, reader._closedPromise, storedError => {
|
1410 | if (preventAbort === false) {
|
1411 | shutdownWithAction(() => WritableStreamAbort(dest, storedError), true, storedError);
|
1412 | }
|
1413 | else {
|
1414 | shutdown(true, storedError);
|
1415 | }
|
1416 | });
|
1417 |
|
1418 | isOrBecomesErrored(dest, writer._closedPromise, storedError => {
|
1419 | if (preventCancel === false) {
|
1420 | shutdownWithAction(() => ReadableStreamCancel(source, storedError), true, storedError);
|
1421 | }
|
1422 | else {
|
1423 | shutdown(true, storedError);
|
1424 | }
|
1425 | });
|
1426 |
|
1427 | isOrBecomesClosed(source, reader._closedPromise, () => {
|
1428 | if (preventClose === false) {
|
1429 | shutdownWithAction(() => WritableStreamDefaultWriterCloseWithErrorPropagation(writer));
|
1430 | }
|
1431 | else {
|
1432 | shutdown();
|
1433 | }
|
1434 | });
|
1435 |
|
1436 | if (WritableStreamCloseQueuedOrInFlight(dest) === true || dest._state === 'closed') {
|
1437 | const destClosed = new TypeError('the destination writable stream closed before all data could be piped to it');
|
1438 | if (preventCancel === false) {
|
1439 | shutdownWithAction(() => ReadableStreamCancel(source, destClosed), true, destClosed);
|
1440 | }
|
1441 | else {
|
1442 | shutdown(true, destClosed);
|
1443 | }
|
1444 | }
|
1445 | setPromiseIsHandledToTrue(pipeLoop());
|
1446 | function waitForWritesToFinish() {
|
1447 |
|
1448 |
|
1449 | const oldCurrentWrite = currentWrite;
|
1450 | return PerformPromiseThen(currentWrite, () => oldCurrentWrite !== currentWrite ? waitForWritesToFinish() : undefined);
|
1451 | }
|
1452 | function isOrBecomesErrored(stream, promise, action) {
|
1453 | if (stream._state === 'errored') {
|
1454 | action(stream._storedError);
|
1455 | }
|
1456 | else {
|
1457 | uponRejection(promise, action);
|
1458 | }
|
1459 | }
|
1460 | function isOrBecomesClosed(stream, promise, action) {
|
1461 | if (stream._state === 'closed') {
|
1462 | action();
|
1463 | }
|
1464 | else {
|
1465 | uponFulfillment(promise, action);
|
1466 | }
|
1467 | }
|
1468 | function shutdownWithAction(action, originalIsError, originalError) {
|
1469 | if (shuttingDown === true) {
|
1470 | return;
|
1471 | }
|
1472 | shuttingDown = true;
|
1473 | if (dest._state === 'writable' && WritableStreamCloseQueuedOrInFlight(dest) === false) {
|
1474 | uponFulfillment(waitForWritesToFinish(), doTheRest);
|
1475 | }
|
1476 | else {
|
1477 | doTheRest();
|
1478 | }
|
1479 | function doTheRest() {
|
1480 | uponPromise(action(), () => finalize(originalIsError, originalError), newError => finalize(true, newError));
|
1481 | }
|
1482 | }
|
1483 | function shutdown(isError, error) {
|
1484 | if (shuttingDown === true) {
|
1485 | return;
|
1486 | }
|
1487 | shuttingDown = true;
|
1488 | if (dest._state === 'writable' && WritableStreamCloseQueuedOrInFlight(dest) === false) {
|
1489 | uponFulfillment(waitForWritesToFinish(), () => finalize(isError, error));
|
1490 | }
|
1491 | else {
|
1492 | finalize(isError, error);
|
1493 | }
|
1494 | }
|
1495 | function finalize(isError, error) {
|
1496 | WritableStreamDefaultWriterRelease(writer);
|
1497 | ReadableStreamReaderGenericRelease(reader);
|
1498 | if (signal !== undefined) {
|
1499 | signal.removeEventListener('abort', abortAlgorithm);
|
1500 | }
|
1501 | if (isError) {
|
1502 | reject(error);
|
1503 | }
|
1504 | else {
|
1505 | resolve(undefined);
|
1506 | }
|
1507 | }
|
1508 | });
|
1509 | }
|
1510 |
|
1511 | class ReadableStreamDefaultController {
|
1512 |
|
1513 | constructor() {
|
1514 | throw new TypeError();
|
1515 | }
|
1516 | get desiredSize() {
|
1517 | if (IsReadableStreamDefaultController(this) === false) {
|
1518 | throw defaultControllerBrandCheckException('desiredSize');
|
1519 | }
|
1520 | return ReadableStreamDefaultControllerGetDesiredSize(this);
|
1521 | }
|
1522 | close() {
|
1523 | if (IsReadableStreamDefaultController(this) === false) {
|
1524 | throw defaultControllerBrandCheckException('close');
|
1525 | }
|
1526 | if (ReadableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
|
1527 | throw new TypeError('The stream is not in a state that permits close');
|
1528 | }
|
1529 | ReadableStreamDefaultControllerClose(this);
|
1530 | }
|
1531 | enqueue(chunk) {
|
1532 | if (IsReadableStreamDefaultController(this) === false) {
|
1533 | throw defaultControllerBrandCheckException('enqueue');
|
1534 | }
|
1535 | if (ReadableStreamDefaultControllerCanCloseOrEnqueue(this) === false) {
|
1536 | throw new TypeError('The stream is not in a state that permits enqueue');
|
1537 | }
|
1538 | return ReadableStreamDefaultControllerEnqueue(this, chunk);
|
1539 | }
|
1540 | error(e) {
|
1541 | if (IsReadableStreamDefaultController(this) === false) {
|
1542 | throw defaultControllerBrandCheckException('error');
|
1543 | }
|
1544 | ReadableStreamDefaultControllerError(this, e);
|
1545 | }
|
1546 |
|
1547 | [CancelSteps](reason) {
|
1548 | ResetQueue(this);
|
1549 | const result = this._cancelAlgorithm(reason);
|
1550 | ReadableStreamDefaultControllerClearAlgorithms(this);
|
1551 | return result;
|
1552 | }
|
1553 |
|
1554 | [PullSteps]() {
|
1555 | const stream = this._controlledReadableStream;
|
1556 | if (this._queue.length > 0) {
|
1557 | const chunk = DequeueValue(this);
|
1558 | if (this._closeRequested === true && this._queue.length === 0) {
|
1559 | ReadableStreamDefaultControllerClearAlgorithms(this);
|
1560 | ReadableStreamClose(stream);
|
1561 | }
|
1562 | else {
|
1563 | ReadableStreamDefaultControllerCallPullIfNeeded(this);
|
1564 | }
|
1565 | return promiseResolvedWith(ReadableStreamCreateReadResult(chunk, false, stream._reader._forAuthorCode));
|
1566 | }
|
1567 | const pendingPromise = ReadableStreamAddReadRequest(stream);
|
1568 | ReadableStreamDefaultControllerCallPullIfNeeded(this);
|
1569 | return pendingPromise;
|
1570 | }
|
1571 | }
|
1572 |
|
1573 | function IsReadableStreamDefaultController(x) {
|
1574 | if (!typeIsObject(x)) {
|
1575 | return false;
|
1576 | }
|
1577 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableStream')) {
|
1578 | return false;
|
1579 | }
|
1580 | return true;
|
1581 | }
|
1582 | function ReadableStreamDefaultControllerCallPullIfNeeded(controller) {
|
1583 | const shouldPull = ReadableStreamDefaultControllerShouldCallPull(controller);
|
1584 | if (shouldPull === false) {
|
1585 | return;
|
1586 | }
|
1587 | if (controller._pulling === true) {
|
1588 | controller._pullAgain = true;
|
1589 | return;
|
1590 | }
|
1591 | controller._pulling = true;
|
1592 | const pullPromise = controller._pullAlgorithm();
|
1593 | uponPromise(pullPromise, () => {
|
1594 | controller._pulling = false;
|
1595 | if (controller._pullAgain === true) {
|
1596 | controller._pullAgain = false;
|
1597 | ReadableStreamDefaultControllerCallPullIfNeeded(controller);
|
1598 | }
|
1599 | }, e => {
|
1600 | ReadableStreamDefaultControllerError(controller, e);
|
1601 | });
|
1602 | }
|
1603 | function ReadableStreamDefaultControllerShouldCallPull(controller) {
|
1604 | const stream = controller._controlledReadableStream;
|
1605 | if (ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) === false) {
|
1606 | return false;
|
1607 | }
|
1608 | if (controller._started === false) {
|
1609 | return false;
|
1610 | }
|
1611 | if (IsReadableStreamLocked(stream) === true && ReadableStreamGetNumReadRequests(stream) > 0) {
|
1612 | return true;
|
1613 | }
|
1614 | const desiredSize = ReadableStreamDefaultControllerGetDesiredSize(controller);
|
1615 | if (desiredSize > 0) {
|
1616 | return true;
|
1617 | }
|
1618 | return false;
|
1619 | }
|
1620 | function ReadableStreamDefaultControllerClearAlgorithms(controller) {
|
1621 | controller._pullAlgorithm = undefined;
|
1622 | controller._cancelAlgorithm = undefined;
|
1623 | controller._strategySizeAlgorithm = undefined;
|
1624 | }
|
1625 |
|
1626 | function ReadableStreamDefaultControllerClose(controller) {
|
1627 | const stream = controller._controlledReadableStream;
|
1628 | controller._closeRequested = true;
|
1629 | if (controller._queue.length === 0) {
|
1630 | ReadableStreamDefaultControllerClearAlgorithms(controller);
|
1631 | ReadableStreamClose(stream);
|
1632 | }
|
1633 | }
|
1634 | function ReadableStreamDefaultControllerEnqueue(controller, chunk) {
|
1635 | const stream = controller._controlledReadableStream;
|
1636 | if (IsReadableStreamLocked(stream) === true && ReadableStreamGetNumReadRequests(stream) > 0) {
|
1637 | ReadableStreamFulfillReadRequest(stream, chunk, false);
|
1638 | }
|
1639 | else {
|
1640 | let chunkSize;
|
1641 | try {
|
1642 | chunkSize = controller._strategySizeAlgorithm(chunk);
|
1643 | }
|
1644 | catch (chunkSizeE) {
|
1645 | ReadableStreamDefaultControllerError(controller, chunkSizeE);
|
1646 | throw chunkSizeE;
|
1647 | }
|
1648 | try {
|
1649 | EnqueueValueWithSize(controller, chunk, chunkSize);
|
1650 | }
|
1651 | catch (enqueueE) {
|
1652 | ReadableStreamDefaultControllerError(controller, enqueueE);
|
1653 | throw enqueueE;
|
1654 | }
|
1655 | }
|
1656 | ReadableStreamDefaultControllerCallPullIfNeeded(controller);
|
1657 | }
|
1658 | function ReadableStreamDefaultControllerError(controller, e) {
|
1659 | const stream = controller._controlledReadableStream;
|
1660 | if (stream._state !== 'readable') {
|
1661 | return;
|
1662 | }
|
1663 | ResetQueue(controller);
|
1664 | ReadableStreamDefaultControllerClearAlgorithms(controller);
|
1665 | ReadableStreamError(stream, e);
|
1666 | }
|
1667 | function ReadableStreamDefaultControllerGetDesiredSize(controller) {
|
1668 | const stream = controller._controlledReadableStream;
|
1669 | const state = stream._state;
|
1670 | if (state === 'errored') {
|
1671 | return null;
|
1672 | }
|
1673 | if (state === 'closed') {
|
1674 | return 0;
|
1675 | }
|
1676 | return controller._strategyHWM - controller._queueTotalSize;
|
1677 | }
|
1678 |
|
1679 | function ReadableStreamDefaultControllerHasBackpressure(controller) {
|
1680 | if (ReadableStreamDefaultControllerShouldCallPull(controller) === true) {
|
1681 | return false;
|
1682 | }
|
1683 | return true;
|
1684 | }
|
1685 | function ReadableStreamDefaultControllerCanCloseOrEnqueue(controller) {
|
1686 | const state = controller._controlledReadableStream._state;
|
1687 | if (controller._closeRequested === false && state === 'readable') {
|
1688 | return true;
|
1689 | }
|
1690 | return false;
|
1691 | }
|
1692 | function SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm) {
|
1693 | controller._controlledReadableStream = stream;
|
1694 | controller._queue = undefined;
|
1695 | controller._queueTotalSize = undefined;
|
1696 | ResetQueue(controller);
|
1697 | controller._started = false;
|
1698 | controller._closeRequested = false;
|
1699 | controller._pullAgain = false;
|
1700 | controller._pulling = false;
|
1701 | controller._strategySizeAlgorithm = sizeAlgorithm;
|
1702 | controller._strategyHWM = highWaterMark;
|
1703 | controller._pullAlgorithm = pullAlgorithm;
|
1704 | controller._cancelAlgorithm = cancelAlgorithm;
|
1705 | stream._readableStreamController = controller;
|
1706 | const startResult = startAlgorithm();
|
1707 | uponPromise(promiseResolvedWith(startResult), () => {
|
1708 | controller._started = true;
|
1709 | ReadableStreamDefaultControllerCallPullIfNeeded(controller);
|
1710 | }, r => {
|
1711 | ReadableStreamDefaultControllerError(controller, r);
|
1712 | });
|
1713 | }
|
1714 | function SetUpReadableStreamDefaultControllerFromUnderlyingSource(stream, underlyingSource, highWaterMark, sizeAlgorithm) {
|
1715 | const controller = Object.create(ReadableStreamDefaultController.prototype);
|
1716 | function startAlgorithm() {
|
1717 | return InvokeOrNoop(underlyingSource, 'start', [controller]);
|
1718 | }
|
1719 | const pullAlgorithm = CreateAlgorithmFromUnderlyingMethod(underlyingSource, 'pull', 0, [controller]);
|
1720 | const cancelAlgorithm = CreateAlgorithmFromUnderlyingMethod(underlyingSource, 'cancel', 1, []);
|
1721 | SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
|
1722 | }
|
1723 |
|
1724 | function defaultControllerBrandCheckException(name) {
|
1725 | return new TypeError(`ReadableStreamDefaultController.prototype.${name} can only be used on a ReadableStreamDefaultController`);
|
1726 | }
|
1727 |
|
1728 | function ReadableStreamTee(stream, cloneForBranch2) {
|
1729 | const reader = AcquireReadableStreamDefaultReader(stream);
|
1730 | let reading = false;
|
1731 | let canceled1 = false;
|
1732 | let canceled2 = false;
|
1733 | let reason1;
|
1734 | let reason2;
|
1735 | let branch1;
|
1736 | let branch2;
|
1737 | let resolveCancelPromise;
|
1738 | const cancelPromise = newPromise(resolve => {
|
1739 | resolveCancelPromise = resolve;
|
1740 | });
|
1741 | function pullAlgorithm() {
|
1742 | if (reading === true) {
|
1743 | return promiseResolvedWith(undefined);
|
1744 | }
|
1745 | reading = true;
|
1746 | const readPromise = transformPromiseWith(ReadableStreamDefaultReaderRead(reader), result => {
|
1747 | reading = false;
|
1748 | const done = result.done;
|
1749 | if (done === true) {
|
1750 | if (canceled1 === false) {
|
1751 | ReadableStreamDefaultControllerClose(branch1._readableStreamController);
|
1752 | }
|
1753 | if (canceled2 === false) {
|
1754 | ReadableStreamDefaultControllerClose(branch2._readableStreamController);
|
1755 | }
|
1756 | return;
|
1757 | }
|
1758 | const value = result.value;
|
1759 | const value1 = value;
|
1760 | const value2 = value;
|
1761 |
|
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 | if (canceled1 === false) {
|
1767 | ReadableStreamDefaultControllerEnqueue(branch1._readableStreamController, value1);
|
1768 | }
|
1769 | if (canceled2 === false) {
|
1770 | ReadableStreamDefaultControllerEnqueue(branch2._readableStreamController, value2);
|
1771 | }
|
1772 | });
|
1773 | setPromiseIsHandledToTrue(readPromise);
|
1774 | return promiseResolvedWith(undefined);
|
1775 | }
|
1776 | function cancel1Algorithm(reason) {
|
1777 | canceled1 = true;
|
1778 | reason1 = reason;
|
1779 | if (canceled2 === true) {
|
1780 | const compositeReason = createArrayFromList([reason1, reason2]);
|
1781 | const cancelResult = ReadableStreamCancel(stream, compositeReason);
|
1782 | resolveCancelPromise(cancelResult);
|
1783 | }
|
1784 | return cancelPromise;
|
1785 | }
|
1786 | function cancel2Algorithm(reason) {
|
1787 | canceled2 = true;
|
1788 | reason2 = reason;
|
1789 | if (canceled1 === true) {
|
1790 | const compositeReason = createArrayFromList([reason1, reason2]);
|
1791 | const cancelResult = ReadableStreamCancel(stream, compositeReason);
|
1792 | resolveCancelPromise(cancelResult);
|
1793 | }
|
1794 | return cancelPromise;
|
1795 | }
|
1796 | function startAlgorithm() { }
|
1797 | branch1 = CreateReadableStream(startAlgorithm, pullAlgorithm, cancel1Algorithm);
|
1798 | branch2 = CreateReadableStream(startAlgorithm, pullAlgorithm, cancel2Algorithm);
|
1799 | uponRejection(reader._closedPromise, (r) => {
|
1800 | ReadableStreamDefaultControllerError(branch1._readableStreamController, r);
|
1801 | ReadableStreamDefaultControllerError(branch2._readableStreamController, r);
|
1802 | });
|
1803 | return [branch1, branch2];
|
1804 | }
|
1805 |
|
1806 |
|
1807 |
|
1808 | const NumberIsInteger = Number.isInteger || function (value) {
|
1809 | return typeof value === 'number' &&
|
1810 | isFinite(value) &&
|
1811 | Math.floor(value) === value;
|
1812 | };
|
1813 |
|
1814 | class ReadableStreamBYOBRequest {
|
1815 |
|
1816 | constructor() {
|
1817 | throw new TypeError('ReadableStreamBYOBRequest cannot be used directly');
|
1818 | }
|
1819 | get view() {
|
1820 | if (IsReadableStreamBYOBRequest(this) === false) {
|
1821 | throw byobRequestBrandCheckException('view');
|
1822 | }
|
1823 | return this._view;
|
1824 | }
|
1825 | respond(bytesWritten) {
|
1826 | if (IsReadableStreamBYOBRequest(this) === false) {
|
1827 | throw byobRequestBrandCheckException('respond');
|
1828 | }
|
1829 | if (this._associatedReadableByteStreamController === undefined) {
|
1830 | throw new TypeError('This BYOB request has been invalidated');
|
1831 | }
|
1832 | if (IsDetachedBuffer(this._view.buffer) === true) ;
|
1833 | ReadableByteStreamControllerRespond(this._associatedReadableByteStreamController, bytesWritten);
|
1834 | }
|
1835 | respondWithNewView(view) {
|
1836 | if (IsReadableStreamBYOBRequest(this) === false) {
|
1837 | throw byobRequestBrandCheckException('respond');
|
1838 | }
|
1839 | if (this._associatedReadableByteStreamController === undefined) {
|
1840 | throw new TypeError('This BYOB request has been invalidated');
|
1841 | }
|
1842 | if (!ArrayBuffer.isView(view)) {
|
1843 | throw new TypeError('You can only respond with array buffer views');
|
1844 | }
|
1845 | if (IsDetachedBuffer(view.buffer) === true) ;
|
1846 | ReadableByteStreamControllerRespondWithNewView(this._associatedReadableByteStreamController, view);
|
1847 | }
|
1848 | }
|
1849 | class ReadableByteStreamController {
|
1850 |
|
1851 | constructor() {
|
1852 | throw new TypeError('ReadableByteStreamController constructor cannot be used directly');
|
1853 | }
|
1854 | get byobRequest() {
|
1855 | if (IsReadableByteStreamController(this) === false) {
|
1856 | throw byteStreamControllerBrandCheckException('byobRequest');
|
1857 | }
|
1858 | if (this._byobRequest === undefined && this._pendingPullIntos.length > 0) {
|
1859 | const firstDescriptor = this._pendingPullIntos.peek();
|
1860 | const view = new Uint8Array(firstDescriptor.buffer, firstDescriptor.byteOffset + firstDescriptor.bytesFilled, firstDescriptor.byteLength - firstDescriptor.bytesFilled);
|
1861 | const byobRequest = Object.create(ReadableStreamBYOBRequest.prototype);
|
1862 | SetUpReadableStreamBYOBRequest(byobRequest, this, view);
|
1863 | this._byobRequest = byobRequest;
|
1864 | }
|
1865 | return this._byobRequest;
|
1866 | }
|
1867 | get desiredSize() {
|
1868 | if (IsReadableByteStreamController(this) === false) {
|
1869 | throw byteStreamControllerBrandCheckException('desiredSize');
|
1870 | }
|
1871 | return ReadableByteStreamControllerGetDesiredSize(this);
|
1872 | }
|
1873 | close() {
|
1874 | if (IsReadableByteStreamController(this) === false) {
|
1875 | throw byteStreamControllerBrandCheckException('close');
|
1876 | }
|
1877 | if (this._closeRequested === true) {
|
1878 | throw new TypeError('The stream has already been closed; do not close it again!');
|
1879 | }
|
1880 | const state = this._controlledReadableByteStream._state;
|
1881 | if (state !== 'readable') {
|
1882 | throw new TypeError(`The stream (in ${state} state) is not in the readable state and cannot be closed`);
|
1883 | }
|
1884 | ReadableByteStreamControllerClose(this);
|
1885 | }
|
1886 | enqueue(chunk) {
|
1887 | if (IsReadableByteStreamController(this) === false) {
|
1888 | throw byteStreamControllerBrandCheckException('enqueue');
|
1889 | }
|
1890 | if (this._closeRequested === true) {
|
1891 | throw new TypeError('stream is closed or draining');
|
1892 | }
|
1893 | const state = this._controlledReadableByteStream._state;
|
1894 | if (state !== 'readable') {
|
1895 | throw new TypeError(`The stream (in ${state} state) is not in the readable state and cannot be enqueued to`);
|
1896 | }
|
1897 | if (!ArrayBuffer.isView(chunk)) {
|
1898 | throw new TypeError('You can only enqueue array buffer views when using a ReadableByteStreamController');
|
1899 | }
|
1900 | if (IsDetachedBuffer(chunk.buffer) === true) ;
|
1901 | ReadableByteStreamControllerEnqueue(this, chunk);
|
1902 | }
|
1903 | error(e) {
|
1904 | if (IsReadableByteStreamController(this) === false) {
|
1905 | throw byteStreamControllerBrandCheckException('error');
|
1906 | }
|
1907 | ReadableByteStreamControllerError(this, e);
|
1908 | }
|
1909 |
|
1910 | [CancelSteps](reason) {
|
1911 | if (this._pendingPullIntos.length > 0) {
|
1912 | const firstDescriptor = this._pendingPullIntos.peek();
|
1913 | firstDescriptor.bytesFilled = 0;
|
1914 | }
|
1915 | ResetQueue(this);
|
1916 | const result = this._cancelAlgorithm(reason);
|
1917 | ReadableByteStreamControllerClearAlgorithms(this);
|
1918 | return result;
|
1919 | }
|
1920 |
|
1921 | [PullSteps]() {
|
1922 | const stream = this._controlledReadableByteStream;
|
1923 | if (this._queueTotalSize > 0) {
|
1924 | const entry = this._queue.shift();
|
1925 | this._queueTotalSize -= entry.byteLength;
|
1926 | ReadableByteStreamControllerHandleQueueDrain(this);
|
1927 | let view;
|
1928 | try {
|
1929 | view = new Uint8Array(entry.buffer, entry.byteOffset, entry.byteLength);
|
1930 | }
|
1931 | catch (viewE) {
|
1932 | return promiseRejectedWith(viewE);
|
1933 | }
|
1934 | return promiseResolvedWith(ReadableStreamCreateReadResult(view, false, stream._reader._forAuthorCode));
|
1935 | }
|
1936 | const autoAllocateChunkSize = this._autoAllocateChunkSize;
|
1937 | if (autoAllocateChunkSize !== undefined) {
|
1938 | let buffer;
|
1939 | try {
|
1940 | buffer = new ArrayBuffer(autoAllocateChunkSize);
|
1941 | }
|
1942 | catch (bufferE) {
|
1943 | return promiseRejectedWith(bufferE);
|
1944 | }
|
1945 | const pullIntoDescriptor = {
|
1946 | buffer,
|
1947 | byteOffset: 0,
|
1948 | byteLength: autoAllocateChunkSize,
|
1949 | bytesFilled: 0,
|
1950 | elementSize: 1,
|
1951 | ctor: Uint8Array,
|
1952 | readerType: 'default'
|
1953 | };
|
1954 | this._pendingPullIntos.push(pullIntoDescriptor);
|
1955 | }
|
1956 | const promise = ReadableStreamAddReadRequest(stream);
|
1957 | ReadableByteStreamControllerCallPullIfNeeded(this);
|
1958 | return promise;
|
1959 | }
|
1960 | }
|
1961 |
|
1962 | function IsReadableByteStreamController(x) {
|
1963 | if (!typeIsObject(x)) {
|
1964 | return false;
|
1965 | }
|
1966 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledReadableByteStream')) {
|
1967 | return false;
|
1968 | }
|
1969 | return true;
|
1970 | }
|
1971 | function IsReadableStreamBYOBRequest(x) {
|
1972 | if (!typeIsObject(x)) {
|
1973 | return false;
|
1974 | }
|
1975 | if (!Object.prototype.hasOwnProperty.call(x, '_associatedReadableByteStreamController')) {
|
1976 | return false;
|
1977 | }
|
1978 | return true;
|
1979 | }
|
1980 | function ReadableByteStreamControllerCallPullIfNeeded(controller) {
|
1981 | const shouldPull = ReadableByteStreamControllerShouldCallPull(controller);
|
1982 | if (shouldPull === false) {
|
1983 | return;
|
1984 | }
|
1985 | if (controller._pulling === true) {
|
1986 | controller._pullAgain = true;
|
1987 | return;
|
1988 | }
|
1989 | controller._pulling = true;
|
1990 |
|
1991 | const pullPromise = controller._pullAlgorithm();
|
1992 | uponPromise(pullPromise, () => {
|
1993 | controller._pulling = false;
|
1994 | if (controller._pullAgain === true) {
|
1995 | controller._pullAgain = false;
|
1996 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
1997 | }
|
1998 | }, e => {
|
1999 | ReadableByteStreamControllerError(controller, e);
|
2000 | });
|
2001 | }
|
2002 | function ReadableByteStreamControllerClearPendingPullIntos(controller) {
|
2003 | ReadableByteStreamControllerInvalidateBYOBRequest(controller);
|
2004 | controller._pendingPullIntos = new SimpleQueue();
|
2005 | }
|
2006 | function ReadableByteStreamControllerCommitPullIntoDescriptor(stream, pullIntoDescriptor) {
|
2007 | let done = false;
|
2008 | if (stream._state === 'closed') {
|
2009 | done = true;
|
2010 | }
|
2011 | const filledView = ReadableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor);
|
2012 | if (pullIntoDescriptor.readerType === 'default') {
|
2013 | ReadableStreamFulfillReadRequest(stream, filledView, done);
|
2014 | }
|
2015 | else {
|
2016 | ReadableStreamFulfillReadIntoRequest(stream, filledView, done);
|
2017 | }
|
2018 | }
|
2019 | function ReadableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor) {
|
2020 | const bytesFilled = pullIntoDescriptor.bytesFilled;
|
2021 | const elementSize = pullIntoDescriptor.elementSize;
|
2022 | return new pullIntoDescriptor.ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, bytesFilled / elementSize);
|
2023 | }
|
2024 | function ReadableByteStreamControllerEnqueueChunkToQueue(controller, buffer, byteOffset, byteLength) {
|
2025 | controller._queue.push({ buffer, byteOffset, byteLength });
|
2026 | controller._queueTotalSize += byteLength;
|
2027 | }
|
2028 | function ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor) {
|
2029 | const elementSize = pullIntoDescriptor.elementSize;
|
2030 | const currentAlignedBytes = pullIntoDescriptor.bytesFilled - pullIntoDescriptor.bytesFilled % elementSize;
|
2031 | const maxBytesToCopy = Math.min(controller._queueTotalSize, pullIntoDescriptor.byteLength - pullIntoDescriptor.bytesFilled);
|
2032 | const maxBytesFilled = pullIntoDescriptor.bytesFilled + maxBytesToCopy;
|
2033 | const maxAlignedBytes = maxBytesFilled - maxBytesFilled % elementSize;
|
2034 | let totalBytesToCopyRemaining = maxBytesToCopy;
|
2035 | let ready = false;
|
2036 | if (maxAlignedBytes > currentAlignedBytes) {
|
2037 | totalBytesToCopyRemaining = maxAlignedBytes - pullIntoDescriptor.bytesFilled;
|
2038 | ready = true;
|
2039 | }
|
2040 | const queue = controller._queue;
|
2041 | while (totalBytesToCopyRemaining > 0) {
|
2042 | const headOfQueue = queue.peek();
|
2043 | const bytesToCopy = Math.min(totalBytesToCopyRemaining, headOfQueue.byteLength);
|
2044 | const destStart = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
|
2045 | ArrayBufferCopy(pullIntoDescriptor.buffer, destStart, headOfQueue.buffer, headOfQueue.byteOffset, bytesToCopy);
|
2046 | if (headOfQueue.byteLength === bytesToCopy) {
|
2047 | queue.shift();
|
2048 | }
|
2049 | else {
|
2050 | headOfQueue.byteOffset += bytesToCopy;
|
2051 | headOfQueue.byteLength -= bytesToCopy;
|
2052 | }
|
2053 | controller._queueTotalSize -= bytesToCopy;
|
2054 | ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesToCopy, pullIntoDescriptor);
|
2055 | totalBytesToCopyRemaining -= bytesToCopy;
|
2056 | }
|
2057 | return ready;
|
2058 | }
|
2059 | function ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, size, pullIntoDescriptor) {
|
2060 | ReadableByteStreamControllerInvalidateBYOBRequest(controller);
|
2061 | pullIntoDescriptor.bytesFilled += size;
|
2062 | }
|
2063 | function ReadableByteStreamControllerHandleQueueDrain(controller) {
|
2064 | if (controller._queueTotalSize === 0 && controller._closeRequested === true) {
|
2065 | ReadableByteStreamControllerClearAlgorithms(controller);
|
2066 | ReadableStreamClose(controller._controlledReadableByteStream);
|
2067 | }
|
2068 | else {
|
2069 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
2070 | }
|
2071 | }
|
2072 | function ReadableByteStreamControllerInvalidateBYOBRequest(controller) {
|
2073 | if (controller._byobRequest === undefined) {
|
2074 | return;
|
2075 | }
|
2076 | controller._byobRequest._associatedReadableByteStreamController = undefined;
|
2077 | controller._byobRequest._view = undefined;
|
2078 | controller._byobRequest = undefined;
|
2079 | }
|
2080 | function ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller) {
|
2081 | while (controller._pendingPullIntos.length > 0) {
|
2082 | if (controller._queueTotalSize === 0) {
|
2083 | return;
|
2084 | }
|
2085 | const pullIntoDescriptor = controller._pendingPullIntos.peek();
|
2086 | if (ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor) === true) {
|
2087 | ReadableByteStreamControllerShiftPendingPullInto(controller);
|
2088 | ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
|
2089 | }
|
2090 | }
|
2091 | }
|
2092 | function ReadableByteStreamControllerPullInto(controller, view) {
|
2093 | const stream = controller._controlledReadableByteStream;
|
2094 | let elementSize = 1;
|
2095 | if (view.constructor !== DataView) {
|
2096 | elementSize = view.constructor.BYTES_PER_ELEMENT;
|
2097 | }
|
2098 | const ctor = view.constructor;
|
2099 | const buffer = TransferArrayBuffer(view.buffer);
|
2100 | const pullIntoDescriptor = {
|
2101 | buffer,
|
2102 | byteOffset: view.byteOffset,
|
2103 | byteLength: view.byteLength,
|
2104 | bytesFilled: 0,
|
2105 | elementSize,
|
2106 | ctor,
|
2107 | readerType: 'byob'
|
2108 | };
|
2109 | if (controller._pendingPullIntos.length > 0) {
|
2110 | controller._pendingPullIntos.push(pullIntoDescriptor);
|
2111 |
|
2112 |
|
2113 |
|
2114 | return ReadableStreamAddReadIntoRequest(stream);
|
2115 | }
|
2116 | if (stream._state === 'closed') {
|
2117 | const emptyView = new ctor(pullIntoDescriptor.buffer, pullIntoDescriptor.byteOffset, 0);
|
2118 | return promiseResolvedWith(ReadableStreamCreateReadResult(emptyView, true, stream._reader._forAuthorCode));
|
2119 | }
|
2120 | if (controller._queueTotalSize > 0) {
|
2121 | if (ReadableByteStreamControllerFillPullIntoDescriptorFromQueue(controller, pullIntoDescriptor) === true) {
|
2122 | const filledView = ReadableByteStreamControllerConvertPullIntoDescriptor(pullIntoDescriptor);
|
2123 | ReadableByteStreamControllerHandleQueueDrain(controller);
|
2124 | return promiseResolvedWith(ReadableStreamCreateReadResult(filledView, false, stream._reader._forAuthorCode));
|
2125 | }
|
2126 | if (controller._closeRequested === true) {
|
2127 | const e = new TypeError('Insufficient bytes to fill elements in the given buffer');
|
2128 | ReadableByteStreamControllerError(controller, e);
|
2129 | return promiseRejectedWith(e);
|
2130 | }
|
2131 | }
|
2132 | controller._pendingPullIntos.push(pullIntoDescriptor);
|
2133 | const promise = ReadableStreamAddReadIntoRequest(stream);
|
2134 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
2135 | return promise;
|
2136 | }
|
2137 | function ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor) {
|
2138 | firstDescriptor.buffer = TransferArrayBuffer(firstDescriptor.buffer);
|
2139 | const stream = controller._controlledReadableByteStream;
|
2140 | if (ReadableStreamHasBYOBReader(stream) === true) {
|
2141 | while (ReadableStreamGetNumReadIntoRequests(stream) > 0) {
|
2142 | const pullIntoDescriptor = ReadableByteStreamControllerShiftPendingPullInto(controller);
|
2143 | ReadableByteStreamControllerCommitPullIntoDescriptor(stream, pullIntoDescriptor);
|
2144 | }
|
2145 | }
|
2146 | }
|
2147 | function ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, pullIntoDescriptor) {
|
2148 | if (pullIntoDescriptor.bytesFilled + bytesWritten > pullIntoDescriptor.byteLength) {
|
2149 | throw new RangeError('bytesWritten out of range');
|
2150 | }
|
2151 | ReadableByteStreamControllerFillHeadPullIntoDescriptor(controller, bytesWritten, pullIntoDescriptor);
|
2152 | if (pullIntoDescriptor.bytesFilled < pullIntoDescriptor.elementSize) {
|
2153 |
|
2154 | return;
|
2155 | }
|
2156 | ReadableByteStreamControllerShiftPendingPullInto(controller);
|
2157 | const remainderSize = pullIntoDescriptor.bytesFilled % pullIntoDescriptor.elementSize;
|
2158 | if (remainderSize > 0) {
|
2159 | const end = pullIntoDescriptor.byteOffset + pullIntoDescriptor.bytesFilled;
|
2160 | const remainder = pullIntoDescriptor.buffer.slice(end - remainderSize, end);
|
2161 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, remainder, 0, remainder.byteLength);
|
2162 | }
|
2163 | pullIntoDescriptor.buffer = TransferArrayBuffer(pullIntoDescriptor.buffer);
|
2164 | pullIntoDescriptor.bytesFilled -= remainderSize;
|
2165 | ReadableByteStreamControllerCommitPullIntoDescriptor(controller._controlledReadableByteStream, pullIntoDescriptor);
|
2166 | ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
|
2167 | }
|
2168 | function ReadableByteStreamControllerRespondInternal(controller, bytesWritten) {
|
2169 | const firstDescriptor = controller._pendingPullIntos.peek();
|
2170 | const stream = controller._controlledReadableByteStream;
|
2171 | if (stream._state === 'closed') {
|
2172 | if (bytesWritten !== 0) {
|
2173 | throw new TypeError('bytesWritten must be 0 when calling respond() on a closed stream');
|
2174 | }
|
2175 | ReadableByteStreamControllerRespondInClosedState(controller, firstDescriptor);
|
2176 | }
|
2177 | else {
|
2178 | ReadableByteStreamControllerRespondInReadableState(controller, bytesWritten, firstDescriptor);
|
2179 | }
|
2180 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
2181 | }
|
2182 | function ReadableByteStreamControllerShiftPendingPullInto(controller) {
|
2183 | const descriptor = controller._pendingPullIntos.shift();
|
2184 | ReadableByteStreamControllerInvalidateBYOBRequest(controller);
|
2185 | return descriptor;
|
2186 | }
|
2187 | function ReadableByteStreamControllerShouldCallPull(controller) {
|
2188 | const stream = controller._controlledReadableByteStream;
|
2189 | if (stream._state !== 'readable') {
|
2190 | return false;
|
2191 | }
|
2192 | if (controller._closeRequested === true) {
|
2193 | return false;
|
2194 | }
|
2195 | if (controller._started === false) {
|
2196 | return false;
|
2197 | }
|
2198 | if (ReadableStreamHasDefaultReader(stream) === true && ReadableStreamGetNumReadRequests(stream) > 0) {
|
2199 | return true;
|
2200 | }
|
2201 | if (ReadableStreamHasBYOBReader(stream) === true && ReadableStreamGetNumReadIntoRequests(stream) > 0) {
|
2202 | return true;
|
2203 | }
|
2204 | const desiredSize = ReadableByteStreamControllerGetDesiredSize(controller);
|
2205 | if (desiredSize > 0) {
|
2206 | return true;
|
2207 | }
|
2208 | return false;
|
2209 | }
|
2210 | function ReadableByteStreamControllerClearAlgorithms(controller) {
|
2211 | controller._pullAlgorithm = undefined;
|
2212 | controller._cancelAlgorithm = undefined;
|
2213 | }
|
2214 |
|
2215 | function ReadableByteStreamControllerClose(controller) {
|
2216 | const stream = controller._controlledReadableByteStream;
|
2217 | if (controller._queueTotalSize > 0) {
|
2218 | controller._closeRequested = true;
|
2219 | return;
|
2220 | }
|
2221 | if (controller._pendingPullIntos.length > 0) {
|
2222 | const firstPendingPullInto = controller._pendingPullIntos.peek();
|
2223 | if (firstPendingPullInto.bytesFilled > 0) {
|
2224 | const e = new TypeError('Insufficient bytes to fill elements in the given buffer');
|
2225 | ReadableByteStreamControllerError(controller, e);
|
2226 | throw e;
|
2227 | }
|
2228 | }
|
2229 | ReadableByteStreamControllerClearAlgorithms(controller);
|
2230 | ReadableStreamClose(stream);
|
2231 | }
|
2232 | function ReadableByteStreamControllerEnqueue(controller, chunk) {
|
2233 | const stream = controller._controlledReadableByteStream;
|
2234 | const buffer = chunk.buffer;
|
2235 | const byteOffset = chunk.byteOffset;
|
2236 | const byteLength = chunk.byteLength;
|
2237 | const transferredBuffer = TransferArrayBuffer(buffer);
|
2238 | if (ReadableStreamHasDefaultReader(stream) === true) {
|
2239 | if (ReadableStreamGetNumReadRequests(stream) === 0) {
|
2240 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
|
2241 | }
|
2242 | else {
|
2243 | const transferredView = new Uint8Array(transferredBuffer, byteOffset, byteLength);
|
2244 | ReadableStreamFulfillReadRequest(stream, transferredView, false);
|
2245 | }
|
2246 | }
|
2247 | else if (ReadableStreamHasBYOBReader(stream) === true) {
|
2248 |
|
2249 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
|
2250 | ReadableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(controller);
|
2251 | }
|
2252 | else {
|
2253 | ReadableByteStreamControllerEnqueueChunkToQueue(controller, transferredBuffer, byteOffset, byteLength);
|
2254 | }
|
2255 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
2256 | }
|
2257 | function ReadableByteStreamControllerError(controller, e) {
|
2258 | const stream = controller._controlledReadableByteStream;
|
2259 | if (stream._state !== 'readable') {
|
2260 | return;
|
2261 | }
|
2262 | ReadableByteStreamControllerClearPendingPullIntos(controller);
|
2263 | ResetQueue(controller);
|
2264 | ReadableByteStreamControllerClearAlgorithms(controller);
|
2265 | ReadableStreamError(stream, e);
|
2266 | }
|
2267 | function ReadableByteStreamControllerGetDesiredSize(controller) {
|
2268 | const stream = controller._controlledReadableByteStream;
|
2269 | const state = stream._state;
|
2270 | if (state === 'errored') {
|
2271 | return null;
|
2272 | }
|
2273 | if (state === 'closed') {
|
2274 | return 0;
|
2275 | }
|
2276 | return controller._strategyHWM - controller._queueTotalSize;
|
2277 | }
|
2278 | function ReadableByteStreamControllerRespond(controller, bytesWritten) {
|
2279 | bytesWritten = Number(bytesWritten);
|
2280 | if (IsFiniteNonNegativeNumber(bytesWritten) === false) {
|
2281 | throw new RangeError('bytesWritten must be a finite');
|
2282 | }
|
2283 | ReadableByteStreamControllerRespondInternal(controller, bytesWritten);
|
2284 | }
|
2285 | function ReadableByteStreamControllerRespondWithNewView(controller, view) {
|
2286 | const firstDescriptor = controller._pendingPullIntos.peek();
|
2287 | if (firstDescriptor.byteOffset + firstDescriptor.bytesFilled !== view.byteOffset) {
|
2288 | throw new RangeError('The region specified by view does not match byobRequest');
|
2289 | }
|
2290 | if (firstDescriptor.byteLength !== view.byteLength) {
|
2291 | throw new RangeError('The buffer of view has different capacity than byobRequest');
|
2292 | }
|
2293 | firstDescriptor.buffer = view.buffer;
|
2294 | ReadableByteStreamControllerRespondInternal(controller, view.byteLength);
|
2295 | }
|
2296 | function SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize) {
|
2297 | controller._controlledReadableByteStream = stream;
|
2298 | controller._pullAgain = false;
|
2299 | controller._pulling = false;
|
2300 | controller._byobRequest = undefined;
|
2301 |
|
2302 | controller._queue = controller._queueTotalSize = undefined;
|
2303 | ResetQueue(controller);
|
2304 | controller._closeRequested = false;
|
2305 | controller._started = false;
|
2306 | controller._strategyHWM = ValidateAndNormalizeHighWaterMark(highWaterMark);
|
2307 | controller._pullAlgorithm = pullAlgorithm;
|
2308 | controller._cancelAlgorithm = cancelAlgorithm;
|
2309 | controller._autoAllocateChunkSize = autoAllocateChunkSize;
|
2310 | controller._pendingPullIntos = new SimpleQueue();
|
2311 | stream._readableStreamController = controller;
|
2312 | const startResult = startAlgorithm();
|
2313 | uponPromise(promiseResolvedWith(startResult), () => {
|
2314 | controller._started = true;
|
2315 | ReadableByteStreamControllerCallPullIfNeeded(controller);
|
2316 | }, r => {
|
2317 | ReadableByteStreamControllerError(controller, r);
|
2318 | });
|
2319 | }
|
2320 | function SetUpReadableByteStreamControllerFromUnderlyingSource(stream, underlyingByteSource, highWaterMark) {
|
2321 | const controller = Object.create(ReadableByteStreamController.prototype);
|
2322 | function startAlgorithm() {
|
2323 | return InvokeOrNoop(underlyingByteSource, 'start', [controller]);
|
2324 | }
|
2325 | const pullAlgorithm = CreateAlgorithmFromUnderlyingMethod(underlyingByteSource, 'pull', 0, [controller]);
|
2326 | const cancelAlgorithm = CreateAlgorithmFromUnderlyingMethod(underlyingByteSource, 'cancel', 1, []);
|
2327 | let autoAllocateChunkSize = underlyingByteSource.autoAllocateChunkSize;
|
2328 | if (autoAllocateChunkSize !== undefined) {
|
2329 | autoAllocateChunkSize = Number(autoAllocateChunkSize);
|
2330 | if (NumberIsInteger(autoAllocateChunkSize) === false || autoAllocateChunkSize <= 0) {
|
2331 | throw new RangeError('autoAllocateChunkSize must be a positive integer');
|
2332 | }
|
2333 | }
|
2334 | SetUpReadableByteStreamController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, autoAllocateChunkSize);
|
2335 | }
|
2336 | function SetUpReadableStreamBYOBRequest(request, controller, view) {
|
2337 | request._associatedReadableByteStreamController = controller;
|
2338 | request._view = view;
|
2339 | }
|
2340 |
|
2341 | function byobRequestBrandCheckException(name) {
|
2342 | return new TypeError(`ReadableStreamBYOBRequest.prototype.${name} can only be used on a ReadableStreamBYOBRequest`);
|
2343 | }
|
2344 |
|
2345 | function byteStreamControllerBrandCheckException(name) {
|
2346 | return new TypeError(`ReadableByteStreamController.prototype.${name} can only be used on a ReadableByteStreamController`);
|
2347 | }
|
2348 |
|
2349 |
|
2350 | function AcquireReadableStreamBYOBReader(stream, forAuthorCode = false) {
|
2351 | const reader = new ReadableStreamBYOBReader(stream);
|
2352 | reader._forAuthorCode = forAuthorCode;
|
2353 | return reader;
|
2354 | }
|
2355 |
|
2356 | function ReadableStreamAddReadIntoRequest(stream) {
|
2357 | const promise = newPromise((resolve, reject) => {
|
2358 | const readIntoRequest = {
|
2359 | _resolve: resolve,
|
2360 | _reject: reject
|
2361 | };
|
2362 | stream._reader._readIntoRequests.push(readIntoRequest);
|
2363 | });
|
2364 | return promise;
|
2365 | }
|
2366 | function ReadableStreamFulfillReadIntoRequest(stream, chunk, done) {
|
2367 | const reader = stream._reader;
|
2368 | const readIntoRequest = reader._readIntoRequests.shift();
|
2369 | readIntoRequest._resolve(ReadableStreamCreateReadResult(chunk, done, reader._forAuthorCode));
|
2370 | }
|
2371 | function ReadableStreamGetNumReadIntoRequests(stream) {
|
2372 | return stream._reader._readIntoRequests.length;
|
2373 | }
|
2374 | function ReadableStreamHasBYOBReader(stream) {
|
2375 | const reader = stream._reader;
|
2376 | if (reader === undefined) {
|
2377 | return false;
|
2378 | }
|
2379 | if (!IsReadableStreamBYOBReader(reader)) {
|
2380 | return false;
|
2381 | }
|
2382 | return true;
|
2383 | }
|
2384 | class ReadableStreamBYOBReader {
|
2385 | constructor(stream) {
|
2386 | if (!IsReadableStream(stream)) {
|
2387 | throw new TypeError('ReadableStreamBYOBReader can only be constructed with a ReadableStream instance given a ' +
|
2388 | 'byte source');
|
2389 | }
|
2390 | if (IsReadableByteStreamController(stream._readableStreamController) === false) {
|
2391 | throw new TypeError('Cannot construct a ReadableStreamBYOBReader for a stream not constructed with a byte ' +
|
2392 | 'source');
|
2393 | }
|
2394 | if (IsReadableStreamLocked(stream)) {
|
2395 | throw new TypeError('This stream has already been locked for exclusive reading by another reader');
|
2396 | }
|
2397 | ReadableStreamReaderGenericInitialize(this, stream);
|
2398 | this._readIntoRequests = new SimpleQueue();
|
2399 | }
|
2400 | get closed() {
|
2401 | if (!IsReadableStreamBYOBReader(this)) {
|
2402 | return promiseRejectedWith(byobReaderBrandCheckException('closed'));
|
2403 | }
|
2404 | return this._closedPromise;
|
2405 | }
|
2406 | cancel(reason) {
|
2407 | if (!IsReadableStreamBYOBReader(this)) {
|
2408 | return promiseRejectedWith(byobReaderBrandCheckException('cancel'));
|
2409 | }
|
2410 | if (this._ownerReadableStream === undefined) {
|
2411 | return promiseRejectedWith(readerLockException('cancel'));
|
2412 | }
|
2413 | return ReadableStreamReaderGenericCancel(this, reason);
|
2414 | }
|
2415 | read(view) {
|
2416 | if (!IsReadableStreamBYOBReader(this)) {
|
2417 | return promiseRejectedWith(byobReaderBrandCheckException('read'));
|
2418 | }
|
2419 | if (this._ownerReadableStream === undefined) {
|
2420 | return promiseRejectedWith(readerLockException('read from'));
|
2421 | }
|
2422 | if (!ArrayBuffer.isView(view)) {
|
2423 | return promiseRejectedWith(new TypeError('view must be an array buffer view'));
|
2424 | }
|
2425 | if (IsDetachedBuffer(view.buffer) === true) ;
|
2426 | if (view.byteLength === 0) {
|
2427 | return promiseRejectedWith(new TypeError('view must have non-zero byteLength'));
|
2428 | }
|
2429 | return ReadableStreamBYOBReaderRead(this, view);
|
2430 | }
|
2431 | releaseLock() {
|
2432 | if (!IsReadableStreamBYOBReader(this)) {
|
2433 | throw byobReaderBrandCheckException('releaseLock');
|
2434 | }
|
2435 | if (this._ownerReadableStream === undefined) {
|
2436 | return;
|
2437 | }
|
2438 | if (this._readIntoRequests.length > 0) {
|
2439 | throw new TypeError('Tried to release a reader lock when that reader has pending read() calls un-settled');
|
2440 | }
|
2441 | ReadableStreamReaderGenericRelease(this);
|
2442 | }
|
2443 | }
|
2444 |
|
2445 | function IsReadableStreamBYOBReader(x) {
|
2446 | if (!typeIsObject(x)) {
|
2447 | return false;
|
2448 | }
|
2449 | if (!Object.prototype.hasOwnProperty.call(x, '_readIntoRequests')) {
|
2450 | return false;
|
2451 | }
|
2452 | return true;
|
2453 | }
|
2454 | function ReadableStreamBYOBReaderRead(reader, view) {
|
2455 | const stream = reader._ownerReadableStream;
|
2456 | stream._disturbed = true;
|
2457 | if (stream._state === 'errored') {
|
2458 | return promiseRejectedWith(stream._storedError);
|
2459 | }
|
2460 |
|
2461 | return ReadableByteStreamControllerPullInto(stream._readableStreamController, view);
|
2462 | }
|
2463 |
|
2464 | function byobReaderBrandCheckException(name) {
|
2465 | return new TypeError(`ReadableStreamBYOBReader.prototype.${name} can only be used on a ReadableStreamBYOBReader`);
|
2466 | }
|
2467 |
|
2468 | class ReadableStream {
|
2469 | constructor(underlyingSource = {}, strategy = {}) {
|
2470 | InitializeReadableStream(this);
|
2471 | const size = strategy.size;
|
2472 | let highWaterMark = strategy.highWaterMark;
|
2473 | const type = underlyingSource.type;
|
2474 | const typeString = String(type);
|
2475 | if (typeString === 'bytes') {
|
2476 | if (size !== undefined) {
|
2477 | throw new RangeError('The strategy for a byte stream cannot have a size function');
|
2478 | }
|
2479 | if (highWaterMark === undefined) {
|
2480 | highWaterMark = 0;
|
2481 | }
|
2482 | highWaterMark = ValidateAndNormalizeHighWaterMark(highWaterMark);
|
2483 | SetUpReadableByteStreamControllerFromUnderlyingSource(this, underlyingSource, highWaterMark);
|
2484 | }
|
2485 | else if (type === undefined) {
|
2486 | const sizeAlgorithm = MakeSizeAlgorithmFromSizeFunction(size);
|
2487 | if (highWaterMark === undefined) {
|
2488 | highWaterMark = 1;
|
2489 | }
|
2490 | highWaterMark = ValidateAndNormalizeHighWaterMark(highWaterMark);
|
2491 | SetUpReadableStreamDefaultControllerFromUnderlyingSource(this, underlyingSource, highWaterMark, sizeAlgorithm);
|
2492 | }
|
2493 | else {
|
2494 | throw new RangeError('Invalid type is specified');
|
2495 | }
|
2496 | }
|
2497 | get locked() {
|
2498 | if (IsReadableStream(this) === false) {
|
2499 | throw streamBrandCheckException$1('locked');
|
2500 | }
|
2501 | return IsReadableStreamLocked(this);
|
2502 | }
|
2503 | cancel(reason) {
|
2504 | if (IsReadableStream(this) === false) {
|
2505 | return promiseRejectedWith(streamBrandCheckException$1('cancel'));
|
2506 | }
|
2507 | if (IsReadableStreamLocked(this) === true) {
|
2508 | return promiseRejectedWith(new TypeError('Cannot cancel a stream that already has a reader'));
|
2509 | }
|
2510 | return ReadableStreamCancel(this, reason);
|
2511 | }
|
2512 | getReader({ mode } = {}) {
|
2513 | if (IsReadableStream(this) === false) {
|
2514 | throw streamBrandCheckException$1('getReader');
|
2515 | }
|
2516 | if (mode === undefined) {
|
2517 | return AcquireReadableStreamDefaultReader(this, true);
|
2518 | }
|
2519 | mode = String(mode);
|
2520 | if (mode === 'byob') {
|
2521 | return AcquireReadableStreamBYOBReader(this, true);
|
2522 | }
|
2523 | throw new RangeError('Invalid mode is specified');
|
2524 | }
|
2525 | pipeThrough({ writable, readable }, { preventClose, preventAbort, preventCancel, signal } = {}) {
|
2526 | if (IsReadableStream(this) === false) {
|
2527 | throw streamBrandCheckException$1('pipeThrough');
|
2528 | }
|
2529 | if (IsWritableStream(writable) === false) {
|
2530 | throw new TypeError('writable argument to pipeThrough must be a WritableStream');
|
2531 | }
|
2532 | if (IsReadableStream(readable) === false) {
|
2533 | throw new TypeError('readable argument to pipeThrough must be a ReadableStream');
|
2534 | }
|
2535 | preventClose = Boolean(preventClose);
|
2536 | preventAbort = Boolean(preventAbort);
|
2537 | preventCancel = Boolean(preventCancel);
|
2538 | if (signal !== undefined && !isAbortSignal(signal)) {
|
2539 | throw new TypeError('ReadableStream.prototype.pipeThrough\'s signal option must be an AbortSignal');
|
2540 | }
|
2541 | if (IsReadableStreamLocked(this) === true) {
|
2542 | throw new TypeError('ReadableStream.prototype.pipeThrough cannot be used on a locked ReadableStream');
|
2543 | }
|
2544 | if (IsWritableStreamLocked(writable) === true) {
|
2545 | throw new TypeError('ReadableStream.prototype.pipeThrough cannot be used on a locked WritableStream');
|
2546 | }
|
2547 | const promise = ReadableStreamPipeTo(this, writable, preventClose, preventAbort, preventCancel, signal);
|
2548 | setPromiseIsHandledToTrue(promise);
|
2549 | return readable;
|
2550 | }
|
2551 | pipeTo(dest, { preventClose, preventAbort, preventCancel, signal } = {}) {
|
2552 | if (IsReadableStream(this) === false) {
|
2553 | return promiseRejectedWith(streamBrandCheckException$1('pipeTo'));
|
2554 | }
|
2555 | if (IsWritableStream(dest) === false) {
|
2556 | return promiseRejectedWith(new TypeError('ReadableStream.prototype.pipeTo\'s first argument must be a WritableStream'));
|
2557 | }
|
2558 | preventClose = Boolean(preventClose);
|
2559 | preventAbort = Boolean(preventAbort);
|
2560 | preventCancel = Boolean(preventCancel);
|
2561 | if (signal !== undefined && !isAbortSignal(signal)) {
|
2562 | return promiseRejectedWith(new TypeError('ReadableStream.prototype.pipeTo\'s signal option must be an AbortSignal'));
|
2563 | }
|
2564 | if (IsReadableStreamLocked(this) === true) {
|
2565 | return promiseRejectedWith(new TypeError('ReadableStream.prototype.pipeTo cannot be used on a locked ReadableStream'));
|
2566 | }
|
2567 | if (IsWritableStreamLocked(dest) === true) {
|
2568 | return promiseRejectedWith(new TypeError('ReadableStream.prototype.pipeTo cannot be used on a locked WritableStream'));
|
2569 | }
|
2570 | return ReadableStreamPipeTo(this, dest, preventClose, preventAbort, preventCancel, signal);
|
2571 | }
|
2572 | tee() {
|
2573 | if (IsReadableStream(this) === false) {
|
2574 | throw streamBrandCheckException$1('tee');
|
2575 | }
|
2576 | const branches = ReadableStreamTee(this);
|
2577 | return createArrayFromList(branches);
|
2578 | }
|
2579 | getIterator({ preventCancel = false } = {}) {
|
2580 | if (IsReadableStream(this) === false) {
|
2581 | throw streamBrandCheckException$1('getIterator');
|
2582 | }
|
2583 | return AcquireReadableStreamAsyncIterator(this, preventCancel);
|
2584 | }
|
2585 | }
|
2586 | if (typeof SymbolPolyfill.asyncIterator === 'symbol') {
|
2587 | Object.defineProperty(ReadableStream.prototype, SymbolPolyfill.asyncIterator, {
|
2588 | value: ReadableStream.prototype.getIterator,
|
2589 | enumerable: false,
|
2590 | writable: true,
|
2591 | configurable: true
|
2592 | });
|
2593 | }
|
2594 |
|
2595 |
|
2596 | function CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark = 1, sizeAlgorithm = () => 1) {
|
2597 | const stream = Object.create(ReadableStream.prototype);
|
2598 | InitializeReadableStream(stream);
|
2599 | const controller = Object.create(ReadableStreamDefaultController.prototype);
|
2600 | SetUpReadableStreamDefaultController(stream, controller, startAlgorithm, pullAlgorithm, cancelAlgorithm, highWaterMark, sizeAlgorithm);
|
2601 | return stream;
|
2602 | }
|
2603 | function InitializeReadableStream(stream) {
|
2604 | stream._state = 'readable';
|
2605 | stream._reader = undefined;
|
2606 | stream._storedError = undefined;
|
2607 | stream._disturbed = false;
|
2608 | }
|
2609 | function IsReadableStream(x) {
|
2610 | if (!typeIsObject(x)) {
|
2611 | return false;
|
2612 | }
|
2613 | if (!Object.prototype.hasOwnProperty.call(x, '_readableStreamController')) {
|
2614 | return false;
|
2615 | }
|
2616 | return true;
|
2617 | }
|
2618 | function IsReadableStreamLocked(stream) {
|
2619 | if (stream._reader === undefined) {
|
2620 | return false;
|
2621 | }
|
2622 | return true;
|
2623 | }
|
2624 |
|
2625 | function ReadableStreamCancel(stream, reason) {
|
2626 | stream._disturbed = true;
|
2627 | if (stream._state === 'closed') {
|
2628 | return promiseResolvedWith(undefined);
|
2629 | }
|
2630 | if (stream._state === 'errored') {
|
2631 | return promiseRejectedWith(stream._storedError);
|
2632 | }
|
2633 | ReadableStreamClose(stream);
|
2634 | const sourceCancelPromise = stream._readableStreamController[CancelSteps](reason);
|
2635 | return transformPromiseWith(sourceCancelPromise, noop);
|
2636 | }
|
2637 | function ReadableStreamClose(stream) {
|
2638 | stream._state = 'closed';
|
2639 | const reader = stream._reader;
|
2640 | if (reader === undefined) {
|
2641 | return;
|
2642 | }
|
2643 | if (IsReadableStreamDefaultReader(reader)) {
|
2644 | reader._readRequests.forEach(readRequest => {
|
2645 | readRequest._resolve(ReadableStreamCreateReadResult(undefined, true, reader._forAuthorCode));
|
2646 | });
|
2647 | reader._readRequests = new SimpleQueue();
|
2648 | }
|
2649 | defaultReaderClosedPromiseResolve(reader);
|
2650 | }
|
2651 | function ReadableStreamError(stream, e) {
|
2652 | stream._state = 'errored';
|
2653 | stream._storedError = e;
|
2654 | const reader = stream._reader;
|
2655 | if (reader === undefined) {
|
2656 | return;
|
2657 | }
|
2658 | if (IsReadableStreamDefaultReader(reader)) {
|
2659 | reader._readRequests.forEach(readRequest => {
|
2660 | readRequest._reject(e);
|
2661 | });
|
2662 | reader._readRequests = new SimpleQueue();
|
2663 | }
|
2664 | else {
|
2665 | reader._readIntoRequests.forEach(readIntoRequest => {
|
2666 | readIntoRequest._reject(e);
|
2667 | });
|
2668 | reader._readIntoRequests = new SimpleQueue();
|
2669 | }
|
2670 | defaultReaderClosedPromiseReject(reader, e);
|
2671 | }
|
2672 |
|
2673 | function streamBrandCheckException$1(name) {
|
2674 | return new TypeError(`ReadableStream.prototype.${name} can only be used on a ReadableStream`);
|
2675 | }
|
2676 |
|
2677 |
|
2678 | class TransformStream {
|
2679 | constructor(transformer = {}, writableStrategy = {}, readableStrategy = {}) {
|
2680 | const writableSizeFunction = writableStrategy.size;
|
2681 | let writableHighWaterMark = writableStrategy.highWaterMark;
|
2682 | const readableSizeFunction = readableStrategy.size;
|
2683 | let readableHighWaterMark = readableStrategy.highWaterMark;
|
2684 | const writableType = transformer.writableType;
|
2685 | if (writableType !== undefined) {
|
2686 | throw new RangeError('Invalid writable type specified');
|
2687 | }
|
2688 | const writableSizeAlgorithm = MakeSizeAlgorithmFromSizeFunction(writableSizeFunction);
|
2689 | if (writableHighWaterMark === undefined) {
|
2690 | writableHighWaterMark = 1;
|
2691 | }
|
2692 | writableHighWaterMark = ValidateAndNormalizeHighWaterMark(writableHighWaterMark);
|
2693 | const readableType = transformer.readableType;
|
2694 | if (readableType !== undefined) {
|
2695 | throw new RangeError('Invalid readable type specified');
|
2696 | }
|
2697 | const readableSizeAlgorithm = MakeSizeAlgorithmFromSizeFunction(readableSizeFunction);
|
2698 | if (readableHighWaterMark === undefined) {
|
2699 | readableHighWaterMark = 0;
|
2700 | }
|
2701 | readableHighWaterMark = ValidateAndNormalizeHighWaterMark(readableHighWaterMark);
|
2702 | let startPromise_resolve;
|
2703 | const startPromise = newPromise(resolve => {
|
2704 | startPromise_resolve = resolve;
|
2705 | });
|
2706 | InitializeTransformStream(this, startPromise, writableHighWaterMark, writableSizeAlgorithm, readableHighWaterMark, readableSizeAlgorithm);
|
2707 | SetUpTransformStreamDefaultControllerFromTransformer(this, transformer);
|
2708 | const startResult = InvokeOrNoop(transformer, 'start', [this._transformStreamController]);
|
2709 | startPromise_resolve(startResult);
|
2710 | }
|
2711 | get readable() {
|
2712 | if (IsTransformStream(this) === false) {
|
2713 | throw streamBrandCheckException$2('readable');
|
2714 | }
|
2715 | return this._readable;
|
2716 | }
|
2717 | get writable() {
|
2718 | if (IsTransformStream(this) === false) {
|
2719 | throw streamBrandCheckException$2('writable');
|
2720 | }
|
2721 | return this._writable;
|
2722 | }
|
2723 | }
|
2724 | function InitializeTransformStream(stream, startPromise, writableHighWaterMark, writableSizeAlgorithm, readableHighWaterMark, readableSizeAlgorithm) {
|
2725 | function startAlgorithm() {
|
2726 | return startPromise;
|
2727 | }
|
2728 | function writeAlgorithm(chunk) {
|
2729 | return TransformStreamDefaultSinkWriteAlgorithm(stream, chunk);
|
2730 | }
|
2731 | function abortAlgorithm(reason) {
|
2732 | return TransformStreamDefaultSinkAbortAlgorithm(stream, reason);
|
2733 | }
|
2734 | function closeAlgorithm() {
|
2735 | return TransformStreamDefaultSinkCloseAlgorithm(stream);
|
2736 | }
|
2737 | stream._writable = CreateWritableStream(startAlgorithm, writeAlgorithm, closeAlgorithm, abortAlgorithm, writableHighWaterMark, writableSizeAlgorithm);
|
2738 | function pullAlgorithm() {
|
2739 | return TransformStreamDefaultSourcePullAlgorithm(stream);
|
2740 | }
|
2741 | function cancelAlgorithm(reason) {
|
2742 | TransformStreamErrorWritableAndUnblockWrite(stream, reason);
|
2743 | return promiseResolvedWith(undefined);
|
2744 | }
|
2745 | stream._readable = CreateReadableStream(startAlgorithm, pullAlgorithm, cancelAlgorithm, readableHighWaterMark, readableSizeAlgorithm);
|
2746 |
|
2747 | stream._backpressure = undefined;
|
2748 | stream._backpressureChangePromise = undefined;
|
2749 | stream._backpressureChangePromise_resolve = undefined;
|
2750 | TransformStreamSetBackpressure(stream, true);
|
2751 |
|
2752 | stream._transformStreamController = undefined;
|
2753 | }
|
2754 | function IsTransformStream(x) {
|
2755 | if (!typeIsObject(x)) {
|
2756 | return false;
|
2757 | }
|
2758 | if (!Object.prototype.hasOwnProperty.call(x, '_transformStreamController')) {
|
2759 | return false;
|
2760 | }
|
2761 | return true;
|
2762 | }
|
2763 |
|
2764 | function TransformStreamError(stream, e) {
|
2765 | ReadableStreamDefaultControllerError(stream._readable._readableStreamController, e);
|
2766 | TransformStreamErrorWritableAndUnblockWrite(stream, e);
|
2767 | }
|
2768 | function TransformStreamErrorWritableAndUnblockWrite(stream, e) {
|
2769 | TransformStreamDefaultControllerClearAlgorithms(stream._transformStreamController);
|
2770 | WritableStreamDefaultControllerErrorIfNeeded(stream._writable._writableStreamController, e);
|
2771 | if (stream._backpressure === true) {
|
2772 |
|
2773 |
|
2774 |
|
2775 | TransformStreamSetBackpressure(stream, false);
|
2776 | }
|
2777 | }
|
2778 | function TransformStreamSetBackpressure(stream, backpressure) {
|
2779 |
|
2780 | if (stream._backpressureChangePromise !== undefined) {
|
2781 | stream._backpressureChangePromise_resolve();
|
2782 | }
|
2783 | stream._backpressureChangePromise = newPromise(resolve => {
|
2784 | stream._backpressureChangePromise_resolve = resolve;
|
2785 | });
|
2786 | stream._backpressure = backpressure;
|
2787 | }
|
2788 | class TransformStreamDefaultController {
|
2789 |
|
2790 | constructor() {
|
2791 | throw new TypeError('TransformStreamDefaultController instances cannot be created directly');
|
2792 | }
|
2793 | get desiredSize() {
|
2794 | if (IsTransformStreamDefaultController(this) === false) {
|
2795 | throw defaultControllerBrandCheckException$1('desiredSize');
|
2796 | }
|
2797 | const readableController = this._controlledTransformStream._readable._readableStreamController;
|
2798 | return ReadableStreamDefaultControllerGetDesiredSize(readableController);
|
2799 | }
|
2800 | enqueue(chunk) {
|
2801 | if (IsTransformStreamDefaultController(this) === false) {
|
2802 | throw defaultControllerBrandCheckException$1('enqueue');
|
2803 | }
|
2804 | TransformStreamDefaultControllerEnqueue(this, chunk);
|
2805 | }
|
2806 | error(reason) {
|
2807 | if (IsTransformStreamDefaultController(this) === false) {
|
2808 | throw defaultControllerBrandCheckException$1('error');
|
2809 | }
|
2810 | TransformStreamDefaultControllerError(this, reason);
|
2811 | }
|
2812 | terminate() {
|
2813 | if (IsTransformStreamDefaultController(this) === false) {
|
2814 | throw defaultControllerBrandCheckException$1('terminate');
|
2815 | }
|
2816 | TransformStreamDefaultControllerTerminate(this);
|
2817 | }
|
2818 | }
|
2819 |
|
2820 | function IsTransformStreamDefaultController(x) {
|
2821 | if (!typeIsObject(x)) {
|
2822 | return false;
|
2823 | }
|
2824 | if (!Object.prototype.hasOwnProperty.call(x, '_controlledTransformStream')) {
|
2825 | return false;
|
2826 | }
|
2827 | return true;
|
2828 | }
|
2829 | function SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm) {
|
2830 | controller._controlledTransformStream = stream;
|
2831 | stream._transformStreamController = controller;
|
2832 | controller._transformAlgorithm = transformAlgorithm;
|
2833 | controller._flushAlgorithm = flushAlgorithm;
|
2834 | }
|
2835 | function SetUpTransformStreamDefaultControllerFromTransformer(stream, transformer) {
|
2836 | const controller = Object.create(TransformStreamDefaultController.prototype);
|
2837 | let transformAlgorithm = (chunk) => {
|
2838 | try {
|
2839 | TransformStreamDefaultControllerEnqueue(controller, chunk);
|
2840 | return promiseResolvedWith(undefined);
|
2841 | }
|
2842 | catch (transformResultE) {
|
2843 | return promiseRejectedWith(transformResultE);
|
2844 | }
|
2845 | };
|
2846 | const transformMethod = transformer.transform;
|
2847 | if (transformMethod !== undefined) {
|
2848 | if (typeof transformMethod !== 'function') {
|
2849 | throw new TypeError('transform is not a method');
|
2850 | }
|
2851 | transformAlgorithm = chunk => PromiseCall(transformMethod, transformer, [chunk, controller]);
|
2852 | }
|
2853 | const flushAlgorithm = CreateAlgorithmFromUnderlyingMethod(transformer, 'flush', 0, [controller]);
|
2854 | SetUpTransformStreamDefaultController(stream, controller, transformAlgorithm, flushAlgorithm);
|
2855 | }
|
2856 | function TransformStreamDefaultControllerClearAlgorithms(controller) {
|
2857 | controller._transformAlgorithm = undefined;
|
2858 | controller._flushAlgorithm = undefined;
|
2859 | }
|
2860 | function TransformStreamDefaultControllerEnqueue(controller, chunk) {
|
2861 | const stream = controller._controlledTransformStream;
|
2862 | const readableController = stream._readable._readableStreamController;
|
2863 | if (ReadableStreamDefaultControllerCanCloseOrEnqueue(readableController) === false) {
|
2864 | throw new TypeError('Readable side is not in a state that permits enqueue');
|
2865 | }
|
2866 |
|
2867 |
|
2868 | try {
|
2869 | ReadableStreamDefaultControllerEnqueue(readableController, chunk);
|
2870 | }
|
2871 | catch (e) {
|
2872 |
|
2873 | TransformStreamErrorWritableAndUnblockWrite(stream, e);
|
2874 | throw stream._readable._storedError;
|
2875 | }
|
2876 | const backpressure = ReadableStreamDefaultControllerHasBackpressure(readableController);
|
2877 | if (backpressure !== stream._backpressure) {
|
2878 | TransformStreamSetBackpressure(stream, true);
|
2879 | }
|
2880 | }
|
2881 | function TransformStreamDefaultControllerError(controller, e) {
|
2882 | TransformStreamError(controller._controlledTransformStream, e);
|
2883 | }
|
2884 | function TransformStreamDefaultControllerPerformTransform(controller, chunk) {
|
2885 | const transformPromise = controller._transformAlgorithm(chunk);
|
2886 | return transformPromiseWith(transformPromise, undefined, r => {
|
2887 | TransformStreamError(controller._controlledTransformStream, r);
|
2888 | throw r;
|
2889 | });
|
2890 | }
|
2891 | function TransformStreamDefaultControllerTerminate(controller) {
|
2892 | const stream = controller._controlledTransformStream;
|
2893 | const readableController = stream._readable._readableStreamController;
|
2894 | if (ReadableStreamDefaultControllerCanCloseOrEnqueue(readableController) === true) {
|
2895 | ReadableStreamDefaultControllerClose(readableController);
|
2896 | }
|
2897 | const error = new TypeError('TransformStream terminated');
|
2898 | TransformStreamErrorWritableAndUnblockWrite(stream, error);
|
2899 | }
|
2900 |
|
2901 | function TransformStreamDefaultSinkWriteAlgorithm(stream, chunk) {
|
2902 | const controller = stream._transformStreamController;
|
2903 | if (stream._backpressure === true) {
|
2904 | const backpressureChangePromise = stream._backpressureChangePromise;
|
2905 | return transformPromiseWith(backpressureChangePromise, () => {
|
2906 | const writable = stream._writable;
|
2907 | const state = writable._state;
|
2908 | if (state === 'erroring') {
|
2909 | throw writable._storedError;
|
2910 | }
|
2911 | return TransformStreamDefaultControllerPerformTransform(controller, chunk);
|
2912 | });
|
2913 | }
|
2914 | return TransformStreamDefaultControllerPerformTransform(controller, chunk);
|
2915 | }
|
2916 | function TransformStreamDefaultSinkAbortAlgorithm(stream, reason) {
|
2917 |
|
2918 |
|
2919 | TransformStreamError(stream, reason);
|
2920 | return promiseResolvedWith(undefined);
|
2921 | }
|
2922 | function TransformStreamDefaultSinkCloseAlgorithm(stream) {
|
2923 |
|
2924 | const readable = stream._readable;
|
2925 | const controller = stream._transformStreamController;
|
2926 | const flushPromise = controller._flushAlgorithm();
|
2927 | TransformStreamDefaultControllerClearAlgorithms(controller);
|
2928 |
|
2929 | return transformPromiseWith(flushPromise, () => {
|
2930 | if (readable._state === 'errored') {
|
2931 | throw readable._storedError;
|
2932 | }
|
2933 | const readableController = readable._readableStreamController;
|
2934 | if (ReadableStreamDefaultControllerCanCloseOrEnqueue(readableController) === true) {
|
2935 | ReadableStreamDefaultControllerClose(readableController);
|
2936 | }
|
2937 | }, r => {
|
2938 | TransformStreamError(stream, r);
|
2939 | throw readable._storedError;
|
2940 | });
|
2941 | }
|
2942 |
|
2943 | function TransformStreamDefaultSourcePullAlgorithm(stream) {
|
2944 |
|
2945 | TransformStreamSetBackpressure(stream, false);
|
2946 |
|
2947 | return stream._backpressureChangePromise;
|
2948 | }
|
2949 |
|
2950 | function defaultControllerBrandCheckException$1(name) {
|
2951 | return new TypeError(`TransformStreamDefaultController.prototype.${name} can only be used on a TransformStreamDefaultController`);
|
2952 | }
|
2953 |
|
2954 | function streamBrandCheckException$2(name) {
|
2955 | return new TypeError(`TransformStream.prototype.${name} can only be used on a TransformStream`);
|
2956 | }
|
2957 |
|
2958 |
|
2959 |
|
2960 |
|
2961 |
|
2962 |
|
2963 |
|
2964 |
|
2965 |
|
2966 |
|
2967 |
|
2968 |
|
2969 |
|
2970 |
|
2971 |
|
2972 |
|
2973 |
|
2974 | var extendStatics = function(d, b) {
|
2975 | extendStatics = Object.setPrototypeOf ||
|
2976 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
2977 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
2978 | return extendStatics(d, b);
|
2979 | };
|
2980 |
|
2981 | function __extends(d, b) {
|
2982 | extendStatics(d, b);
|
2983 | function __() { this.constructor = d; }
|
2984 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
2985 | }
|
2986 |
|
2987 | function assert(test) {
|
2988 | if (!test) {
|
2989 | throw new TypeError('Assertion failed');
|
2990 | }
|
2991 | }
|
2992 |
|
2993 | function noop$1() {
|
2994 | return;
|
2995 | }
|
2996 | function typeIsObject$1(x) {
|
2997 | return (typeof x === 'object' && x !== null) || typeof x === 'function';
|
2998 | }
|
2999 |
|
3000 | function isStreamConstructor(ctor) {
|
3001 | if (typeof ctor !== 'function') {
|
3002 | return false;
|
3003 | }
|
3004 | var startCalled = false;
|
3005 | try {
|
3006 | new ctor({
|
3007 | start: function () {
|
3008 | startCalled = true;
|
3009 | }
|
3010 | });
|
3011 | }
|
3012 | catch (e) {
|
3013 |
|
3014 | }
|
3015 | return startCalled;
|
3016 | }
|
3017 | function isReadableStream(readable) {
|
3018 | if (!typeIsObject$1(readable)) {
|
3019 | return false;
|
3020 | }
|
3021 | if (typeof readable.getReader !== 'function') {
|
3022 | return false;
|
3023 | }
|
3024 | return true;
|
3025 | }
|
3026 | function isReadableStreamConstructor(ctor) {
|
3027 | if (!isStreamConstructor(ctor)) {
|
3028 | return false;
|
3029 | }
|
3030 | if (!isReadableStream(new ctor())) {
|
3031 | return false;
|
3032 | }
|
3033 | return true;
|
3034 | }
|
3035 | function supportsByobReader(readable) {
|
3036 | try {
|
3037 | var reader = readable.getReader({ mode: 'byob' });
|
3038 | reader.releaseLock();
|
3039 | return true;
|
3040 | }
|
3041 | catch (_a) {
|
3042 | return false;
|
3043 | }
|
3044 | }
|
3045 | function supportsByteSource(ctor) {
|
3046 | try {
|
3047 | new ctor({ type: 'bytes' });
|
3048 | return true;
|
3049 | }
|
3050 | catch (_a) {
|
3051 | return false;
|
3052 | }
|
3053 | }
|
3054 |
|
3055 | function createReadableStreamWrapper(ctor) {
|
3056 | assert(isReadableStreamConstructor(ctor));
|
3057 | var byteSourceSupported = supportsByteSource(ctor);
|
3058 | return function (readable, _a) {
|
3059 | var type = (_a === void 0 ? {} : _a).type;
|
3060 | type = parseReadableType(type);
|
3061 | if (type === 'bytes' && !byteSourceSupported) {
|
3062 | type = undefined;
|
3063 | }
|
3064 | if (readable.constructor === ctor) {
|
3065 | if (type !== 'bytes' || supportsByobReader(readable)) {
|
3066 | return readable;
|
3067 | }
|
3068 | }
|
3069 | var source = createWrappingReadableSource(readable, { type: type });
|
3070 | return new ctor(source);
|
3071 | };
|
3072 | }
|
3073 | function createWrappingReadableSource(readable, _a) {
|
3074 | var type = (_a === void 0 ? {} : _a).type;
|
3075 | assert(isReadableStream(readable));
|
3076 | assert(readable.locked === false);
|
3077 | type = parseReadableType(type);
|
3078 | var source;
|
3079 | if (type === 'bytes') {
|
3080 | source = new WrappingReadableByteStreamSource(readable);
|
3081 | }
|
3082 | else {
|
3083 | source = new WrappingReadableStreamDefaultSource(readable);
|
3084 | }
|
3085 | return source;
|
3086 | }
|
3087 | function parseReadableType(type) {
|
3088 | var typeString = String(type);
|
3089 | if (typeString === 'bytes') {
|
3090 | return typeString;
|
3091 | }
|
3092 | else if (type === undefined) {
|
3093 | return type;
|
3094 | }
|
3095 | else {
|
3096 | throw new RangeError('Invalid type is specified');
|
3097 | }
|
3098 | }
|
3099 | var AbstractWrappingReadableStreamSource = (function () {
|
3100 | function AbstractWrappingReadableStreamSource(underlyingStream) {
|
3101 | this._underlyingReader = undefined;
|
3102 | this._readerMode = undefined;
|
3103 | this._readableStreamController = undefined;
|
3104 | this._pendingRead = undefined;
|
3105 | this._underlyingStream = underlyingStream;
|
3106 |
|
3107 | this._attachDefaultReader();
|
3108 | }
|
3109 | AbstractWrappingReadableStreamSource.prototype.start = function (controller) {
|
3110 | this._readableStreamController = controller;
|
3111 | };
|
3112 | AbstractWrappingReadableStreamSource.prototype.cancel = function (reason) {
|
3113 | assert(this._underlyingReader !== undefined);
|
3114 | return this._underlyingReader.cancel(reason);
|
3115 | };
|
3116 | AbstractWrappingReadableStreamSource.prototype._attachDefaultReader = function () {
|
3117 | if (this._readerMode === "default" ) {
|
3118 | return;
|
3119 | }
|
3120 | this._detachReader();
|
3121 | var reader = this._underlyingStream.getReader();
|
3122 | this._readerMode = "default" ;
|
3123 | this._attachReader(reader);
|
3124 | };
|
3125 | AbstractWrappingReadableStreamSource.prototype._attachReader = function (reader) {
|
3126 | var _this = this;
|
3127 | assert(this._underlyingReader === undefined);
|
3128 | this._underlyingReader = reader;
|
3129 | var closed = this._underlyingReader.closed;
|
3130 | if (!closed) {
|
3131 | return;
|
3132 | }
|
3133 | closed
|
3134 | .then(function () { return _this._finishPendingRead(); })
|
3135 | .then(function () {
|
3136 | if (reader === _this._underlyingReader) {
|
3137 | _this._readableStreamController.close();
|
3138 | }
|
3139 | }, function (reason) {
|
3140 | if (reader === _this._underlyingReader) {
|
3141 | _this._readableStreamController.error(reason);
|
3142 | }
|
3143 | })
|
3144 | .catch(noop$1);
|
3145 | };
|
3146 | AbstractWrappingReadableStreamSource.prototype._detachReader = function () {
|
3147 | if (this._underlyingReader === undefined) {
|
3148 | return;
|
3149 | }
|
3150 | this._underlyingReader.releaseLock();
|
3151 | this._underlyingReader = undefined;
|
3152 | this._readerMode = undefined;
|
3153 | };
|
3154 | AbstractWrappingReadableStreamSource.prototype._pullWithDefaultReader = function () {
|
3155 | var _this = this;
|
3156 | this._attachDefaultReader();
|
3157 |
|
3158 | var read = this._underlyingReader.read()
|
3159 | .then(function (result) {
|
3160 | var controller = _this._readableStreamController;
|
3161 | if (result.done) {
|
3162 | _this._tryClose();
|
3163 | }
|
3164 | else {
|
3165 | controller.enqueue(result.value);
|
3166 | }
|
3167 | });
|
3168 | this._setPendingRead(read);
|
3169 | return read;
|
3170 | };
|
3171 | AbstractWrappingReadableStreamSource.prototype._tryClose = function () {
|
3172 | try {
|
3173 | this._readableStreamController.close();
|
3174 | }
|
3175 | catch (_a) {
|
3176 |
|
3177 | }
|
3178 | };
|
3179 | AbstractWrappingReadableStreamSource.prototype._setPendingRead = function (readPromise) {
|
3180 | var _this = this;
|
3181 | var pendingRead;
|
3182 | var finishRead = function () {
|
3183 | if (_this._pendingRead === pendingRead) {
|
3184 | _this._pendingRead = undefined;
|
3185 | }
|
3186 | };
|
3187 | this._pendingRead = pendingRead = readPromise.then(finishRead, finishRead);
|
3188 | };
|
3189 | AbstractWrappingReadableStreamSource.prototype._finishPendingRead = function () {
|
3190 | var _this = this;
|
3191 | if (!this._pendingRead) {
|
3192 | return undefined;
|
3193 | }
|
3194 | var afterRead = function () { return _this._finishPendingRead(); };
|
3195 | return this._pendingRead.then(afterRead, afterRead);
|
3196 | };
|
3197 | return AbstractWrappingReadableStreamSource;
|
3198 | }());
|
3199 | var WrappingReadableStreamDefaultSource = (function (_super) {
|
3200 | __extends(WrappingReadableStreamDefaultSource, _super);
|
3201 | function WrappingReadableStreamDefaultSource() {
|
3202 | return _super !== null && _super.apply(this, arguments) || this;
|
3203 | }
|
3204 | WrappingReadableStreamDefaultSource.prototype.pull = function () {
|
3205 | return this._pullWithDefaultReader();
|
3206 | };
|
3207 | return WrappingReadableStreamDefaultSource;
|
3208 | }(AbstractWrappingReadableStreamSource));
|
3209 | function toUint8Array(view) {
|
3210 | return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
3211 | }
|
3212 | function copyArrayBufferView(from, to) {
|
3213 | var fromArray = toUint8Array(from);
|
3214 | var toArray = toUint8Array(to);
|
3215 | toArray.set(fromArray, 0);
|
3216 | }
|
3217 | var WrappingReadableByteStreamSource = (function (_super) {
|
3218 | __extends(WrappingReadableByteStreamSource, _super);
|
3219 | function WrappingReadableByteStreamSource(underlyingStream) {
|
3220 | var _this = this;
|
3221 | var supportsByob = supportsByobReader(underlyingStream);
|
3222 | _this = _super.call(this, underlyingStream) || this;
|
3223 | _this._supportsByob = supportsByob;
|
3224 | return _this;
|
3225 | }
|
3226 | Object.defineProperty(WrappingReadableByteStreamSource.prototype, "type", {
|
3227 | get: function () {
|
3228 | return 'bytes';
|
3229 | },
|
3230 | enumerable: true,
|
3231 | configurable: true
|
3232 | });
|
3233 | WrappingReadableByteStreamSource.prototype._attachByobReader = function () {
|
3234 | if (this._readerMode === "byob" ) {
|
3235 | return;
|
3236 | }
|
3237 | assert(this._supportsByob);
|
3238 | this._detachReader();
|
3239 | var reader = this._underlyingStream.getReader({ mode: 'byob' });
|
3240 | this._readerMode = "byob" ;
|
3241 | this._attachReader(reader);
|
3242 | };
|
3243 | WrappingReadableByteStreamSource.prototype.pull = function () {
|
3244 | if (this._supportsByob) {
|
3245 | var byobRequest = this._readableStreamController.byobRequest;
|
3246 | if (byobRequest !== undefined) {
|
3247 | return this._pullWithByobRequest(byobRequest);
|
3248 | }
|
3249 | }
|
3250 | return this._pullWithDefaultReader();
|
3251 | };
|
3252 | WrappingReadableByteStreamSource.prototype._pullWithByobRequest = function (byobRequest) {
|
3253 | var _this = this;
|
3254 | this._attachByobReader();
|
3255 |
|
3256 |
|
3257 | var buffer = new Uint8Array(byobRequest.view.byteLength);
|
3258 |
|
3259 | var read = this._underlyingReader.read(buffer)
|
3260 | .then(function (result) {
|
3261 | _this._readableStreamController;
|
3262 | if (result.done) {
|
3263 | _this._tryClose();
|
3264 | byobRequest.respond(0);
|
3265 | }
|
3266 | else {
|
3267 | copyArrayBufferView(result.value, byobRequest.view);
|
3268 | byobRequest.respond(result.value.byteLength);
|
3269 | }
|
3270 | });
|
3271 | this._setPendingRead(read);
|
3272 | return read;
|
3273 | };
|
3274 | return WrappingReadableByteStreamSource;
|
3275 | }(AbstractWrappingReadableStreamSource));
|
3276 |
|
3277 | const isNode = typeof globalThis.process === 'object' &&
|
3278 | typeof globalThis.process.versions === 'object';
|
3279 |
|
3280 | const NodeReadableStream = isNode && void('stream').Readable;
|
3281 |
|
3282 |
|
3283 |
|
3284 |
|
3285 |
|
3286 |
|
3287 | function isStream(input) {
|
3288 | if (globalThis.ReadableStream && globalThis.ReadableStream.prototype.isPrototypeOf(input)) {
|
3289 | return 'web';
|
3290 | }
|
3291 | if (stream.ReadableStream.prototype.isPrototypeOf(input)) {
|
3292 | return 'ponyfill';
|
3293 | }
|
3294 | if (NodeReadableStream && NodeReadableStream.prototype.isPrototypeOf(input)) {
|
3295 | return 'node';
|
3296 | }
|
3297 | return false;
|
3298 | }
|
3299 |
|
3300 |
|
3301 |
|
3302 |
|
3303 |
|
3304 |
|
3305 | function isUint8Array(input) {
|
3306 | return Uint8Array.prototype.isPrototypeOf(input);
|
3307 | }
|
3308 |
|
3309 |
|
3310 |
|
3311 |
|
3312 |
|
3313 |
|
3314 | function concatUint8Array(arrays) {
|
3315 | if (arrays.length === 1) return arrays[0];
|
3316 |
|
3317 | let totalLength = 0;
|
3318 | for (let i = 0; i < arrays.length; i++) {
|
3319 | if (!isUint8Array(arrays[i])) {
|
3320 | throw new Error('concatUint8Array: Data must be in the form of a Uint8Array');
|
3321 | }
|
3322 |
|
3323 | totalLength += arrays[i].length;
|
3324 | }
|
3325 |
|
3326 | const result = new Uint8Array(totalLength);
|
3327 | let pos = 0;
|
3328 | arrays.forEach(function (element) {
|
3329 | result.set(element, pos);
|
3330 | pos += element.length;
|
3331 | });
|
3332 |
|
3333 | return result;
|
3334 | }
|
3335 |
|
3336 | const NodeBuffer = isNode && void('buffer').Buffer;
|
3337 | const NodeReadableStream$1 = isNode && void('stream').Readable;
|
3338 |
|
3339 |
|
3340 |
|
3341 |
|
3342 |
|
3343 |
|
3344 | let nodeToWeb;
|
3345 | let webToNode;
|
3346 |
|
3347 | if (NodeReadableStream$1) {
|
3348 |
|
3349 | |
3350 |
|
3351 |
|
3352 |
|
3353 |
|
3354 | nodeToWeb = function(nodeStream) {
|
3355 | return new stream.ReadableStream({
|
3356 | start(controller) {
|
3357 | nodeStream.pause();
|
3358 | nodeStream.on('data', chunk => {
|
3359 | if (NodeBuffer.isBuffer(chunk)) {
|
3360 | chunk = new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
3361 | }
|
3362 | controller.enqueue(chunk);
|
3363 | nodeStream.pause();
|
3364 | });
|
3365 | nodeStream.on('end', () => controller.close());
|
3366 | nodeStream.on('error', e => controller.error(e));
|
3367 | },
|
3368 | pull() {
|
3369 | nodeStream.resume();
|
3370 | },
|
3371 | cancel(reason) {
|
3372 | nodeStream.pause();
|
3373 | if (nodeStream.cancel) {
|
3374 | return nodeStream.cancel(reason);
|
3375 | }
|
3376 | }
|
3377 | });
|
3378 | };
|
3379 |
|
3380 |
|
3381 | class NodeReadable extends NodeReadableStream$1 {
|
3382 | constructor(webStream, options) {
|
3383 | super(options);
|
3384 | this._webStream = webStream;
|
3385 | this._reader = stream.getReader(webStream);
|
3386 | this._reading = false;
|
3387 | this._doneReadingPromise = Promise.resolve();
|
3388 | this._cancelling = false;
|
3389 | }
|
3390 |
|
3391 | _read(size) {
|
3392 | if (this._reading || this._cancelling) {
|
3393 | return;
|
3394 | }
|
3395 | this._reading = true;
|
3396 | const doRead = async () => {
|
3397 | try {
|
3398 | while (true) {
|
3399 | const { done, value } = await this._reader.read();
|
3400 | if (done) {
|
3401 | this.push(null);
|
3402 | break;
|
3403 | }
|
3404 | if (!this.push(value) || this._cancelling) {
|
3405 | this._reading = false;
|
3406 | break;
|
3407 | }
|
3408 | }
|
3409 | } catch(e) {
|
3410 | this.emit('error', e);
|
3411 | }
|
3412 | };
|
3413 | this._doneReadingPromise = doRead();
|
3414 | }
|
3415 |
|
3416 | async cancel(reason) {
|
3417 | this._cancelling = true;
|
3418 | await this._doneReadingPromise;
|
3419 | this._reader.releaseLock();
|
3420 | return this._webStream.cancel(reason);
|
3421 | }
|
3422 | }
|
3423 |
|
3424 | |
3425 |
|
3426 |
|
3427 |
|
3428 |
|
3429 | webToNode = function(webStream) {
|
3430 | return new NodeReadable(webStream);
|
3431 | };
|
3432 |
|
3433 | }
|
3434 |
|
3435 | const doneReadingSet = new WeakSet();
|
3436 | const externalBuffer = Symbol('externalBuffer');
|
3437 |
|
3438 |
|
3439 |
|
3440 |
|
3441 |
|
3442 |
|
3443 |
|
3444 |
|
3445 | function Reader(input) {
|
3446 | this.stream = input;
|
3447 | if (input[externalBuffer]) {
|
3448 | this[externalBuffer] = input[externalBuffer].slice();
|
3449 | }
|
3450 | let streamType = stream.isStream(input);
|
3451 | if (streamType === 'node') {
|
3452 | input = stream.nodeToWeb(input);
|
3453 | }
|
3454 | if (streamType) {
|
3455 | const reader = input.getReader();
|
3456 | this._read = reader.read.bind(reader);
|
3457 | this._releaseLock = () => {
|
3458 | reader.closed.catch(function() {});
|
3459 | reader.releaseLock();
|
3460 | };
|
3461 | return;
|
3462 | }
|
3463 | let doneReading = false;
|
3464 | this._read = async () => {
|
3465 | if (doneReading || doneReadingSet.has(input)) {
|
3466 | return { value: undefined, done: true };
|
3467 | }
|
3468 | doneReading = true;
|
3469 | return { value: input, done: false };
|
3470 | };
|
3471 | this._releaseLock = () => {
|
3472 | if (doneReading) {
|
3473 | try {
|
3474 | doneReadingSet.add(input);
|
3475 | } catch(e) {}
|
3476 | }
|
3477 | };
|
3478 | }
|
3479 |
|
3480 |
|
3481 |
|
3482 |
|
3483 |
|
3484 |
|
3485 | Reader.prototype.read = async function() {
|
3486 | if (this[externalBuffer] && this[externalBuffer].length) {
|
3487 | const value = this[externalBuffer].shift();
|
3488 | return { done: false, value };
|
3489 | }
|
3490 | return this._read();
|
3491 | };
|
3492 |
|
3493 |
|
3494 |
|
3495 |
|
3496 | Reader.prototype.releaseLock = function() {
|
3497 | if (this[externalBuffer]) {
|
3498 | this.stream[externalBuffer] = this[externalBuffer];
|
3499 | }
|
3500 | this._releaseLock();
|
3501 | };
|
3502 |
|
3503 |
|
3504 |
|
3505 |
|
3506 |
|
3507 |
|
3508 | Reader.prototype.readLine = async function() {
|
3509 | let buffer = [];
|
3510 | let returnVal;
|
3511 | while (!returnVal) {
|
3512 | let { done, value } = await this.read();
|
3513 | value += '';
|
3514 | if (done) {
|
3515 | if (buffer.length) return stream.concat(buffer);
|
3516 | return;
|
3517 | }
|
3518 | const lineEndIndex = value.indexOf('\n') + 1;
|
3519 | if (lineEndIndex) {
|
3520 | returnVal = stream.concat(buffer.concat(value.substr(0, lineEndIndex)));
|
3521 | buffer = [];
|
3522 | }
|
3523 | if (lineEndIndex !== value.length) {
|
3524 | buffer.push(value.substr(lineEndIndex));
|
3525 | }
|
3526 | }
|
3527 | this.unshift(...buffer);
|
3528 | return returnVal;
|
3529 | };
|
3530 |
|
3531 |
|
3532 |
|
3533 |
|
3534 |
|
3535 |
|
3536 | Reader.prototype.readByte = async function() {
|
3537 | const { done, value } = await this.read();
|
3538 | if (done) return;
|
3539 | const byte = value[0];
|
3540 | this.unshift(stream.slice(value, 1));
|
3541 | return byte;
|
3542 | };
|
3543 |
|
3544 |
|
3545 |
|
3546 |
|
3547 |
|
3548 |
|
3549 | Reader.prototype.readBytes = async function(length) {
|
3550 | const buffer = [];
|
3551 | let bufferLength = 0;
|
3552 | while (true) {
|
3553 | const { done, value } = await this.read();
|
3554 | if (done) {
|
3555 | if (buffer.length) return stream.concat(buffer);
|
3556 | return;
|
3557 | }
|
3558 | buffer.push(value);
|
3559 | bufferLength += value.length;
|
3560 | if (bufferLength >= length) {
|
3561 | const bufferConcat = stream.concat(buffer);
|
3562 | this.unshift(stream.slice(bufferConcat, length));
|
3563 | return stream.slice(bufferConcat, 0, length);
|
3564 | }
|
3565 | }
|
3566 | };
|
3567 |
|
3568 |
|
3569 |
|
3570 |
|
3571 |
|
3572 |
|
3573 | Reader.prototype.peekBytes = async function(length) {
|
3574 | const bytes = await this.readBytes(length);
|
3575 | this.unshift(bytes);
|
3576 | return bytes;
|
3577 | };
|
3578 |
|
3579 |
|
3580 |
|
3581 |
|
3582 |
|
3583 |
|
3584 | Reader.prototype.unshift = function(...values) {
|
3585 | if (!this[externalBuffer]) {
|
3586 | this[externalBuffer] = [];
|
3587 | }
|
3588 | if (
|
3589 | values.length === 1 && isUint8Array(values[0]) &&
|
3590 | this[externalBuffer].length && values[0].length &&
|
3591 | this[externalBuffer][0].byteOffset >= values[0].length
|
3592 | ) {
|
3593 | this[externalBuffer][0] = new Uint8Array(
|
3594 | this[externalBuffer][0].buffer,
|
3595 | this[externalBuffer][0].byteOffset - values[0].length,
|
3596 | this[externalBuffer][0].byteLength + values[0].length
|
3597 | );
|
3598 | return;
|
3599 | }
|
3600 | this[externalBuffer].unshift(...values.filter(value => value && value.length));
|
3601 | };
|
3602 |
|
3603 |
|
3604 |
|
3605 |
|
3606 |
|
3607 |
|
3608 |
|
3609 | Reader.prototype.readToEnd = async function(join=stream.concat) {
|
3610 | const result = [];
|
3611 | while (true) {
|
3612 | const { done, value } = await this.read();
|
3613 | if (done) break;
|
3614 | result.push(value);
|
3615 | }
|
3616 | return join(result);
|
3617 | };
|
3618 |
|
3619 | const { ReadableStream: ReadableStream$1, WritableStream: WritableStream$1, TransformStream: TransformStream$1 } = globalThis.TransformStream ? globalThis : {
|
3620 | ReadableStream: ReadableStream,
|
3621 | WritableStream: WritableStream,
|
3622 | TransformStream: TransformStream
|
3623 | };
|
3624 |
|
3625 | const toPonyfillReadable = globalThis.ReadableStream &&
|
3626 | ReadableStream$1 !== globalThis.ReadableStream &&
|
3627 | createReadableStreamWrapper(ReadableStream$1);
|
3628 |
|
3629 | const NodeBuffer$1 = isNode && void('buffer').Buffer;
|
3630 |
|
3631 |
|
3632 |
|
3633 |
|
3634 |
|
3635 |
|
3636 | function toStream(input) {
|
3637 | let streamType = isStream(input);
|
3638 | if (streamType === 'node') {
|
3639 | return nodeToWeb(input);
|
3640 | }
|
3641 | if (streamType === 'web' && toPonyfillReadable) {
|
3642 | return toPonyfillReadable(input);
|
3643 | }
|
3644 | if (streamType) {
|
3645 | return input;
|
3646 | }
|
3647 | return new ReadableStream$1({
|
3648 | start(controller) {
|
3649 | controller.enqueue(input);
|
3650 | controller.close();
|
3651 | }
|
3652 | });
|
3653 | }
|
3654 |
|
3655 |
|
3656 |
|
3657 |
|
3658 |
|
3659 |
|
3660 |
|
3661 | function concat(list) {
|
3662 | if (list.some(isStream)) {
|
3663 | return concatStream(list);
|
3664 | }
|
3665 | if (typeof list[0] === 'string') {
|
3666 | return list.join('');
|
3667 | }
|
3668 | if (NodeBuffer$1 && NodeBuffer$1.isBuffer(list[0])) {
|
3669 | return NodeBuffer$1.concat(list);
|
3670 | }
|
3671 | return concatUint8Array(list);
|
3672 | }
|
3673 |
|
3674 |
|
3675 |
|
3676 |
|
3677 |
|
3678 |
|
3679 | function concatStream(list) {
|
3680 | list = list.map(toStream);
|
3681 | const transform = transformWithCancel(async function(reason) {
|
3682 | await Promise.all(transforms.map(stream => cancel(stream, reason)));
|
3683 | });
|
3684 | let prev = Promise.resolve();
|
3685 | const transforms = list.map((stream, i) => transformPair(stream, (readable, writable) => {
|
3686 | prev = prev.then(() => pipe(readable, transform.writable, {
|
3687 | preventClose: i !== list.length - 1
|
3688 | }));
|
3689 | return prev;
|
3690 | }));
|
3691 | return transform.readable;
|
3692 | }
|
3693 |
|
3694 |
|
3695 |
|
3696 |
|
3697 |
|
3698 |
|
3699 | function getReader(input) {
|
3700 | return new Reader(input);
|
3701 | }
|
3702 |
|
3703 |
|
3704 |
|
3705 |
|
3706 |
|
3707 |
|
3708 | function getWriter(input) {
|
3709 | const writer = input.getWriter();
|
3710 | const releaseLock = writer.releaseLock;
|
3711 | writer.releaseLock = () => {
|
3712 | writer.closed.catch(function() {});
|
3713 | releaseLock.call(writer);
|
3714 | };
|
3715 | return writer;
|
3716 | }
|
3717 |
|
3718 |
|
3719 |
|
3720 |
|
3721 |
|
3722 |
|
3723 |
|
3724 |
|
3725 |
|
3726 | async function pipe(input, target, options) {
|
3727 | input = toStream(input);
|
3728 | try {
|
3729 | if (input[externalBuffer]) {
|
3730 | const writer = getWriter(target);
|
3731 | for (let i = 0; i < input[externalBuffer].length; i++) {
|
3732 | await writer.ready;
|
3733 | await writer.write(input[externalBuffer][i]);
|
3734 | }
|
3735 | writer.releaseLock();
|
3736 | }
|
3737 | return await input.pipeTo(target, options);
|
3738 | } catch(e) {}
|
3739 | }
|
3740 |
|
3741 |
|
3742 |
|
3743 |
|
3744 |
|
3745 |
|
3746 |
|
3747 | function transformRaw(input, options) {
|
3748 | const transformStream = new TransformStream$1(options);
|
3749 | pipe(input, transformStream.writable);
|
3750 | return transformStream.readable;
|
3751 | }
|
3752 |
|
3753 |
|
3754 |
|
3755 |
|
3756 |
|
3757 |
|
3758 | function transformWithCancel(cancel) {
|
3759 | let pulled = false;
|
3760 | let backpressureChangePromiseResolve;
|
3761 | let outputController;
|
3762 | return {
|
3763 | readable: new ReadableStream$1({
|
3764 | start(controller) {
|
3765 | outputController = controller;
|
3766 | },
|
3767 | pull() {
|
3768 | if (backpressureChangePromiseResolve) {
|
3769 | backpressureChangePromiseResolve();
|
3770 | } else {
|
3771 | pulled = true;
|
3772 | }
|
3773 | },
|
3774 | cancel
|
3775 | }, {highWaterMark: 0}),
|
3776 | writable: new WritableStream$1({
|
3777 | write: async function(chunk) {
|
3778 | outputController.enqueue(chunk);
|
3779 | if (!pulled) {
|
3780 | await new Promise(resolve => {
|
3781 | backpressureChangePromiseResolve = resolve;
|
3782 | });
|
3783 | backpressureChangePromiseResolve = null;
|
3784 | } else {
|
3785 | pulled = false;
|
3786 | }
|
3787 | },
|
3788 | close: outputController.close.bind(outputController),
|
3789 | abort: outputController.error.bind(outputController)
|
3790 | })
|
3791 | };
|
3792 | }
|
3793 |
|
3794 |
|
3795 |
|
3796 |
|
3797 |
|
3798 |
|
3799 |
|
3800 |
|
3801 | function transform(input, process = () => undefined, finish = () => undefined) {
|
3802 | if (isStream(input)) {
|
3803 | return transformRaw(input, {
|
3804 | async transform(value, controller) {
|
3805 | try {
|
3806 | const result = await process(value);
|
3807 | if (result !== undefined) controller.enqueue(result);
|
3808 | } catch(e) {
|
3809 | controller.error(e);
|
3810 | }
|
3811 | },
|
3812 | async flush(controller) {
|
3813 | try {
|
3814 | const result = await finish();
|
3815 | if (result !== undefined) controller.enqueue(result);
|
3816 | } catch(e) {
|
3817 | controller.error(e);
|
3818 | }
|
3819 | }
|
3820 | });
|
3821 | }
|
3822 | const result1 = process(input);
|
3823 | const result2 = finish();
|
3824 | if (result1 !== undefined && result2 !== undefined) return concat([result1, result2]);
|
3825 | return result1 !== undefined ? result1 : result2;
|
3826 | }
|
3827 |
|
3828 |
|
3829 |
|
3830 |
|
3831 |
|
3832 |
|
3833 |
|
3834 |
|
3835 |
|
3836 |
|
3837 | function transformPair(input, fn) {
|
3838 | let incomingTransformController;
|
3839 | const incoming = new TransformStream$1({
|
3840 | start(controller) {
|
3841 | incomingTransformController = controller;
|
3842 | }
|
3843 | });
|
3844 |
|
3845 | const pipeDonePromise = pipe(input, incoming.writable);
|
3846 |
|
3847 | const outgoing = transformWithCancel(async function() {
|
3848 | incomingTransformController.error(new Error('Readable side was canceled.'));
|
3849 | await pipeDonePromise;
|
3850 | await new Promise(setTimeout);
|
3851 | });
|
3852 | fn(incoming.readable, outgoing.writable);
|
3853 | return outgoing.readable;
|
3854 | }
|
3855 |
|
3856 |
|
3857 |
|
3858 |
|
3859 |
|
3860 |
|
3861 |
|
3862 |
|
3863 |
|
3864 |
|
3865 | function parse(input, fn) {
|
3866 | let returnValue;
|
3867 | const transformed = transformPair(input, (readable, writable) => {
|
3868 | const reader = getReader(readable);
|
3869 | reader.remainder = () => {
|
3870 | reader.releaseLock();
|
3871 | pipe(readable, writable);
|
3872 | return transformed;
|
3873 | };
|
3874 | returnValue = fn(reader);
|
3875 | });
|
3876 | return returnValue;
|
3877 | }
|
3878 |
|
3879 |
|
3880 |
|
3881 |
|
3882 |
|
3883 |
|
3884 |
|
3885 |
|
3886 | function tee(input) {
|
3887 | if (isStream(input)) {
|
3888 | const teed = toStream(input).tee();
|
3889 | teed[0][externalBuffer] = teed[1][externalBuffer] = input[externalBuffer];
|
3890 | return teed;
|
3891 | }
|
3892 | return [slice(input), slice(input)];
|
3893 | }
|
3894 |
|
3895 |
|
3896 |
|
3897 |
|
3898 |
|
3899 |
|
3900 |
|
3901 |
|
3902 | function clone(input) {
|
3903 | if (isStream(input)) {
|
3904 | const teed = tee(input);
|
3905 | overwrite(input, teed[0]);
|
3906 | return teed[1];
|
3907 | }
|
3908 | return slice(input);
|
3909 | }
|
3910 |
|
3911 |
|
3912 |
|
3913 |
|
3914 |
|
3915 |
|
3916 |
|
3917 |
|
3918 |
|
3919 | function passiveClone(input) {
|
3920 | if (isStream(input)) {
|
3921 | return new ReadableStream$1({
|
3922 | start(controller) {
|
3923 | const transformed = transformPair(input, async (readable, writable) => {
|
3924 | const reader = getReader(readable);
|
3925 | const writer = getWriter(writable);
|
3926 | try {
|
3927 | while (true) {
|
3928 | await writer.ready;
|
3929 | const { done, value } = await reader.read();
|
3930 | if (done) {
|
3931 | try { controller.close(); } catch(e) {}
|
3932 | await writer.close();
|
3933 | return;
|
3934 | }
|
3935 | try { controller.enqueue(value); } catch(e) {}
|
3936 | await writer.write(value);
|
3937 | }
|
3938 | } catch(e) {
|
3939 | controller.error(e);
|
3940 | await writer.abort(e);
|
3941 | }
|
3942 | });
|
3943 | overwrite(input, transformed);
|
3944 | }
|
3945 | });
|
3946 | }
|
3947 | return slice(input);
|
3948 | }
|
3949 |
|
3950 |
|
3951 |
|
3952 |
|
3953 |
|
3954 |
|
3955 |
|
3956 | function overwrite(input, clone) {
|
3957 |
|
3958 | Object.entries(Object.getOwnPropertyDescriptors(ReadableStream$1.prototype)).forEach(([name, descriptor]) => {
|
3959 | if (name === 'constructor') {
|
3960 | return;
|
3961 | }
|
3962 | if (descriptor.value) {
|
3963 | descriptor.value = descriptor.value.bind(clone);
|
3964 | } else {
|
3965 | descriptor.get = descriptor.get.bind(clone);
|
3966 | }
|
3967 | Object.defineProperty(input, name, descriptor);
|
3968 | });
|
3969 | }
|
3970 |
|
3971 |
|
3972 |
|
3973 |
|
3974 |
|
3975 |
|
3976 | function slice(input, begin=0, end=Infinity) {
|
3977 | if (isStream(input)) {
|
3978 | if (begin >= 0 && end >= 0) {
|
3979 | let bytesRead = 0;
|
3980 | return transformRaw(input, {
|
3981 | transform(value, controller) {
|
3982 | if (bytesRead < end) {
|
3983 | if (bytesRead + value.length >= begin) {
|
3984 | controller.enqueue(slice(value, Math.max(begin - bytesRead, 0), end - bytesRead));
|
3985 | }
|
3986 | bytesRead += value.length;
|
3987 | } else {
|
3988 | controller.terminate();
|
3989 | }
|
3990 | }
|
3991 | });
|
3992 | }
|
3993 | if (begin < 0 && (end < 0 || end === Infinity)) {
|
3994 | let lastBytes = [];
|
3995 | return transform(input, value => {
|
3996 | if (value.length >= -begin) lastBytes = [value];
|
3997 | else lastBytes.push(value);
|
3998 | }, () => slice(concat(lastBytes), begin, end));
|
3999 | }
|
4000 | if (begin === 0 && end < 0) {
|
4001 | let lastBytes;
|
4002 | return transform(input, value => {
|
4003 | const returnValue = lastBytes ? concat([lastBytes, value]) : value;
|
4004 | if (returnValue.length >= -end) {
|
4005 | lastBytes = slice(returnValue, end);
|
4006 | return slice(returnValue, begin, end);
|
4007 | } else {
|
4008 | lastBytes = returnValue;
|
4009 | }
|
4010 | });
|
4011 | }
|
4012 | console.warn(`stream.slice(input, ${begin}, ${end}) not implemented efficiently.`);
|
4013 | return fromAsync(async () => slice(await readToEnd(input), begin, end));
|
4014 | }
|
4015 | if (input[externalBuffer]) {
|
4016 | input = concat(input[externalBuffer].concat([input]));
|
4017 | }
|
4018 | if (isUint8Array(input) && !(NodeBuffer$1 && NodeBuffer$1.isBuffer(input))) {
|
4019 | if (end === Infinity) end = input.length;
|
4020 | return input.subarray(begin, end);
|
4021 | }
|
4022 | return input.slice(begin, end);
|
4023 | }
|
4024 |
|
4025 |
|
4026 |
|
4027 |
|
4028 |
|
4029 |
|
4030 |
|
4031 |
|
4032 | async function readToEnd(input, concat) {
|
4033 | if (isStream(input)) {
|
4034 | return getReader(input).readToEnd(concat);
|
4035 | }
|
4036 | return input;
|
4037 | }
|
4038 |
|
4039 |
|
4040 |
|
4041 |
|
4042 |
|
4043 |
|
4044 |
|
4045 |
|
4046 | async function cancel(input, reason) {
|
4047 | if (isStream(input) && input.cancel) {
|
4048 | return input.cancel(reason);
|
4049 | }
|
4050 | }
|
4051 |
|
4052 |
|
4053 |
|
4054 |
|
4055 |
|
4056 |
|
4057 | function fromAsync(fn) {
|
4058 | return new ReadableStream$1({
|
4059 | pull: async controller => {
|
4060 | try {
|
4061 | controller.enqueue(await fn());
|
4062 | controller.close();
|
4063 | } catch(e) {
|
4064 | controller.error(e);
|
4065 | }
|
4066 | }
|
4067 | });
|
4068 | }
|
4069 |
|
4070 |
|
4071 | var stream = { ReadableStream: ReadableStream$1, WritableStream: WritableStream$1, TransformStream: TransformStream$1, isStream, isUint8Array, toStream, concatUint8Array, concatStream, concat, getReader, getWriter, pipe, transformRaw, transform, transformPair, parse, clone, passiveClone, slice, readToEnd, cancel, fromAsync, nodeToWeb, webToNode };
|
4072 |
|
4073 |
|
4074 |
|
4075 |
|
4076 |
|
4077 |
|
4078 |
|
4079 |
|
4080 |
|
4081 |
|
4082 |
|
4083 |
|
4084 |
|
4085 |
|
4086 |
|
4087 |
|
4088 | class BigInteger {
|
4089 | |
4090 |
|
4091 |
|
4092 |
|
4093 |
|
4094 | constructor(n) {
|
4095 | if (n === undefined) {
|
4096 | throw new Error('Invalid BigInteger input');
|
4097 | }
|
4098 |
|
4099 | if (n instanceof Uint8Array) {
|
4100 | const bytes = n;
|
4101 | const hex = new Array(bytes.length);
|
4102 | for (let i = 0; i < bytes.length; i++) {
|
4103 | const hexByte = bytes[i].toString(16);
|
4104 | hex[i] = (bytes[i] <= 0xF) ? ('0' + hexByte) : hexByte;
|
4105 | }
|
4106 | this.value = BigInt('0x0' + hex.join(''));
|
4107 | } else {
|
4108 | this.value = BigInt(n);
|
4109 | }
|
4110 | }
|
4111 |
|
4112 | clone() {
|
4113 | return new BigInteger(this.value);
|
4114 | }
|
4115 |
|
4116 | |
4117 |
|
4118 |
|
4119 | iinc() {
|
4120 | this.value++;
|
4121 | return this;
|
4122 | }
|
4123 |
|
4124 | |
4125 |
|
4126 |
|
4127 |
|
4128 | inc() {
|
4129 | return this.clone().iinc();
|
4130 | }
|
4131 |
|
4132 | |
4133 |
|
4134 |
|
4135 | idec() {
|
4136 | this.value--;
|
4137 | return this;
|
4138 | }
|
4139 |
|
4140 | |
4141 |
|
4142 |
|
4143 |
|
4144 | dec() {
|
4145 | return this.clone().idec();
|
4146 | }
|
4147 |
|
4148 | |
4149 |
|
4150 |
|
4151 |
|
4152 | iadd(x) {
|
4153 | this.value += x.value;
|
4154 | return this;
|
4155 | }
|
4156 |
|
4157 | |
4158 |
|
4159 |
|
4160 |
|
4161 |
|
4162 | add(x) {
|
4163 | return this.clone().iadd(x);
|
4164 | }
|
4165 |
|
4166 | |
4167 |
|
4168 |
|
4169 |
|
4170 | isub(x) {
|
4171 | this.value -= x.value;
|
4172 | return this;
|
4173 | }
|
4174 |
|
4175 | |
4176 |
|
4177 |
|
4178 |
|
4179 |
|
4180 | sub(x) {
|
4181 | return this.clone().isub(x);
|
4182 | }
|
4183 |
|
4184 | |
4185 |
|
4186 |
|
4187 |
|
4188 | imul(x) {
|
4189 | this.value *= x.value;
|
4190 | return this;
|
4191 | }
|
4192 |
|
4193 | |
4194 |
|
4195 |
|
4196 |
|
4197 |
|
4198 | mul(x) {
|
4199 | return this.clone().imul(x);
|
4200 | }
|
4201 |
|
4202 | |
4203 |
|
4204 |
|
4205 |
|
4206 | imod(m) {
|
4207 | this.value %= m.value;
|
4208 | if (this.isNegative()) {
|
4209 | this.iadd(m);
|
4210 | }
|
4211 | return this;
|
4212 | }
|
4213 |
|
4214 | |
4215 |
|
4216 |
|
4217 |
|
4218 |
|
4219 | mod(m) {
|
4220 | return this.clone().imod(m);
|
4221 | }
|
4222 |
|
4223 | |
4224 |
|
4225 |
|
4226 |
|
4227 |
|
4228 |
|
4229 | modExp(e, n) {
|
4230 | if (n.isZero()) throw Error("Modulo cannot be zero");
|
4231 | if (n.isOne()) return new BigInteger(0);
|
4232 | if (e.isNegative()) throw Error("Unsopported negative exponent");
|
4233 |
|
4234 | let exp = e.value;
|
4235 | let x = this.value;
|
4236 |
|
4237 | x %= n.value;
|
4238 | let r = BigInt(1);
|
4239 | while (exp > BigInt(0)) {
|
4240 | const lsb = exp & BigInt(1);
|
4241 | exp >>= BigInt(1);
|
4242 |
|
4243 | const rx = (r * x) % n.value;
|
4244 |
|
4245 | r = lsb ? rx : r;
|
4246 | x = (x * x) % n.value;
|
4247 | }
|
4248 | return new BigInteger(r);
|
4249 | }
|
4250 |
|
4251 |
|
4252 | |
4253 |
|
4254 |
|
4255 |
|
4256 |
|
4257 |
|
4258 |
|
4259 | modInv(n) {
|
4260 | const { gcd, x } = this._egcd(n);
|
4261 | if (!gcd.isOne()) {
|
4262 | throw new Error('Inverse does not exist');
|
4263 | }
|
4264 | return x.add(n).mod(n);
|
4265 | }
|
4266 |
|
4267 | |
4268 |
|
4269 |
|
4270 |
|
4271 |
|
4272 |
|
4273 | _egcd(b) {
|
4274 | let x = BigInt(0);
|
4275 | let y = BigInt(1);
|
4276 | let xPrev = BigInt(1);
|
4277 | let yPrev = BigInt(0);
|
4278 |
|
4279 | let a = this.value;
|
4280 | b = b.value;
|
4281 |
|
4282 | while (b !== BigInt(0)) {
|
4283 | const q = a / b;
|
4284 | let tmp = x;
|
4285 | x = xPrev - q * x;
|
4286 | xPrev = tmp;
|
4287 |
|
4288 | tmp = y;
|
4289 | y = yPrev - q * y;
|
4290 | yPrev = tmp;
|
4291 |
|
4292 | tmp = b;
|
4293 | b = a % b;
|
4294 | a = tmp;
|
4295 | }
|
4296 |
|
4297 | return {
|
4298 | x: new BigInteger(xPrev),
|
4299 | y: new BigInteger(yPrev),
|
4300 | gcd: new BigInteger(a)
|
4301 | };
|
4302 | }
|
4303 |
|
4304 | |
4305 |
|
4306 |
|
4307 |
|
4308 |
|
4309 | gcd(b) {
|
4310 | let a = this.value;
|
4311 | b = b.value;
|
4312 | while (b !== BigInt(0)) {
|
4313 | const tmp = b;
|
4314 | b = a % b;
|
4315 | a = tmp;
|
4316 | }
|
4317 | return new BigInteger(a);
|
4318 | }
|
4319 |
|
4320 | |
4321 |
|
4322 |
|
4323 |
|
4324 | ileftShift(x) {
|
4325 | this.value <<= x.value;
|
4326 | return this;
|
4327 | }
|
4328 |
|
4329 | |
4330 |
|
4331 |
|
4332 |
|
4333 |
|
4334 | leftShift(x) {
|
4335 | return this.clone().ileftShift(x);
|
4336 | }
|
4337 |
|
4338 | |
4339 |
|
4340 |
|
4341 |
|
4342 | irightShift(x) {
|
4343 | this.value >>= x.value;
|
4344 | return this;
|
4345 | }
|
4346 |
|
4347 | |
4348 |
|
4349 |
|
4350 |
|
4351 |
|
4352 | rightShift(x) {
|
4353 | return this.clone().irightShift(x);
|
4354 | }
|
4355 |
|
4356 | |
4357 |
|
4358 |
|
4359 |
|
4360 |
|
4361 | equal(x) {
|
4362 | return this.value === x.value;
|
4363 | }
|
4364 |
|
4365 | |
4366 |
|
4367 |
|
4368 |
|
4369 |
|
4370 | lt(x) {
|
4371 | return this.value < x.value;
|
4372 | }
|
4373 |
|
4374 | |
4375 |
|
4376 |
|
4377 |
|
4378 |
|
4379 | lte(x) {
|
4380 | return this.value <= x.value;
|
4381 | }
|
4382 |
|
4383 | |
4384 |
|
4385 |
|
4386 |
|
4387 |
|
4388 | gt(x) {
|
4389 | return this.value > x.value;
|
4390 | }
|
4391 |
|
4392 | |
4393 |
|
4394 |
|
4395 |
|
4396 |
|
4397 | gte(x) {
|
4398 | return this.value >= x.value;
|
4399 | }
|
4400 |
|
4401 | isZero() {
|
4402 | return this.value === BigInt(0);
|
4403 | }
|
4404 |
|
4405 | isOne() {
|
4406 | return this.value === BigInt(1);
|
4407 | }
|
4408 |
|
4409 | isNegative() {
|
4410 | return this.value < BigInt(0);
|
4411 | }
|
4412 |
|
4413 | isEven() {
|
4414 | return !(this.value & BigInt(1));
|
4415 | }
|
4416 |
|
4417 | abs() {
|
4418 | const res = this.clone();
|
4419 | if (this.isNegative()) {
|
4420 | res.value = -res.value;
|
4421 | }
|
4422 | return res;
|
4423 | }
|
4424 |
|
4425 | |
4426 |
|
4427 |
|
4428 |
|
4429 | toString() {
|
4430 | return this.value.toString();
|
4431 | }
|
4432 |
|
4433 | |
4434 |
|
4435 |
|
4436 |
|
4437 |
|
4438 | toNumber() {
|
4439 | const number = Number(this.value);
|
4440 | if (number > Number.MAX_SAFE_INTEGER) {
|
4441 |
|
4442 | throw new Error('Number can only safely store up to 53 bits');
|
4443 | }
|
4444 | return number;
|
4445 | }
|
4446 |
|
4447 | |
4448 |
|
4449 |
|
4450 |
|
4451 |
|
4452 | getBit(i) {
|
4453 | const bit = (this.value >> BigInt(i)) & BigInt(1);
|
4454 | return (bit === BigInt(0)) ? 0 : 1;
|
4455 | }
|
4456 |
|
4457 | |
4458 |
|
4459 |
|
4460 |
|
4461 | bitLength() {
|
4462 | const zero = new BigInteger(0);
|
4463 | const one = new BigInteger(1);
|
4464 | const negOne = new BigInteger(-1);
|
4465 |
|
4466 |
|
4467 |
|
4468 | const target = this.isNegative() ? negOne : zero;
|
4469 | let bitlen = 1;
|
4470 | const tmp = this.clone();
|
4471 | while (!tmp.irightShift(one).equal(target)) {
|
4472 | bitlen++;
|
4473 | }
|
4474 | return bitlen;
|
4475 | }
|
4476 |
|
4477 | |
4478 |
|
4479 |
|
4480 |
|
4481 | byteLength() {
|
4482 | const zero = new BigInteger(0);
|
4483 | const negOne = new BigInteger(-1);
|
4484 |
|
4485 | const target = this.isNegative() ? negOne : zero;
|
4486 | const eight = new BigInteger(8);
|
4487 | let len = 1;
|
4488 | const tmp = this.clone();
|
4489 | while (!tmp.irightShift(eight).equal(target)) {
|
4490 | len++;
|
4491 | }
|
4492 | return len;
|
4493 | }
|
4494 |
|
4495 | |
4496 |
|
4497 |
|
4498 |
|
4499 |
|
4500 |
|
4501 | toUint8Array(endian = 'be', length) {
|
4502 |
|
4503 |
|
4504 | let hex = this.value.toString(16);
|
4505 | if (hex.length % 2 === 1) {
|
4506 | hex = '0' + hex;
|
4507 | }
|
4508 |
|
4509 | const rawLength = hex.length / 2;
|
4510 | const bytes = new Uint8Array(length || rawLength);
|
4511 |
|
4512 | const offset = length ? (length - rawLength) : 0;
|
4513 | let i = 0;
|
4514 | while (i < rawLength) {
|
4515 | bytes[i + offset] = parseInt(hex.slice(2 * i, 2 * i + 2), 16);
|
4516 | i++;
|
4517 | }
|
4518 |
|
4519 | if (endian !== 'be') {
|
4520 | bytes.reverse();
|
4521 | }
|
4522 |
|
4523 | return bytes;
|
4524 | }
|
4525 | }
|
4526 |
|
4527 | async function getBigInteger() {
|
4528 | if (util.detectBigInt()) {
|
4529 | return BigInteger;
|
4530 | } else {
|
4531 | const { default: BigInteger } = await Promise.resolve().then(function () { return bn_interface; });
|
4532 | return BigInteger;
|
4533 | }
|
4534 | }
|
4535 |
|
4536 |
|
4537 |
|
4538 | const debugMode = globalThis.process && globalThis.process.env.NODE_ENV === 'development';
|
4539 |
|
4540 | const util = {
|
4541 | isString: function(data) {
|
4542 | return typeof data === 'string' || String.prototype.isPrototypeOf(data);
|
4543 | },
|
4544 |
|
4545 | isArray: function(data) {
|
4546 | return Array.prototype.isPrototypeOf(data);
|
4547 | },
|
4548 |
|
4549 | isBigInteger: function(data) {
|
4550 | return data !== null && typeof data === 'object' && data.value &&
|
4551 |
|
4552 | (typeof data.value === 'bigint' || this.isBN(data.value));
|
4553 | },
|
4554 |
|
4555 | isBN: function(data) {
|
4556 | return data !== null && typeof data === 'object' &&
|
4557 | (data.constructor.name === 'BN' ||
|
4558 | (data.constructor.wordSize === 26 && Array.isArray(data.words)));
|
4559 | },
|
4560 |
|
4561 | isUint8Array: stream.isUint8Array,
|
4562 |
|
4563 | isStream: stream.isStream,
|
4564 |
|
4565 | |
4566 |
|
4567 |
|
4568 |
|
4569 |
|
4570 | restoreStreams: function(obj, streaming) {
|
4571 | if (Object.prototype.toString.call(obj) === '[object MessagePort]') {
|
4572 | return new (streaming === 'web' ? globalThis.ReadableStream : stream.ReadableStream)({
|
4573 | pull(controller) {
|
4574 | return new Promise(resolve => {
|
4575 | obj.onmessage = evt => {
|
4576 | const { done, value, error } = evt.data;
|
4577 | if (error) {
|
4578 | controller.error(new Error(error));
|
4579 | } else if (!done) {
|
4580 | controller.enqueue(value);
|
4581 | } else {
|
4582 | controller.close();
|
4583 | }
|
4584 | resolve();
|
4585 | };
|
4586 | obj.postMessage({ action: 'read' });
|
4587 | });
|
4588 | },
|
4589 | cancel() {
|
4590 | return new Promise(resolve => {
|
4591 | obj.onmessage = resolve;
|
4592 | obj.postMessage({ action: 'cancel' });
|
4593 | });
|
4594 | }
|
4595 | }, { highWaterMark: 0 });
|
4596 | }
|
4597 | if (Object.prototype.isPrototypeOf(obj) && !Uint8Array.prototype.isPrototypeOf(obj)) {
|
4598 | Object.entries(obj).forEach(([key, value]) => {
|
4599 | obj[key] = util.restoreStreams(value, streaming);
|
4600 | });
|
4601 | }
|
4602 | return obj;
|
4603 | },
|
4604 |
|
4605 | readNumber: function (bytes) {
|
4606 | let n = 0;
|
4607 | for (let i = 0; i < bytes.length; i++) {
|
4608 | n += (256 ** i) * bytes[bytes.length - 1 - i];
|
4609 | }
|
4610 | return n;
|
4611 | },
|
4612 |
|
4613 | writeNumber: function (n, bytes) {
|
4614 | const b = new Uint8Array(bytes);
|
4615 | for (let i = 0; i < bytes; i++) {
|
4616 | b[i] = (n >> (8 * (bytes - i - 1))) & 0xFF;
|
4617 | }
|
4618 |
|
4619 | return b;
|
4620 | },
|
4621 |
|
4622 | readDate: function (bytes) {
|
4623 | const n = util.readNumber(bytes);
|
4624 | const d = new Date(n * 1000);
|
4625 | return d;
|
4626 | },
|
4627 |
|
4628 | writeDate: function (time) {
|
4629 | const numeric = Math.floor(time.getTime() / 1000);
|
4630 |
|
4631 | return util.writeNumber(numeric, 4);
|
4632 | },
|
4633 |
|
4634 | normalizeDate: function (time = Date.now()) {
|
4635 | return time === null || time === Infinity ? time : new Date(Math.floor(+time / 1000) * 1000);
|
4636 | },
|
4637 |
|
4638 | |
4639 |
|
4640 |
|
4641 |
|
4642 |
|
4643 | strToHex: function (str) {
|
4644 | if (str === null) {
|
4645 | return "";
|
4646 | }
|
4647 | const r = [];
|
4648 | const e = str.length;
|
4649 | let c = 0;
|
4650 | let h;
|
4651 | while (c < e) {
|
4652 | h = str.charCodeAt(c++).toString(16);
|
4653 | while (h.length < 2) {
|
4654 | h = "0" + h;
|
4655 | }
|
4656 | r.push("" + h);
|
4657 | }
|
4658 | return r.join('');
|
4659 | },
|
4660 |
|
4661 | |
4662 |
|
4663 |
|
4664 |
|
4665 |
|
4666 | hexToStr: function (hex) {
|
4667 | let str = '';
|
4668 | for (let i = 0; i < hex.length; i += 2) {
|
4669 | str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
|
4670 | }
|
4671 | return str;
|
4672 | },
|
4673 |
|
4674 | |
4675 |
|
4676 |
|
4677 |
|
4678 |
|
4679 | readMPI: function (bytes) {
|
4680 | const bits = (bytes[0] << 8) | bytes[1];
|
4681 | const bytelen = (bits + 7) >>> 3;
|
4682 | return bytes.subarray(2, 2 + bytelen);
|
4683 | },
|
4684 |
|
4685 | |
4686 |
|
4687 |
|
4688 |
|
4689 |
|
4690 |
|
4691 | leftPad(bytes, length) {
|
4692 | const padded = new Uint8Array(length);
|
4693 | const offset = length - bytes.length;
|
4694 | padded.set(bytes, offset);
|
4695 | return padded;
|
4696 | },
|
4697 |
|
4698 | |
4699 |
|
4700 |
|
4701 |
|
4702 |
|
4703 | uint8ArrayToMpi: function (bin) {
|
4704 | let i;
|
4705 | for (i = 0; i < bin.length; i++) if (bin[i] !== 0) break;
|
4706 | if (i === bin.length) {
|
4707 | throw new Error('Zero MPI');
|
4708 | }
|
4709 | const stripped = bin.subarray(i);
|
4710 | const size = (stripped.length - 1) * 8 + util.nbits(stripped[0]);
|
4711 | const prefix = Uint8Array.from([(size & 0xFF00) >> 8, size & 0xFF]);
|
4712 | return util.concatUint8Array([prefix, stripped]);
|
4713 | },
|
4714 |
|
4715 | |
4716 |
|
4717 |
|
4718 |
|
4719 |
|
4720 | hexToUint8Array: function (hex) {
|
4721 | const result = new Uint8Array(hex.length >> 1);
|
4722 | for (let k = 0; k < hex.length >> 1; k++) {
|
4723 | result[k] = parseInt(hex.substr(k << 1, 2), 16);
|
4724 | }
|
4725 | return result;
|
4726 | },
|
4727 |
|
4728 | |
4729 |
|
4730 |
|
4731 |
|
4732 |
|
4733 | uint8ArrayToHex: function (bytes) {
|
4734 | const r = [];
|
4735 | const e = bytes.length;
|
4736 | let c = 0;
|
4737 | let h;
|
4738 | while (c < e) {
|
4739 | h = bytes[c++].toString(16);
|
4740 | while (h.length < 2) {
|
4741 | h = "0" + h;
|
4742 | }
|
4743 | r.push("" + h);
|
4744 | }
|
4745 | return r.join('');
|
4746 | },
|
4747 |
|
4748 | |
4749 |
|
4750 |
|
4751 |
|
4752 |
|
4753 | strToUint8Array: function (str) {
|
4754 | return stream.transform(str, str => {
|
4755 | if (!util.isString(str)) {
|
4756 | throw new Error('strToUint8Array: Data must be in the form of a string');
|
4757 | }
|
4758 |
|
4759 | const result = new Uint8Array(str.length);
|
4760 | for (let i = 0; i < str.length; i++) {
|
4761 | result[i] = str.charCodeAt(i);
|
4762 | }
|
4763 | return result;
|
4764 | });
|
4765 | },
|
4766 |
|
4767 | |
4768 |
|
4769 |
|
4770 |
|
4771 |
|
4772 | uint8ArrayToStr: function (bytes) {
|
4773 | bytes = new Uint8Array(bytes);
|
4774 | const result = [];
|
4775 | const bs = 1 << 14;
|
4776 | const j = bytes.length;
|
4777 |
|
4778 | for (let i = 0; i < j; i += bs) {
|
4779 | result.push(String.fromCharCode.apply(String, bytes.subarray(i, i + bs < j ? i + bs : j)));
|
4780 | }
|
4781 | return result.join('');
|
4782 | },
|
4783 |
|
4784 | |
4785 |
|
4786 |
|
4787 |
|
4788 |
|
4789 | encodeUtf8: function (str) {
|
4790 | const encoder = new TextEncoder('utf-8');
|
4791 |
|
4792 | function process(value, lastChunk = false) {
|
4793 | return encoder.encode(value, { stream: !lastChunk });
|
4794 | }
|
4795 | return stream.transform(str, process, () => process('', true));
|
4796 | },
|
4797 |
|
4798 | |
4799 |
|
4800 |
|
4801 |
|
4802 |
|
4803 | decodeUtf8: function (utf8) {
|
4804 | const decoder = new TextDecoder('utf-8');
|
4805 |
|
4806 | function process(value, lastChunk = false) {
|
4807 | return decoder.decode(value, { stream: !lastChunk });
|
4808 | }
|
4809 | return stream.transform(utf8, process, () => process(new Uint8Array(), true));
|
4810 | },
|
4811 |
|
4812 | |
4813 |
|
4814 |
|
4815 |
|
4816 |
|
4817 |
|
4818 | concat: stream.concat,
|
4819 |
|
4820 | |
4821 |
|
4822 |
|
4823 |
|
4824 |
|
4825 | concatUint8Array: stream.concatUint8Array,
|
4826 |
|
4827 | |
4828 |
|
4829 |
|
4830 |
|
4831 |
|
4832 |
|
4833 | equalsUint8Array: function (array1, array2) {
|
4834 | if (!util.isUint8Array(array1) || !util.isUint8Array(array2)) {
|
4835 | throw new Error('Data must be in the form of a Uint8Array');
|
4836 | }
|
4837 |
|
4838 | if (array1.length !== array2.length) {
|
4839 | return false;
|
4840 | }
|
4841 |
|
4842 | for (let i = 0; i < array1.length; i++) {
|
4843 | if (array1[i] !== array2[i]) {
|
4844 | return false;
|
4845 | }
|
4846 | }
|
4847 | return true;
|
4848 | },
|
4849 |
|
4850 | |
4851 |
|
4852 |
|
4853 |
|
4854 |
|
4855 |
|
4856 | writeChecksum: function (text) {
|
4857 | let s = 0;
|
4858 | for (let i = 0; i < text.length; i++) {
|
4859 | s = (s + text[i]) & 0xFFFF;
|
4860 | }
|
4861 | return util.writeNumber(s, 2);
|
4862 | },
|
4863 |
|
4864 | |
4865 |
|
4866 |
|
4867 |
|
4868 |
|
4869 | printDebug: function (str) {
|
4870 | if (debugMode) {
|
4871 | console.log(str);
|
4872 | }
|
4873 | },
|
4874 |
|
4875 | |
4876 |
|
4877 |
|
4878 |
|
4879 |
|
4880 |
|
4881 | printDebugHexArrayDump: function (str, arrToHex) {
|
4882 | if (debugMode) {
|
4883 | str += ': ' + util.uint8ArrayToHex(arrToHex);
|
4884 | console.log(str);
|
4885 | }
|
4886 | },
|
4887 |
|
4888 | |
4889 |
|
4890 |
|
4891 |
|
4892 |
|
4893 |
|
4894 | printDebugHexStrDump: function (str, strToHex) {
|
4895 | if (debugMode) {
|
4896 | str += util.strToHex(strToHex);
|
4897 | console.log(str);
|
4898 | }
|
4899 | },
|
4900 |
|
4901 | |
4902 |
|
4903 |
|
4904 |
|
4905 |
|
4906 | printDebugError: function (error) {
|
4907 | if (debugMode) {
|
4908 | console.error(error);
|
4909 | }
|
4910 | },
|
4911 |
|
4912 | |
4913 |
|
4914 |
|
4915 |
|
4916 |
|
4917 |
|
4918 | printEntireStream: function (str, input, concat) {
|
4919 | stream.readToEnd(stream.clone(input), concat).then(result => {
|
4920 | console.log(str + ': ', result);
|
4921 | });
|
4922 | },
|
4923 |
|
4924 |
|
4925 | nbits: function (x) {
|
4926 | let r = 1;
|
4927 | let t = x >>> 16;
|
4928 | if (t !== 0) {
|
4929 | x = t;
|
4930 | r += 16;
|
4931 | }
|
4932 | t = x >> 8;
|
4933 | if (t !== 0) {
|
4934 | x = t;
|
4935 | r += 8;
|
4936 | }
|
4937 | t = x >> 4;
|
4938 | if (t !== 0) {
|
4939 | x = t;
|
4940 | r += 4;
|
4941 | }
|
4942 | t = x >> 2;
|
4943 | if (t !== 0) {
|
4944 | x = t;
|
4945 | r += 2;
|
4946 | }
|
4947 | t = x >> 1;
|
4948 | if (t !== 0) {
|
4949 | x = t;
|
4950 | r += 1;
|
4951 | }
|
4952 | return r;
|
4953 | },
|
4954 |
|
4955 | |
4956 |
|
4957 |
|
4958 |
|
4959 |
|
4960 |
|
4961 |
|
4962 |
|
4963 |
|
4964 | double: function(data) {
|
4965 | const double_var = new Uint8Array(data.length);
|
4966 | const last = data.length - 1;
|
4967 | for (let i = 0; i < last; i++) {
|
4968 | double_var[i] = (data[i] << 1) ^ (data[i + 1] >> 7);
|
4969 | }
|
4970 | double_var[last] = (data[last] << 1) ^ ((data[0] >> 7) * 0x87);
|
4971 | return double_var;
|
4972 | },
|
4973 |
|
4974 | |
4975 |
|
4976 |
|
4977 |
|
4978 |
|
4979 |
|
4980 |
|
4981 | shiftRight: function (array, bits) {
|
4982 | if (bits) {
|
4983 | for (let i = array.length - 1; i >= 0; i--) {
|
4984 | array[i] >>= bits;
|
4985 | if (i > 0) {
|
4986 | array[i] |= (array[i - 1] << (8 - bits));
|
4987 | }
|
4988 | }
|
4989 | }
|
4990 | return array;
|
4991 | },
|
4992 |
|
4993 | |
4994 |
|
4995 |
|
4996 |
|
4997 | getWebCrypto: function() {
|
4998 | return typeof globalThis !== 'undefined' && globalThis.crypto && globalThis.crypto.subtle;
|
4999 | },
|
5000 |
|
5001 | |
5002 |
|
5003 |
|
5004 | detectNode: function() {
|
5005 | return typeof globalThis.process === 'object' &&
|
5006 | typeof globalThis.process.versions === 'object';
|
5007 | },
|
5008 |
|
5009 | |
5010 |
|
5011 |
|
5012 | detectBigInt: () => typeof BigInt !== 'undefined',
|
5013 |
|
5014 | |
5015 |
|
5016 |
|
5017 |
|
5018 |
|
5019 |
|
5020 |
|
5021 | getBigInteger,
|
5022 |
|
5023 | |
5024 |
|
5025 |
|
5026 |
|
5027 | getNodeCrypto: function() {
|
5028 | return void('crypto');
|
5029 | },
|
5030 |
|
5031 | getNodeZlib: function() {
|
5032 | return void('zlib');
|
5033 | },
|
5034 |
|
5035 | |
5036 |
|
5037 |
|
5038 |
|
5039 |
|
5040 | getNodeBuffer: function() {
|
5041 | return ({}).Buffer;
|
5042 | },
|
5043 |
|
5044 | getNodeStream: function() {
|
5045 | return ({}).Readable;
|
5046 | },
|
5047 |
|
5048 | getHardwareConcurrency: function() {
|
5049 | if (util.detectNode()) {
|
5050 | const os = void('os');
|
5051 | return os.cpus().length;
|
5052 | }
|
5053 |
|
5054 | return navigator.hardwareConcurrency || 1;
|
5055 | },
|
5056 |
|
5057 | isEmailAddress: function(data) {
|
5058 | if (!util.isString(data)) {
|
5059 | return false;
|
5060 | }
|
5061 | const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+([a-zA-Z]{2,}|xn--[a-zA-Z\-0-9]+)))$/;
|
5062 | return re.test(data);
|
5063 | },
|
5064 |
|
5065 | |
5066 |
|
5067 |
|
5068 |
|
5069 | canonicalizeEOL: function(data) {
|
5070 | const CR = 13;
|
5071 | const LF = 10;
|
5072 | let carryOverCR = false;
|
5073 |
|
5074 | return stream.transform(data, bytes => {
|
5075 | if (carryOverCR) {
|
5076 | bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
|
5077 | }
|
5078 |
|
5079 | if (bytes[bytes.length - 1] === CR) {
|
5080 | carryOverCR = true;
|
5081 | bytes = bytes.subarray(0, -1);
|
5082 | } else {
|
5083 | carryOverCR = false;
|
5084 | }
|
5085 |
|
5086 | let index;
|
5087 | const indices = [];
|
5088 | for (let i = 0; ; i = index) {
|
5089 | index = bytes.indexOf(LF, i) + 1;
|
5090 | if (index) {
|
5091 | if (bytes[index - 2] !== CR) indices.push(index);
|
5092 | } else {
|
5093 | break;
|
5094 | }
|
5095 | }
|
5096 | if (!indices.length) {
|
5097 | return bytes;
|
5098 | }
|
5099 |
|
5100 | const normalized = new Uint8Array(bytes.length + indices.length);
|
5101 | let j = 0;
|
5102 | for (let i = 0; i < indices.length; i++) {
|
5103 | const sub = bytes.subarray(indices[i - 1] || 0, indices[i]);
|
5104 | normalized.set(sub, j);
|
5105 | j += sub.length;
|
5106 | normalized[j - 1] = CR;
|
5107 | normalized[j] = LF;
|
5108 | j++;
|
5109 | }
|
5110 | normalized.set(bytes.subarray(indices[indices.length - 1] || 0), j);
|
5111 | return normalized;
|
5112 | }, () => (carryOverCR ? new Uint8Array([CR]) : undefined));
|
5113 | },
|
5114 |
|
5115 | |
5116 |
|
5117 |
|
5118 |
|
5119 | nativeEOL: function(data) {
|
5120 | const CR = 13;
|
5121 | const LF = 10;
|
5122 | let carryOverCR = false;
|
5123 |
|
5124 | return stream.transform(data, bytes => {
|
5125 | if (carryOverCR && bytes[0] !== LF) {
|
5126 | bytes = util.concatUint8Array([new Uint8Array([CR]), bytes]);
|
5127 | } else {
|
5128 | bytes = new Uint8Array(bytes);
|
5129 | }
|
5130 |
|
5131 | if (bytes[bytes.length - 1] === CR) {
|
5132 | carryOverCR = true;
|
5133 | bytes = bytes.subarray(0, -1);
|
5134 | } else {
|
5135 | carryOverCR = false;
|
5136 | }
|
5137 |
|
5138 | let index;
|
5139 | let j = 0;
|
5140 | for (let i = 0; i !== bytes.length; i = index) {
|
5141 | index = bytes.indexOf(CR, i) + 1;
|
5142 | if (!index) index = bytes.length;
|
5143 | const last = index - (bytes[index] === LF ? 1 : 0);
|
5144 | if (i) bytes.copyWithin(j, i, last);
|
5145 | j += last - i;
|
5146 | }
|
5147 | return bytes.subarray(0, j);
|
5148 | }, () => (carryOverCR ? new Uint8Array([CR]) : undefined));
|
5149 | },
|
5150 |
|
5151 | |
5152 |
|
5153 |
|
5154 | removeTrailingSpaces: function(text) {
|
5155 | return text.split('\n').map(line => {
|
5156 | let i = line.length - 1;
|
5157 | for (; i >= 0 && (line[i] === ' ' || line[i] === '\t'); i--);
|
5158 | return line.substr(0, i + 1);
|
5159 | }).join('\n');
|
5160 | },
|
5161 |
|
5162 | wrapError: function(message, error) {
|
5163 | if (!error) {
|
5164 | return new Error(message);
|
5165 | }
|
5166 |
|
5167 |
|
5168 | try {
|
5169 | error.message = message + ': ' + error.message;
|
5170 | } catch (e) {}
|
5171 |
|
5172 | return error;
|
5173 | }
|
5174 | };
|
5175 |
|
5176 |
|
5177 |
|
5178 |
|
5179 |
|
5180 |
|
5181 |
|
5182 |
|
5183 |
|
5184 |
|
5185 |
|
5186 |
|
5187 |
|
5188 |
|
5189 | const Buffer = util.getNodeBuffer();
|
5190 |
|
5191 | let encodeChunk;
|
5192 | let decodeChunk;
|
5193 | if (Buffer) {
|
5194 | encodeChunk = buf => Buffer.from(buf).toString('base64');
|
5195 | decodeChunk = str => {
|
5196 | const b = Buffer.from(str, 'base64');
|
5197 | return new Uint8Array(b.buffer, b.byteOffset, b.byteLength);
|
5198 | };
|
5199 | } else {
|
5200 | encodeChunk = buf => btoa(util.uint8ArrayToStr(buf));
|
5201 | decodeChunk = str => util.strToUint8Array(atob(str));
|
5202 | }
|
5203 |
|
5204 |
|
5205 |
|
5206 |
|
5207 |
|
5208 |
|
5209 |
|
5210 | function encode(data) {
|
5211 | let buf = new Uint8Array();
|
5212 | return stream.transform(data, value => {
|
5213 | buf = util.concatUint8Array([buf, value]);
|
5214 | const r = [];
|
5215 | const bytesPerLine = 45;
|
5216 | const lines = Math.floor(buf.length / bytesPerLine);
|
5217 | const bytes = lines * bytesPerLine;
|
5218 | const encoded = encodeChunk(buf.subarray(0, bytes));
|
5219 | for (let i = 0; i < lines; i++) {
|
5220 | r.push(encoded.substr(i * 60, 60));
|
5221 | r.push('\n');
|
5222 | }
|
5223 | buf = buf.subarray(bytes);
|
5224 | return r.join('');
|
5225 | }, () => (buf.length ? encodeChunk(buf) + '\n' : ''));
|
5226 | }
|
5227 |
|
5228 |
|
5229 |
|
5230 |
|
5231 |
|
5232 |
|
5233 |
|
5234 | function decode(data) {
|
5235 | let buf = '';
|
5236 | return stream.transform(data, value => {
|
5237 | buf += value;
|
5238 |
|
5239 |
|
5240 | let spaces = 0;
|
5241 | const spacechars = [' ', '\t', '\r', '\n'];
|
5242 | for (let i = 0; i < spacechars.length; i++) {
|
5243 | const spacechar = spacechars[i];
|
5244 | for (let pos = buf.indexOf(spacechar); pos !== -1; pos = buf.indexOf(spacechar, pos + 1)) {
|
5245 | spaces++;
|
5246 | }
|
5247 | }
|
5248 |
|
5249 |
|
5250 |
|
5251 | let length = buf.length;
|
5252 | for (; length > 0 && (length - spaces) % 4 !== 0; length--) {
|
5253 | if (spacechars.includes(buf[length])) spaces--;
|
5254 | }
|
5255 |
|
5256 | const decoded = decodeChunk(buf.substr(0, length));
|
5257 | buf = buf.substr(length);
|
5258 | return decoded;
|
5259 | }, () => decodeChunk(buf));
|
5260 | }
|
5261 |
|
5262 |
|
5263 |
|
5264 |
|
5265 |
|
5266 |
|
5267 |
|
5268 |
|
5269 | function b64ToUint8Array(base64) {
|
5270 | return decode(base64.replace(/-/g, '+').replace(/_/g, '/'));
|
5271 | }
|
5272 |
|
5273 |
|
5274 |
|
5275 |
|
5276 |
|
5277 |
|
5278 |
|
5279 | function uint8ArrayToB64(bytes, url) {
|
5280 | let encoded = encode(bytes).replace(/[\r\n]/g, '');
|
5281 | if (url) {
|
5282 | encoded = encoded.replace(/[+]/g, '-').replace(/[/]/g, '_').replace(/[=]/g, '');
|
5283 | }
|
5284 | return encoded;
|
5285 | }
|
5286 |
|
5287 |
|
5288 |
|
5289 |
|
5290 |
|
5291 | const byValue = Symbol('byValue');
|
5292 |
|
5293 | var enums = {
|
5294 |
|
5295 | |
5296 |
|
5297 |
|
5298 |
|
5299 |
|
5300 | curve: {
|
5301 |
|
5302 | "p256": "p256",
|
5303 | "P-256": "p256",
|
5304 | "secp256r1": "p256",
|
5305 | "prime256v1": "p256",
|
5306 | "1.2.840.10045.3.1.7": "p256",
|
5307 | "2a8648ce3d030107": "p256",
|
5308 | "2A8648CE3D030107": "p256",
|
5309 |
|
5310 |
|
5311 | "p384": "p384",
|
5312 | "P-384": "p384",
|
5313 | "secp384r1": "p384",
|
5314 | "1.3.132.0.34": "p384",
|
5315 | "2b81040022": "p384",
|
5316 | "2B81040022": "p384",
|
5317 |
|
5318 |
|
5319 | "p521": "p521",
|
5320 | "P-521": "p521",
|
5321 | "secp521r1": "p521",
|
5322 | "1.3.132.0.35": "p521",
|
5323 | "2b81040023": "p521",
|
5324 | "2B81040023": "p521",
|
5325 |
|
5326 |
|
5327 | "secp256k1": "secp256k1",
|
5328 | "1.3.132.0.10": "secp256k1",
|
5329 | "2b8104000a": "secp256k1",
|
5330 | "2B8104000A": "secp256k1",
|
5331 |
|
5332 |
|
5333 | "ED25519": "ed25519",
|
5334 | "ed25519": "ed25519",
|
5335 | "Ed25519": "ed25519",
|
5336 | "1.3.6.1.4.1.11591.15.1": "ed25519",
|
5337 | "2b06010401da470f01": "ed25519",
|
5338 | "2B06010401DA470F01": "ed25519",
|
5339 |
|
5340 |
|
5341 | "X25519": "curve25519",
|
5342 | "cv25519": "curve25519",
|
5343 | "curve25519": "curve25519",
|
5344 | "Curve25519": "curve25519",
|
5345 | "1.3.6.1.4.1.3029.1.5.1": "curve25519",
|
5346 | "2b060104019755010501": "curve25519",
|
5347 | "2B060104019755010501": "curve25519",
|
5348 |
|
5349 |
|
5350 | "brainpoolP256r1": "brainpoolP256r1",
|
5351 | "1.3.36.3.3.2.8.1.1.7": "brainpoolP256r1",
|
5352 | "2b2403030208010107": "brainpoolP256r1",
|
5353 | "2B2403030208010107": "brainpoolP256r1",
|
5354 |
|
5355 |
|
5356 | "brainpoolP384r1": "brainpoolP384r1",
|
5357 | "1.3.36.3.3.2.8.1.1.11": "brainpoolP384r1",
|
5358 | "2b240303020801010b": "brainpoolP384r1",
|
5359 | "2B240303020801010B": "brainpoolP384r1",
|
5360 |
|
5361 |
|
5362 | "brainpoolP512r1": "brainpoolP512r1",
|
5363 | "1.3.36.3.3.2.8.1.1.13": "brainpoolP512r1",
|
5364 | "2b240303020801010d": "brainpoolP512r1",
|
5365 | "2B240303020801010D": "brainpoolP512r1"
|
5366 | },
|
5367 |
|
5368 | |
5369 |
|
5370 |
|
5371 |
|
5372 | s2k: {
|
5373 | simple: 0,
|
5374 | salted: 1,
|
5375 | iterated: 3,
|
5376 | gnu: 101
|
5377 | },
|
5378 |
|
5379 | |
5380 |
|
5381 |
|
5382 |
|
5383 | publicKey: {
|
5384 |
|
5385 | rsaEncryptSign: 1,
|
5386 |
|
5387 | rsaEncrypt: 2,
|
5388 |
|
5389 | rsaSign: 3,
|
5390 |
|
5391 | elgamal: 16,
|
5392 |
|
5393 | dsa: 17,
|
5394 |
|
5395 | ecdh: 18,
|
5396 |
|
5397 | ecdsa: 19,
|
5398 | |
5399 |
|
5400 | eddsa: 22,
|
5401 |
|
5402 | aedh: 23,
|
5403 |
|
5404 | aedsa: 24
|
5405 | },
|
5406 |
|
5407 | |
5408 |
|
5409 |
|
5410 |
|
5411 | symmetric: {
|
5412 | plaintext: 0,
|
5413 |
|
5414 | idea: 1,
|
5415 | tripledes: 2,
|
5416 | cast5: 3,
|
5417 | blowfish: 4,
|
5418 | aes128: 7,
|
5419 | aes192: 8,
|
5420 | aes256: 9,
|
5421 | twofish: 10
|
5422 | },
|
5423 |
|
5424 | |
5425 |
|
5426 |
|
5427 |
|
5428 | compression: {
|
5429 | uncompressed: 0,
|
5430 |
|
5431 | zip: 1,
|
5432 |
|
5433 | zlib: 2,
|
5434 | bzip2: 3
|
5435 | },
|
5436 |
|
5437 | |
5438 |
|
5439 |
|
5440 |
|
5441 | hash: {
|
5442 | md5: 1,
|
5443 | sha1: 2,
|
5444 | ripemd: 3,
|
5445 | sha256: 8,
|
5446 | sha384: 9,
|
5447 | sha512: 10,
|
5448 | sha224: 11
|
5449 | },
|
5450 |
|
5451 | |
5452 |
|
5453 |
|
5454 |
|
5455 | webHash: {
|
5456 | 'SHA-1': 2,
|
5457 | 'SHA-256': 8,
|
5458 | 'SHA-384': 9,
|
5459 | 'SHA-512': 10
|
5460 | },
|
5461 |
|
5462 | |
5463 |
|
5464 |
|
5465 |
|
5466 | aead: {
|
5467 | eax: 1,
|
5468 | ocb: 2,
|
5469 | experimentalGcm: 100
|
5470 | },
|
5471 |
|
5472 | |
5473 |
|
5474 |
|
5475 |
|
5476 | packet: {
|
5477 | publicKeyEncryptedSessionKey: 1,
|
5478 | signature: 2,
|
5479 | symEncryptedSessionKey: 3,
|
5480 | onePassSignature: 4,
|
5481 | secretKey: 5,
|
5482 | publicKey: 6,
|
5483 | secretSubkey: 7,
|
5484 | compressedData: 8,
|
5485 | symmetricallyEncryptedData: 9,
|
5486 | marker: 10,
|
5487 | literalData: 11,
|
5488 | trust: 12,
|
5489 | userID: 13,
|
5490 | publicSubkey: 14,
|
5491 | userAttribute: 17,
|
5492 | symEncryptedIntegrityProtectedData: 18,
|
5493 | modificationDetectionCode: 19,
|
5494 | AEADEncryptedData: 20
|
5495 | },
|
5496 |
|
5497 | |
5498 |
|
5499 |
|
5500 |
|
5501 | literal: {
|
5502 |
|
5503 | binary: 'b'.charCodeAt(),
|
5504 |
|
5505 | text: 't'.charCodeAt(),
|
5506 |
|
5507 | utf8: 'u'.charCodeAt(),
|
5508 |
|
5509 | mime: 'm'.charCodeAt()
|
5510 | },
|
5511 |
|
5512 |
|
5513 | |
5514 |
|
5515 |
|
5516 |
|
5517 | signature: {
|
5518 |
|
5519 | binary: 0,
|
5520 | |
5521 |
|
5522 |
|
5523 | text: 1,
|
5524 | |
5525 |
|
5526 |
|
5527 |
|
5528 |
|
5529 |
|
5530 | standalone: 2,
|
5531 | |
5532 |
|
5533 |
|
5534 |
|
5535 |
|
5536 | certGeneric: 16,
|
5537 | |
5538 |
|
5539 |
|
5540 |
|
5541 | certPersona: 17,
|
5542 | |
5543 |
|
5544 |
|
5545 |
|
5546 | certCasual: 18,
|
5547 | |
5548 |
|
5549 |
|
5550 |
|
5551 |
|
5552 |
|
5553 |
|
5554 |
|
5555 | certPositive: 19,
|
5556 | |
5557 |
|
5558 |
|
5559 |
|
5560 |
|
5561 |
|
5562 |
|
5563 |
|
5564 |
|
5565 | certRevocation: 48,
|
5566 | |
5567 |
|
5568 |
|
5569 |
|
5570 |
|
5571 |
|
5572 |
|
5573 |
|
5574 |
|
5575 | subkeyBinding: 24,
|
5576 | |
5577 |
|
5578 |
|
5579 |
|
5580 |
|
5581 |
|
5582 |
|
5583 |
|
5584 |
|
5585 |
|
5586 |
|
5587 |
|
5588 |
|
5589 |
|
5590 | keyBinding: 25,
|
5591 | |
5592 |
|
5593 |
|
5594 |
|
5595 |
|
5596 |
|
5597 |
|
5598 |
|
5599 |
|
5600 | key: 31,
|
5601 | |
5602 |
|
5603 |
|
5604 |
|
5605 |
|
5606 |
|
5607 | keyRevocation: 32,
|
5608 | |
5609 |
|
5610 |
|
5611 |
|
5612 |
|
5613 |
|
5614 |
|
5615 |
|
5616 |
|
5617 |
|
5618 | subkeyRevocation: 40,
|
5619 | |
5620 |
|
5621 |
|
5622 | timestamp: 64,
|
5623 | |
5624 |
|
5625 |
|
5626 |
|
5627 |
|
5628 |
|
5629 |
|
5630 |
|
5631 |
|
5632 | thirdParty: 80
|
5633 | },
|
5634 |
|
5635 | |
5636 |
|
5637 |
|
5638 |
|
5639 | signatureSubpacket: {
|
5640 | signatureCreationTime: 2,
|
5641 | signatureExpirationTime: 3,
|
5642 | exportableCertification: 4,
|
5643 | trustSignature: 5,
|
5644 | regularExpression: 6,
|
5645 | revocable: 7,
|
5646 | keyExpirationTime: 9,
|
5647 | placeholderBackwardsCompatibility: 10,
|
5648 | preferredSymmetricAlgorithms: 11,
|
5649 | revocationKey: 12,
|
5650 | issuer: 16,
|
5651 | notationData: 20,
|
5652 | preferredHashAlgorithms: 21,
|
5653 | preferredCompressionAlgorithms: 22,
|
5654 | keyServerPreferences: 23,
|
5655 | preferredKeyServer: 24,
|
5656 | primaryUserId: 25,
|
5657 | policyUri: 26,
|
5658 | keyFlags: 27,
|
5659 | signersUserId: 28,
|
5660 | reasonForRevocation: 29,
|
5661 | features: 30,
|
5662 | signatureTarget: 31,
|
5663 | embeddedSignature: 32,
|
5664 | issuerFingerprint: 33,
|
5665 | preferredAeadAlgorithms: 34
|
5666 | },
|
5667 |
|
5668 | |
5669 |
|
5670 |
|
5671 |
|
5672 | keyFlags: {
|
5673 |
|
5674 | certifyKeys: 1,
|
5675 |
|
5676 | signData: 2,
|
5677 |
|
5678 | encryptCommunication: 4,
|
5679 |
|
5680 | encryptStorage: 8,
|
5681 | |
5682 |
|
5683 | splitPrivateKey: 16,
|
5684 |
|
5685 | authentication: 32,
|
5686 | |
5687 |
|
5688 | sharedPrivateKey: 128
|
5689 | },
|
5690 |
|
5691 | |
5692 |
|
5693 |
|
5694 |
|
5695 | armor: {
|
5696 | multipartSection: 0,
|
5697 | multipartLast: 1,
|
5698 | signed: 2,
|
5699 | message: 3,
|
5700 | publicKey: 4,
|
5701 | privateKey: 5,
|
5702 | signature: 6
|
5703 | },
|
5704 |
|
5705 | |
5706 |
|
5707 |
|
5708 |
|
5709 | reasonForRevocation: {
|
5710 |
|
5711 | noReason: 0,
|
5712 |
|
5713 | keySuperseded: 1,
|
5714 |
|
5715 | keyCompromised: 2,
|
5716 |
|
5717 | keyRetired: 3,
|
5718 |
|
5719 | userIdInvalid: 32
|
5720 | },
|
5721 |
|
5722 | |
5723 |
|
5724 |
|
5725 |
|
5726 | features: {
|
5727 |
|
5728 | modificationDetection: 1,
|
5729 | |
5730 |
|
5731 | aead: 2,
|
5732 | |
5733 |
|
5734 | v5Keys: 4
|
5735 | },
|
5736 |
|
5737 |
|
5738 | write: function(type, e) {
|
5739 | if (typeof e === 'number') {
|
5740 | e = this.read(type, e);
|
5741 | }
|
5742 |
|
5743 | if (type[e] !== undefined) {
|
5744 | return type[e];
|
5745 | }
|
5746 |
|
5747 | throw new Error('Invalid enum value.');
|
5748 | },
|
5749 |
|
5750 |
|
5751 | read: function(type, e) {
|
5752 | if (!type[byValue]) {
|
5753 | type[byValue] = [];
|
5754 | Object.entries(type).forEach(([key, value]) => {
|
5755 | type[byValue][value] = key;
|
5756 | });
|
5757 | }
|
5758 |
|
5759 | if (type[byValue][e] !== undefined) {
|
5760 | return type[byValue][e];
|
5761 | }
|
5762 |
|
5763 | throw new Error('Invalid enum value.');
|
5764 | }
|
5765 |
|
5766 | };
|
5767 |
|
5768 |
|
5769 |
|
5770 | var defaultConfig = {
|
5771 | |
5772 |
|
5773 |
|
5774 |
|
5775 | preferHashAlgorithm: enums.hash.sha256,
|
5776 | |
5777 |
|
5778 |
|
5779 |
|
5780 | encryptionCipher: enums.symmetric.aes256,
|
5781 | |
5782 |
|
5783 |
|
5784 |
|
5785 | compression: enums.compression.uncompressed,
|
5786 | |
5787 |
|
5788 |
|
5789 |
|
5790 | deflateLevel: 6,
|
5791 |
|
5792 | |
5793 |
|
5794 |
|
5795 |
|
5796 |
|
5797 |
|
5798 |
|
5799 |
|
5800 | aeadProtect: false,
|
5801 | |
5802 |
|
5803 |
|
5804 |
|
5805 |
|
5806 |
|
5807 | aeadMode: enums.aead.eax,
|
5808 | |
5809 |
|
5810 |
|
5811 |
|
5812 |
|
5813 |
|
5814 |
|
5815 | aeadChunkSizeByte: 12,
|
5816 | |
5817 |
|
5818 |
|
5819 |
|
5820 |
|
5821 |
|
5822 |
|
5823 | v5Keys: false,
|
5824 | |
5825 |
|
5826 |
|
5827 |
|
5828 |
|
5829 |
|
5830 | s2kIterationCountByte: 224,
|
5831 | |
5832 |
|
5833 |
|
5834 |
|
5835 |
|
5836 |
|
5837 |
|
5838 |
|
5839 | allowUnauthenticatedMessages: false,
|
5840 | |
5841 |
|
5842 |
|
5843 |
|
5844 |
|
5845 |
|
5846 | allowUnauthenticatedStream: false,
|
5847 | |
5848 |
|
5849 |
|
5850 |
|
5851 | checksumRequired: false,
|
5852 | |
5853 |
|
5854 |
|
5855 |
|
5856 | minRsaBits: 2048,
|
5857 | |
5858 |
|
5859 |
|
5860 |
|
5861 |
|
5862 |
|
5863 | passwordCollisionCheck: false,
|
5864 | |
5865 |
|
5866 |
|
5867 |
|
5868 | revocationsExpire: false,
|
5869 | |
5870 |
|
5871 |
|
5872 |
|
5873 |
|
5874 |
|
5875 |
|
5876 | allowInsecureDecryptionWithSigningKeys: false,
|
5877 |
|
5878 | |
5879 |
|
5880 |
|
5881 |
|
5882 | minBytesForWebCrypto: 1000,
|
5883 | |
5884 |
|
5885 |
|
5886 |
|
5887 | tolerant: true,
|
5888 |
|
5889 | |
5890 |
|
5891 |
|
5892 |
|
5893 | showVersion: false,
|
5894 | |
5895 |
|
5896 |
|
5897 |
|
5898 | showComment: false,
|
5899 | |
5900 |
|
5901 |
|
5902 |
|
5903 | versionString: "OpenPGP.js 5.0.0-1",
|
5904 | |
5905 |
|
5906 |
|
5907 |
|
5908 | commentString: "https://openpgpjs.org",
|
5909 |
|
5910 | |
5911 |
|
5912 |
|
5913 |
|
5914 |
|
5915 | maxUseridLength: 1024 * 5,
|
5916 | |
5917 |
|
5918 |
|
5919 |
|
5920 |
|
5921 |
|
5922 | knownNotations: ["preferred-email-encoding@pgp.com", "pka-address@gnupg.org"],
|
5923 | |
5924 |
|
5925 |
|
5926 |
|
5927 | useIndutnyElliptic: true,
|
5928 | |
5929 |
|
5930 |
|
5931 |
|
5932 | rejectHashAlgorithms: new globalThis.Set([enums.hash.md5, enums.hash.ripemd]),
|
5933 | |
5934 |
|
5935 |
|
5936 |
|
5937 | rejectMessageHashAlgorithms: new globalThis.Set([enums.hash.md5, enums.hash.ripemd, enums.hash.sha1])
|
5938 | };
|
5939 |
|
5940 |
|
5941 |
|
5942 |
|
5943 |
|
5944 |
|
5945 |
|
5946 |
|
5947 |
|
5948 |
|
5949 |
|
5950 |
|
5951 |
|
5952 |
|
5953 |
|
5954 | function getType(text) {
|
5955 | const reHeader = /^-----BEGIN PGP (MESSAGE, PART \d+\/\d+|MESSAGE, PART \d+|SIGNED MESSAGE|MESSAGE|PUBLIC KEY BLOCK|PRIVATE KEY BLOCK|SIGNATURE)-----$/m;
|
5956 |
|
5957 | const header = text.match(reHeader);
|
5958 |
|
5959 | if (!header) {
|
5960 | throw new Error('Unknown ASCII armor type');
|
5961 | }
|
5962 |
|
5963 |
|
5964 |
|
5965 |
|
5966 | if (/MESSAGE, PART \d+\/\d+/.test(header[1])) {
|
5967 | return enums.armor.multipartSection;
|
5968 | } else
|
5969 |
|
5970 |
|
5971 |
|
5972 |
|
5973 | if (/MESSAGE, PART \d+/.test(header[1])) {
|
5974 | return enums.armor.multipartLast;
|
5975 | } else
|
5976 |
|
5977 | if (/SIGNED MESSAGE/.test(header[1])) {
|
5978 | return enums.armor.signed;
|
5979 | } else
|
5980 |
|
5981 |
|
5982 | if (/MESSAGE/.test(header[1])) {
|
5983 | return enums.armor.message;
|
5984 | } else
|
5985 |
|
5986 |
|
5987 | if (/PUBLIC KEY BLOCK/.test(header[1])) {
|
5988 | return enums.armor.publicKey;
|
5989 | } else
|
5990 |
|
5991 |
|
5992 | if (/PRIVATE KEY BLOCK/.test(header[1])) {
|
5993 | return enums.armor.privateKey;
|
5994 | } else
|
5995 |
|
5996 |
|
5997 |
|
5998 |
|
5999 | if (/SIGNATURE/.test(header[1])) {
|
6000 | return enums.armor.signature;
|
6001 | }
|
6002 | }
|
6003 |
|
6004 |
|
6005 |
|
6006 |
|
6007 |
|
6008 |
|
6009 |
|
6010 |
|
6011 |
|
6012 |
|
6013 | function addheader(customComment, config) {
|
6014 | let result = "";
|
6015 | if (config.showVersion) {
|
6016 | result += "Version: " + config.versionString + '\n';
|
6017 | }
|
6018 | if (config.showComment) {
|
6019 | result += "Comment: " + config.commentString + '\n';
|
6020 | }
|
6021 | if (customComment) {
|
6022 | result += "Comment: " + customComment + '\n';
|
6023 | }
|
6024 | result += '\n';
|
6025 | return result;
|
6026 | }
|
6027 |
|
6028 |
|
6029 |
|
6030 |
|
6031 |
|
6032 |
|
6033 |
|
6034 |
|
6035 | function getCheckSum(data) {
|
6036 | const crc = createcrc24(data);
|
6037 | return encode(crc);
|
6038 | }
|
6039 |
|
6040 |
|
6041 |
|
6042 | const crc_table = [
|
6043 | new Array(0xFF),
|
6044 | new Array(0xFF),
|
6045 | new Array(0xFF),
|
6046 | new Array(0xFF)
|
6047 | ];
|
6048 |
|
6049 | for (let i = 0; i <= 0xFF; i++) {
|
6050 | let crc = i << 16;
|
6051 | for (let j = 0; j < 8; j++) {
|
6052 | crc = (crc << 1) ^ ((crc & 0x800000) !== 0 ? 0x864CFB : 0);
|
6053 | }
|
6054 | crc_table[0][i] =
|
6055 | ((crc & 0xFF0000) >> 16) |
|
6056 | (crc & 0x00FF00) |
|
6057 | ((crc & 0x0000FF) << 16);
|
6058 | }
|
6059 | for (let i = 0; i <= 0xFF; i++) {
|
6060 | crc_table[1][i] = (crc_table[0][i] >> 8) ^ crc_table[0][crc_table[0][i] & 0xFF];
|
6061 | }
|
6062 | for (let i = 0; i <= 0xFF; i++) {
|
6063 | crc_table[2][i] = (crc_table[1][i] >> 8) ^ crc_table[0][crc_table[1][i] & 0xFF];
|
6064 | }
|
6065 | for (let i = 0; i <= 0xFF; i++) {
|
6066 | crc_table[3][i] = (crc_table[2][i] >> 8) ^ crc_table[0][crc_table[2][i] & 0xFF];
|
6067 | }
|
6068 |
|
6069 |
|
6070 | const isLittleEndian = (function() {
|
6071 | const buffer = new ArrayBuffer(2);
|
6072 | new DataView(buffer).setInt16(0, 0xFF, true );
|
6073 |
|
6074 | return new Int16Array(buffer)[0] === 0xFF;
|
6075 | }());
|
6076 |
|
6077 |
|
6078 |
|
6079 |
|
6080 |
|
6081 |
|
6082 |
|
6083 | function createcrc24(input) {
|
6084 | let crc = 0xCE04B7;
|
6085 | return stream.transform(input, value => {
|
6086 | const len32 = isLittleEndian ? Math.floor(value.length / 4) : 0;
|
6087 | const arr32 = new Uint32Array(value.buffer, value.byteOffset, len32);
|
6088 | for (let i = 0; i < len32; i++) {
|
6089 | crc ^= arr32[i];
|
6090 | crc =
|
6091 | crc_table[0][(crc >> 24) & 0xFF] ^
|
6092 | crc_table[1][(crc >> 16) & 0xFF] ^
|
6093 | crc_table[2][(crc >> 8) & 0xFF] ^
|
6094 | crc_table[3][(crc >> 0) & 0xFF];
|
6095 | }
|
6096 | for (let i = len32 * 4; i < value.length; i++) {
|
6097 | crc = (crc >> 8) ^ crc_table[0][(crc & 0xFF) ^ value[i]];
|
6098 | }
|
6099 | }, () => new Uint8Array([crc, crc >> 8, crc >> 16]));
|
6100 | }
|
6101 |
|
6102 |
|
6103 |
|
6104 |
|
6105 |
|
6106 |
|
6107 |
|
6108 | function verifyHeaders(headers) {
|
6109 | for (let i = 0; i < headers.length; i++) {
|
6110 | if (!/^([^\s:]|[^\s:][^:]*[^\s:]): .+$/.test(headers[i])) {
|
6111 | throw new Error('Improperly formatted armor header: ' + headers[i]);
|
6112 | }
|
6113 | if (!/^(Version|Comment|MessageID|Hash|Charset): .+$/.test(headers[i])) {
|
6114 | util.printDebugError(new Error('Unknown header: ' + headers[i]));
|
6115 | }
|
6116 | }
|
6117 | }
|
6118 |
|
6119 |
|
6120 |
|
6121 |
|
6122 |
|
6123 |
|
6124 |
|
6125 |
|
6126 | function splitChecksum(text) {
|
6127 | let body = text;
|
6128 | let checksum = "";
|
6129 |
|
6130 | const lastEquals = text.lastIndexOf("=");
|
6131 |
|
6132 | if (lastEquals >= 0 && lastEquals !== text.length - 1) {
|
6133 | body = text.slice(0, lastEquals);
|
6134 | checksum = text.slice(lastEquals + 1).substr(0, 4);
|
6135 | }
|
6136 |
|
6137 | return { body: body, checksum: checksum };
|
6138 | }
|
6139 |
|
6140 |
|
6141 |
|
6142 |
|
6143 |
|
6144 |
|
6145 |
|
6146 |
|
6147 |
|
6148 |
|
6149 | function unarmor(input, config = defaultConfig) {
|
6150 | return new Promise(async (resolve, reject) => {
|
6151 | try {
|
6152 | const reSplit = /^-----[^-]+-----$/m;
|
6153 | const reEmptyLine = /^[ \f\r\t\u00a0\u2000-\u200a\u202f\u205f\u3000]*$/;
|
6154 |
|
6155 | let type;
|
6156 | const headers = [];
|
6157 | let lastHeaders = headers;
|
6158 | let headersDone;
|
6159 | let text = [];
|
6160 | let textDone;
|
6161 | let checksum;
|
6162 | let data = decode(stream.transformPair(input, async (readable, writable) => {
|
6163 | const reader = stream.getReader(readable);
|
6164 | try {
|
6165 | while (true) {
|
6166 | let line = await reader.readLine();
|
6167 | if (line === undefined) {
|
6168 | throw new Error('Misformed armored text');
|
6169 | }
|
6170 |
|
6171 | line = util.removeTrailingSpaces(line.replace(/[\r\n]/g, ''));
|
6172 | if (!type) {
|
6173 | if (reSplit.test(line)) {
|
6174 | type = getType(line);
|
6175 | }
|
6176 | } else if (!headersDone) {
|
6177 | if (reSplit.test(line)) {
|
6178 | reject(new Error('Mandatory blank line missing between armor headers and armor data'));
|
6179 | }
|
6180 | if (!reEmptyLine.test(line)) {
|
6181 | lastHeaders.push(line);
|
6182 | } else {
|
6183 | verifyHeaders(lastHeaders);
|
6184 | headersDone = true;
|
6185 | if (textDone || type !== 2) {
|
6186 | resolve({ text, data, headers, type });
|
6187 | break;
|
6188 | }
|
6189 | }
|
6190 | } else if (!textDone && type === 2) {
|
6191 | if (!reSplit.test(line)) {
|
6192 |
|
6193 | text.push(line.replace(/^- /, ''));
|
6194 | } else {
|
6195 | text = text.join('\r\n');
|
6196 | textDone = true;
|
6197 | verifyHeaders(lastHeaders);
|
6198 | lastHeaders = [];
|
6199 | headersDone = false;
|
6200 | }
|
6201 | }
|
6202 | }
|
6203 | } catch (e) {
|
6204 | reject(e);
|
6205 | return;
|
6206 | }
|
6207 | const writer = stream.getWriter(writable);
|
6208 | try {
|
6209 | while (true) {
|
6210 | await writer.ready;
|
6211 | const { done, value } = await reader.read();
|
6212 | if (done) {
|
6213 | throw new Error('Misformed armored text');
|
6214 | }
|
6215 | const line = value + '';
|
6216 | if (line.indexOf('=') === -1 && line.indexOf('-') === -1) {
|
6217 | await writer.write(line);
|
6218 | } else {
|
6219 | let remainder = await reader.readToEnd();
|
6220 | if (!remainder.length) remainder = '';
|
6221 | remainder = line + remainder;
|
6222 | remainder = util.removeTrailingSpaces(remainder.replace(/\r/g, ''));
|
6223 | const parts = remainder.split(reSplit);
|
6224 | if (parts.length === 1) {
|
6225 | throw new Error('Misformed armored text');
|
6226 | }
|
6227 | const split = splitChecksum(parts[0].slice(0, -1));
|
6228 | checksum = split.checksum;
|
6229 | await writer.write(split.body);
|
6230 | break;
|
6231 | }
|
6232 | }
|
6233 | await writer.ready;
|
6234 | await writer.close();
|
6235 | } catch (e) {
|
6236 | await writer.abort(e);
|
6237 | }
|
6238 | }));
|
6239 | data = stream.transformPair(data, async (readable, writable) => {
|
6240 | const checksumVerified = stream.readToEnd(getCheckSum(stream.passiveClone(readable)));
|
6241 | checksumVerified.catch(() => {});
|
6242 | await stream.pipe(readable, writable, {
|
6243 | preventClose: true
|
6244 | });
|
6245 | const writer = stream.getWriter(writable);
|
6246 | try {
|
6247 | const checksumVerifiedString = (await checksumVerified).replace('\n', '');
|
6248 | if (checksum !== checksumVerifiedString && (checksum || config.checksumRequired)) {
|
6249 | throw new Error("Ascii armor integrity check on message failed: '" + checksum + "' should be '" +
|
6250 | checksumVerifiedString + "'");
|
6251 | }
|
6252 | await writer.ready;
|
6253 | await writer.close();
|
6254 | } catch (e) {
|
6255 | await writer.abort(e);
|
6256 | }
|
6257 | });
|
6258 | } catch (e) {
|
6259 | reject(e);
|
6260 | }
|
6261 | });
|
6262 | }
|
6263 |
|
6264 |
|
6265 |
|
6266 |
|
6267 |
|
6268 |
|
6269 |
|
6270 |
|
6271 |
|
6272 |
|
6273 |
|
6274 |
|
6275 | function armor(messageType, body, partIndex, partTotal, customComment, config = defaultConfig) {
|
6276 | let text;
|
6277 | let hash;
|
6278 | if (messageType === enums.armor.signed) {
|
6279 | text = body.text;
|
6280 | hash = body.hash;
|
6281 | body = body.data;
|
6282 | }
|
6283 | const bodyClone = stream.passiveClone(body);
|
6284 | const result = [];
|
6285 | switch (messageType) {
|
6286 | case enums.armor.multipartSection:
|
6287 | result.push("-----BEGIN PGP MESSAGE, PART " + partIndex + "/" + partTotal + "-----\n");
|
6288 | result.push(addheader(customComment, config));
|
6289 | result.push(encode(body));
|
6290 | result.push("=", getCheckSum(bodyClone));
|
6291 | result.push("-----END PGP MESSAGE, PART " + partIndex + "/" + partTotal + "-----\n");
|
6292 | break;
|
6293 | case enums.armor.multipartLast:
|
6294 | result.push("-----BEGIN PGP MESSAGE, PART " + partIndex + "-----\n");
|
6295 | result.push(addheader(customComment, config));
|
6296 | result.push(encode(body));
|
6297 | result.push("=", getCheckSum(bodyClone));
|
6298 | result.push("-----END PGP MESSAGE, PART " + partIndex + "-----\n");
|
6299 | break;
|
6300 | case enums.armor.signed:
|
6301 | result.push("\n-----BEGIN PGP SIGNED MESSAGE-----\n");
|
6302 | result.push("Hash: " + hash + "\n\n");
|
6303 | result.push(text.replace(/^-/mg, "- -"));
|
6304 | result.push("\n-----BEGIN PGP SIGNATURE-----\n");
|
6305 | result.push(addheader(customComment, config));
|
6306 | result.push(encode(body));
|
6307 | result.push("=", getCheckSum(bodyClone));
|
6308 | result.push("-----END PGP SIGNATURE-----\n");
|
6309 | break;
|
6310 | case enums.armor.message:
|
6311 | result.push("-----BEGIN PGP MESSAGE-----\n");
|
6312 | result.push(addheader(customComment, config));
|
6313 | result.push(encode(body));
|
6314 | result.push("=", getCheckSum(bodyClone));
|
6315 | result.push("-----END PGP MESSAGE-----\n");
|
6316 | break;
|
6317 | case enums.armor.publicKey:
|
6318 | result.push("-----BEGIN PGP PUBLIC KEY BLOCK-----\n");
|
6319 | result.push(addheader(customComment, config));
|
6320 | result.push(encode(body));
|
6321 | result.push("=", getCheckSum(bodyClone));
|
6322 | result.push("-----END PGP PUBLIC KEY BLOCK-----\n");
|
6323 | break;
|
6324 | case enums.armor.privateKey:
|
6325 | result.push("-----BEGIN PGP PRIVATE KEY BLOCK-----\n");
|
6326 | result.push(addheader(customComment, config));
|
6327 | result.push(encode(body));
|
6328 | result.push("=", getCheckSum(bodyClone));
|
6329 | result.push("-----END PGP PRIVATE KEY BLOCK-----\n");
|
6330 | break;
|
6331 | case enums.armor.signature:
|
6332 | result.push("-----BEGIN PGP SIGNATURE-----\n");
|
6333 | result.push(addheader(customComment, config));
|
6334 | result.push(encode(body));
|
6335 | result.push("=", getCheckSum(bodyClone));
|
6336 | result.push("-----END PGP SIGNATURE-----\n");
|
6337 | break;
|
6338 | }
|
6339 |
|
6340 | return util.concat(result);
|
6341 | }
|
6342 |
|
6343 |
|
6344 |
|
6345 |
|
6346 |
|
6347 |
|
6348 |
|
6349 |
|
6350 |
|
6351 |
|
6352 |
|
6353 |
|
6354 | class Keyid {
|
6355 | constructor() {
|
6356 | this.bytes = '';
|
6357 | }
|
6358 |
|
6359 | |
6360 |
|
6361 |
|
6362 |
|
6363 | read(bytes) {
|
6364 | this.bytes = util.uint8ArrayToStr(bytes.subarray(0, 8));
|
6365 | }
|
6366 |
|
6367 | |
6368 |
|
6369 |
|
6370 |
|
6371 | write() {
|
6372 | return util.strToUint8Array(this.bytes);
|
6373 | }
|
6374 |
|
6375 | |
6376 |
|
6377 |
|
6378 |
|
6379 | toHex() {
|
6380 | return util.strToHex(this.bytes);
|
6381 | }
|
6382 |
|
6383 | |
6384 |
|
6385 |
|
6386 |
|
6387 |
|
6388 | equals(keyid, matchWildcard = false) {
|
6389 | return (matchWildcard && (keyid.isWildcard() || this.isWildcard())) || this.bytes === keyid.bytes;
|
6390 | }
|
6391 |
|
6392 | |
6393 |
|
6394 |
|
6395 |
|
6396 | isNull() {
|
6397 | return this.bytes === '';
|
6398 | }
|
6399 |
|
6400 | |
6401 |
|
6402 |
|
6403 |
|
6404 | isWildcard() {
|
6405 | return /^0+$/.test(this.toHex());
|
6406 | }
|
6407 |
|
6408 | static mapToHex(keyId) {
|
6409 | return keyId.toHex();
|
6410 | }
|
6411 |
|
6412 | static fromId(hex) {
|
6413 | const keyid = new Keyid();
|
6414 | keyid.read(util.hexToUint8Array(hex));
|
6415 | return keyid;
|
6416 | }
|
6417 |
|
6418 | static wildcard() {
|
6419 | const keyid = new Keyid();
|
6420 | keyid.read(new Uint8Array(8));
|
6421 | return keyid;
|
6422 | }
|
6423 | }
|
6424 |
|
6425 |
|
6426 |
|
6427 |
|
6428 |
|
6429 |
|
6430 | var AES_asm = function () {
|
6431 |
|
6432 | |
6433 |
|
6434 |
|
6435 | var ginit_done = false;
|
6436 |
|
6437 | |
6438 |
|
6439 |
|
6440 | var gexp3, glog3;
|
6441 |
|
6442 | |
6443 |
|
6444 |
|
6445 | function ginit() {
|
6446 | gexp3 = [],
|
6447 | glog3 = [];
|
6448 |
|
6449 | var a = 1, c, d;
|
6450 | for (c = 0; c < 255; c++) {
|
6451 | gexp3[c] = a;
|
6452 |
|
6453 |
|
6454 | d = a & 0x80, a <<= 1, a &= 255;
|
6455 | if (d === 0x80) a ^= 0x1b;
|
6456 | a ^= gexp3[c];
|
6457 |
|
6458 |
|
6459 | glog3[gexp3[c]] = c;
|
6460 | }
|
6461 | gexp3[255] = gexp3[0];
|
6462 | glog3[0] = 0;
|
6463 |
|
6464 | ginit_done = true;
|
6465 | }
|
6466 |
|
6467 | |
6468 |
|
6469 |
|
6470 |
|
6471 |
|
6472 |
|
6473 | function gmul(a, b) {
|
6474 | var c = gexp3[(glog3[a] + glog3[b]) % 255];
|
6475 | if (a === 0 || b === 0) c = 0;
|
6476 | return c;
|
6477 | }
|
6478 |
|
6479 | |
6480 |
|
6481 |
|
6482 |
|
6483 |
|
6484 | function ginv(a) {
|
6485 | var i = gexp3[255 - glog3[a]];
|
6486 | if (a === 0) i = 0;
|
6487 | return i;
|
6488 | }
|
6489 |
|
6490 | |
6491 |
|
6492 |
|
6493 | var aes_init_done = false;
|
6494 |
|
6495 | |
6496 |
|
6497 |
|
6498 |
|
6499 |
|
6500 | var aes_sbox;
|
6501 |
|
6502 | |
6503 |
|
6504 |
|
6505 | var aes_sinv;
|
6506 |
|
6507 | |
6508 |
|
6509 |
|
6510 | var aes_enc;
|
6511 |
|
6512 | |
6513 |
|
6514 |
|
6515 | var aes_dec;
|
6516 |
|
6517 | |
6518 |
|
6519 |
|
6520 | function aes_init() {
|
6521 | if (!ginit_done) ginit();
|
6522 |
|
6523 |
|
6524 | function _s(a) {
|
6525 | var c, s, x;
|
6526 | s = x = ginv(a);
|
6527 | for (c = 0; c < 4; c++) {
|
6528 | s = ((s << 1) | (s >>> 7)) & 255;
|
6529 | x ^= s;
|
6530 | }
|
6531 | x ^= 99;
|
6532 | return x;
|
6533 | }
|
6534 |
|
6535 |
|
6536 | aes_sbox = [],
|
6537 | aes_sinv = [],
|
6538 | aes_enc = [[], [], [], []],
|
6539 | aes_dec = [[], [], [], []];
|
6540 |
|
6541 | for (var i = 0; i < 256; i++) {
|
6542 | var s = _s(i);
|
6543 |
|
6544 |
|
6545 | aes_sbox[i] = s;
|
6546 | aes_sinv[s] = i;
|
6547 |
|
6548 |
|
6549 | aes_enc[0][i] = (gmul(2, s) << 24) | (s << 16) | (s << 8) | gmul(3, s);
|
6550 | aes_dec[0][s] = (gmul(14, i) << 24) | (gmul(9, i) << 16) | (gmul(13, i) << 8) | gmul(11, i);
|
6551 |
|
6552 | for (var t = 1; t < 4; t++) {
|
6553 | aes_enc[t][i] = (aes_enc[t - 1][i] >>> 8) | (aes_enc[t - 1][i] << 24);
|
6554 | aes_dec[t][s] = (aes_dec[t - 1][s] >>> 8) | (aes_dec[t - 1][s] << 24);
|
6555 | }
|
6556 | }
|
6557 |
|
6558 | aes_init_done = true;
|
6559 | }
|
6560 |
|
6561 | |
6562 |
|
6563 |
|
6564 |
|
6565 |
|
6566 |
|
6567 |
|
6568 |
|
6569 |
|
6570 |
|
6571 |
|
6572 |
|
6573 |
|
6574 |
|
6575 |
|
6576 |
|
6577 |
|
6578 |
|
6579 |
|
6580 |
|
6581 |
|
6582 |
|
6583 |
|
6584 | var wrapper = function (foreign, buffer) {
|
6585 |
|
6586 | if (!aes_init_done) aes_init();
|
6587 |
|
6588 |
|
6589 | var heap = new Uint32Array(buffer);
|
6590 | heap.set(aes_sbox, 0x0800 >> 2);
|
6591 | heap.set(aes_sinv, 0x0c00 >> 2);
|
6592 | for (var i = 0; i < 4; i++) {
|
6593 | heap.set(aes_enc[i], (0x1000 + 0x400 * i) >> 2);
|
6594 | heap.set(aes_dec[i], (0x2000 + 0x400 * i) >> 2);
|
6595 | }
|
6596 |
|
6597 | |
6598 |
|
6599 |
|
6600 |
|
6601 |
|
6602 |
|
6603 |
|
6604 |
|
6605 |
|
6606 |
|
6607 |
|
6608 |
|
6609 |
|
6610 |
|
6611 | function set_key(ks, k0, k1, k2, k3, k4, k5, k6, k7) {
|
6612 | var ekeys = heap.subarray(0x000, 60),
|
6613 | dkeys = heap.subarray(0x100, 0x100 + 60);
|
6614 |
|
6615 |
|
6616 | ekeys.set([k0, k1, k2, k3, k4, k5, k6, k7]);
|
6617 | for (var i = ks, rcon = 1; i < 4 * ks + 28; i++) {
|
6618 | var k = ekeys[i - 1];
|
6619 | if ((i % ks === 0) || (ks === 8 && i % ks === 4)) {
|
6620 | k = aes_sbox[k >>> 24] << 24 ^ aes_sbox[k >>> 16 & 255] << 16 ^ aes_sbox[k >>> 8 & 255] << 8 ^ aes_sbox[k & 255];
|
6621 | }
|
6622 | if (i % ks === 0) {
|
6623 | k = (k << 8) ^ (k >>> 24) ^ (rcon << 24);
|
6624 | rcon = (rcon << 1) ^ ((rcon & 0x80) ? 0x1b : 0);
|
6625 | }
|
6626 | ekeys[i] = ekeys[i - ks] ^ k;
|
6627 | }
|
6628 |
|
6629 |
|
6630 | for (var j = 0; j < i; j += 4) {
|
6631 | for (var jj = 0; jj < 4; jj++) {
|
6632 | var k = ekeys[i - (4 + j) + (4 - jj) % 4];
|
6633 | if (j < 4 || j >= i - 4) {
|
6634 | dkeys[j + jj] = k;
|
6635 | } else {
|
6636 | dkeys[j + jj] = aes_dec[0][aes_sbox[k >>> 24]]
|
6637 | ^ aes_dec[1][aes_sbox[k >>> 16 & 255]]
|
6638 | ^ aes_dec[2][aes_sbox[k >>> 8 & 255]]
|
6639 | ^ aes_dec[3][aes_sbox[k & 255]];
|
6640 | }
|
6641 | }
|
6642 | }
|
6643 |
|
6644 |
|
6645 | asm.set_rounds(ks + 5);
|
6646 | }
|
6647 |
|
6648 |
|
6649 | var stdlib = {Uint8Array: Uint8Array, Uint32Array: Uint32Array};
|
6650 |
|
6651 | var asm = function (stdlib, foreign, buffer) {
|
6652 | "use asm";
|
6653 |
|
6654 | var S0 = 0, S1 = 0, S2 = 0, S3 = 0,
|
6655 | I0 = 0, I1 = 0, I2 = 0, I3 = 0,
|
6656 | N0 = 0, N1 = 0, N2 = 0, N3 = 0,
|
6657 | M0 = 0, M1 = 0, M2 = 0, M3 = 0,
|
6658 | H0 = 0, H1 = 0, H2 = 0, H3 = 0,
|
6659 | R = 0;
|
6660 |
|
6661 | var HEAP = new stdlib.Uint32Array(buffer),
|
6662 | DATA = new stdlib.Uint8Array(buffer);
|
6663 |
|
6664 | |
6665 |
|
6666 |
|
6667 |
|
6668 |
|
6669 |
|
6670 |
|
6671 |
|
6672 |
|
6673 |
|
6674 |
|
6675 | function _core(k, s, t, r, x0, x1, x2, x3) {
|
6676 | k = k | 0;
|
6677 | s = s | 0;
|
6678 | t = t | 0;
|
6679 | r = r | 0;
|
6680 | x0 = x0 | 0;
|
6681 | x1 = x1 | 0;
|
6682 | x2 = x2 | 0;
|
6683 | x3 = x3 | 0;
|
6684 |
|
6685 | var t1 = 0, t2 = 0, t3 = 0,
|
6686 | y0 = 0, y1 = 0, y2 = 0, y3 = 0,
|
6687 | i = 0;
|
6688 |
|
6689 | t1 = t | 0x400, t2 = t | 0x800, t3 = t | 0xc00;
|
6690 |
|
6691 |
|
6692 | x0 = x0 ^ HEAP[(k | 0) >> 2],
|
6693 | x1 = x1 ^ HEAP[(k | 4) >> 2],
|
6694 | x2 = x2 ^ HEAP[(k | 8) >> 2],
|
6695 | x3 = x3 ^ HEAP[(k | 12) >> 2];
|
6696 |
|
6697 |
|
6698 | for (i = 16; (i | 0) <= (r << 4); i = (i + 16) | 0) {
|
6699 | y0 = HEAP[(t | x0 >> 22 & 1020) >> 2] ^ HEAP[(t1 | x1 >> 14 & 1020) >> 2] ^ HEAP[(t2 | x2 >> 6 & 1020) >> 2] ^ HEAP[(t3 | x3 << 2 & 1020) >> 2] ^ HEAP[(k | i | 0) >> 2],
|
6700 | y1 = HEAP[(t | x1 >> 22 & 1020) >> 2] ^ HEAP[(t1 | x2 >> 14 & 1020) >> 2] ^ HEAP[(t2 | x3 >> 6 & 1020) >> 2] ^ HEAP[(t3 | x0 << 2 & 1020) >> 2] ^ HEAP[(k | i | 4) >> 2],
|
6701 | y2 = HEAP[(t | x2 >> 22 & 1020) >> 2] ^ HEAP[(t1 | x3 >> 14 & 1020) >> 2] ^ HEAP[(t2 | x0 >> 6 & 1020) >> 2] ^ HEAP[(t3 | x1 << 2 & 1020) >> 2] ^ HEAP[(k | i | 8) >> 2],
|
6702 | y3 = HEAP[(t | x3 >> 22 & 1020) >> 2] ^ HEAP[(t1 | x0 >> 14 & 1020) >> 2] ^ HEAP[(t2 | x1 >> 6 & 1020) >> 2] ^ HEAP[(t3 | x2 << 2 & 1020) >> 2] ^ HEAP[(k | i | 12) >> 2];
|
6703 | x0 = y0, x1 = y1, x2 = y2, x3 = y3;
|
6704 | }
|
6705 |
|
6706 |
|
6707 | S0 = HEAP[(s | x0 >> 22 & 1020) >> 2] << 24 ^ HEAP[(s | x1 >> 14 & 1020) >> 2] << 16 ^ HEAP[(s | x2 >> 6 & 1020) >> 2] << 8 ^ HEAP[(s | x3 << 2 & 1020) >> 2] ^ HEAP[(k | i | 0) >> 2],
|
6708 | S1 = HEAP[(s | x1 >> 22 & 1020) >> 2] << 24 ^ HEAP[(s | x2 >> 14 & 1020) >> 2] << 16 ^ HEAP[(s | x3 >> 6 & 1020) >> 2] << 8 ^ HEAP[(s | x0 << 2 & 1020) >> 2] ^ HEAP[(k | i | 4) >> 2],
|
6709 | S2 = HEAP[(s | x2 >> 22 & 1020) >> 2] << 24 ^ HEAP[(s | x3 >> 14 & 1020) >> 2] << 16 ^ HEAP[(s | x0 >> 6 & 1020) >> 2] << 8 ^ HEAP[(s | x1 << 2 & 1020) >> 2] ^ HEAP[(k | i | 8) >> 2],
|
6710 | S3 = HEAP[(s | x3 >> 22 & 1020) >> 2] << 24 ^ HEAP[(s | x0 >> 14 & 1020) >> 2] << 16 ^ HEAP[(s | x1 >> 6 & 1020) >> 2] << 8 ^ HEAP[(s | x2 << 2 & 1020) >> 2] ^ HEAP[(k | i | 12) >> 2];
|
6711 | }
|
6712 |
|
6713 | |
6714 |
|
6715 |
|
6716 |
|
6717 |
|
6718 |
|
6719 |
|
6720 | function _ecb_enc(x0, x1, x2, x3) {
|
6721 | x0 = x0 | 0;
|
6722 | x1 = x1 | 0;
|
6723 | x2 = x2 | 0;
|
6724 | x3 = x3 | 0;
|
6725 |
|
6726 | _core(
|
6727 | 0x0000, 0x0800, 0x1000,
|
6728 | R,
|
6729 | x0,
|
6730 | x1,
|
6731 | x2,
|
6732 | x3
|
6733 | );
|
6734 | }
|
6735 |
|
6736 | |
6737 |
|
6738 |
|
6739 |
|
6740 |
|
6741 |
|
6742 |
|
6743 | function _ecb_dec(x0, x1, x2, x3) {
|
6744 | x0 = x0 | 0;
|
6745 | x1 = x1 | 0;
|
6746 | x2 = x2 | 0;
|
6747 | x3 = x3 | 0;
|
6748 |
|
6749 | var t = 0;
|
6750 |
|
6751 | _core(
|
6752 | 0x0400, 0x0c00, 0x2000,
|
6753 | R,
|
6754 | x0,
|
6755 | x3,
|
6756 | x2,
|
6757 | x1
|
6758 | );
|
6759 |
|
6760 | t = S1, S1 = S3, S3 = t;
|
6761 | }
|
6762 |
|
6763 |
|
6764 | |
6765 |
|
6766 |
|
6767 |
|
6768 |
|
6769 |
|
6770 |
|
6771 | function _cbc_enc(x0, x1, x2, x3) {
|
6772 | x0 = x0 | 0;
|
6773 | x1 = x1 | 0;
|
6774 | x2 = x2 | 0;
|
6775 | x3 = x3 | 0;
|
6776 |
|
6777 | _core(
|
6778 | 0x0000, 0x0800, 0x1000,
|
6779 | R,
|
6780 | I0 ^ x0,
|
6781 | I1 ^ x1,
|
6782 | I2 ^ x2,
|
6783 | I3 ^ x3
|
6784 | );
|
6785 |
|
6786 | I0 = S0,
|
6787 | I1 = S1,
|
6788 | I2 = S2,
|
6789 | I3 = S3;
|
6790 | }
|
6791 |
|
6792 | |
6793 |
|
6794 |
|
6795 |
|
6796 |
|
6797 |
|
6798 |
|
6799 | function _cbc_dec(x0, x1, x2, x3) {
|
6800 | x0 = x0 | 0;
|
6801 | x1 = x1 | 0;
|
6802 | x2 = x2 | 0;
|
6803 | x3 = x3 | 0;
|
6804 |
|
6805 | var t = 0;
|
6806 |
|
6807 | _core(
|
6808 | 0x0400, 0x0c00, 0x2000,
|
6809 | R,
|
6810 | x0,
|
6811 | x3,
|
6812 | x2,
|
6813 | x1
|
6814 | );
|
6815 |
|
6816 | t = S1, S1 = S3, S3 = t;
|
6817 |
|
6818 | S0 = S0 ^ I0,
|
6819 | S1 = S1 ^ I1,
|
6820 | S2 = S2 ^ I2,
|
6821 | S3 = S3 ^ I3;
|
6822 |
|
6823 | I0 = x0,
|
6824 | I1 = x1,
|
6825 | I2 = x2,
|
6826 | I3 = x3;
|
6827 | }
|
6828 |
|
6829 | |
6830 |
|
6831 |
|
6832 |
|
6833 |
|
6834 |
|
6835 |
|
6836 | function _cfb_enc(x0, x1, x2, x3) {
|
6837 | x0 = x0 | 0;
|
6838 | x1 = x1 | 0;
|
6839 | x2 = x2 | 0;
|
6840 | x3 = x3 | 0;
|
6841 |
|
6842 | _core(
|
6843 | 0x0000, 0x0800, 0x1000,
|
6844 | R,
|
6845 | I0,
|
6846 | I1,
|
6847 | I2,
|
6848 | I3
|
6849 | );
|
6850 |
|
6851 | I0 = S0 = S0 ^ x0,
|
6852 | I1 = S1 = S1 ^ x1,
|
6853 | I2 = S2 = S2 ^ x2,
|
6854 | I3 = S3 = S3 ^ x3;
|
6855 | }
|
6856 |
|
6857 |
|
6858 | |
6859 |
|
6860 |
|
6861 |
|
6862 |
|
6863 |
|
6864 |
|
6865 | function _cfb_dec(x0, x1, x2, x3) {
|
6866 | x0 = x0 | 0;
|
6867 | x1 = x1 | 0;
|
6868 | x2 = x2 | 0;
|
6869 | x3 = x3 | 0;
|
6870 |
|
6871 | _core(
|
6872 | 0x0000, 0x0800, 0x1000,
|
6873 | R,
|
6874 | I0,
|
6875 | I1,
|
6876 | I2,
|
6877 | I3
|
6878 | );
|
6879 |
|
6880 | S0 = S0 ^ x0,
|
6881 | S1 = S1 ^ x1,
|
6882 | S2 = S2 ^ x2,
|
6883 | S3 = S3 ^ x3;
|
6884 |
|
6885 | I0 = x0,
|
6886 | I1 = x1,
|
6887 | I2 = x2,
|
6888 | I3 = x3;
|
6889 | }
|
6890 |
|
6891 | |
6892 |
|
6893 |
|
6894 |
|
6895 |
|
6896 |
|
6897 |
|
6898 | function _ofb(x0, x1, x2, x3) {
|
6899 | x0 = x0 | 0;
|
6900 | x1 = x1 | 0;
|
6901 | x2 = x2 | 0;
|
6902 | x3 = x3 | 0;
|
6903 |
|
6904 | _core(
|
6905 | 0x0000, 0x0800, 0x1000,
|
6906 | R,
|
6907 | I0,
|
6908 | I1,
|
6909 | I2,
|
6910 | I3
|
6911 | );
|
6912 |
|
6913 | I0 = S0,
|
6914 | I1 = S1,
|
6915 | I2 = S2,
|
6916 | I3 = S3;
|
6917 |
|
6918 | S0 = S0 ^ x0,
|
6919 | S1 = S1 ^ x1,
|
6920 | S2 = S2 ^ x2,
|
6921 | S3 = S3 ^ x3;
|
6922 | }
|
6923 |
|
6924 | |
6925 |
|
6926 |
|
6927 |
|
6928 |
|
6929 |
|
6930 |
|
6931 | function _ctr(x0, x1, x2, x3) {
|
6932 | x0 = x0 | 0;
|
6933 | x1 = x1 | 0;
|
6934 | x2 = x2 | 0;
|
6935 | x3 = x3 | 0;
|
6936 |
|
6937 | _core(
|
6938 | 0x0000, 0x0800, 0x1000,
|
6939 | R,
|
6940 | N0,
|
6941 | N1,
|
6942 | N2,
|
6943 | N3
|
6944 | );
|
6945 |
|
6946 | N3 = (~M3 & N3) | M3 & (N3 + 1);
|
6947 | N2 = (~M2 & N2) | M2 & (N2 + ((N3 | 0) == 0));
|
6948 | N1 = (~M1 & N1) | M1 & (N1 + ((N2 | 0) == 0));
|
6949 | N0 = (~M0 & N0) | M0 & (N0 + ((N1 | 0) == 0));
|
6950 |
|
6951 | S0 = S0 ^ x0;
|
6952 | S1 = S1 ^ x1;
|
6953 | S2 = S2 ^ x2;
|
6954 | S3 = S3 ^ x3;
|
6955 | }
|
6956 |
|
6957 | |
6958 |
|
6959 |
|
6960 |
|
6961 |
|
6962 |
|
6963 |
|
6964 | function _gcm_mac(x0, x1, x2, x3) {
|
6965 | x0 = x0 | 0;
|
6966 | x1 = x1 | 0;
|
6967 | x2 = x2 | 0;
|
6968 | x3 = x3 | 0;
|
6969 |
|
6970 | var y0 = 0, y1 = 0, y2 = 0, y3 = 0,
|
6971 | z0 = 0, z1 = 0, z2 = 0, z3 = 0,
|
6972 | i = 0, c = 0;
|
6973 |
|
6974 | x0 = x0 ^ I0,
|
6975 | x1 = x1 ^ I1,
|
6976 | x2 = x2 ^ I2,
|
6977 | x3 = x3 ^ I3;
|
6978 |
|
6979 | y0 = H0 | 0,
|
6980 | y1 = H1 | 0,
|
6981 | y2 = H2 | 0,
|
6982 | y3 = H3 | 0;
|
6983 |
|
6984 | for (; (i | 0) < 128; i = (i + 1) | 0) {
|
6985 | if (y0 >>> 31) {
|
6986 | z0 = z0 ^ x0,
|
6987 | z1 = z1 ^ x1,
|
6988 | z2 = z2 ^ x2,
|
6989 | z3 = z3 ^ x3;
|
6990 | }
|
6991 |
|
6992 | y0 = (y0 << 1) | (y1 >>> 31),
|
6993 | y1 = (y1 << 1) | (y2 >>> 31),
|
6994 | y2 = (y2 << 1) | (y3 >>> 31),
|
6995 | y3 = (y3 << 1);
|
6996 |
|
6997 | c = x3 & 1;
|
6998 |
|
6999 | x3 = (x3 >>> 1) | (x2 << 31),
|
7000 | x2 = (x2 >>> 1) | (x1 << 31),
|
7001 | x1 = (x1 >>> 1) | (x0 << 31),
|
7002 | x0 = (x0 >>> 1);
|
7003 |
|
7004 | if (c) x0 = x0 ^ 0xe1000000;
|
7005 | }
|
7006 |
|
7007 | I0 = z0,
|
7008 | I1 = z1,
|
7009 | I2 = z2,
|
7010 | I3 = z3;
|
7011 | }
|
7012 |
|
7013 | |
7014 |
|
7015 |
|
7016 |
|
7017 |
|
7018 |
|
7019 | function set_rounds(r) {
|
7020 | r = r | 0;
|
7021 | R = r;
|
7022 | }
|
7023 |
|
7024 | |
7025 |
|
7026 |
|
7027 |
|
7028 |
|
7029 |
|
7030 |
|
7031 |
|
7032 |
|
7033 | function set_state(s0, s1, s2, s3) {
|
7034 | s0 = s0 | 0;
|
7035 | s1 = s1 | 0;
|
7036 | s2 = s2 | 0;
|
7037 | s3 = s3 | 0;
|
7038 |
|
7039 | S0 = s0,
|
7040 | S1 = s1,
|
7041 | S2 = s2,
|
7042 | S3 = s3;
|
7043 | }
|
7044 |
|
7045 | |
7046 |
|
7047 |
|
7048 |
|
7049 |
|
7050 |
|
7051 |
|
7052 |
|
7053 |
|
7054 | function set_iv(i0, i1, i2, i3) {
|
7055 | i0 = i0 | 0;
|
7056 | i1 = i1 | 0;
|
7057 | i2 = i2 | 0;
|
7058 | i3 = i3 | 0;
|
7059 |
|
7060 | I0 = i0,
|
7061 | I1 = i1,
|
7062 | I2 = i2,
|
7063 | I3 = i3;
|
7064 | }
|
7065 |
|
7066 | |
7067 |
|
7068 |
|
7069 |
|
7070 |
|
7071 |
|
7072 |
|
7073 |
|
7074 |
|
7075 | function set_nonce(n0, n1, n2, n3) {
|
7076 | n0 = n0 | 0;
|
7077 | n1 = n1 | 0;
|
7078 | n2 = n2 | 0;
|
7079 | n3 = n3 | 0;
|
7080 |
|
7081 | N0 = n0,
|
7082 | N1 = n1,
|
7083 | N2 = n2,
|
7084 | N3 = n3;
|
7085 | }
|
7086 |
|
7087 | |
7088 |
|
7089 |
|
7090 |
|
7091 |
|
7092 |
|
7093 |
|
7094 |
|
7095 |
|
7096 | function set_mask(m0, m1, m2, m3) {
|
7097 | m0 = m0 | 0;
|
7098 | m1 = m1 | 0;
|
7099 | m2 = m2 | 0;
|
7100 | m3 = m3 | 0;
|
7101 |
|
7102 | M0 = m0,
|
7103 | M1 = m1,
|
7104 | M2 = m2,
|
7105 | M3 = m3;
|
7106 | }
|
7107 |
|
7108 | |
7109 |
|
7110 |
|
7111 |
|
7112 |
|
7113 |
|
7114 |
|
7115 |
|
7116 |
|
7117 | function set_counter(c0, c1, c2, c3) {
|
7118 | c0 = c0 | 0;
|
7119 | c1 = c1 | 0;
|
7120 | c2 = c2 | 0;
|
7121 | c3 = c3 | 0;
|
7122 |
|
7123 | N3 = (~M3 & N3) | M3 & c3,
|
7124 | N2 = (~M2 & N2) | M2 & c2,
|
7125 | N1 = (~M1 & N1) | M1 & c1,
|
7126 | N0 = (~M0 & N0) | M0 & c0;
|
7127 | }
|
7128 |
|
7129 | |
7130 |
|
7131 |
|
7132 |
|
7133 |
|
7134 |
|
7135 |
|
7136 | function get_state(pos) {
|
7137 | pos = pos | 0;
|
7138 |
|
7139 | if (pos & 15) return -1;
|
7140 |
|
7141 | DATA[pos | 0] = S0 >>> 24,
|
7142 | DATA[pos | 1] = S0 >>> 16 & 255,
|
7143 | DATA[pos | 2] = S0 >>> 8 & 255,
|
7144 | DATA[pos | 3] = S0 & 255,
|
7145 | DATA[pos | 4] = S1 >>> 24,
|
7146 | DATA[pos | 5] = S1 >>> 16 & 255,
|
7147 | DATA[pos | 6] = S1 >>> 8 & 255,
|
7148 | DATA[pos | 7] = S1 & 255,
|
7149 | DATA[pos | 8] = S2 >>> 24,
|
7150 | DATA[pos | 9] = S2 >>> 16 & 255,
|
7151 | DATA[pos | 10] = S2 >>> 8 & 255,
|
7152 | DATA[pos | 11] = S2 & 255,
|
7153 | DATA[pos | 12] = S3 >>> 24,
|
7154 | DATA[pos | 13] = S3 >>> 16 & 255,
|
7155 | DATA[pos | 14] = S3 >>> 8 & 255,
|
7156 | DATA[pos | 15] = S3 & 255;
|
7157 |
|
7158 | return 16;
|
7159 | }
|
7160 |
|
7161 | |
7162 |
|
7163 |
|
7164 |
|
7165 |
|
7166 |
|
7167 |
|
7168 | function get_iv(pos) {
|
7169 | pos = pos | 0;
|
7170 |
|
7171 | if (pos & 15) return -1;
|
7172 |
|
7173 | DATA[pos | 0] = I0 >>> 24,
|
7174 | DATA[pos | 1] = I0 >>> 16 & 255,
|
7175 | DATA[pos | 2] = I0 >>> 8 & 255,
|
7176 | DATA[pos | 3] = I0 & 255,
|
7177 | DATA[pos | 4] = I1 >>> 24,
|
7178 | DATA[pos | 5] = I1 >>> 16 & 255,
|
7179 | DATA[pos | 6] = I1 >>> 8 & 255,
|
7180 | DATA[pos | 7] = I1 & 255,
|
7181 | DATA[pos | 8] = I2 >>> 24,
|
7182 | DATA[pos | 9] = I2 >>> 16 & 255,
|
7183 | DATA[pos | 10] = I2 >>> 8 & 255,
|
7184 | DATA[pos | 11] = I2 & 255,
|
7185 | DATA[pos | 12] = I3 >>> 24,
|
7186 | DATA[pos | 13] = I3 >>> 16 & 255,
|
7187 | DATA[pos | 14] = I3 >>> 8 & 255,
|
7188 | DATA[pos | 15] = I3 & 255;
|
7189 |
|
7190 | return 16;
|
7191 | }
|
7192 |
|
7193 | |
7194 |
|
7195 |
|
7196 |
|
7197 |
|
7198 | function gcm_init() {
|
7199 | _ecb_enc(0, 0, 0, 0);
|
7200 | H0 = S0,
|
7201 | H1 = S1,
|
7202 | H2 = S2,
|
7203 | H3 = S3;
|
7204 | }
|
7205 |
|
7206 | |
7207 |
|
7208 |
|
7209 |
|
7210 |
|
7211 |
|
7212 |
|
7213 |
|
7214 |
|
7215 | function cipher(mode, pos, len) {
|
7216 | mode = mode | 0;
|
7217 | pos = pos | 0;
|
7218 | len = len | 0;
|
7219 |
|
7220 | var ret = 0;
|
7221 |
|
7222 | if (pos & 15) return -1;
|
7223 |
|
7224 | while ((len | 0) >= 16) {
|
7225 | _cipher_modes[mode & 7](
|
7226 | DATA[pos | 0] << 24 | DATA[pos | 1] << 16 | DATA[pos | 2] << 8 | DATA[pos | 3],
|
7227 | DATA[pos | 4] << 24 | DATA[pos | 5] << 16 | DATA[pos | 6] << 8 | DATA[pos | 7],
|
7228 | DATA[pos | 8] << 24 | DATA[pos | 9] << 16 | DATA[pos | 10] << 8 | DATA[pos | 11],
|
7229 | DATA[pos | 12] << 24 | DATA[pos | 13] << 16 | DATA[pos | 14] << 8 | DATA[pos | 15]
|
7230 | );
|
7231 |
|
7232 | DATA[pos | 0] = S0 >>> 24,
|
7233 | DATA[pos | 1] = S0 >>> 16 & 255,
|
7234 | DATA[pos | 2] = S0 >>> 8 & 255,
|
7235 | DATA[pos | 3] = S0 & 255,
|
7236 | DATA[pos | 4] = S1 >>> 24,
|
7237 | DATA[pos | 5] = S1 >>> 16 & 255,
|
7238 | DATA[pos | 6] = S1 >>> 8 & 255,
|
7239 | DATA[pos | 7] = S1 & 255,
|
7240 | DATA[pos | 8] = S2 >>> 24,
|
7241 | DATA[pos | 9] = S2 >>> 16 & 255,
|
7242 | DATA[pos | 10] = S2 >>> 8 & 255,
|
7243 | DATA[pos | 11] = S2 & 255,
|
7244 | DATA[pos | 12] = S3 >>> 24,
|
7245 | DATA[pos | 13] = S3 >>> 16 & 255,
|
7246 | DATA[pos | 14] = S3 >>> 8 & 255,
|
7247 | DATA[pos | 15] = S3 & 255;
|
7248 |
|
7249 | ret = (ret + 16) | 0,
|
7250 | pos = (pos + 16) | 0,
|
7251 | len = (len - 16) | 0;
|
7252 | }
|
7253 |
|
7254 | return ret | 0;
|
7255 | }
|
7256 |
|
7257 | |
7258 |
|
7259 |
|
7260 |
|
7261 |
|
7262 |
|
7263 |
|
7264 |
|
7265 |
|
7266 | function mac(mode, pos, len) {
|
7267 | mode = mode | 0;
|
7268 | pos = pos | 0;
|
7269 | len = len | 0;
|
7270 |
|
7271 | var ret = 0;
|
7272 |
|
7273 | if (pos & 15) return -1;
|
7274 |
|
7275 | while ((len | 0) >= 16) {
|
7276 | _mac_modes[mode & 1](
|
7277 | DATA[pos | 0] << 24 | DATA[pos | 1] << 16 | DATA[pos | 2] << 8 | DATA[pos | 3],
|
7278 | DATA[pos | 4] << 24 | DATA[pos | 5] << 16 | DATA[pos | 6] << 8 | DATA[pos | 7],
|
7279 | DATA[pos | 8] << 24 | DATA[pos | 9] << 16 | DATA[pos | 10] << 8 | DATA[pos | 11],
|
7280 | DATA[pos | 12] << 24 | DATA[pos | 13] << 16 | DATA[pos | 14] << 8 | DATA[pos | 15]
|
7281 | );
|
7282 |
|
7283 | ret = (ret + 16) | 0,
|
7284 | pos = (pos + 16) | 0,
|
7285 | len = (len - 16) | 0;
|
7286 | }
|
7287 |
|
7288 | return ret | 0;
|
7289 | }
|
7290 |
|
7291 | |
7292 |
|
7293 |
|
7294 | var _cipher_modes = [_ecb_enc, _ecb_dec, _cbc_enc, _cbc_dec, _cfb_enc, _cfb_dec, _ofb, _ctr];
|
7295 |
|
7296 | |
7297 |
|
7298 |
|
7299 | var _mac_modes = [_cbc_enc, _gcm_mac];
|
7300 |
|
7301 | |
7302 |
|
7303 |
|
7304 | return {
|
7305 | set_rounds: set_rounds,
|
7306 | set_state: set_state,
|
7307 | set_iv: set_iv,
|
7308 | set_nonce: set_nonce,
|
7309 | set_mask: set_mask,
|
7310 | set_counter: set_counter,
|
7311 | get_state: get_state,
|
7312 | get_iv: get_iv,
|
7313 | gcm_init: gcm_init,
|
7314 | cipher: cipher,
|
7315 | mac: mac,
|
7316 | };
|
7317 | }(stdlib, foreign, buffer);
|
7318 |
|
7319 | asm.set_key = set_key;
|
7320 |
|
7321 | return asm;
|
7322 | };
|
7323 |
|
7324 | |
7325 |
|
7326 |
|
7327 |
|
7328 |
|
7329 | wrapper.ENC = {
|
7330 | ECB: 0,
|
7331 | CBC: 2,
|
7332 | CFB: 4,
|
7333 | OFB: 6,
|
7334 | CTR: 7,
|
7335 | },
|
7336 |
|
7337 | |
7338 |
|
7339 |
|
7340 |
|
7341 |
|
7342 | wrapper.DEC = {
|
7343 | ECB: 1,
|
7344 | CBC: 3,
|
7345 | CFB: 5,
|
7346 | OFB: 6,
|
7347 | CTR: 7,
|
7348 | },
|
7349 |
|
7350 | |
7351 |
|
7352 |
|
7353 |
|
7354 |
|
7355 | wrapper.MAC = {
|
7356 | CBC: 0,
|
7357 | GCM: 1,
|
7358 | };
|
7359 |
|
7360 | |
7361 |
|
7362 |
|
7363 |
|
7364 |
|
7365 | wrapper.HEAP_DATA = 0x4000;
|
7366 |
|
7367 | return wrapper;
|
7368 | }();
|
7369 |
|
7370 | function is_bytes(a) {
|
7371 | return a instanceof Uint8Array;
|
7372 | }
|
7373 | function _heap_init(heap, heapSize) {
|
7374 | const size = heap ? heap.byteLength : heapSize || 65536;
|
7375 | if (size & 0xfff || size <= 0)
|
7376 | throw new Error('heap size must be a positive integer and a multiple of 4096');
|
7377 | heap = heap || new Uint8Array(new ArrayBuffer(size));
|
7378 | return heap;
|
7379 | }
|
7380 | function _heap_write(heap, hpos, data, dpos, dlen) {
|
7381 | const hlen = heap.length - hpos;
|
7382 | const wlen = hlen < dlen ? hlen : dlen;
|
7383 | heap.set(data.subarray(dpos, dpos + wlen), hpos);
|
7384 | return wlen;
|
7385 | }
|
7386 | function joinBytes(...arg) {
|
7387 | const totalLenght = arg.reduce((sum, curr) => sum + curr.length, 0);
|
7388 | const ret = new Uint8Array(totalLenght);
|
7389 | let cursor = 0;
|
7390 | for (let i = 0; i < arg.length; i++) {
|
7391 | ret.set(arg[i], cursor);
|
7392 | cursor += arg[i].length;
|
7393 | }
|
7394 | return ret;
|
7395 | }
|
7396 |
|
7397 | class IllegalStateError extends Error {
|
7398 | constructor(...args) {
|
7399 | super(...args);
|
7400 | }
|
7401 | }
|
7402 | class IllegalArgumentError extends Error {
|
7403 | constructor(...args) {
|
7404 | super(...args);
|
7405 | }
|
7406 | }
|
7407 | class SecurityError extends Error {
|
7408 | constructor(...args) {
|
7409 | super(...args);
|
7410 | }
|
7411 | }
|
7412 |
|
7413 | const heap_pool = [];
|
7414 | const asm_pool = [];
|
7415 | class AES {
|
7416 | constructor(key, iv, padding = true, mode, heap, asm) {
|
7417 | this.pos = 0;
|
7418 | this.len = 0;
|
7419 | this.mode = mode;
|
7420 |
|
7421 | this.pos = 0;
|
7422 | this.len = 0;
|
7423 | this.key = key;
|
7424 | this.iv = iv;
|
7425 | this.padding = padding;
|
7426 |
|
7427 | this.acquire_asm(heap, asm);
|
7428 | }
|
7429 | acquire_asm(heap, asm) {
|
7430 | if (this.heap === undefined || this.asm === undefined) {
|
7431 | this.heap = heap || heap_pool.pop() || _heap_init().subarray(AES_asm.HEAP_DATA);
|
7432 | this.asm = asm || asm_pool.pop() || new AES_asm(null, this.heap.buffer);
|
7433 | this.reset(this.key, this.iv);
|
7434 | }
|
7435 | return { heap: this.heap, asm: this.asm };
|
7436 | }
|
7437 | release_asm() {
|
7438 | if (this.heap !== undefined && this.asm !== undefined) {
|
7439 | heap_pool.push(this.heap);
|
7440 | asm_pool.push(this.asm);
|
7441 | }
|
7442 | this.heap = undefined;
|
7443 | this.asm = undefined;
|
7444 | }
|
7445 | reset(key, iv) {
|
7446 | const { asm } = this.acquire_asm();
|
7447 |
|
7448 | const keylen = key.length;
|
7449 | if (keylen !== 16 && keylen !== 24 && keylen !== 32)
|
7450 | throw new IllegalArgumentError('illegal key size');
|
7451 | const keyview = new DataView(key.buffer, key.byteOffset, key.byteLength);
|
7452 | asm.set_key(keylen >> 2, keyview.getUint32(0), keyview.getUint32(4), keyview.getUint32(8), keyview.getUint32(12), keylen > 16 ? keyview.getUint32(16) : 0, keylen > 16 ? keyview.getUint32(20) : 0, keylen > 24 ? keyview.getUint32(24) : 0, keylen > 24 ? keyview.getUint32(28) : 0);
|
7453 |
|
7454 | if (iv !== undefined) {
|
7455 | if (iv.length !== 16)
|
7456 | throw new IllegalArgumentError('illegal iv size');
|
7457 | let ivview = new DataView(iv.buffer, iv.byteOffset, iv.byteLength);
|
7458 | asm.set_iv(ivview.getUint32(0), ivview.getUint32(4), ivview.getUint32(8), ivview.getUint32(12));
|
7459 | }
|
7460 | else {
|
7461 | asm.set_iv(0, 0, 0, 0);
|
7462 | }
|
7463 | }
|
7464 | AES_Encrypt_process(data) {
|
7465 | if (!is_bytes(data))
|
7466 | throw new TypeError("data isn't of expected type");
|
7467 | let { heap, asm } = this.acquire_asm();
|
7468 | let amode = AES_asm.ENC[this.mode];
|
7469 | let hpos = AES_asm.HEAP_DATA;
|
7470 | let pos = this.pos;
|
7471 | let len = this.len;
|
7472 | let dpos = 0;
|
7473 | let dlen = data.length || 0;
|
7474 | let rpos = 0;
|
7475 | let rlen = (len + dlen) & -16;
|
7476 | let wlen = 0;
|
7477 | let result = new Uint8Array(rlen);
|
7478 | while (dlen > 0) {
|
7479 | wlen = _heap_write(heap, pos + len, data, dpos, dlen);
|
7480 | len += wlen;
|
7481 | dpos += wlen;
|
7482 | dlen -= wlen;
|
7483 | wlen = asm.cipher(amode, hpos + pos, len);
|
7484 | if (wlen)
|
7485 | result.set(heap.subarray(pos, pos + wlen), rpos);
|
7486 | rpos += wlen;
|
7487 | if (wlen < len) {
|
7488 | pos += wlen;
|
7489 | len -= wlen;
|
7490 | }
|
7491 | else {
|
7492 | pos = 0;
|
7493 | len = 0;
|
7494 | }
|
7495 | }
|
7496 | this.pos = pos;
|
7497 | this.len = len;
|
7498 | return result;
|
7499 | }
|
7500 | AES_Encrypt_finish() {
|
7501 | let { heap, asm } = this.acquire_asm();
|
7502 | let amode = AES_asm.ENC[this.mode];
|
7503 | let hpos = AES_asm.HEAP_DATA;
|
7504 | let pos = this.pos;
|
7505 | let len = this.len;
|
7506 | let plen = 16 - (len % 16);
|
7507 | let rlen = len;
|
7508 | if (this.hasOwnProperty('padding')) {
|
7509 | if (this.padding) {
|
7510 | for (let p = 0; p < plen; ++p) {
|
7511 | heap[pos + len + p] = plen;
|
7512 | }
|
7513 | len += plen;
|
7514 | rlen = len;
|
7515 | }
|
7516 | else if (len % 16) {
|
7517 | throw new IllegalArgumentError('data length must be a multiple of the block size');
|
7518 | }
|
7519 | }
|
7520 | else {
|
7521 | len += plen;
|
7522 | }
|
7523 | const result = new Uint8Array(rlen);
|
7524 | if (len)
|
7525 | asm.cipher(amode, hpos + pos, len);
|
7526 | if (rlen)
|
7527 | result.set(heap.subarray(pos, pos + rlen));
|
7528 | this.pos = 0;
|
7529 | this.len = 0;
|
7530 | this.release_asm();
|
7531 | return result;
|
7532 | }
|
7533 | AES_Decrypt_process(data) {
|
7534 | if (!is_bytes(data))
|
7535 | throw new TypeError("data isn't of expected type");
|
7536 | let { heap, asm } = this.acquire_asm();
|
7537 | let amode = AES_asm.DEC[this.mode];
|
7538 | let hpos = AES_asm.HEAP_DATA;
|
7539 | let pos = this.pos;
|
7540 | let len = this.len;
|
7541 | let dpos = 0;
|
7542 | let dlen = data.length || 0;
|
7543 | let rpos = 0;
|
7544 | let rlen = (len + dlen) & -16;
|
7545 | let plen = 0;
|
7546 | let wlen = 0;
|
7547 | if (this.padding) {
|
7548 | plen = len + dlen - rlen || 16;
|
7549 | rlen -= plen;
|
7550 | }
|
7551 | const result = new Uint8Array(rlen);
|
7552 | while (dlen > 0) {
|
7553 | wlen = _heap_write(heap, pos + len, data, dpos, dlen);
|
7554 | len += wlen;
|
7555 | dpos += wlen;
|
7556 | dlen -= wlen;
|
7557 | wlen = asm.cipher(amode, hpos + pos, len - (!dlen ? plen : 0));
|
7558 | if (wlen)
|
7559 | result.set(heap.subarray(pos, pos + wlen), rpos);
|
7560 | rpos += wlen;
|
7561 | if (wlen < len) {
|
7562 | pos += wlen;
|
7563 | len -= wlen;
|
7564 | }
|
7565 | else {
|
7566 | pos = 0;
|
7567 | len = 0;
|
7568 | }
|
7569 | }
|
7570 | this.pos = pos;
|
7571 | this.len = len;
|
7572 | return result;
|
7573 | }
|
7574 | AES_Decrypt_finish() {
|
7575 | let { heap, asm } = this.acquire_asm();
|
7576 | let amode = AES_asm.DEC[this.mode];
|
7577 | let hpos = AES_asm.HEAP_DATA;
|
7578 | let pos = this.pos;
|
7579 | let len = this.len;
|
7580 | let rlen = len;
|
7581 | if (len > 0) {
|
7582 | if (len % 16) {
|
7583 | if (this.hasOwnProperty('padding')) {
|
7584 | throw new IllegalArgumentError('data length must be a multiple of the block size');
|
7585 | }
|
7586 | else {
|
7587 | len += 16 - (len % 16);
|
7588 | }
|
7589 | }
|
7590 | asm.cipher(amode, hpos + pos, len);
|
7591 | if (this.hasOwnProperty('padding') && this.padding) {
|
7592 | let pad = heap[pos + rlen - 1];
|
7593 | if (pad < 1 || pad > 16 || pad > rlen)
|
7594 | throw new SecurityError('bad padding');
|
7595 | let pcheck = 0;
|
7596 | for (let i = pad; i > 1; i--)
|
7597 | pcheck |= pad ^ heap[pos + rlen - i];
|
7598 | if (pcheck)
|
7599 | throw new SecurityError('bad padding');
|
7600 | rlen -= pad;
|
7601 | }
|
7602 | }
|
7603 | const result = new Uint8Array(rlen);
|
7604 | if (rlen > 0) {
|
7605 | result.set(heap.subarray(pos, pos + rlen));
|
7606 | }
|
7607 | this.pos = 0;
|
7608 | this.len = 0;
|
7609 | this.release_asm();
|
7610 | return result;
|
7611 | }
|
7612 | }
|
7613 |
|
7614 | class AES_ECB {
|
7615 | static encrypt(data, key, padding = false) {
|
7616 | return new AES_ECB(key, padding).encrypt(data);
|
7617 | }
|
7618 | static decrypt(data, key, padding = false) {
|
7619 | return new AES_ECB(key, padding).decrypt(data);
|
7620 | }
|
7621 | constructor(key, padding = false, aes) {
|
7622 | this.aes = aes ? aes : new AES(key, undefined, padding, 'ECB');
|
7623 | }
|
7624 | encrypt(data) {
|
7625 | const r1 = this.aes.AES_Encrypt_process(data);
|
7626 | const r2 = this.aes.AES_Encrypt_finish();
|
7627 | return joinBytes(r1, r2);
|
7628 | }
|
7629 | decrypt(data) {
|
7630 | const r1 = this.aes.AES_Decrypt_process(data);
|
7631 | const r2 = this.aes.AES_Decrypt_finish();
|
7632 | return joinBytes(r1, r2);
|
7633 | }
|
7634 | }
|
7635 |
|
7636 |
|
7637 | function aes(length) {
|
7638 | const C = function(key) {
|
7639 | const aes_ecb = new AES_ECB(key);
|
7640 |
|
7641 | this.encrypt = function(block) {
|
7642 | return aes_ecb.encrypt(block);
|
7643 | };
|
7644 |
|
7645 | this.decrypt = function(block) {
|
7646 | return aes_ecb.decrypt(block);
|
7647 | };
|
7648 | };
|
7649 |
|
7650 | C.blockSize = C.prototype.blockSize = 16;
|
7651 | C.keySize = C.prototype.keySize = length / 8;
|
7652 |
|
7653 | return C;
|
7654 | }
|
7655 |
|
7656 |
|
7657 |
|
7658 |
|
7659 |
|
7660 |
|
7661 |
|
7662 |
|
7663 |
|
7664 |
|
7665 |
|
7666 |
|
7667 |
|
7668 |
|
7669 |
|
7670 |
|
7671 |
|
7672 |
|
7673 |
|
7674 |
|
7675 |
|
7676 |
|
7677 |
|
7678 |
|
7679 | function des(keys, message, encrypt, mode, iv, padding) {
|
7680 |
|
7681 | const spfunction1 = [
|
7682 | 0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400,
|
7683 | 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000,
|
7684 | 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4,
|
7685 | 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404,
|
7686 | 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400,
|
7687 | 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004
|
7688 | ];
|
7689 | const spfunction2 = [
|
7690 | -0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20, -0x7fefffe0, -0x7fff7fe0,
|
7691 | -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000, -0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020,
|
7692 | -0x7fff7fe0, 0, -0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000, 0x8020, -0x7fef8000,
|
7693 | -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000, -0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000,
|
7694 | -0x7fff8000, 0x20, -0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000, -0x7fffffe0,
|
7695 | 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000, 0x8020, -0x80000000, -0x7fefffe0,
|
7696 | -0x7fef7fe0, 0x108000
|
7697 | ];
|
7698 | const spfunction3 = [
|
7699 | 0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008,
|
7700 | 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000,
|
7701 | 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000,
|
7702 | 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0,
|
7703 | 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208,
|
7704 | 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200
|
7705 | ];
|
7706 | const spfunction4 = [
|
7707 | 0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000,
|
7708 | 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080,
|
7709 | 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0,
|
7710 | 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001,
|
7711 | 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080
|
7712 | ];
|
7713 | const spfunction5 = [
|
7714 | 0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000,
|
7715 | 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000,
|
7716 | 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100,
|
7717 | 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100,
|
7718 | 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100,
|
7719 | 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0,
|
7720 | 0x40080000, 0x2080100, 0x40000100
|
7721 | ];
|
7722 | const spfunction6 = [
|
7723 | 0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000,
|
7724 | 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010,
|
7725 | 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000,
|
7726 | 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000,
|
7727 | 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000,
|
7728 | 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010
|
7729 | ];
|
7730 | const spfunction7 = [
|
7731 | 0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802,
|
7732 | 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002,
|
7733 | 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000,
|
7734 | 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000,
|
7735 | 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0,
|
7736 | 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002
|
7737 | ];
|
7738 | const spfunction8 = [
|
7739 | 0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000,
|
7740 | 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000,
|
7741 | 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040,
|
7742 | 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040,
|
7743 | 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000,
|
7744 | 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000
|
7745 | ];
|
7746 |
|
7747 |
|
7748 | let m = 0;
|
7749 | let i;
|
7750 | let j;
|
7751 | let temp;
|
7752 | let right1;
|
7753 | let right2;
|
7754 | let left;
|
7755 | let right;
|
7756 | let looping;
|
7757 | let cbcleft;
|
7758 | let cbcleft2;
|
7759 | let cbcright;
|
7760 | let cbcright2;
|
7761 | let endloop;
|
7762 | let loopinc;
|
7763 | let len = message.length;
|
7764 |
|
7765 |
|
7766 | const iterations = keys.length === 32 ? 3 : 9;
|
7767 | if (iterations === 3) {
|
7768 | looping = encrypt ? [0, 32, 2] : [30, -2, -2];
|
7769 | } else {
|
7770 | looping = encrypt ? [0, 32, 2, 62, 30, -2, 64, 96, 2] : [94, 62, -2, 32, 64, 2, 30, -2, -2];
|
7771 | }
|
7772 |
|
7773 |
|
7774 |
|
7775 | if (encrypt) {
|
7776 | message = des_addPadding(message, padding);
|
7777 | len = message.length;
|
7778 | }
|
7779 |
|
7780 |
|
7781 | let result = new Uint8Array(len);
|
7782 | let k = 0;
|
7783 |
|
7784 | if (mode === 1) {
|
7785 | cbcleft = (iv[m++] << 24) | (iv[m++] << 16) | (iv[m++] << 8) | iv[m++];
|
7786 | cbcright = (iv[m++] << 24) | (iv[m++] << 16) | (iv[m++] << 8) | iv[m++];
|
7787 | m = 0;
|
7788 | }
|
7789 |
|
7790 |
|
7791 | while (m < len) {
|
7792 | left = (message[m++] << 24) | (message[m++] << 16) | (message[m++] << 8) | message[m++];
|
7793 | right = (message[m++] << 24) | (message[m++] << 16) | (message[m++] << 8) | message[m++];
|
7794 |
|
7795 |
|
7796 | if (mode === 1) {
|
7797 | if (encrypt) {
|
7798 | left ^= cbcleft;
|
7799 | right ^= cbcright;
|
7800 | } else {
|
7801 | cbcleft2 = cbcleft;
|
7802 | cbcright2 = cbcright;
|
7803 | cbcleft = left;
|
7804 | cbcright = right;
|
7805 | }
|
7806 | }
|
7807 |
|
7808 |
|
7809 | temp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
|
7810 | right ^= temp;
|
7811 | left ^= (temp << 4);
|
7812 | temp = ((left >>> 16) ^ right) & 0x0000ffff;
|
7813 | right ^= temp;
|
7814 | left ^= (temp << 16);
|
7815 | temp = ((right >>> 2) ^ left) & 0x33333333;
|
7816 | left ^= temp;
|
7817 | right ^= (temp << 2);
|
7818 | temp = ((right >>> 8) ^ left) & 0x00ff00ff;
|
7819 | left ^= temp;
|
7820 | right ^= (temp << 8);
|
7821 | temp = ((left >>> 1) ^ right) & 0x55555555;
|
7822 | right ^= temp;
|
7823 | left ^= (temp << 1);
|
7824 |
|
7825 | left = ((left << 1) | (left >>> 31));
|
7826 | right = ((right << 1) | (right >>> 31));
|
7827 |
|
7828 |
|
7829 | for (j = 0; j < iterations; j += 3) {
|
7830 | endloop = looping[j + 1];
|
7831 | loopinc = looping[j + 2];
|
7832 |
|
7833 | for (i = looping[j]; i !== endloop; i += loopinc) {
|
7834 | right1 = right ^ keys[i];
|
7835 | right2 = ((right >>> 4) | (right << 28)) ^ keys[i + 1];
|
7836 |
|
7837 | temp = left;
|
7838 | left = right;
|
7839 | right = temp ^ (spfunction2[(right1 >>> 24) & 0x3f] | spfunction4[(right1 >>> 16) & 0x3f] | spfunction6[(right1 >>>
|
7840 | 8) & 0x3f] | spfunction8[right1 & 0x3f] | spfunction1[(right2 >>> 24) & 0x3f] | spfunction3[(right2 >>> 16) &
|
7841 | 0x3f] | spfunction5[(right2 >>> 8) & 0x3f] | spfunction7[right2 & 0x3f]);
|
7842 | }
|
7843 | temp = left;
|
7844 | left = right;
|
7845 | right = temp;
|
7846 | }
|
7847 |
|
7848 |
|
7849 | left = ((left >>> 1) | (left << 31));
|
7850 | right = ((right >>> 1) | (right << 31));
|
7851 |
|
7852 |
|
7853 | temp = ((left >>> 1) ^ right) & 0x55555555;
|
7854 | right ^= temp;
|
7855 | left ^= (temp << 1);
|
7856 | temp = ((right >>> 8) ^ left) & 0x00ff00ff;
|
7857 | left ^= temp;
|
7858 | right ^= (temp << 8);
|
7859 | temp = ((right >>> 2) ^ left) & 0x33333333;
|
7860 | left ^= temp;
|
7861 | right ^= (temp << 2);
|
7862 | temp = ((left >>> 16) ^ right) & 0x0000ffff;
|
7863 | right ^= temp;
|
7864 | left ^= (temp << 16);
|
7865 | temp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
|
7866 | right ^= temp;
|
7867 | left ^= (temp << 4);
|
7868 |
|
7869 |
|
7870 | if (mode === 1) {
|
7871 | if (encrypt) {
|
7872 | cbcleft = left;
|
7873 | cbcright = right;
|
7874 | } else {
|
7875 | left ^= cbcleft2;
|
7876 | right ^= cbcright2;
|
7877 | }
|
7878 | }
|
7879 |
|
7880 | result[k++] = (left >>> 24);
|
7881 | result[k++] = ((left >>> 16) & 0xff);
|
7882 | result[k++] = ((left >>> 8) & 0xff);
|
7883 | result[k++] = (left & 0xff);
|
7884 | result[k++] = (right >>> 24);
|
7885 | result[k++] = ((right >>> 16) & 0xff);
|
7886 | result[k++] = ((right >>> 8) & 0xff);
|
7887 | result[k++] = (right & 0xff);
|
7888 | }
|
7889 |
|
7890 |
|
7891 | if (!encrypt) {
|
7892 | result = des_removePadding(result, padding);
|
7893 | }
|
7894 |
|
7895 | return result;
|
7896 | }
|
7897 |
|
7898 |
|
7899 |
|
7900 |
|
7901 |
|
7902 |
|
7903 | function des_createKeys(key) {
|
7904 |
|
7905 | const pc2bytes0 = [
|
7906 | 0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204,
|
7907 | 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204
|
7908 | ];
|
7909 | const pc2bytes1 = [
|
7910 | 0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100,
|
7911 | 0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101
|
7912 | ];
|
7913 | const pc2bytes2 = [
|
7914 | 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808,
|
7915 | 0x1000000, 0x1000008, 0x1000800, 0x1000808
|
7916 | ];
|
7917 | const pc2bytes3 = [
|
7918 | 0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000,
|
7919 | 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000
|
7920 | ];
|
7921 | const pc2bytes4 = [
|
7922 | 0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000,
|
7923 | 0x41000, 0x1010, 0x41010
|
7924 | ];
|
7925 | const pc2bytes5 = [
|
7926 | 0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420,
|
7927 | 0x2000000, 0x2000400, 0x2000020, 0x2000420
|
7928 | ];
|
7929 | const pc2bytes6 = [
|
7930 | 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000,
|
7931 | 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002
|
7932 | ];
|
7933 | const pc2bytes7 = [
|
7934 | 0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000,
|
7935 | 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800
|
7936 | ];
|
7937 | const pc2bytes8 = [
|
7938 | 0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000,
|
7939 | 0x2000002, 0x2040002, 0x2000002, 0x2040002
|
7940 | ];
|
7941 | const pc2bytes9 = [
|
7942 | 0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408,
|
7943 | 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408
|
7944 | ];
|
7945 | const pc2bytes10 = [
|
7946 | 0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020,
|
7947 | 0x102000, 0x102020, 0x102000, 0x102020
|
7948 | ];
|
7949 | const pc2bytes11 = [
|
7950 | 0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000,
|
7951 | 0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200
|
7952 | ];
|
7953 | const pc2bytes12 = [
|
7954 | 0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010,
|
7955 | 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010
|
7956 | ];
|
7957 | const pc2bytes13 = [0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105];
|
7958 |
|
7959 |
|
7960 | const iterations = key.length > 8 ? 3 : 1;
|
7961 |
|
7962 | const keys = new Array(32 * iterations);
|
7963 |
|
7964 | const shifts = [0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0];
|
7965 |
|
7966 | let lefttemp;
|
7967 | let righttemp;
|
7968 | let m = 0;
|
7969 | let n = 0;
|
7970 | let temp;
|
7971 |
|
7972 | for (let j = 0; j < iterations; j++) {
|
7973 | let left = (key[m++] << 24) | (key[m++] << 16) | (key[m++] << 8) | key[m++];
|
7974 | let right = (key[m++] << 24) | (key[m++] << 16) | (key[m++] << 8) | key[m++];
|
7975 |
|
7976 | temp = ((left >>> 4) ^ right) & 0x0f0f0f0f;
|
7977 | right ^= temp;
|
7978 | left ^= (temp << 4);
|
7979 | temp = ((right >>> -16) ^ left) & 0x0000ffff;
|
7980 | left ^= temp;
|
7981 | right ^= (temp << -16);
|
7982 | temp = ((left >>> 2) ^ right) & 0x33333333;
|
7983 | right ^= temp;
|
7984 | left ^= (temp << 2);
|
7985 | temp = ((right >>> -16) ^ left) & 0x0000ffff;
|
7986 | left ^= temp;
|
7987 | right ^= (temp << -16);
|
7988 | temp = ((left >>> 1) ^ right) & 0x55555555;
|
7989 | right ^= temp;
|
7990 | left ^= (temp << 1);
|
7991 | temp = ((right >>> 8) ^ left) & 0x00ff00ff;
|
7992 | left ^= temp;
|
7993 | right ^= (temp << 8);
|
7994 | temp = ((left >>> 1) ^ right) & 0x55555555;
|
7995 | right ^= temp;
|
7996 | left ^= (temp << 1);
|
7997 |
|
7998 |
|
7999 | temp = (left << 8) | ((right >>> 20) & 0x000000f0);
|
8000 |
|
8001 | left = (right << 24) | ((right << 8) & 0xff0000) | ((right >>> 8) & 0xff00) | ((right >>> 24) & 0xf0);
|
8002 | right = temp;
|
8003 |
|
8004 |
|
8005 | for (let i = 0; i < shifts.length; i++) {
|
8006 |
|
8007 | if (shifts[i]) {
|
8008 | left = (left << 2) | (left >>> 26);
|
8009 | right = (right << 2) | (right >>> 26);
|
8010 | } else {
|
8011 | left = (left << 1) | (left >>> 27);
|
8012 | right = (right << 1) | (right >>> 27);
|
8013 | }
|
8014 | left &= -0xf;
|
8015 | right &= -0xf;
|
8016 |
|
8017 |
|
8018 |
|
8019 |
|
8020 |
|
8021 | lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[(left >>> 24) & 0xf] | pc2bytes2[(left >>> 20) & 0xf] | pc2bytes3[(
|
8022 | left >>> 16) & 0xf] | pc2bytes4[(left >>> 12) & 0xf] | pc2bytes5[(left >>> 8) & 0xf] | pc2bytes6[(left >>> 4) &
|
8023 | 0xf];
|
8024 | righttemp = pc2bytes7[right >>> 28] | pc2bytes8[(right >>> 24) & 0xf] | pc2bytes9[(right >>> 20) & 0xf] |
|
8025 | pc2bytes10[(right >>> 16) & 0xf] | pc2bytes11[(right >>> 12) & 0xf] | pc2bytes12[(right >>> 8) & 0xf] |
|
8026 | pc2bytes13[(right >>> 4) & 0xf];
|
8027 | temp = ((righttemp >>> 16) ^ lefttemp) & 0x0000ffff;
|
8028 | keys[n++] = lefttemp ^ temp;
|
8029 | keys[n++] = righttemp ^ (temp << 16);
|
8030 | }
|
8031 | }
|
8032 |
|
8033 | return keys;
|
8034 | }
|
8035 |
|
8036 |
|
8037 | function des_addPadding(message, padding) {
|
8038 | const padLength = 8 - (message.length % 8);
|
8039 |
|
8040 | let pad;
|
8041 | if (padding === 2 && (padLength < 8)) {
|
8042 | pad = " ".charCodeAt(0);
|
8043 | } else if (padding === 1) {
|
8044 | pad = padLength;
|
8045 | } else if (!padding && (padLength < 8)) {
|
8046 | pad = 0;
|
8047 | } else if (padLength === 8) {
|
8048 | return message;
|
8049 | } else {
|
8050 | throw new Error('des: invalid padding');
|
8051 | }
|
8052 |
|
8053 | const paddedMessage = new Uint8Array(message.length + padLength);
|
8054 | for (let i = 0; i < message.length; i++) {
|
8055 | paddedMessage[i] = message[i];
|
8056 | }
|
8057 | for (let j = 0; j < padLength; j++) {
|
8058 | paddedMessage[message.length + j] = pad;
|
8059 | }
|
8060 |
|
8061 | return paddedMessage;
|
8062 | }
|
8063 |
|
8064 | function des_removePadding(message, padding) {
|
8065 | let padLength = null;
|
8066 | let pad;
|
8067 | if (padding === 2) {
|
8068 | pad = " ".charCodeAt(0);
|
8069 | } else if (padding === 1) {
|
8070 | padLength = message[message.length - 1];
|
8071 | } else if (!padding) {
|
8072 | pad = 0;
|
8073 | } else {
|
8074 | throw new Error('des: invalid padding');
|
8075 | }
|
8076 |
|
8077 | if (!padLength) {
|
8078 | padLength = 1;
|
8079 | while (message[message.length - padLength] === pad) {
|
8080 | padLength++;
|
8081 | }
|
8082 | padLength--;
|
8083 | }
|
8084 |
|
8085 | return message.subarray(0, message.length - padLength);
|
8086 | }
|
8087 |
|
8088 |
|
8089 |
|
8090 | function TripleDES(key) {
|
8091 | this.key = [];
|
8092 |
|
8093 | for (let i = 0; i < 3; i++) {
|
8094 | this.key.push(new Uint8Array(key.subarray(i * 8, (i * 8) + 8)));
|
8095 | }
|
8096 |
|
8097 | this.encrypt = function(block) {
|
8098 | return des(
|
8099 | des_createKeys(this.key[2]),
|
8100 | des(
|
8101 | des_createKeys(this.key[1]),
|
8102 | des(
|
8103 | des_createKeys(this.key[0]),
|
8104 | block, true, 0, null, null
|
8105 | ),
|
8106 | false, 0, null, null
|
8107 | ), true, 0, null, null
|
8108 | );
|
8109 | };
|
8110 | }
|
8111 |
|
8112 | TripleDES.keySize = TripleDES.prototype.keySize = 24;
|
8113 | TripleDES.blockSize = TripleDES.prototype.blockSize = 8;
|
8114 |
|
8115 |
|
8116 |
|
8117 | function DES(key) {
|
8118 | this.key = key;
|
8119 |
|
8120 | this.encrypt = function(block, padding) {
|
8121 | const keys = des_createKeys(this.key);
|
8122 | return des(keys, block, true, 0, null, padding);
|
8123 | };
|
8124 |
|
8125 | this.decrypt = function(block, padding) {
|
8126 | const keys = des_createKeys(this.key);
|
8127 | return des(keys, block, false, 0, null, padding);
|
8128 | };
|
8129 | }
|
8130 |
|
8131 |
|
8132 |
|
8133 |
|
8134 |
|
8135 |
|
8136 |
|
8137 |
|
8138 |
|
8139 |
|
8140 |
|
8141 |
|
8142 |
|
8143 |
|
8144 |
|
8145 |
|
8146 |
|
8147 | function OpenpgpSymencCast5() {
|
8148 | this.BlockSize = 8;
|
8149 | this.KeySize = 16;
|
8150 |
|
8151 | this.setKey = function(key) {
|
8152 | this.masking = new Array(16);
|
8153 | this.rotate = new Array(16);
|
8154 |
|
8155 | this.reset();
|
8156 |
|
8157 | if (key.length === this.KeySize) {
|
8158 | this.keySchedule(key);
|
8159 | } else {
|
8160 | throw new Error('CAST-128: keys must be 16 bytes');
|
8161 | }
|
8162 | return true;
|
8163 | };
|
8164 |
|
8165 | this.reset = function() {
|
8166 | for (let i = 0; i < 16; i++) {
|
8167 | this.masking[i] = 0;
|
8168 | this.rotate[i] = 0;
|
8169 | }
|
8170 | };
|
8171 |
|
8172 | this.getBlockSize = function() {
|
8173 | return this.BlockSize;
|
8174 | };
|
8175 |
|
8176 | this.encrypt = function(src) {
|
8177 | const dst = new Array(src.length);
|
8178 |
|
8179 | for (let i = 0; i < src.length; i += 8) {
|
8180 | let l = (src[i] << 24) | (src[i + 1] << 16) | (src[i + 2] << 8) | src[i + 3];
|
8181 | let r = (src[i + 4] << 24) | (src[i + 5] << 16) | (src[i + 6] << 8) | src[i + 7];
|
8182 | let t;
|
8183 |
|
8184 | t = r;
|
8185 | r = l ^ f1(r, this.masking[0], this.rotate[0]);
|
8186 | l = t;
|
8187 | t = r;
|
8188 | r = l ^ f2(r, this.masking[1], this.rotate[1]);
|
8189 | l = t;
|
8190 | t = r;
|
8191 | r = l ^ f3(r, this.masking[2], this.rotate[2]);
|
8192 | l = t;
|
8193 | t = r;
|
8194 | r = l ^ f1(r, this.masking[3], this.rotate[3]);
|
8195 | l = t;
|
8196 |
|
8197 | t = r;
|
8198 | r = l ^ f2(r, this.masking[4], this.rotate[4]);
|
8199 | l = t;
|
8200 | t = r;
|
8201 | r = l ^ f3(r, this.masking[5], this.rotate[5]);
|
8202 | l = t;
|
8203 | t = r;
|
8204 | r = l ^ f1(r, this.masking[6], this.rotate[6]);
|
8205 | l = t;
|
8206 | t = r;
|
8207 | r = l ^ f2(r, this.masking[7], this.rotate[7]);
|
8208 | l = t;
|
8209 |
|
8210 | t = r;
|
8211 | r = l ^ f3(r, this.masking[8], this.rotate[8]);
|
8212 | l = t;
|
8213 | t = r;
|
8214 | r = l ^ f1(r, this.masking[9], this.rotate[9]);
|
8215 | l = t;
|
8216 | t = r;
|
8217 | r = l ^ f2(r, this.masking[10], this.rotate[10]);
|
8218 | l = t;
|
8219 | t = r;
|
8220 | r = l ^ f3(r, this.masking[11], this.rotate[11]);
|
8221 | l = t;
|
8222 |
|
8223 | t = r;
|
8224 | r = l ^ f1(r, this.masking[12], this.rotate[12]);
|
8225 | l = t;
|
8226 | t = r;
|
8227 | r = l ^ f2(r, this.masking[13], this.rotate[13]);
|
8228 | l = t;
|
8229 | t = r;
|
8230 | r = l ^ f3(r, this.masking[14], this.rotate[14]);
|
8231 | l = t;
|
8232 | t = r;
|
8233 | r = l ^ f1(r, this.masking[15], this.rotate[15]);
|
8234 | l = t;
|
8235 |
|
8236 | dst[i] = (r >>> 24) & 255;
|
8237 | dst[i + 1] = (r >>> 16) & 255;
|
8238 | dst[i + 2] = (r >>> 8) & 255;
|
8239 | dst[i + 3] = r & 255;
|
8240 | dst[i + 4] = (l >>> 24) & 255;
|
8241 | dst[i + 5] = (l >>> 16) & 255;
|
8242 | dst[i + 6] = (l >>> 8) & 255;
|
8243 | dst[i + 7] = l & 255;
|
8244 | }
|
8245 |
|
8246 | return dst;
|
8247 | };
|
8248 |
|
8249 | this.decrypt = function(src) {
|
8250 | const dst = new Array(src.length);
|
8251 |
|
8252 | for (let i = 0; i < src.length; i += 8) {
|
8253 | let l = (src[i] << 24) | (src[i + 1] << 16) | (src[i + 2] << 8) | src[i + 3];
|
8254 | let r = (src[i + 4] << 24) | (src[i + 5] << 16) | (src[i + 6] << 8) | src[i + 7];
|
8255 | let t;
|
8256 |
|
8257 | t = r;
|
8258 | r = l ^ f1(r, this.masking[15], this.rotate[15]);
|
8259 | l = t;
|
8260 | t = r;
|
8261 | r = l ^ f3(r, this.masking[14], this.rotate[14]);
|
8262 | l = t;
|
8263 | t = r;
|
8264 | r = l ^ f2(r, this.masking[13], this.rotate[13]);
|
8265 | l = t;
|
8266 | t = r;
|
8267 | r = l ^ f1(r, this.masking[12], this.rotate[12]);
|
8268 | l = t;
|
8269 |
|
8270 | t = r;
|
8271 | r = l ^ f3(r, this.masking[11], this.rotate[11]);
|
8272 | l = t;
|
8273 | t = r;
|
8274 | r = l ^ f2(r, this.masking[10], this.rotate[10]);
|
8275 | l = t;
|
8276 | t = r;
|
8277 | r = l ^ f1(r, this.masking[9], this.rotate[9]);
|
8278 | l = t;
|
8279 | t = r;
|
8280 | r = l ^ f3(r, this.masking[8], this.rotate[8]);
|
8281 | l = t;
|
8282 |
|
8283 | t = r;
|
8284 | r = l ^ f2(r, this.masking[7], this.rotate[7]);
|
8285 | l = t;
|
8286 | t = r;
|
8287 | r = l ^ f1(r, this.masking[6], this.rotate[6]);
|
8288 | l = t;
|
8289 | t = r;
|
8290 | r = l ^ f3(r, this.masking[5], this.rotate[5]);
|
8291 | l = t;
|
8292 | t = r;
|
8293 | r = l ^ f2(r, this.masking[4], this.rotate[4]);
|
8294 | l = t;
|
8295 |
|
8296 | t = r;
|
8297 | r = l ^ f1(r, this.masking[3], this.rotate[3]);
|
8298 | l = t;
|
8299 | t = r;
|
8300 | r = l ^ f3(r, this.masking[2], this.rotate[2]);
|
8301 | l = t;
|
8302 | t = r;
|
8303 | r = l ^ f2(r, this.masking[1], this.rotate[1]);
|
8304 | l = t;
|
8305 | t = r;
|
8306 | r = l ^ f1(r, this.masking[0], this.rotate[0]);
|
8307 | l = t;
|
8308 |
|
8309 | dst[i] = (r >>> 24) & 255;
|
8310 | dst[i + 1] = (r >>> 16) & 255;
|
8311 | dst[i + 2] = (r >>> 8) & 255;
|
8312 | dst[i + 3] = r & 255;
|
8313 | dst[i + 4] = (l >>> 24) & 255;
|
8314 | dst[i + 5] = (l >> 16) & 255;
|
8315 | dst[i + 6] = (l >> 8) & 255;
|
8316 | dst[i + 7] = l & 255;
|
8317 | }
|
8318 |
|
8319 | return dst;
|
8320 | };
|
8321 | const scheduleA = new Array(4);
|
8322 |
|
8323 | scheduleA[0] = new Array(4);
|
8324 | scheduleA[0][0] = [4, 0, 0xd, 0xf, 0xc, 0xe, 0x8];
|
8325 | scheduleA[0][1] = [5, 2, 16 + 0, 16 + 2, 16 + 1, 16 + 3, 0xa];
|
8326 | scheduleA[0][2] = [6, 3, 16 + 7, 16 + 6, 16 + 5, 16 + 4, 9];
|
8327 | scheduleA[0][3] = [7, 1, 16 + 0xa, 16 + 9, 16 + 0xb, 16 + 8, 0xb];
|
8328 |
|
8329 | scheduleA[1] = new Array(4);
|
8330 | scheduleA[1][0] = [0, 6, 16 + 5, 16 + 7, 16 + 4, 16 + 6, 16 + 0];
|
8331 | scheduleA[1][1] = [1, 4, 0, 2, 1, 3, 16 + 2];
|
8332 | scheduleA[1][2] = [2, 5, 7, 6, 5, 4, 16 + 1];
|
8333 | scheduleA[1][3] = [3, 7, 0xa, 9, 0xb, 8, 16 + 3];
|
8334 |
|
8335 | scheduleA[2] = new Array(4);
|
8336 | scheduleA[2][0] = [4, 0, 0xd, 0xf, 0xc, 0xe, 8];
|
8337 | scheduleA[2][1] = [5, 2, 16 + 0, 16 + 2, 16 + 1, 16 + 3, 0xa];
|
8338 | scheduleA[2][2] = [6, 3, 16 + 7, 16 + 6, 16 + 5, 16 + 4, 9];
|
8339 | scheduleA[2][3] = [7, 1, 16 + 0xa, 16 + 9, 16 + 0xb, 16 + 8, 0xb];
|
8340 |
|
8341 |
|
8342 | scheduleA[3] = new Array(4);
|
8343 | scheduleA[3][0] = [0, 6, 16 + 5, 16 + 7, 16 + 4, 16 + 6, 16 + 0];
|
8344 | scheduleA[3][1] = [1, 4, 0, 2, 1, 3, 16 + 2];
|
8345 | scheduleA[3][2] = [2, 5, 7, 6, 5, 4, 16 + 1];
|
8346 | scheduleA[3][3] = [3, 7, 0xa, 9, 0xb, 8, 16 + 3];
|
8347 |
|
8348 | const scheduleB = new Array(4);
|
8349 |
|
8350 | scheduleB[0] = new Array(4);
|
8351 | scheduleB[0][0] = [16 + 8, 16 + 9, 16 + 7, 16 + 6, 16 + 2];
|
8352 | scheduleB[0][1] = [16 + 0xa, 16 + 0xb, 16 + 5, 16 + 4, 16 + 6];
|
8353 | scheduleB[0][2] = [16 + 0xc, 16 + 0xd, 16 + 3, 16 + 2, 16 + 9];
|
8354 | scheduleB[0][3] = [16 + 0xe, 16 + 0xf, 16 + 1, 16 + 0, 16 + 0xc];
|
8355 |
|
8356 | scheduleB[1] = new Array(4);
|
8357 | scheduleB[1][0] = [3, 2, 0xc, 0xd, 8];
|
8358 | scheduleB[1][1] = [1, 0, 0xe, 0xf, 0xd];
|
8359 | scheduleB[1][2] = [7, 6, 8, 9, 3];
|
8360 | scheduleB[1][3] = [5, 4, 0xa, 0xb, 7];
|
8361 |
|
8362 |
|
8363 | scheduleB[2] = new Array(4);
|
8364 | scheduleB[2][0] = [16 + 3, 16 + 2, 16 + 0xc, 16 + 0xd, 16 + 9];
|
8365 | scheduleB[2][1] = [16 + 1, 16 + 0, 16 + 0xe, 16 + 0xf, 16 + 0xc];
|
8366 | scheduleB[2][2] = [16 + 7, 16 + 6, 16 + 8, 16 + 9, 16 + 2];
|
8367 | scheduleB[2][3] = [16 + 5, 16 + 4, 16 + 0xa, 16 + 0xb, 16 + 6];
|
8368 |
|
8369 |
|
8370 | scheduleB[3] = new Array(4);
|
8371 | scheduleB[3][0] = [8, 9, 7, 6, 3];
|
8372 | scheduleB[3][1] = [0xa, 0xb, 5, 4, 7];
|
8373 | scheduleB[3][2] = [0xc, 0xd, 3, 2, 8];
|
8374 | scheduleB[3][3] = [0xe, 0xf, 1, 0, 0xd];
|
8375 |
|
8376 |
|
8377 | this.keySchedule = function(inn) {
|
8378 | const t = new Array(8);
|
8379 | const k = new Array(32);
|
8380 |
|
8381 | let j;
|
8382 |
|
8383 | for (let i = 0; i < 4; i++) {
|
8384 | j = i * 4;
|
8385 | t[i] = (inn[j] << 24) | (inn[j + 1] << 16) | (inn[j + 2] << 8) | inn[j + 3];
|
8386 | }
|
8387 |
|
8388 | const x = [6, 7, 4, 5];
|
8389 | let ki = 0;
|
8390 | let w;
|
8391 |
|
8392 | for (let half = 0; half < 2; half++) {
|
8393 | for (let round = 0; round < 4; round++) {
|
8394 | for (j = 0; j < 4; j++) {
|
8395 | const a = scheduleA[round][j];
|
8396 | w = t[a[1]];
|
8397 |
|
8398 | w ^= sBox[4][(t[a[2] >>> 2] >>> (24 - 8 * (a[2] & 3))) & 0xff];
|
8399 | w ^= sBox[5][(t[a[3] >>> 2] >>> (24 - 8 * (a[3] & 3))) & 0xff];
|
8400 | w ^= sBox[6][(t[a[4] >>> 2] >>> (24 - 8 * (a[4] & 3))) & 0xff];
|
8401 | w ^= sBox[7][(t[a[5] >>> 2] >>> (24 - 8 * (a[5] & 3))) & 0xff];
|
8402 | w ^= sBox[x[j]][(t[a[6] >>> 2] >>> (24 - 8 * (a[6] & 3))) & 0xff];
|
8403 | t[a[0]] = w;
|
8404 | }
|
8405 |
|
8406 | for (j = 0; j < 4; j++) {
|
8407 | const b = scheduleB[round][j];
|
8408 | w = sBox[4][(t[b[0] >>> 2] >>> (24 - 8 * (b[0] & 3))) & 0xff];
|
8409 |
|
8410 | w ^= sBox[5][(t[b[1] >>> 2] >>> (24 - 8 * (b[1] & 3))) & 0xff];
|
8411 | w ^= sBox[6][(t[b[2] >>> 2] >>> (24 - 8 * (b[2] & 3))) & 0xff];
|
8412 | w ^= sBox[7][(t[b[3] >>> 2] >>> (24 - 8 * (b[3] & 3))) & 0xff];
|
8413 | w ^= sBox[4 + j][(t[b[4] >>> 2] >>> (24 - 8 * (b[4] & 3))) & 0xff];
|
8414 | k[ki] = w;
|
8415 | ki++;
|
8416 | }
|
8417 | }
|
8418 | }
|
8419 |
|
8420 | for (let i = 0; i < 16; i++) {
|
8421 | this.masking[i] = k[i];
|
8422 | this.rotate[i] = k[16 + i] & 0x1f;
|
8423 | }
|
8424 | };
|
8425 |
|
8426 |
|
8427 |
|
8428 | function f1(d, m, r) {
|
8429 | const t = m + d;
|
8430 | const I = (t << r) | (t >>> (32 - r));
|
8431 | return ((sBox[0][I >>> 24] ^ sBox[1][(I >>> 16) & 255]) - sBox[2][(I >>> 8) & 255]) + sBox[3][I & 255];
|
8432 | }
|
8433 |
|
8434 | function f2(d, m, r) {
|
8435 | const t = m ^ d;
|
8436 | const I = (t << r) | (t >>> (32 - r));
|
8437 | return ((sBox[0][I >>> 24] - sBox[1][(I >>> 16) & 255]) + sBox[2][(I >>> 8) & 255]) ^ sBox[3][I & 255];
|
8438 | }
|
8439 |
|
8440 | function f3(d, m, r) {
|
8441 | const t = m - d;
|
8442 | const I = (t << r) | (t >>> (32 - r));
|
8443 | return ((sBox[0][I >>> 24] + sBox[1][(I >>> 16) & 255]) ^ sBox[2][(I >>> 8) & 255]) - sBox[3][I & 255];
|
8444 | }
|
8445 |
|
8446 | const sBox = new Array(8);
|
8447 | sBox[0] = [
|
8448 | 0x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f, 0x9c004dd3, 0x6003e540, 0xcf9fc949,
|
8449 | 0xbfd4af27, 0x88bbbdb5, 0xe2034090, 0x98d09675, 0x6e63a0e0, 0x15c361d2, 0xc2e7661d, 0x22d4ff8e,
|
8450 | 0x28683b6f, 0xc07fd059, 0xff2379c8, 0x775f50e2, 0x43c340d3, 0xdf2f8656, 0x887ca41a, 0xa2d2bd2d,
|
8451 | 0xa1c9e0d6, 0x346c4819, 0x61b76d87, 0x22540f2f, 0x2abe32e1, 0xaa54166b, 0x22568e3a, 0xa2d341d0,
|
8452 | 0x66db40c8, 0xa784392f, 0x004dff2f, 0x2db9d2de, 0x97943fac, 0x4a97c1d8, 0x527644b7, 0xb5f437a7,
|
8453 | 0xb82cbaef, 0xd751d159, 0x6ff7f0ed, 0x5a097a1f, 0x827b68d0, 0x90ecf52e, 0x22b0c054, 0xbc8e5935,
|
8454 | 0x4b6d2f7f, 0x50bb64a2, 0xd2664910, 0xbee5812d, 0xb7332290, 0xe93b159f, 0xb48ee411, 0x4bff345d,
|
8455 | 0xfd45c240, 0xad31973f, 0xc4f6d02e, 0x55fc8165, 0xd5b1caad, 0xa1ac2dae, 0xa2d4b76d, 0xc19b0c50,
|
8456 | 0x882240f2, 0x0c6e4f38, 0xa4e4bfd7, 0x4f5ba272, 0x564c1d2f, 0xc59c5319, 0xb949e354, 0xb04669fe,
|
8457 | 0xb1b6ab8a, 0xc71358dd, 0x6385c545, 0x110f935d, 0x57538ad5, 0x6a390493, 0xe63d37e0, 0x2a54f6b3,
|
8458 | 0x3a787d5f, 0x6276a0b5, 0x19a6fcdf, 0x7a42206a, 0x29f9d4d5, 0xf61b1891, 0xbb72275e, 0xaa508167,
|
8459 | 0x38901091, 0xc6b505eb, 0x84c7cb8c, 0x2ad75a0f, 0x874a1427, 0xa2d1936b, 0x2ad286af, 0xaa56d291,
|
8460 | 0xd7894360, 0x425c750d, 0x93b39e26, 0x187184c9, 0x6c00b32d, 0x73e2bb14, 0xa0bebc3c, 0x54623779,
|
8461 | 0x64459eab, 0x3f328b82, 0x7718cf82, 0x59a2cea6, 0x04ee002e, 0x89fe78e6, 0x3fab0950, 0x325ff6c2,
|
8462 | 0x81383f05, 0x6963c5c8, 0x76cb5ad6, 0xd49974c9, 0xca180dcf, 0x380782d5, 0xc7fa5cf6, 0x8ac31511,
|
8463 | 0x35e79e13, 0x47da91d0, 0xf40f9086, 0xa7e2419e, 0x31366241, 0x051ef495, 0xaa573b04, 0x4a805d8d,
|
8464 | 0x548300d0, 0x00322a3c, 0xbf64cddf, 0xba57a68e, 0x75c6372b, 0x50afd341, 0xa7c13275, 0x915a0bf5,
|
8465 | 0x6b54bfab, 0x2b0b1426, 0xab4cc9d7, 0x449ccd82, 0xf7fbf265, 0xab85c5f3, 0x1b55db94, 0xaad4e324,
|
8466 | 0xcfa4bd3f, 0x2deaa3e2, 0x9e204d02, 0xc8bd25ac, 0xeadf55b3, 0xd5bd9e98, 0xe31231b2, 0x2ad5ad6c,
|
8467 | 0x954329de, 0xadbe4528, 0xd8710f69, 0xaa51c90f, 0xaa786bf6, 0x22513f1e, 0xaa51a79b, 0x2ad344cc,
|
8468 | 0x7b5a41f0, 0xd37cfbad, 0x1b069505, 0x41ece491, 0xb4c332e6, 0x032268d4, 0xc9600acc, 0xce387e6d,
|
8469 | 0xbf6bb16c, 0x6a70fb78, 0x0d03d9c9, 0xd4df39de, 0xe01063da, 0x4736f464, 0x5ad328d8, 0xb347cc96,
|
8470 | 0x75bb0fc3, 0x98511bfb, 0x4ffbcc35, 0xb58bcf6a, 0xe11f0abc, 0xbfc5fe4a, 0xa70aec10, 0xac39570a,
|
8471 | 0x3f04442f, 0x6188b153, 0xe0397a2e, 0x5727cb79, 0x9ceb418f, 0x1cacd68d, 0x2ad37c96, 0x0175cb9d,
|
8472 | 0xc69dff09, 0xc75b65f0, 0xd9db40d8, 0xec0e7779, 0x4744ead4, 0xb11c3274, 0xdd24cb9e, 0x7e1c54bd,
|
8473 | 0xf01144f9, 0xd2240eb1, 0x9675b3fd, 0xa3ac3755, 0xd47c27af, 0x51c85f4d, 0x56907596, 0xa5bb15e6,
|
8474 | 0x580304f0, 0xca042cf1, 0x011a37ea, 0x8dbfaadb, 0x35ba3e4a, 0x3526ffa0, 0xc37b4d09, 0xbc306ed9,
|
8475 | 0x98a52666, 0x5648f725, 0xff5e569d, 0x0ced63d0, 0x7c63b2cf, 0x700b45e1, 0xd5ea50f1, 0x85a92872,
|
8476 | 0xaf1fbda7, 0xd4234870, 0xa7870bf3, 0x2d3b4d79, 0x42e04198, 0x0cd0ede7, 0x26470db8, 0xf881814c,
|
8477 | 0x474d6ad7, 0x7c0c5e5c, 0xd1231959, 0x381b7298, 0xf5d2f4db, 0xab838653, 0x6e2f1e23, 0x83719c9e,
|
8478 | 0xbd91e046, 0x9a56456e, 0xdc39200c, 0x20c8c571, 0x962bda1c, 0xe1e696ff, 0xb141ab08, 0x7cca89b9,
|
8479 | 0x1a69e783, 0x02cc4843, 0xa2f7c579, 0x429ef47d, 0x427b169c, 0x5ac9f049, 0xdd8f0f00, 0x5c8165bf
|
8480 | ];
|
8481 |
|
8482 | sBox[1] = [
|
8483 | 0x1f201094, 0xef0ba75b, 0x69e3cf7e, 0x393f4380, 0xfe61cf7a, 0xeec5207a, 0x55889c94, 0x72fc0651,
|
8484 | 0xada7ef79, 0x4e1d7235, 0xd55a63ce, 0xde0436ba, 0x99c430ef, 0x5f0c0794, 0x18dcdb7d, 0xa1d6eff3,
|
8485 | 0xa0b52f7b, 0x59e83605, 0xee15b094, 0xe9ffd909, 0xdc440086, 0xef944459, 0xba83ccb3, 0xe0c3cdfb,
|
8486 | 0xd1da4181, 0x3b092ab1, 0xf997f1c1, 0xa5e6cf7b, 0x01420ddb, 0xe4e7ef5b, 0x25a1ff41, 0xe180f806,
|
8487 | 0x1fc41080, 0x179bee7a, 0xd37ac6a9, 0xfe5830a4, 0x98de8b7f, 0x77e83f4e, 0x79929269, 0x24fa9f7b,
|
8488 | 0xe113c85b, 0xacc40083, 0xd7503525, 0xf7ea615f, 0x62143154, 0x0d554b63, 0x5d681121, 0xc866c359,
|
8489 | 0x3d63cf73, 0xcee234c0, 0xd4d87e87, 0x5c672b21, 0x071f6181, 0x39f7627f, 0x361e3084, 0xe4eb573b,
|
8490 | 0x602f64a4, 0xd63acd9c, 0x1bbc4635, 0x9e81032d, 0x2701f50c, 0x99847ab4, 0xa0e3df79, 0xba6cf38c,
|
8491 | 0x10843094, 0x2537a95e, 0xf46f6ffe, 0xa1ff3b1f, 0x208cfb6a, 0x8f458c74, 0xd9e0a227, 0x4ec73a34,
|
8492 | 0xfc884f69, 0x3e4de8df, 0xef0e0088, 0x3559648d, 0x8a45388c, 0x1d804366, 0x721d9bfd, 0xa58684bb,
|
8493 | 0xe8256333, 0x844e8212, 0x128d8098, 0xfed33fb4, 0xce280ae1, 0x27e19ba5, 0xd5a6c252, 0xe49754bd,
|
8494 | 0xc5d655dd, 0xeb667064, 0x77840b4d, 0xa1b6a801, 0x84db26a9, 0xe0b56714, 0x21f043b7, 0xe5d05860,
|
8495 | 0x54f03084, 0x066ff472, 0xa31aa153, 0xdadc4755, 0xb5625dbf, 0x68561be6, 0x83ca6b94, 0x2d6ed23b,
|
8496 | 0xeccf01db, 0xa6d3d0ba, 0xb6803d5c, 0xaf77a709, 0x33b4a34c, 0x397bc8d6, 0x5ee22b95, 0x5f0e5304,
|
8497 | 0x81ed6f61, 0x20e74364, 0xb45e1378, 0xde18639b, 0x881ca122, 0xb96726d1, 0x8049a7e8, 0x22b7da7b,
|
8498 | 0x5e552d25, 0x5272d237, 0x79d2951c, 0xc60d894c, 0x488cb402, 0x1ba4fe5b, 0xa4b09f6b, 0x1ca815cf,
|
8499 | 0xa20c3005, 0x8871df63, 0xb9de2fcb, 0x0cc6c9e9, 0x0beeff53, 0xe3214517, 0xb4542835, 0x9f63293c,
|
8500 | 0xee41e729, 0x6e1d2d7c, 0x50045286, 0x1e6685f3, 0xf33401c6, 0x30a22c95, 0x31a70850, 0x60930f13,
|
8501 | 0x73f98417, 0xa1269859, 0xec645c44, 0x52c877a9, 0xcdff33a6, 0xa02b1741, 0x7cbad9a2, 0x2180036f,
|
8502 | 0x50d99c08, 0xcb3f4861, 0xc26bd765, 0x64a3f6ab, 0x80342676, 0x25a75e7b, 0xe4e6d1fc, 0x20c710e6,
|
8503 | 0xcdf0b680, 0x17844d3b, 0x31eef84d, 0x7e0824e4, 0x2ccb49eb, 0x846a3bae, 0x8ff77888, 0xee5d60f6,
|
8504 | 0x7af75673, 0x2fdd5cdb, 0xa11631c1, 0x30f66f43, 0xb3faec54, 0x157fd7fa, 0xef8579cc, 0xd152de58,
|
8505 | 0xdb2ffd5e, 0x8f32ce19, 0x306af97a, 0x02f03ef8, 0x99319ad5, 0xc242fa0f, 0xa7e3ebb0, 0xc68e4906,
|
8506 | 0xb8da230c, 0x80823028, 0xdcdef3c8, 0xd35fb171, 0x088a1bc8, 0xbec0c560, 0x61a3c9e8, 0xbca8f54d,
|
8507 | 0xc72feffa, 0x22822e99, 0x82c570b4, 0xd8d94e89, 0x8b1c34bc, 0x301e16e6, 0x273be979, 0xb0ffeaa6,
|
8508 | 0x61d9b8c6, 0x00b24869, 0xb7ffce3f, 0x08dc283b, 0x43daf65a, 0xf7e19798, 0x7619b72f, 0x8f1c9ba4,
|
8509 | 0xdc8637a0, 0x16a7d3b1, 0x9fc393b7, 0xa7136eeb, 0xc6bcc63e, 0x1a513742, 0xef6828bc, 0x520365d6,
|
8510 | 0x2d6a77ab, 0x3527ed4b, 0x821fd216, 0x095c6e2e, 0xdb92f2fb, 0x5eea29cb, 0x145892f5, 0x91584f7f,
|
8511 | 0x5483697b, 0x2667a8cc, 0x85196048, 0x8c4bacea, 0x833860d4, 0x0d23e0f9, 0x6c387e8a, 0x0ae6d249,
|
8512 | 0xb284600c, 0xd835731d, 0xdcb1c647, 0xac4c56ea, 0x3ebd81b3, 0x230eabb0, 0x6438bc87, 0xf0b5b1fa,
|
8513 | 0x8f5ea2b3, 0xfc184642, 0x0a036b7a, 0x4fb089bd, 0x649da589, 0xa345415e, 0x5c038323, 0x3e5d3bb9,
|
8514 | 0x43d79572, 0x7e6dd07c, 0x06dfdf1e, 0x6c6cc4ef, 0x7160a539, 0x73bfbe70, 0x83877605, 0x4523ecf1
|
8515 | ];
|
8516 |
|
8517 | sBox[2] = [
|
8518 | 0x8defc240, 0x25fa5d9f, 0xeb903dbf, 0xe810c907, 0x47607fff, 0x369fe44b, 0x8c1fc644, 0xaececa90,
|
8519 | 0xbeb1f9bf, 0xeefbcaea, 0xe8cf1950, 0x51df07ae, 0x920e8806, 0xf0ad0548, 0xe13c8d83, 0x927010d5,
|
8520 | 0x11107d9f, 0x07647db9, 0xb2e3e4d4, 0x3d4f285e, 0xb9afa820, 0xfade82e0, 0xa067268b, 0x8272792e,
|
8521 | 0x553fb2c0, 0x489ae22b, 0xd4ef9794, 0x125e3fbc, 0x21fffcee, 0x825b1bfd, 0x9255c5ed, 0x1257a240,
|
8522 | 0x4e1a8302, 0xbae07fff, 0x528246e7, 0x8e57140e, 0x3373f7bf, 0x8c9f8188, 0xa6fc4ee8, 0xc982b5a5,
|
8523 | 0xa8c01db7, 0x579fc264, 0x67094f31, 0xf2bd3f5f, 0x40fff7c1, 0x1fb78dfc, 0x8e6bd2c1, 0x437be59b,
|
8524 | 0x99b03dbf, 0xb5dbc64b, 0x638dc0e6, 0x55819d99, 0xa197c81c, 0x4a012d6e, 0xc5884a28, 0xccc36f71,
|
8525 | 0xb843c213, 0x6c0743f1, 0x8309893c, 0x0feddd5f, 0x2f7fe850, 0xd7c07f7e, 0x02507fbf, 0x5afb9a04,
|
8526 | 0xa747d2d0, 0x1651192e, 0xaf70bf3e, 0x58c31380, 0x5f98302e, 0x727cc3c4, 0x0a0fb402, 0x0f7fef82,
|
8527 | 0x8c96fdad, 0x5d2c2aae, 0x8ee99a49, 0x50da88b8, 0x8427f4a0, 0x1eac5790, 0x796fb449, 0x8252dc15,
|
8528 | 0xefbd7d9b, 0xa672597d, 0xada840d8, 0x45f54504, 0xfa5d7403, 0xe83ec305, 0x4f91751a, 0x925669c2,
|
8529 | 0x23efe941, 0xa903f12e, 0x60270df2, 0x0276e4b6, 0x94fd6574, 0x927985b2, 0x8276dbcb, 0x02778176,
|
8530 | 0xf8af918d, 0x4e48f79e, 0x8f616ddf, 0xe29d840e, 0x842f7d83, 0x340ce5c8, 0x96bbb682, 0x93b4b148,
|
8531 | 0xef303cab, 0x984faf28, 0x779faf9b, 0x92dc560d, 0x224d1e20, 0x8437aa88, 0x7d29dc96, 0x2756d3dc,
|
8532 | 0x8b907cee, 0xb51fd240, 0xe7c07ce3, 0xe566b4a1, 0xc3e9615e, 0x3cf8209d, 0x6094d1e3, 0xcd9ca341,
|
8533 | 0x5c76460e, 0x00ea983b, 0xd4d67881, 0xfd47572c, 0xf76cedd9, 0xbda8229c, 0x127dadaa, 0x438a074e,
|
8534 | 0x1f97c090, 0x081bdb8a, 0x93a07ebe, 0xb938ca15, 0x97b03cff, 0x3dc2c0f8, 0x8d1ab2ec, 0x64380e51,
|
8535 | 0x68cc7bfb, 0xd90f2788, 0x12490181, 0x5de5ffd4, 0xdd7ef86a, 0x76a2e214, 0xb9a40368, 0x925d958f,
|
8536 | 0x4b39fffa, 0xba39aee9, 0xa4ffd30b, 0xfaf7933b, 0x6d498623, 0x193cbcfa, 0x27627545, 0x825cf47a,
|
8537 | 0x61bd8ba0, 0xd11e42d1, 0xcead04f4, 0x127ea392, 0x10428db7, 0x8272a972, 0x9270c4a8, 0x127de50b,
|
8538 | 0x285ba1c8, 0x3c62f44f, 0x35c0eaa5, 0xe805d231, 0x428929fb, 0xb4fcdf82, 0x4fb66a53, 0x0e7dc15b,
|
8539 | 0x1f081fab, 0x108618ae, 0xfcfd086d, 0xf9ff2889, 0x694bcc11, 0x236a5cae, 0x12deca4d, 0x2c3f8cc5,
|
8540 | 0xd2d02dfe, 0xf8ef5896, 0xe4cf52da, 0x95155b67, 0x494a488c, 0xb9b6a80c, 0x5c8f82bc, 0x89d36b45,
|
8541 | 0x3a609437, 0xec00c9a9, 0x44715253, 0x0a874b49, 0xd773bc40, 0x7c34671c, 0x02717ef6, 0x4feb5536,
|
8542 | 0xa2d02fff, 0xd2bf60c4, 0xd43f03c0, 0x50b4ef6d, 0x07478cd1, 0x006e1888, 0xa2e53f55, 0xb9e6d4bc,
|
8543 | 0xa2048016, 0x97573833, 0xd7207d67, 0xde0f8f3d, 0x72f87b33, 0xabcc4f33, 0x7688c55d, 0x7b00a6b0,
|
8544 | 0x947b0001, 0x570075d2, 0xf9bb88f8, 0x8942019e, 0x4264a5ff, 0x856302e0, 0x72dbd92b, 0xee971b69,
|
8545 | 0x6ea22fde, 0x5f08ae2b, 0xaf7a616d, 0xe5c98767, 0xcf1febd2, 0x61efc8c2, 0xf1ac2571, 0xcc8239c2,
|
8546 | 0x67214cb8, 0xb1e583d1, 0xb7dc3e62, 0x7f10bdce, 0xf90a5c38, 0x0ff0443d, 0x606e6dc6, 0x60543a49,
|
8547 | 0x5727c148, 0x2be98a1d, 0x8ab41738, 0x20e1be24, 0xaf96da0f, 0x68458425, 0x99833be5, 0x600d457d,
|
8548 | 0x282f9350, 0x8334b362, 0xd91d1120, 0x2b6d8da0, 0x642b1e31, 0x9c305a00, 0x52bce688, 0x1b03588a,
|
8549 | 0xf7baefd5, 0x4142ed9c, 0xa4315c11, 0x83323ec5, 0xdfef4636, 0xa133c501, 0xe9d3531c, 0xee353783
|
8550 | ];
|
8551 |
|
8552 | sBox[3] = [
|
8553 | 0x9db30420, 0x1fb6e9de, 0xa7be7bef, 0xd273a298, 0x4a4f7bdb, 0x64ad8c57, 0x85510443, 0xfa020ed1,
|
8554 | 0x7e287aff, 0xe60fb663, 0x095f35a1, 0x79ebf120, 0xfd059d43, 0x6497b7b1, 0xf3641f63, 0x241e4adf,
|
8555 | 0x28147f5f, 0x4fa2b8cd, 0xc9430040, 0x0cc32220, 0xfdd30b30, 0xc0a5374f, 0x1d2d00d9, 0x24147b15,
|
8556 | 0xee4d111a, 0x0fca5167, 0x71ff904c, 0x2d195ffe, 0x1a05645f, 0x0c13fefe, 0x081b08ca, 0x05170121,
|
8557 | 0x80530100, 0xe83e5efe, 0xac9af4f8, 0x7fe72701, 0xd2b8ee5f, 0x06df4261, 0xbb9e9b8a, 0x7293ea25,
|
8558 | 0xce84ffdf, 0xf5718801, 0x3dd64b04, 0xa26f263b, 0x7ed48400, 0x547eebe6, 0x446d4ca0, 0x6cf3d6f5,
|
8559 | 0x2649abdf, 0xaea0c7f5, 0x36338cc1, 0x503f7e93, 0xd3772061, 0x11b638e1, 0x72500e03, 0xf80eb2bb,
|
8560 | 0xabe0502e, 0xec8d77de, 0x57971e81, 0xe14f6746, 0xc9335400, 0x6920318f, 0x081dbb99, 0xffc304a5,
|
8561 | 0x4d351805, 0x7f3d5ce3, 0xa6c866c6, 0x5d5bcca9, 0xdaec6fea, 0x9f926f91, 0x9f46222f, 0x3991467d,
|
8562 | 0xa5bf6d8e, 0x1143c44f, 0x43958302, 0xd0214eeb, 0x022083b8, 0x3fb6180c, 0x18f8931e, 0x281658e6,
|
8563 | 0x26486e3e, 0x8bd78a70, 0x7477e4c1, 0xb506e07c, 0xf32d0a25, 0x79098b02, 0xe4eabb81, 0x28123b23,
|
8564 | 0x69dead38, 0x1574ca16, 0xdf871b62, 0x211c40b7, 0xa51a9ef9, 0x0014377b, 0x041e8ac8, 0x09114003,
|
8565 | 0xbd59e4d2, 0xe3d156d5, 0x4fe876d5, 0x2f91a340, 0x557be8de, 0x00eae4a7, 0x0ce5c2ec, 0x4db4bba6,
|
8566 | 0xe756bdff, 0xdd3369ac, 0xec17b035, 0x06572327, 0x99afc8b0, 0x56c8c391, 0x6b65811c, 0x5e146119,
|
8567 | 0x6e85cb75, 0xbe07c002, 0xc2325577, 0x893ff4ec, 0x5bbfc92d, 0xd0ec3b25, 0xb7801ab7, 0x8d6d3b24,
|
8568 | 0x20c763ef, 0xc366a5fc, 0x9c382880, 0x0ace3205, 0xaac9548a, 0xeca1d7c7, 0x041afa32, 0x1d16625a,
|
8569 | 0x6701902c, 0x9b757a54, 0x31d477f7, 0x9126b031, 0x36cc6fdb, 0xc70b8b46, 0xd9e66a48, 0x56e55a79,
|
8570 | 0x026a4ceb, 0x52437eff, 0x2f8f76b4, 0x0df980a5, 0x8674cde3, 0xedda04eb, 0x17a9be04, 0x2c18f4df,
|
8571 | 0xb7747f9d, 0xab2af7b4, 0xefc34d20, 0x2e096b7c, 0x1741a254, 0xe5b6a035, 0x213d42f6, 0x2c1c7c26,
|
8572 | 0x61c2f50f, 0x6552daf9, 0xd2c231f8, 0x25130f69, 0xd8167fa2, 0x0418f2c8, 0x001a96a6, 0x0d1526ab,
|
8573 | 0x63315c21, 0x5e0a72ec, 0x49bafefd, 0x187908d9, 0x8d0dbd86, 0x311170a7, 0x3e9b640c, 0xcc3e10d7,
|
8574 | 0xd5cad3b6, 0x0caec388, 0xf73001e1, 0x6c728aff, 0x71eae2a1, 0x1f9af36e, 0xcfcbd12f, 0xc1de8417,
|
8575 | 0xac07be6b, 0xcb44a1d8, 0x8b9b0f56, 0x013988c3, 0xb1c52fca, 0xb4be31cd, 0xd8782806, 0x12a3a4e2,
|
8576 | 0x6f7de532, 0x58fd7eb6, 0xd01ee900, 0x24adffc2, 0xf4990fc5, 0x9711aac5, 0x001d7b95, 0x82e5e7d2,
|
8577 | 0x109873f6, 0x00613096, 0xc32d9521, 0xada121ff, 0x29908415, 0x7fbb977f, 0xaf9eb3db, 0x29c9ed2a,
|
8578 | 0x5ce2a465, 0xa730f32c, 0xd0aa3fe8, 0x8a5cc091, 0xd49e2ce7, 0x0ce454a9, 0xd60acd86, 0x015f1919,
|
8579 | 0x77079103, 0xdea03af6, 0x78a8565e, 0xdee356df, 0x21f05cbe, 0x8b75e387, 0xb3c50651, 0xb8a5c3ef,
|
8580 | 0xd8eeb6d2, 0xe523be77, 0xc2154529, 0x2f69efdf, 0xafe67afb, 0xf470c4b2, 0xf3e0eb5b, 0xd6cc9876,
|
8581 | 0x39e4460c, 0x1fda8538, 0x1987832f, 0xca007367, 0xa99144f8, 0x296b299e, 0x492fc295, 0x9266beab,
|
8582 | 0xb5676e69, 0x9bd3ddda, 0xdf7e052f, 0xdb25701c, 0x1b5e51ee, 0xf65324e6, 0x6afce36c, 0x0316cc04,
|
8583 | 0x8644213e, 0xb7dc59d0, 0x7965291f, 0xccd6fd43, 0x41823979, 0x932bcdf6, 0xb657c34d, 0x4edfd282,
|
8584 | 0x7ae5290c, 0x3cb9536b, 0x851e20fe, 0x9833557e, 0x13ecf0b0, 0xd3ffb372, 0x3f85c5c1, 0x0aef7ed2
|
8585 | ];
|
8586 |
|
8587 | sBox[4] = [
|
8588 | 0x7ec90c04, 0x2c6e74b9, 0x9b0e66df, 0xa6337911, 0xb86a7fff, 0x1dd358f5, 0x44dd9d44, 0x1731167f,
|
8589 | 0x08fbf1fa, 0xe7f511cc, 0xd2051b00, 0x735aba00, 0x2ab722d8, 0x386381cb, 0xacf6243a, 0x69befd7a,
|
8590 | 0xe6a2e77f, 0xf0c720cd, 0xc4494816, 0xccf5c180, 0x38851640, 0x15b0a848, 0xe68b18cb, 0x4caadeff,
|
8591 | 0x5f480a01, 0x0412b2aa, 0x259814fc, 0x41d0efe2, 0x4e40b48d, 0x248eb6fb, 0x8dba1cfe, 0x41a99b02,
|
8592 | 0x1a550a04, 0xba8f65cb, 0x7251f4e7, 0x95a51725, 0xc106ecd7, 0x97a5980a, 0xc539b9aa, 0x4d79fe6a,
|
8593 | 0xf2f3f763, 0x68af8040, 0xed0c9e56, 0x11b4958b, 0xe1eb5a88, 0x8709e6b0, 0xd7e07156, 0x4e29fea7,
|
8594 | 0x6366e52d, 0x02d1c000, 0xc4ac8e05, 0x9377f571, 0x0c05372a, 0x578535f2, 0x2261be02, 0xd642a0c9,
|
8595 | 0xdf13a280, 0x74b55bd2, 0x682199c0, 0xd421e5ec, 0x53fb3ce8, 0xc8adedb3, 0x28a87fc9, 0x3d959981,
|
8596 | 0x5c1ff900, 0xfe38d399, 0x0c4eff0b, 0x062407ea, 0xaa2f4fb1, 0x4fb96976, 0x90c79505, 0xb0a8a774,
|
8597 | 0xef55a1ff, 0xe59ca2c2, 0xa6b62d27, 0xe66a4263, 0xdf65001f, 0x0ec50966, 0xdfdd55bc, 0x29de0655,
|
8598 | 0x911e739a, 0x17af8975, 0x32c7911c, 0x89f89468, 0x0d01e980, 0x524755f4, 0x03b63cc9, 0x0cc844b2,
|
8599 | 0xbcf3f0aa, 0x87ac36e9, 0xe53a7426, 0x01b3d82b, 0x1a9e7449, 0x64ee2d7e, 0xcddbb1da, 0x01c94910,
|
8600 | 0xb868bf80, 0x0d26f3fd, 0x9342ede7, 0x04a5c284, 0x636737b6, 0x50f5b616, 0xf24766e3, 0x8eca36c1,
|
8601 | 0x136e05db, 0xfef18391, 0xfb887a37, 0xd6e7f7d4, 0xc7fb7dc9, 0x3063fcdf, 0xb6f589de, 0xec2941da,
|
8602 | 0x26e46695, 0xb7566419, 0xf654efc5, 0xd08d58b7, 0x48925401, 0xc1bacb7f, 0xe5ff550f, 0xb6083049,
|
8603 | 0x5bb5d0e8, 0x87d72e5a, 0xab6a6ee1, 0x223a66ce, 0xc62bf3cd, 0x9e0885f9, 0x68cb3e47, 0x086c010f,
|
8604 | 0xa21de820, 0xd18b69de, 0xf3f65777, 0xfa02c3f6, 0x407edac3, 0xcbb3d550, 0x1793084d, 0xb0d70eba,
|
8605 | 0x0ab378d5, 0xd951fb0c, 0xded7da56, 0x4124bbe4, 0x94ca0b56, 0x0f5755d1, 0xe0e1e56e, 0x6184b5be,
|
8606 | 0x580a249f, 0x94f74bc0, 0xe327888e, 0x9f7b5561, 0xc3dc0280, 0x05687715, 0x646c6bd7, 0x44904db3,
|
8607 | 0x66b4f0a3, 0xc0f1648a, 0x697ed5af, 0x49e92ff6, 0x309e374f, 0x2cb6356a, 0x85808573, 0x4991f840,
|
8608 | 0x76f0ae02, 0x083be84d, 0x28421c9a, 0x44489406, 0x736e4cb8, 0xc1092910, 0x8bc95fc6, 0x7d869cf4,
|
8609 | 0x134f616f, 0x2e77118d, 0xb31b2be1, 0xaa90b472, 0x3ca5d717, 0x7d161bba, 0x9cad9010, 0xaf462ba2,
|
8610 | 0x9fe459d2, 0x45d34559, 0xd9f2da13, 0xdbc65487, 0xf3e4f94e, 0x176d486f, 0x097c13ea, 0x631da5c7,
|
8611 | 0x445f7382, 0x175683f4, 0xcdc66a97, 0x70be0288, 0xb3cdcf72, 0x6e5dd2f3, 0x20936079, 0x459b80a5,
|
8612 | 0xbe60e2db, 0xa9c23101, 0xeba5315c, 0x224e42f2, 0x1c5c1572, 0xf6721b2c, 0x1ad2fff3, 0x8c25404e,
|
8613 | 0x324ed72f, 0x4067b7fd, 0x0523138e, 0x5ca3bc78, 0xdc0fd66e, 0x75922283, 0x784d6b17, 0x58ebb16e,
|
8614 | 0x44094f85, 0x3f481d87, 0xfcfeae7b, 0x77b5ff76, 0x8c2302bf, 0xaaf47556, 0x5f46b02a, 0x2b092801,
|
8615 | 0x3d38f5f7, 0x0ca81f36, 0x52af4a8a, 0x66d5e7c0, 0xdf3b0874, 0x95055110, 0x1b5ad7a8, 0xf61ed5ad,
|
8616 | 0x6cf6e479, 0x20758184, 0xd0cefa65, 0x88f7be58, 0x4a046826, 0x0ff6f8f3, 0xa09c7f70, 0x5346aba0,
|
8617 | 0x5ce96c28, 0xe176eda3, 0x6bac307f, 0x376829d2, 0x85360fa9, 0x17e3fe2a, 0x24b79767, 0xf5a96b20,
|
8618 | 0xd6cd2595, 0x68ff1ebf, 0x7555442c, 0xf19f06be, 0xf9e0659a, 0xeeb9491d, 0x34010718, 0xbb30cab8,
|
8619 | 0xe822fe15, 0x88570983, 0x750e6249, 0xda627e55, 0x5e76ffa8, 0xb1534546, 0x6d47de08, 0xefe9e7d4
|
8620 | ];
|
8621 |
|
8622 | sBox[5] = [
|
8623 | 0xf6fa8f9d, 0x2cac6ce1, 0x4ca34867, 0xe2337f7c, 0x95db08e7, 0x016843b4, 0xeced5cbc, 0x325553ac,
|
8624 | 0xbf9f0960, 0xdfa1e2ed, 0x83f0579d, 0x63ed86b9, 0x1ab6a6b8, 0xde5ebe39, 0xf38ff732, 0x8989b138,
|
8625 | 0x33f14961, 0xc01937bd, 0xf506c6da, 0xe4625e7e, 0xa308ea99, 0x4e23e33c, 0x79cbd7cc, 0x48a14367,
|
8626 | 0xa3149619, 0xfec94bd5, 0xa114174a, 0xeaa01866, 0xa084db2d, 0x09a8486f, 0xa888614a, 0x2900af98,
|
8627 | 0x01665991, 0xe1992863, 0xc8f30c60, 0x2e78ef3c, 0xd0d51932, 0xcf0fec14, 0xf7ca07d2, 0xd0a82072,
|
8628 | 0xfd41197e, 0x9305a6b0, 0xe86be3da, 0x74bed3cd, 0x372da53c, 0x4c7f4448, 0xdab5d440, 0x6dba0ec3,
|
8629 | 0x083919a7, 0x9fbaeed9, 0x49dbcfb0, 0x4e670c53, 0x5c3d9c01, 0x64bdb941, 0x2c0e636a, 0xba7dd9cd,
|
8630 | 0xea6f7388, 0xe70bc762, 0x35f29adb, 0x5c4cdd8d, 0xf0d48d8c, 0xb88153e2, 0x08a19866, 0x1ae2eac8,
|
8631 | 0x284caf89, 0xaa928223, 0x9334be53, 0x3b3a21bf, 0x16434be3, 0x9aea3906, 0xefe8c36e, 0xf890cdd9,
|
8632 | 0x80226dae, 0xc340a4a3, 0xdf7e9c09, 0xa694a807, 0x5b7c5ecc, 0x221db3a6, 0x9a69a02f, 0x68818a54,
|
8633 | 0xceb2296f, 0x53c0843a, 0xfe893655, 0x25bfe68a, 0xb4628abc, 0xcf222ebf, 0x25ac6f48, 0xa9a99387,
|
8634 | 0x53bddb65, 0xe76ffbe7, 0xe967fd78, 0x0ba93563, 0x8e342bc1, 0xe8a11be9, 0x4980740d, 0xc8087dfc,
|
8635 | 0x8de4bf99, 0xa11101a0, 0x7fd37975, 0xda5a26c0, 0xe81f994f, 0x9528cd89, 0xfd339fed, 0xb87834bf,
|
8636 | 0x5f04456d, 0x22258698, 0xc9c4c83b, 0x2dc156be, 0x4f628daa, 0x57f55ec5, 0xe2220abe, 0xd2916ebf,
|
8637 | 0x4ec75b95, 0x24f2c3c0, 0x42d15d99, 0xcd0d7fa0, 0x7b6e27ff, 0xa8dc8af0, 0x7345c106, 0xf41e232f,
|
8638 | 0x35162386, 0xe6ea8926, 0x3333b094, 0x157ec6f2, 0x372b74af, 0x692573e4, 0xe9a9d848, 0xf3160289,
|
8639 | 0x3a62ef1d, 0xa787e238, 0xf3a5f676, 0x74364853, 0x20951063, 0x4576698d, 0xb6fad407, 0x592af950,
|
8640 | 0x36f73523, 0x4cfb6e87, 0x7da4cec0, 0x6c152daa, 0xcb0396a8, 0xc50dfe5d, 0xfcd707ab, 0x0921c42f,
|
8641 | 0x89dff0bb, 0x5fe2be78, 0x448f4f33, 0x754613c9, 0x2b05d08d, 0x48b9d585, 0xdc049441, 0xc8098f9b,
|
8642 | 0x7dede786, 0xc39a3373, 0x42410005, 0x6a091751, 0x0ef3c8a6, 0x890072d6, 0x28207682, 0xa9a9f7be,
|
8643 | 0xbf32679d, 0xd45b5b75, 0xb353fd00, 0xcbb0e358, 0x830f220a, 0x1f8fb214, 0xd372cf08, 0xcc3c4a13,
|
8644 | 0x8cf63166, 0x061c87be, 0x88c98f88, 0x6062e397, 0x47cf8e7a, 0xb6c85283, 0x3cc2acfb, 0x3fc06976,
|
8645 | 0x4e8f0252, 0x64d8314d, 0xda3870e3, 0x1e665459, 0xc10908f0, 0x513021a5, 0x6c5b68b7, 0x822f8aa0,
|
8646 | 0x3007cd3e, 0x74719eef, 0xdc872681, 0x073340d4, 0x7e432fd9, 0x0c5ec241, 0x8809286c, 0xf592d891,
|
8647 | 0x08a930f6, 0x957ef305, 0xb7fbffbd, 0xc266e96f, 0x6fe4ac98, 0xb173ecc0, 0xbc60b42a, 0x953498da,
|
8648 | 0xfba1ae12, 0x2d4bd736, 0x0f25faab, 0xa4f3fceb, 0xe2969123, 0x257f0c3d, 0x9348af49, 0x361400bc,
|
8649 | 0xe8816f4a, 0x3814f200, 0xa3f94043, 0x9c7a54c2, 0xbc704f57, 0xda41e7f9, 0xc25ad33a, 0x54f4a084,
|
8650 | 0xb17f5505, 0x59357cbe, 0xedbd15c8, 0x7f97c5ab, 0xba5ac7b5, 0xb6f6deaf, 0x3a479c3a, 0x5302da25,
|
8651 | 0x653d7e6a, 0x54268d49, 0x51a477ea, 0x5017d55b, 0xd7d25d88, 0x44136c76, 0x0404a8c8, 0xb8e5a121,
|
8652 | 0xb81a928a, 0x60ed5869, 0x97c55b96, 0xeaec991b, 0x29935913, 0x01fdb7f1, 0x088e8dfa, 0x9ab6f6f5,
|
8653 | 0x3b4cbf9f, 0x4a5de3ab, 0xe6051d35, 0xa0e1d855, 0xd36b4cf1, 0xf544edeb, 0xb0e93524, 0xbebb8fbd,
|
8654 | 0xa2d762cf, 0x49c92f54, 0x38b5f331, 0x7128a454, 0x48392905, 0xa65b1db8, 0x851c97bd, 0xd675cf2f
|
8655 | ];
|
8656 |
|
8657 | sBox[6] = [
|
8658 | 0x85e04019, 0x332bf567, 0x662dbfff, 0xcfc65693, 0x2a8d7f6f, 0xab9bc912, 0xde6008a1, 0x2028da1f,
|
8659 | 0x0227bce7, 0x4d642916, 0x18fac300, 0x50f18b82, 0x2cb2cb11, 0xb232e75c, 0x4b3695f2, 0xb28707de,
|
8660 | 0xa05fbcf6, 0xcd4181e9, 0xe150210c, 0xe24ef1bd, 0xb168c381, 0xfde4e789, 0x5c79b0d8, 0x1e8bfd43,
|
8661 | 0x4d495001, 0x38be4341, 0x913cee1d, 0x92a79c3f, 0x089766be, 0xbaeeadf4, 0x1286becf, 0xb6eacb19,
|
8662 | 0x2660c200, 0x7565bde4, 0x64241f7a, 0x8248dca9, 0xc3b3ad66, 0x28136086, 0x0bd8dfa8, 0x356d1cf2,
|
8663 | 0x107789be, 0xb3b2e9ce, 0x0502aa8f, 0x0bc0351e, 0x166bf52a, 0xeb12ff82, 0xe3486911, 0xd34d7516,
|
8664 | 0x4e7b3aff, 0x5f43671b, 0x9cf6e037, 0x4981ac83, 0x334266ce, 0x8c9341b7, 0xd0d854c0, 0xcb3a6c88,
|
8665 | 0x47bc2829, 0x4725ba37, 0xa66ad22b, 0x7ad61f1e, 0x0c5cbafa, 0x4437f107, 0xb6e79962, 0x42d2d816,
|
8666 | 0x0a961288, 0xe1a5c06e, 0x13749e67, 0x72fc081a, 0xb1d139f7, 0xf9583745, 0xcf19df58, 0xbec3f756,
|
8667 | 0xc06eba30, 0x07211b24, 0x45c28829, 0xc95e317f, 0xbc8ec511, 0x38bc46e9, 0xc6e6fa14, 0xbae8584a,
|
8668 | 0xad4ebc46, 0x468f508b, 0x7829435f, 0xf124183b, 0x821dba9f, 0xaff60ff4, 0xea2c4e6d, 0x16e39264,
|
8669 | 0x92544a8b, 0x009b4fc3, 0xaba68ced, 0x9ac96f78, 0x06a5b79a, 0xb2856e6e, 0x1aec3ca9, 0xbe838688,
|
8670 | 0x0e0804e9, 0x55f1be56, 0xe7e5363b, 0xb3a1f25d, 0xf7debb85, 0x61fe033c, 0x16746233, 0x3c034c28,
|
8671 | 0xda6d0c74, 0x79aac56c, 0x3ce4e1ad, 0x51f0c802, 0x98f8f35a, 0x1626a49f, 0xeed82b29, 0x1d382fe3,
|
8672 | 0x0c4fb99a, 0xbb325778, 0x3ec6d97b, 0x6e77a6a9, 0xcb658b5c, 0xd45230c7, 0x2bd1408b, 0x60c03eb7,
|
8673 | 0xb9068d78, 0xa33754f4, 0xf430c87d, 0xc8a71302, 0xb96d8c32, 0xebd4e7be, 0xbe8b9d2d, 0x7979fb06,
|
8674 | 0xe7225308, 0x8b75cf77, 0x11ef8da4, 0xe083c858, 0x8d6b786f, 0x5a6317a6, 0xfa5cf7a0, 0x5dda0033,
|
8675 | 0xf28ebfb0, 0xf5b9c310, 0xa0eac280, 0x08b9767a, 0xa3d9d2b0, 0x79d34217, 0x021a718d, 0x9ac6336a,
|
8676 | 0x2711fd60, 0x438050e3, 0x069908a8, 0x3d7fedc4, 0x826d2bef, 0x4eeb8476, 0x488dcf25, 0x36c9d566,
|
8677 | 0x28e74e41, 0xc2610aca, 0x3d49a9cf, 0xbae3b9df, 0xb65f8de6, 0x92aeaf64, 0x3ac7d5e6, 0x9ea80509,
|
8678 | 0xf22b017d, 0xa4173f70, 0xdd1e16c3, 0x15e0d7f9, 0x50b1b887, 0x2b9f4fd5, 0x625aba82, 0x6a017962,
|
8679 | 0x2ec01b9c, 0x15488aa9, 0xd716e740, 0x40055a2c, 0x93d29a22, 0xe32dbf9a, 0x058745b9, 0x3453dc1e,
|
8680 | 0xd699296e, 0x496cff6f, 0x1c9f4986, 0xdfe2ed07, 0xb87242d1, 0x19de7eae, 0x053e561a, 0x15ad6f8c,
|
8681 | 0x66626c1c, 0x7154c24c, 0xea082b2a, 0x93eb2939, 0x17dcb0f0, 0x58d4f2ae, 0x9ea294fb, 0x52cf564c,
|
8682 | 0x9883fe66, 0x2ec40581, 0x763953c3, 0x01d6692e, 0xd3a0c108, 0xa1e7160e, 0xe4f2dfa6, 0x693ed285,
|
8683 | 0x74904698, 0x4c2b0edd, 0x4f757656, 0x5d393378, 0xa132234f, 0x3d321c5d, 0xc3f5e194, 0x4b269301,
|
8684 | 0xc79f022f, 0x3c997e7e, 0x5e4f9504, 0x3ffafbbd, 0x76f7ad0e, 0x296693f4, 0x3d1fce6f, 0xc61e45be,
|
8685 | 0xd3b5ab34, 0xf72bf9b7, 0x1b0434c0, 0x4e72b567, 0x5592a33d, 0xb5229301, 0xcfd2a87f, 0x60aeb767,
|
8686 | 0x1814386b, 0x30bcc33d, 0x38a0c07d, 0xfd1606f2, 0xc363519b, 0x589dd390, 0x5479f8e6, 0x1cb8d647,
|
8687 | 0x97fd61a9, 0xea7759f4, 0x2d57539d, 0x569a58cf, 0xe84e63ad, 0x462e1b78, 0x6580f87e, 0xf3817914,
|
8688 | 0x91da55f4, 0x40a230f3, 0xd1988f35, 0xb6e318d2, 0x3ffa50bc, 0x3d40f021, 0xc3c0bdae, 0x4958c24c,
|
8689 | 0x518f36b2, 0x84b1d370, 0x0fedce83, 0x878ddada, 0xf2a279c7, 0x94e01be8, 0x90716f4b, 0x954b8aa3
|
8690 | ];
|
8691 |
|
8692 | sBox[7] = [
|
8693 | 0xe216300d, 0xbbddfffc, 0xa7ebdabd, 0x35648095, 0x7789f8b7, 0xe6c1121b, 0x0e241600, 0x052ce8b5,
|
8694 | 0x11a9cfb0, 0xe5952f11, 0xece7990a, 0x9386d174, 0x2a42931c, 0x76e38111, 0xb12def3a, 0x37ddddfc,
|
8695 | 0xde9adeb1, 0x0a0cc32c, 0xbe197029, 0x84a00940, 0xbb243a0f, 0xb4d137cf, 0xb44e79f0, 0x049eedfd,
|
8696 | 0x0b15a15d, 0x480d3168, 0x8bbbde5a, 0x669ded42, 0xc7ece831, 0x3f8f95e7, 0x72df191b, 0x7580330d,
|
8697 | 0x94074251, 0x5c7dcdfa, 0xabbe6d63, 0xaa402164, 0xb301d40a, 0x02e7d1ca, 0x53571dae, 0x7a3182a2,
|
8698 | 0x12a8ddec, 0xfdaa335d, 0x176f43e8, 0x71fb46d4, 0x38129022, 0xce949ad4, 0xb84769ad, 0x965bd862,
|
8699 | 0x82f3d055, 0x66fb9767, 0x15b80b4e, 0x1d5b47a0, 0x4cfde06f, 0xc28ec4b8, 0x57e8726e, 0x647a78fc,
|
8700 | 0x99865d44, 0x608bd593, 0x6c200e03, 0x39dc5ff6, 0x5d0b00a3, 0xae63aff2, 0x7e8bd632, 0x70108c0c,
|
8701 | 0xbbd35049, 0x2998df04, 0x980cf42a, 0x9b6df491, 0x9e7edd53, 0x06918548, 0x58cb7e07, 0x3b74ef2e,
|
8702 | 0x522fffb1, 0xd24708cc, 0x1c7e27cd, 0xa4eb215b, 0x3cf1d2e2, 0x19b47a38, 0x424f7618, 0x35856039,
|
8703 | 0x9d17dee7, 0x27eb35e6, 0xc9aff67b, 0x36baf5b8, 0x09c467cd, 0xc18910b1, 0xe11dbf7b, 0x06cd1af8,
|
8704 | 0x7170c608, 0x2d5e3354, 0xd4de495a, 0x64c6d006, 0xbcc0c62c, 0x3dd00db3, 0x708f8f34, 0x77d51b42,
|
8705 | 0x264f620f, 0x24b8d2bf, 0x15c1b79e, 0x46a52564, 0xf8d7e54e, 0x3e378160, 0x7895cda5, 0x859c15a5,
|
8706 | 0xe6459788, 0xc37bc75f, 0xdb07ba0c, 0x0676a3ab, 0x7f229b1e, 0x31842e7b, 0x24259fd7, 0xf8bef472,
|
8707 | 0x835ffcb8, 0x6df4c1f2, 0x96f5b195, 0xfd0af0fc, 0xb0fe134c, 0xe2506d3d, 0x4f9b12ea, 0xf215f225,
|
8708 | 0xa223736f, 0x9fb4c428, 0x25d04979, 0x34c713f8, 0xc4618187, 0xea7a6e98, 0x7cd16efc, 0x1436876c,
|
8709 | 0xf1544107, 0xbedeee14, 0x56e9af27, 0xa04aa441, 0x3cf7c899, 0x92ecbae6, 0xdd67016d, 0x151682eb,
|
8710 | 0xa842eedf, 0xfdba60b4, 0xf1907b75, 0x20e3030f, 0x24d8c29e, 0xe139673b, 0xefa63fb8, 0x71873054,
|
8711 | 0xb6f2cf3b, 0x9f326442, 0xcb15a4cc, 0xb01a4504, 0xf1e47d8d, 0x844a1be5, 0xbae7dfdc, 0x42cbda70,
|
8712 | 0xcd7dae0a, 0x57e85b7a, 0xd53f5af6, 0x20cf4d8c, 0xcea4d428, 0x79d130a4, 0x3486ebfb, 0x33d3cddc,
|
8713 | 0x77853b53, 0x37effcb5, 0xc5068778, 0xe580b3e6, 0x4e68b8f4, 0xc5c8b37e, 0x0d809ea2, 0x398feb7c,
|
8714 | 0x132a4f94, 0x43b7950e, 0x2fee7d1c, 0x223613bd, 0xdd06caa2, 0x37df932b, 0xc4248289, 0xacf3ebc3,
|
8715 | 0x5715f6b7, 0xef3478dd, 0xf267616f, 0xc148cbe4, 0x9052815e, 0x5e410fab, 0xb48a2465, 0x2eda7fa4,
|
8716 | 0xe87b40e4, 0xe98ea084, 0x5889e9e1, 0xefd390fc, 0xdd07d35b, 0xdb485694, 0x38d7e5b2, 0x57720101,
|
8717 | 0x730edebc, 0x5b643113, 0x94917e4f, 0x503c2fba, 0x646f1282, 0x7523d24a, 0xe0779695, 0xf9c17a8f,
|
8718 | 0x7a5b2121, 0xd187b896, 0x29263a4d, 0xba510cdf, 0x81f47c9f, 0xad1163ed, 0xea7b5965, 0x1a00726e,
|
8719 | 0x11403092, 0x00da6d77, 0x4a0cdd61, 0xad1f4603, 0x605bdfb0, 0x9eedc364, 0x22ebe6a8, 0xcee7d28a,
|
8720 | 0xa0e736a0, 0x5564a6b9, 0x10853209, 0xc7eb8f37, 0x2de705ca, 0x8951570f, 0xdf09822b, 0xbd691a6c,
|
8721 | 0xaa12e4f2, 0x87451c0f, 0xe0f6a27a, 0x3ada4819, 0x4cf1764f, 0x0d771c2b, 0x67cdb156, 0x350d8384,
|
8722 | 0x5938fa0f, 0x42399ef3, 0x36997b07, 0x0e84093d, 0x4aa93e61, 0x8360d87b, 0x1fa98b0c, 0x1149382c,
|
8723 | 0xe97625a5, 0x0614d1b7, 0x0e25244b, 0x0c768347, 0x589e8d82, 0x0d2059d1, 0xa466bb1e, 0xf8da0a82,
|
8724 | 0x04f19130, 0xba6e4ec0, 0x99265164, 0x1ee7230d, 0x50b2ad80, 0xeaee6801, 0x8db2a283, 0xea8bf59e
|
8725 | ];
|
8726 | }
|
8727 |
|
8728 | function Cast5(key) {
|
8729 | this.cast5 = new OpenpgpSymencCast5();
|
8730 | this.cast5.setKey(key);
|
8731 |
|
8732 | this.encrypt = function(block) {
|
8733 | return this.cast5.encrypt(block);
|
8734 | };
|
8735 | }
|
8736 |
|
8737 | Cast5.blockSize = Cast5.prototype.blockSize = 8;
|
8738 | Cast5.keySize = Cast5.prototype.keySize = 16;
|
8739 |
|
8740 |
|
8741 |
|
8742 |
|
8743 |
|
8744 |
|
8745 |
|
8746 |
|
8747 |
|
8748 |
|
8749 |
|
8750 |
|
8751 |
|
8752 |
|
8753 |
|
8754 |
|
8755 |
|
8756 |
|
8757 |
|
8758 |
|
8759 |
|
8760 |
|
8761 |
|
8762 |
|
8763 |
|
8764 |
|
8765 |
|
8766 |
|
8767 | const MAXINT = 0xFFFFFFFF;
|
8768 |
|
8769 | function rotw(w, n) {
|
8770 | return (w << n | w >>> (32 - n)) & MAXINT;
|
8771 | }
|
8772 |
|
8773 | function getW(a, i) {
|
8774 | return a[i] | a[i + 1] << 8 | a[i + 2] << 16 | a[i + 3] << 24;
|
8775 | }
|
8776 |
|
8777 | function setW(a, i, w) {
|
8778 | a.splice(i, 4, w & 0xFF, (w >>> 8) & 0xFF, (w >>> 16) & 0xFF, (w >>> 24) & 0xFF);
|
8779 | }
|
8780 |
|
8781 | function getB(x, n) {
|
8782 | return (x >>> (n * 8)) & 0xFF;
|
8783 | }
|
8784 |
|
8785 |
|
8786 |
|
8787 |
|
8788 |
|
8789 | function createTwofish() {
|
8790 |
|
8791 | let keyBytes = null;
|
8792 | let dataBytes = null;
|
8793 | let dataOffset = -1;
|
8794 |
|
8795 |
|
8796 |
|
8797 |
|
8798 | let tfsKey = [];
|
8799 | let tfsM = [
|
8800 | [],
|
8801 | [],
|
8802 | [],
|
8803 | []
|
8804 | ];
|
8805 |
|
8806 | function tfsInit(key) {
|
8807 | keyBytes = key;
|
8808 | let i;
|
8809 | let a;
|
8810 | let b;
|
8811 | let c;
|
8812 | let d;
|
8813 | const meKey = [];
|
8814 | const moKey = [];
|
8815 | const inKey = [];
|
8816 | let kLen;
|
8817 | const sKey = [];
|
8818 | let f01;
|
8819 | let f5b;
|
8820 | let fef;
|
8821 |
|
8822 | const q0 = [
|
8823 | [8, 1, 7, 13, 6, 15, 3, 2, 0, 11, 5, 9, 14, 12, 10, 4],
|
8824 | [2, 8, 11, 13, 15, 7, 6, 14, 3, 1, 9, 4, 0, 10, 12, 5]
|
8825 | ];
|
8826 | const q1 = [
|
8827 | [14, 12, 11, 8, 1, 2, 3, 5, 15, 4, 10, 6, 7, 0, 9, 13],
|
8828 | [1, 14, 2, 11, 4, 12, 3, 7, 6, 13, 10, 5, 15, 9, 0, 8]
|
8829 | ];
|
8830 | const q2 = [
|
8831 | [11, 10, 5, 14, 6, 13, 9, 0, 12, 8, 15, 3, 2, 4, 7, 1],
|
8832 | [4, 12, 7, 5, 1, 6, 9, 10, 0, 14, 13, 8, 2, 11, 3, 15]
|
8833 | ];
|
8834 | const q3 = [
|
8835 | [13, 7, 15, 4, 1, 2, 6, 14, 9, 11, 3, 0, 8, 5, 12, 10],
|
8836 | [11, 9, 5, 1, 12, 3, 13, 14, 6, 4, 7, 15, 2, 0, 8, 10]
|
8837 | ];
|
8838 | const ror4 = [0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15];
|
8839 | const ashx = [0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 5, 14, 7];
|
8840 | const q = [
|
8841 | [],
|
8842 | []
|
8843 | ];
|
8844 | const m = [
|
8845 | [],
|
8846 | [],
|
8847 | [],
|
8848 | []
|
8849 | ];
|
8850 |
|
8851 | function ffm5b(x) {
|
8852 | return x ^ (x >> 2) ^ [0, 90, 180, 238][x & 3];
|
8853 | }
|
8854 |
|
8855 | function ffmEf(x) {
|
8856 | return x ^ (x >> 1) ^ (x >> 2) ^ [0, 238, 180, 90][x & 3];
|
8857 | }
|
8858 |
|
8859 | function mdsRem(p, q) {
|
8860 | let i;
|
8861 | let t;
|
8862 | let u;
|
8863 | for (i = 0; i < 8; i++) {
|
8864 | t = q >>> 24;
|
8865 | q = ((q << 8) & MAXINT) | p >>> 24;
|
8866 | p = (p << 8) & MAXINT;
|
8867 | u = t << 1;
|
8868 | if (t & 128) {
|
8869 | u ^= 333;
|
8870 | }
|
8871 | q ^= t ^ (u << 16);
|
8872 | u ^= t >>> 1;
|
8873 | if (t & 1) {
|
8874 | u ^= 166;
|
8875 | }
|
8876 | q ^= u << 24 | u << 8;
|
8877 | }
|
8878 | return q;
|
8879 | }
|
8880 |
|
8881 | function qp(n, x) {
|
8882 | const a = x >> 4;
|
8883 | const b = x & 15;
|
8884 | const c = q0[n][a ^ b];
|
8885 | const d = q1[n][ror4[b] ^ ashx[a]];
|
8886 | return q3[n][ror4[d] ^ ashx[c]] << 4 | q2[n][c ^ d];
|
8887 | }
|
8888 |
|
8889 | function hFun(x, key) {
|
8890 | let a = getB(x, 0);
|
8891 | let b = getB(x, 1);
|
8892 | let c = getB(x, 2);
|
8893 | let d = getB(x, 3);
|
8894 | switch (kLen) {
|
8895 | case 4:
|
8896 | a = q[1][a] ^ getB(key[3], 0);
|
8897 | b = q[0][b] ^ getB(key[3], 1);
|
8898 | c = q[0][c] ^ getB(key[3], 2);
|
8899 | d = q[1][d] ^ getB(key[3], 3);
|
8900 | case 3:
|
8901 | a = q[1][a] ^ getB(key[2], 0);
|
8902 | b = q[1][b] ^ getB(key[2], 1);
|
8903 | c = q[0][c] ^ getB(key[2], 2);
|
8904 | d = q[0][d] ^ getB(key[2], 3);
|
8905 | case 2:
|
8906 | a = q[0][q[0][a] ^ getB(key[1], 0)] ^ getB(key[0], 0);
|
8907 | b = q[0][q[1][b] ^ getB(key[1], 1)] ^ getB(key[0], 1);
|
8908 | c = q[1][q[0][c] ^ getB(key[1], 2)] ^ getB(key[0], 2);
|
8909 | d = q[1][q[1][d] ^ getB(key[1], 3)] ^ getB(key[0], 3);
|
8910 | }
|
8911 | return m[0][a] ^ m[1][b] ^ m[2][c] ^ m[3][d];
|
8912 | }
|
8913 |
|
8914 | keyBytes = keyBytes.slice(0, 32);
|
8915 | i = keyBytes.length;
|
8916 | while (i !== 16 && i !== 24 && i !== 32) {
|
8917 | keyBytes[i++] = 0;
|
8918 | }
|
8919 |
|
8920 | for (i = 0; i < keyBytes.length; i += 4) {
|
8921 | inKey[i >> 2] = getW(keyBytes, i);
|
8922 | }
|
8923 | for (i = 0; i < 256; i++) {
|
8924 | q[0][i] = qp(0, i);
|
8925 | q[1][i] = qp(1, i);
|
8926 | }
|
8927 | for (i = 0; i < 256; i++) {
|
8928 | f01 = q[1][i];
|
8929 | f5b = ffm5b(f01);
|
8930 | fef = ffmEf(f01);
|
8931 | m[0][i] = f01 + (f5b << 8) + (fef << 16) + (fef << 24);
|
8932 | m[2][i] = f5b + (fef << 8) + (f01 << 16) + (fef << 24);
|
8933 | f01 = q[0][i];
|
8934 | f5b = ffm5b(f01);
|
8935 | fef = ffmEf(f01);
|
8936 | m[1][i] = fef + (fef << 8) + (f5b << 16) + (f01 << 24);
|
8937 | m[3][i] = f5b + (f01 << 8) + (fef << 16) + (f5b << 24);
|
8938 | }
|
8939 |
|
8940 | kLen = inKey.length / 2;
|
8941 | for (i = 0; i < kLen; i++) {
|
8942 | a = inKey[i + i];
|
8943 | meKey[i] = a;
|
8944 | b = inKey[i + i + 1];
|
8945 | moKey[i] = b;
|
8946 | sKey[kLen - i - 1] = mdsRem(a, b);
|
8947 | }
|
8948 | for (i = 0; i < 40; i += 2) {
|
8949 | a = 0x1010101 * i;
|
8950 | b = a + 0x1010101;
|
8951 | a = hFun(a, meKey);
|
8952 | b = rotw(hFun(b, moKey), 8);
|
8953 | tfsKey[i] = (a + b) & MAXINT;
|
8954 | tfsKey[i + 1] = rotw(a + 2 * b, 9);
|
8955 | }
|
8956 | for (i = 0; i < 256; i++) {
|
8957 | a = b = c = d = i;
|
8958 | switch (kLen) {
|
8959 | case 4:
|
8960 | a = q[1][a] ^ getB(sKey[3], 0);
|
8961 | b = q[0][b] ^ getB(sKey[3], 1);
|
8962 | c = q[0][c] ^ getB(sKey[3], 2);
|
8963 | d = q[1][d] ^ getB(sKey[3], 3);
|
8964 | case 3:
|
8965 | a = q[1][a] ^ getB(sKey[2], 0);
|
8966 | b = q[1][b] ^ getB(sKey[2], 1);
|
8967 | c = q[0][c] ^ getB(sKey[2], 2);
|
8968 | d = q[0][d] ^ getB(sKey[2], 3);
|
8969 | case 2:
|
8970 | tfsM[0][i] = m[0][q[0][q[0][a] ^ getB(sKey[1], 0)] ^ getB(sKey[0], 0)];
|
8971 | tfsM[1][i] = m[1][q[0][q[1][b] ^ getB(sKey[1], 1)] ^ getB(sKey[0], 1)];
|
8972 | tfsM[2][i] = m[2][q[1][q[0][c] ^ getB(sKey[1], 2)] ^ getB(sKey[0], 2)];
|
8973 | tfsM[3][i] = m[3][q[1][q[1][d] ^ getB(sKey[1], 3)] ^ getB(sKey[0], 3)];
|
8974 | }
|
8975 | }
|
8976 | }
|
8977 |
|
8978 | function tfsG0(x) {
|
8979 | return tfsM[0][getB(x, 0)] ^ tfsM[1][getB(x, 1)] ^ tfsM[2][getB(x, 2)] ^ tfsM[3][getB(x, 3)];
|
8980 | }
|
8981 |
|
8982 | function tfsG1(x) {
|
8983 | return tfsM[0][getB(x, 3)] ^ tfsM[1][getB(x, 0)] ^ tfsM[2][getB(x, 1)] ^ tfsM[3][getB(x, 2)];
|
8984 | }
|
8985 |
|
8986 | function tfsFrnd(r, blk) {
|
8987 | let a = tfsG0(blk[0]);
|
8988 | let b = tfsG1(blk[1]);
|
8989 | blk[2] = rotw(blk[2] ^ (a + b + tfsKey[4 * r + 8]) & MAXINT, 31);
|
8990 | blk[3] = rotw(blk[3], 1) ^ (a + 2 * b + tfsKey[4 * r + 9]) & MAXINT;
|
8991 | a = tfsG0(blk[2]);
|
8992 | b = tfsG1(blk[3]);
|
8993 | blk[0] = rotw(blk[0] ^ (a + b + tfsKey[4 * r + 10]) & MAXINT, 31);
|
8994 | blk[1] = rotw(blk[1], 1) ^ (a + 2 * b + tfsKey[4 * r + 11]) & MAXINT;
|
8995 | }
|
8996 |
|
8997 | function tfsIrnd(i, blk) {
|
8998 | let a = tfsG0(blk[0]);
|
8999 | let b = tfsG1(blk[1]);
|
9000 | blk[2] = rotw(blk[2], 1) ^ (a + b + tfsKey[4 * i + 10]) & MAXINT;
|
9001 | blk[3] = rotw(blk[3] ^ (a + 2 * b + tfsKey[4 * i + 11]) & MAXINT, 31);
|
9002 | a = tfsG0(blk[2]);
|
9003 | b = tfsG1(blk[3]);
|
9004 | blk[0] = rotw(blk[0], 1) ^ (a + b + tfsKey[4 * i + 8]) & MAXINT;
|
9005 | blk[1] = rotw(blk[1] ^ (a + 2 * b + tfsKey[4 * i + 9]) & MAXINT, 31);
|
9006 | }
|
9007 |
|
9008 | function tfsClose() {
|
9009 | tfsKey = [];
|
9010 | tfsM = [
|
9011 | [],
|
9012 | [],
|
9013 | [],
|
9014 | []
|
9015 | ];
|
9016 | }
|
9017 |
|
9018 | function tfsEncrypt(data, offset) {
|
9019 | dataBytes = data;
|
9020 | dataOffset = offset;
|
9021 | const blk = [getW(dataBytes, dataOffset) ^ tfsKey[0],
|
9022 | getW(dataBytes, dataOffset + 4) ^ tfsKey[1],
|
9023 | getW(dataBytes, dataOffset + 8) ^ tfsKey[2],
|
9024 | getW(dataBytes, dataOffset + 12) ^ tfsKey[3]];
|
9025 | for (let j = 0; j < 8; j++) {
|
9026 | tfsFrnd(j, blk);
|
9027 | }
|
9028 | setW(dataBytes, dataOffset, blk[2] ^ tfsKey[4]);
|
9029 | setW(dataBytes, dataOffset + 4, blk[3] ^ tfsKey[5]);
|
9030 | setW(dataBytes, dataOffset + 8, blk[0] ^ tfsKey[6]);
|
9031 | setW(dataBytes, dataOffset + 12, blk[1] ^ tfsKey[7]);
|
9032 | dataOffset += 16;
|
9033 | return dataBytes;
|
9034 | }
|
9035 |
|
9036 | function tfsDecrypt(data, offset) {
|
9037 | dataBytes = data;
|
9038 | dataOffset = offset;
|
9039 | const blk = [getW(dataBytes, dataOffset) ^ tfsKey[4],
|
9040 | getW(dataBytes, dataOffset + 4) ^ tfsKey[5],
|
9041 | getW(dataBytes, dataOffset + 8) ^ tfsKey[6],
|
9042 | getW(dataBytes, dataOffset + 12) ^ tfsKey[7]];
|
9043 | for (let j = 7; j >= 0; j--) {
|
9044 | tfsIrnd(j, blk);
|
9045 | }
|
9046 | setW(dataBytes, dataOffset, blk[2] ^ tfsKey[0]);
|
9047 | setW(dataBytes, dataOffset + 4, blk[3] ^ tfsKey[1]);
|
9048 | setW(dataBytes, dataOffset + 8, blk[0] ^ tfsKey[2]);
|
9049 | setW(dataBytes, dataOffset + 12, blk[1] ^ tfsKey[3]);
|
9050 | dataOffset += 16;
|
9051 | }
|
9052 |
|
9053 |
|
9054 |
|
9055 | function tfsFinal() {
|
9056 | return dataBytes;
|
9057 | }
|
9058 |
|
9059 | return {
|
9060 | name: "twofish",
|
9061 | blocksize: 128 / 8,
|
9062 | open: tfsInit,
|
9063 | close: tfsClose,
|
9064 | encrypt: tfsEncrypt,
|
9065 | decrypt: tfsDecrypt,
|
9066 |
|
9067 | finalize: tfsFinal
|
9068 | };
|
9069 | }
|
9070 |
|
9071 |
|
9072 |
|
9073 | function TF(key) {
|
9074 | this.tf = createTwofish();
|
9075 | this.tf.open(Array.from(key), 0);
|
9076 |
|
9077 | this.encrypt = function(block) {
|
9078 | return this.tf.encrypt(Array.from(block), 0);
|
9079 | };
|
9080 | }
|
9081 |
|
9082 | TF.keySize = TF.prototype.keySize = 32;
|
9083 | TF.blockSize = TF.prototype.blockSize = 16;
|
9084 |
|
9085 |
|
9086 |
|
9087 |
|
9088 |
|
9089 |
|
9090 |
|
9091 |
|
9092 |
|
9093 |
|
9094 |
|
9095 |
|
9096 |
|
9097 | function Blowfish() {}
|
9098 |
|
9099 |
|
9100 |
|
9101 |
|
9102 |
|
9103 | Blowfish.prototype.BLOCKSIZE = 8;
|
9104 |
|
9105 |
|
9106 |
|
9107 |
|
9108 | Blowfish.prototype.SBOXES = [
|
9109 | [
|
9110 | 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
|
9111 | 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16,
|
9112 | 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658,
|
9113 | 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013,
|
9114 | 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e,
|
9115 | 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60,
|
9116 | 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6,
|
9117 | 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a,
|
9118 | 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c,
|
9119 | 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193,
|
9120 | 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1,
|
9121 | 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239,
|
9122 | 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a,
|
9123 | 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3,
|
9124 | 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176,
|
9125 | 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe,
|
9126 | 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706,
|
9127 | 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b,
|
9128 | 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b,
|
9129 | 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463,
|
9130 | 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c,
|
9131 | 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3,
|
9132 | 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a,
|
9133 | 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8,
|
9134 | 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760,
|
9135 | 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db,
|
9136 | 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8,
|
9137 | 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b,
|
9138 | 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33,
|
9139 | 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4,
|
9140 | 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0,
|
9141 | 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c,
|
9142 | 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777,
|
9143 | 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299,
|
9144 | 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705,
|
9145 | 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf,
|
9146 | 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e,
|
9147 | 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa,
|
9148 | 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9,
|
9149 | 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915,
|
9150 | 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f,
|
9151 | 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664,
|
9152 | 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a
|
9153 | ],
|
9154 | [
|
9155 | 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d,
|
9156 | 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1,
|
9157 | 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65,
|
9158 | 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1,
|
9159 | 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9,
|
9160 | 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737,
|
9161 | 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d,
|
9162 | 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd,
|
9163 | 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc,
|
9164 | 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41,
|
9165 | 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908,
|
9166 | 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af,
|
9167 | 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124,
|
9168 | 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c,
|
9169 | 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908,
|
9170 | 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd,
|
9171 | 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b,
|
9172 | 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e,
|
9173 | 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa,
|
9174 | 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a,
|
9175 | 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d,
|
9176 | 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66,
|
9177 | 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5,
|
9178 | 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84,
|
9179 | 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96,
|
9180 | 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14,
|
9181 | 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca,
|
9182 | 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7,
|
9183 | 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77,
|
9184 | 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99,
|
9185 | 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054,
|
9186 | 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73,
|
9187 | 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea,
|
9188 | 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105,
|
9189 | 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646,
|
9190 | 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285,
|
9191 | 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea,
|
9192 | 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb,
|
9193 | 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e,
|
9194 | 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc,
|
9195 | 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd,
|
9196 | 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20,
|
9197 | 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7
|
9198 | ],
|
9199 | [
|
9200 | 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7,
|
9201 | 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af,
|
9202 | 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af,
|
9203 | 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504,
|
9204 | 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4,
|
9205 | 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee,
|
9206 | 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec,
|
9207 | 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b,
|
9208 | 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332,
|
9209 | 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527,
|
9210 | 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58,
|
9211 | 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c,
|
9212 | 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22,
|
9213 | 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17,
|
9214 | 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60,
|
9215 | 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115,
|
9216 | 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99,
|
9217 | 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0,
|
9218 | 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74,
|
9219 | 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d,
|
9220 | 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3,
|
9221 | 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3,
|
9222 | 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979,
|
9223 | 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c,
|
9224 | 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa,
|
9225 | 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a,
|
9226 | 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086,
|
9227 | 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc,
|
9228 | 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24,
|
9229 | 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2,
|
9230 | 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84,
|
9231 | 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c,
|
9232 | 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09,
|
9233 | 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10,
|
9234 | 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe,
|
9235 | 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027,
|
9236 | 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0,
|
9237 | 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634,
|
9238 | 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188,
|
9239 | 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc,
|
9240 | 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8,
|
9241 | 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837,
|
9242 | 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0
|
9243 | ],
|
9244 | [
|
9245 | 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742,
|
9246 | 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b,
|
9247 | 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79,
|
9248 | 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6,
|
9249 | 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a,
|
9250 | 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4,
|
9251 | 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1,
|
9252 | 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59,
|
9253 | 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797,
|
9254 | 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28,
|
9255 | 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6,
|
9256 | 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28,
|
9257 | 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba,
|
9258 | 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a,
|
9259 | 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5,
|
9260 | 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f,
|
9261 | 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce,
|
9262 | 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680,
|
9263 | 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd,
|
9264 | 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb,
|
9265 | 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb,
|
9266 | 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370,
|
9267 | 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc,
|
9268 | 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048,
|
9269 | 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc,
|
9270 | 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9,
|
9271 | 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a,
|
9272 | 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f,
|
9273 | 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a,
|
9274 | 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1,
|
9275 | 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b,
|
9276 | 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e,
|
9277 | 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e,
|
9278 | 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f,
|
9279 | 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623,
|
9280 | 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc,
|
9281 | 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a,
|
9282 | 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6,
|
9283 | 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3,
|
9284 | 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060,
|
9285 | 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c,
|
9286 | 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f,
|
9287 | 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6
|
9288 | ]
|
9289 | ];
|
9290 |
|
9291 |
|
9292 |
|
9293 |
|
9294 | Blowfish.prototype.PARRAY = [
|
9295 | 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0,
|
9296 | 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
|
9297 | 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b
|
9298 | ];
|
9299 |
|
9300 |
|
9301 |
|
9302 |
|
9303 | Blowfish.prototype.NN = 16;
|
9304 |
|
9305 |
|
9306 |
|
9307 |
|
9308 |
|
9309 |
|
9310 |
|
9311 |
|
9312 | Blowfish.prototype._clean = function(xx) {
|
9313 | if (xx < 0) {
|
9314 | const yy = xx & 0x7FFFFFFF;
|
9315 | xx = yy + 0x80000000;
|
9316 | }
|
9317 | return xx;
|
9318 | };
|
9319 |
|
9320 |
|
9321 |
|
9322 |
|
9323 | Blowfish.prototype._F = function(xx) {
|
9324 | let yy;
|
9325 |
|
9326 | const dd = xx & 0x00FF;
|
9327 | xx >>>= 8;
|
9328 | const cc = xx & 0x00FF;
|
9329 | xx >>>= 8;
|
9330 | const bb = xx & 0x00FF;
|
9331 | xx >>>= 8;
|
9332 | const aa = xx & 0x00FF;
|
9333 |
|
9334 | yy = this.sboxes[0][aa] + this.sboxes[1][bb];
|
9335 | yy ^= this.sboxes[2][cc];
|
9336 | yy += this.sboxes[3][dd];
|
9337 |
|
9338 | return yy;
|
9339 | };
|
9340 |
|
9341 |
|
9342 |
|
9343 |
|
9344 |
|
9345 | Blowfish.prototype._encrypt_block = function(vals) {
|
9346 | let dataL = vals[0];
|
9347 | let dataR = vals[1];
|
9348 |
|
9349 | let ii;
|
9350 |
|
9351 | for (ii = 0; ii < this.NN; ++ii) {
|
9352 | dataL ^= this.parray[ii];
|
9353 | dataR = this._F(dataL) ^ dataR;
|
9354 |
|
9355 | const tmp = dataL;
|
9356 | dataL = dataR;
|
9357 | dataR = tmp;
|
9358 | }
|
9359 |
|
9360 | dataL ^= this.parray[this.NN + 0];
|
9361 | dataR ^= this.parray[this.NN + 1];
|
9362 |
|
9363 | vals[0] = this._clean(dataR);
|
9364 | vals[1] = this._clean(dataL);
|
9365 | };
|
9366 |
|
9367 |
|
9368 |
|
9369 |
|
9370 |
|
9371 |
|
9372 |
|
9373 |
|
9374 |
|
9375 |
|
9376 | Blowfish.prototype.encrypt_block = function(vector) {
|
9377 | let ii;
|
9378 | const vals = [0, 0];
|
9379 | const off = this.BLOCKSIZE / 2;
|
9380 | for (ii = 0; ii < this.BLOCKSIZE / 2; ++ii) {
|
9381 | vals[0] = (vals[0] << 8) | (vector[ii + 0] & 0x00FF);
|
9382 | vals[1] = (vals[1] << 8) | (vector[ii + off] & 0x00FF);
|
9383 | }
|
9384 |
|
9385 | this._encrypt_block(vals);
|
9386 |
|
9387 | const ret = [];
|
9388 | for (ii = 0; ii < this.BLOCKSIZE / 2; ++ii) {
|
9389 | ret[ii + 0] = ((vals[0] >>> (24 - 8 * (ii))) & 0x00FF);
|
9390 | ret[ii + off] = ((vals[1] >>> (24 - 8 * (ii))) & 0x00FF);
|
9391 |
|
9392 |
|
9393 | }
|
9394 |
|
9395 | return ret;
|
9396 | };
|
9397 |
|
9398 |
|
9399 |
|
9400 |
|
9401 |
|
9402 | Blowfish.prototype._decrypt_block = function(vals) {
|
9403 | let dataL = vals[0];
|
9404 | let dataR = vals[1];
|
9405 |
|
9406 | let ii;
|
9407 |
|
9408 | for (ii = this.NN + 1; ii > 1; --ii) {
|
9409 | dataL ^= this.parray[ii];
|
9410 | dataR = this._F(dataL) ^ dataR;
|
9411 |
|
9412 | const tmp = dataL;
|
9413 | dataL = dataR;
|
9414 | dataR = tmp;
|
9415 | }
|
9416 |
|
9417 | dataL ^= this.parray[1];
|
9418 | dataR ^= this.parray[0];
|
9419 |
|
9420 | vals[0] = this._clean(dataR);
|
9421 | vals[1] = this._clean(dataL);
|
9422 | };
|
9423 |
|
9424 |
|
9425 |
|
9426 |
|
9427 |
|
9428 | Blowfish.prototype.init = function(key) {
|
9429 | let ii;
|
9430 | let jj = 0;
|
9431 |
|
9432 | this.parray = [];
|
9433 | for (ii = 0; ii < this.NN + 2; ++ii) {
|
9434 | let data = 0x00000000;
|
9435 | for (let kk = 0; kk < 4; ++kk) {
|
9436 | data = (data << 8) | (key[jj] & 0x00FF);
|
9437 | if (++jj >= key.length) {
|
9438 | jj = 0;
|
9439 | }
|
9440 | }
|
9441 | this.parray[ii] = this.PARRAY[ii] ^ data;
|
9442 | }
|
9443 |
|
9444 | this.sboxes = [];
|
9445 | for (ii = 0; ii < 4; ++ii) {
|
9446 | this.sboxes[ii] = [];
|
9447 | for (jj = 0; jj < 256; ++jj) {
|
9448 | this.sboxes[ii][jj] = this.SBOXES[ii][jj];
|
9449 | }
|
9450 | }
|
9451 |
|
9452 | const vals = [0x00000000, 0x00000000];
|
9453 |
|
9454 | for (ii = 0; ii < this.NN + 2; ii += 2) {
|
9455 | this._encrypt_block(vals);
|
9456 | this.parray[ii + 0] = vals[0];
|
9457 | this.parray[ii + 1] = vals[1];
|
9458 | }
|
9459 |
|
9460 | for (ii = 0; ii < 4; ++ii) {
|
9461 | for (jj = 0; jj < 256; jj += 2) {
|
9462 | this._encrypt_block(vals);
|
9463 | this.sboxes[ii][jj + 0] = vals[0];
|
9464 | this.sboxes[ii][jj + 1] = vals[1];
|
9465 | }
|
9466 | }
|
9467 | };
|
9468 |
|
9469 |
|
9470 | function BF(key) {
|
9471 | this.bf = new Blowfish();
|
9472 | this.bf.init(key);
|
9473 |
|
9474 | this.encrypt = function(block) {
|
9475 | return this.bf.encrypt_block(block);
|
9476 | };
|
9477 | }
|
9478 |
|
9479 | BF.keySize = BF.prototype.keySize = 16;
|
9480 | BF.blockSize = BF.prototype.blockSize = 8;
|
9481 |
|
9482 |
|
9483 |
|
9484 |
|
9485 |
|
9486 |
|
9487 |
|
9488 |
|
9489 |
|
9490 |
|
9491 |
|
9492 |
|
9493 |
|
9494 |
|
9495 |
|
9496 | const aes128 = aes(128);
|
9497 |
|
9498 |
|
9499 |
|
9500 |
|
9501 |
|
9502 |
|
9503 |
|
9504 |
|
9505 | const aes192 = aes(192);
|
9506 |
|
9507 |
|
9508 |
|
9509 |
|
9510 |
|
9511 |
|
9512 |
|
9513 |
|
9514 | const aes256 = aes(256);
|
9515 |
|
9516 | const des$1 = DES;
|
9517 |
|
9518 |
|
9519 |
|
9520 |
|
9521 |
|
9522 |
|
9523 |
|
9524 | const tripledes = TripleDES;
|
9525 |
|
9526 |
|
9527 |
|
9528 |
|
9529 |
|
9530 |
|
9531 |
|
9532 | const cast5 = Cast5;
|
9533 |
|
9534 |
|
9535 |
|
9536 |
|
9537 |
|
9538 |
|
9539 |
|
9540 | const twofish = TF;
|
9541 |
|
9542 |
|
9543 |
|
9544 |
|
9545 |
|
9546 |
|
9547 |
|
9548 | const blowfish = BF;
|
9549 |
|
9550 |
|
9551 |
|
9552 |
|
9553 |
|
9554 | const idea = function() {
|
9555 | throw new Error('IDEA symmetric-key algorithm not implemented');
|
9556 | };
|
9557 |
|
9558 | var cipher = Object.freeze({
|
9559 | __proto__: null,
|
9560 | aes128: aes128,
|
9561 | aes192: aes192,
|
9562 | aes256: aes256,
|
9563 | des: des$1,
|
9564 | tripledes: tripledes,
|
9565 | cast5: cast5,
|
9566 | twofish: twofish,
|
9567 | blowfish: blowfish,
|
9568 | idea: idea
|
9569 | });
|
9570 |
|
9571 | var sha1_asm = function ( stdlib, foreign, buffer ) {
|
9572 | "use asm";
|
9573 |
|
9574 |
|
9575 | var H0 = 0, H1 = 0, H2 = 0, H3 = 0, H4 = 0,
|
9576 | TOTAL0 = 0, TOTAL1 = 0;
|
9577 |
|
9578 |
|
9579 | var I0 = 0, I1 = 0, I2 = 0, I3 = 0, I4 = 0,
|
9580 | O0 = 0, O1 = 0, O2 = 0, O3 = 0, O4 = 0;
|
9581 |
|
9582 |
|
9583 | var HEAP = new stdlib.Uint8Array(buffer);
|
9584 |
|
9585 | function _core ( w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15 ) {
|
9586 | w0 = w0|0;
|
9587 | w1 = w1|0;
|
9588 | w2 = w2|0;
|
9589 | w3 = w3|0;
|
9590 | w4 = w4|0;
|
9591 | w5 = w5|0;
|
9592 | w6 = w6|0;
|
9593 | w7 = w7|0;
|
9594 | w8 = w8|0;
|
9595 | w9 = w9|0;
|
9596 | w10 = w10|0;
|
9597 | w11 = w11|0;
|
9598 | w12 = w12|0;
|
9599 | w13 = w13|0;
|
9600 | w14 = w14|0;
|
9601 | w15 = w15|0;
|
9602 |
|
9603 | var a = 0, b = 0, c = 0, d = 0, e = 0, n = 0, t = 0,
|
9604 | w16 = 0, w17 = 0, w18 = 0, w19 = 0,
|
9605 | w20 = 0, w21 = 0, w22 = 0, w23 = 0, w24 = 0, w25 = 0, w26 = 0, w27 = 0, w28 = 0, w29 = 0,
|
9606 | w30 = 0, w31 = 0, w32 = 0, w33 = 0, w34 = 0, w35 = 0, w36 = 0, w37 = 0, w38 = 0, w39 = 0,
|
9607 | w40 = 0, w41 = 0, w42 = 0, w43 = 0, w44 = 0, w45 = 0, w46 = 0, w47 = 0, w48 = 0, w49 = 0,
|
9608 | w50 = 0, w51 = 0, w52 = 0, w53 = 0, w54 = 0, w55 = 0, w56 = 0, w57 = 0, w58 = 0, w59 = 0,
|
9609 | w60 = 0, w61 = 0, w62 = 0, w63 = 0, w64 = 0, w65 = 0, w66 = 0, w67 = 0, w68 = 0, w69 = 0,
|
9610 | w70 = 0, w71 = 0, w72 = 0, w73 = 0, w74 = 0, w75 = 0, w76 = 0, w77 = 0, w78 = 0, w79 = 0;
|
9611 |
|
9612 | a = H0;
|
9613 | b = H1;
|
9614 | c = H2;
|
9615 | d = H3;
|
9616 | e = H4;
|
9617 |
|
9618 |
|
9619 | t = ( w0 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9620 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9621 |
|
9622 |
|
9623 | t = ( w1 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9624 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9625 |
|
9626 |
|
9627 | t = ( w2 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9628 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9629 |
|
9630 |
|
9631 | t = ( w3 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9632 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9633 |
|
9634 |
|
9635 | t = ( w4 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9636 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9637 |
|
9638 |
|
9639 | t = ( w5 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9640 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9641 |
|
9642 |
|
9643 | t = ( w6 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9644 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9645 |
|
9646 |
|
9647 | t = ( w7 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9648 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9649 |
|
9650 |
|
9651 | t = ( w8 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9652 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9653 |
|
9654 |
|
9655 | t = ( w9 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9656 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9657 |
|
9658 |
|
9659 | t = ( w10 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9660 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9661 |
|
9662 |
|
9663 | t = ( w11 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9664 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9665 |
|
9666 |
|
9667 | t = ( w12 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9668 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9669 |
|
9670 |
|
9671 | t = ( w13 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9672 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9673 |
|
9674 |
|
9675 | t = ( w14 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9676 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9677 |
|
9678 |
|
9679 | t = ( w15 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9680 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9681 |
|
9682 |
|
9683 | n = w13 ^ w8 ^ w2 ^ w0;
|
9684 | w16 = (n << 1) | (n >>> 31);
|
9685 | t = (w16 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9686 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9687 |
|
9688 |
|
9689 | n = w14 ^ w9 ^ w3 ^ w1;
|
9690 | w17 = (n << 1) | (n >>> 31);
|
9691 | t = (w17 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9692 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9693 |
|
9694 |
|
9695 | n = w15 ^ w10 ^ w4 ^ w2;
|
9696 | w18 = (n << 1) | (n >>> 31);
|
9697 | t = (w18 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9698 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9699 |
|
9700 |
|
9701 | n = w16 ^ w11 ^ w5 ^ w3;
|
9702 | w19 = (n << 1) | (n >>> 31);
|
9703 | t = (w19 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (~b & d)) + 0x5a827999 )|0;
|
9704 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9705 |
|
9706 |
|
9707 | n = w17 ^ w12 ^ w6 ^ w4;
|
9708 | w20 = (n << 1) | (n >>> 31);
|
9709 | t = (w20 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9710 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9711 |
|
9712 |
|
9713 | n = w18 ^ w13 ^ w7 ^ w5;
|
9714 | w21 = (n << 1) | (n >>> 31);
|
9715 | t = (w21 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9716 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9717 |
|
9718 |
|
9719 | n = w19 ^ w14 ^ w8 ^ w6;
|
9720 | w22 = (n << 1) | (n >>> 31);
|
9721 | t = (w22 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9722 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9723 |
|
9724 |
|
9725 | n = w20 ^ w15 ^ w9 ^ w7;
|
9726 | w23 = (n << 1) | (n >>> 31);
|
9727 | t = (w23 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9728 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9729 |
|
9730 |
|
9731 | n = w21 ^ w16 ^ w10 ^ w8;
|
9732 | w24 = (n << 1) | (n >>> 31);
|
9733 | t = (w24 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9734 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9735 |
|
9736 |
|
9737 | n = w22 ^ w17 ^ w11 ^ w9;
|
9738 | w25 = (n << 1) | (n >>> 31);
|
9739 | t = (w25 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9740 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9741 |
|
9742 |
|
9743 | n = w23 ^ w18 ^ w12 ^ w10;
|
9744 | w26 = (n << 1) | (n >>> 31);
|
9745 | t = (w26 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9746 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9747 |
|
9748 |
|
9749 | n = w24 ^ w19 ^ w13 ^ w11;
|
9750 | w27 = (n << 1) | (n >>> 31);
|
9751 | t = (w27 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9752 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9753 |
|
9754 |
|
9755 | n = w25 ^ w20 ^ w14 ^ w12;
|
9756 | w28 = (n << 1) | (n >>> 31);
|
9757 | t = (w28 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9758 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9759 |
|
9760 |
|
9761 | n = w26 ^ w21 ^ w15 ^ w13;
|
9762 | w29 = (n << 1) | (n >>> 31);
|
9763 | t = (w29 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9764 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9765 |
|
9766 |
|
9767 | n = w27 ^ w22 ^ w16 ^ w14;
|
9768 | w30 = (n << 1) | (n >>> 31);
|
9769 | t = (w30 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9770 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9771 |
|
9772 |
|
9773 | n = w28 ^ w23 ^ w17 ^ w15;
|
9774 | w31 = (n << 1) | (n >>> 31);
|
9775 | t = (w31 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9776 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9777 |
|
9778 |
|
9779 | n = w29 ^ w24 ^ w18 ^ w16;
|
9780 | w32 = (n << 1) | (n >>> 31);
|
9781 | t = (w32 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9782 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9783 |
|
9784 |
|
9785 | n = w30 ^ w25 ^ w19 ^ w17;
|
9786 | w33 = (n << 1) | (n >>> 31);
|
9787 | t = (w33 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9788 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9789 |
|
9790 |
|
9791 | n = w31 ^ w26 ^ w20 ^ w18;
|
9792 | w34 = (n << 1) | (n >>> 31);
|
9793 | t = (w34 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9794 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9795 |
|
9796 |
|
9797 | n = w32 ^ w27 ^ w21 ^ w19;
|
9798 | w35 = (n << 1) | (n >>> 31);
|
9799 | t = (w35 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9800 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9801 |
|
9802 |
|
9803 | n = w33 ^ w28 ^ w22 ^ w20;
|
9804 | w36 = (n << 1) | (n >>> 31);
|
9805 | t = (w36 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9806 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9807 |
|
9808 |
|
9809 | n = w34 ^ w29 ^ w23 ^ w21;
|
9810 | w37 = (n << 1) | (n >>> 31);
|
9811 | t = (w37 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9812 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9813 |
|
9814 |
|
9815 | n = w35 ^ w30 ^ w24 ^ w22;
|
9816 | w38 = (n << 1) | (n >>> 31);
|
9817 | t = (w38 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9818 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9819 |
|
9820 |
|
9821 | n = w36 ^ w31 ^ w25 ^ w23;
|
9822 | w39 = (n << 1) | (n >>> 31);
|
9823 | t = (w39 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) + 0x6ed9eba1 )|0;
|
9824 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9825 |
|
9826 |
|
9827 | n = w37 ^ w32 ^ w26 ^ w24;
|
9828 | w40 = (n << 1) | (n >>> 31);
|
9829 | t = (w40 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9830 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9831 |
|
9832 |
|
9833 | n = w38 ^ w33 ^ w27 ^ w25;
|
9834 | w41 = (n << 1) | (n >>> 31);
|
9835 | t = (w41 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9836 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9837 |
|
9838 |
|
9839 | n = w39 ^ w34 ^ w28 ^ w26;
|
9840 | w42 = (n << 1) | (n >>> 31);
|
9841 | t = (w42 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9842 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9843 |
|
9844 |
|
9845 | n = w40 ^ w35 ^ w29 ^ w27;
|
9846 | w43 = (n << 1) | (n >>> 31);
|
9847 | t = (w43 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9848 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9849 |
|
9850 |
|
9851 | n = w41 ^ w36 ^ w30 ^ w28;
|
9852 | w44 = (n << 1) | (n >>> 31);
|
9853 | t = (w44 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9854 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9855 |
|
9856 |
|
9857 | n = w42 ^ w37 ^ w31 ^ w29;
|
9858 | w45 = (n << 1) | (n >>> 31);
|
9859 | t = (w45 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9860 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9861 |
|
9862 |
|
9863 | n = w43 ^ w38 ^ w32 ^ w30;
|
9864 | w46 = (n << 1) | (n >>> 31);
|
9865 | t = (w46 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9866 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9867 |
|
9868 |
|
9869 | n = w44 ^ w39 ^ w33 ^ w31;
|
9870 | w47 = (n << 1) | (n >>> 31);
|
9871 | t = (w47 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9872 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9873 |
|
9874 |
|
9875 | n = w45 ^ w40 ^ w34 ^ w32;
|
9876 | w48 = (n << 1) | (n >>> 31);
|
9877 | t = (w48 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9878 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9879 |
|
9880 |
|
9881 | n = w46 ^ w41 ^ w35 ^ w33;
|
9882 | w49 = (n << 1) | (n >>> 31);
|
9883 | t = (w49 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9884 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9885 |
|
9886 |
|
9887 | n = w47 ^ w42 ^ w36 ^ w34;
|
9888 | w50 = (n << 1) | (n >>> 31);
|
9889 | t = (w50 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9890 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9891 |
|
9892 |
|
9893 | n = w48 ^ w43 ^ w37 ^ w35;
|
9894 | w51 = (n << 1) | (n >>> 31);
|
9895 | t = (w51 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9896 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9897 |
|
9898 |
|
9899 | n = w49 ^ w44 ^ w38 ^ w36;
|
9900 | w52 = (n << 1) | (n >>> 31);
|
9901 | t = (w52 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9902 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9903 |
|
9904 |
|
9905 | n = w50 ^ w45 ^ w39 ^ w37;
|
9906 | w53 = (n << 1) | (n >>> 31);
|
9907 | t = (w53 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9908 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9909 |
|
9910 |
|
9911 | n = w51 ^ w46 ^ w40 ^ w38;
|
9912 | w54 = (n << 1) | (n >>> 31);
|
9913 | t = (w54 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9914 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9915 |
|
9916 |
|
9917 | n = w52 ^ w47 ^ w41 ^ w39;
|
9918 | w55 = (n << 1) | (n >>> 31);
|
9919 | t = (w55 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9920 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9921 |
|
9922 |
|
9923 | n = w53 ^ w48 ^ w42 ^ w40;
|
9924 | w56 = (n << 1) | (n >>> 31);
|
9925 | t = (w56 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9926 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9927 |
|
9928 |
|
9929 | n = w54 ^ w49 ^ w43 ^ w41;
|
9930 | w57 = (n << 1) | (n >>> 31);
|
9931 | t = (w57 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9932 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9933 |
|
9934 |
|
9935 | n = w55 ^ w50 ^ w44 ^ w42;
|
9936 | w58 = (n << 1) | (n >>> 31);
|
9937 | t = (w58 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9938 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9939 |
|
9940 |
|
9941 | n = w56 ^ w51 ^ w45 ^ w43;
|
9942 | w59 = (n << 1) | (n >>> 31);
|
9943 | t = (w59 + ((a << 5) | (a >>> 27)) + e + ((b & c) | (b & d) | (c & d)) - 0x70e44324 )|0;
|
9944 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9945 |
|
9946 |
|
9947 | n = w57 ^ w52 ^ w46 ^ w44;
|
9948 | w60 = (n << 1) | (n >>> 31);
|
9949 | t = (w60 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9950 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9951 |
|
9952 |
|
9953 | n = w58 ^ w53 ^ w47 ^ w45;
|
9954 | w61 = (n << 1) | (n >>> 31);
|
9955 | t = (w61 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9956 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9957 |
|
9958 |
|
9959 | n = w59 ^ w54 ^ w48 ^ w46;
|
9960 | w62 = (n << 1) | (n >>> 31);
|
9961 | t = (w62 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9962 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9963 |
|
9964 |
|
9965 | n = w60 ^ w55 ^ w49 ^ w47;
|
9966 | w63 = (n << 1) | (n >>> 31);
|
9967 | t = (w63 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9968 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9969 |
|
9970 |
|
9971 | n = w61 ^ w56 ^ w50 ^ w48;
|
9972 | w64 = (n << 1) | (n >>> 31);
|
9973 | t = (w64 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9974 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9975 |
|
9976 |
|
9977 | n = w62 ^ w57 ^ w51 ^ w49;
|
9978 | w65 = (n << 1) | (n >>> 31);
|
9979 | t = (w65 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9980 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9981 |
|
9982 |
|
9983 | n = w63 ^ w58 ^ w52 ^ w50;
|
9984 | w66 = (n << 1) | (n >>> 31);
|
9985 | t = (w66 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9986 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9987 |
|
9988 |
|
9989 | n = w64 ^ w59 ^ w53 ^ w51;
|
9990 | w67 = (n << 1) | (n >>> 31);
|
9991 | t = (w67 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9992 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9993 |
|
9994 |
|
9995 | n = w65 ^ w60 ^ w54 ^ w52;
|
9996 | w68 = (n << 1) | (n >>> 31);
|
9997 | t = (w68 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
9998 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
9999 |
|
10000 |
|
10001 | n = w66 ^ w61 ^ w55 ^ w53;
|
10002 | w69 = (n << 1) | (n >>> 31);
|
10003 | t = (w69 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10004 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10005 |
|
10006 |
|
10007 | n = w67 ^ w62 ^ w56 ^ w54;
|
10008 | w70 = (n << 1) | (n >>> 31);
|
10009 | t = (w70 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10010 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10011 |
|
10012 |
|
10013 | n = w68 ^ w63 ^ w57 ^ w55;
|
10014 | w71 = (n << 1) | (n >>> 31);
|
10015 | t = (w71 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10016 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10017 |
|
10018 |
|
10019 | n = w69 ^ w64 ^ w58 ^ w56;
|
10020 | w72 = (n << 1) | (n >>> 31);
|
10021 | t = (w72 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10022 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10023 |
|
10024 |
|
10025 | n = w70 ^ w65 ^ w59 ^ w57;
|
10026 | w73 = (n << 1) | (n >>> 31);
|
10027 | t = (w73 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10028 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10029 |
|
10030 |
|
10031 | n = w71 ^ w66 ^ w60 ^ w58;
|
10032 | w74 = (n << 1) | (n >>> 31);
|
10033 | t = (w74 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10034 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10035 |
|
10036 |
|
10037 | n = w72 ^ w67 ^ w61 ^ w59;
|
10038 | w75 = (n << 1) | (n >>> 31);
|
10039 | t = (w75 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10040 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10041 |
|
10042 |
|
10043 | n = w73 ^ w68 ^ w62 ^ w60;
|
10044 | w76 = (n << 1) | (n >>> 31);
|
10045 | t = (w76 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10046 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10047 |
|
10048 |
|
10049 | n = w74 ^ w69 ^ w63 ^ w61;
|
10050 | w77 = (n << 1) | (n >>> 31);
|
10051 | t = (w77 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10052 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10053 |
|
10054 |
|
10055 | n = w75 ^ w70 ^ w64 ^ w62;
|
10056 | w78 = (n << 1) | (n >>> 31);
|
10057 | t = (w78 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10058 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10059 |
|
10060 |
|
10061 | n = w76 ^ w71 ^ w65 ^ w63;
|
10062 | w79 = (n << 1) | (n >>> 31);
|
10063 | t = (w79 + ((a << 5) | (a >>> 27)) + e + (b ^ c ^ d) - 0x359d3e2a )|0;
|
10064 | e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t;
|
10065 |
|
10066 | H0 = ( H0 + a )|0;
|
10067 | H1 = ( H1 + b )|0;
|
10068 | H2 = ( H2 + c )|0;
|
10069 | H3 = ( H3 + d )|0;
|
10070 | H4 = ( H4 + e )|0;
|
10071 |
|
10072 | }
|
10073 |
|
10074 | function _core_heap ( offset ) {
|
10075 | offset = offset|0;
|
10076 |
|
10077 | _core(
|
10078 | HEAP[offset|0]<<24 | HEAP[offset|1]<<16 | HEAP[offset|2]<<8 | HEAP[offset|3],
|
10079 | HEAP[offset|4]<<24 | HEAP[offset|5]<<16 | HEAP[offset|6]<<8 | HEAP[offset|7],
|
10080 | HEAP[offset|8]<<24 | HEAP[offset|9]<<16 | HEAP[offset|10]<<8 | HEAP[offset|11],
|
10081 | HEAP[offset|12]<<24 | HEAP[offset|13]<<16 | HEAP[offset|14]<<8 | HEAP[offset|15],
|
10082 | HEAP[offset|16]<<24 | HEAP[offset|17]<<16 | HEAP[offset|18]<<8 | HEAP[offset|19],
|
10083 | HEAP[offset|20]<<24 | HEAP[offset|21]<<16 | HEAP[offset|22]<<8 | HEAP[offset|23],
|
10084 | HEAP[offset|24]<<24 | HEAP[offset|25]<<16 | HEAP[offset|26]<<8 | HEAP[offset|27],
|
10085 | HEAP[offset|28]<<24 | HEAP[offset|29]<<16 | HEAP[offset|30]<<8 | HEAP[offset|31],
|
10086 | HEAP[offset|32]<<24 | HEAP[offset|33]<<16 | HEAP[offset|34]<<8 | HEAP[offset|35],
|
10087 | HEAP[offset|36]<<24 | HEAP[offset|37]<<16 | HEAP[offset|38]<<8 | HEAP[offset|39],
|
10088 | HEAP[offset|40]<<24 | HEAP[offset|41]<<16 | HEAP[offset|42]<<8 | HEAP[offset|43],
|
10089 | HEAP[offset|44]<<24 | HEAP[offset|45]<<16 | HEAP[offset|46]<<8 | HEAP[offset|47],
|
10090 | HEAP[offset|48]<<24 | HEAP[offset|49]<<16 | HEAP[offset|50]<<8 | HEAP[offset|51],
|
10091 | HEAP[offset|52]<<24 | HEAP[offset|53]<<16 | HEAP[offset|54]<<8 | HEAP[offset|55],
|
10092 | HEAP[offset|56]<<24 | HEAP[offset|57]<<16 | HEAP[offset|58]<<8 | HEAP[offset|59],
|
10093 | HEAP[offset|60]<<24 | HEAP[offset|61]<<16 | HEAP[offset|62]<<8 | HEAP[offset|63]
|
10094 | );
|
10095 | }
|
10096 |
|
10097 |
|
10098 | function _state_to_heap ( output ) {
|
10099 | output = output|0;
|
10100 |
|
10101 | HEAP[output|0] = H0>>>24;
|
10102 | HEAP[output|1] = H0>>>16&255;
|
10103 | HEAP[output|2] = H0>>>8&255;
|
10104 | HEAP[output|3] = H0&255;
|
10105 | HEAP[output|4] = H1>>>24;
|
10106 | HEAP[output|5] = H1>>>16&255;
|
10107 | HEAP[output|6] = H1>>>8&255;
|
10108 | HEAP[output|7] = H1&255;
|
10109 | HEAP[output|8] = H2>>>24;
|
10110 | HEAP[output|9] = H2>>>16&255;
|
10111 | HEAP[output|10] = H2>>>8&255;
|
10112 | HEAP[output|11] = H2&255;
|
10113 | HEAP[output|12] = H3>>>24;
|
10114 | HEAP[output|13] = H3>>>16&255;
|
10115 | HEAP[output|14] = H3>>>8&255;
|
10116 | HEAP[output|15] = H3&255;
|
10117 | HEAP[output|16] = H4>>>24;
|
10118 | HEAP[output|17] = H4>>>16&255;
|
10119 | HEAP[output|18] = H4>>>8&255;
|
10120 | HEAP[output|19] = H4&255;
|
10121 | }
|
10122 |
|
10123 | function reset () {
|
10124 | H0 = 0x67452301;
|
10125 | H1 = 0xefcdab89;
|
10126 | H2 = 0x98badcfe;
|
10127 | H3 = 0x10325476;
|
10128 | H4 = 0xc3d2e1f0;
|
10129 | TOTAL0 = TOTAL1 = 0;
|
10130 | }
|
10131 |
|
10132 | function init ( h0, h1, h2, h3, h4, total0, total1 ) {
|
10133 | h0 = h0|0;
|
10134 | h1 = h1|0;
|
10135 | h2 = h2|0;
|
10136 | h3 = h3|0;
|
10137 | h4 = h4|0;
|
10138 | total0 = total0|0;
|
10139 | total1 = total1|0;
|
10140 |
|
10141 | H0 = h0;
|
10142 | H1 = h1;
|
10143 | H2 = h2;
|
10144 | H3 = h3;
|
10145 | H4 = h4;
|
10146 | TOTAL0 = total0;
|
10147 | TOTAL1 = total1;
|
10148 | }
|
10149 |
|
10150 |
|
10151 | function process ( offset, length ) {
|
10152 | offset = offset|0;
|
10153 | length = length|0;
|
10154 |
|
10155 | var hashed = 0;
|
10156 |
|
10157 | if ( offset & 63 )
|
10158 | return -1;
|
10159 |
|
10160 | while ( (length|0) >= 64 ) {
|
10161 | _core_heap(offset);
|
10162 |
|
10163 | offset = ( offset + 64 )|0;
|
10164 | length = ( length - 64 )|0;
|
10165 |
|
10166 | hashed = ( hashed + 64 )|0;
|
10167 | }
|
10168 |
|
10169 | TOTAL0 = ( TOTAL0 + hashed )|0;
|
10170 | if ( TOTAL0>>>0 < hashed>>>0 ) TOTAL1 = ( TOTAL1 + 1 )|0;
|
10171 |
|
10172 | return hashed|0;
|
10173 | }
|
10174 |
|
10175 |
|
10176 |
|
10177 | function finish ( offset, length, output ) {
|
10178 | offset = offset|0;
|
10179 | length = length|0;
|
10180 | output = output|0;
|
10181 |
|
10182 | var hashed = 0,
|
10183 | i = 0;
|
10184 |
|
10185 | if ( offset & 63 )
|
10186 | return -1;
|
10187 |
|
10188 | if ( ~output )
|
10189 | if ( output & 31 )
|
10190 | return -1;
|
10191 |
|
10192 | if ( (length|0) >= 64 ) {
|
10193 | hashed = process( offset, length )|0;
|
10194 | if ( (hashed|0) == -1 )
|
10195 | return -1;
|
10196 |
|
10197 | offset = ( offset + hashed )|0;
|
10198 | length = ( length - hashed )|0;
|
10199 | }
|
10200 |
|
10201 | hashed = ( hashed + length )|0;
|
10202 | TOTAL0 = ( TOTAL0 + length )|0;
|
10203 | if ( TOTAL0>>>0 < length>>>0 ) TOTAL1 = (TOTAL1 + 1)|0;
|
10204 |
|
10205 | HEAP[offset|length] = 0x80;
|
10206 |
|
10207 | if ( (length|0) >= 56 ) {
|
10208 | for ( i = (length+1)|0; (i|0) < 64; i = (i+1)|0 )
|
10209 | HEAP[offset|i] = 0x00;
|
10210 | _core_heap(offset);
|
10211 |
|
10212 | length = 0;
|
10213 |
|
10214 | HEAP[offset|0] = 0;
|
10215 | }
|
10216 |
|
10217 | for ( i = (length+1)|0; (i|0) < 59; i = (i+1)|0 )
|
10218 | HEAP[offset|i] = 0;
|
10219 |
|
10220 | HEAP[offset|56] = TOTAL1>>>21&255;
|
10221 | HEAP[offset|57] = TOTAL1>>>13&255;
|
10222 | HEAP[offset|58] = TOTAL1>>>5&255;
|
10223 | HEAP[offset|59] = TOTAL1<<3&255 | TOTAL0>>>29;
|
10224 | HEAP[offset|60] = TOTAL0>>>21&255;
|
10225 | HEAP[offset|61] = TOTAL0>>>13&255;
|
10226 | HEAP[offset|62] = TOTAL0>>>5&255;
|
10227 | HEAP[offset|63] = TOTAL0<<3&255;
|
10228 | _core_heap(offset);
|
10229 |
|
10230 | if ( ~output )
|
10231 | _state_to_heap(output);
|
10232 |
|
10233 | return hashed|0;
|
10234 | }
|
10235 |
|
10236 | function hmac_reset () {
|
10237 | H0 = I0;
|
10238 | H1 = I1;
|
10239 | H2 = I2;
|
10240 | H3 = I3;
|
10241 | H4 = I4;
|
10242 | TOTAL0 = 64;
|
10243 | TOTAL1 = 0;
|
10244 | }
|
10245 |
|
10246 | function _hmac_opad () {
|
10247 | H0 = O0;
|
10248 | H1 = O1;
|
10249 | H2 = O2;
|
10250 | H3 = O3;
|
10251 | H4 = O4;
|
10252 | TOTAL0 = 64;
|
10253 | TOTAL1 = 0;
|
10254 | }
|
10255 |
|
10256 | function hmac_init ( p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 ) {
|
10257 | p0 = p0|0;
|
10258 | p1 = p1|0;
|
10259 | p2 = p2|0;
|
10260 | p3 = p3|0;
|
10261 | p4 = p4|0;
|
10262 | p5 = p5|0;
|
10263 | p6 = p6|0;
|
10264 | p7 = p7|0;
|
10265 | p8 = p8|0;
|
10266 | p9 = p9|0;
|
10267 | p10 = p10|0;
|
10268 | p11 = p11|0;
|
10269 | p12 = p12|0;
|
10270 | p13 = p13|0;
|
10271 | p14 = p14|0;
|
10272 | p15 = p15|0;
|
10273 |
|
10274 |
|
10275 | reset();
|
10276 | _core(
|
10277 | p0 ^ 0x5c5c5c5c,
|
10278 | p1 ^ 0x5c5c5c5c,
|
10279 | p2 ^ 0x5c5c5c5c,
|
10280 | p3 ^ 0x5c5c5c5c,
|
10281 | p4 ^ 0x5c5c5c5c,
|
10282 | p5 ^ 0x5c5c5c5c,
|
10283 | p6 ^ 0x5c5c5c5c,
|
10284 | p7 ^ 0x5c5c5c5c,
|
10285 | p8 ^ 0x5c5c5c5c,
|
10286 | p9 ^ 0x5c5c5c5c,
|
10287 | p10 ^ 0x5c5c5c5c,
|
10288 | p11 ^ 0x5c5c5c5c,
|
10289 | p12 ^ 0x5c5c5c5c,
|
10290 | p13 ^ 0x5c5c5c5c,
|
10291 | p14 ^ 0x5c5c5c5c,
|
10292 | p15 ^ 0x5c5c5c5c
|
10293 | );
|
10294 | O0 = H0;
|
10295 | O1 = H1;
|
10296 | O2 = H2;
|
10297 | O3 = H3;
|
10298 | O4 = H4;
|
10299 |
|
10300 |
|
10301 | reset();
|
10302 | _core(
|
10303 | p0 ^ 0x36363636,
|
10304 | p1 ^ 0x36363636,
|
10305 | p2 ^ 0x36363636,
|
10306 | p3 ^ 0x36363636,
|
10307 | p4 ^ 0x36363636,
|
10308 | p5 ^ 0x36363636,
|
10309 | p6 ^ 0x36363636,
|
10310 | p7 ^ 0x36363636,
|
10311 | p8 ^ 0x36363636,
|
10312 | p9 ^ 0x36363636,
|
10313 | p10 ^ 0x36363636,
|
10314 | p11 ^ 0x36363636,
|
10315 | p12 ^ 0x36363636,
|
10316 | p13 ^ 0x36363636,
|
10317 | p14 ^ 0x36363636,
|
10318 | p15 ^ 0x36363636
|
10319 | );
|
10320 | I0 = H0;
|
10321 | I1 = H1;
|
10322 | I2 = H2;
|
10323 | I3 = H3;
|
10324 | I4 = H4;
|
10325 |
|
10326 | TOTAL0 = 64;
|
10327 | TOTAL1 = 0;
|
10328 | }
|
10329 |
|
10330 |
|
10331 |
|
10332 | function hmac_finish ( offset, length, output ) {
|
10333 | offset = offset|0;
|
10334 | length = length|0;
|
10335 | output = output|0;
|
10336 |
|
10337 | var t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, hashed = 0;
|
10338 |
|
10339 | if ( offset & 63 )
|
10340 | return -1;
|
10341 |
|
10342 | if ( ~output )
|
10343 | if ( output & 31 )
|
10344 | return -1;
|
10345 |
|
10346 | hashed = finish( offset, length, -1 )|0;
|
10347 | t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4;
|
10348 |
|
10349 | _hmac_opad();
|
10350 | _core( t0, t1, t2, t3, t4, 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 672 );
|
10351 |
|
10352 | if ( ~output )
|
10353 | _state_to_heap(output);
|
10354 |
|
10355 | return hashed|0;
|
10356 | }
|
10357 |
|
10358 |
|
10359 |
|
10360 |
|
10361 | function pbkdf2_generate_block ( offset, length, block, count, output ) {
|
10362 | offset = offset|0;
|
10363 | length = length|0;
|
10364 | block = block|0;
|
10365 | count = count|0;
|
10366 | output = output|0;
|
10367 |
|
10368 | var h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0,
|
10369 | t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0;
|
10370 |
|
10371 | if ( offset & 63 )
|
10372 | return -1;
|
10373 |
|
10374 | if ( ~output )
|
10375 | if ( output & 31 )
|
10376 | return -1;
|
10377 |
|
10378 |
|
10379 |
|
10380 | HEAP[(offset+length)|0] = block>>>24;
|
10381 | HEAP[(offset+length+1)|0] = block>>>16&255;
|
10382 | HEAP[(offset+length+2)|0] = block>>>8&255;
|
10383 | HEAP[(offset+length+3)|0] = block&255;
|
10384 |
|
10385 |
|
10386 | hmac_finish( offset, (length+4)|0, -1 )|0;
|
10387 | h0 = t0 = H0, h1 = t1 = H1, h2 = t2 = H2, h3 = t3 = H3, h4 = t4 = H4;
|
10388 | count = (count-1)|0;
|
10389 |
|
10390 |
|
10391 | while ( (count|0) > 0 ) {
|
10392 | hmac_reset();
|
10393 | _core( t0, t1, t2, t3, t4, 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 672 );
|
10394 | t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4;
|
10395 |
|
10396 | _hmac_opad();
|
10397 | _core( t0, t1, t2, t3, t4, 0x80000000, 0, 0, 0, 0, 0, 0, 0, 0, 0, 672 );
|
10398 | t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4;
|
10399 |
|
10400 | h0 = h0 ^ H0;
|
10401 | h1 = h1 ^ H1;
|
10402 | h2 = h2 ^ H2;
|
10403 | h3 = h3 ^ H3;
|
10404 | h4 = h4 ^ H4;
|
10405 |
|
10406 | count = (count-1)|0;
|
10407 | }
|
10408 |
|
10409 | H0 = h0;
|
10410 | H1 = h1;
|
10411 | H2 = h2;
|
10412 | H3 = h3;
|
10413 | H4 = h4;
|
10414 |
|
10415 | if ( ~output )
|
10416 | _state_to_heap(output);
|
10417 |
|
10418 | return 0;
|
10419 | }
|
10420 |
|
10421 | return {
|
10422 |
|
10423 | reset: reset,
|
10424 | init: init,
|
10425 | process: process,
|
10426 | finish: finish,
|
10427 |
|
10428 |
|
10429 | hmac_reset: hmac_reset,
|
10430 | hmac_init: hmac_init,
|
10431 | hmac_finish: hmac_finish,
|
10432 |
|
10433 |
|
10434 | pbkdf2_generate_block: pbkdf2_generate_block
|
10435 | }
|
10436 | };
|
10437 |
|
10438 | class Hash {
|
10439 | constructor() {
|
10440 | this.pos = 0;
|
10441 | this.len = 0;
|
10442 | }
|
10443 | reset() {
|
10444 | const { asm } = this.acquire_asm();
|
10445 | this.result = null;
|
10446 | this.pos = 0;
|
10447 | this.len = 0;
|
10448 | asm.reset();
|
10449 | return this;
|
10450 | }
|
10451 | process(data) {
|
10452 | if (this.result !== null)
|
10453 | throw new IllegalStateError('state must be reset before processing new data');
|
10454 | const { asm, heap } = this.acquire_asm();
|
10455 | let hpos = this.pos;
|
10456 | let hlen = this.len;
|
10457 | let dpos = 0;
|
10458 | let dlen = data.length;
|
10459 | let wlen = 0;
|
10460 | while (dlen > 0) {
|
10461 | wlen = _heap_write(heap, hpos + hlen, data, dpos, dlen);
|
10462 | hlen += wlen;
|
10463 | dpos += wlen;
|
10464 | dlen -= wlen;
|
10465 | wlen = asm.process(hpos, hlen);
|
10466 | hpos += wlen;
|
10467 | hlen -= wlen;
|
10468 | if (!hlen)
|
10469 | hpos = 0;
|
10470 | }
|
10471 | this.pos = hpos;
|
10472 | this.len = hlen;
|
10473 | return this;
|
10474 | }
|
10475 | finish() {
|
10476 | if (this.result !== null)
|
10477 | throw new IllegalStateError('state must be reset before processing new data');
|
10478 | const { asm, heap } = this.acquire_asm();
|
10479 | asm.finish(this.pos, this.len, 0);
|
10480 | this.result = new Uint8Array(this.HASH_SIZE);
|
10481 | this.result.set(heap.subarray(0, this.HASH_SIZE));
|
10482 | this.pos = 0;
|
10483 | this.len = 0;
|
10484 | this.release_asm();
|
10485 | return this;
|
10486 | }
|
10487 | }
|
10488 |
|
10489 | const _sha1_block_size = 64;
|
10490 | const _sha1_hash_size = 20;
|
10491 | const heap_pool$1 = [];
|
10492 | const asm_pool$1 = [];
|
10493 | class Sha1 extends Hash {
|
10494 | constructor() {
|
10495 | super();
|
10496 | this.NAME = 'sha1';
|
10497 | this.BLOCK_SIZE = _sha1_block_size;
|
10498 | this.HASH_SIZE = _sha1_hash_size;
|
10499 | this.acquire_asm();
|
10500 | }
|
10501 | acquire_asm() {
|
10502 | if (this.heap === undefined || this.asm === undefined) {
|
10503 | this.heap = heap_pool$1.pop() || _heap_init();
|
10504 | this.asm = asm_pool$1.pop() || sha1_asm({ Uint8Array: Uint8Array }, null, this.heap.buffer);
|
10505 | this.reset();
|
10506 | }
|
10507 | return { heap: this.heap, asm: this.asm };
|
10508 | }
|
10509 | release_asm() {
|
10510 | if (this.heap !== undefined && this.asm !== undefined) {
|
10511 | heap_pool$1.push(this.heap);
|
10512 | asm_pool$1.push(this.asm);
|
10513 | }
|
10514 | this.heap = undefined;
|
10515 | this.asm = undefined;
|
10516 | }
|
10517 | static bytes(data) {
|
10518 | return new Sha1().process(data).finish().result;
|
10519 | }
|
10520 | }
|
10521 | Sha1.NAME = 'sha1';
|
10522 | Sha1.heap_pool = [];
|
10523 | Sha1.asm_pool = [];
|
10524 | Sha1.asm_function = sha1_asm;
|
10525 |
|
10526 | var sha256_asm = function ( stdlib, foreign, buffer ) {
|
10527 | "use asm";
|
10528 |
|
10529 |
|
10530 | var H0 = 0, H1 = 0, H2 = 0, H3 = 0, H4 = 0, H5 = 0, H6 = 0, H7 = 0,
|
10531 | TOTAL0 = 0, TOTAL1 = 0;
|
10532 |
|
10533 |
|
10534 | var I0 = 0, I1 = 0, I2 = 0, I3 = 0, I4 = 0, I5 = 0, I6 = 0, I7 = 0,
|
10535 | O0 = 0, O1 = 0, O2 = 0, O3 = 0, O4 = 0, O5 = 0, O6 = 0, O7 = 0;
|
10536 |
|
10537 |
|
10538 | var HEAP = new stdlib.Uint8Array(buffer);
|
10539 |
|
10540 | function _core ( w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15 ) {
|
10541 | w0 = w0|0;
|
10542 | w1 = w1|0;
|
10543 | w2 = w2|0;
|
10544 | w3 = w3|0;
|
10545 | w4 = w4|0;
|
10546 | w5 = w5|0;
|
10547 | w6 = w6|0;
|
10548 | w7 = w7|0;
|
10549 | w8 = w8|0;
|
10550 | w9 = w9|0;
|
10551 | w10 = w10|0;
|
10552 | w11 = w11|0;
|
10553 | w12 = w12|0;
|
10554 | w13 = w13|0;
|
10555 | w14 = w14|0;
|
10556 | w15 = w15|0;
|
10557 |
|
10558 | var a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
|
10559 |
|
10560 | a = H0;
|
10561 | b = H1;
|
10562 | c = H2;
|
10563 | d = H3;
|
10564 | e = H4;
|
10565 | f = H5;
|
10566 | g = H6;
|
10567 | h = H7;
|
10568 |
|
10569 |
|
10570 | h = ( w0 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x428a2f98 )|0;
|
10571 | d = ( d + h )|0;
|
10572 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10573 |
|
10574 |
|
10575 | g = ( w1 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0x71374491 )|0;
|
10576 | c = ( c + g )|0;
|
10577 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10578 |
|
10579 |
|
10580 | f = ( w2 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0xb5c0fbcf )|0;
|
10581 | b = ( b + f )|0;
|
10582 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10583 |
|
10584 |
|
10585 | e = ( w3 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0xe9b5dba5 )|0;
|
10586 | a = ( a + e )|0;
|
10587 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10588 |
|
10589 |
|
10590 | d = ( w4 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0x3956c25b )|0;
|
10591 | h = ( h + d )|0;
|
10592 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10593 |
|
10594 |
|
10595 | c = ( w5 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0x59f111f1 )|0;
|
10596 | g = ( g + c )|0;
|
10597 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10598 |
|
10599 |
|
10600 | b = ( w6 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0x923f82a4 )|0;
|
10601 | f = ( f + b )|0;
|
10602 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10603 |
|
10604 |
|
10605 | a = ( w7 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0xab1c5ed5 )|0;
|
10606 | e = ( e + a )|0;
|
10607 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10608 |
|
10609 |
|
10610 | h = ( w8 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xd807aa98 )|0;
|
10611 | d = ( d + h )|0;
|
10612 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10613 |
|
10614 |
|
10615 | g = ( w9 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0x12835b01 )|0;
|
10616 | c = ( c + g )|0;
|
10617 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10618 |
|
10619 |
|
10620 | f = ( w10 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0x243185be )|0;
|
10621 | b = ( b + f )|0;
|
10622 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10623 |
|
10624 |
|
10625 | e = ( w11 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0x550c7dc3 )|0;
|
10626 | a = ( a + e )|0;
|
10627 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10628 |
|
10629 |
|
10630 | d = ( w12 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0x72be5d74 )|0;
|
10631 | h = ( h + d )|0;
|
10632 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10633 |
|
10634 |
|
10635 | c = ( w13 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0x80deb1fe )|0;
|
10636 | g = ( g + c )|0;
|
10637 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10638 |
|
10639 |
|
10640 | b = ( w14 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0x9bdc06a7 )|0;
|
10641 | f = ( f + b )|0;
|
10642 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10643 |
|
10644 |
|
10645 | a = ( w15 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0xc19bf174 )|0;
|
10646 | e = ( e + a )|0;
|
10647 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10648 |
|
10649 |
|
10650 | w0 = ( ( w1>>>7 ^ w1>>>18 ^ w1>>>3 ^ w1<<25 ^ w1<<14 ) + ( w14>>>17 ^ w14>>>19 ^ w14>>>10 ^ w14<<15 ^ w14<<13 ) + w0 + w9 )|0;
|
10651 | h = ( w0 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xe49b69c1 )|0;
|
10652 | d = ( d + h )|0;
|
10653 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10654 |
|
10655 |
|
10656 | w1 = ( ( w2>>>7 ^ w2>>>18 ^ w2>>>3 ^ w2<<25 ^ w2<<14 ) + ( w15>>>17 ^ w15>>>19 ^ w15>>>10 ^ w15<<15 ^ w15<<13 ) + w1 + w10 )|0;
|
10657 | g = ( w1 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0xefbe4786 )|0;
|
10658 | c = ( c + g )|0;
|
10659 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10660 |
|
10661 |
|
10662 | w2 = ( ( w3>>>7 ^ w3>>>18 ^ w3>>>3 ^ w3<<25 ^ w3<<14 ) + ( w0>>>17 ^ w0>>>19 ^ w0>>>10 ^ w0<<15 ^ w0<<13 ) + w2 + w11 )|0;
|
10663 | f = ( w2 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0x0fc19dc6 )|0;
|
10664 | b = ( b + f )|0;
|
10665 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10666 |
|
10667 |
|
10668 | w3 = ( ( w4>>>7 ^ w4>>>18 ^ w4>>>3 ^ w4<<25 ^ w4<<14 ) + ( w1>>>17 ^ w1>>>19 ^ w1>>>10 ^ w1<<15 ^ w1<<13 ) + w3 + w12 )|0;
|
10669 | e = ( w3 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0x240ca1cc )|0;
|
10670 | a = ( a + e )|0;
|
10671 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10672 |
|
10673 |
|
10674 | w4 = ( ( w5>>>7 ^ w5>>>18 ^ w5>>>3 ^ w5<<25 ^ w5<<14 ) + ( w2>>>17 ^ w2>>>19 ^ w2>>>10 ^ w2<<15 ^ w2<<13 ) + w4 + w13 )|0;
|
10675 | d = ( w4 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0x2de92c6f )|0;
|
10676 | h = ( h + d )|0;
|
10677 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10678 |
|
10679 |
|
10680 | w5 = ( ( w6>>>7 ^ w6>>>18 ^ w6>>>3 ^ w6<<25 ^ w6<<14 ) + ( w3>>>17 ^ w3>>>19 ^ w3>>>10 ^ w3<<15 ^ w3<<13 ) + w5 + w14 )|0;
|
10681 | c = ( w5 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0x4a7484aa )|0;
|
10682 | g = ( g + c )|0;
|
10683 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10684 |
|
10685 |
|
10686 | w6 = ( ( w7>>>7 ^ w7>>>18 ^ w7>>>3 ^ w7<<25 ^ w7<<14 ) + ( w4>>>17 ^ w4>>>19 ^ w4>>>10 ^ w4<<15 ^ w4<<13 ) + w6 + w15 )|0;
|
10687 | b = ( w6 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0x5cb0a9dc )|0;
|
10688 | f = ( f + b )|0;
|
10689 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10690 |
|
10691 |
|
10692 | w7 = ( ( w8>>>7 ^ w8>>>18 ^ w8>>>3 ^ w8<<25 ^ w8<<14 ) + ( w5>>>17 ^ w5>>>19 ^ w5>>>10 ^ w5<<15 ^ w5<<13 ) + w7 + w0 )|0;
|
10693 | a = ( w7 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0x76f988da )|0;
|
10694 | e = ( e + a )|0;
|
10695 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10696 |
|
10697 |
|
10698 | w8 = ( ( w9>>>7 ^ w9>>>18 ^ w9>>>3 ^ w9<<25 ^ w9<<14 ) + ( w6>>>17 ^ w6>>>19 ^ w6>>>10 ^ w6<<15 ^ w6<<13 ) + w8 + w1 )|0;
|
10699 | h = ( w8 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x983e5152 )|0;
|
10700 | d = ( d + h )|0;
|
10701 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10702 |
|
10703 |
|
10704 | w9 = ( ( w10>>>7 ^ w10>>>18 ^ w10>>>3 ^ w10<<25 ^ w10<<14 ) + ( w7>>>17 ^ w7>>>19 ^ w7>>>10 ^ w7<<15 ^ w7<<13 ) + w9 + w2 )|0;
|
10705 | g = ( w9 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0xa831c66d )|0;
|
10706 | c = ( c + g )|0;
|
10707 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10708 |
|
10709 |
|
10710 | w10 = ( ( w11>>>7 ^ w11>>>18 ^ w11>>>3 ^ w11<<25 ^ w11<<14 ) + ( w8>>>17 ^ w8>>>19 ^ w8>>>10 ^ w8<<15 ^ w8<<13 ) + w10 + w3 )|0;
|
10711 | f = ( w10 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0xb00327c8 )|0;
|
10712 | b = ( b + f )|0;
|
10713 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10714 |
|
10715 |
|
10716 | w11 = ( ( w12>>>7 ^ w12>>>18 ^ w12>>>3 ^ w12<<25 ^ w12<<14 ) + ( w9>>>17 ^ w9>>>19 ^ w9>>>10 ^ w9<<15 ^ w9<<13 ) + w11 + w4 )|0;
|
10717 | e = ( w11 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0xbf597fc7 )|0;
|
10718 | a = ( a + e )|0;
|
10719 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10720 |
|
10721 |
|
10722 | w12 = ( ( w13>>>7 ^ w13>>>18 ^ w13>>>3 ^ w13<<25 ^ w13<<14 ) + ( w10>>>17 ^ w10>>>19 ^ w10>>>10 ^ w10<<15 ^ w10<<13 ) + w12 + w5 )|0;
|
10723 | d = ( w12 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0xc6e00bf3 )|0;
|
10724 | h = ( h + d )|0;
|
10725 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10726 |
|
10727 |
|
10728 | w13 = ( ( w14>>>7 ^ w14>>>18 ^ w14>>>3 ^ w14<<25 ^ w14<<14 ) + ( w11>>>17 ^ w11>>>19 ^ w11>>>10 ^ w11<<15 ^ w11<<13 ) + w13 + w6 )|0;
|
10729 | c = ( w13 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0xd5a79147 )|0;
|
10730 | g = ( g + c )|0;
|
10731 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10732 |
|
10733 |
|
10734 | w14 = ( ( w15>>>7 ^ w15>>>18 ^ w15>>>3 ^ w15<<25 ^ w15<<14 ) + ( w12>>>17 ^ w12>>>19 ^ w12>>>10 ^ w12<<15 ^ w12<<13 ) + w14 + w7 )|0;
|
10735 | b = ( w14 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0x06ca6351 )|0;
|
10736 | f = ( f + b )|0;
|
10737 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10738 |
|
10739 |
|
10740 | w15 = ( ( w0>>>7 ^ w0>>>18 ^ w0>>>3 ^ w0<<25 ^ w0<<14 ) + ( w13>>>17 ^ w13>>>19 ^ w13>>>10 ^ w13<<15 ^ w13<<13 ) + w15 + w8 )|0;
|
10741 | a = ( w15 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0x14292967 )|0;
|
10742 | e = ( e + a )|0;
|
10743 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10744 |
|
10745 |
|
10746 | w0 = ( ( w1>>>7 ^ w1>>>18 ^ w1>>>3 ^ w1<<25 ^ w1<<14 ) + ( w14>>>17 ^ w14>>>19 ^ w14>>>10 ^ w14<<15 ^ w14<<13 ) + w0 + w9 )|0;
|
10747 | h = ( w0 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x27b70a85 )|0;
|
10748 | d = ( d + h )|0;
|
10749 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10750 |
|
10751 |
|
10752 | w1 = ( ( w2>>>7 ^ w2>>>18 ^ w2>>>3 ^ w2<<25 ^ w2<<14 ) + ( w15>>>17 ^ w15>>>19 ^ w15>>>10 ^ w15<<15 ^ w15<<13 ) + w1 + w10 )|0;
|
10753 | g = ( w1 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0x2e1b2138 )|0;
|
10754 | c = ( c + g )|0;
|
10755 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10756 |
|
10757 |
|
10758 | w2 = ( ( w3>>>7 ^ w3>>>18 ^ w3>>>3 ^ w3<<25 ^ w3<<14 ) + ( w0>>>17 ^ w0>>>19 ^ w0>>>10 ^ w0<<15 ^ w0<<13 ) + w2 + w11 )|0;
|
10759 | f = ( w2 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0x4d2c6dfc )|0;
|
10760 | b = ( b + f )|0;
|
10761 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10762 |
|
10763 |
|
10764 | w3 = ( ( w4>>>7 ^ w4>>>18 ^ w4>>>3 ^ w4<<25 ^ w4<<14 ) + ( w1>>>17 ^ w1>>>19 ^ w1>>>10 ^ w1<<15 ^ w1<<13 ) + w3 + w12 )|0;
|
10765 | e = ( w3 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0x53380d13 )|0;
|
10766 | a = ( a + e )|0;
|
10767 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10768 |
|
10769 |
|
10770 | w4 = ( ( w5>>>7 ^ w5>>>18 ^ w5>>>3 ^ w5<<25 ^ w5<<14 ) + ( w2>>>17 ^ w2>>>19 ^ w2>>>10 ^ w2<<15 ^ w2<<13 ) + w4 + w13 )|0;
|
10771 | d = ( w4 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0x650a7354 )|0;
|
10772 | h = ( h + d )|0;
|
10773 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10774 |
|
10775 |
|
10776 | w5 = ( ( w6>>>7 ^ w6>>>18 ^ w6>>>3 ^ w6<<25 ^ w6<<14 ) + ( w3>>>17 ^ w3>>>19 ^ w3>>>10 ^ w3<<15 ^ w3<<13 ) + w5 + w14 )|0;
|
10777 | c = ( w5 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0x766a0abb )|0;
|
10778 | g = ( g + c )|0;
|
10779 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10780 |
|
10781 |
|
10782 | w6 = ( ( w7>>>7 ^ w7>>>18 ^ w7>>>3 ^ w7<<25 ^ w7<<14 ) + ( w4>>>17 ^ w4>>>19 ^ w4>>>10 ^ w4<<15 ^ w4<<13 ) + w6 + w15 )|0;
|
10783 | b = ( w6 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0x81c2c92e )|0;
|
10784 | f = ( f + b )|0;
|
10785 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10786 |
|
10787 |
|
10788 | w7 = ( ( w8>>>7 ^ w8>>>18 ^ w8>>>3 ^ w8<<25 ^ w8<<14 ) + ( w5>>>17 ^ w5>>>19 ^ w5>>>10 ^ w5<<15 ^ w5<<13 ) + w7 + w0 )|0;
|
10789 | a = ( w7 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0x92722c85 )|0;
|
10790 | e = ( e + a )|0;
|
10791 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10792 |
|
10793 |
|
10794 | w8 = ( ( w9>>>7 ^ w9>>>18 ^ w9>>>3 ^ w9<<25 ^ w9<<14 ) + ( w6>>>17 ^ w6>>>19 ^ w6>>>10 ^ w6<<15 ^ w6<<13 ) + w8 + w1 )|0;
|
10795 | h = ( w8 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xa2bfe8a1 )|0;
|
10796 | d = ( d + h )|0;
|
10797 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10798 |
|
10799 |
|
10800 | w9 = ( ( w10>>>7 ^ w10>>>18 ^ w10>>>3 ^ w10<<25 ^ w10<<14 ) + ( w7>>>17 ^ w7>>>19 ^ w7>>>10 ^ w7<<15 ^ w7<<13 ) + w9 + w2 )|0;
|
10801 | g = ( w9 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0xa81a664b )|0;
|
10802 | c = ( c + g )|0;
|
10803 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10804 |
|
10805 |
|
10806 | w10 = ( ( w11>>>7 ^ w11>>>18 ^ w11>>>3 ^ w11<<25 ^ w11<<14 ) + ( w8>>>17 ^ w8>>>19 ^ w8>>>10 ^ w8<<15 ^ w8<<13 ) + w10 + w3 )|0;
|
10807 | f = ( w10 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0xc24b8b70 )|0;
|
10808 | b = ( b + f )|0;
|
10809 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10810 |
|
10811 |
|
10812 | w11 = ( ( w12>>>7 ^ w12>>>18 ^ w12>>>3 ^ w12<<25 ^ w12<<14 ) + ( w9>>>17 ^ w9>>>19 ^ w9>>>10 ^ w9<<15 ^ w9<<13 ) + w11 + w4 )|0;
|
10813 | e = ( w11 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0xc76c51a3 )|0;
|
10814 | a = ( a + e )|0;
|
10815 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10816 |
|
10817 |
|
10818 | w12 = ( ( w13>>>7 ^ w13>>>18 ^ w13>>>3 ^ w13<<25 ^ w13<<14 ) + ( w10>>>17 ^ w10>>>19 ^ w10>>>10 ^ w10<<15 ^ w10<<13 ) + w12 + w5 )|0;
|
10819 | d = ( w12 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0xd192e819 )|0;
|
10820 | h = ( h + d )|0;
|
10821 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10822 |
|
10823 |
|
10824 | w13 = ( ( w14>>>7 ^ w14>>>18 ^ w14>>>3 ^ w14<<25 ^ w14<<14 ) + ( w11>>>17 ^ w11>>>19 ^ w11>>>10 ^ w11<<15 ^ w11<<13 ) + w13 + w6 )|0;
|
10825 | c = ( w13 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0xd6990624 )|0;
|
10826 | g = ( g + c )|0;
|
10827 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10828 |
|
10829 |
|
10830 | w14 = ( ( w15>>>7 ^ w15>>>18 ^ w15>>>3 ^ w15<<25 ^ w15<<14 ) + ( w12>>>17 ^ w12>>>19 ^ w12>>>10 ^ w12<<15 ^ w12<<13 ) + w14 + w7 )|0;
|
10831 | b = ( w14 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0xf40e3585 )|0;
|
10832 | f = ( f + b )|0;
|
10833 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10834 |
|
10835 |
|
10836 | w15 = ( ( w0>>>7 ^ w0>>>18 ^ w0>>>3 ^ w0<<25 ^ w0<<14 ) + ( w13>>>17 ^ w13>>>19 ^ w13>>>10 ^ w13<<15 ^ w13<<13 ) + w15 + w8 )|0;
|
10837 | a = ( w15 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0x106aa070 )|0;
|
10838 | e = ( e + a )|0;
|
10839 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10840 |
|
10841 |
|
10842 | w0 = ( ( w1>>>7 ^ w1>>>18 ^ w1>>>3 ^ w1<<25 ^ w1<<14 ) + ( w14>>>17 ^ w14>>>19 ^ w14>>>10 ^ w14<<15 ^ w14<<13 ) + w0 + w9 )|0;
|
10843 | h = ( w0 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x19a4c116 )|0;
|
10844 | d = ( d + h )|0;
|
10845 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10846 |
|
10847 |
|
10848 | w1 = ( ( w2>>>7 ^ w2>>>18 ^ w2>>>3 ^ w2<<25 ^ w2<<14 ) + ( w15>>>17 ^ w15>>>19 ^ w15>>>10 ^ w15<<15 ^ w15<<13 ) + w1 + w10 )|0;
|
10849 | g = ( w1 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0x1e376c08 )|0;
|
10850 | c = ( c + g )|0;
|
10851 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10852 |
|
10853 |
|
10854 | w2 = ( ( w3>>>7 ^ w3>>>18 ^ w3>>>3 ^ w3<<25 ^ w3<<14 ) + ( w0>>>17 ^ w0>>>19 ^ w0>>>10 ^ w0<<15 ^ w0<<13 ) + w2 + w11 )|0;
|
10855 | f = ( w2 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0x2748774c )|0;
|
10856 | b = ( b + f )|0;
|
10857 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10858 |
|
10859 |
|
10860 | w3 = ( ( w4>>>7 ^ w4>>>18 ^ w4>>>3 ^ w4<<25 ^ w4<<14 ) + ( w1>>>17 ^ w1>>>19 ^ w1>>>10 ^ w1<<15 ^ w1<<13 ) + w3 + w12 )|0;
|
10861 | e = ( w3 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0x34b0bcb5 )|0;
|
10862 | a = ( a + e )|0;
|
10863 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10864 |
|
10865 |
|
10866 | w4 = ( ( w5>>>7 ^ w5>>>18 ^ w5>>>3 ^ w5<<25 ^ w5<<14 ) + ( w2>>>17 ^ w2>>>19 ^ w2>>>10 ^ w2<<15 ^ w2<<13 ) + w4 + w13 )|0;
|
10867 | d = ( w4 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0x391c0cb3 )|0;
|
10868 | h = ( h + d )|0;
|
10869 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10870 |
|
10871 |
|
10872 | w5 = ( ( w6>>>7 ^ w6>>>18 ^ w6>>>3 ^ w6<<25 ^ w6<<14 ) + ( w3>>>17 ^ w3>>>19 ^ w3>>>10 ^ w3<<15 ^ w3<<13 ) + w5 + w14 )|0;
|
10873 | c = ( w5 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0x4ed8aa4a )|0;
|
10874 | g = ( g + c )|0;
|
10875 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10876 |
|
10877 |
|
10878 | w6 = ( ( w7>>>7 ^ w7>>>18 ^ w7>>>3 ^ w7<<25 ^ w7<<14 ) + ( w4>>>17 ^ w4>>>19 ^ w4>>>10 ^ w4<<15 ^ w4<<13 ) + w6 + w15 )|0;
|
10879 | b = ( w6 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0x5b9cca4f )|0;
|
10880 | f = ( f + b )|0;
|
10881 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10882 |
|
10883 |
|
10884 | w7 = ( ( w8>>>7 ^ w8>>>18 ^ w8>>>3 ^ w8<<25 ^ w8<<14 ) + ( w5>>>17 ^ w5>>>19 ^ w5>>>10 ^ w5<<15 ^ w5<<13 ) + w7 + w0 )|0;
|
10885 | a = ( w7 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0x682e6ff3 )|0;
|
10886 | e = ( e + a )|0;
|
10887 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10888 |
|
10889 |
|
10890 | w8 = ( ( w9>>>7 ^ w9>>>18 ^ w9>>>3 ^ w9<<25 ^ w9<<14 ) + ( w6>>>17 ^ w6>>>19 ^ w6>>>10 ^ w6<<15 ^ w6<<13 ) + w8 + w1 )|0;
|
10891 | h = ( w8 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x748f82ee )|0;
|
10892 | d = ( d + h )|0;
|
10893 | h = ( h + ( (a & b) ^ ( c & (a ^ b) ) ) + ( a>>>2 ^ a>>>13 ^ a>>>22 ^ a<<30 ^ a<<19 ^ a<<10 ) )|0;
|
10894 |
|
10895 |
|
10896 | w9 = ( ( w10>>>7 ^ w10>>>18 ^ w10>>>3 ^ w10<<25 ^ w10<<14 ) + ( w7>>>17 ^ w7>>>19 ^ w7>>>10 ^ w7<<15 ^ w7<<13 ) + w9 + w2 )|0;
|
10897 | g = ( w9 + g + ( d>>>6 ^ d>>>11 ^ d>>>25 ^ d<<26 ^ d<<21 ^ d<<7 ) + ( f ^ d & (e^f) ) + 0x78a5636f )|0;
|
10898 | c = ( c + g )|0;
|
10899 | g = ( g + ( (h & a) ^ ( b & (h ^ a) ) ) + ( h>>>2 ^ h>>>13 ^ h>>>22 ^ h<<30 ^ h<<19 ^ h<<10 ) )|0;
|
10900 |
|
10901 |
|
10902 | w10 = ( ( w11>>>7 ^ w11>>>18 ^ w11>>>3 ^ w11<<25 ^ w11<<14 ) + ( w8>>>17 ^ w8>>>19 ^ w8>>>10 ^ w8<<15 ^ w8<<13 ) + w10 + w3 )|0;
|
10903 | f = ( w10 + f + ( c>>>6 ^ c>>>11 ^ c>>>25 ^ c<<26 ^ c<<21 ^ c<<7 ) + ( e ^ c & (d^e) ) + 0x84c87814 )|0;
|
10904 | b = ( b + f )|0;
|
10905 | f = ( f + ( (g & h) ^ ( a & (g ^ h) ) ) + ( g>>>2 ^ g>>>13 ^ g>>>22 ^ g<<30 ^ g<<19 ^ g<<10 ) )|0;
|
10906 |
|
10907 |
|
10908 | w11 = ( ( w12>>>7 ^ w12>>>18 ^ w12>>>3 ^ w12<<25 ^ w12<<14 ) + ( w9>>>17 ^ w9>>>19 ^ w9>>>10 ^ w9<<15 ^ w9<<13 ) + w11 + w4 )|0;
|
10909 | e = ( w11 + e + ( b>>>6 ^ b>>>11 ^ b>>>25 ^ b<<26 ^ b<<21 ^ b<<7 ) + ( d ^ b & (c^d) ) + 0x8cc70208 )|0;
|
10910 | a = ( a + e )|0;
|
10911 | e = ( e + ( (f & g) ^ ( h & (f ^ g) ) ) + ( f>>>2 ^ f>>>13 ^ f>>>22 ^ f<<30 ^ f<<19 ^ f<<10 ) )|0;
|
10912 |
|
10913 |
|
10914 | w12 = ( ( w13>>>7 ^ w13>>>18 ^ w13>>>3 ^ w13<<25 ^ w13<<14 ) + ( w10>>>17 ^ w10>>>19 ^ w10>>>10 ^ w10<<15 ^ w10<<13 ) + w12 + w5 )|0;
|
10915 | d = ( w12 + d + ( a>>>6 ^ a>>>11 ^ a>>>25 ^ a<<26 ^ a<<21 ^ a<<7 ) + ( c ^ a & (b^c) ) + 0x90befffa )|0;
|
10916 | h = ( h + d )|0;
|
10917 | d = ( d + ( (e & f) ^ ( g & (e ^ f) ) ) + ( e>>>2 ^ e>>>13 ^ e>>>22 ^ e<<30 ^ e<<19 ^ e<<10 ) )|0;
|
10918 |
|
10919 |
|
10920 | w13 = ( ( w14>>>7 ^ w14>>>18 ^ w14>>>3 ^ w14<<25 ^ w14<<14 ) + ( w11>>>17 ^ w11>>>19 ^ w11>>>10 ^ w11<<15 ^ w11<<13 ) + w13 + w6 )|0;
|
10921 | c = ( w13 + c + ( h>>>6 ^ h>>>11 ^ h>>>25 ^ h<<26 ^ h<<21 ^ h<<7 ) + ( b ^ h & (a^b) ) + 0xa4506ceb )|0;
|
10922 | g = ( g + c )|0;
|
10923 | c = ( c + ( (d & e) ^ ( f & (d ^ e) ) ) + ( d>>>2 ^ d>>>13 ^ d>>>22 ^ d<<30 ^ d<<19 ^ d<<10 ) )|0;
|
10924 |
|
10925 |
|
10926 | w14 = ( ( w15>>>7 ^ w15>>>18 ^ w15>>>3 ^ w15<<25 ^ w15<<14 ) + ( w12>>>17 ^ w12>>>19 ^ w12>>>10 ^ w12<<15 ^ w12<<13 ) + w14 + w7 )|0;
|
10927 | b = ( w14 + b + ( g>>>6 ^ g>>>11 ^ g>>>25 ^ g<<26 ^ g<<21 ^ g<<7 ) + ( a ^ g & (h^a) ) + 0xbef9a3f7 )|0;
|
10928 | f = ( f + b )|0;
|
10929 | b = ( b + ( (c & d) ^ ( e & (c ^ d) ) ) + ( c>>>2 ^ c>>>13 ^ c>>>22 ^ c<<30 ^ c<<19 ^ c<<10 ) )|0;
|
10930 |
|
10931 |
|
10932 | w15 = ( ( w0>>>7 ^ w0>>>18 ^ w0>>>3 ^ w0<<25 ^ w0<<14 ) + ( w13>>>17 ^ w13>>>19 ^ w13>>>10 ^ w13<<15 ^ w13<<13 ) + w15 + w8 )|0;
|
10933 | a = ( w15 + a + ( f>>>6 ^ f>>>11 ^ f>>>25 ^ f<<26 ^ f<<21 ^ f<<7 ) + ( h ^ f & (g^h) ) + 0xc67178f2 )|0;
|
10934 | e = ( e + a )|0;
|
10935 | a = ( a + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
|
10936 |
|
10937 | H0 = ( H0 + a )|0;
|
10938 | H1 = ( H1 + b )|0;
|
10939 | H2 = ( H2 + c )|0;
|
10940 | H3 = ( H3 + d )|0;
|
10941 | H4 = ( H4 + e )|0;
|
10942 | H5 = ( H5 + f )|0;
|
10943 | H6 = ( H6 + g )|0;
|
10944 | H7 = ( H7 + h )|0;
|
10945 | }
|
10946 |
|
10947 | function _core_heap ( offset ) {
|
10948 | offset = offset|0;
|
10949 |
|
10950 | _core(
|
10951 | HEAP[offset|0]<<24 | HEAP[offset|1]<<16 | HEAP[offset|2]<<8 | HEAP[offset|3],
|
10952 | HEAP[offset|4]<<24 | HEAP[offset|5]<<16 | HEAP[offset|6]<<8 | HEAP[offset|7],
|
10953 | HEAP[offset|8]<<24 | HEAP[offset|9]<<16 | HEAP[offset|10]<<8 | HEAP[offset|11],
|
10954 | HEAP[offset|12]<<24 | HEAP[offset|13]<<16 | HEAP[offset|14]<<8 | HEAP[offset|15],
|
10955 | HEAP[offset|16]<<24 | HEAP[offset|17]<<16 | HEAP[offset|18]<<8 | HEAP[offset|19],
|
10956 | HEAP[offset|20]<<24 | HEAP[offset|21]<<16 | HEAP[offset|22]<<8 | HEAP[offset|23],
|
10957 | HEAP[offset|24]<<24 | HEAP[offset|25]<<16 | HEAP[offset|26]<<8 | HEAP[offset|27],
|
10958 | HEAP[offset|28]<<24 | HEAP[offset|29]<<16 | HEAP[offset|30]<<8 | HEAP[offset|31],
|
10959 | HEAP[offset|32]<<24 | HEAP[offset|33]<<16 | HEAP[offset|34]<<8 | HEAP[offset|35],
|
10960 | HEAP[offset|36]<<24 | HEAP[offset|37]<<16 | HEAP[offset|38]<<8 | HEAP[offset|39],
|
10961 | HEAP[offset|40]<<24 | HEAP[offset|41]<<16 | HEAP[offset|42]<<8 | HEAP[offset|43],
|
10962 | HEAP[offset|44]<<24 | HEAP[offset|45]<<16 | HEAP[offset|46]<<8 | HEAP[offset|47],
|
10963 | HEAP[offset|48]<<24 | HEAP[offset|49]<<16 | HEAP[offset|50]<<8 | HEAP[offset|51],
|
10964 | HEAP[offset|52]<<24 | HEAP[offset|53]<<16 | HEAP[offset|54]<<8 | HEAP[offset|55],
|
10965 | HEAP[offset|56]<<24 | HEAP[offset|57]<<16 | HEAP[offset|58]<<8 | HEAP[offset|59],
|
10966 | HEAP[offset|60]<<24 | HEAP[offset|61]<<16 | HEAP[offset|62]<<8 | HEAP[offset|63]
|
10967 | );
|
10968 | }
|
10969 |
|
10970 |
|
10971 | function _state_to_heap ( output ) {
|
10972 | output = output|0;
|
10973 |
|
10974 | HEAP[output|0] = H0>>>24;
|
10975 | HEAP[output|1] = H0>>>16&255;
|
10976 | HEAP[output|2] = H0>>>8&255;
|
10977 | HEAP[output|3] = H0&255;
|
10978 | HEAP[output|4] = H1>>>24;
|
10979 | HEAP[output|5] = H1>>>16&255;
|
10980 | HEAP[output|6] = H1>>>8&255;
|
10981 | HEAP[output|7] = H1&255;
|
10982 | HEAP[output|8] = H2>>>24;
|
10983 | HEAP[output|9] = H2>>>16&255;
|
10984 | HEAP[output|10] = H2>>>8&255;
|
10985 | HEAP[output|11] = H2&255;
|
10986 | HEAP[output|12] = H3>>>24;
|
10987 | HEAP[output|13] = H3>>>16&255;
|
10988 | HEAP[output|14] = H3>>>8&255;
|
10989 | HEAP[output|15] = H3&255;
|
10990 | HEAP[output|16] = H4>>>24;
|
10991 | HEAP[output|17] = H4>>>16&255;
|
10992 | HEAP[output|18] = H4>>>8&255;
|
10993 | HEAP[output|19] = H4&255;
|
10994 | HEAP[output|20] = H5>>>24;
|
10995 | HEAP[output|21] = H5>>>16&255;
|
10996 | HEAP[output|22] = H5>>>8&255;
|
10997 | HEAP[output|23] = H5&255;
|
10998 | HEAP[output|24] = H6>>>24;
|
10999 | HEAP[output|25] = H6>>>16&255;
|
11000 | HEAP[output|26] = H6>>>8&255;
|
11001 | HEAP[output|27] = H6&255;
|
11002 | HEAP[output|28] = H7>>>24;
|
11003 | HEAP[output|29] = H7>>>16&255;
|
11004 | HEAP[output|30] = H7>>>8&255;
|
11005 | HEAP[output|31] = H7&255;
|
11006 | }
|
11007 |
|
11008 | function reset () {
|
11009 | H0 = 0x6a09e667;
|
11010 | H1 = 0xbb67ae85;
|
11011 | H2 = 0x3c6ef372;
|
11012 | H3 = 0xa54ff53a;
|
11013 | H4 = 0x510e527f;
|
11014 | H5 = 0x9b05688c;
|
11015 | H6 = 0x1f83d9ab;
|
11016 | H7 = 0x5be0cd19;
|
11017 | TOTAL0 = TOTAL1 = 0;
|
11018 | }
|
11019 |
|
11020 | function init ( h0, h1, h2, h3, h4, h5, h6, h7, total0, total1 ) {
|
11021 | h0 = h0|0;
|
11022 | h1 = h1|0;
|
11023 | h2 = h2|0;
|
11024 | h3 = h3|0;
|
11025 | h4 = h4|0;
|
11026 | h5 = h5|0;
|
11027 | h6 = h6|0;
|
11028 | h7 = h7|0;
|
11029 | total0 = total0|0;
|
11030 | total1 = total1|0;
|
11031 |
|
11032 | H0 = h0;
|
11033 | H1 = h1;
|
11034 | H2 = h2;
|
11035 | H3 = h3;
|
11036 | H4 = h4;
|
11037 | H5 = h5;
|
11038 | H6 = h6;
|
11039 | H7 = h7;
|
11040 | TOTAL0 = total0;
|
11041 | TOTAL1 = total1;
|
11042 | }
|
11043 |
|
11044 |
|
11045 | function process ( offset, length ) {
|
11046 | offset = offset|0;
|
11047 | length = length|0;
|
11048 |
|
11049 | var hashed = 0;
|
11050 |
|
11051 | if ( offset & 63 )
|
11052 | return -1;
|
11053 |
|
11054 | while ( (length|0) >= 64 ) {
|
11055 | _core_heap(offset);
|
11056 |
|
11057 | offset = ( offset + 64 )|0;
|
11058 | length = ( length - 64 )|0;
|
11059 |
|
11060 | hashed = ( hashed + 64 )|0;
|
11061 | }
|
11062 |
|
11063 | TOTAL0 = ( TOTAL0 + hashed )|0;
|
11064 | if ( TOTAL0>>>0 < hashed>>>0 ) TOTAL1 = ( TOTAL1 + 1 )|0;
|
11065 |
|
11066 | return hashed|0;
|
11067 | }
|
11068 |
|
11069 |
|
11070 |
|
11071 | function finish ( offset, length, output ) {
|
11072 | offset = offset|0;
|
11073 | length = length|0;
|
11074 | output = output|0;
|
11075 |
|
11076 | var hashed = 0,
|
11077 | i = 0;
|
11078 |
|
11079 | if ( offset & 63 )
|
11080 | return -1;
|
11081 |
|
11082 | if ( ~output )
|
11083 | if ( output & 31 )
|
11084 | return -1;
|
11085 |
|
11086 | if ( (length|0) >= 64 ) {
|
11087 | hashed = process( offset, length )|0;
|
11088 | if ( (hashed|0) == -1 )
|
11089 | return -1;
|
11090 |
|
11091 | offset = ( offset + hashed )|0;
|
11092 | length = ( length - hashed )|0;
|
11093 | }
|
11094 |
|
11095 | hashed = ( hashed + length )|0;
|
11096 | TOTAL0 = ( TOTAL0 + length )|0;
|
11097 | if ( TOTAL0>>>0 < length>>>0 ) TOTAL1 = ( TOTAL1 + 1 )|0;
|
11098 |
|
11099 | HEAP[offset|length] = 0x80;
|
11100 |
|
11101 | if ( (length|0) >= 56 ) {
|
11102 | for ( i = (length+1)|0; (i|0) < 64; i = (i+1)|0 )
|
11103 | HEAP[offset|i] = 0x00;
|
11104 |
|
11105 | _core_heap(offset);
|
11106 |
|
11107 | length = 0;
|
11108 |
|
11109 | HEAP[offset|0] = 0;
|
11110 | }
|
11111 |
|
11112 | for ( i = (length+1)|0; (i|0) < 59; i = (i+1)|0 )
|
11113 | HEAP[offset|i] = 0;
|
11114 |
|
11115 | HEAP[offset|56] = TOTAL1>>>21&255;
|
11116 | HEAP[offset|57] = TOTAL1>>>13&255;
|
11117 | HEAP[offset|58] = TOTAL1>>>5&255;
|
11118 | HEAP[offset|59] = TOTAL1<<3&255 | TOTAL0>>>29;
|
11119 | HEAP[offset|60] = TOTAL0>>>21&255;
|
11120 | HEAP[offset|61] = TOTAL0>>>13&255;
|
11121 | HEAP[offset|62] = TOTAL0>>>5&255;
|
11122 | HEAP[offset|63] = TOTAL0<<3&255;
|
11123 | _core_heap(offset);
|
11124 |
|
11125 | if ( ~output )
|
11126 | _state_to_heap(output);
|
11127 |
|
11128 | return hashed|0;
|
11129 | }
|
11130 |
|
11131 | function hmac_reset () {
|
11132 | H0 = I0;
|
11133 | H1 = I1;
|
11134 | H2 = I2;
|
11135 | H3 = I3;
|
11136 | H4 = I4;
|
11137 | H5 = I5;
|
11138 | H6 = I6;
|
11139 | H7 = I7;
|
11140 | TOTAL0 = 64;
|
11141 | TOTAL1 = 0;
|
11142 | }
|
11143 |
|
11144 | function _hmac_opad () {
|
11145 | H0 = O0;
|
11146 | H1 = O1;
|
11147 | H2 = O2;
|
11148 | H3 = O3;
|
11149 | H4 = O4;
|
11150 | H5 = O5;
|
11151 | H6 = O6;
|
11152 | H7 = O7;
|
11153 | TOTAL0 = 64;
|
11154 | TOTAL1 = 0;
|
11155 | }
|
11156 |
|
11157 | function hmac_init ( p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 ) {
|
11158 | p0 = p0|0;
|
11159 | p1 = p1|0;
|
11160 | p2 = p2|0;
|
11161 | p3 = p3|0;
|
11162 | p4 = p4|0;
|
11163 | p5 = p5|0;
|
11164 | p6 = p6|0;
|
11165 | p7 = p7|0;
|
11166 | p8 = p8|0;
|
11167 | p9 = p9|0;
|
11168 | p10 = p10|0;
|
11169 | p11 = p11|0;
|
11170 | p12 = p12|0;
|
11171 | p13 = p13|0;
|
11172 | p14 = p14|0;
|
11173 | p15 = p15|0;
|
11174 |
|
11175 |
|
11176 | reset();
|
11177 | _core(
|
11178 | p0 ^ 0x5c5c5c5c,
|
11179 | p1 ^ 0x5c5c5c5c,
|
11180 | p2 ^ 0x5c5c5c5c,
|
11181 | p3 ^ 0x5c5c5c5c,
|
11182 | p4 ^ 0x5c5c5c5c,
|
11183 | p5 ^ 0x5c5c5c5c,
|
11184 | p6 ^ 0x5c5c5c5c,
|
11185 | p7 ^ 0x5c5c5c5c,
|
11186 | p8 ^ 0x5c5c5c5c,
|
11187 | p9 ^ 0x5c5c5c5c,
|
11188 | p10 ^ 0x5c5c5c5c,
|
11189 | p11 ^ 0x5c5c5c5c,
|
11190 | p12 ^ 0x5c5c5c5c,
|
11191 | p13 ^ 0x5c5c5c5c,
|
11192 | p14 ^ 0x5c5c5c5c,
|
11193 | p15 ^ 0x5c5c5c5c
|
11194 | );
|
11195 | O0 = H0;
|
11196 | O1 = H1;
|
11197 | O2 = H2;
|
11198 | O3 = H3;
|
11199 | O4 = H4;
|
11200 | O5 = H5;
|
11201 | O6 = H6;
|
11202 | O7 = H7;
|
11203 |
|
11204 |
|
11205 | reset();
|
11206 | _core(
|
11207 | p0 ^ 0x36363636,
|
11208 | p1 ^ 0x36363636,
|
11209 | p2 ^ 0x36363636,
|
11210 | p3 ^ 0x36363636,
|
11211 | p4 ^ 0x36363636,
|
11212 | p5 ^ 0x36363636,
|
11213 | p6 ^ 0x36363636,
|
11214 | p7 ^ 0x36363636,
|
11215 | p8 ^ 0x36363636,
|
11216 | p9 ^ 0x36363636,
|
11217 | p10 ^ 0x36363636,
|
11218 | p11 ^ 0x36363636,
|
11219 | p12 ^ 0x36363636,
|
11220 | p13 ^ 0x36363636,
|
11221 | p14 ^ 0x36363636,
|
11222 | p15 ^ 0x36363636
|
11223 | );
|
11224 | I0 = H0;
|
11225 | I1 = H1;
|
11226 | I2 = H2;
|
11227 | I3 = H3;
|
11228 | I4 = H4;
|
11229 | I5 = H5;
|
11230 | I6 = H6;
|
11231 | I7 = H7;
|
11232 |
|
11233 | TOTAL0 = 64;
|
11234 | TOTAL1 = 0;
|
11235 | }
|
11236 |
|
11237 |
|
11238 |
|
11239 | function hmac_finish ( offset, length, output ) {
|
11240 | offset = offset|0;
|
11241 | length = length|0;
|
11242 | output = output|0;
|
11243 |
|
11244 | var t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
|
11245 | hashed = 0;
|
11246 |
|
11247 | if ( offset & 63 )
|
11248 | return -1;
|
11249 |
|
11250 | if ( ~output )
|
11251 | if ( output & 31 )
|
11252 | return -1;
|
11253 |
|
11254 | hashed = finish( offset, length, -1 )|0;
|
11255 | t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4, t5 = H5, t6 = H6, t7 = H7;
|
11256 |
|
11257 | _hmac_opad();
|
11258 | _core( t0, t1, t2, t3, t4, t5, t6, t7, 0x80000000, 0, 0, 0, 0, 0, 0, 768 );
|
11259 |
|
11260 | if ( ~output )
|
11261 | _state_to_heap(output);
|
11262 |
|
11263 | return hashed|0;
|
11264 | }
|
11265 |
|
11266 |
|
11267 |
|
11268 |
|
11269 | function pbkdf2_generate_block ( offset, length, block, count, output ) {
|
11270 | offset = offset|0;
|
11271 | length = length|0;
|
11272 | block = block|0;
|
11273 | count = count|0;
|
11274 | output = output|0;
|
11275 |
|
11276 | var h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0, h7 = 0,
|
11277 | t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0;
|
11278 |
|
11279 | if ( offset & 63 )
|
11280 | return -1;
|
11281 |
|
11282 | if ( ~output )
|
11283 | if ( output & 31 )
|
11284 | return -1;
|
11285 |
|
11286 |
|
11287 |
|
11288 | HEAP[(offset+length)|0] = block>>>24;
|
11289 | HEAP[(offset+length+1)|0] = block>>>16&255;
|
11290 | HEAP[(offset+length+2)|0] = block>>>8&255;
|
11291 | HEAP[(offset+length+3)|0] = block&255;
|
11292 |
|
11293 |
|
11294 | hmac_finish( offset, (length+4)|0, -1 )|0;
|
11295 | h0 = t0 = H0, h1 = t1 = H1, h2 = t2 = H2, h3 = t3 = H3, h4 = t4 = H4, h5 = t5 = H5, h6 = t6 = H6, h7 = t7 = H7;
|
11296 | count = (count-1)|0;
|
11297 |
|
11298 |
|
11299 | while ( (count|0) > 0 ) {
|
11300 | hmac_reset();
|
11301 | _core( t0, t1, t2, t3, t4, t5, t6, t7, 0x80000000, 0, 0, 0, 0, 0, 0, 768 );
|
11302 | t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4, t5 = H5, t6 = H6, t7 = H7;
|
11303 |
|
11304 | _hmac_opad();
|
11305 | _core( t0, t1, t2, t3, t4, t5, t6, t7, 0x80000000, 0, 0, 0, 0, 0, 0, 768 );
|
11306 | t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4, t5 = H5, t6 = H6, t7 = H7;
|
11307 |
|
11308 | h0 = h0 ^ H0;
|
11309 | h1 = h1 ^ H1;
|
11310 | h2 = h2 ^ H2;
|
11311 | h3 = h3 ^ H3;
|
11312 | h4 = h4 ^ H4;
|
11313 | h5 = h5 ^ H5;
|
11314 | h6 = h6 ^ H6;
|
11315 | h7 = h7 ^ H7;
|
11316 |
|
11317 | count = (count-1)|0;
|
11318 | }
|
11319 |
|
11320 | H0 = h0;
|
11321 | H1 = h1;
|
11322 | H2 = h2;
|
11323 | H3 = h3;
|
11324 | H4 = h4;
|
11325 | H5 = h5;
|
11326 | H6 = h6;
|
11327 | H7 = h7;
|
11328 |
|
11329 | if ( ~output )
|
11330 | _state_to_heap(output);
|
11331 |
|
11332 | return 0;
|
11333 | }
|
11334 |
|
11335 | return {
|
11336 |
|
11337 | reset: reset,
|
11338 | init: init,
|
11339 | process: process,
|
11340 | finish: finish,
|
11341 |
|
11342 |
|
11343 | hmac_reset: hmac_reset,
|
11344 | hmac_init: hmac_init,
|
11345 | hmac_finish: hmac_finish,
|
11346 |
|
11347 |
|
11348 | pbkdf2_generate_block: pbkdf2_generate_block
|
11349 | }
|
11350 | };
|
11351 |
|
11352 | const _sha256_block_size = 64;
|
11353 | const _sha256_hash_size = 32;
|
11354 | const heap_pool$2 = [];
|
11355 | const asm_pool$2 = [];
|
11356 | class Sha256 extends Hash {
|
11357 | constructor() {
|
11358 | super();
|
11359 | this.NAME = 'sha256';
|
11360 | this.BLOCK_SIZE = _sha256_block_size;
|
11361 | this.HASH_SIZE = _sha256_hash_size;
|
11362 | this.acquire_asm();
|
11363 | }
|
11364 | acquire_asm() {
|
11365 | if (this.heap === undefined || this.asm === undefined) {
|
11366 | this.heap = heap_pool$2.pop() || _heap_init();
|
11367 | this.asm = asm_pool$2.pop() || sha256_asm({ Uint8Array: Uint8Array }, null, this.heap.buffer);
|
11368 | this.reset();
|
11369 | }
|
11370 | return { heap: this.heap, asm: this.asm };
|
11371 | }
|
11372 | release_asm() {
|
11373 | if (this.heap !== undefined && this.asm !== undefined) {
|
11374 | heap_pool$2.push(this.heap);
|
11375 | asm_pool$2.push(this.asm);
|
11376 | }
|
11377 | this.heap = undefined;
|
11378 | this.asm = undefined;
|
11379 | }
|
11380 | static bytes(data) {
|
11381 | return new Sha256().process(data).finish().result;
|
11382 | }
|
11383 | }
|
11384 | Sha256.NAME = 'sha256';
|
11385 |
|
11386 | var minimalisticAssert = assert$1;
|
11387 |
|
11388 | function assert$1(val, msg) {
|
11389 | if (!val)
|
11390 | throw new Error(msg || 'Assertion failed');
|
11391 | }
|
11392 |
|
11393 | assert$1.equal = function assertEqual(l, r, msg) {
|
11394 | if (l != r)
|
11395 | throw new Error(msg || ('Assertion failed: ' + l + ' != ' + r));
|
11396 | };
|
11397 |
|
11398 | var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
11399 |
|
11400 | function createCommonjsModule(fn, module) {
|
11401 | return module = { exports: {} }, fn(module, module.exports), module.exports;
|
11402 | }
|
11403 |
|
11404 | function commonjsRequire () {
|
11405 | throw new Error('Dynamic requires are not currently supported by @rollup/plugin-commonjs');
|
11406 | }
|
11407 |
|
11408 | var inherits_browser = createCommonjsModule(function (module) {
|
11409 | if (typeof Object.create === 'function') {
|
11410 |
|
11411 | module.exports = function inherits(ctor, superCtor) {
|
11412 | ctor.super_ = superCtor;
|
11413 | ctor.prototype = Object.create(superCtor.prototype, {
|
11414 | constructor: {
|
11415 | value: ctor,
|
11416 | enumerable: false,
|
11417 | writable: true,
|
11418 | configurable: true
|
11419 | }
|
11420 | });
|
11421 | };
|
11422 | } else {
|
11423 |
|
11424 | module.exports = function inherits(ctor, superCtor) {
|
11425 | ctor.super_ = superCtor;
|
11426 | var TempCtor = function () {};
|
11427 | TempCtor.prototype = superCtor.prototype;
|
11428 | ctor.prototype = new TempCtor();
|
11429 | ctor.prototype.constructor = ctor;
|
11430 | };
|
11431 | }
|
11432 | });
|
11433 |
|
11434 | var inherits_1 = inherits_browser;
|
11435 |
|
11436 | function toArray(msg, enc) {
|
11437 | if (Array.isArray(msg))
|
11438 | return msg.slice();
|
11439 | if (!msg)
|
11440 | return [];
|
11441 | var res = [];
|
11442 | if (typeof msg === 'string') {
|
11443 | if (!enc) {
|
11444 | for (var i = 0; i < msg.length; i++) {
|
11445 | var c = msg.charCodeAt(i);
|
11446 | var hi = c >> 8;
|
11447 | var lo = c & 0xff;
|
11448 | if (hi)
|
11449 | res.push(hi, lo);
|
11450 | else
|
11451 | res.push(lo);
|
11452 | }
|
11453 | } else if (enc === 'hex') {
|
11454 | msg = msg.replace(/[^a-z0-9]+/ig, '');
|
11455 | if (msg.length % 2 !== 0)
|
11456 | msg = '0' + msg;
|
11457 | for (i = 0; i < msg.length; i += 2)
|
11458 | res.push(parseInt(msg[i] + msg[i + 1], 16));
|
11459 | }
|
11460 | } else {
|
11461 | for (i = 0; i < msg.length; i++)
|
11462 | res[i] = msg[i] | 0;
|
11463 | }
|
11464 | return res;
|
11465 | }
|
11466 | var toArray_1 = toArray;
|
11467 |
|
11468 | function toHex(msg) {
|
11469 | var res = '';
|
11470 | for (var i = 0; i < msg.length; i++)
|
11471 | res += zero2(msg[i].toString(16));
|
11472 | return res;
|
11473 | }
|
11474 | var toHex_1 = toHex;
|
11475 |
|
11476 | function htonl(w) {
|
11477 | var res = (w >>> 24) |
|
11478 | ((w >>> 8) & 0xff00) |
|
11479 | ((w << 8) & 0xff0000) |
|
11480 | ((w & 0xff) << 24);
|
11481 | return res >>> 0;
|
11482 | }
|
11483 | var htonl_1 = htonl;
|
11484 |
|
11485 | function toHex32(msg, endian) {
|
11486 | var res = '';
|
11487 | for (var i = 0; i < msg.length; i++) {
|
11488 | var w = msg[i];
|
11489 | if (endian === 'little')
|
11490 | w = htonl(w);
|
11491 | res += zero8(w.toString(16));
|
11492 | }
|
11493 | return res;
|
11494 | }
|
11495 | var toHex32_1 = toHex32;
|
11496 |
|
11497 | function zero2(word) {
|
11498 | if (word.length === 1)
|
11499 | return '0' + word;
|
11500 | else
|
11501 | return word;
|
11502 | }
|
11503 | var zero2_1 = zero2;
|
11504 |
|
11505 | function zero8(word) {
|
11506 | if (word.length === 7)
|
11507 | return '0' + word;
|
11508 | else if (word.length === 6)
|
11509 | return '00' + word;
|
11510 | else if (word.length === 5)
|
11511 | return '000' + word;
|
11512 | else if (word.length === 4)
|
11513 | return '0000' + word;
|
11514 | else if (word.length === 3)
|
11515 | return '00000' + word;
|
11516 | else if (word.length === 2)
|
11517 | return '000000' + word;
|
11518 | else if (word.length === 1)
|
11519 | return '0000000' + word;
|
11520 | else
|
11521 | return word;
|
11522 | }
|
11523 | var zero8_1 = zero8;
|
11524 |
|
11525 | function join32(msg, start, end, endian) {
|
11526 | var len = end - start;
|
11527 | minimalisticAssert(len % 4 === 0);
|
11528 | var res = new Array(len / 4);
|
11529 | for (var i = 0, k = start; i < res.length; i++, k += 4) {
|
11530 | var w;
|
11531 | if (endian === 'big')
|
11532 | w = (msg[k] << 24) | (msg[k + 1] << 16) | (msg[k + 2] << 8) | msg[k + 3];
|
11533 | else
|
11534 | w = (msg[k + 3] << 24) | (msg[k + 2] << 16) | (msg[k + 1] << 8) | msg[k];
|
11535 | res[i] = w >>> 0;
|
11536 | }
|
11537 | return res;
|
11538 | }
|
11539 | var join32_1 = join32;
|
11540 |
|
11541 | function split32(msg, endian) {
|
11542 | var res = new Array(msg.length * 4);
|
11543 | for (var i = 0, k = 0; i < msg.length; i++, k += 4) {
|
11544 | var m = msg[i];
|
11545 | if (endian === 'big') {
|
11546 | res[k] = m >>> 24;
|
11547 | res[k + 1] = (m >>> 16) & 0xff;
|
11548 | res[k + 2] = (m >>> 8) & 0xff;
|
11549 | res[k + 3] = m & 0xff;
|
11550 | } else {
|
11551 | res[k + 3] = m >>> 24;
|
11552 | res[k + 2] = (m >>> 16) & 0xff;
|
11553 | res[k + 1] = (m >>> 8) & 0xff;
|
11554 | res[k] = m & 0xff;
|
11555 | }
|
11556 | }
|
11557 | return res;
|
11558 | }
|
11559 | var split32_1 = split32;
|
11560 |
|
11561 | function rotr32(w, b) {
|
11562 | return (w >>> b) | (w << (32 - b));
|
11563 | }
|
11564 | var rotr32_1 = rotr32;
|
11565 |
|
11566 | function rotl32(w, b) {
|
11567 | return (w << b) | (w >>> (32 - b));
|
11568 | }
|
11569 | var rotl32_1 = rotl32;
|
11570 |
|
11571 | function sum32(a, b) {
|
11572 | return (a + b) >>> 0;
|
11573 | }
|
11574 | var sum32_1 = sum32;
|
11575 |
|
11576 | function sum32_3(a, b, c) {
|
11577 | return (a + b + c) >>> 0;
|
11578 | }
|
11579 | var sum32_3_1 = sum32_3;
|
11580 |
|
11581 | function sum32_4(a, b, c, d) {
|
11582 | return (a + b + c + d) >>> 0;
|
11583 | }
|
11584 | var sum32_4_1 = sum32_4;
|
11585 |
|
11586 | function sum32_5(a, b, c, d, e) {
|
11587 | return (a + b + c + d + e) >>> 0;
|
11588 | }
|
11589 | var sum32_5_1 = sum32_5;
|
11590 |
|
11591 | function sum64(buf, pos, ah, al) {
|
11592 | var bh = buf[pos];
|
11593 | var bl = buf[pos + 1];
|
11594 |
|
11595 | var lo = (al + bl) >>> 0;
|
11596 | var hi = (lo < al ? 1 : 0) + ah + bh;
|
11597 | buf[pos] = hi >>> 0;
|
11598 | buf[pos + 1] = lo;
|
11599 | }
|
11600 | var sum64_1 = sum64;
|
11601 |
|
11602 | function sum64_hi(ah, al, bh, bl) {
|
11603 | var lo = (al + bl) >>> 0;
|
11604 | var hi = (lo < al ? 1 : 0) + ah + bh;
|
11605 | return hi >>> 0;
|
11606 | }
|
11607 | var sum64_hi_1 = sum64_hi;
|
11608 |
|
11609 | function sum64_lo(ah, al, bh, bl) {
|
11610 | var lo = al + bl;
|
11611 | return lo >>> 0;
|
11612 | }
|
11613 | var sum64_lo_1 = sum64_lo;
|
11614 |
|
11615 | function sum64_4_hi(ah, al, bh, bl, ch, cl, dh, dl) {
|
11616 | var carry = 0;
|
11617 | var lo = al;
|
11618 | lo = (lo + bl) >>> 0;
|
11619 | carry += lo < al ? 1 : 0;
|
11620 | lo = (lo + cl) >>> 0;
|
11621 | carry += lo < cl ? 1 : 0;
|
11622 | lo = (lo + dl) >>> 0;
|
11623 | carry += lo < dl ? 1 : 0;
|
11624 |
|
11625 | var hi = ah + bh + ch + dh + carry;
|
11626 | return hi >>> 0;
|
11627 | }
|
11628 | var sum64_4_hi_1 = sum64_4_hi;
|
11629 |
|
11630 | function sum64_4_lo(ah, al, bh, bl, ch, cl, dh, dl) {
|
11631 | var lo = al + bl + cl + dl;
|
11632 | return lo >>> 0;
|
11633 | }
|
11634 | var sum64_4_lo_1 = sum64_4_lo;
|
11635 |
|
11636 | function sum64_5_hi(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
|
11637 | var carry = 0;
|
11638 | var lo = al;
|
11639 | lo = (lo + bl) >>> 0;
|
11640 | carry += lo < al ? 1 : 0;
|
11641 | lo = (lo + cl) >>> 0;
|
11642 | carry += lo < cl ? 1 : 0;
|
11643 | lo = (lo + dl) >>> 0;
|
11644 | carry += lo < dl ? 1 : 0;
|
11645 | lo = (lo + el) >>> 0;
|
11646 | carry += lo < el ? 1 : 0;
|
11647 |
|
11648 | var hi = ah + bh + ch + dh + eh + carry;
|
11649 | return hi >>> 0;
|
11650 | }
|
11651 | var sum64_5_hi_1 = sum64_5_hi;
|
11652 |
|
11653 | function sum64_5_lo(ah, al, bh, bl, ch, cl, dh, dl, eh, el) {
|
11654 | var lo = al + bl + cl + dl + el;
|
11655 |
|
11656 | return lo >>> 0;
|
11657 | }
|
11658 | var sum64_5_lo_1 = sum64_5_lo;
|
11659 |
|
11660 | function rotr64_hi(ah, al, num) {
|
11661 | var r = (al << (32 - num)) | (ah >>> num);
|
11662 | return r >>> 0;
|
11663 | }
|
11664 | var rotr64_hi_1 = rotr64_hi;
|
11665 |
|
11666 | function rotr64_lo(ah, al, num) {
|
11667 | var r = (ah << (32 - num)) | (al >>> num);
|
11668 | return r >>> 0;
|
11669 | }
|
11670 | var rotr64_lo_1 = rotr64_lo;
|
11671 |
|
11672 | function shr64_hi(ah, al, num) {
|
11673 | return ah >>> num;
|
11674 | }
|
11675 | var shr64_hi_1 = shr64_hi;
|
11676 |
|
11677 | function shr64_lo(ah, al, num) {
|
11678 | var r = (ah << (32 - num)) | (al >>> num);
|
11679 | return r >>> 0;
|
11680 | }
|
11681 | var shr64_lo_1 = shr64_lo;
|
11682 |
|
11683 | var utils = {
|
11684 | inherits: inherits_1,
|
11685 | toArray: toArray_1,
|
11686 | toHex: toHex_1,
|
11687 | htonl: htonl_1,
|
11688 | toHex32: toHex32_1,
|
11689 | zero2: zero2_1,
|
11690 | zero8: zero8_1,
|
11691 | join32: join32_1,
|
11692 | split32: split32_1,
|
11693 | rotr32: rotr32_1,
|
11694 | rotl32: rotl32_1,
|
11695 | sum32: sum32_1,
|
11696 | sum32_3: sum32_3_1,
|
11697 | sum32_4: sum32_4_1,
|
11698 | sum32_5: sum32_5_1,
|
11699 | sum64: sum64_1,
|
11700 | sum64_hi: sum64_hi_1,
|
11701 | sum64_lo: sum64_lo_1,
|
11702 | sum64_4_hi: sum64_4_hi_1,
|
11703 | sum64_4_lo: sum64_4_lo_1,
|
11704 | sum64_5_hi: sum64_5_hi_1,
|
11705 | sum64_5_lo: sum64_5_lo_1,
|
11706 | rotr64_hi: rotr64_hi_1,
|
11707 | rotr64_lo: rotr64_lo_1,
|
11708 | shr64_hi: shr64_hi_1,
|
11709 | shr64_lo: shr64_lo_1
|
11710 | };
|
11711 |
|
11712 | function BlockHash() {
|
11713 | this.pending = null;
|
11714 | this.pendingTotal = 0;
|
11715 | this.blockSize = this.constructor.blockSize;
|
11716 | this.outSize = this.constructor.outSize;
|
11717 | this.hmacStrength = this.constructor.hmacStrength;
|
11718 | this.padLength = this.constructor.padLength / 8;
|
11719 | this.endian = 'big';
|
11720 |
|
11721 | this._delta8 = this.blockSize / 8;
|
11722 | this._delta32 = this.blockSize / 32;
|
11723 | }
|
11724 | var BlockHash_1 = BlockHash;
|
11725 |
|
11726 | BlockHash.prototype.update = function update(msg, enc) {
|
11727 |
|
11728 | msg = utils.toArray(msg, enc);
|
11729 | if (!this.pending)
|
11730 | this.pending = msg;
|
11731 | else
|
11732 | this.pending = this.pending.concat(msg);
|
11733 | this.pendingTotal += msg.length;
|
11734 |
|
11735 |
|
11736 | if (this.pending.length >= this._delta8) {
|
11737 | msg = this.pending;
|
11738 |
|
11739 |
|
11740 | var r = msg.length % this._delta8;
|
11741 | this.pending = msg.slice(msg.length - r, msg.length);
|
11742 | if (this.pending.length === 0)
|
11743 | this.pending = null;
|
11744 |
|
11745 | msg = utils.join32(msg, 0, msg.length - r, this.endian);
|
11746 | for (var i = 0; i < msg.length; i += this._delta32)
|
11747 | this._update(msg, i, i + this._delta32);
|
11748 | }
|
11749 |
|
11750 | return this;
|
11751 | };
|
11752 |
|
11753 | BlockHash.prototype.digest = function digest(enc) {
|
11754 | this.update(this._pad());
|
11755 | minimalisticAssert(this.pending === null);
|
11756 |
|
11757 | return this._digest(enc);
|
11758 | };
|
11759 |
|
11760 | BlockHash.prototype._pad = function pad() {
|
11761 | var len = this.pendingTotal;
|
11762 | var bytes = this._delta8;
|
11763 | var k = bytes - ((len + this.padLength) % bytes);
|
11764 | var res = new Array(k + this.padLength);
|
11765 | res[0] = 0x80;
|
11766 | for (var i = 1; i < k; i++)
|
11767 | res[i] = 0;
|
11768 |
|
11769 |
|
11770 | len <<= 3;
|
11771 | if (this.endian === 'big') {
|
11772 | for (var t = 8; t < this.padLength; t++)
|
11773 | res[i++] = 0;
|
11774 |
|
11775 | res[i++] = 0;
|
11776 | res[i++] = 0;
|
11777 | res[i++] = 0;
|
11778 | res[i++] = 0;
|
11779 | res[i++] = (len >>> 24) & 0xff;
|
11780 | res[i++] = (len >>> 16) & 0xff;
|
11781 | res[i++] = (len >>> 8) & 0xff;
|
11782 | res[i++] = len & 0xff;
|
11783 | } else {
|
11784 | res[i++] = len & 0xff;
|
11785 | res[i++] = (len >>> 8) & 0xff;
|
11786 | res[i++] = (len >>> 16) & 0xff;
|
11787 | res[i++] = (len >>> 24) & 0xff;
|
11788 | res[i++] = 0;
|
11789 | res[i++] = 0;
|
11790 | res[i++] = 0;
|
11791 | res[i++] = 0;
|
11792 |
|
11793 | for (t = 8; t < this.padLength; t++)
|
11794 | res[i++] = 0;
|
11795 | }
|
11796 |
|
11797 | return res;
|
11798 | };
|
11799 |
|
11800 | var common = {
|
11801 | BlockHash: BlockHash_1
|
11802 | };
|
11803 |
|
11804 | var rotr32$1 = utils.rotr32;
|
11805 |
|
11806 | function ft_1(s, x, y, z) {
|
11807 | if (s === 0)
|
11808 | return ch32(x, y, z);
|
11809 | if (s === 1 || s === 3)
|
11810 | return p32(x, y, z);
|
11811 | if (s === 2)
|
11812 | return maj32(x, y, z);
|
11813 | }
|
11814 | var ft_1_1 = ft_1;
|
11815 |
|
11816 | function ch32(x, y, z) {
|
11817 | return (x & y) ^ ((~x) & z);
|
11818 | }
|
11819 | var ch32_1 = ch32;
|
11820 |
|
11821 | function maj32(x, y, z) {
|
11822 | return (x & y) ^ (x & z) ^ (y & z);
|
11823 | }
|
11824 | var maj32_1 = maj32;
|
11825 |
|
11826 | function p32(x, y, z) {
|
11827 | return x ^ y ^ z;
|
11828 | }
|
11829 | var p32_1 = p32;
|
11830 |
|
11831 | function s0_256(x) {
|
11832 | return rotr32$1(x, 2) ^ rotr32$1(x, 13) ^ rotr32$1(x, 22);
|
11833 | }
|
11834 | var s0_256_1 = s0_256;
|
11835 |
|
11836 | function s1_256(x) {
|
11837 | return rotr32$1(x, 6) ^ rotr32$1(x, 11) ^ rotr32$1(x, 25);
|
11838 | }
|
11839 | var s1_256_1 = s1_256;
|
11840 |
|
11841 | function g0_256(x) {
|
11842 | return rotr32$1(x, 7) ^ rotr32$1(x, 18) ^ (x >>> 3);
|
11843 | }
|
11844 | var g0_256_1 = g0_256;
|
11845 |
|
11846 | function g1_256(x) {
|
11847 | return rotr32$1(x, 17) ^ rotr32$1(x, 19) ^ (x >>> 10);
|
11848 | }
|
11849 | var g1_256_1 = g1_256;
|
11850 |
|
11851 | var common$1 = {
|
11852 | ft_1: ft_1_1,
|
11853 | ch32: ch32_1,
|
11854 | maj32: maj32_1,
|
11855 | p32: p32_1,
|
11856 | s0_256: s0_256_1,
|
11857 | s1_256: s1_256_1,
|
11858 | g0_256: g0_256_1,
|
11859 | g1_256: g1_256_1
|
11860 | };
|
11861 |
|
11862 | var sum32$1 = utils.sum32;
|
11863 | var sum32_4$1 = utils.sum32_4;
|
11864 | var sum32_5$1 = utils.sum32_5;
|
11865 | var ch32$1 = common$1.ch32;
|
11866 | var maj32$1 = common$1.maj32;
|
11867 | var s0_256$1 = common$1.s0_256;
|
11868 | var s1_256$1 = common$1.s1_256;
|
11869 | var g0_256$1 = common$1.g0_256;
|
11870 | var g1_256$1 = common$1.g1_256;
|
11871 |
|
11872 | var BlockHash$1 = common.BlockHash;
|
11873 |
|
11874 | var sha256_K = [
|
11875 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
11876 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
11877 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
11878 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
11879 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
11880 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
11881 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
11882 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
11883 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
11884 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
11885 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
11886 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
11887 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
11888 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
11889 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
11890 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
11891 | ];
|
11892 |
|
11893 | function SHA256() {
|
11894 | if (!(this instanceof SHA256))
|
11895 | return new SHA256();
|
11896 |
|
11897 | BlockHash$1.call(this);
|
11898 | this.h = [
|
11899 | 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
|
11900 | 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
|
11901 | ];
|
11902 | this.k = sha256_K;
|
11903 | this.W = new Array(64);
|
11904 | }
|
11905 | utils.inherits(SHA256, BlockHash$1);
|
11906 | var _256 = SHA256;
|
11907 |
|
11908 | SHA256.blockSize = 512;
|
11909 | SHA256.outSize = 256;
|
11910 | SHA256.hmacStrength = 192;
|
11911 | SHA256.padLength = 64;
|
11912 |
|
11913 | SHA256.prototype._update = function _update(msg, start) {
|
11914 | var W = this.W;
|
11915 |
|
11916 | for (var i = 0; i < 16; i++)
|
11917 | W[i] = msg[start + i];
|
11918 | for (; i < W.length; i++)
|
11919 | W[i] = sum32_4$1(g1_256$1(W[i - 2]), W[i - 7], g0_256$1(W[i - 15]), W[i - 16]);
|
11920 |
|
11921 | var a = this.h[0];
|
11922 | var b = this.h[1];
|
11923 | var c = this.h[2];
|
11924 | var d = this.h[3];
|
11925 | var e = this.h[4];
|
11926 | var f = this.h[5];
|
11927 | var g = this.h[6];
|
11928 | var h = this.h[7];
|
11929 |
|
11930 | minimalisticAssert(this.k.length === W.length);
|
11931 | for (i = 0; i < W.length; i++) {
|
11932 | var T1 = sum32_5$1(h, s1_256$1(e), ch32$1(e, f, g), this.k[i], W[i]);
|
11933 | var T2 = sum32$1(s0_256$1(a), maj32$1(a, b, c));
|
11934 | h = g;
|
11935 | g = f;
|
11936 | f = e;
|
11937 | e = sum32$1(d, T1);
|
11938 | d = c;
|
11939 | c = b;
|
11940 | b = a;
|
11941 | a = sum32$1(T1, T2);
|
11942 | }
|
11943 |
|
11944 | this.h[0] = sum32$1(this.h[0], a);
|
11945 | this.h[1] = sum32$1(this.h[1], b);
|
11946 | this.h[2] = sum32$1(this.h[2], c);
|
11947 | this.h[3] = sum32$1(this.h[3], d);
|
11948 | this.h[4] = sum32$1(this.h[4], e);
|
11949 | this.h[5] = sum32$1(this.h[5], f);
|
11950 | this.h[6] = sum32$1(this.h[6], g);
|
11951 | this.h[7] = sum32$1(this.h[7], h);
|
11952 | };
|
11953 |
|
11954 | SHA256.prototype._digest = function digest(enc) {
|
11955 | if (enc === 'hex')
|
11956 | return utils.toHex32(this.h, 'big');
|
11957 | else
|
11958 | return utils.split32(this.h, 'big');
|
11959 | };
|
11960 |
|
11961 | function SHA224() {
|
11962 | if (!(this instanceof SHA224))
|
11963 | return new SHA224();
|
11964 |
|
11965 | _256.call(this);
|
11966 | this.h = [
|
11967 | 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
|
11968 | 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 ];
|
11969 | }
|
11970 | utils.inherits(SHA224, _256);
|
11971 | var _224 = SHA224;
|
11972 |
|
11973 | SHA224.blockSize = 512;
|
11974 | SHA224.outSize = 224;
|
11975 | SHA224.hmacStrength = 192;
|
11976 | SHA224.padLength = 64;
|
11977 |
|
11978 | SHA224.prototype._digest = function digest(enc) {
|
11979 |
|
11980 | if (enc === 'hex')
|
11981 | return utils.toHex32(this.h.slice(0, 7), 'big');
|
11982 | else
|
11983 | return utils.split32(this.h.slice(0, 7), 'big');
|
11984 | };
|
11985 |
|
11986 | var rotr64_hi$1 = utils.rotr64_hi;
|
11987 | var rotr64_lo$1 = utils.rotr64_lo;
|
11988 | var shr64_hi$1 = utils.shr64_hi;
|
11989 | var shr64_lo$1 = utils.shr64_lo;
|
11990 | var sum64$1 = utils.sum64;
|
11991 | var sum64_hi$1 = utils.sum64_hi;
|
11992 | var sum64_lo$1 = utils.sum64_lo;
|
11993 | var sum64_4_hi$1 = utils.sum64_4_hi;
|
11994 | var sum64_4_lo$1 = utils.sum64_4_lo;
|
11995 | var sum64_5_hi$1 = utils.sum64_5_hi;
|
11996 | var sum64_5_lo$1 = utils.sum64_5_lo;
|
11997 |
|
11998 | var BlockHash$2 = common.BlockHash;
|
11999 |
|
12000 | var sha512_K = [
|
12001 | 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
|
12002 | 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
|
12003 | 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
|
12004 | 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
|
12005 | 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
|
12006 | 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
|
12007 | 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
|
12008 | 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
|
12009 | 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
|
12010 | 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
|
12011 | 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
|
12012 | 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
|
12013 | 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
|
12014 | 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
|
12015 | 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
|
12016 | 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
|
12017 | 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
|
12018 | 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
|
12019 | 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
|
12020 | 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
|
12021 | 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
|
12022 | 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
|
12023 | 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
|
12024 | 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
|
12025 | 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
|
12026 | 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
|
12027 | 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
|
12028 | 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
|
12029 | 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
|
12030 | 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
|
12031 | 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
|
12032 | 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
|
12033 | 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
|
12034 | 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
|
12035 | 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
|
12036 | 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
|
12037 | 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
|
12038 | 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
|
12039 | 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
|
12040 | 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
|
12041 | ];
|
12042 |
|
12043 | function SHA512() {
|
12044 | if (!(this instanceof SHA512))
|
12045 | return new SHA512();
|
12046 |
|
12047 | BlockHash$2.call(this);
|
12048 | this.h = [
|
12049 | 0x6a09e667, 0xf3bcc908,
|
12050 | 0xbb67ae85, 0x84caa73b,
|
12051 | 0x3c6ef372, 0xfe94f82b,
|
12052 | 0xa54ff53a, 0x5f1d36f1,
|
12053 | 0x510e527f, 0xade682d1,
|
12054 | 0x9b05688c, 0x2b3e6c1f,
|
12055 | 0x1f83d9ab, 0xfb41bd6b,
|
12056 | 0x5be0cd19, 0x137e2179 ];
|
12057 | this.k = sha512_K;
|
12058 | this.W = new Array(160);
|
12059 | }
|
12060 | utils.inherits(SHA512, BlockHash$2);
|
12061 | var _512 = SHA512;
|
12062 |
|
12063 | SHA512.blockSize = 1024;
|
12064 | SHA512.outSize = 512;
|
12065 | SHA512.hmacStrength = 192;
|
12066 | SHA512.padLength = 128;
|
12067 |
|
12068 | SHA512.prototype._prepareBlock = function _prepareBlock(msg, start) {
|
12069 | var W = this.W;
|
12070 |
|
12071 |
|
12072 | for (var i = 0; i < 32; i++)
|
12073 | W[i] = msg[start + i];
|
12074 | for (; i < W.length; i += 2) {
|
12075 | var c0_hi = g1_512_hi(W[i - 4], W[i - 3]);
|
12076 | var c0_lo = g1_512_lo(W[i - 4], W[i - 3]);
|
12077 | var c1_hi = W[i - 14];
|
12078 | var c1_lo = W[i - 13];
|
12079 | var c2_hi = g0_512_hi(W[i - 30], W[i - 29]);
|
12080 | var c2_lo = g0_512_lo(W[i - 30], W[i - 29]);
|
12081 | var c3_hi = W[i - 32];
|
12082 | var c3_lo = W[i - 31];
|
12083 |
|
12084 | W[i] = sum64_4_hi$1(
|
12085 | c0_hi, c0_lo,
|
12086 | c1_hi, c1_lo,
|
12087 | c2_hi, c2_lo,
|
12088 | c3_hi, c3_lo);
|
12089 | W[i + 1] = sum64_4_lo$1(
|
12090 | c0_hi, c0_lo,
|
12091 | c1_hi, c1_lo,
|
12092 | c2_hi, c2_lo,
|
12093 | c3_hi, c3_lo);
|
12094 | }
|
12095 | };
|
12096 |
|
12097 | SHA512.prototype._update = function _update(msg, start) {
|
12098 | this._prepareBlock(msg, start);
|
12099 |
|
12100 | var W = this.W;
|
12101 |
|
12102 | var ah = this.h[0];
|
12103 | var al = this.h[1];
|
12104 | var bh = this.h[2];
|
12105 | var bl = this.h[3];
|
12106 | var ch = this.h[4];
|
12107 | var cl = this.h[5];
|
12108 | var dh = this.h[6];
|
12109 | var dl = this.h[7];
|
12110 | var eh = this.h[8];
|
12111 | var el = this.h[9];
|
12112 | var fh = this.h[10];
|
12113 | var fl = this.h[11];
|
12114 | var gh = this.h[12];
|
12115 | var gl = this.h[13];
|
12116 | var hh = this.h[14];
|
12117 | var hl = this.h[15];
|
12118 |
|
12119 | minimalisticAssert(this.k.length === W.length);
|
12120 | for (var i = 0; i < W.length; i += 2) {
|
12121 | var c0_hi = hh;
|
12122 | var c0_lo = hl;
|
12123 | var c1_hi = s1_512_hi(eh, el);
|
12124 | var c1_lo = s1_512_lo(eh, el);
|
12125 | var c2_hi = ch64_hi(eh, el, fh, fl, gh);
|
12126 | var c2_lo = ch64_lo(eh, el, fh, fl, gh, gl);
|
12127 | var c3_hi = this.k[i];
|
12128 | var c3_lo = this.k[i + 1];
|
12129 | var c4_hi = W[i];
|
12130 | var c4_lo = W[i + 1];
|
12131 |
|
12132 | var T1_hi = sum64_5_hi$1(
|
12133 | c0_hi, c0_lo,
|
12134 | c1_hi, c1_lo,
|
12135 | c2_hi, c2_lo,
|
12136 | c3_hi, c3_lo,
|
12137 | c4_hi, c4_lo);
|
12138 | var T1_lo = sum64_5_lo$1(
|
12139 | c0_hi, c0_lo,
|
12140 | c1_hi, c1_lo,
|
12141 | c2_hi, c2_lo,
|
12142 | c3_hi, c3_lo,
|
12143 | c4_hi, c4_lo);
|
12144 |
|
12145 | c0_hi = s0_512_hi(ah, al);
|
12146 | c0_lo = s0_512_lo(ah, al);
|
12147 | c1_hi = maj64_hi(ah, al, bh, bl, ch);
|
12148 | c1_lo = maj64_lo(ah, al, bh, bl, ch, cl);
|
12149 |
|
12150 | var T2_hi = sum64_hi$1(c0_hi, c0_lo, c1_hi, c1_lo);
|
12151 | var T2_lo = sum64_lo$1(c0_hi, c0_lo, c1_hi, c1_lo);
|
12152 |
|
12153 | hh = gh;
|
12154 | hl = gl;
|
12155 |
|
12156 | gh = fh;
|
12157 | gl = fl;
|
12158 |
|
12159 | fh = eh;
|
12160 | fl = el;
|
12161 |
|
12162 | eh = sum64_hi$1(dh, dl, T1_hi, T1_lo);
|
12163 | el = sum64_lo$1(dl, dl, T1_hi, T1_lo);
|
12164 |
|
12165 | dh = ch;
|
12166 | dl = cl;
|
12167 |
|
12168 | ch = bh;
|
12169 | cl = bl;
|
12170 |
|
12171 | bh = ah;
|
12172 | bl = al;
|
12173 |
|
12174 | ah = sum64_hi$1(T1_hi, T1_lo, T2_hi, T2_lo);
|
12175 | al = sum64_lo$1(T1_hi, T1_lo, T2_hi, T2_lo);
|
12176 | }
|
12177 |
|
12178 | sum64$1(this.h, 0, ah, al);
|
12179 | sum64$1(this.h, 2, bh, bl);
|
12180 | sum64$1(this.h, 4, ch, cl);
|
12181 | sum64$1(this.h, 6, dh, dl);
|
12182 | sum64$1(this.h, 8, eh, el);
|
12183 | sum64$1(this.h, 10, fh, fl);
|
12184 | sum64$1(this.h, 12, gh, gl);
|
12185 | sum64$1(this.h, 14, hh, hl);
|
12186 | };
|
12187 |
|
12188 | SHA512.prototype._digest = function digest(enc) {
|
12189 | if (enc === 'hex')
|
12190 | return utils.toHex32(this.h, 'big');
|
12191 | else
|
12192 | return utils.split32(this.h, 'big');
|
12193 | };
|
12194 |
|
12195 | function ch64_hi(xh, xl, yh, yl, zh) {
|
12196 | var r = (xh & yh) ^ ((~xh) & zh);
|
12197 | if (r < 0)
|
12198 | r += 0x100000000;
|
12199 | return r;
|
12200 | }
|
12201 |
|
12202 | function ch64_lo(xh, xl, yh, yl, zh, zl) {
|
12203 | var r = (xl & yl) ^ ((~xl) & zl);
|
12204 | if (r < 0)
|
12205 | r += 0x100000000;
|
12206 | return r;
|
12207 | }
|
12208 |
|
12209 | function maj64_hi(xh, xl, yh, yl, zh) {
|
12210 | var r = (xh & yh) ^ (xh & zh) ^ (yh & zh);
|
12211 | if (r < 0)
|
12212 | r += 0x100000000;
|
12213 | return r;
|
12214 | }
|
12215 |
|
12216 | function maj64_lo(xh, xl, yh, yl, zh, zl) {
|
12217 | var r = (xl & yl) ^ (xl & zl) ^ (yl & zl);
|
12218 | if (r < 0)
|
12219 | r += 0x100000000;
|
12220 | return r;
|
12221 | }
|
12222 |
|
12223 | function s0_512_hi(xh, xl) {
|
12224 | var c0_hi = rotr64_hi$1(xh, xl, 28);
|
12225 | var c1_hi = rotr64_hi$1(xl, xh, 2);
|
12226 | var c2_hi = rotr64_hi$1(xl, xh, 7);
|
12227 |
|
12228 | var r = c0_hi ^ c1_hi ^ c2_hi;
|
12229 | if (r < 0)
|
12230 | r += 0x100000000;
|
12231 | return r;
|
12232 | }
|
12233 |
|
12234 | function s0_512_lo(xh, xl) {
|
12235 | var c0_lo = rotr64_lo$1(xh, xl, 28);
|
12236 | var c1_lo = rotr64_lo$1(xl, xh, 2);
|
12237 | var c2_lo = rotr64_lo$1(xl, xh, 7);
|
12238 |
|
12239 | var r = c0_lo ^ c1_lo ^ c2_lo;
|
12240 | if (r < 0)
|
12241 | r += 0x100000000;
|
12242 | return r;
|
12243 | }
|
12244 |
|
12245 | function s1_512_hi(xh, xl) {
|
12246 | var c0_hi = rotr64_hi$1(xh, xl, 14);
|
12247 | var c1_hi = rotr64_hi$1(xh, xl, 18);
|
12248 | var c2_hi = rotr64_hi$1(xl, xh, 9);
|
12249 |
|
12250 | var r = c0_hi ^ c1_hi ^ c2_hi;
|
12251 | if (r < 0)
|
12252 | r += 0x100000000;
|
12253 | return r;
|
12254 | }
|
12255 |
|
12256 | function s1_512_lo(xh, xl) {
|
12257 | var c0_lo = rotr64_lo$1(xh, xl, 14);
|
12258 | var c1_lo = rotr64_lo$1(xh, xl, 18);
|
12259 | var c2_lo = rotr64_lo$1(xl, xh, 9);
|
12260 |
|
12261 | var r = c0_lo ^ c1_lo ^ c2_lo;
|
12262 | if (r < 0)
|
12263 | r += 0x100000000;
|
12264 | return r;
|
12265 | }
|
12266 |
|
12267 | function g0_512_hi(xh, xl) {
|
12268 | var c0_hi = rotr64_hi$1(xh, xl, 1);
|
12269 | var c1_hi = rotr64_hi$1(xh, xl, 8);
|
12270 | var c2_hi = shr64_hi$1(xh, xl, 7);
|
12271 |
|
12272 | var r = c0_hi ^ c1_hi ^ c2_hi;
|
12273 | if (r < 0)
|
12274 | r += 0x100000000;
|
12275 | return r;
|
12276 | }
|
12277 |
|
12278 | function g0_512_lo(xh, xl) {
|
12279 | var c0_lo = rotr64_lo$1(xh, xl, 1);
|
12280 | var c1_lo = rotr64_lo$1(xh, xl, 8);
|
12281 | var c2_lo = shr64_lo$1(xh, xl, 7);
|
12282 |
|
12283 | var r = c0_lo ^ c1_lo ^ c2_lo;
|
12284 | if (r < 0)
|
12285 | r += 0x100000000;
|
12286 | return r;
|
12287 | }
|
12288 |
|
12289 | function g1_512_hi(xh, xl) {
|
12290 | var c0_hi = rotr64_hi$1(xh, xl, 19);
|
12291 | var c1_hi = rotr64_hi$1(xl, xh, 29);
|
12292 | var c2_hi = shr64_hi$1(xh, xl, 6);
|
12293 |
|
12294 | var r = c0_hi ^ c1_hi ^ c2_hi;
|
12295 | if (r < 0)
|
12296 | r += 0x100000000;
|
12297 | return r;
|
12298 | }
|
12299 |
|
12300 | function g1_512_lo(xh, xl) {
|
12301 | var c0_lo = rotr64_lo$1(xh, xl, 19);
|
12302 | var c1_lo = rotr64_lo$1(xl, xh, 29);
|
12303 | var c2_lo = shr64_lo$1(xh, xl, 6);
|
12304 |
|
12305 | var r = c0_lo ^ c1_lo ^ c2_lo;
|
12306 | if (r < 0)
|
12307 | r += 0x100000000;
|
12308 | return r;
|
12309 | }
|
12310 |
|
12311 | function SHA384() {
|
12312 | if (!(this instanceof SHA384))
|
12313 | return new SHA384();
|
12314 |
|
12315 | _512.call(this);
|
12316 | this.h = [
|
12317 | 0xcbbb9d5d, 0xc1059ed8,
|
12318 | 0x629a292a, 0x367cd507,
|
12319 | 0x9159015a, 0x3070dd17,
|
12320 | 0x152fecd8, 0xf70e5939,
|
12321 | 0x67332667, 0xffc00b31,
|
12322 | 0x8eb44a87, 0x68581511,
|
12323 | 0xdb0c2e0d, 0x64f98fa7,
|
12324 | 0x47b5481d, 0xbefa4fa4 ];
|
12325 | }
|
12326 | utils.inherits(SHA384, _512);
|
12327 | var _384 = SHA384;
|
12328 |
|
12329 | SHA384.blockSize = 1024;
|
12330 | SHA384.outSize = 384;
|
12331 | SHA384.hmacStrength = 192;
|
12332 | SHA384.padLength = 128;
|
12333 |
|
12334 | SHA384.prototype._digest = function digest(enc) {
|
12335 | if (enc === 'hex')
|
12336 | return utils.toHex32(this.h.slice(0, 12), 'big');
|
12337 | else
|
12338 | return utils.split32(this.h.slice(0, 12), 'big');
|
12339 | };
|
12340 |
|
12341 | var rotl32$1 = utils.rotl32;
|
12342 | var sum32$2 = utils.sum32;
|
12343 | var sum32_3$1 = utils.sum32_3;
|
12344 | var sum32_4$2 = utils.sum32_4;
|
12345 | var BlockHash$3 = common.BlockHash;
|
12346 |
|
12347 | function RIPEMD160() {
|
12348 | if (!(this instanceof RIPEMD160))
|
12349 | return new RIPEMD160();
|
12350 |
|
12351 | BlockHash$3.call(this);
|
12352 |
|
12353 | this.h = [ 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 ];
|
12354 | this.endian = 'little';
|
12355 | }
|
12356 | utils.inherits(RIPEMD160, BlockHash$3);
|
12357 | var ripemd160 = RIPEMD160;
|
12358 |
|
12359 | RIPEMD160.blockSize = 512;
|
12360 | RIPEMD160.outSize = 160;
|
12361 | RIPEMD160.hmacStrength = 192;
|
12362 | RIPEMD160.padLength = 64;
|
12363 |
|
12364 | RIPEMD160.prototype._update = function update(msg, start) {
|
12365 | var A = this.h[0];
|
12366 | var B = this.h[1];
|
12367 | var C = this.h[2];
|
12368 | var D = this.h[3];
|
12369 | var E = this.h[4];
|
12370 | var Ah = A;
|
12371 | var Bh = B;
|
12372 | var Ch = C;
|
12373 | var Dh = D;
|
12374 | var Eh = E;
|
12375 | for (var j = 0; j < 80; j++) {
|
12376 | var T = sum32$2(
|
12377 | rotl32$1(
|
12378 | sum32_4$2(A, f(j, B, C, D), msg[r[j] + start], K(j)),
|
12379 | s[j]),
|
12380 | E);
|
12381 | A = E;
|
12382 | E = D;
|
12383 | D = rotl32$1(C, 10);
|
12384 | C = B;
|
12385 | B = T;
|
12386 | T = sum32$2(
|
12387 | rotl32$1(
|
12388 | sum32_4$2(Ah, f(79 - j, Bh, Ch, Dh), msg[rh[j] + start], Kh(j)),
|
12389 | sh[j]),
|
12390 | Eh);
|
12391 | Ah = Eh;
|
12392 | Eh = Dh;
|
12393 | Dh = rotl32$1(Ch, 10);
|
12394 | Ch = Bh;
|
12395 | Bh = T;
|
12396 | }
|
12397 | T = sum32_3$1(this.h[1], C, Dh);
|
12398 | this.h[1] = sum32_3$1(this.h[2], D, Eh);
|
12399 | this.h[2] = sum32_3$1(this.h[3], E, Ah);
|
12400 | this.h[3] = sum32_3$1(this.h[4], A, Bh);
|
12401 | this.h[4] = sum32_3$1(this.h[0], B, Ch);
|
12402 | this.h[0] = T;
|
12403 | };
|
12404 |
|
12405 | RIPEMD160.prototype._digest = function digest(enc) {
|
12406 | if (enc === 'hex')
|
12407 | return utils.toHex32(this.h, 'little');
|
12408 | else
|
12409 | return utils.split32(this.h, 'little');
|
12410 | };
|
12411 |
|
12412 | function f(j, x, y, z) {
|
12413 | if (j <= 15)
|
12414 | return x ^ y ^ z;
|
12415 | else if (j <= 31)
|
12416 | return (x & y) | ((~x) & z);
|
12417 | else if (j <= 47)
|
12418 | return (x | (~y)) ^ z;
|
12419 | else if (j <= 63)
|
12420 | return (x & z) | (y & (~z));
|
12421 | else
|
12422 | return x ^ (y | (~z));
|
12423 | }
|
12424 |
|
12425 | function K(j) {
|
12426 | if (j <= 15)
|
12427 | return 0x00000000;
|
12428 | else if (j <= 31)
|
12429 | return 0x5a827999;
|
12430 | else if (j <= 47)
|
12431 | return 0x6ed9eba1;
|
12432 | else if (j <= 63)
|
12433 | return 0x8f1bbcdc;
|
12434 | else
|
12435 | return 0xa953fd4e;
|
12436 | }
|
12437 |
|
12438 | function Kh(j) {
|
12439 | if (j <= 15)
|
12440 | return 0x50a28be6;
|
12441 | else if (j <= 31)
|
12442 | return 0x5c4dd124;
|
12443 | else if (j <= 47)
|
12444 | return 0x6d703ef3;
|
12445 | else if (j <= 63)
|
12446 | return 0x7a6d76e9;
|
12447 | else
|
12448 | return 0x00000000;
|
12449 | }
|
12450 |
|
12451 | var r = [
|
12452 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
12453 | 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
|
12454 | 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
|
12455 | 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
|
12456 | 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
|
12457 | ];
|
12458 |
|
12459 | var rh = [
|
12460 | 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
|
12461 | 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
|
12462 | 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
|
12463 | 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
|
12464 | 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
|
12465 | ];
|
12466 |
|
12467 | var s = [
|
12468 | 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
|
12469 | 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
|
12470 | 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
|
12471 | 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
|
12472 | 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
|
12473 | ];
|
12474 |
|
12475 | var sh = [
|
12476 | 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
|
12477 | 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
|
12478 | 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
|
12479 | 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
|
12480 | 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
|
12481 | ];
|
12482 |
|
12483 | var ripemd = {
|
12484 | ripemd160: ripemd160
|
12485 | };
|
12486 |
|
12487 |
|
12488 |
|
12489 |
|
12490 |
|
12491 |
|
12492 |
|
12493 |
|
12494 |
|
12495 |
|
12496 |
|
12497 |
|
12498 |
|
12499 |
|
12500 |
|
12501 |
|
12502 | async function md5(entree) {
|
12503 | const digest = md51(util.uint8ArrayToStr(entree));
|
12504 | return util.hexToUint8Array(hex(digest));
|
12505 | }
|
12506 |
|
12507 | function md5cycle(x, k) {
|
12508 | let a = x[0];
|
12509 | let b = x[1];
|
12510 | let c = x[2];
|
12511 | let d = x[3];
|
12512 |
|
12513 | a = ff(a, b, c, d, k[0], 7, -680876936);
|
12514 | d = ff(d, a, b, c, k[1], 12, -389564586);
|
12515 | c = ff(c, d, a, b, k[2], 17, 606105819);
|
12516 | b = ff(b, c, d, a, k[3], 22, -1044525330);
|
12517 | a = ff(a, b, c, d, k[4], 7, -176418897);
|
12518 | d = ff(d, a, b, c, k[5], 12, 1200080426);
|
12519 | c = ff(c, d, a, b, k[6], 17, -1473231341);
|
12520 | b = ff(b, c, d, a, k[7], 22, -45705983);
|
12521 | a = ff(a, b, c, d, k[8], 7, 1770035416);
|
12522 | d = ff(d, a, b, c, k[9], 12, -1958414417);
|
12523 | c = ff(c, d, a, b, k[10], 17, -42063);
|
12524 | b = ff(b, c, d, a, k[11], 22, -1990404162);
|
12525 | a = ff(a, b, c, d, k[12], 7, 1804603682);
|
12526 | d = ff(d, a, b, c, k[13], 12, -40341101);
|
12527 | c = ff(c, d, a, b, k[14], 17, -1502002290);
|
12528 | b = ff(b, c, d, a, k[15], 22, 1236535329);
|
12529 |
|
12530 | a = gg(a, b, c, d, k[1], 5, -165796510);
|
12531 | d = gg(d, a, b, c, k[6], 9, -1069501632);
|
12532 | c = gg(c, d, a, b, k[11], 14, 643717713);
|
12533 | b = gg(b, c, d, a, k[0], 20, -373897302);
|
12534 | a = gg(a, b, c, d, k[5], 5, -701558691);
|
12535 | d = gg(d, a, b, c, k[10], 9, 38016083);
|
12536 | c = gg(c, d, a, b, k[15], 14, -660478335);
|
12537 | b = gg(b, c, d, a, k[4], 20, -405537848);
|
12538 | a = gg(a, b, c, d, k[9], 5, 568446438);
|
12539 | d = gg(d, a, b, c, k[14], 9, -1019803690);
|
12540 | c = gg(c, d, a, b, k[3], 14, -187363961);
|
12541 | b = gg(b, c, d, a, k[8], 20, 1163531501);
|
12542 | a = gg(a, b, c, d, k[13], 5, -1444681467);
|
12543 | d = gg(d, a, b, c, k[2], 9, -51403784);
|
12544 | c = gg(c, d, a, b, k[7], 14, 1735328473);
|
12545 | b = gg(b, c, d, a, k[12], 20, -1926607734);
|
12546 |
|
12547 | a = hh(a, b, c, d, k[5], 4, -378558);
|
12548 | d = hh(d, a, b, c, k[8], 11, -2022574463);
|
12549 | c = hh(c, d, a, b, k[11], 16, 1839030562);
|
12550 | b = hh(b, c, d, a, k[14], 23, -35309556);
|
12551 | a = hh(a, b, c, d, k[1], 4, -1530992060);
|
12552 | d = hh(d, a, b, c, k[4], 11, 1272893353);
|
12553 | c = hh(c, d, a, b, k[7], 16, -155497632);
|
12554 | b = hh(b, c, d, a, k[10], 23, -1094730640);
|
12555 | a = hh(a, b, c, d, k[13], 4, 681279174);
|
12556 | d = hh(d, a, b, c, k[0], 11, -358537222);
|
12557 | c = hh(c, d, a, b, k[3], 16, -722521979);
|
12558 | b = hh(b, c, d, a, k[6], 23, 76029189);
|
12559 | a = hh(a, b, c, d, k[9], 4, -640364487);
|
12560 | d = hh(d, a, b, c, k[12], 11, -421815835);
|
12561 | c = hh(c, d, a, b, k[15], 16, 530742520);
|
12562 | b = hh(b, c, d, a, k[2], 23, -995338651);
|
12563 |
|
12564 | a = ii(a, b, c, d, k[0], 6, -198630844);
|
12565 | d = ii(d, a, b, c, k[7], 10, 1126891415);
|
12566 | c = ii(c, d, a, b, k[14], 15, -1416354905);
|
12567 | b = ii(b, c, d, a, k[5], 21, -57434055);
|
12568 | a = ii(a, b, c, d, k[12], 6, 1700485571);
|
12569 | d = ii(d, a, b, c, k[3], 10, -1894986606);
|
12570 | c = ii(c, d, a, b, k[10], 15, -1051523);
|
12571 | b = ii(b, c, d, a, k[1], 21, -2054922799);
|
12572 | a = ii(a, b, c, d, k[8], 6, 1873313359);
|
12573 | d = ii(d, a, b, c, k[15], 10, -30611744);
|
12574 | c = ii(c, d, a, b, k[6], 15, -1560198380);
|
12575 | b = ii(b, c, d, a, k[13], 21, 1309151649);
|
12576 | a = ii(a, b, c, d, k[4], 6, -145523070);
|
12577 | d = ii(d, a, b, c, k[11], 10, -1120210379);
|
12578 | c = ii(c, d, a, b, k[2], 15, 718787259);
|
12579 | b = ii(b, c, d, a, k[9], 21, -343485551);
|
12580 |
|
12581 | x[0] = add32(a, x[0]);
|
12582 | x[1] = add32(b, x[1]);
|
12583 | x[2] = add32(c, x[2]);
|
12584 | x[3] = add32(d, x[3]);
|
12585 | }
|
12586 |
|
12587 | function cmn(q, a, b, x, s, t) {
|
12588 | a = add32(add32(a, q), add32(x, t));
|
12589 | return add32((a << s) | (a >>> (32 - s)), b);
|
12590 | }
|
12591 |
|
12592 | function ff(a, b, c, d, x, s, t) {
|
12593 | return cmn((b & c) | ((~b) & d), a, b, x, s, t);
|
12594 | }
|
12595 |
|
12596 | function gg(a, b, c, d, x, s, t) {
|
12597 | return cmn((b & d) | (c & (~d)), a, b, x, s, t);
|
12598 | }
|
12599 |
|
12600 | function hh(a, b, c, d, x, s, t) {
|
12601 | return cmn(b ^ c ^ d, a, b, x, s, t);
|
12602 | }
|
12603 |
|
12604 | function ii(a, b, c, d, x, s, t) {
|
12605 | return cmn(c ^ (b | (~d)), a, b, x, s, t);
|
12606 | }
|
12607 |
|
12608 | function md51(s) {
|
12609 | const n = s.length;
|
12610 | const state = [1732584193, -271733879, -1732584194, 271733878];
|
12611 | let i;
|
12612 | for (i = 64; i <= s.length; i += 64) {
|
12613 | md5cycle(state, md5blk(s.substring(i - 64, i)));
|
12614 | }
|
12615 | s = s.substring(i - 64);
|
12616 | const tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
12617 | for (i = 0; i < s.length; i++) {
|
12618 | tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3);
|
12619 | }
|
12620 | tail[i >> 2] |= 0x80 << ((i % 4) << 3);
|
12621 | if (i > 55) {
|
12622 | md5cycle(state, tail);
|
12623 | for (i = 0; i < 16; i++) {
|
12624 | tail[i] = 0;
|
12625 | }
|
12626 | }
|
12627 | tail[14] = n * 8;
|
12628 | md5cycle(state, tail);
|
12629 | return state;
|
12630 | }
|
12631 |
|
12632 |
|
12633 |
|
12634 |
|
12635 |
|
12636 |
|
12637 |
|
12638 |
|
12639 |
|
12640 |
|
12641 |
|
12642 |
|
12643 |
|
12644 |
|
12645 |
|
12646 |
|
12647 | function md5blk(s) {
|
12648 | const md5blks = [];
|
12649 | let i;
|
12650 | for (i = 0; i < 64; i += 4) {
|
12651 | md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) <<
|
12652 | 24);
|
12653 | }
|
12654 | return md5blks;
|
12655 | }
|
12656 |
|
12657 | const hex_chr = '0123456789abcdef'.split('');
|
12658 |
|
12659 | function rhex(n) {
|
12660 | let s = '';
|
12661 | let j = 0;
|
12662 | for (; j < 4; j++) {
|
12663 | s += hex_chr[(n >> (j * 8 + 4)) & 0x0F] + hex_chr[(n >> (j * 8)) & 0x0F];
|
12664 | }
|
12665 | return s;
|
12666 | }
|
12667 |
|
12668 | function hex(x) {
|
12669 | for (let i = 0; i < x.length; i++) {
|
12670 | x[i] = rhex(x[i]);
|
12671 | }
|
12672 | return x.join('');
|
12673 | }
|
12674 |
|
12675 |
|
12676 |
|
12677 |
|
12678 |
|
12679 |
|
12680 |
|
12681 | function add32(a, b) {
|
12682 | return (a + b) & 0xFFFFFFFF;
|
12683 | }
|
12684 |
|
12685 |
|
12686 |
|
12687 |
|
12688 |
|
12689 |
|
12690 |
|
12691 |
|
12692 |
|
12693 | const webCrypto = util.getWebCrypto();
|
12694 | const nodeCrypto = util.getNodeCrypto();
|
12695 | const Buffer$1 = util.getNodeBuffer();
|
12696 |
|
12697 | function node_hash(type) {
|
12698 | return async function (data) {
|
12699 | const shasum = nodeCrypto.createHash(type);
|
12700 | return stream.transform(data, value => {
|
12701 | shasum.update(Buffer$1.from(value));
|
12702 | }, () => new Uint8Array(shasum.digest()));
|
12703 | };
|
12704 | }
|
12705 |
|
12706 | function hashjs_hash(hash, webCryptoHash) {
|
12707 | return async function(data, config = defaultConfig) {
|
12708 | if (!util.isStream(data) && webCrypto && webCryptoHash && data.length >= config.minBytesForWebCrypto) {
|
12709 | return new Uint8Array(await webCrypto.digest(webCryptoHash, data));
|
12710 | }
|
12711 | const hashInstance = hash();
|
12712 | return stream.transform(data, value => {
|
12713 | hashInstance.update(value);
|
12714 | }, () => new Uint8Array(hashInstance.digest()));
|
12715 | };
|
12716 | }
|
12717 |
|
12718 | function asmcrypto_hash(hash, webCryptoHash) {
|
12719 | return async function(data, config = defaultConfig) {
|
12720 | if (util.isStream(data)) {
|
12721 | const hashInstance = new hash();
|
12722 | return stream.transform(data, value => {
|
12723 | hashInstance.process(value);
|
12724 | }, () => hashInstance.finish().result);
|
12725 | } else if (webCrypto && webCryptoHash && data.length >= config.minBytesForWebCrypto) {
|
12726 | return new Uint8Array(await webCrypto.digest(webCryptoHash, data));
|
12727 | } else {
|
12728 | return hash.bytes(data);
|
12729 | }
|
12730 | };
|
12731 | }
|
12732 |
|
12733 | let hash_fns;
|
12734 | if (nodeCrypto) {
|
12735 | hash_fns = {
|
12736 | md5: node_hash('md5'),
|
12737 | sha1: node_hash('sha1'),
|
12738 | sha224: node_hash('sha224'),
|
12739 | sha256: node_hash('sha256'),
|
12740 | sha384: node_hash('sha384'),
|
12741 | sha512: node_hash('sha512'),
|
12742 | ripemd: node_hash('ripemd160')
|
12743 | };
|
12744 | } else {
|
12745 | hash_fns = {
|
12746 | md5: md5,
|
12747 | sha1: asmcrypto_hash(Sha1, navigator.userAgent.indexOf('Edge') === -1 && 'SHA-1'),
|
12748 | sha224: hashjs_hash(_224),
|
12749 | sha256: asmcrypto_hash(Sha256, 'SHA-256'),
|
12750 | sha384: hashjs_hash(_384, 'SHA-384'),
|
12751 | sha512: hashjs_hash(_512, 'SHA-512'),
|
12752 | ripemd: hashjs_hash(ripemd160)
|
12753 | };
|
12754 | }
|
12755 |
|
12756 | var hash = {
|
12757 |
|
12758 |
|
12759 | md5: hash_fns.md5,
|
12760 |
|
12761 | sha1: hash_fns.sha1,
|
12762 |
|
12763 | sha224: hash_fns.sha224,
|
12764 |
|
12765 | sha256: hash_fns.sha256,
|
12766 |
|
12767 | sha384: hash_fns.sha384,
|
12768 |
|
12769 | sha512: hash_fns.sha512,
|
12770 |
|
12771 | ripemd: hash_fns.ripemd,
|
12772 |
|
12773 | |
12774 |
|
12775 |
|
12776 |
|
12777 |
|
12778 |
|
12779 |
|
12780 | digest: function(algo, data) {
|
12781 | switch (algo) {
|
12782 | case 1:
|
12783 |
|
12784 | return this.md5(data);
|
12785 | case 2:
|
12786 |
|
12787 | return this.sha1(data);
|
12788 | case 3:
|
12789 |
|
12790 | return this.ripemd(data);
|
12791 | case 8:
|
12792 |
|
12793 | return this.sha256(data);
|
12794 | case 9:
|
12795 |
|
12796 | return this.sha384(data);
|
12797 | case 10:
|
12798 |
|
12799 | return this.sha512(data);
|
12800 | case 11:
|
12801 |
|
12802 | return this.sha224(data);
|
12803 | default:
|
12804 | throw new Error('Invalid hash function.');
|
12805 | }
|
12806 | },
|
12807 |
|
12808 | |
12809 |
|
12810 |
|
12811 |
|
12812 |
|
12813 | getHashByteLength: function(algo) {
|
12814 | switch (algo) {
|
12815 | case 1:
|
12816 | return 16;
|
12817 | case 2:
|
12818 | case 3:
|
12819 | return 20;
|
12820 | case 8:
|
12821 | return 32;
|
12822 | case 9:
|
12823 | return 48;
|
12824 | case 10:
|
12825 | return 64;
|
12826 | case 11:
|
12827 | return 28;
|
12828 | default:
|
12829 | throw new Error('Invalid hash algorithm.');
|
12830 | }
|
12831 | }
|
12832 | };
|
12833 |
|
12834 | class AES_CFB {
|
12835 | static encrypt(data, key, iv) {
|
12836 | return new AES_CFB(key, iv).encrypt(data);
|
12837 | }
|
12838 | static decrypt(data, key, iv) {
|
12839 | return new AES_CFB(key, iv).decrypt(data);
|
12840 | }
|
12841 | constructor(key, iv, aes) {
|
12842 | this.aes = aes ? aes : new AES(key, iv, true, 'CFB');
|
12843 | delete this.aes.padding;
|
12844 | }
|
12845 | encrypt(data) {
|
12846 | const r1 = this.aes.AES_Encrypt_process(data);
|
12847 | const r2 = this.aes.AES_Encrypt_finish();
|
12848 | return joinBytes(r1, r2);
|
12849 | }
|
12850 | decrypt(data) {
|
12851 | const r1 = this.aes.AES_Decrypt_process(data);
|
12852 | const r2 = this.aes.AES_Decrypt_finish();
|
12853 | return joinBytes(r1, r2);
|
12854 | }
|
12855 | }
|
12856 |
|
12857 |
|
12858 |
|
12859 | const webCrypto$1 = util.getWebCrypto();
|
12860 | const nodeCrypto$1 = util.getNodeCrypto();
|
12861 | const Buffer$2 = util.getNodeBuffer();
|
12862 |
|
12863 | const knownAlgos = nodeCrypto$1 ? nodeCrypto$1.getCiphers() : [];
|
12864 | const nodeAlgos = {
|
12865 | idea: knownAlgos.includes('idea-cfb') ? 'idea-cfb' : undefined,
|
12866 | tripledes: knownAlgos.includes('des-ede3-cfb') ? 'des-ede3-cfb' : undefined,
|
12867 | cast5: knownAlgos.includes('cast5-cfb') ? 'cast5-cfb' : undefined,
|
12868 | blowfish: knownAlgos.includes('bf-cfb') ? 'bf-cfb' : undefined,
|
12869 | aes128: knownAlgos.includes('aes-128-cfb') ? 'aes-128-cfb' : undefined,
|
12870 | aes192: knownAlgos.includes('aes-192-cfb') ? 'aes-192-cfb' : undefined,
|
12871 | aes256: knownAlgos.includes('aes-256-cfb') ? 'aes-256-cfb' : undefined
|
12872 |
|
12873 | };
|
12874 |
|
12875 | async function encrypt(algo, key, plaintext, iv, config) {
|
12876 | if (util.getNodeCrypto() && nodeAlgos[algo]) {
|
12877 | return nodeEncrypt(algo, key, plaintext, iv);
|
12878 | }
|
12879 | if (algo.substr(0, 3) === 'aes') {
|
12880 | return aesEncrypt(algo, key, plaintext, iv, config);
|
12881 | }
|
12882 |
|
12883 | const cipherfn = new cipher[algo](key);
|
12884 | const block_size = cipherfn.blockSize;
|
12885 |
|
12886 | const blockc = iv.slice();
|
12887 | let pt = new Uint8Array();
|
12888 | const process = chunk => {
|
12889 | if (chunk) {
|
12890 | pt = util.concatUint8Array([pt, chunk]);
|
12891 | }
|
12892 | const ciphertext = new Uint8Array(pt.length);
|
12893 | let i;
|
12894 | let j = 0;
|
12895 | while (chunk ? pt.length >= block_size : pt.length) {
|
12896 | const encblock = cipherfn.encrypt(blockc);
|
12897 | for (i = 0; i < block_size; i++) {
|
12898 | blockc[i] = pt[i] ^ encblock[i];
|
12899 | ciphertext[j++] = blockc[i];
|
12900 | }
|
12901 | pt = pt.subarray(block_size);
|
12902 | }
|
12903 | return ciphertext.subarray(0, j);
|
12904 | };
|
12905 | return stream.transform(plaintext, process, process);
|
12906 | }
|
12907 |
|
12908 | async function decrypt(algo, key, ciphertext, iv) {
|
12909 | if (util.getNodeCrypto() && nodeAlgos[algo]) {
|
12910 | return nodeDecrypt(algo, key, ciphertext, iv);
|
12911 | }
|
12912 | if (algo.substr(0, 3) === 'aes') {
|
12913 | return aesDecrypt(algo, key, ciphertext, iv);
|
12914 | }
|
12915 |
|
12916 | const cipherfn = new cipher[algo](key);
|
12917 | const block_size = cipherfn.blockSize;
|
12918 |
|
12919 | let blockp = iv;
|
12920 | let ct = new Uint8Array();
|
12921 | const process = chunk => {
|
12922 | if (chunk) {
|
12923 | ct = util.concatUint8Array([ct, chunk]);
|
12924 | }
|
12925 | const plaintext = new Uint8Array(ct.length);
|
12926 | let i;
|
12927 | let j = 0;
|
12928 | while (chunk ? ct.length >= block_size : ct.length) {
|
12929 | const decblock = cipherfn.encrypt(blockp);
|
12930 | blockp = ct;
|
12931 | for (i = 0; i < block_size; i++) {
|
12932 | plaintext[j++] = blockp[i] ^ decblock[i];
|
12933 | }
|
12934 | ct = ct.subarray(block_size);
|
12935 | }
|
12936 | return plaintext.subarray(0, j);
|
12937 | };
|
12938 | return stream.transform(ciphertext, process, process);
|
12939 | }
|
12940 |
|
12941 | function aesEncrypt(algo, key, pt, iv, config) {
|
12942 | if (
|
12943 | util.getWebCrypto() &&
|
12944 | key.length !== 24 &&
|
12945 | !util.isStream(pt) &&
|
12946 | pt.length >= 3000 * config.minBytesForWebCrypto
|
12947 | ) {
|
12948 | return webEncrypt(algo, key, pt, iv);
|
12949 | }
|
12950 |
|
12951 | const cfb = new AES_CFB(key, iv);
|
12952 | return stream.transform(pt, value => cfb.aes.AES_Encrypt_process(value), () => cfb.aes.AES_Encrypt_finish());
|
12953 | }
|
12954 |
|
12955 | function aesDecrypt(algo, key, ct, iv) {
|
12956 | if (util.isStream(ct)) {
|
12957 | const cfb = new AES_CFB(key, iv);
|
12958 | return stream.transform(ct, value => cfb.aes.AES_Decrypt_process(value), () => cfb.aes.AES_Decrypt_finish());
|
12959 | }
|
12960 | return AES_CFB.decrypt(ct, key, iv);
|
12961 | }
|
12962 |
|
12963 | function xorMut(a, b) {
|
12964 | for (let i = 0; i < a.length; i++) {
|
12965 | a[i] = a[i] ^ b[i];
|
12966 | }
|
12967 | }
|
12968 |
|
12969 | async function webEncrypt(algo, key, pt, iv) {
|
12970 | const ALGO = 'AES-CBC';
|
12971 | const _key = await webCrypto$1.importKey('raw', key, { name: ALGO }, false, ['encrypt']);
|
12972 | const { blockSize } = cipher[algo];
|
12973 | const cbc_pt = util.concatUint8Array([new Uint8Array(blockSize), pt]);
|
12974 | const ct = new Uint8Array(await webCrypto$1.encrypt({ name: ALGO, iv }, _key, cbc_pt)).subarray(0, pt.length);
|
12975 | xorMut(ct, pt);
|
12976 | return ct;
|
12977 | }
|
12978 |
|
12979 | function nodeEncrypt(algo, key, pt, iv) {
|
12980 | key = Buffer$2.from(key);
|
12981 | iv = Buffer$2.from(iv);
|
12982 | const cipherObj = new nodeCrypto$1.createCipheriv(nodeAlgos[algo], key, iv);
|
12983 | return stream.transform(pt, value => new Uint8Array(cipherObj.update(Buffer$2.from(value))));
|
12984 | }
|
12985 |
|
12986 | function nodeDecrypt(algo, key, ct, iv) {
|
12987 | key = Buffer$2.from(key);
|
12988 | iv = Buffer$2.from(iv);
|
12989 | const decipherObj = new nodeCrypto$1.createDecipheriv(nodeAlgos[algo], key, iv);
|
12990 | return stream.transform(ct, value => new Uint8Array(decipherObj.update(Buffer$2.from(value))));
|
12991 | }
|
12992 |
|
12993 | var cfb = Object.freeze({
|
12994 | __proto__: null,
|
12995 | encrypt: encrypt,
|
12996 | decrypt: decrypt
|
12997 | });
|
12998 |
|
12999 | const _AES_GCM_data_maxLength = 68719476704;
|
13000 | class AES_GCM {
|
13001 | constructor(key, nonce, adata, tagSize = 16, aes) {
|
13002 | this.tagSize = tagSize;
|
13003 | this.gamma0 = 0;
|
13004 | this.counter = 1;
|
13005 | this.aes = aes ? aes : new AES(key, undefined, false, 'CTR');
|
13006 | let { asm, heap } = this.aes.acquire_asm();
|
13007 |
|
13008 | asm.gcm_init();
|
13009 |
|
13010 | if (this.tagSize < 4 || this.tagSize > 16)
|
13011 | throw new IllegalArgumentError('illegal tagSize value');
|
13012 |
|
13013 | const noncelen = nonce.length || 0;
|
13014 | const noncebuf = new Uint8Array(16);
|
13015 | if (noncelen !== 12) {
|
13016 | this._gcm_mac_process(nonce);
|
13017 | heap[0] = 0;
|
13018 | heap[1] = 0;
|
13019 | heap[2] = 0;
|
13020 | heap[3] = 0;
|
13021 | heap[4] = 0;
|
13022 | heap[5] = 0;
|
13023 | heap[6] = 0;
|
13024 | heap[7] = 0;
|
13025 | heap[8] = 0;
|
13026 | heap[9] = 0;
|
13027 | heap[10] = 0;
|
13028 | heap[11] = noncelen >>> 29;
|
13029 | heap[12] = (noncelen >>> 21) & 255;
|
13030 | heap[13] = (noncelen >>> 13) & 255;
|
13031 | heap[14] = (noncelen >>> 5) & 255;
|
13032 | heap[15] = (noncelen << 3) & 255;
|
13033 | asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA, 16);
|
13034 | asm.get_iv(AES_asm.HEAP_DATA);
|
13035 | asm.set_iv(0, 0, 0, 0);
|
13036 | noncebuf.set(heap.subarray(0, 16));
|
13037 | }
|
13038 | else {
|
13039 | noncebuf.set(nonce);
|
13040 | noncebuf[15] = 1;
|
13041 | }
|
13042 | const nonceview = new DataView(noncebuf.buffer);
|
13043 | this.gamma0 = nonceview.getUint32(12);
|
13044 | asm.set_nonce(nonceview.getUint32(0), nonceview.getUint32(4), nonceview.getUint32(8), 0);
|
13045 | asm.set_mask(0, 0, 0, 0xffffffff);
|
13046 |
|
13047 | if (adata !== undefined) {
|
13048 | if (adata.length > _AES_GCM_data_maxLength)
|
13049 | throw new IllegalArgumentError('illegal adata length');
|
13050 | if (adata.length) {
|
13051 | this.adata = adata;
|
13052 | this._gcm_mac_process(adata);
|
13053 | }
|
13054 | else {
|
13055 | this.adata = undefined;
|
13056 | }
|
13057 | }
|
13058 | else {
|
13059 | this.adata = undefined;
|
13060 | }
|
13061 |
|
13062 | if (this.counter < 1 || this.counter > 0xffffffff)
|
13063 | throw new RangeError('counter must be a positive 32-bit integer');
|
13064 | asm.set_counter(0, 0, 0, (this.gamma0 + this.counter) | 0);
|
13065 | }
|
13066 | static encrypt(cleartext, key, nonce, adata, tagsize) {
|
13067 | return new AES_GCM(key, nonce, adata, tagsize).encrypt(cleartext);
|
13068 | }
|
13069 | static decrypt(ciphertext, key, nonce, adata, tagsize) {
|
13070 | return new AES_GCM(key, nonce, adata, tagsize).decrypt(ciphertext);
|
13071 | }
|
13072 | encrypt(data) {
|
13073 | return this.AES_GCM_encrypt(data);
|
13074 | }
|
13075 | decrypt(data) {
|
13076 | return this.AES_GCM_decrypt(data);
|
13077 | }
|
13078 | AES_GCM_Encrypt_process(data) {
|
13079 | let dpos = 0;
|
13080 | let dlen = data.length || 0;
|
13081 | let { asm, heap } = this.aes.acquire_asm();
|
13082 | let counter = this.counter;
|
13083 | let pos = this.aes.pos;
|
13084 | let len = this.aes.len;
|
13085 | let rpos = 0;
|
13086 | let rlen = (len + dlen) & -16;
|
13087 | let wlen = 0;
|
13088 | if (((counter - 1) << 4) + len + dlen > _AES_GCM_data_maxLength)
|
13089 | throw new RangeError('counter overflow');
|
13090 | const result = new Uint8Array(rlen);
|
13091 | while (dlen > 0) {
|
13092 | wlen = _heap_write(heap, pos + len, data, dpos, dlen);
|
13093 | len += wlen;
|
13094 | dpos += wlen;
|
13095 | dlen -= wlen;
|
13096 | wlen = asm.cipher(AES_asm.ENC.CTR, AES_asm.HEAP_DATA + pos, len);
|
13097 | wlen = asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, wlen);
|
13098 | if (wlen)
|
13099 | result.set(heap.subarray(pos, pos + wlen), rpos);
|
13100 | counter += wlen >>> 4;
|
13101 | rpos += wlen;
|
13102 | if (wlen < len) {
|
13103 | pos += wlen;
|
13104 | len -= wlen;
|
13105 | }
|
13106 | else {
|
13107 | pos = 0;
|
13108 | len = 0;
|
13109 | }
|
13110 | }
|
13111 | this.counter = counter;
|
13112 | this.aes.pos = pos;
|
13113 | this.aes.len = len;
|
13114 | return result;
|
13115 | }
|
13116 | AES_GCM_Encrypt_finish() {
|
13117 | let { asm, heap } = this.aes.acquire_asm();
|
13118 | let counter = this.counter;
|
13119 | let tagSize = this.tagSize;
|
13120 | let adata = this.adata;
|
13121 | let pos = this.aes.pos;
|
13122 | let len = this.aes.len;
|
13123 | const result = new Uint8Array(len + tagSize);
|
13124 | asm.cipher(AES_asm.ENC.CTR, AES_asm.HEAP_DATA + pos, (len + 15) & -16);
|
13125 | if (len)
|
13126 | result.set(heap.subarray(pos, pos + len));
|
13127 | let i = len;
|
13128 | for (; i & 15; i++)
|
13129 | heap[pos + i] = 0;
|
13130 | asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, i);
|
13131 | const alen = adata !== undefined ? adata.length : 0;
|
13132 | const clen = ((counter - 1) << 4) + len;
|
13133 | heap[0] = 0;
|
13134 | heap[1] = 0;
|
13135 | heap[2] = 0;
|
13136 | heap[3] = alen >>> 29;
|
13137 | heap[4] = alen >>> 21;
|
13138 | heap[5] = (alen >>> 13) & 255;
|
13139 | heap[6] = (alen >>> 5) & 255;
|
13140 | heap[7] = (alen << 3) & 255;
|
13141 | heap[8] = heap[9] = heap[10] = 0;
|
13142 | heap[11] = clen >>> 29;
|
13143 | heap[12] = (clen >>> 21) & 255;
|
13144 | heap[13] = (clen >>> 13) & 255;
|
13145 | heap[14] = (clen >>> 5) & 255;
|
13146 | heap[15] = (clen << 3) & 255;
|
13147 | asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA, 16);
|
13148 | asm.get_iv(AES_asm.HEAP_DATA);
|
13149 | asm.set_counter(0, 0, 0, this.gamma0);
|
13150 | asm.cipher(AES_asm.ENC.CTR, AES_asm.HEAP_DATA, 16);
|
13151 | result.set(heap.subarray(0, tagSize), len);
|
13152 | this.counter = 1;
|
13153 | this.aes.pos = 0;
|
13154 | this.aes.len = 0;
|
13155 | return result;
|
13156 | }
|
13157 | AES_GCM_Decrypt_process(data) {
|
13158 | let dpos = 0;
|
13159 | let dlen = data.length || 0;
|
13160 | let { asm, heap } = this.aes.acquire_asm();
|
13161 | let counter = this.counter;
|
13162 | let tagSize = this.tagSize;
|
13163 | let pos = this.aes.pos;
|
13164 | let len = this.aes.len;
|
13165 | let rpos = 0;
|
13166 | let rlen = len + dlen > tagSize ? (len + dlen - tagSize) & -16 : 0;
|
13167 | let tlen = len + dlen - rlen;
|
13168 | let wlen = 0;
|
13169 | if (((counter - 1) << 4) + len + dlen > _AES_GCM_data_maxLength)
|
13170 | throw new RangeError('counter overflow');
|
13171 | const result = new Uint8Array(rlen);
|
13172 | while (dlen > tlen) {
|
13173 | wlen = _heap_write(heap, pos + len, data, dpos, dlen - tlen);
|
13174 | len += wlen;
|
13175 | dpos += wlen;
|
13176 | dlen -= wlen;
|
13177 | wlen = asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, wlen);
|
13178 | wlen = asm.cipher(AES_asm.DEC.CTR, AES_asm.HEAP_DATA + pos, wlen);
|
13179 | if (wlen)
|
13180 | result.set(heap.subarray(pos, pos + wlen), rpos);
|
13181 | counter += wlen >>> 4;
|
13182 | rpos += wlen;
|
13183 | pos = 0;
|
13184 | len = 0;
|
13185 | }
|
13186 | if (dlen > 0) {
|
13187 | len += _heap_write(heap, 0, data, dpos, dlen);
|
13188 | }
|
13189 | this.counter = counter;
|
13190 | this.aes.pos = pos;
|
13191 | this.aes.len = len;
|
13192 | return result;
|
13193 | }
|
13194 | AES_GCM_Decrypt_finish() {
|
13195 | let { asm, heap } = this.aes.acquire_asm();
|
13196 | let tagSize = this.tagSize;
|
13197 | let adata = this.adata;
|
13198 | let counter = this.counter;
|
13199 | let pos = this.aes.pos;
|
13200 | let len = this.aes.len;
|
13201 | let rlen = len - tagSize;
|
13202 | if (len < tagSize)
|
13203 | throw new IllegalStateError('authentication tag not found');
|
13204 | const result = new Uint8Array(rlen);
|
13205 | const atag = new Uint8Array(heap.subarray(pos + rlen, pos + len));
|
13206 | let i = rlen;
|
13207 | for (; i & 15; i++)
|
13208 | heap[pos + i] = 0;
|
13209 | asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, i);
|
13210 | asm.cipher(AES_asm.DEC.CTR, AES_asm.HEAP_DATA + pos, i);
|
13211 | if (rlen)
|
13212 | result.set(heap.subarray(pos, pos + rlen));
|
13213 | const alen = adata !== undefined ? adata.length : 0;
|
13214 | const clen = ((counter - 1) << 4) + len - tagSize;
|
13215 | heap[0] = 0;
|
13216 | heap[1] = 0;
|
13217 | heap[2] = 0;
|
13218 | heap[3] = alen >>> 29;
|
13219 | heap[4] = alen >>> 21;
|
13220 | heap[5] = (alen >>> 13) & 255;
|
13221 | heap[6] = (alen >>> 5) & 255;
|
13222 | heap[7] = (alen << 3) & 255;
|
13223 | heap[8] = heap[9] = heap[10] = 0;
|
13224 | heap[11] = clen >>> 29;
|
13225 | heap[12] = (clen >>> 21) & 255;
|
13226 | heap[13] = (clen >>> 13) & 255;
|
13227 | heap[14] = (clen >>> 5) & 255;
|
13228 | heap[15] = (clen << 3) & 255;
|
13229 | asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA, 16);
|
13230 | asm.get_iv(AES_asm.HEAP_DATA);
|
13231 | asm.set_counter(0, 0, 0, this.gamma0);
|
13232 | asm.cipher(AES_asm.ENC.CTR, AES_asm.HEAP_DATA, 16);
|
13233 | let acheck = 0;
|
13234 | for (let i = 0; i < tagSize; ++i)
|
13235 | acheck |= atag[i] ^ heap[i];
|
13236 | if (acheck)
|
13237 | throw new SecurityError('data integrity check failed');
|
13238 | this.counter = 1;
|
13239 | this.aes.pos = 0;
|
13240 | this.aes.len = 0;
|
13241 | return result;
|
13242 | }
|
13243 | AES_GCM_decrypt(data) {
|
13244 | const result1 = this.AES_GCM_Decrypt_process(data);
|
13245 | const result2 = this.AES_GCM_Decrypt_finish();
|
13246 | const result = new Uint8Array(result1.length + result2.length);
|
13247 | if (result1.length)
|
13248 | result.set(result1);
|
13249 | if (result2.length)
|
13250 | result.set(result2, result1.length);
|
13251 | return result;
|
13252 | }
|
13253 | AES_GCM_encrypt(data) {
|
13254 | const result1 = this.AES_GCM_Encrypt_process(data);
|
13255 | const result2 = this.AES_GCM_Encrypt_finish();
|
13256 | const result = new Uint8Array(result1.length + result2.length);
|
13257 | if (result1.length)
|
13258 | result.set(result1);
|
13259 | if (result2.length)
|
13260 | result.set(result2, result1.length);
|
13261 | return result;
|
13262 | }
|
13263 | _gcm_mac_process(data) {
|
13264 | let { asm, heap } = this.aes.acquire_asm();
|
13265 | let dpos = 0;
|
13266 | let dlen = data.length || 0;
|
13267 | let wlen = 0;
|
13268 | while (dlen > 0) {
|
13269 | wlen = _heap_write(heap, 0, data, dpos, dlen);
|
13270 | dpos += wlen;
|
13271 | dlen -= wlen;
|
13272 | while (wlen & 15)
|
13273 | heap[wlen++] = 0;
|
13274 | asm.mac(AES_asm.MAC.GCM, AES_asm.HEAP_DATA, wlen);
|
13275 | }
|
13276 | }
|
13277 | }
|
13278 |
|
13279 |
|
13280 |
|
13281 | const webCrypto$2 = util.getWebCrypto();
|
13282 | const nodeCrypto$2 = util.getNodeCrypto();
|
13283 | const Buffer$3 = util.getNodeBuffer();
|
13284 |
|
13285 | const blockLength = 16;
|
13286 | const ivLength = 12;
|
13287 | const tagLength = 16;
|
13288 | const ALGO = 'AES-GCM';
|
13289 |
|
13290 |
|
13291 |
|
13292 |
|
13293 |
|
13294 |
|
13295 | async function GCM(cipher, key) {
|
13296 | if (cipher.substr(0, 3) !== 'aes') {
|
13297 | throw new Error('GCM mode supports only AES cipher');
|
13298 | }
|
13299 |
|
13300 | if (util.getWebCrypto() && key.length !== 24) {
|
13301 | const _key = await webCrypto$2.importKey('raw', key, { name: ALGO }, false, ['encrypt', 'decrypt']);
|
13302 |
|
13303 | return {
|
13304 | encrypt: async function(pt, iv, adata = new Uint8Array()) {
|
13305 | if (
|
13306 | !pt.length ||
|
13307 |
|
13308 |
|
13309 | (!adata.length && navigator.userAgent.indexOf('Edge') !== -1)
|
13310 |
|
13311 | ) {
|
13312 | return AES_GCM.encrypt(pt, key, iv, adata);
|
13313 | }
|
13314 | const ct = await webCrypto$2.encrypt({ name: ALGO, iv, additionalData: adata, tagLength: tagLength * 8 }, _key, pt);
|
13315 | return new Uint8Array(ct);
|
13316 | },
|
13317 |
|
13318 | decrypt: async function(ct, iv, adata = new Uint8Array()) {
|
13319 | if (
|
13320 | ct.length === tagLength ||
|
13321 |
|
13322 |
|
13323 | (!adata.length && navigator.userAgent.indexOf('Edge') !== -1)
|
13324 |
|
13325 | ) {
|
13326 | return AES_GCM.decrypt(ct, key, iv, adata);
|
13327 | }
|
13328 | const pt = await webCrypto$2.decrypt({ name: ALGO, iv, additionalData: adata, tagLength: tagLength * 8 }, _key, ct);
|
13329 | return new Uint8Array(pt);
|
13330 | }
|
13331 | };
|
13332 | }
|
13333 |
|
13334 | if (util.getNodeCrypto()) {
|
13335 | key = Buffer$3.from(key);
|
13336 |
|
13337 | return {
|
13338 | encrypt: async function(pt, iv, adata = new Uint8Array()) {
|
13339 | pt = Buffer$3.from(pt);
|
13340 | iv = Buffer$3.from(iv);
|
13341 | adata = Buffer$3.from(adata);
|
13342 | const en = new nodeCrypto$2.createCipheriv('aes-' + (key.length * 8) + '-gcm', key, iv);
|
13343 | en.setAAD(adata);
|
13344 | const ct = Buffer$3.concat([en.update(pt), en.final(), en.getAuthTag()]);
|
13345 | return new Uint8Array(ct);
|
13346 | },
|
13347 |
|
13348 | decrypt: async function(ct, iv, adata = new Uint8Array()) {
|
13349 | ct = Buffer$3.from(ct);
|
13350 | iv = Buffer$3.from(iv);
|
13351 | adata = Buffer$3.from(adata);
|
13352 | const de = new nodeCrypto$2.createDecipheriv('aes-' + (key.length * 8) + '-gcm', key, iv);
|
13353 | de.setAAD(adata);
|
13354 | de.setAuthTag(ct.slice(ct.length - tagLength, ct.length));
|
13355 | const pt = Buffer$3.concat([de.update(ct.slice(0, ct.length - tagLength)), de.final()]);
|
13356 | return new Uint8Array(pt);
|
13357 | }
|
13358 | };
|
13359 | }
|
13360 |
|
13361 | return {
|
13362 | encrypt: async function(pt, iv, adata) {
|
13363 | return AES_GCM.encrypt(pt, key, iv, adata);
|
13364 | },
|
13365 |
|
13366 | decrypt: async function(ct, iv, adata) {
|
13367 | return AES_GCM.decrypt(ct, key, iv, adata);
|
13368 | }
|
13369 | };
|
13370 | }
|
13371 |
|
13372 |
|
13373 |
|
13374 |
|
13375 |
|
13376 |
|
13377 |
|
13378 |
|
13379 |
|
13380 |
|
13381 | GCM.getNonce = function(iv, chunkIndex) {
|
13382 | const nonce = iv.slice();
|
13383 | for (let i = 0; i < chunkIndex.length; i++) {
|
13384 | nonce[4 + i] ^= chunkIndex[i];
|
13385 | }
|
13386 | return nonce;
|
13387 | };
|
13388 |
|
13389 | GCM.blockLength = blockLength;
|
13390 | GCM.ivLength = ivLength;
|
13391 | GCM.tagLength = tagLength;
|
13392 |
|
13393 | class AES_CTR {
|
13394 | static encrypt(data, key, nonce) {
|
13395 | return new AES_CTR(key, nonce).encrypt(data);
|
13396 | }
|
13397 | static decrypt(data, key, nonce) {
|
13398 | return new AES_CTR(key, nonce).encrypt(data);
|
13399 | }
|
13400 | constructor(key, nonce, aes) {
|
13401 | this.aes = aes ? aes : new AES(key, undefined, false, 'CTR');
|
13402 | delete this.aes.padding;
|
13403 | this.AES_CTR_set_options(nonce);
|
13404 | }
|
13405 | encrypt(data) {
|
13406 | const r1 = this.aes.AES_Encrypt_process(data);
|
13407 | const r2 = this.aes.AES_Encrypt_finish();
|
13408 | return joinBytes(r1, r2);
|
13409 | }
|
13410 | decrypt(data) {
|
13411 | const r1 = this.aes.AES_Encrypt_process(data);
|
13412 | const r2 = this.aes.AES_Encrypt_finish();
|
13413 | return joinBytes(r1, r2);
|
13414 | }
|
13415 | AES_CTR_set_options(nonce, counter, size) {
|
13416 | let { asm } = this.aes.acquire_asm();
|
13417 | if (size !== undefined) {
|
13418 | if (size < 8 || size > 48)
|
13419 | throw new IllegalArgumentError('illegal counter size');
|
13420 | let mask = Math.pow(2, size) - 1;
|
13421 | asm.set_mask(0, 0, (mask / 0x100000000) | 0, mask | 0);
|
13422 | }
|
13423 | else {
|
13424 | size = 48;
|
13425 | asm.set_mask(0, 0, 0xffff, 0xffffffff);
|
13426 | }
|
13427 | if (nonce !== undefined) {
|
13428 | let len = nonce.length;
|
13429 | if (!len || len > 16)
|
13430 | throw new IllegalArgumentError('illegal nonce size');
|
13431 | let view = new DataView(new ArrayBuffer(16));
|
13432 | new Uint8Array(view.buffer).set(nonce);
|
13433 | asm.set_nonce(view.getUint32(0), view.getUint32(4), view.getUint32(8), view.getUint32(12));
|
13434 | }
|
13435 | else {
|
13436 | throw new Error('nonce is required');
|
13437 | }
|
13438 | if (counter !== undefined) {
|
13439 | if (counter < 0 || counter >= Math.pow(2, size))
|
13440 | throw new IllegalArgumentError('illegal counter value');
|
13441 | asm.set_counter(0, 0, (counter / 0x100000000) | 0, counter | 0);
|
13442 | }
|
13443 | }
|
13444 | }
|
13445 |
|
13446 | class AES_CBC {
|
13447 | static encrypt(data, key, padding = true, iv) {
|
13448 | return new AES_CBC(key, iv, padding).encrypt(data);
|
13449 | }
|
13450 | static decrypt(data, key, padding = true, iv) {
|
13451 | return new AES_CBC(key, iv, padding).decrypt(data);
|
13452 | }
|
13453 | constructor(key, iv, padding = true, aes) {
|
13454 | this.aes = aes ? aes : new AES(key, iv, padding, 'CBC');
|
13455 | }
|
13456 | encrypt(data) {
|
13457 | const r1 = this.aes.AES_Encrypt_process(data);
|
13458 | const r2 = this.aes.AES_Encrypt_finish();
|
13459 | return joinBytes(r1, r2);
|
13460 | }
|
13461 | decrypt(data) {
|
13462 | const r1 = this.aes.AES_Decrypt_process(data);
|
13463 | const r2 = this.aes.AES_Decrypt_finish();
|
13464 | return joinBytes(r1, r2);
|
13465 | }
|
13466 | }
|
13467 |
|
13468 |
|
13469 |
|
13470 |
|
13471 |
|
13472 |
|
13473 |
|
13474 |
|
13475 | const webCrypto$3 = util.getWebCrypto();
|
13476 | const nodeCrypto$3 = util.getNodeCrypto();
|
13477 | const Buffer$4 = util.getNodeBuffer();
|
13478 |
|
13479 |
|
13480 |
|
13481 |
|
13482 |
|
13483 |
|
13484 |
|
13485 |
|
13486 |
|
13487 |
|
13488 |
|
13489 |
|
13490 |
|
13491 |
|
13492 |
|
13493 | const blockLength$1 = 16;
|
13494 |
|
13495 |
|
13496 |
|
13497 |
|
13498 |
|
13499 |
|
13500 |
|
13501 |
|
13502 |
|
13503 |
|
13504 | function rightXorMut(data, padding) {
|
13505 | const offset = data.length - blockLength$1;
|
13506 | for (let i = 0; i < blockLength$1; i++) {
|
13507 | data[i + offset] ^= padding[i];
|
13508 | }
|
13509 | return data;
|
13510 | }
|
13511 |
|
13512 | function pad(data, padding, padding2) {
|
13513 |
|
13514 | if (data.length && data.length % blockLength$1 === 0) {
|
13515 |
|
13516 | return rightXorMut(data, padding);
|
13517 | }
|
13518 |
|
13519 | const padded = new Uint8Array(data.length + (blockLength$1 - data.length % blockLength$1));
|
13520 | padded.set(data);
|
13521 | padded[data.length] = 0b10000000;
|
13522 | return rightXorMut(padded, padding2);
|
13523 | }
|
13524 |
|
13525 | const zeroBlock = new Uint8Array(blockLength$1);
|
13526 |
|
13527 | async function CMAC(key) {
|
13528 | const cbc = await CBC(key);
|
13529 |
|
13530 |
|
13531 | const padding = util.double(await cbc(zeroBlock));
|
13532 | const padding2 = util.double(padding);
|
13533 |
|
13534 | return async function(data) {
|
13535 |
|
13536 | return (await cbc(pad(data, padding, padding2))).subarray(-blockLength$1);
|
13537 | };
|
13538 | }
|
13539 |
|
13540 | async function CBC(key) {
|
13541 | if (util.getWebCrypto() && key.length !== 24) {
|
13542 | key = await webCrypto$3.importKey('raw', key, { name: 'AES-CBC', length: key.length * 8 }, false, ['encrypt']);
|
13543 | return async function(pt) {
|
13544 | const ct = await webCrypto$3.encrypt({ name: 'AES-CBC', iv: zeroBlock, length: blockLength$1 * 8 }, key, pt);
|
13545 | return new Uint8Array(ct).subarray(0, ct.byteLength - blockLength$1);
|
13546 | };
|
13547 | }
|
13548 | if (util.getNodeCrypto()) {
|
13549 | key = Buffer$4.from(key);
|
13550 | return async function(pt) {
|
13551 | pt = Buffer$4.from(pt);
|
13552 | const en = new nodeCrypto$3.createCipheriv('aes-' + (key.length * 8) + '-cbc', key, zeroBlock);
|
13553 | const ct = en.update(pt);
|
13554 | return new Uint8Array(ct);
|
13555 | };
|
13556 | }
|
13557 |
|
13558 | return async function(pt) {
|
13559 | return AES_CBC.encrypt(pt, key, false, zeroBlock);
|
13560 | };
|
13561 | }
|
13562 |
|
13563 |
|
13564 |
|
13565 | const webCrypto$4 = util.getWebCrypto();
|
13566 | const nodeCrypto$4 = util.getNodeCrypto();
|
13567 | const Buffer$5 = util.getNodeBuffer();
|
13568 |
|
13569 |
|
13570 | const blockLength$2 = 16;
|
13571 | const ivLength$1 = blockLength$2;
|
13572 | const tagLength$1 = blockLength$2;
|
13573 |
|
13574 | const zero = new Uint8Array(blockLength$2);
|
13575 | const one = new Uint8Array(blockLength$2); one[blockLength$2 - 1] = 1;
|
13576 | const two = new Uint8Array(blockLength$2); two[blockLength$2 - 1] = 2;
|
13577 |
|
13578 | async function OMAC(key) {
|
13579 | const cmac = await CMAC(key);
|
13580 | return function(t, message) {
|
13581 | return cmac(util.concatUint8Array([t, message]));
|
13582 | };
|
13583 | }
|
13584 |
|
13585 | async function CTR(key) {
|
13586 | if (
|
13587 | util.getWebCrypto() &&
|
13588 | key.length !== 24 &&
|
13589 | navigator.userAgent.indexOf('Edge') === -1
|
13590 | ) {
|
13591 | key = await webCrypto$4.importKey('raw', key, { name: 'AES-CTR', length: key.length * 8 }, false, ['encrypt']);
|
13592 | return async function(pt, iv) {
|
13593 | const ct = await webCrypto$4.encrypt({ name: 'AES-CTR', counter: iv, length: blockLength$2 * 8 }, key, pt);
|
13594 | return new Uint8Array(ct);
|
13595 | };
|
13596 | }
|
13597 | if (util.getNodeCrypto()) {
|
13598 | key = Buffer$5.from(key);
|
13599 | return async function(pt, iv) {
|
13600 | pt = Buffer$5.from(pt);
|
13601 | iv = Buffer$5.from(iv);
|
13602 | const en = new nodeCrypto$4.createCipheriv('aes-' + (key.length * 8) + '-ctr', key, iv);
|
13603 | const ct = Buffer$5.concat([en.update(pt), en.final()]);
|
13604 | return new Uint8Array(ct);
|
13605 | };
|
13606 | }
|
13607 |
|
13608 | return async function(pt, iv) {
|
13609 | return AES_CTR.encrypt(pt, key, iv);
|
13610 | };
|
13611 | }
|
13612 |
|
13613 |
|
13614 |
|
13615 |
|
13616 |
|
13617 |
|
13618 |
|
13619 | async function EAX(cipher, key) {
|
13620 | if (cipher.substr(0, 3) !== 'aes') {
|
13621 | throw new Error('EAX mode supports only AES cipher');
|
13622 | }
|
13623 |
|
13624 | const [
|
13625 | omac,
|
13626 | ctr
|
13627 | ] = await Promise.all([
|
13628 | OMAC(key),
|
13629 | CTR(key)
|
13630 | ]);
|
13631 |
|
13632 | return {
|
13633 | |
13634 |
|
13635 |
|
13636 |
|
13637 |
|
13638 |
|
13639 |
|
13640 |
|
13641 | encrypt: async function(plaintext, nonce, adata) {
|
13642 | const [
|
13643 | omacNonce,
|
13644 | omacAdata
|
13645 | ] = await Promise.all([
|
13646 | omac(zero, nonce),
|
13647 | omac(one, adata)
|
13648 | ]);
|
13649 | const ciphered = await ctr(plaintext, omacNonce);
|
13650 | const omacCiphered = await omac(two, ciphered);
|
13651 | const tag = omacCiphered;
|
13652 | for (let i = 0; i < tagLength$1; i++) {
|
13653 | tag[i] ^= omacAdata[i] ^ omacNonce[i];
|
13654 | }
|
13655 | return util.concatUint8Array([ciphered, tag]);
|
13656 | },
|
13657 |
|
13658 | |
13659 |
|
13660 |
|
13661 |
|
13662 |
|
13663 |
|
13664 |
|
13665 |
|
13666 | decrypt: async function(ciphertext, nonce, adata) {
|
13667 | if (ciphertext.length < tagLength$1) throw new Error('Invalid EAX ciphertext');
|
13668 | const ciphered = ciphertext.subarray(0, -tagLength$1);
|
13669 | const ctTag = ciphertext.subarray(-tagLength$1);
|
13670 | const [
|
13671 | omacNonce,
|
13672 | omacAdata,
|
13673 | omacCiphered
|
13674 | ] = await Promise.all([
|
13675 | omac(zero, nonce),
|
13676 | omac(one, adata),
|
13677 | omac(two, ciphered)
|
13678 | ]);
|
13679 | const tag = omacCiphered;
|
13680 | for (let i = 0; i < tagLength$1; i++) {
|
13681 | tag[i] ^= omacAdata[i] ^ omacNonce[i];
|
13682 | }
|
13683 | if (!util.equalsUint8Array(ctTag, tag)) throw new Error('Authentication tag mismatch');
|
13684 | const plaintext = await ctr(ciphered, omacNonce);
|
13685 | return plaintext;
|
13686 | }
|
13687 | };
|
13688 | }
|
13689 |
|
13690 |
|
13691 |
|
13692 |
|
13693 |
|
13694 |
|
13695 |
|
13696 | EAX.getNonce = function(iv, chunkIndex) {
|
13697 | const nonce = iv.slice();
|
13698 | for (let i = 0; i < chunkIndex.length; i++) {
|
13699 | nonce[8 + i] ^= chunkIndex[i];
|
13700 | }
|
13701 | return nonce;
|
13702 | };
|
13703 |
|
13704 | EAX.blockLength = blockLength$2;
|
13705 | EAX.ivLength = ivLength$1;
|
13706 | EAX.tagLength = tagLength$1;
|
13707 |
|
13708 |
|
13709 |
|
13710 |
|
13711 | const blockLength$3 = 16;
|
13712 | const ivLength$2 = 15;
|
13713 |
|
13714 |
|
13715 |
|
13716 |
|
13717 |
|
13718 | const tagLength$2 = 16;
|
13719 |
|
13720 |
|
13721 | function ntz(n) {
|
13722 | let ntz = 0;
|
13723 | for (let i = 1; (n & i) === 0; i <<= 1) {
|
13724 | ntz++;
|
13725 | }
|
13726 | return ntz;
|
13727 | }
|
13728 |
|
13729 | function xorMut$1(S, T) {
|
13730 | for (let i = 0; i < S.length; i++) {
|
13731 | S[i] ^= T[i];
|
13732 | }
|
13733 | return S;
|
13734 | }
|
13735 |
|
13736 | function xor(S, T) {
|
13737 | return xorMut$1(S.slice(), T);
|
13738 | }
|
13739 |
|
13740 | const zeroBlock$1 = new Uint8Array(blockLength$3);
|
13741 | const one$1 = new Uint8Array([1]);
|
13742 |
|
13743 |
|
13744 |
|
13745 |
|
13746 |
|
13747 |
|
13748 | async function OCB(cipher$1, key) {
|
13749 |
|
13750 | let maxNtz = 0;
|
13751 | let encipher;
|
13752 | let decipher;
|
13753 | let mask;
|
13754 |
|
13755 | constructKeyVariables(cipher$1, key);
|
13756 |
|
13757 | function constructKeyVariables(cipher$1, key) {
|
13758 | const aes = new cipher[cipher$1](key);
|
13759 | encipher = aes.encrypt.bind(aes);
|
13760 | decipher = aes.decrypt.bind(aes);
|
13761 |
|
13762 | const mask_x = encipher(zeroBlock$1);
|
13763 | const mask_$ = util.double(mask_x);
|
13764 | mask = [];
|
13765 | mask[0] = util.double(mask_$);
|
13766 |
|
13767 |
|
13768 | mask.x = mask_x;
|
13769 | mask.$ = mask_$;
|
13770 | }
|
13771 |
|
13772 | function extendKeyVariables(text, adata) {
|
13773 | const newMaxNtz = util.nbits(Math.max(text.length, adata.length) / blockLength$3 | 0) - 1;
|
13774 | for (let i = maxNtz + 1; i <= newMaxNtz; i++) {
|
13775 | mask[i] = util.double(mask[i - 1]);
|
13776 | }
|
13777 | maxNtz = newMaxNtz;
|
13778 | }
|
13779 |
|
13780 | function hash(adata) {
|
13781 | if (!adata.length) {
|
13782 |
|
13783 | return zeroBlock$1;
|
13784 | }
|
13785 |
|
13786 |
|
13787 |
|
13788 |
|
13789 | const m = adata.length / blockLength$3 | 0;
|
13790 |
|
13791 | const offset = new Uint8Array(blockLength$3);
|
13792 | const sum = new Uint8Array(blockLength$3);
|
13793 | for (let i = 0; i < m; i++) {
|
13794 | xorMut$1(offset, mask[ntz(i + 1)]);
|
13795 | xorMut$1(sum, encipher(xor(offset, adata)));
|
13796 | adata = adata.subarray(blockLength$3);
|
13797 | }
|
13798 |
|
13799 |
|
13800 |
|
13801 |
|
13802 | if (adata.length) {
|
13803 | xorMut$1(offset, mask.x);
|
13804 |
|
13805 | const cipherInput = new Uint8Array(blockLength$3);
|
13806 | cipherInput.set(adata, 0);
|
13807 | cipherInput[adata.length] = 0b10000000;
|
13808 | xorMut$1(cipherInput, offset);
|
13809 |
|
13810 | xorMut$1(sum, encipher(cipherInput));
|
13811 | }
|
13812 |
|
13813 | return sum;
|
13814 | }
|
13815 |
|
13816 | |
13817 |
|
13818 |
|
13819 |
|
13820 |
|
13821 |
|
13822 |
|
13823 |
|
13824 | function crypt(fn, text, nonce, adata) {
|
13825 |
|
13826 |
|
13827 |
|
13828 | const m = text.length / blockLength$3 | 0;
|
13829 |
|
13830 |
|
13831 |
|
13832 |
|
13833 | extendKeyVariables(text, adata);
|
13834 |
|
13835 |
|
13836 |
|
13837 |
|
13838 |
|
13839 |
|
13840 | const paddedNonce = util.concatUint8Array([zeroBlock$1.subarray(0, ivLength$2 - nonce.length), one$1, nonce]);
|
13841 |
|
13842 | const bottom = paddedNonce[blockLength$3 - 1] & 0b111111;
|
13843 |
|
13844 | paddedNonce[blockLength$3 - 1] &= 0b11000000;
|
13845 | const kTop = encipher(paddedNonce);
|
13846 |
|
13847 | const stretched = util.concatUint8Array([kTop, xor(kTop.subarray(0, 8), kTop.subarray(1, 9))]);
|
13848 |
|
13849 | const offset = util.shiftRight(stretched.subarray(0 + (bottom >> 3), 17 + (bottom >> 3)), 8 - (bottom & 7)).subarray(1);
|
13850 |
|
13851 | const checksum = new Uint8Array(blockLength$3);
|
13852 |
|
13853 | const ct = new Uint8Array(text.length + tagLength$2);
|
13854 |
|
13855 |
|
13856 |
|
13857 |
|
13858 | let i;
|
13859 | let pos = 0;
|
13860 | for (i = 0; i < m; i++) {
|
13861 |
|
13862 | xorMut$1(offset, mask[ntz(i + 1)]);
|
13863 |
|
13864 |
|
13865 | ct.set(xorMut$1(fn(xor(offset, text)), offset), pos);
|
13866 |
|
13867 | xorMut$1(checksum, fn === encipher ? text : ct.subarray(pos));
|
13868 |
|
13869 | text = text.subarray(blockLength$3);
|
13870 | pos += blockLength$3;
|
13871 | }
|
13872 |
|
13873 |
|
13874 |
|
13875 |
|
13876 | if (text.length) {
|
13877 |
|
13878 | xorMut$1(offset, mask.x);
|
13879 |
|
13880 | const padding = encipher(offset);
|
13881 |
|
13882 | ct.set(xor(text, padding), pos);
|
13883 |
|
13884 |
|
13885 | const xorInput = new Uint8Array(blockLength$3);
|
13886 | xorInput.set(fn === encipher ? text : ct.subarray(pos, -tagLength$2), 0);
|
13887 | xorInput[text.length] = 0b10000000;
|
13888 | xorMut$1(checksum, xorInput);
|
13889 | pos += text.length;
|
13890 | }
|
13891 |
|
13892 | const tag = xorMut$1(encipher(xorMut$1(xorMut$1(checksum, offset), mask.$)), hash(adata));
|
13893 |
|
13894 |
|
13895 |
|
13896 |
|
13897 |
|
13898 | ct.set(tag, pos);
|
13899 | return ct;
|
13900 | }
|
13901 |
|
13902 |
|
13903 | return {
|
13904 | |
13905 |
|
13906 |
|
13907 |
|
13908 |
|
13909 |
|
13910 |
|
13911 |
|
13912 | encrypt: async function(plaintext, nonce, adata) {
|
13913 | return crypt(encipher, plaintext, nonce, adata);
|
13914 | },
|
13915 |
|
13916 | |
13917 |
|
13918 |
|
13919 |
|
13920 |
|
13921 |
|
13922 |
|
13923 |
|
13924 | decrypt: async function(ciphertext, nonce, adata) {
|
13925 | if (ciphertext.length < tagLength$2) throw new Error('Invalid OCB ciphertext');
|
13926 |
|
13927 | const tag = ciphertext.subarray(-tagLength$2);
|
13928 | ciphertext = ciphertext.subarray(0, -tagLength$2);
|
13929 |
|
13930 | const crypted = crypt(decipher, ciphertext, nonce, adata);
|
13931 |
|
13932 | if (util.equalsUint8Array(tag, crypted.subarray(-tagLength$2))) {
|
13933 | return crypted.subarray(0, -tagLength$2);
|
13934 | }
|
13935 | throw new Error('Authentication tag mismatch');
|
13936 | }
|
13937 | };
|
13938 | }
|
13939 |
|
13940 |
|
13941 |
|
13942 |
|
13943 |
|
13944 |
|
13945 |
|
13946 | OCB.getNonce = function(iv, chunkIndex) {
|
13947 | const nonce = iv.slice();
|
13948 | for (let i = 0; i < chunkIndex.length; i++) {
|
13949 | nonce[7 + i] ^= chunkIndex[i];
|
13950 | }
|
13951 | return nonce;
|
13952 | };
|
13953 |
|
13954 | OCB.blockLength = blockLength$3;
|
13955 | OCB.ivLength = ivLength$2;
|
13956 | OCB.tagLength = tagLength$2;
|
13957 |
|
13958 | var naclFastLight = createCommonjsModule(function (module) {
|
13959 |
|
13960 |
|
13961 | (function(nacl) {
|
13962 |
|
13963 |
|
13964 |
|
13965 |
|
13966 |
|
13967 |
|
13968 |
|
13969 | var gf = function(init) {
|
13970 | var i, r = new Float64Array(16);
|
13971 | if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
|
13972 | return r;
|
13973 | };
|
13974 |
|
13975 |
|
13976 | var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };
|
13977 |
|
13978 | var _9 = new Uint8Array(32); _9[0] = 9;
|
13979 |
|
13980 | var gf0 = gf(),
|
13981 | gf1 = gf([1]),
|
13982 | _121665 = gf([0xdb41, 1]),
|
13983 | D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),
|
13984 | D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]),
|
13985 | X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]),
|
13986 | Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]),
|
13987 | I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
|
13988 |
|
13989 | function vn(x, xi, y, yi, n) {
|
13990 | var i,d = 0;
|
13991 | for (i = 0; i < n; i++) d |= x[xi+i]^y[yi+i];
|
13992 | return (1 & ((d - 1) >>> 8)) - 1;
|
13993 | }
|
13994 |
|
13995 | function crypto_verify_32(x, xi, y, yi) {
|
13996 | return vn(x,xi,y,yi,32);
|
13997 | }
|
13998 |
|
13999 | function set25519(r, a) {
|
14000 | var i;
|
14001 | for (i = 0; i < 16; i++) r[i] = a[i]|0;
|
14002 | }
|
14003 |
|
14004 | function car25519(o) {
|
14005 | var i, v, c = 1;
|
14006 | for (i = 0; i < 16; i++) {
|
14007 | v = o[i] + c + 65535;
|
14008 | c = Math.floor(v / 65536);
|
14009 | o[i] = v - c * 65536;
|
14010 | }
|
14011 | o[0] += c-1 + 37 * (c-1);
|
14012 | }
|
14013 |
|
14014 | function sel25519(p, q, b) {
|
14015 | var t, c = ~(b-1);
|
14016 | for (var i = 0; i < 16; i++) {
|
14017 | t = c & (p[i] ^ q[i]);
|
14018 | p[i] ^= t;
|
14019 | q[i] ^= t;
|
14020 | }
|
14021 | }
|
14022 |
|
14023 | function pack25519(o, n) {
|
14024 | var i, j, b;
|
14025 | var m = gf(), t = gf();
|
14026 | for (i = 0; i < 16; i++) t[i] = n[i];
|
14027 | car25519(t);
|
14028 | car25519(t);
|
14029 | car25519(t);
|
14030 | for (j = 0; j < 2; j++) {
|
14031 | m[0] = t[0] - 0xffed;
|
14032 | for (i = 1; i < 15; i++) {
|
14033 | m[i] = t[i] - 0xffff - ((m[i-1]>>16) & 1);
|
14034 | m[i-1] &= 0xffff;
|
14035 | }
|
14036 | m[15] = t[15] - 0x7fff - ((m[14]>>16) & 1);
|
14037 | b = (m[15]>>16) & 1;
|
14038 | m[14] &= 0xffff;
|
14039 | sel25519(t, m, 1-b);
|
14040 | }
|
14041 | for (i = 0; i < 16; i++) {
|
14042 | o[2*i] = t[i] & 0xff;
|
14043 | o[2*i+1] = t[i]>>8;
|
14044 | }
|
14045 | }
|
14046 |
|
14047 | function neq25519(a, b) {
|
14048 | var c = new Uint8Array(32), d = new Uint8Array(32);
|
14049 | pack25519(c, a);
|
14050 | pack25519(d, b);
|
14051 | return crypto_verify_32(c, 0, d, 0);
|
14052 | }
|
14053 |
|
14054 | function par25519(a) {
|
14055 | var d = new Uint8Array(32);
|
14056 | pack25519(d, a);
|
14057 | return d[0] & 1;
|
14058 | }
|
14059 |
|
14060 | function unpack25519(o, n) {
|
14061 | var i;
|
14062 | for (i = 0; i < 16; i++) o[i] = n[2*i] + (n[2*i+1] << 8);
|
14063 | o[15] &= 0x7fff;
|
14064 | }
|
14065 |
|
14066 | function A(o, a, b) {
|
14067 | for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
|
14068 | }
|
14069 |
|
14070 | function Z(o, a, b) {
|
14071 | for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
|
14072 | }
|
14073 |
|
14074 | function M(o, a, b) {
|
14075 | var v, c,
|
14076 | t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
|
14077 | t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,
|
14078 | t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0,
|
14079 | t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0,
|
14080 | b0 = b[0],
|
14081 | b1 = b[1],
|
14082 | b2 = b[2],
|
14083 | b3 = b[3],
|
14084 | b4 = b[4],
|
14085 | b5 = b[5],
|
14086 | b6 = b[6],
|
14087 | b7 = b[7],
|
14088 | b8 = b[8],
|
14089 | b9 = b[9],
|
14090 | b10 = b[10],
|
14091 | b11 = b[11],
|
14092 | b12 = b[12],
|
14093 | b13 = b[13],
|
14094 | b14 = b[14],
|
14095 | b15 = b[15];
|
14096 |
|
14097 | v = a[0];
|
14098 | t0 += v * b0;
|
14099 | t1 += v * b1;
|
14100 | t2 += v * b2;
|
14101 | t3 += v * b3;
|
14102 | t4 += v * b4;
|
14103 | t5 += v * b5;
|
14104 | t6 += v * b6;
|
14105 | t7 += v * b7;
|
14106 | t8 += v * b8;
|
14107 | t9 += v * b9;
|
14108 | t10 += v * b10;
|
14109 | t11 += v * b11;
|
14110 | t12 += v * b12;
|
14111 | t13 += v * b13;
|
14112 | t14 += v * b14;
|
14113 | t15 += v * b15;
|
14114 | v = a[1];
|
14115 | t1 += v * b0;
|
14116 | t2 += v * b1;
|
14117 | t3 += v * b2;
|
14118 | t4 += v * b3;
|
14119 | t5 += v * b4;
|
14120 | t6 += v * b5;
|
14121 | t7 += v * b6;
|
14122 | t8 += v * b7;
|
14123 | t9 += v * b8;
|
14124 | t10 += v * b9;
|
14125 | t11 += v * b10;
|
14126 | t12 += v * b11;
|
14127 | t13 += v * b12;
|
14128 | t14 += v * b13;
|
14129 | t15 += v * b14;
|
14130 | t16 += v * b15;
|
14131 | v = a[2];
|
14132 | t2 += v * b0;
|
14133 | t3 += v * b1;
|
14134 | t4 += v * b2;
|
14135 | t5 += v * b3;
|
14136 | t6 += v * b4;
|
14137 | t7 += v * b5;
|
14138 | t8 += v * b6;
|
14139 | t9 += v * b7;
|
14140 | t10 += v * b8;
|
14141 | t11 += v * b9;
|
14142 | t12 += v * b10;
|
14143 | t13 += v * b11;
|
14144 | t14 += v * b12;
|
14145 | t15 += v * b13;
|
14146 | t16 += v * b14;
|
14147 | t17 += v * b15;
|
14148 | v = a[3];
|
14149 | t3 += v * b0;
|
14150 | t4 += v * b1;
|
14151 | t5 += v * b2;
|
14152 | t6 += v * b3;
|
14153 | t7 += v * b4;
|
14154 | t8 += v * b5;
|
14155 | t9 += v * b6;
|
14156 | t10 += v * b7;
|
14157 | t11 += v * b8;
|
14158 | t12 += v * b9;
|
14159 | t13 += v * b10;
|
14160 | t14 += v * b11;
|
14161 | t15 += v * b12;
|
14162 | t16 += v * b13;
|
14163 | t17 += v * b14;
|
14164 | t18 += v * b15;
|
14165 | v = a[4];
|
14166 | t4 += v * b0;
|
14167 | t5 += v * b1;
|
14168 | t6 += v * b2;
|
14169 | t7 += v * b3;
|
14170 | t8 += v * b4;
|
14171 | t9 += v * b5;
|
14172 | t10 += v * b6;
|
14173 | t11 += v * b7;
|
14174 | t12 += v * b8;
|
14175 | t13 += v * b9;
|
14176 | t14 += v * b10;
|
14177 | t15 += v * b11;
|
14178 | t16 += v * b12;
|
14179 | t17 += v * b13;
|
14180 | t18 += v * b14;
|
14181 | t19 += v * b15;
|
14182 | v = a[5];
|
14183 | t5 += v * b0;
|
14184 | t6 += v * b1;
|
14185 | t7 += v * b2;
|
14186 | t8 += v * b3;
|
14187 | t9 += v * b4;
|
14188 | t10 += v * b5;
|
14189 | t11 += v * b6;
|
14190 | t12 += v * b7;
|
14191 | t13 += v * b8;
|
14192 | t14 += v * b9;
|
14193 | t15 += v * b10;
|
14194 | t16 += v * b11;
|
14195 | t17 += v * b12;
|
14196 | t18 += v * b13;
|
14197 | t19 += v * b14;
|
14198 | t20 += v * b15;
|
14199 | v = a[6];
|
14200 | t6 += v * b0;
|
14201 | t7 += v * b1;
|
14202 | t8 += v * b2;
|
14203 | t9 += v * b3;
|
14204 | t10 += v * b4;
|
14205 | t11 += v * b5;
|
14206 | t12 += v * b6;
|
14207 | t13 += v * b7;
|
14208 | t14 += v * b8;
|
14209 | t15 += v * b9;
|
14210 | t16 += v * b10;
|
14211 | t17 += v * b11;
|
14212 | t18 += v * b12;
|
14213 | t19 += v * b13;
|
14214 | t20 += v * b14;
|
14215 | t21 += v * b15;
|
14216 | v = a[7];
|
14217 | t7 += v * b0;
|
14218 | t8 += v * b1;
|
14219 | t9 += v * b2;
|
14220 | t10 += v * b3;
|
14221 | t11 += v * b4;
|
14222 | t12 += v * b5;
|
14223 | t13 += v * b6;
|
14224 | t14 += v * b7;
|
14225 | t15 += v * b8;
|
14226 | t16 += v * b9;
|
14227 | t17 += v * b10;
|
14228 | t18 += v * b11;
|
14229 | t19 += v * b12;
|
14230 | t20 += v * b13;
|
14231 | t21 += v * b14;
|
14232 | t22 += v * b15;
|
14233 | v = a[8];
|
14234 | t8 += v * b0;
|
14235 | t9 += v * b1;
|
14236 | t10 += v * b2;
|
14237 | t11 += v * b3;
|
14238 | t12 += v * b4;
|
14239 | t13 += v * b5;
|
14240 | t14 += v * b6;
|
14241 | t15 += v * b7;
|
14242 | t16 += v * b8;
|
14243 | t17 += v * b9;
|
14244 | t18 += v * b10;
|
14245 | t19 += v * b11;
|
14246 | t20 += v * b12;
|
14247 | t21 += v * b13;
|
14248 | t22 += v * b14;
|
14249 | t23 += v * b15;
|
14250 | v = a[9];
|
14251 | t9 += v * b0;
|
14252 | t10 += v * b1;
|
14253 | t11 += v * b2;
|
14254 | t12 += v * b3;
|
14255 | t13 += v * b4;
|
14256 | t14 += v * b5;
|
14257 | t15 += v * b6;
|
14258 | t16 += v * b7;
|
14259 | t17 += v * b8;
|
14260 | t18 += v * b9;
|
14261 | t19 += v * b10;
|
14262 | t20 += v * b11;
|
14263 | t21 += v * b12;
|
14264 | t22 += v * b13;
|
14265 | t23 += v * b14;
|
14266 | t24 += v * b15;
|
14267 | v = a[10];
|
14268 | t10 += v * b0;
|
14269 | t11 += v * b1;
|
14270 | t12 += v * b2;
|
14271 | t13 += v * b3;
|
14272 | t14 += v * b4;
|
14273 | t15 += v * b5;
|
14274 | t16 += v * b6;
|
14275 | t17 += v * b7;
|
14276 | t18 += v * b8;
|
14277 | t19 += v * b9;
|
14278 | t20 += v * b10;
|
14279 | t21 += v * b11;
|
14280 | t22 += v * b12;
|
14281 | t23 += v * b13;
|
14282 | t24 += v * b14;
|
14283 | t25 += v * b15;
|
14284 | v = a[11];
|
14285 | t11 += v * b0;
|
14286 | t12 += v * b1;
|
14287 | t13 += v * b2;
|
14288 | t14 += v * b3;
|
14289 | t15 += v * b4;
|
14290 | t16 += v * b5;
|
14291 | t17 += v * b6;
|
14292 | t18 += v * b7;
|
14293 | t19 += v * b8;
|
14294 | t20 += v * b9;
|
14295 | t21 += v * b10;
|
14296 | t22 += v * b11;
|
14297 | t23 += v * b12;
|
14298 | t24 += v * b13;
|
14299 | t25 += v * b14;
|
14300 | t26 += v * b15;
|
14301 | v = a[12];
|
14302 | t12 += v * b0;
|
14303 | t13 += v * b1;
|
14304 | t14 += v * b2;
|
14305 | t15 += v * b3;
|
14306 | t16 += v * b4;
|
14307 | t17 += v * b5;
|
14308 | t18 += v * b6;
|
14309 | t19 += v * b7;
|
14310 | t20 += v * b8;
|
14311 | t21 += v * b9;
|
14312 | t22 += v * b10;
|
14313 | t23 += v * b11;
|
14314 | t24 += v * b12;
|
14315 | t25 += v * b13;
|
14316 | t26 += v * b14;
|
14317 | t27 += v * b15;
|
14318 | v = a[13];
|
14319 | t13 += v * b0;
|
14320 | t14 += v * b1;
|
14321 | t15 += v * b2;
|
14322 | t16 += v * b3;
|
14323 | t17 += v * b4;
|
14324 | t18 += v * b5;
|
14325 | t19 += v * b6;
|
14326 | t20 += v * b7;
|
14327 | t21 += v * b8;
|
14328 | t22 += v * b9;
|
14329 | t23 += v * b10;
|
14330 | t24 += v * b11;
|
14331 | t25 += v * b12;
|
14332 | t26 += v * b13;
|
14333 | t27 += v * b14;
|
14334 | t28 += v * b15;
|
14335 | v = a[14];
|
14336 | t14 += v * b0;
|
14337 | t15 += v * b1;
|
14338 | t16 += v * b2;
|
14339 | t17 += v * b3;
|
14340 | t18 += v * b4;
|
14341 | t19 += v * b5;
|
14342 | t20 += v * b6;
|
14343 | t21 += v * b7;
|
14344 | t22 += v * b8;
|
14345 | t23 += v * b9;
|
14346 | t24 += v * b10;
|
14347 | t25 += v * b11;
|
14348 | t26 += v * b12;
|
14349 | t27 += v * b13;
|
14350 | t28 += v * b14;
|
14351 | t29 += v * b15;
|
14352 | v = a[15];
|
14353 | t15 += v * b0;
|
14354 | t16 += v * b1;
|
14355 | t17 += v * b2;
|
14356 | t18 += v * b3;
|
14357 | t19 += v * b4;
|
14358 | t20 += v * b5;
|
14359 | t21 += v * b6;
|
14360 | t22 += v * b7;
|
14361 | t23 += v * b8;
|
14362 | t24 += v * b9;
|
14363 | t25 += v * b10;
|
14364 | t26 += v * b11;
|
14365 | t27 += v * b12;
|
14366 | t28 += v * b13;
|
14367 | t29 += v * b14;
|
14368 | t30 += v * b15;
|
14369 |
|
14370 | t0 += 38 * t16;
|
14371 | t1 += 38 * t17;
|
14372 | t2 += 38 * t18;
|
14373 | t3 += 38 * t19;
|
14374 | t4 += 38 * t20;
|
14375 | t5 += 38 * t21;
|
14376 | t6 += 38 * t22;
|
14377 | t7 += 38 * t23;
|
14378 | t8 += 38 * t24;
|
14379 | t9 += 38 * t25;
|
14380 | t10 += 38 * t26;
|
14381 | t11 += 38 * t27;
|
14382 | t12 += 38 * t28;
|
14383 | t13 += 38 * t29;
|
14384 | t14 += 38 * t30;
|
14385 |
|
14386 |
|
14387 |
|
14388 | c = 1;
|
14389 | v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
|
14390 | v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
|
14391 | v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
|
14392 | v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
|
14393 | v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
|
14394 | v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
|
14395 | v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
|
14396 | v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
|
14397 | v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
|
14398 | v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
|
14399 | v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
|
14400 | v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
|
14401 | v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
|
14402 | v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
|
14403 | v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
|
14404 | v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
|
14405 | t0 += c-1 + 37 * (c-1);
|
14406 |
|
14407 |
|
14408 | c = 1;
|
14409 | v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
|
14410 | v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
|
14411 | v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
|
14412 | v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
|
14413 | v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
|
14414 | v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
|
14415 | v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
|
14416 | v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
|
14417 | v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
|
14418 | v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
|
14419 | v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
|
14420 | v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
|
14421 | v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
|
14422 | v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
|
14423 | v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
|
14424 | v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
|
14425 | t0 += c-1 + 37 * (c-1);
|
14426 |
|
14427 | o[ 0] = t0;
|
14428 | o[ 1] = t1;
|
14429 | o[ 2] = t2;
|
14430 | o[ 3] = t3;
|
14431 | o[ 4] = t4;
|
14432 | o[ 5] = t5;
|
14433 | o[ 6] = t6;
|
14434 | o[ 7] = t7;
|
14435 | o[ 8] = t8;
|
14436 | o[ 9] = t9;
|
14437 | o[10] = t10;
|
14438 | o[11] = t11;
|
14439 | o[12] = t12;
|
14440 | o[13] = t13;
|
14441 | o[14] = t14;
|
14442 | o[15] = t15;
|
14443 | }
|
14444 |
|
14445 | function S(o, a) {
|
14446 | M(o, a, a);
|
14447 | }
|
14448 |
|
14449 | function inv25519(o, i) {
|
14450 | var c = gf();
|
14451 | var a;
|
14452 | for (a = 0; a < 16; a++) c[a] = i[a];
|
14453 | for (a = 253; a >= 0; a--) {
|
14454 | S(c, c);
|
14455 | if(a !== 2 && a !== 4) M(c, c, i);
|
14456 | }
|
14457 | for (a = 0; a < 16; a++) o[a] = c[a];
|
14458 | }
|
14459 |
|
14460 | function pow2523(o, i) {
|
14461 | var c = gf();
|
14462 | var a;
|
14463 | for (a = 0; a < 16; a++) c[a] = i[a];
|
14464 | for (a = 250; a >= 0; a--) {
|
14465 | S(c, c);
|
14466 | if(a !== 1) M(c, c, i);
|
14467 | }
|
14468 | for (a = 0; a < 16; a++) o[a] = c[a];
|
14469 | }
|
14470 |
|
14471 | function crypto_scalarmult(q, n, p) {
|
14472 | var z = new Uint8Array(32);
|
14473 | var x = new Float64Array(80), r, i;
|
14474 | var a = gf(), b = gf(), c = gf(),
|
14475 | d = gf(), e = gf(), f = gf();
|
14476 | for (i = 0; i < 31; i++) z[i] = n[i];
|
14477 | z[31]=(n[31]&127)|64;
|
14478 | z[0]&=248;
|
14479 | unpack25519(x,p);
|
14480 | for (i = 0; i < 16; i++) {
|
14481 | b[i]=x[i];
|
14482 | d[i]=a[i]=c[i]=0;
|
14483 | }
|
14484 | a[0]=d[0]=1;
|
14485 | for (i=254; i>=0; --i) {
|
14486 | r=(z[i>>>3]>>>(i&7))&1;
|
14487 | sel25519(a,b,r);
|
14488 | sel25519(c,d,r);
|
14489 | A(e,a,c);
|
14490 | Z(a,a,c);
|
14491 | A(c,b,d);
|
14492 | Z(b,b,d);
|
14493 | S(d,e);
|
14494 | S(f,a);
|
14495 | M(a,c,a);
|
14496 | M(c,b,e);
|
14497 | A(e,a,c);
|
14498 | Z(a,a,c);
|
14499 | S(b,a);
|
14500 | Z(c,d,f);
|
14501 | M(a,c,_121665);
|
14502 | A(a,a,d);
|
14503 | M(c,c,a);
|
14504 | M(a,d,f);
|
14505 | M(d,b,x);
|
14506 | S(b,e);
|
14507 | sel25519(a,b,r);
|
14508 | sel25519(c,d,r);
|
14509 | }
|
14510 | for (i = 0; i < 16; i++) {
|
14511 | x[i+16]=a[i];
|
14512 | x[i+32]=c[i];
|
14513 | x[i+48]=b[i];
|
14514 | x[i+64]=d[i];
|
14515 | }
|
14516 | var x32 = x.subarray(32);
|
14517 | var x16 = x.subarray(16);
|
14518 | inv25519(x32,x32);
|
14519 | M(x16,x16,x32);
|
14520 | pack25519(q,x16);
|
14521 | return 0;
|
14522 | }
|
14523 |
|
14524 | function crypto_scalarmult_base(q, n) {
|
14525 | return crypto_scalarmult(q, n, _9);
|
14526 | }
|
14527 |
|
14528 | function crypto_box_keypair(y, x) {
|
14529 | randombytes(x, 32);
|
14530 | return crypto_scalarmult_base(y, x);
|
14531 | }
|
14532 |
|
14533 | function add(p, q) {
|
14534 | var a = gf(), b = gf(), c = gf(),
|
14535 | d = gf(), e = gf(), f = gf(),
|
14536 | g = gf(), h = gf(), t = gf();
|
14537 |
|
14538 | Z(a, p[1], p[0]);
|
14539 | Z(t, q[1], q[0]);
|
14540 | M(a, a, t);
|
14541 | A(b, p[0], p[1]);
|
14542 | A(t, q[0], q[1]);
|
14543 | M(b, b, t);
|
14544 | M(c, p[3], q[3]);
|
14545 | M(c, c, D2);
|
14546 | M(d, p[2], q[2]);
|
14547 | A(d, d, d);
|
14548 | Z(e, b, a);
|
14549 | Z(f, d, c);
|
14550 | A(g, d, c);
|
14551 | A(h, b, a);
|
14552 |
|
14553 | M(p[0], e, f);
|
14554 | M(p[1], h, g);
|
14555 | M(p[2], g, f);
|
14556 | M(p[3], e, h);
|
14557 | }
|
14558 |
|
14559 | function cswap(p, q, b) {
|
14560 | var i;
|
14561 | for (i = 0; i < 4; i++) {
|
14562 | sel25519(p[i], q[i], b);
|
14563 | }
|
14564 | }
|
14565 |
|
14566 | function pack(r, p) {
|
14567 | var tx = gf(), ty = gf(), zi = gf();
|
14568 | inv25519(zi, p[2]);
|
14569 | M(tx, p[0], zi);
|
14570 | M(ty, p[1], zi);
|
14571 | pack25519(r, ty);
|
14572 | r[31] ^= par25519(tx) << 7;
|
14573 | }
|
14574 |
|
14575 | function scalarmult(p, q, s) {
|
14576 | var b, i;
|
14577 | set25519(p[0], gf0);
|
14578 | set25519(p[1], gf1);
|
14579 | set25519(p[2], gf1);
|
14580 | set25519(p[3], gf0);
|
14581 | for (i = 255; i >= 0; --i) {
|
14582 | b = (s[(i/8)|0] >> (i&7)) & 1;
|
14583 | cswap(p, q, b);
|
14584 | add(q, p);
|
14585 | add(p, p);
|
14586 | cswap(p, q, b);
|
14587 | }
|
14588 | }
|
14589 |
|
14590 | function scalarbase(p, s) {
|
14591 | var q = [gf(), gf(), gf(), gf()];
|
14592 | set25519(q[0], X);
|
14593 | set25519(q[1], Y);
|
14594 | set25519(q[2], gf1);
|
14595 | M(q[3], X, Y);
|
14596 | scalarmult(p, q, s);
|
14597 | }
|
14598 |
|
14599 | function crypto_sign_keypair(pk, sk, seeded) {
|
14600 | var d;
|
14601 | var p = [gf(), gf(), gf(), gf()];
|
14602 | var i;
|
14603 |
|
14604 | if (!seeded) randombytes(sk, 32);
|
14605 | d = nacl.hash(sk.subarray(0, 32));
|
14606 | d[0] &= 248;
|
14607 | d[31] &= 127;
|
14608 | d[31] |= 64;
|
14609 |
|
14610 | scalarbase(p, d);
|
14611 | pack(pk, p);
|
14612 |
|
14613 | for (i = 0; i < 32; i++) sk[i+32] = pk[i];
|
14614 | return 0;
|
14615 | }
|
14616 |
|
14617 | var L = new Float64Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]);
|
14618 |
|
14619 | function modL(r, x) {
|
14620 | var carry, i, j, k;
|
14621 | for (i = 63; i >= 32; --i) {
|
14622 | carry = 0;
|
14623 | for (j = i - 32, k = i - 12; j < k; ++j) {
|
14624 | x[j] += carry - 16 * x[i] * L[j - (i - 32)];
|
14625 | carry = Math.floor((x[j] + 128) / 256);
|
14626 | x[j] -= carry * 256;
|
14627 | }
|
14628 | x[j] += carry;
|
14629 | x[i] = 0;
|
14630 | }
|
14631 | carry = 0;
|
14632 | for (j = 0; j < 32; j++) {
|
14633 | x[j] += carry - (x[31] >> 4) * L[j];
|
14634 | carry = x[j] >> 8;
|
14635 | x[j] &= 255;
|
14636 | }
|
14637 | for (j = 0; j < 32; j++) x[j] -= carry * L[j];
|
14638 | for (i = 0; i < 32; i++) {
|
14639 | x[i+1] += x[i] >> 8;
|
14640 | r[i] = x[i] & 255;
|
14641 | }
|
14642 | }
|
14643 |
|
14644 | function reduce(r) {
|
14645 | var x = new Float64Array(64), i;
|
14646 | for (i = 0; i < 64; i++) x[i] = r[i];
|
14647 | for (i = 0; i < 64; i++) r[i] = 0;
|
14648 | modL(r, x);
|
14649 | }
|
14650 |
|
14651 |
|
14652 | function crypto_sign(sm, m, n, sk) {
|
14653 | var d, h, r;
|
14654 | var i, j, x = new Float64Array(64);
|
14655 | var p = [gf(), gf(), gf(), gf()];
|
14656 |
|
14657 | d = nacl.hash(sk.subarray(0, 32));
|
14658 | d[0] &= 248;
|
14659 | d[31] &= 127;
|
14660 | d[31] |= 64;
|
14661 |
|
14662 | var smlen = n + 64;
|
14663 | for (i = 0; i < n; i++) sm[64 + i] = m[i];
|
14664 | for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
|
14665 |
|
14666 | r = nacl.hash(sm.subarray(32, smlen));
|
14667 | reduce(r);
|
14668 | scalarbase(p, r);
|
14669 | pack(sm, p);
|
14670 |
|
14671 | for (i = 32; i < 64; i++) sm[i] = sk[i];
|
14672 | h = nacl.hash(sm.subarray(0, smlen));
|
14673 | reduce(h);
|
14674 |
|
14675 | for (i = 0; i < 64; i++) x[i] = 0;
|
14676 | for (i = 0; i < 32; i++) x[i] = r[i];
|
14677 | for (i = 0; i < 32; i++) {
|
14678 | for (j = 0; j < 32; j++) {
|
14679 | x[i+j] += h[i] * d[j];
|
14680 | }
|
14681 | }
|
14682 |
|
14683 | modL(sm.subarray(32), x);
|
14684 | return smlen;
|
14685 | }
|
14686 |
|
14687 | function unpackneg(r, p) {
|
14688 | var t = gf(), chk = gf(), num = gf(),
|
14689 | den = gf(), den2 = gf(), den4 = gf(),
|
14690 | den6 = gf();
|
14691 |
|
14692 | set25519(r[2], gf1);
|
14693 | unpack25519(r[1], p);
|
14694 | S(num, r[1]);
|
14695 | M(den, num, D);
|
14696 | Z(num, num, r[2]);
|
14697 | A(den, r[2], den);
|
14698 |
|
14699 | S(den2, den);
|
14700 | S(den4, den2);
|
14701 | M(den6, den4, den2);
|
14702 | M(t, den6, num);
|
14703 | M(t, t, den);
|
14704 |
|
14705 | pow2523(t, t);
|
14706 | M(t, t, num);
|
14707 | M(t, t, den);
|
14708 | M(t, t, den);
|
14709 | M(r[0], t, den);
|
14710 |
|
14711 | S(chk, r[0]);
|
14712 | M(chk, chk, den);
|
14713 | if (neq25519(chk, num)) M(r[0], r[0], I);
|
14714 |
|
14715 | S(chk, r[0]);
|
14716 | M(chk, chk, den);
|
14717 | if (neq25519(chk, num)) return -1;
|
14718 |
|
14719 | if (par25519(r[0]) === (p[31]>>7)) Z(r[0], gf0, r[0]);
|
14720 |
|
14721 | M(r[3], r[0], r[1]);
|
14722 | return 0;
|
14723 | }
|
14724 |
|
14725 | function crypto_sign_open(m, sm, n, pk) {
|
14726 | var i;
|
14727 | var t = new Uint8Array(32), h;
|
14728 | var p = [gf(), gf(), gf(), gf()],
|
14729 | q = [gf(), gf(), gf(), gf()];
|
14730 |
|
14731 | if (n < 64) return -1;
|
14732 |
|
14733 | if (unpackneg(q, pk)) return -1;
|
14734 |
|
14735 | for (i = 0; i < n; i++) m[i] = sm[i];
|
14736 | for (i = 0; i < 32; i++) m[i+32] = pk[i];
|
14737 | h = nacl.hash(m.subarray(0, n));
|
14738 | reduce(h);
|
14739 | scalarmult(p, q, h);
|
14740 |
|
14741 | scalarbase(q, sm.subarray(32));
|
14742 | add(p, q);
|
14743 | pack(t, p);
|
14744 |
|
14745 | n -= 64;
|
14746 | if (crypto_verify_32(sm, 0, t, 0)) {
|
14747 | for (i = 0; i < n; i++) m[i] = 0;
|
14748 | return -1;
|
14749 | }
|
14750 |
|
14751 | for (i = 0; i < n; i++) m[i] = sm[i + 64];
|
14752 | return n;
|
14753 | }
|
14754 |
|
14755 | var crypto_scalarmult_BYTES = 32,
|
14756 | crypto_scalarmult_SCALARBYTES = 32,
|
14757 | crypto_box_PUBLICKEYBYTES = 32,
|
14758 | crypto_box_SECRETKEYBYTES = 32,
|
14759 | crypto_sign_BYTES = 64,
|
14760 | crypto_sign_PUBLICKEYBYTES = 32,
|
14761 | crypto_sign_SECRETKEYBYTES = 64,
|
14762 | crypto_sign_SEEDBYTES = 32;
|
14763 |
|
14764 | function checkArrayTypes() {
|
14765 | for (var i = 0; i < arguments.length; i++) {
|
14766 | if (!(arguments[i] instanceof Uint8Array))
|
14767 | throw new TypeError('unexpected type, use Uint8Array');
|
14768 | }
|
14769 | }
|
14770 |
|
14771 | function cleanup(arr) {
|
14772 | for (var i = 0; i < arr.length; i++) arr[i] = 0;
|
14773 | }
|
14774 |
|
14775 | nacl.scalarMult = function(n, p) {
|
14776 | checkArrayTypes(n, p);
|
14777 | if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
|
14778 | if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size');
|
14779 | var q = new Uint8Array(crypto_scalarmult_BYTES);
|
14780 | crypto_scalarmult(q, n, p);
|
14781 | return q;
|
14782 | };
|
14783 |
|
14784 | nacl.box = {};
|
14785 |
|
14786 | nacl.box.keyPair = function() {
|
14787 | var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
|
14788 | var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
|
14789 | crypto_box_keypair(pk, sk);
|
14790 | return {publicKey: pk, secretKey: sk};
|
14791 | };
|
14792 |
|
14793 | nacl.box.keyPair.fromSecretKey = function(secretKey) {
|
14794 | checkArrayTypes(secretKey);
|
14795 | if (secretKey.length !== crypto_box_SECRETKEYBYTES)
|
14796 | throw new Error('bad secret key size');
|
14797 | var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
|
14798 | crypto_scalarmult_base(pk, secretKey);
|
14799 | return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
|
14800 | };
|
14801 |
|
14802 | nacl.sign = function(msg, secretKey) {
|
14803 | checkArrayTypes(msg, secretKey);
|
14804 | if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
|
14805 | throw new Error('bad secret key size');
|
14806 | var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);
|
14807 | crypto_sign(signedMsg, msg, msg.length, secretKey);
|
14808 | return signedMsg;
|
14809 | };
|
14810 |
|
14811 | nacl.sign.detached = function(msg, secretKey) {
|
14812 | var signedMsg = nacl.sign(msg, secretKey);
|
14813 | var sig = new Uint8Array(crypto_sign_BYTES);
|
14814 | for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
|
14815 | return sig;
|
14816 | };
|
14817 |
|
14818 | nacl.sign.detached.verify = function(msg, sig, publicKey) {
|
14819 | checkArrayTypes(msg, sig, publicKey);
|
14820 | if (sig.length !== crypto_sign_BYTES)
|
14821 | throw new Error('bad signature size');
|
14822 | if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
|
14823 | throw new Error('bad public key size');
|
14824 | var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
|
14825 | var m = new Uint8Array(crypto_sign_BYTES + msg.length);
|
14826 | var i;
|
14827 | for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
|
14828 | for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];
|
14829 | return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
|
14830 | };
|
14831 |
|
14832 | nacl.sign.keyPair = function() {
|
14833 | var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
14834 | var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
|
14835 | crypto_sign_keypair(pk, sk);
|
14836 | return {publicKey: pk, secretKey: sk};
|
14837 | };
|
14838 |
|
14839 | nacl.sign.keyPair.fromSecretKey = function(secretKey) {
|
14840 | checkArrayTypes(secretKey);
|
14841 | if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
|
14842 | throw new Error('bad secret key size');
|
14843 | var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
14844 | for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];
|
14845 | return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
|
14846 | };
|
14847 |
|
14848 | nacl.sign.keyPair.fromSeed = function(seed) {
|
14849 | checkArrayTypes(seed);
|
14850 | if (seed.length !== crypto_sign_SEEDBYTES)
|
14851 | throw new Error('bad seed size');
|
14852 | var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
|
14853 | var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
|
14854 | for (var i = 0; i < 32; i++) sk[i] = seed[i];
|
14855 | crypto_sign_keypair(pk, sk, true);
|
14856 | return {publicKey: pk, secretKey: sk};
|
14857 | };
|
14858 |
|
14859 | nacl.setPRNG = function(fn) {
|
14860 | randombytes = fn;
|
14861 | };
|
14862 |
|
14863 | (function() {
|
14864 |
|
14865 |
|
14866 | var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
|
14867 | if (crypto && crypto.getRandomValues) {
|
14868 |
|
14869 | var QUOTA = 65536;
|
14870 | nacl.setPRNG(function(x, n) {
|
14871 | var i, v = new Uint8Array(n);
|
14872 | for (i = 0; i < n; i += QUOTA) {
|
14873 | crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
|
14874 | }
|
14875 | for (i = 0; i < n; i++) x[i] = v[i];
|
14876 | cleanup(v);
|
14877 | });
|
14878 | } else if (typeof commonjsRequire !== 'undefined') {
|
14879 |
|
14880 | crypto = void('crypto');
|
14881 | if (crypto && crypto.randomBytes) {
|
14882 | nacl.setPRNG(function(x, n) {
|
14883 | var i, v = crypto.randomBytes(n);
|
14884 | for (i = 0; i < n; i++) x[i] = v[i];
|
14885 | cleanup(v);
|
14886 | });
|
14887 | }
|
14888 | }
|
14889 | })();
|
14890 |
|
14891 | })(module.exports ? module.exports : (self.nacl = self.nacl || {}));
|
14892 | });
|
14893 |
|
14894 |
|
14895 |
|
14896 | const nodeCrypto$5 = util.getNodeCrypto();
|
14897 |
|
14898 |
|
14899 |
|
14900 |
|
14901 | class RandomBuffer {
|
14902 | constructor() {
|
14903 | this.buffer = null;
|
14904 | this.size = null;
|
14905 | this.callback = null;
|
14906 | }
|
14907 |
|
14908 | |
14909 |
|
14910 |
|
14911 |
|
14912 | init(size, callback) {
|
14913 | this.buffer = new Uint8Array(size);
|
14914 | this.size = 0;
|
14915 | this.callback = callback;
|
14916 | }
|
14917 |
|
14918 | |
14919 |
|
14920 |
|
14921 |
|
14922 | set(buf) {
|
14923 | if (!this.buffer) {
|
14924 | throw new Error('RandomBuffer is not initialized');
|
14925 | }
|
14926 | if (!(buf instanceof Uint8Array)) {
|
14927 | throw new Error('Invalid type: buf not an Uint8Array');
|
14928 | }
|
14929 | const freeSpace = this.buffer.length - this.size;
|
14930 | if (buf.length > freeSpace) {
|
14931 | buf = buf.subarray(0, freeSpace);
|
14932 | }
|
14933 |
|
14934 | this.buffer.set(buf, this.size);
|
14935 | this.size += buf.length;
|
14936 | }
|
14937 |
|
14938 | |
14939 |
|
14940 |
|
14941 |
|
14942 | async get(buf) {
|
14943 | if (!this.buffer) {
|
14944 | throw new Error('RandomBuffer is not initialized');
|
14945 | }
|
14946 | if (!(buf instanceof Uint8Array)) {
|
14947 | throw new Error('Invalid type: buf not an Uint8Array');
|
14948 | }
|
14949 | if (this.size < buf.length) {
|
14950 | if (!this.callback) {
|
14951 | throw new Error('Random number buffer depleted');
|
14952 | }
|
14953 |
|
14954 | await this.callback();
|
14955 | return this.get(buf);
|
14956 | }
|
14957 | for (let i = 0; i < buf.length; i++) {
|
14958 | buf[i] = this.buffer[--this.size];
|
14959 |
|
14960 | this.buffer[this.size] = 0;
|
14961 | }
|
14962 | }
|
14963 | }
|
14964 |
|
14965 |
|
14966 |
|
14967 |
|
14968 |
|
14969 |
|
14970 |
|
14971 | async function getRandomBytes(length) {
|
14972 | const buf = new Uint8Array(length);
|
14973 | if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
|
14974 | crypto.getRandomValues(buf);
|
14975 | } else if (typeof globalThis !== 'undefined' && typeof globalThis.msCrypto === 'object' && typeof globalThis.msCrypto.getRandomValues === 'function') {
|
14976 | globalThis.msCrypto.getRandomValues(buf);
|
14977 | } else if (nodeCrypto$5) {
|
14978 | const bytes = nodeCrypto$5.randomBytes(buf.length);
|
14979 | buf.set(bytes);
|
14980 | } else if (randomBuffer.buffer) {
|
14981 | await randomBuffer.get(buf);
|
14982 | } else {
|
14983 | throw new Error('No secure random number generator available.');
|
14984 | }
|
14985 | return buf;
|
14986 | }
|
14987 |
|
14988 |
|
14989 |
|
14990 |
|
14991 |
|
14992 |
|
14993 |
|
14994 |
|
14995 | async function getRandomBigInteger(min, max) {
|
14996 | const BigInteger = await util.getBigInteger();
|
14997 |
|
14998 | if (max.lt(min)) {
|
14999 | throw new Error('Illegal parameter value: max <= min');
|
15000 | }
|
15001 |
|
15002 | const modulus = max.sub(min);
|
15003 | const bytes = modulus.byteLength();
|
15004 |
|
15005 |
|
15006 |
|
15007 |
|
15008 | const r = new BigInteger(await getRandomBytes(bytes + 8));
|
15009 | return r.mod(modulus).add(min);
|
15010 | }
|
15011 |
|
15012 | const randomBuffer = new RandomBuffer();
|
15013 |
|
15014 | var random = Object.freeze({
|
15015 | __proto__: null,
|
15016 | getRandomBytes: getRandomBytes,
|
15017 | getRandomBigInteger: getRandomBigInteger,
|
15018 | randomBuffer: randomBuffer
|
15019 | });
|
15020 |
|
15021 |
|
15022 |
|
15023 |
|
15024 |
|
15025 |
|
15026 |
|
15027 |
|
15028 |
|
15029 |
|
15030 |
|
15031 | async function randomProbablePrime(bits, e, k) {
|
15032 | const BigInteger = await util.getBigInteger();
|
15033 | const one = new BigInteger(1);
|
15034 | const min = one.leftShift(new BigInteger(bits - 1));
|
15035 | const thirty = new BigInteger(30);
|
15036 | |
15037 |
|
15038 |
|
15039 |
|
15040 |
|
15041 |
|
15042 | const adds = [1, 6, 5, 4, 3, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 2, 1, 4, 3, 2, 1, 6, 5, 4, 3, 2, 1, 2];
|
15043 |
|
15044 | const n = await getRandomBigInteger(min, min.leftShift(one));
|
15045 | let i = n.mod(thirty).toNumber();
|
15046 |
|
15047 | do {
|
15048 | n.iadd(new BigInteger(adds[i]));
|
15049 | i = (i + adds[i]) % adds.length;
|
15050 |
|
15051 | if (n.bitLength() > bits) {
|
15052 | n.imod(min.leftShift(one)).iadd(min);
|
15053 | i = n.mod(thirty).toNumber();
|
15054 | }
|
15055 | } while (!await isProbablePrime(n, e, k));
|
15056 | return n;
|
15057 | }
|
15058 |
|
15059 |
|
15060 |
|
15061 |
|
15062 |
|
15063 |
|
15064 |
|
15065 |
|
15066 |
|
15067 | async function isProbablePrime(n, e, k) {
|
15068 | if (e && !n.dec().gcd(e).isOne()) {
|
15069 | return false;
|
15070 | }
|
15071 | if (!await divisionTest(n)) {
|
15072 | return false;
|
15073 | }
|
15074 | if (!await fermat(n)) {
|
15075 | return false;
|
15076 | }
|
15077 | if (!await millerRabin(n, k)) {
|
15078 | return false;
|
15079 | }
|
15080 |
|
15081 |
|
15082 | return true;
|
15083 | }
|
15084 |
|
15085 |
|
15086 |
|
15087 |
|
15088 |
|
15089 |
|
15090 |
|
15091 |
|
15092 | async function fermat(n, b) {
|
15093 | const BigInteger = await util.getBigInteger();
|
15094 | b = b || new BigInteger(2);
|
15095 | return b.modExp(n.dec(), n).isOne();
|
15096 | }
|
15097 |
|
15098 | async function divisionTest(n) {
|
15099 | const BigInteger = await util.getBigInteger();
|
15100 | return smallPrimes.every(m => {
|
15101 | return n.mod(new BigInteger(m)) !== 0;
|
15102 | });
|
15103 | }
|
15104 |
|
15105 |
|
15106 | const smallPrimes = [
|
15107 | 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
|
15108 | 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101,
|
15109 | 103, 107, 109, 113, 127, 131, 137, 139, 149, 151,
|
15110 | 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
|
15111 | 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
|
15112 | 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
|
15113 | 331, 337, 347, 349, 353, 359, 367, 373, 379, 383,
|
15114 | 389, 397, 401, 409, 419, 421, 431, 433, 439, 443,
|
15115 | 449, 457, 461, 463, 467, 479, 487, 491, 499, 503,
|
15116 | 509, 521, 523, 541, 547, 557, 563, 569, 571, 577,
|
15117 | 587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
|
15118 | 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
|
15119 | 709, 719, 727, 733, 739, 743, 751, 757, 761, 769,
|
15120 | 773, 787, 797, 809, 811, 821, 823, 827, 829, 839,
|
15121 | 853, 857, 859, 863, 877, 881, 883, 887, 907, 911,
|
15122 | 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
|
15123 | 991, 997, 1009, 1013, 1019, 1021, 1031, 1033,
|
15124 | 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091,
|
15125 | 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151,
|
15126 | 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
|
15127 | 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277,
|
15128 | 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307,
|
15129 | 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
|
15130 | 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451,
|
15131 | 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493,
|
15132 | 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559,
|
15133 | 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609,
|
15134 | 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667,
|
15135 | 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733,
|
15136 | 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
|
15137 | 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
|
15138 | 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931,
|
15139 | 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997,
|
15140 | 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053,
|
15141 | 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111,
|
15142 | 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161,
|
15143 | 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243,
|
15144 | 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297,
|
15145 | 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357,
|
15146 | 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411,
|
15147 | 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473,
|
15148 | 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
|
15149 | 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633,
|
15150 | 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687,
|
15151 | 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729,
|
15152 | 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791,
|
15153 | 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851,
|
15154 | 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917,
|
15155 | 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999,
|
15156 | 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061,
|
15157 | 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137,
|
15158 | 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209,
|
15159 | 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
|
15160 | 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331,
|
15161 | 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391,
|
15162 | 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467,
|
15163 | 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533,
|
15164 | 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583,
|
15165 | 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643,
|
15166 | 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709,
|
15167 | 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779,
|
15168 | 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851,
|
15169 | 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917,
|
15170 | 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
|
15171 | 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049,
|
15172 | 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111,
|
15173 | 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177,
|
15174 | 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243,
|
15175 | 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297,
|
15176 | 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391,
|
15177 | 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457,
|
15178 | 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519,
|
15179 | 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597,
|
15180 | 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657,
|
15181 | 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
|
15182 | 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799,
|
15183 | 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889,
|
15184 | 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951,
|
15185 | 4957, 4967, 4969, 4973, 4987, 4993, 4999
|
15186 | ];
|
15187 |
|
15188 |
|
15189 |
|
15190 |
|
15191 |
|
15192 |
|
15193 |
|
15194 |
|
15195 |
|
15196 |
|
15197 |
|
15198 |
|
15199 |
|
15200 |
|
15201 |
|
15202 |
|
15203 |
|
15204 |
|
15205 |
|
15206 |
|
15207 |
|
15208 |
|
15209 |
|
15210 |
|
15211 |
|
15212 |
|
15213 |
|
15214 |
|
15215 |
|
15216 |
|
15217 |
|
15218 |
|
15219 |
|
15220 |
|
15221 |
|
15222 |
|
15223 |
|
15224 |
|
15225 |
|
15226 |
|
15227 | async function millerRabin(n, k, rand) {
|
15228 | const BigInteger = await util.getBigInteger();
|
15229 | const len = n.bitLength();
|
15230 |
|
15231 | if (!k) {
|
15232 | k = Math.max(1, (len / 48) | 0);
|
15233 | }
|
15234 |
|
15235 | const n1 = n.dec();
|
15236 |
|
15237 |
|
15238 | let s = 0;
|
15239 | while (!n1.getBit(s)) { s++; }
|
15240 | const d = n.rightShift(new BigInteger(s));
|
15241 |
|
15242 | for (; k > 0; k--) {
|
15243 | const a = rand ? rand() : await getRandomBigInteger(new BigInteger(2), n1);
|
15244 |
|
15245 | let x = a.modExp(d, n);
|
15246 | if (x.isOne() || x.equal(n1)) {
|
15247 | continue;
|
15248 | }
|
15249 |
|
15250 | let i;
|
15251 | for (i = 1; i < s; i++) {
|
15252 | x = x.mul(x).mod(n);
|
15253 |
|
15254 | if (x.isOne()) {
|
15255 | return false;
|
15256 | }
|
15257 | if (x.equal(n1)) {
|
15258 | break;
|
15259 | }
|
15260 | }
|
15261 |
|
15262 | if (i === s) {
|
15263 | return false;
|
15264 | }
|
15265 | }
|
15266 |
|
15267 | return true;
|
15268 | }
|
15269 |
|
15270 |
|
15271 |
|
15272 |
|
15273 |
|
15274 |
|
15275 |
|
15276 | const hash_headers = [];
|
15277 | hash_headers[1] = [0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04,
|
15278 | 0x10];
|
15279 | hash_headers[2] = [0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14];
|
15280 | hash_headers[3] = [0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 0x01, 0x05, 0x00, 0x04, 0x14];
|
15281 | hash_headers[8] = [0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00,
|
15282 | 0x04, 0x20];
|
15283 | hash_headers[9] = [0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00,
|
15284 | 0x04, 0x30];
|
15285 | hash_headers[10] = [0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
|
15286 | 0x00, 0x04, 0x40];
|
15287 | hash_headers[11] = [0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05,
|
15288 | 0x00, 0x04, 0x1C];
|
15289 |
|
15290 |
|
15291 |
|
15292 |
|
15293 |
|
15294 |
|
15295 |
|
15296 |
|
15297 | async function getPkcs1Padding(length) {
|
15298 | const result = new Uint8Array(length);
|
15299 | let count = 0;
|
15300 | while (count < length) {
|
15301 | const randomBytes = await getRandomBytes(length - count);
|
15302 | for (let i = 0; i < randomBytes.length; i++) {
|
15303 | if (randomBytes[i] !== 0) {
|
15304 | result[count++] = randomBytes[i];
|
15305 | }
|
15306 | }
|
15307 | }
|
15308 | return result;
|
15309 | }
|
15310 |
|
15311 |
|
15312 |
|
15313 |
|
15314 |
|
15315 |
|
15316 |
|
15317 |
|
15318 |
|
15319 | async function emeEncode(message, keyLength) {
|
15320 | const mLength = message.length;
|
15321 |
|
15322 | if (mLength > keyLength - 11) {
|
15323 | throw new Error('Message too long');
|
15324 | }
|
15325 |
|
15326 |
|
15327 | const PS = await getPkcs1Padding(keyLength - mLength - 3);
|
15328 |
|
15329 |
|
15330 | const encoded = new Uint8Array(keyLength);
|
15331 |
|
15332 | encoded[1] = 2;
|
15333 | encoded.set(PS, 2);
|
15334 |
|
15335 | encoded.set(message, keyLength - mLength);
|
15336 | return encoded;
|
15337 | }
|
15338 |
|
15339 |
|
15340 |
|
15341 |
|
15342 |
|
15343 |
|
15344 |
|
15345 | function emeDecode(encoded) {
|
15346 | let i = 2;
|
15347 | while (encoded[i] !== 0 && i < encoded.length) {
|
15348 | i++;
|
15349 | }
|
15350 | const psLen = i - 2;
|
15351 | const separator = encoded[i++];
|
15352 | if (encoded[0] === 0 && encoded[1] === 2 && psLen >= 8 && separator === 0) {
|
15353 | return encoded.subarray(i);
|
15354 | }
|
15355 | throw new Error('Decryption error');
|
15356 | }
|
15357 |
|
15358 |
|
15359 |
|
15360 |
|
15361 |
|
15362 |
|
15363 |
|
15364 |
|
15365 |
|
15366 | async function emsaEncode(algo, hashed, emLen) {
|
15367 | let i;
|
15368 | if (hashed.length !== hash.getHashByteLength(algo)) {
|
15369 | throw new Error('Invalid hash length');
|
15370 | }
|
15371 |
|
15372 |
|
15373 | const hashPrefix = new Uint8Array(hash_headers[algo].length);
|
15374 | for (i = 0; i < hash_headers[algo].length; i++) {
|
15375 | hashPrefix[i] = hash_headers[algo][i];
|
15376 | }
|
15377 |
|
15378 | const tLen = hashPrefix.length + hashed.length;
|
15379 | if (emLen < tLen + 11) {
|
15380 | throw new Error('Intended encoded message length too short');
|
15381 | }
|
15382 |
|
15383 |
|
15384 | const PS = new Uint8Array(emLen - tLen - 3).fill(0xff);
|
15385 |
|
15386 |
|
15387 |
|
15388 | const EM = new Uint8Array(emLen);
|
15389 | EM[1] = 0x01;
|
15390 | EM.set(PS, 2);
|
15391 | EM.set(hashPrefix, emLen - tLen);
|
15392 | EM.set(hashed, emLen - hashed.length);
|
15393 | return EM;
|
15394 | }
|
15395 |
|
15396 | var pkcs1 = Object.freeze({
|
15397 | __proto__: null,
|
15398 | emeEncode: emeEncode,
|
15399 | emeDecode: emeDecode,
|
15400 | emsaEncode: emsaEncode
|
15401 | });
|
15402 |
|
15403 |
|
15404 |
|
15405 | const webCrypto$5 = util.getWebCrypto();
|
15406 | const nodeCrypto$6 = util.getNodeCrypto();
|
15407 | const asn1 = nodeCrypto$6 ? void('asn1.js') : undefined;
|
15408 |
|
15409 |
|
15410 | function promisifyIE11Op(keyObj, err) {
|
15411 | if (typeof keyObj.then !== 'function') {
|
15412 | return new Promise(function(resolve, reject) {
|
15413 | keyObj.onerror = function () {
|
15414 | reject(new Error(err));
|
15415 | };
|
15416 | keyObj.oncomplete = function (e) {
|
15417 | resolve(e.target.result);
|
15418 | };
|
15419 | });
|
15420 | }
|
15421 | return keyObj;
|
15422 | }
|
15423 |
|
15424 |
|
15425 | const RSAPrivateKey = util.detectNode() ? asn1.define('RSAPrivateKey', function () {
|
15426 | this.seq().obj(
|
15427 | this.key('version').int(),
|
15428 | this.key('modulus').int(),
|
15429 | this.key('publicExponent').int(),
|
15430 | this.key('privateExponent').int(),
|
15431 | this.key('prime1').int(),
|
15432 | this.key('prime2').int(),
|
15433 | this.key('exponent1').int(),
|
15434 | this.key('exponent2').int(),
|
15435 | this.key('coefficient').int()
|
15436 | );
|
15437 | }) : undefined;
|
15438 |
|
15439 | const RSAPublicKey = util.detectNode() ? asn1.define('RSAPubliceKey', function () {
|
15440 | this.seq().obj(
|
15441 | this.key('modulus').int(),
|
15442 | this.key('publicExponent').int(),
|
15443 | );
|
15444 | }) : undefined;
|
15445 |
|
15446 |
|
15447 |
|
15448 |
|
15449 |
|
15450 |
|
15451 |
|
15452 |
|
15453 |
|
15454 |
|
15455 |
|
15456 |
|
15457 |
|
15458 |
|
15459 |
|
15460 | async function sign(hash_algo, data, n, e, d, p, q, u, hashed) {
|
15461 | if (data && !util.isStream(data)) {
|
15462 | if (util.getWebCrypto()) {
|
15463 | try {
|
15464 | return await webSign(enums.read(enums.webHash, hash_algo), data, n, e, d, p, q, u);
|
15465 | } catch (err) {
|
15466 | util.printDebugError(err);
|
15467 | }
|
15468 | } else if (util.getNodeCrypto()) {
|
15469 | return nodeSign(hash_algo, data, n, e, d, p, q, u);
|
15470 | }
|
15471 | }
|
15472 | return bnSign(hash_algo, n, d, hashed);
|
15473 | }
|
15474 |
|
15475 |
|
15476 |
|
15477 |
|
15478 |
|
15479 |
|
15480 |
|
15481 |
|
15482 |
|
15483 |
|
15484 |
|
15485 |
|
15486 | async function verify(hash_algo, data, s, n, e, hashed) {
|
15487 | if (data && !util.isStream(data)) {
|
15488 | if (util.getWebCrypto()) {
|
15489 | try {
|
15490 | return await webVerify(enums.read(enums.webHash, hash_algo), data, s, n, e);
|
15491 | } catch (err) {
|
15492 | util.printDebugError(err);
|
15493 | }
|
15494 | } else if (util.getNodeCrypto()) {
|
15495 | return nodeVerify(hash_algo, data, s, n, e);
|
15496 | }
|
15497 | }
|
15498 | return bnVerify(hash_algo, s, n, e, hashed);
|
15499 | }
|
15500 |
|
15501 |
|
15502 |
|
15503 |
|
15504 |
|
15505 |
|
15506 |
|
15507 |
|
15508 |
|
15509 | async function encrypt$1(data, n, e) {
|
15510 | if (util.getNodeCrypto()) {
|
15511 | return nodeEncrypt$1(data, n, e);
|
15512 | }
|
15513 | return bnEncrypt(data, n, e);
|
15514 | }
|
15515 |
|
15516 |
|
15517 |
|
15518 |
|
15519 |
|
15520 |
|
15521 |
|
15522 |
|
15523 |
|
15524 |
|
15525 |
|
15526 |
|
15527 |
|
15528 | async function decrypt$1(data, n, e, d, p, q, u) {
|
15529 | if (util.getNodeCrypto()) {
|
15530 | return nodeDecrypt$1(data, n, e, d, p, q, u);
|
15531 | }
|
15532 | return bnDecrypt(data, n, e, d, p, q, u);
|
15533 | }
|
15534 |
|
15535 |
|
15536 |
|
15537 |
|
15538 |
|
15539 |
|
15540 |
|
15541 |
|
15542 |
|
15543 |
|
15544 |
|
15545 |
|
15546 |
|
15547 |
|
15548 | async function generate(bits, e) {
|
15549 | const BigInteger = await util.getBigInteger();
|
15550 |
|
15551 | e = new BigInteger(e);
|
15552 |
|
15553 |
|
15554 | if (util.getWebCrypto()) {
|
15555 | let keyPair;
|
15556 | let keyGenOpt;
|
15557 | if ((globalThis.crypto && globalThis.crypto.subtle) || globalThis.msCrypto) {
|
15558 |
|
15559 | keyGenOpt = {
|
15560 | name: 'RSASSA-PKCS1-v1_5',
|
15561 | modulusLength: bits,
|
15562 | publicExponent: e.toUint8Array(),
|
15563 | hash: {
|
15564 | name: 'SHA-1'
|
15565 | }
|
15566 | };
|
15567 | keyPair = webCrypto$5.generateKey(keyGenOpt, true, ['sign', 'verify']);
|
15568 | keyPair = await promisifyIE11Op(keyPair, 'Error generating RSA key pair.');
|
15569 | } else if (globalThis.crypto && globalThis.crypto.webkitSubtle) {
|
15570 |
|
15571 | keyGenOpt = {
|
15572 | name: 'RSA-OAEP',
|
15573 | modulusLength: bits,
|
15574 | publicExponent: e.toUint8Array(),
|
15575 | hash: {
|
15576 | name: 'SHA-1'
|
15577 | }
|
15578 | };
|
15579 | keyPair = await webCrypto$5.generateKey(keyGenOpt, true, ['encrypt', 'decrypt']);
|
15580 | } else {
|
15581 | throw new Error('Unknown WebCrypto implementation');
|
15582 | }
|
15583 |
|
15584 |
|
15585 |
|
15586 | let jwk = webCrypto$5.exportKey('jwk', keyPair.privateKey);
|
15587 | jwk = await promisifyIE11Op(jwk, 'Error exporting RSA key pair.');
|
15588 |
|
15589 |
|
15590 | if (jwk instanceof ArrayBuffer) {
|
15591 | jwk = JSON.parse(String.fromCharCode.apply(null, new Uint8Array(jwk)));
|
15592 | }
|
15593 |
|
15594 | return {
|
15595 | n: b64ToUint8Array(jwk.n),
|
15596 | e: e.toUint8Array(),
|
15597 | d: b64ToUint8Array(jwk.d),
|
15598 |
|
15599 | p: b64ToUint8Array(jwk.q),
|
15600 | q: b64ToUint8Array(jwk.p),
|
15601 |
|
15602 | u: b64ToUint8Array(jwk.qi)
|
15603 | };
|
15604 | } else if (util.getNodeCrypto() && nodeCrypto$6.generateKeyPair && RSAPrivateKey) {
|
15605 | const opts = {
|
15606 | modulusLength: bits,
|
15607 | publicExponent: e.toNumber(),
|
15608 | publicKeyEncoding: { type: 'pkcs1', format: 'der' },
|
15609 | privateKeyEncoding: { type: 'pkcs1', format: 'der' }
|
15610 | };
|
15611 | const prv = await new Promise((resolve, reject) => nodeCrypto$6.generateKeyPair('rsa', opts, (err, _, der) => {
|
15612 | if (err) {
|
15613 | reject(err);
|
15614 | } else {
|
15615 | resolve(RSAPrivateKey.decode(der, 'der'));
|
15616 | }
|
15617 | }));
|
15618 | |
15619 |
|
15620 |
|
15621 |
|
15622 |
|
15623 | return {
|
15624 | n: prv.modulus.toArrayLike(Uint8Array),
|
15625 | e: prv.publicExponent.toArrayLike(Uint8Array),
|
15626 | d: prv.privateExponent.toArrayLike(Uint8Array),
|
15627 |
|
15628 | p: prv.prime2.toArrayLike(Uint8Array),
|
15629 | q: prv.prime1.toArrayLike(Uint8Array),
|
15630 |
|
15631 | u: prv.coefficient.toArrayLike(Uint8Array)
|
15632 | };
|
15633 | }
|
15634 |
|
15635 |
|
15636 |
|
15637 |
|
15638 | let q = await randomProbablePrime(bits - (bits >> 1), e, 40);
|
15639 | let p = await randomProbablePrime(bits >> 1, e, 40);
|
15640 |
|
15641 | if (q.lt(p)) {
|
15642 | [p, q] = [q, p];
|
15643 | }
|
15644 | const phi = p.dec().imul(q.dec());
|
15645 | return {
|
15646 | n: p.mul(q).toUint8Array(),
|
15647 | e: e.toUint8Array(),
|
15648 | d: e.modInv(phi).toUint8Array(),
|
15649 | p: p.toUint8Array(),
|
15650 | q: q.toUint8Array(),
|
15651 |
|
15652 |
|
15653 | u: p.modInv(q).toUint8Array()
|
15654 | };
|
15655 | }
|
15656 |
|
15657 |
|
15658 |
|
15659 |
|
15660 |
|
15661 |
|
15662 |
|
15663 |
|
15664 |
|
15665 |
|
15666 |
|
15667 |
|
15668 | async function validateParams(n, e, d, p, q, u) {
|
15669 | const BigInteger = await util.getBigInteger();
|
15670 | n = new BigInteger(n);
|
15671 | p = new BigInteger(p);
|
15672 | q = new BigInteger(q);
|
15673 |
|
15674 |
|
15675 | if (!p.mul(q).equal(n)) {
|
15676 | return false;
|
15677 | }
|
15678 |
|
15679 | const two = new BigInteger(2);
|
15680 |
|
15681 | u = new BigInteger(u);
|
15682 | if (!p.mul(u).mod(q).isOne()) {
|
15683 | return false;
|
15684 | }
|
15685 |
|
15686 | e = new BigInteger(e);
|
15687 | d = new BigInteger(d);
|
15688 | |
15689 |
|
15690 |
|
15691 |
|
15692 |
|
15693 |
|
15694 |
|
15695 | const nSizeOver3 = new BigInteger(Math.floor(n.bitLength() / 3));
|
15696 | const r = await getRandomBigInteger(two, two.leftShift(nSizeOver3));
|
15697 | const rde = r.mul(d).mul(e);
|
15698 |
|
15699 | const areInverses = rde.mod(p.dec()).equal(r) && rde.mod(q.dec()).equal(r);
|
15700 | if (!areInverses) {
|
15701 | return false;
|
15702 | }
|
15703 |
|
15704 | return true;
|
15705 | }
|
15706 |
|
15707 | async function bnSign(hash_algo, n, d, hashed) {
|
15708 | const BigInteger = await util.getBigInteger();
|
15709 | n = new BigInteger(n);
|
15710 | const m = new BigInteger(await emsaEncode(hash_algo, hashed, n.byteLength()));
|
15711 | d = new BigInteger(d);
|
15712 | if (m.gte(n)) {
|
15713 | throw new Error('Message size cannot exceed modulus size');
|
15714 | }
|
15715 | return m.modExp(d, n).toUint8Array('be', n.byteLength());
|
15716 | }
|
15717 |
|
15718 | async function webSign(hash_name, data, n, e, d, p, q, u) {
|
15719 | |
15720 |
|
15721 |
|
15722 |
|
15723 |
|
15724 |
|
15725 | const jwk = await privateToJwk(n, e, d, p, q, u);
|
15726 | const algo = {
|
15727 | name: "RSASSA-PKCS1-v1_5",
|
15728 | hash: { name: hash_name }
|
15729 | };
|
15730 | const key = await webCrypto$5.importKey("jwk", jwk, algo, false, ["sign"]);
|
15731 |
|
15732 | return new Uint8Array(await webCrypto$5.sign({ "name": "RSASSA-PKCS1-v1_5", "hash": hash_name }, key, data));
|
15733 | }
|
15734 |
|
15735 | async function nodeSign(hash_algo, data, n, e, d, p, q, u) {
|
15736 | const { default: BN } = await Promise.resolve().then(function () { return bn$1; });
|
15737 | const pBNum = new BN(p);
|
15738 | const qBNum = new BN(q);
|
15739 | const dBNum = new BN(d);
|
15740 | const dq = dBNum.mod(qBNum.subn(1));
|
15741 | const dp = dBNum.mod(pBNum.subn(1));
|
15742 | const sign = nodeCrypto$6.createSign(enums.read(enums.hash, hash_algo));
|
15743 | sign.write(data);
|
15744 | sign.end();
|
15745 | const keyObject = {
|
15746 | version: 0,
|
15747 | modulus: new BN(n),
|
15748 | publicExponent: new BN(e),
|
15749 | privateExponent: new BN(d),
|
15750 |
|
15751 | prime1: new BN(q),
|
15752 | prime2: new BN(p),
|
15753 |
|
15754 | exponent1: dq,
|
15755 | exponent2: dp,
|
15756 | coefficient: new BN(u)
|
15757 | };
|
15758 | if (typeof nodeCrypto$6.createPrivateKey !== 'undefined') {
|
15759 | const der = RSAPrivateKey.encode(keyObject, 'der');
|
15760 | return new Uint8Array(sign.sign({ key: der, format: 'der', type: 'pkcs1' }));
|
15761 | }
|
15762 | const pem = RSAPrivateKey.encode(keyObject, 'pem', {
|
15763 | label: 'RSA PRIVATE KEY'
|
15764 | });
|
15765 | return new Uint8Array(sign.sign(pem));
|
15766 | }
|
15767 |
|
15768 | async function bnVerify(hash_algo, s, n, e, hashed) {
|
15769 | const BigInteger = await util.getBigInteger();
|
15770 | n = new BigInteger(n);
|
15771 | s = new BigInteger(s);
|
15772 | e = new BigInteger(e);
|
15773 | if (s.gte(n)) {
|
15774 | throw new Error('Signature size cannot exceed modulus size');
|
15775 | }
|
15776 | const EM1 = s.modExp(e, n).toUint8Array('be', n.byteLength());
|
15777 | const EM2 = await emsaEncode(hash_algo, hashed, n.byteLength());
|
15778 | return util.equalsUint8Array(EM1, EM2);
|
15779 | }
|
15780 |
|
15781 | async function webVerify(hash_name, data, s, n, e) {
|
15782 | const jwk = publicToJwk(n, e);
|
15783 | const key = await webCrypto$5.importKey("jwk", jwk, {
|
15784 | name: "RSASSA-PKCS1-v1_5",
|
15785 | hash: { name: hash_name }
|
15786 | }, false, ["verify"]);
|
15787 |
|
15788 | return webCrypto$5.verify({ "name": "RSASSA-PKCS1-v1_5", "hash": hash_name }, key, s, data);
|
15789 | }
|
15790 |
|
15791 | async function nodeVerify(hash_algo, data, s, n, e) {
|
15792 | const { default: BN } = await Promise.resolve().then(function () { return bn$1; });
|
15793 |
|
15794 | const verify = nodeCrypto$6.createVerify(enums.read(enums.hash, hash_algo));
|
15795 | verify.write(data);
|
15796 | verify.end();
|
15797 | const keyObject = {
|
15798 | modulus: new BN(n),
|
15799 | publicExponent: new BN(e)
|
15800 | };
|
15801 | let key;
|
15802 | if (typeof nodeCrypto$6.createPrivateKey !== 'undefined') {
|
15803 | const der = RSAPublicKey.encode(keyObject, 'der');
|
15804 | key = { key: der, format: 'der', type: 'pkcs1' };
|
15805 | } else {
|
15806 | key = RSAPublicKey.encode(keyObject, 'pem', {
|
15807 | label: 'RSA PUBLIC KEY'
|
15808 | });
|
15809 | }
|
15810 | try {
|
15811 | return await verify.verify(key, s);
|
15812 | } catch (err) {
|
15813 | return false;
|
15814 | }
|
15815 | }
|
15816 |
|
15817 | async function nodeEncrypt$1(data, n, e) {
|
15818 | const { default: BN } = await Promise.resolve().then(function () { return bn$1; });
|
15819 |
|
15820 | const keyObject = {
|
15821 | modulus: new BN(n),
|
15822 | publicExponent: new BN(e)
|
15823 | };
|
15824 | let key;
|
15825 | if (typeof nodeCrypto$6.createPrivateKey !== 'undefined') {
|
15826 | const der = RSAPublicKey.encode(keyObject, 'der');
|
15827 | key = { key: der, format: 'der', type: 'pkcs1', padding: nodeCrypto$6.constants.RSA_PKCS1_PADDING };
|
15828 | } else {
|
15829 | const pem = RSAPublicKey.encode(keyObject, 'pem', {
|
15830 | label: 'RSA PUBLIC KEY'
|
15831 | });
|
15832 | key = { key: pem, padding: nodeCrypto$6.constants.RSA_PKCS1_PADDING };
|
15833 | }
|
15834 | return new Uint8Array(nodeCrypto$6.publicEncrypt(key, data));
|
15835 | }
|
15836 |
|
15837 | async function bnEncrypt(data, n, e) {
|
15838 | const BigInteger = await util.getBigInteger();
|
15839 | n = new BigInteger(n);
|
15840 | data = new BigInteger(await emeEncode(data, n.byteLength()));
|
15841 | e = new BigInteger(e);
|
15842 | if (data.gte(n)) {
|
15843 | throw new Error('Message size cannot exceed modulus size');
|
15844 | }
|
15845 | return data.modExp(e, n).toUint8Array('be', n.byteLength());
|
15846 | }
|
15847 |
|
15848 | async function nodeDecrypt$1(data, n, e, d, p, q, u) {
|
15849 | const { default: BN } = await Promise.resolve().then(function () { return bn$1; });
|
15850 |
|
15851 | const pBNum = new BN(p);
|
15852 | const qBNum = new BN(q);
|
15853 | const dBNum = new BN(d);
|
15854 | const dq = dBNum.mod(qBNum.subn(1));
|
15855 | const dp = dBNum.mod(pBNum.subn(1));
|
15856 | const keyObject = {
|
15857 | version: 0,
|
15858 | modulus: new BN(n),
|
15859 | publicExponent: new BN(e),
|
15860 | privateExponent: new BN(d),
|
15861 |
|
15862 | prime1: new BN(q),
|
15863 | prime2: new BN(p),
|
15864 |
|
15865 | exponent1: dq,
|
15866 | exponent2: dp,
|
15867 | coefficient: new BN(u)
|
15868 | };
|
15869 | let key;
|
15870 | if (typeof nodeCrypto$6.createPrivateKey !== 'undefined') {
|
15871 | const der = RSAPrivateKey.encode(keyObject, 'der');
|
15872 | key = { key: der, format: 'der' , type: 'pkcs1', padding: nodeCrypto$6.constants.RSA_PKCS1_PADDING };
|
15873 | } else {
|
15874 | const pem = RSAPrivateKey.encode(keyObject, 'pem', {
|
15875 | label: 'RSA PRIVATE KEY'
|
15876 | });
|
15877 | key = { key: pem, padding: nodeCrypto$6.constants.RSA_PKCS1_PADDING };
|
15878 | }
|
15879 | try {
|
15880 | return new Uint8Array(nodeCrypto$6.privateDecrypt(key, data));
|
15881 | } catch (err) {
|
15882 | throw new Error('Decryption error');
|
15883 | }
|
15884 | }
|
15885 |
|
15886 | async function bnDecrypt(data, n, e, d, p, q, u) {
|
15887 | const BigInteger = await util.getBigInteger();
|
15888 | data = new BigInteger(data);
|
15889 | n = new BigInteger(n);
|
15890 | e = new BigInteger(e);
|
15891 | d = new BigInteger(d);
|
15892 | p = new BigInteger(p);
|
15893 | q = new BigInteger(q);
|
15894 | u = new BigInteger(u);
|
15895 | if (data.gte(n)) {
|
15896 | throw new Error('Data too large.');
|
15897 | }
|
15898 | const dq = d.mod(q.dec());
|
15899 | const dp = d.mod(p.dec());
|
15900 |
|
15901 | const unblinder = (await getRandomBigInteger(new BigInteger(2), n)).mod(n);
|
15902 | const blinder = unblinder.modInv(n).modExp(e, n);
|
15903 | data = data.mul(blinder).mod(n);
|
15904 |
|
15905 |
|
15906 | const mp = data.modExp(dp, p);
|
15907 | const mq = data.modExp(dq, q);
|
15908 | const h = u.mul(mq.sub(mp)).mod(q);
|
15909 |
|
15910 | let result = h.mul(p).add(mp);
|
15911 |
|
15912 | result = result.mul(unblinder).mod(n);
|
15913 |
|
15914 |
|
15915 | return emeDecode(result.toUint8Array('be', n.byteLength()));
|
15916 | }
|
15917 |
|
15918 |
|
15919 |
|
15920 |
|
15921 |
|
15922 |
|
15923 |
|
15924 |
|
15925 |
|
15926 |
|
15927 |
|
15928 | async function privateToJwk(n, e, d, p, q, u) {
|
15929 | const BigInteger = await util.getBigInteger();
|
15930 | const pNum = new BigInteger(p);
|
15931 | const qNum = new BigInteger(q);
|
15932 | const dNum = new BigInteger(d);
|
15933 |
|
15934 | let dq = dNum.mod(qNum.dec());
|
15935 | let dp = dNum.mod(pNum.dec());
|
15936 | dp = dp.toUint8Array();
|
15937 | dq = dq.toUint8Array();
|
15938 | return {
|
15939 | kty: 'RSA',
|
15940 | n: uint8ArrayToB64(n, true),
|
15941 | e: uint8ArrayToB64(e, true),
|
15942 | d: uint8ArrayToB64(d, true),
|
15943 |
|
15944 | p: uint8ArrayToB64(q, true),
|
15945 | q: uint8ArrayToB64(p, true),
|
15946 |
|
15947 | dp: uint8ArrayToB64(dq, true),
|
15948 | dq: uint8ArrayToB64(dp, true),
|
15949 | qi: uint8ArrayToB64(u, true),
|
15950 | ext: true
|
15951 | };
|
15952 | }
|
15953 |
|
15954 |
|
15955 |
|
15956 |
|
15957 |
|
15958 |
|
15959 |
|
15960 | function publicToJwk(n, e) {
|
15961 | return {
|
15962 | kty: 'RSA',
|
15963 | n: uint8ArrayToB64(n, true),
|
15964 | e: uint8ArrayToB64(e, true),
|
15965 | ext: true
|
15966 | };
|
15967 | }
|
15968 |
|
15969 | var rsa = Object.freeze({
|
15970 | __proto__: null,
|
15971 | sign: sign,
|
15972 | verify: verify,
|
15973 | encrypt: encrypt$1,
|
15974 | decrypt: decrypt$1,
|
15975 | generate: generate,
|
15976 | validateParams: validateParams
|
15977 | });
|
15978 |
|
15979 |
|
15980 |
|
15981 |
|
15982 |
|
15983 |
|
15984 |
|
15985 |
|
15986 |
|
15987 |
|
15988 |
|
15989 |
|
15990 |
|
15991 | async function encrypt$2(data, p, g, y) {
|
15992 | const BigInteger = await util.getBigInteger();
|
15993 | p = new BigInteger(p);
|
15994 | g = new BigInteger(g);
|
15995 | y = new BigInteger(y);
|
15996 |
|
15997 | const padded = await emeEncode(data, p.byteLength());
|
15998 | const m = new BigInteger(padded);
|
15999 |
|
16000 |
|
16001 |
|
16002 | const k = await getRandomBigInteger(new BigInteger(1), p.dec());
|
16003 | return {
|
16004 | c1: g.modExp(k, p).toUint8Array(),
|
16005 | c2: y.modExp(k, p).imul(m).imod(p).toUint8Array()
|
16006 | };
|
16007 | }
|
16008 |
|
16009 |
|
16010 |
|
16011 |
|
16012 |
|
16013 |
|
16014 |
|
16015 |
|
16016 |
|
16017 |
|
16018 | async function decrypt$2(c1, c2, p, x) {
|
16019 | const BigInteger = await util.getBigInteger();
|
16020 | c1 = new BigInteger(c1);
|
16021 | c2 = new BigInteger(c2);
|
16022 | p = new BigInteger(p);
|
16023 | x = new BigInteger(x);
|
16024 |
|
16025 | const padded = c1.modExp(x, p).modInv(p).imul(c2).imod(p);
|
16026 | return emeDecode(padded.toUint8Array('be', p.byteLength()));
|
16027 | }
|
16028 |
|
16029 |
|
16030 |
|
16031 |
|
16032 |
|
16033 |
|
16034 |
|
16035 |
|
16036 |
|
16037 |
|
16038 | async function validateParams$1(p, g, y, x) {
|
16039 | const BigInteger = await util.getBigInteger();
|
16040 | p = new BigInteger(p);
|
16041 | g = new BigInteger(g);
|
16042 | y = new BigInteger(y);
|
16043 |
|
16044 | const one = new BigInteger(1);
|
16045 |
|
16046 | if (g.lte(one) || g.gte(p)) {
|
16047 | return false;
|
16048 | }
|
16049 |
|
16050 |
|
16051 | const pSize = new BigInteger(p.bitLength());
|
16052 | const n1023 = new BigInteger(1023);
|
16053 | if (pSize.lt(n1023)) {
|
16054 | return false;
|
16055 | }
|
16056 |
|
16057 | |
16058 |
|
16059 |
|
16060 |
|
16061 | if (!g.modExp(p.dec(), p).isOne()) {
|
16062 | return false;
|
16063 | }
|
16064 |
|
16065 | |
16066 |
|
16067 |
|
16068 |
|
16069 |
|
16070 |
|
16071 | let res = g;
|
16072 | const i = new BigInteger(1);
|
16073 | const threshold = new BigInteger(2).leftShift(new BigInteger(17));
|
16074 | while (i.lt(threshold)) {
|
16075 | res = res.mul(g).imod(p);
|
16076 | if (res.isOne()) {
|
16077 | return false;
|
16078 | }
|
16079 | i.iinc();
|
16080 | }
|
16081 |
|
16082 | |
16083 |
|
16084 |
|
16085 |
|
16086 |
|
16087 |
|
16088 | x = new BigInteger(x);
|
16089 | const two = new BigInteger(2);
|
16090 | const r = await getRandomBigInteger(two.leftShift(pSize.dec()), two.leftShift(pSize));
|
16091 | const rqx = p.dec().imul(r).iadd(x);
|
16092 | if (!y.equal(g.modExp(rqx, p))) {
|
16093 | return false;
|
16094 | }
|
16095 |
|
16096 | return true;
|
16097 | }
|
16098 |
|
16099 | var elgamal = Object.freeze({
|
16100 | __proto__: null,
|
16101 | encrypt: encrypt$2,
|
16102 | decrypt: decrypt$2,
|
16103 | validateParams: validateParams$1
|
16104 | });
|
16105 |
|
16106 |
|
16107 |
|
16108 | class OID {
|
16109 | constructor(oid) {
|
16110 | if (oid instanceof OID) {
|
16111 | this.oid = oid.oid;
|
16112 | } else if (util.isArray(oid) ||
|
16113 | util.isUint8Array(oid)) {
|
16114 | oid = new Uint8Array(oid);
|
16115 | if (oid[0] === 0x06) {
|
16116 | if (oid[1] !== oid.length - 2) {
|
16117 | throw new Error('Length mismatch in DER encoded oid');
|
16118 | }
|
16119 | oid = oid.subarray(2);
|
16120 | }
|
16121 | this.oid = oid;
|
16122 | } else {
|
16123 | this.oid = '';
|
16124 | }
|
16125 | }
|
16126 |
|
16127 | |
16128 |
|
16129 |
|
16130 |
|
16131 |
|
16132 | read(input) {
|
16133 | if (input.length >= 1) {
|
16134 | const length = input[0];
|
16135 | if (input.length >= 1 + length) {
|
16136 | this.oid = input.subarray(1, 1 + length);
|
16137 | return 1 + this.oid.length;
|
16138 | }
|
16139 | }
|
16140 | throw new Error('Invalid oid');
|
16141 | }
|
16142 |
|
16143 | |
16144 |
|
16145 |
|
16146 |
|
16147 | write() {
|
16148 | return util.concatUint8Array([new Uint8Array([this.oid.length]), this.oid]);
|
16149 | }
|
16150 |
|
16151 | |
16152 |
|
16153 |
|
16154 |
|
16155 | toHex() {
|
16156 | return util.uint8ArrayToHex(this.oid);
|
16157 | }
|
16158 |
|
16159 | |
16160 |
|
16161 |
|
16162 |
|
16163 | getName() {
|
16164 | const hex = this.toHex();
|
16165 | if (enums.curve[hex]) {
|
16166 | return enums.write(enums.curve, hex);
|
16167 | } else {
|
16168 | throw new Error('Unknown curve object identifier.');
|
16169 | }
|
16170 | }
|
16171 | }
|
16172 |
|
16173 |
|
16174 |
|
16175 | function keyFromPrivate(indutnyCurve, priv) {
|
16176 | const keyPair = indutnyCurve.keyPair({ priv: priv });
|
16177 | return keyPair;
|
16178 | }
|
16179 |
|
16180 | function keyFromPublic(indutnyCurve, pub) {
|
16181 | const keyPair = indutnyCurve.keyPair({ pub: pub });
|
16182 | if (keyPair.validate().result !== true) {
|
16183 | throw new Error('Invalid elliptic public key');
|
16184 | }
|
16185 | return keyPair;
|
16186 | }
|
16187 |
|
16188 | async function getIndutnyCurve(name) {
|
16189 | if (!defaultConfig.useIndutnyElliptic) {
|
16190 | throw new Error('This curve is only supported in the full build of OpenPGP.js');
|
16191 | }
|
16192 | const { default: elliptic } = await Promise.resolve().then(function () { return elliptic$1; });
|
16193 | return new elliptic.ec(name);
|
16194 | }
|
16195 |
|
16196 |
|
16197 |
|
16198 | const webCrypto$6 = util.getWebCrypto();
|
16199 | const nodeCrypto$7 = util.getNodeCrypto();
|
16200 |
|
16201 | const webCurves = {
|
16202 | 'p256': 'P-256',
|
16203 | 'p384': 'P-384',
|
16204 | 'p521': 'P-521'
|
16205 | };
|
16206 | const knownCurves = nodeCrypto$7 ? nodeCrypto$7.getCurves() : [];
|
16207 | const nodeCurves = nodeCrypto$7 ? {
|
16208 | secp256k1: knownCurves.includes('secp256k1') ? 'secp256k1' : undefined,
|
16209 | p256: knownCurves.includes('prime256v1') ? 'prime256v1' : undefined,
|
16210 | p384: knownCurves.includes('secp384r1') ? 'secp384r1' : undefined,
|
16211 | p521: knownCurves.includes('secp521r1') ? 'secp521r1' : undefined,
|
16212 | ed25519: knownCurves.includes('ED25519') ? 'ED25519' : undefined,
|
16213 | curve25519: knownCurves.includes('X25519') ? 'X25519' : undefined,
|
16214 | brainpoolP256r1: knownCurves.includes('brainpoolP256r1') ? 'brainpoolP256r1' : undefined,
|
16215 | brainpoolP384r1: knownCurves.includes('brainpoolP384r1') ? 'brainpoolP384r1' : undefined,
|
16216 | brainpoolP512r1: knownCurves.includes('brainpoolP512r1') ? 'brainpoolP512r1' : undefined
|
16217 | } : {};
|
16218 |
|
16219 | const curves = {
|
16220 | p256: {
|
16221 | oid: [0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07],
|
16222 | keyType: enums.publicKey.ecdsa,
|
16223 | hash: enums.hash.sha256,
|
16224 | cipher: enums.symmetric.aes128,
|
16225 | node: nodeCurves.p256,
|
16226 | web: webCurves.p256,
|
16227 | payloadSize: 32,
|
16228 | sharedSize: 256
|
16229 | },
|
16230 | p384: {
|
16231 | oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22],
|
16232 | keyType: enums.publicKey.ecdsa,
|
16233 | hash: enums.hash.sha384,
|
16234 | cipher: enums.symmetric.aes192,
|
16235 | node: nodeCurves.p384,
|
16236 | web: webCurves.p384,
|
16237 | payloadSize: 48,
|
16238 | sharedSize: 384
|
16239 | },
|
16240 | p521: {
|
16241 | oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x23],
|
16242 | keyType: enums.publicKey.ecdsa,
|
16243 | hash: enums.hash.sha512,
|
16244 | cipher: enums.symmetric.aes256,
|
16245 | node: nodeCurves.p521,
|
16246 | web: webCurves.p521,
|
16247 | payloadSize: 66,
|
16248 | sharedSize: 528
|
16249 | },
|
16250 | secp256k1: {
|
16251 | oid: [0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x0A],
|
16252 | keyType: enums.publicKey.ecdsa,
|
16253 | hash: enums.hash.sha256,
|
16254 | cipher: enums.symmetric.aes128,
|
16255 | node: nodeCurves.secp256k1,
|
16256 | payloadSize: 32
|
16257 | },
|
16258 | ed25519: {
|
16259 | oid: [0x06, 0x09, 0x2B, 0x06, 0x01, 0x04, 0x01, 0xDA, 0x47, 0x0F, 0x01],
|
16260 | keyType: enums.publicKey.eddsa,
|
16261 | hash: enums.hash.sha512,
|
16262 | node: false,
|
16263 | payloadSize: 32
|
16264 | },
|
16265 | curve25519: {
|
16266 | oid: [0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x97, 0x55, 0x01, 0x05, 0x01],
|
16267 | keyType: enums.publicKey.ecdh,
|
16268 | hash: enums.hash.sha256,
|
16269 | cipher: enums.symmetric.aes128,
|
16270 | node: false,
|
16271 | payloadSize: 32
|
16272 | },
|
16273 | brainpoolP256r1: {
|
16274 | oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07],
|
16275 | keyType: enums.publicKey.ecdsa,
|
16276 | hash: enums.hash.sha256,
|
16277 | cipher: enums.symmetric.aes128,
|
16278 | node: nodeCurves.brainpoolP256r1,
|
16279 | payloadSize: 32
|
16280 | },
|
16281 | brainpoolP384r1: {
|
16282 | oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B],
|
16283 | keyType: enums.publicKey.ecdsa,
|
16284 | hash: enums.hash.sha384,
|
16285 | cipher: enums.symmetric.aes192,
|
16286 | node: nodeCurves.brainpoolP384r1,
|
16287 | payloadSize: 48
|
16288 | },
|
16289 | brainpoolP512r1: {
|
16290 | oid: [0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D],
|
16291 | keyType: enums.publicKey.ecdsa,
|
16292 | hash: enums.hash.sha512,
|
16293 | cipher: enums.symmetric.aes256,
|
16294 | node: nodeCurves.brainpoolP512r1,
|
16295 | payloadSize: 64
|
16296 | }
|
16297 | };
|
16298 |
|
16299 | class Curve {
|
16300 | constructor(oid_or_name, params) {
|
16301 | try {
|
16302 | if (util.isArray(oid_or_name) ||
|
16303 | util.isUint8Array(oid_or_name)) {
|
16304 |
|
16305 | oid_or_name = new OID(oid_or_name);
|
16306 | }
|
16307 | if (oid_or_name instanceof OID) {
|
16308 |
|
16309 | oid_or_name = oid_or_name.getName();
|
16310 | }
|
16311 |
|
16312 | this.name = enums.write(enums.curve, oid_or_name);
|
16313 | } catch (err) {
|
16314 | throw new Error('Not valid curve');
|
16315 | }
|
16316 | params = params || curves[this.name];
|
16317 |
|
16318 | this.keyType = params.keyType;
|
16319 |
|
16320 | this.oid = params.oid;
|
16321 | this.hash = params.hash;
|
16322 | this.cipher = params.cipher;
|
16323 | this.node = params.node && curves[this.name];
|
16324 | this.web = params.web && curves[this.name];
|
16325 | this.payloadSize = params.payloadSize;
|
16326 | if (this.web && util.getWebCrypto()) {
|
16327 | this.type = 'web';
|
16328 | } else if (this.node && util.getNodeCrypto()) {
|
16329 | this.type = 'node';
|
16330 | } else if (this.name === 'curve25519') {
|
16331 | this.type = 'curve25519';
|
16332 | } else if (this.name === 'ed25519') {
|
16333 | this.type = 'ed25519';
|
16334 | }
|
16335 | }
|
16336 |
|
16337 | async genKeyPair() {
|
16338 | let keyPair;
|
16339 | switch (this.type) {
|
16340 | case 'web':
|
16341 | try {
|
16342 | return await webGenKeyPair(this.name);
|
16343 | } catch (err) {
|
16344 | util.printDebugError("Browser did not support generating ec key " + err.message);
|
16345 | break;
|
16346 | }
|
16347 | case 'node':
|
16348 | return nodeGenKeyPair(this.name);
|
16349 | case 'curve25519': {
|
16350 | const privateKey = await getRandomBytes(32);
|
16351 | privateKey[0] = (privateKey[0] & 127) | 64;
|
16352 | privateKey[31] &= 248;
|
16353 | const secretKey = privateKey.slice().reverse();
|
16354 | keyPair = naclFastLight.box.keyPair.fromSecretKey(secretKey);
|
16355 | const publicKey = util.concatUint8Array([new Uint8Array([0x40]), keyPair.publicKey]);
|
16356 | return { publicKey, privateKey };
|
16357 | }
|
16358 | case 'ed25519': {
|
16359 | const privateKey = await getRandomBytes(32);
|
16360 | const keyPair = naclFastLight.sign.keyPair.fromSeed(privateKey);
|
16361 | const publicKey = util.concatUint8Array([new Uint8Array([0x40]), keyPair.publicKey]);
|
16362 | return { publicKey, privateKey };
|
16363 | }
|
16364 | }
|
16365 | const indutnyCurve = await getIndutnyCurve(this.name);
|
16366 | keyPair = await indutnyCurve.genKeyPair({
|
16367 | entropy: util.uint8ArrayToStr(await getRandomBytes(32))
|
16368 | });
|
16369 | return { publicKey: new Uint8Array(keyPair.getPublic('array', false)), privateKey: keyPair.getPrivate().toArrayLike(Uint8Array) };
|
16370 | }
|
16371 | }
|
16372 |
|
16373 | async function generate$1(curve) {
|
16374 | const BigInteger = await util.getBigInteger();
|
16375 |
|
16376 | curve = new Curve(curve);
|
16377 | const keyPair = await curve.genKeyPair();
|
16378 | const Q = new BigInteger(keyPair.publicKey).toUint8Array();
|
16379 | const secret = new BigInteger(keyPair.privateKey).toUint8Array('be', curve.payloadSize);
|
16380 | return {
|
16381 | oid: curve.oid,
|
16382 | Q,
|
16383 | secret,
|
16384 | hash: curve.hash,
|
16385 | cipher: curve.cipher
|
16386 | };
|
16387 | }
|
16388 |
|
16389 | function getPreferredHashAlgo(oid) {
|
16390 | return curves[enums.write(enums.curve, oid.toHex())].hash;
|
16391 | }
|
16392 |
|
16393 |
|
16394 |
|
16395 |
|
16396 |
|
16397 |
|
16398 |
|
16399 |
|
16400 |
|
16401 |
|
16402 |
|
16403 | async function validateStandardParams(algo, oid, Q, d) {
|
16404 | const supportedCurves = {
|
16405 | p256: true,
|
16406 | p384: true,
|
16407 | p521: true,
|
16408 | secp256k1: true,
|
16409 | curve25519: algo === enums.publicKey.ecdh,
|
16410 | brainpoolP256r1: true,
|
16411 | brainpoolP384r1: true,
|
16412 | brainpoolP512r1: true
|
16413 | };
|
16414 |
|
16415 |
|
16416 | const curveName = oid.getName();
|
16417 | if (!supportedCurves[curveName]) {
|
16418 | return false;
|
16419 | }
|
16420 |
|
16421 | if (curveName === 'curve25519') {
|
16422 | d = d.slice().reverse();
|
16423 |
|
16424 | const { publicKey } = naclFastLight.box.keyPair.fromSecretKey(d);
|
16425 |
|
16426 | Q = new Uint8Array(Q);
|
16427 | const dG = new Uint8Array([0x40, ...publicKey]);
|
16428 | if (!util.equalsUint8Array(dG, Q)) {
|
16429 | return false;
|
16430 | }
|
16431 |
|
16432 | return true;
|
16433 | }
|
16434 |
|
16435 | const curve = await getIndutnyCurve(curveName);
|
16436 | try {
|
16437 |
|
16438 | Q = keyFromPublic(curve, Q).getPublic();
|
16439 | } catch (validationErrors) {
|
16440 | return false;
|
16441 | }
|
16442 |
|
16443 | |
16444 |
|
16445 |
|
16446 |
|
16447 | const dG = keyFromPrivate(curve, d).getPublic();
|
16448 | if (!dG.eq(Q)) {
|
16449 | return false;
|
16450 | }
|
16451 |
|
16452 | return true;
|
16453 | }
|
16454 |
|
16455 |
|
16456 |
|
16457 |
|
16458 |
|
16459 |
|
16460 |
|
16461 |
|
16462 | async function webGenKeyPair(name) {
|
16463 |
|
16464 | const webCryptoKey = await webCrypto$6.generateKey({ name: "ECDSA", namedCurve: webCurves[name] }, true, ["sign", "verify"]);
|
16465 |
|
16466 | const privateKey = await webCrypto$6.exportKey("jwk", webCryptoKey.privateKey);
|
16467 | const publicKey = await webCrypto$6.exportKey("jwk", webCryptoKey.publicKey);
|
16468 |
|
16469 | return {
|
16470 | publicKey: jwkToRawPublic(publicKey),
|
16471 | privateKey: b64ToUint8Array(privateKey.d)
|
16472 | };
|
16473 | }
|
16474 |
|
16475 | async function nodeGenKeyPair(name) {
|
16476 |
|
16477 | const ecdh = nodeCrypto$7.createECDH(nodeCurves[name]);
|
16478 | await ecdh.generateKeys();
|
16479 | return {
|
16480 | publicKey: new Uint8Array(ecdh.getPublicKey()),
|
16481 | privateKey: new Uint8Array(ecdh.getPrivateKey())
|
16482 | };
|
16483 | }
|
16484 |
|
16485 |
|
16486 |
|
16487 |
|
16488 |
|
16489 |
|
16490 |
|
16491 |
|
16492 |
|
16493 |
|
16494 |
|
16495 |
|
16496 | function jwkToRawPublic(jwk) {
|
16497 | const bufX = b64ToUint8Array(jwk.x);
|
16498 | const bufY = b64ToUint8Array(jwk.y);
|
16499 | const publicKey = new Uint8Array(bufX.length + bufY.length + 1);
|
16500 | publicKey[0] = 0x04;
|
16501 | publicKey.set(bufX, 1);
|
16502 | publicKey.set(bufY, bufX.length + 1);
|
16503 | return publicKey;
|
16504 | }
|
16505 |
|
16506 |
|
16507 |
|
16508 |
|
16509 |
|
16510 |
|
16511 |
|
16512 |
|
16513 | function rawPublicToJwk(payloadSize, name, publicKey) {
|
16514 | const len = payloadSize;
|
16515 | const bufX = publicKey.slice(1, len + 1);
|
16516 | const bufY = publicKey.slice(len + 1, len * 2 + 1);
|
16517 |
|
16518 | const jwk = {
|
16519 | kty: "EC",
|
16520 | crv: name,
|
16521 | x: uint8ArrayToB64(bufX, true),
|
16522 | y: uint8ArrayToB64(bufY, true),
|
16523 | ext: true
|
16524 | };
|
16525 | return jwk;
|
16526 | }
|
16527 |
|
16528 |
|
16529 |
|
16530 |
|
16531 |
|
16532 |
|
16533 |
|
16534 |
|
16535 |
|
16536 | function privateToJwk$1(payloadSize, name, publicKey, privateKey) {
|
16537 | const jwk = rawPublicToJwk(payloadSize, name, publicKey);
|
16538 | jwk.d = uint8ArrayToB64(privateKey, true);
|
16539 | return jwk;
|
16540 | }
|
16541 |
|
16542 |
|
16543 |
|
16544 | const webCrypto$7 = util.getWebCrypto();
|
16545 | const nodeCrypto$8 = util.getNodeCrypto();
|
16546 |
|
16547 |
|
16548 |
|
16549 |
|
16550 |
|
16551 |
|
16552 |
|
16553 |
|
16554 |
|
16555 |
|
16556 |
|
16557 |
|
16558 |
|
16559 | async function sign$1(oid, hash_algo, message, publicKey, privateKey, hashed) {
|
16560 | const curve = new Curve(oid);
|
16561 | if (message && !util.isStream(message)) {
|
16562 | const keyPair = { publicKey, privateKey };
|
16563 | switch (curve.type) {
|
16564 | case 'web': {
|
16565 |
|
16566 | try {
|
16567 |
|
16568 | return await webSign$1(curve, hash_algo, message, keyPair);
|
16569 | } catch (err) {
|
16570 |
|
16571 |
|
16572 |
|
16573 | if (curve.name !== 'p521' && (err.name === 'DataError' || err.name === 'OperationError')) {
|
16574 | throw err;
|
16575 | }
|
16576 | util.printDebugError("Browser did not support signing: " + err.message);
|
16577 | }
|
16578 | break;
|
16579 | }
|
16580 | case 'node': {
|
16581 | const signature = await nodeSign$1(curve, hash_algo, message, keyPair);
|
16582 | return {
|
16583 | r: signature.r.toArrayLike(Uint8Array),
|
16584 | s: signature.s.toArrayLike(Uint8Array)
|
16585 | };
|
16586 | }
|
16587 | }
|
16588 | }
|
16589 | return ellipticSign(curve, hashed, privateKey);
|
16590 | }
|
16591 |
|
16592 |
|
16593 |
|
16594 |
|
16595 |
|
16596 |
|
16597 |
|
16598 |
|
16599 |
|
16600 |
|
16601 |
|
16602 |
|
16603 |
|
16604 | async function verify$1(oid, hash_algo, signature, message, publicKey, hashed) {
|
16605 | const curve = new Curve(oid);
|
16606 | if (message && !util.isStream(message)) {
|
16607 | switch (curve.type) {
|
16608 | case 'web':
|
16609 | try {
|
16610 |
|
16611 | return await webVerify$1(curve, hash_algo, signature, message, publicKey);
|
16612 | } catch (err) {
|
16613 |
|
16614 |
|
16615 |
|
16616 | if (curve.name !== 'p521' && (err.name === 'DataError' || err.name === 'OperationError')) {
|
16617 | throw err;
|
16618 | }
|
16619 | util.printDebugError("Browser did not support verifying: " + err.message);
|
16620 | }
|
16621 | break;
|
16622 | case 'node':
|
16623 | return nodeVerify$1(curve, hash_algo, signature, message, publicKey);
|
16624 | }
|
16625 | }
|
16626 | const digest = (typeof hash_algo === 'undefined') ? message : hashed;
|
16627 | return ellipticVerify(curve, signature, digest, publicKey);
|
16628 | }
|
16629 |
|
16630 |
|
16631 |
|
16632 |
|
16633 |
|
16634 |
|
16635 |
|
16636 |
|
16637 |
|
16638 | async function validateParams$2(oid, Q, d) {
|
16639 | const curve = new Curve(oid);
|
16640 |
|
16641 | if (curve.keyType !== enums.publicKey.ecdsa) {
|
16642 | return false;
|
16643 | }
|
16644 |
|
16645 |
|
16646 |
|
16647 | switch (curve.type) {
|
16648 | case 'web':
|
16649 | case 'node': {
|
16650 | const message = await getRandomBytes(8);
|
16651 | const hashAlgo = enums.hash.sha256;
|
16652 | const hashed = await hash.digest(hashAlgo, message);
|
16653 | try {
|
16654 | const signature = await sign$1(oid, hashAlgo, message, Q, d, hashed);
|
16655 | return await verify$1(oid, hashAlgo, signature, message, Q, hashed);
|
16656 | } catch (err) {
|
16657 | return false;
|
16658 | }
|
16659 | }
|
16660 | default:
|
16661 | return validateStandardParams(enums.publicKey.ecdsa, oid, Q, d);
|
16662 | }
|
16663 | }
|
16664 |
|
16665 |
|
16666 |
|
16667 |
|
16668 |
|
16669 |
|
16670 |
|
16671 |
|
16672 | async function ellipticSign(curve, hashed, privateKey) {
|
16673 | const indutnyCurve = await getIndutnyCurve(curve.name);
|
16674 | const key = keyFromPrivate(indutnyCurve, privateKey);
|
16675 | const signature = key.sign(hashed);
|
16676 | return {
|
16677 | r: signature.r.toArrayLike(Uint8Array),
|
16678 | s: signature.s.toArrayLike(Uint8Array)
|
16679 | };
|
16680 | }
|
16681 |
|
16682 | async function ellipticVerify(curve, signature, digest, publicKey) {
|
16683 | const indutnyCurve = await getIndutnyCurve(curve.name);
|
16684 | const key = keyFromPublic(indutnyCurve, publicKey);
|
16685 | return key.verify(digest, signature);
|
16686 | }
|
16687 |
|
16688 | async function webSign$1(curve, hash_algo, message, keyPair) {
|
16689 | const len = curve.payloadSize;
|
16690 | const jwk = privateToJwk$1(curve.payloadSize, webCurves[curve.name], keyPair.publicKey, keyPair.privateKey);
|
16691 | const key = await webCrypto$7.importKey(
|
16692 | "jwk",
|
16693 | jwk,
|
16694 | {
|
16695 | "name": "ECDSA",
|
16696 | "namedCurve": webCurves[curve.name],
|
16697 | "hash": { name: enums.read(enums.webHash, curve.hash) }
|
16698 | },
|
16699 | false,
|
16700 | ["sign"]
|
16701 | );
|
16702 |
|
16703 | const signature = new Uint8Array(await webCrypto$7.sign(
|
16704 | {
|
16705 | "name": 'ECDSA',
|
16706 | "namedCurve": webCurves[curve.name],
|
16707 | "hash": { name: enums.read(enums.webHash, hash_algo) }
|
16708 | },
|
16709 | key,
|
16710 | message
|
16711 | ));
|
16712 |
|
16713 | return {
|
16714 | r: signature.slice(0, len),
|
16715 | s: signature.slice(len, len << 1)
|
16716 | };
|
16717 | }
|
16718 |
|
16719 | async function webVerify$1(curve, hash_algo, { r, s }, message, publicKey) {
|
16720 | const jwk = rawPublicToJwk(curve.payloadSize, webCurves[curve.name], publicKey);
|
16721 | const key = await webCrypto$7.importKey(
|
16722 | "jwk",
|
16723 | jwk,
|
16724 | {
|
16725 | "name": "ECDSA",
|
16726 | "namedCurve": webCurves[curve.name],
|
16727 | "hash": { name: enums.read(enums.webHash, curve.hash) }
|
16728 | },
|
16729 | false,
|
16730 | ["verify"]
|
16731 | );
|
16732 |
|
16733 | const signature = util.concatUint8Array([r, s]).buffer;
|
16734 |
|
16735 | return webCrypto$7.verify(
|
16736 | {
|
16737 | "name": 'ECDSA',
|
16738 | "namedCurve": webCurves[curve.name],
|
16739 | "hash": { name: enums.read(enums.webHash, hash_algo) }
|
16740 | },
|
16741 | key,
|
16742 | signature,
|
16743 | message
|
16744 | );
|
16745 | }
|
16746 |
|
16747 | async function nodeSign$1(curve, hash_algo, message, keyPair) {
|
16748 | const sign = nodeCrypto$8.createSign(enums.read(enums.hash, hash_algo));
|
16749 | sign.write(message);
|
16750 | sign.end();
|
16751 | const key = ECPrivateKey.encode({
|
16752 | version: 1,
|
16753 | parameters: curve.oid,
|
16754 | privateKey: Array.from(keyPair.privateKey),
|
16755 | publicKey: { unused: 0, data: Array.from(keyPair.publicKey) }
|
16756 | }, 'pem', {
|
16757 | label: 'EC PRIVATE KEY'
|
16758 | });
|
16759 |
|
16760 | return ECDSASignature.decode(sign.sign(key), 'der');
|
16761 | }
|
16762 |
|
16763 | async function nodeVerify$1(curve, hash_algo, { r, s }, message, publicKey) {
|
16764 | const { default: BN } = await Promise.resolve().then(function () { return bn$1; });
|
16765 |
|
16766 | const verify = nodeCrypto$8.createVerify(enums.read(enums.hash, hash_algo));
|
16767 | verify.write(message);
|
16768 | verify.end();
|
16769 | const key = SubjectPublicKeyInfo.encode({
|
16770 | algorithm: {
|
16771 | algorithm: [1, 2, 840, 10045, 2, 1],
|
16772 | parameters: curve.oid
|
16773 | },
|
16774 | subjectPublicKey: { unused: 0, data: Array.from(publicKey) }
|
16775 | }, 'pem', {
|
16776 | label: 'PUBLIC KEY'
|
16777 | });
|
16778 | const signature = ECDSASignature.encode({
|
16779 | r: new BN(r), s: new BN(s)
|
16780 | }, 'der');
|
16781 |
|
16782 | try {
|
16783 | return verify.verify(key, signature);
|
16784 | } catch (err) {
|
16785 | return false;
|
16786 | }
|
16787 | }
|
16788 |
|
16789 |
|
16790 |
|
16791 |
|
16792 |
|
16793 |
|
16794 | const asn1$1 = nodeCrypto$8 ? void('asn1.js') : undefined;
|
16795 |
|
16796 | const ECDSASignature = nodeCrypto$8 ?
|
16797 | asn1$1.define('ECDSASignature', function() {
|
16798 | this.seq().obj(
|
16799 | this.key('r').int(),
|
16800 | this.key('s').int()
|
16801 | );
|
16802 | }) : undefined;
|
16803 |
|
16804 | const ECPrivateKey = nodeCrypto$8 ?
|
16805 | asn1$1.define('ECPrivateKey', function() {
|
16806 | this.seq().obj(
|
16807 | this.key('version').int(),
|
16808 | this.key('privateKey').octstr(),
|
16809 | this.key('parameters').explicit(0).optional().any(),
|
16810 | this.key('publicKey').explicit(1).optional().bitstr()
|
16811 | );
|
16812 | }) : undefined;
|
16813 |
|
16814 | const AlgorithmIdentifier = nodeCrypto$8 ?
|
16815 | asn1$1.define('AlgorithmIdentifier', function() {
|
16816 | this.seq().obj(
|
16817 | this.key('algorithm').objid(),
|
16818 | this.key('parameters').optional().any()
|
16819 | );
|
16820 | }) : undefined;
|
16821 |
|
16822 | const SubjectPublicKeyInfo = nodeCrypto$8 ?
|
16823 | asn1$1.define('SubjectPublicKeyInfo', function() {
|
16824 | this.seq().obj(
|
16825 | this.key('algorithm').use(AlgorithmIdentifier),
|
16826 | this.key('subjectPublicKey').bitstr()
|
16827 | );
|
16828 | }) : undefined;
|
16829 |
|
16830 | var ecdsa = Object.freeze({
|
16831 | __proto__: null,
|
16832 | sign: sign$1,
|
16833 | verify: verify$1,
|
16834 | validateParams: validateParams$2
|
16835 | });
|
16836 |
|
16837 |
|
16838 |
|
16839 | naclFastLight.hash = bytes => new Uint8Array(_512().update(bytes).digest());
|
16840 |
|
16841 |
|
16842 |
|
16843 |
|
16844 |
|
16845 |
|
16846 |
|
16847 |
|
16848 |
|
16849 |
|
16850 |
|
16851 |
|
16852 |
|
16853 | async function sign$2(oid, hash_algo, message, publicKey, privateKey, hashed) {
|
16854 | const secretKey = util.concatUint8Array([privateKey, publicKey.subarray(1)]);
|
16855 | const signature = naclFastLight.sign.detached(hashed, secretKey);
|
16856 |
|
16857 | return {
|
16858 | r: signature.subarray(0, 32),
|
16859 | s: signature.subarray(32)
|
16860 | };
|
16861 | }
|
16862 |
|
16863 |
|
16864 |
|
16865 |
|
16866 |
|
16867 |
|
16868 |
|
16869 |
|
16870 |
|
16871 |
|
16872 |
|
16873 |
|
16874 |
|
16875 | async function verify$2(oid, hash_algo, { r, s }, m, publicKey, hashed) {
|
16876 | const signature = util.concatUint8Array([r, s]);
|
16877 | return naclFastLight.sign.detached.verify(hashed, signature, publicKey.subarray(1));
|
16878 | }
|
16879 |
|
16880 |
|
16881 |
|
16882 |
|
16883 |
|
16884 |
|
16885 |
|
16886 |
|
16887 | async function validateParams$3(oid, Q, k) {
|
16888 |
|
16889 | if (oid.getName() !== 'ed25519') {
|
16890 | return false;
|
16891 | }
|
16892 |
|
16893 | |
16894 |
|
16895 |
|
16896 |
|
16897 | const { publicKey } = naclFastLight.sign.keyPair.fromSeed(k);
|
16898 | const dG = new Uint8Array([0x40, ...publicKey]);
|
16899 | return util.equalsUint8Array(Q, dG);
|
16900 | }
|
16901 |
|
16902 | var eddsa = Object.freeze({
|
16903 | __proto__: null,
|
16904 | sign: sign$2,
|
16905 | verify: verify$2,
|
16906 | validateParams: validateParams$3
|
16907 | });
|
16908 |
|
16909 |
|
16910 |
|
16911 |
|
16912 |
|
16913 |
|
16914 |
|
16915 |
|
16916 |
|
16917 |
|
16918 | function wrap(key, data) {
|
16919 | const aes = new cipher["aes" + (key.length * 8)](key);
|
16920 | const IV = new Uint32Array([0xA6A6A6A6, 0xA6A6A6A6]);
|
16921 | const P = unpack(data);
|
16922 | let A = IV;
|
16923 | const R = P;
|
16924 | const n = P.length / 2;
|
16925 | const t = new Uint32Array([0, 0]);
|
16926 | let B = new Uint32Array(4);
|
16927 | for (let j = 0; j <= 5; ++j) {
|
16928 | for (let i = 0; i < n; ++i) {
|
16929 | t[1] = n * j + (1 + i);
|
16930 |
|
16931 | B[0] = A[0];
|
16932 | B[1] = A[1];
|
16933 |
|
16934 | B[2] = R[2 * i];
|
16935 | B[3] = R[2 * i + 1];
|
16936 |
|
16937 | B = unpack(aes.encrypt(pack(B)));
|
16938 |
|
16939 | A = B.subarray(0, 2);
|
16940 | A[0] ^= t[0];
|
16941 | A[1] ^= t[1];
|
16942 |
|
16943 | R[2 * i] = B[2];
|
16944 | R[2 * i + 1] = B[3];
|
16945 | }
|
16946 | }
|
16947 | return pack(A, R);
|
16948 | }
|
16949 |
|
16950 |
|
16951 |
|
16952 |
|
16953 |
|
16954 |
|
16955 |
|
16956 |
|
16957 |
|
16958 | function unwrap(key, data) {
|
16959 | const aes = new cipher["aes" + (key.length * 8)](key);
|
16960 | const IV = new Uint32Array([0xA6A6A6A6, 0xA6A6A6A6]);
|
16961 | const C = unpack(data);
|
16962 | let A = C.subarray(0, 2);
|
16963 | const R = C.subarray(2);
|
16964 | const n = C.length / 2 - 1;
|
16965 | const t = new Uint32Array([0, 0]);
|
16966 | let B = new Uint32Array(4);
|
16967 | for (let j = 5; j >= 0; --j) {
|
16968 | for (let i = n - 1; i >= 0; --i) {
|
16969 | t[1] = n * j + (i + 1);
|
16970 |
|
16971 | B[0] = A[0] ^ t[0];
|
16972 | B[1] = A[1] ^ t[1];
|
16973 |
|
16974 | B[2] = R[2 * i];
|
16975 | B[3] = R[2 * i + 1];
|
16976 |
|
16977 | B = unpack(aes.decrypt(pack(B)));
|
16978 |
|
16979 | A = B.subarray(0, 2);
|
16980 |
|
16981 | R[2 * i] = B[2];
|
16982 | R[2 * i + 1] = B[3];
|
16983 | }
|
16984 | }
|
16985 | if (A[0] === IV[0] && A[1] === IV[1]) {
|
16986 | return pack(R);
|
16987 | }
|
16988 | throw new Error("Key Data Integrity failed");
|
16989 | }
|
16990 |
|
16991 | function createArrayBuffer(data) {
|
16992 | if (util.isString(data)) {
|
16993 | const { length } = data;
|
16994 | const buffer = new ArrayBuffer(length);
|
16995 | const view = new Uint8Array(buffer);
|
16996 | for (let j = 0; j < length; ++j) {
|
16997 | view[j] = data.charCodeAt(j);
|
16998 | }
|
16999 | return buffer;
|
17000 | }
|
17001 | return new Uint8Array(data).buffer;
|
17002 | }
|
17003 |
|
17004 | function unpack(data) {
|
17005 | const { length } = data;
|
17006 | const buffer = createArrayBuffer(data);
|
17007 | const view = new DataView(buffer);
|
17008 | const arr = new Uint32Array(length / 4);
|
17009 | for (let i = 0; i < length / 4; ++i) {
|
17010 | arr[i] = view.getUint32(4 * i);
|
17011 | }
|
17012 | return arr;
|
17013 | }
|
17014 |
|
17015 | function pack() {
|
17016 | let length = 0;
|
17017 | for (let k = 0; k < arguments.length; ++k) {
|
17018 | length += 4 * arguments[k].length;
|
17019 | }
|
17020 | const buffer = new ArrayBuffer(length);
|
17021 | const view = new DataView(buffer);
|
17022 | let offset = 0;
|
17023 | for (let i = 0; i < arguments.length; ++i) {
|
17024 | for (let j = 0; j < arguments[i].length; ++j) {
|
17025 | view.setUint32(offset + 4 * j, arguments[i][j]);
|
17026 | }
|
17027 | offset += 4 * arguments[i].length;
|
17028 | }
|
17029 | return new Uint8Array(buffer);
|
17030 | }
|
17031 |
|
17032 | var aes_kw = Object.freeze({
|
17033 | __proto__: null,
|
17034 | wrap: wrap,
|
17035 | unwrap: unwrap
|
17036 | });
|
17037 |
|
17038 |
|
17039 |
|
17040 |
|
17041 |
|
17042 |
|
17043 |
|
17044 |
|
17045 |
|
17046 |
|
17047 |
|
17048 |
|
17049 |
|
17050 |
|
17051 |
|
17052 | function encode$1(message) {
|
17053 | const c = 8 - (message.length % 8);
|
17054 | const padded = new Uint8Array(message.length + c).fill(c);
|
17055 | padded.set(message);
|
17056 | return padded;
|
17057 | }
|
17058 |
|
17059 |
|
17060 |
|
17061 |
|
17062 |
|
17063 |
|
17064 | function decode$1(message) {
|
17065 | const len = message.length;
|
17066 | if (len > 0) {
|
17067 | const c = message[len - 1];
|
17068 | if (c >= 1) {
|
17069 | const provided = message.subarray(len - c);
|
17070 | const computed = new Uint8Array(c).fill(c);
|
17071 | if (util.equalsUint8Array(provided, computed)) {
|
17072 | return message.subarray(0, len - c);
|
17073 | }
|
17074 | }
|
17075 | }
|
17076 | throw new Error('Invalid padding');
|
17077 | }
|
17078 |
|
17079 | var pkcs5 = Object.freeze({
|
17080 | __proto__: null,
|
17081 | encode: encode$1,
|
17082 | decode: decode$1
|
17083 | });
|
17084 |
|
17085 |
|
17086 |
|
17087 | const webCrypto$8 = util.getWebCrypto();
|
17088 | const nodeCrypto$9 = util.getNodeCrypto();
|
17089 |
|
17090 |
|
17091 |
|
17092 |
|
17093 |
|
17094 |
|
17095 |
|
17096 |
|
17097 |
|
17098 | async function validateParams$4(oid, Q, d) {
|
17099 | return validateStandardParams(enums.publicKey.ecdh, oid, Q, d);
|
17100 | }
|
17101 |
|
17102 |
|
17103 | function buildEcdhParam(public_algo, oid, kdfParams, fingerprint) {
|
17104 | return util.concatUint8Array([
|
17105 | oid.write(),
|
17106 | new Uint8Array([public_algo]),
|
17107 | kdfParams.write(),
|
17108 | util.strToUint8Array("Anonymous Sender "),
|
17109 | fingerprint.subarray(0, 20)
|
17110 | ]);
|
17111 | }
|
17112 |
|
17113 |
|
17114 | async function kdf(hash_algo, X, length, param, stripLeading = false, stripTrailing = false) {
|
17115 |
|
17116 |
|
17117 |
|
17118 | let i;
|
17119 | if (stripLeading) {
|
17120 |
|
17121 | for (i = 0; i < X.length && X[i] === 0; i++);
|
17122 | X = X.subarray(i);
|
17123 | }
|
17124 | if (stripTrailing) {
|
17125 |
|
17126 | for (i = X.length - 1; i >= 0 && X[i] === 0; i--);
|
17127 | X = X.subarray(0, i + 1);
|
17128 | }
|
17129 | const digest = await hash.digest(hash_algo, util.concatUint8Array([
|
17130 | new Uint8Array([0, 0, 0, 1]),
|
17131 | X,
|
17132 | param
|
17133 | ]));
|
17134 | return digest.subarray(0, length);
|
17135 | }
|
17136 |
|
17137 |
|
17138 |
|
17139 |
|
17140 |
|
17141 |
|
17142 |
|
17143 |
|
17144 |
|
17145 | async function genPublicEphemeralKey(curve, Q) {
|
17146 | switch (curve.type) {
|
17147 | case 'curve25519': {
|
17148 | const d = await getRandomBytes(32);
|
17149 | const { secretKey, sharedKey } = await genPrivateEphemeralKey(curve, Q, null, d);
|
17150 | let { publicKey } = naclFastLight.box.keyPair.fromSecretKey(secretKey);
|
17151 | publicKey = util.concatUint8Array([new Uint8Array([0x40]), publicKey]);
|
17152 | return { publicKey, sharedKey };
|
17153 | }
|
17154 | case 'web':
|
17155 | if (curve.web && util.getWebCrypto()) {
|
17156 | try {
|
17157 | return await webPublicEphemeralKey(curve, Q);
|
17158 | } catch (err) {
|
17159 | util.printDebugError(err);
|
17160 | }
|
17161 | }
|
17162 | break;
|
17163 | case 'node':
|
17164 | return nodePublicEphemeralKey(curve, Q);
|
17165 | }
|
17166 | return ellipticPublicEphemeralKey(curve, Q);
|
17167 | }
|
17168 |
|
17169 |
|
17170 |
|
17171 |
|
17172 |
|
17173 |
|
17174 |
|
17175 |
|
17176 |
|
17177 |
|
17178 |
|
17179 |
|
17180 | async function encrypt$3(oid, kdfParams, data, Q, fingerprint) {
|
17181 | const m = encode$1(data);
|
17182 |
|
17183 | const curve = new Curve(oid);
|
17184 | const { publicKey, sharedKey } = await genPublicEphemeralKey(curve, Q);
|
17185 | const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint);
|
17186 | const cipher_algo = enums.read(enums.symmetric, kdfParams.cipher);
|
17187 | const Z = await kdf(kdfParams.hash, sharedKey, cipher[cipher_algo].keySize, param);
|
17188 | const wrappedKey = wrap(Z, m);
|
17189 | return { publicKey, wrappedKey };
|
17190 | }
|
17191 |
|
17192 |
|
17193 |
|
17194 |
|
17195 |
|
17196 |
|
17197 |
|
17198 |
|
17199 |
|
17200 |
|
17201 |
|
17202 | async function genPrivateEphemeralKey(curve, V, Q, d) {
|
17203 | if (d.length !== curve.payloadSize) {
|
17204 | const privateKey = new Uint8Array(curve.payloadSize);
|
17205 | privateKey.set(d, curve.payloadSize - d.length);
|
17206 | d = privateKey;
|
17207 | }
|
17208 | switch (curve.type) {
|
17209 | case 'curve25519': {
|
17210 | const secretKey = d.slice().reverse();
|
17211 | const sharedKey = naclFastLight.scalarMult(secretKey, V.subarray(1));
|
17212 | return { secretKey, sharedKey };
|
17213 | }
|
17214 | case 'web':
|
17215 | if (curve.web && util.getWebCrypto()) {
|
17216 | try {
|
17217 | return await webPrivateEphemeralKey(curve, V, Q, d);
|
17218 | } catch (err) {
|
17219 | util.printDebugError(err);
|
17220 | }
|
17221 | }
|
17222 | break;
|
17223 | case 'node':
|
17224 | return nodePrivateEphemeralKey(curve, V, d);
|
17225 | }
|
17226 | return ellipticPrivateEphemeralKey(curve, V, d);
|
17227 | }
|
17228 |
|
17229 |
|
17230 |
|
17231 |
|
17232 |
|
17233 |
|
17234 |
|
17235 |
|
17236 |
|
17237 |
|
17238 |
|
17239 |
|
17240 |
|
17241 |
|
17242 | async function decrypt$3(oid, kdfParams, V, C, Q, d, fingerprint) {
|
17243 | const curve = new Curve(oid);
|
17244 | const { sharedKey } = await genPrivateEphemeralKey(curve, V, Q, d);
|
17245 | const param = buildEcdhParam(enums.publicKey.ecdh, oid, kdfParams, fingerprint);
|
17246 | const cipher_algo = enums.read(enums.symmetric, kdfParams.cipher);
|
17247 | let err;
|
17248 | for (let i = 0; i < 3; i++) {
|
17249 | try {
|
17250 |
|
17251 | const Z = await kdf(kdfParams.hash, sharedKey, cipher[cipher_algo].keySize, param, i === 1, i === 2);
|
17252 | return decode$1(unwrap(Z, C));
|
17253 | } catch (e) {
|
17254 | err = e;
|
17255 | }
|
17256 | }
|
17257 | throw err;
|
17258 | }
|
17259 |
|
17260 |
|
17261 |
|
17262 |
|
17263 |
|
17264 |
|
17265 |
|
17266 |
|
17267 |
|
17268 |
|
17269 |
|
17270 | async function webPrivateEphemeralKey(curve, V, Q, d) {
|
17271 | const recipient = privateToJwk$1(curve.payloadSize, curve.web.web, Q, d);
|
17272 | let privateKey = webCrypto$8.importKey(
|
17273 | "jwk",
|
17274 | recipient,
|
17275 | {
|
17276 | name: "ECDH",
|
17277 | namedCurve: curve.web.web
|
17278 | },
|
17279 | true,
|
17280 | ["deriveKey", "deriveBits"]
|
17281 | );
|
17282 | const jwk = rawPublicToJwk(curve.payloadSize, curve.web.web, V);
|
17283 | let sender = webCrypto$8.importKey(
|
17284 | "jwk",
|
17285 | jwk,
|
17286 | {
|
17287 | name: "ECDH",
|
17288 | namedCurve: curve.web.web
|
17289 | },
|
17290 | true,
|
17291 | []
|
17292 | );
|
17293 | [privateKey, sender] = await Promise.all([privateKey, sender]);
|
17294 | let S = webCrypto$8.deriveBits(
|
17295 | {
|
17296 | name: "ECDH",
|
17297 | namedCurve: curve.web.web,
|
17298 | public: sender
|
17299 | },
|
17300 | privateKey,
|
17301 | curve.web.sharedSize
|
17302 | );
|
17303 | let secret = webCrypto$8.exportKey(
|
17304 | "jwk",
|
17305 | privateKey
|
17306 | );
|
17307 | [S, secret] = await Promise.all([S, secret]);
|
17308 | const sharedKey = new Uint8Array(S);
|
17309 | const secretKey = b64ToUint8Array(secret.d);
|
17310 | return { secretKey, sharedKey };
|
17311 | }
|
17312 |
|
17313 |
|
17314 |
|
17315 |
|
17316 |
|
17317 |
|
17318 |
|
17319 |
|
17320 |
|
17321 | async function webPublicEphemeralKey(curve, Q) {
|
17322 | const jwk = rawPublicToJwk(curve.payloadSize, curve.web.web, Q);
|
17323 | let keyPair = webCrypto$8.generateKey(
|
17324 | {
|
17325 | name: "ECDH",
|
17326 | namedCurve: curve.web.web
|
17327 | },
|
17328 | true,
|
17329 | ["deriveKey", "deriveBits"]
|
17330 | );
|
17331 | let recipient = webCrypto$8.importKey(
|
17332 | "jwk",
|
17333 | jwk,
|
17334 | {
|
17335 | name: "ECDH",
|
17336 | namedCurve: curve.web.web
|
17337 | },
|
17338 | false,
|
17339 | []
|
17340 | );
|
17341 | [keyPair, recipient] = await Promise.all([keyPair, recipient]);
|
17342 | let s = webCrypto$8.deriveBits(
|
17343 | {
|
17344 | name: "ECDH",
|
17345 | namedCurve: curve.web.web,
|
17346 | public: recipient
|
17347 | },
|
17348 | keyPair.privateKey,
|
17349 | curve.web.sharedSize
|
17350 | );
|
17351 | let p = webCrypto$8.exportKey(
|
17352 | "jwk",
|
17353 | keyPair.publicKey
|
17354 | );
|
17355 | [s, p] = await Promise.all([s, p]);
|
17356 | const sharedKey = new Uint8Array(s);
|
17357 | const publicKey = new Uint8Array(jwkToRawPublic(p));
|
17358 | return { publicKey, sharedKey };
|
17359 | }
|
17360 |
|
17361 |
|
17362 |
|
17363 |
|
17364 |
|
17365 |
|
17366 |
|
17367 |
|
17368 |
|
17369 |
|
17370 | async function ellipticPrivateEphemeralKey(curve, V, d) {
|
17371 | const indutnyCurve = await getIndutnyCurve(curve.name);
|
17372 | V = keyFromPublic(indutnyCurve, V);
|
17373 | d = keyFromPrivate(indutnyCurve, d);
|
17374 | const secretKey = new Uint8Array(d.getPrivate());
|
17375 | const S = d.derive(V.getPublic());
|
17376 | const len = indutnyCurve.curve.p.byteLength();
|
17377 | const sharedKey = S.toArrayLike(Uint8Array, 'be', len);
|
17378 | return { secretKey, sharedKey };
|
17379 | }
|
17380 |
|
17381 |
|
17382 |
|
17383 |
|
17384 |
|
17385 |
|
17386 |
|
17387 |
|
17388 |
|
17389 | async function ellipticPublicEphemeralKey(curve, Q) {
|
17390 | const indutnyCurve = await getIndutnyCurve(curve.name);
|
17391 | const v = await curve.genKeyPair();
|
17392 | Q = keyFromPublic(indutnyCurve, Q);
|
17393 | const V = keyFromPrivate(indutnyCurve, v.privateKey);
|
17394 | const publicKey = v.publicKey;
|
17395 | const S = V.derive(Q.getPublic());
|
17396 | const len = indutnyCurve.curve.p.byteLength();
|
17397 | const sharedKey = S.toArrayLike(Uint8Array, 'be', len);
|
17398 | return { publicKey, sharedKey };
|
17399 | }
|
17400 |
|
17401 |
|
17402 |
|
17403 |
|
17404 |
|
17405 |
|
17406 |
|
17407 |
|
17408 |
|
17409 |
|
17410 | async function nodePrivateEphemeralKey(curve, V, d) {
|
17411 | const recipient = nodeCrypto$9.createECDH(curve.node.node);
|
17412 | recipient.setPrivateKey(d);
|
17413 | const sharedKey = new Uint8Array(recipient.computeSecret(V));
|
17414 | const secretKey = new Uint8Array(recipient.getPrivateKey());
|
17415 | return { secretKey, sharedKey };
|
17416 | }
|
17417 |
|
17418 |
|
17419 |
|
17420 |
|
17421 |
|
17422 |
|
17423 |
|
17424 |
|
17425 |
|
17426 | async function nodePublicEphemeralKey(curve, Q) {
|
17427 | const sender = nodeCrypto$9.createECDH(curve.node.node);
|
17428 | sender.generateKeys();
|
17429 | const sharedKey = new Uint8Array(sender.computeSecret(Q));
|
17430 | const publicKey = new Uint8Array(sender.getPublicKey());
|
17431 | return { publicKey, sharedKey };
|
17432 | }
|
17433 |
|
17434 | var ecdh = Object.freeze({
|
17435 | __proto__: null,
|
17436 | validateParams: validateParams$4,
|
17437 | encrypt: encrypt$3,
|
17438 | decrypt: decrypt$3
|
17439 | });
|
17440 |
|
17441 |
|
17442 |
|
17443 | var elliptic = Object.freeze({
|
17444 | __proto__: null,
|
17445 | Curve: Curve,
|
17446 | ecdh: ecdh,
|
17447 | ecdsa: ecdsa,
|
17448 | eddsa: eddsa,
|
17449 | generate: generate$1,
|
17450 | getPreferredHashAlgo: getPreferredHashAlgo
|
17451 | });
|
17452 |
|
17453 |
|
17454 |
|
17455 |
|
17456 |
|
17457 |
|
17458 |
|
17459 |
|
17460 |
|
17461 |
|
17462 |
|
17463 |
|
17464 |
|
17465 |
|
17466 |
|
17467 |
|
17468 |
|
17469 |
|
17470 |
|
17471 |
|
17472 | async function sign$3(hash_algo, hashed, g, p, q, x) {
|
17473 | const BigInteger = await util.getBigInteger();
|
17474 | const one = new BigInteger(1);
|
17475 | p = new BigInteger(p);
|
17476 | q = new BigInteger(q);
|
17477 | g = new BigInteger(g);
|
17478 | x = new BigInteger(x);
|
17479 |
|
17480 | let k;
|
17481 | let r;
|
17482 | let s;
|
17483 | let t;
|
17484 | g = g.mod(p);
|
17485 | x = x.mod(q);
|
17486 |
|
17487 |
|
17488 |
|
17489 |
|
17490 |
|
17491 | const h = new BigInteger(hashed.subarray(0, q.byteLength())).mod(q);
|
17492 |
|
17493 |
|
17494 |
|
17495 |
|
17496 |
|
17497 | while (true) {
|
17498 |
|
17499 | k = await getRandomBigInteger(one, q);
|
17500 | r = g.modExp(k, p).imod(q);
|
17501 | if (r.isZero()) {
|
17502 | continue;
|
17503 | }
|
17504 | const xr = x.mul(r).imod(q);
|
17505 | t = h.add(xr).imod(q);
|
17506 | s = k.modInv(q).imul(t).imod(q);
|
17507 | if (s.isZero()) {
|
17508 | continue;
|
17509 | }
|
17510 | break;
|
17511 | }
|
17512 | return {
|
17513 | r: r.toUint8Array('be', q.byteLength()),
|
17514 | s: s.toUint8Array('be', q.byteLength())
|
17515 | };
|
17516 | }
|
17517 |
|
17518 |
|
17519 |
|
17520 |
|
17521 |
|
17522 |
|
17523 |
|
17524 |
|
17525 |
|
17526 |
|
17527 |
|
17528 |
|
17529 |
|
17530 |
|
17531 | async function verify$3(hash_algo, r, s, hashed, g, p, q, y) {
|
17532 | const BigInteger = await util.getBigInteger();
|
17533 | const zero = new BigInteger(0);
|
17534 | r = new BigInteger(r);
|
17535 | s = new BigInteger(s);
|
17536 |
|
17537 | p = new BigInteger(p);
|
17538 | q = new BigInteger(q);
|
17539 | g = new BigInteger(g);
|
17540 | y = new BigInteger(y);
|
17541 |
|
17542 | if (r.lte(zero) || r.gte(q) ||
|
17543 | s.lte(zero) || s.gte(q)) {
|
17544 | util.printDebug("invalid DSA Signature");
|
17545 | return false;
|
17546 | }
|
17547 | const h = new BigInteger(hashed.subarray(0, q.byteLength())).imod(q);
|
17548 | const w = s.modInv(q);
|
17549 | if (w.isZero()) {
|
17550 | util.printDebug("invalid DSA Signature");
|
17551 | return false;
|
17552 | }
|
17553 |
|
17554 | g = g.mod(p);
|
17555 | y = y.mod(p);
|
17556 | const u1 = h.mul(w).imod(q);
|
17557 | const u2 = r.mul(w).imod(q);
|
17558 | const t1 = g.modExp(u1, p);
|
17559 | const t2 = y.modExp(u2, p);
|
17560 | const v = t1.mul(t2).imod(p).imod(q);
|
17561 | return v.equal(r);
|
17562 | }
|
17563 |
|
17564 |
|
17565 |
|
17566 |
|
17567 |
|
17568 |
|
17569 |
|
17570 |
|
17571 |
|
17572 |
|
17573 |
|
17574 | async function validateParams$5(p, q, g, y, x) {
|
17575 | const BigInteger = await util.getBigInteger();
|
17576 | p = new BigInteger(p);
|
17577 | q = new BigInteger(q);
|
17578 | g = new BigInteger(g);
|
17579 | y = new BigInteger(y);
|
17580 | const one = new BigInteger(1);
|
17581 |
|
17582 | if (g.lte(one) || g.gte(p)) {
|
17583 | return false;
|
17584 | }
|
17585 |
|
17586 | |
17587 |
|
17588 |
|
17589 | if (!p.dec().mod(q).isZero()) {
|
17590 | return false;
|
17591 | }
|
17592 |
|
17593 | |
17594 |
|
17595 |
|
17596 |
|
17597 | if (!g.modExp(q, p).isOne()) {
|
17598 | return false;
|
17599 | }
|
17600 |
|
17601 | |
17602 |
|
17603 |
|
17604 | const qSize = new BigInteger(q.bitLength());
|
17605 | const n150 = new BigInteger(150);
|
17606 | if (qSize.lt(n150) || !(await isProbablePrime(q, null, 32))) {
|
17607 | return false;
|
17608 | }
|
17609 |
|
17610 | |
17611 |
|
17612 |
|
17613 |
|
17614 |
|
17615 |
|
17616 | x = new BigInteger(x);
|
17617 | const two = new BigInteger(2);
|
17618 | const r = await getRandomBigInteger(two.leftShift(qSize.dec()), two.leftShift(qSize));
|
17619 | const rqx = q.mul(r).add(x);
|
17620 | if (!y.equal(g.modExp(rqx, p))) {
|
17621 | return false;
|
17622 | }
|
17623 |
|
17624 | return true;
|
17625 | }
|
17626 |
|
17627 | var dsa = Object.freeze({
|
17628 | __proto__: null,
|
17629 | sign: sign$3,
|
17630 | verify: verify$3,
|
17631 | validateParams: validateParams$5
|
17632 | });
|
17633 |
|
17634 |
|
17635 |
|
17636 |
|
17637 |
|
17638 |
|
17639 |
|
17640 | var publicKey = {
|
17641 |
|
17642 | rsa: rsa,
|
17643 |
|
17644 | elgamal: elgamal,
|
17645 |
|
17646 | elliptic: elliptic,
|
17647 |
|
17648 | dsa: dsa,
|
17649 |
|
17650 | nacl: naclFastLight
|
17651 | };
|
17652 |
|
17653 |
|
17654 |
|
17655 |
|
17656 |
|
17657 |
|
17658 |
|
17659 |
|
17660 |
|
17661 |
|
17662 |
|
17663 |
|
17664 |
|
17665 |
|
17666 |
|
17667 |
|
17668 |
|
17669 |
|
17670 | function parseSignatureParams(algo, signature) {
|
17671 | let read = 0;
|
17672 | switch (algo) {
|
17673 |
|
17674 |
|
17675 | case enums.publicKey.rsaEncryptSign:
|
17676 | case enums.publicKey.rsaEncrypt:
|
17677 | case enums.publicKey.rsaSign: {
|
17678 | const s = util.readMPI(signature.subarray(read));
|
17679 |
|
17680 |
|
17681 | return { s };
|
17682 | }
|
17683 |
|
17684 |
|
17685 |
|
17686 | case enums.publicKey.dsa:
|
17687 | case enums.publicKey.ecdsa:
|
17688 | {
|
17689 | const r = util.readMPI(signature.subarray(read)); read += r.length + 2;
|
17690 | const s = util.readMPI(signature.subarray(read));
|
17691 | return { r, s };
|
17692 | }
|
17693 |
|
17694 |
|
17695 |
|
17696 | case enums.publicKey.eddsa: {
|
17697 |
|
17698 |
|
17699 | let r = util.readMPI(signature.subarray(read)); read += r.length + 2;
|
17700 | r = util.leftPad(r, 32);
|
17701 | let s = util.readMPI(signature.subarray(read));
|
17702 | s = util.leftPad(s, 32);
|
17703 | return { r, s };
|
17704 | }
|
17705 | default:
|
17706 | throw new Error('Invalid signature algorithm.');
|
17707 | }
|
17708 | }
|
17709 |
|
17710 |
|
17711 |
|
17712 |
|
17713 |
|
17714 |
|
17715 |
|
17716 |
|
17717 |
|
17718 |
|
17719 |
|
17720 |
|
17721 |
|
17722 |
|
17723 |
|
17724 | async function verify$4(algo, hashAlgo, signature, publicParams, data, hashed) {
|
17725 | switch (algo) {
|
17726 | case enums.publicKey.rsaEncryptSign:
|
17727 | case enums.publicKey.rsaEncrypt:
|
17728 | case enums.publicKey.rsaSign: {
|
17729 | const { n, e } = publicParams;
|
17730 | const s = util.leftPad(signature.s, n.length);
|
17731 | return publicKey.rsa.verify(hashAlgo, data, s, n, e, hashed);
|
17732 | }
|
17733 | case enums.publicKey.dsa: {
|
17734 | const { g, p, q, y } = publicParams;
|
17735 | const { r, s } = signature;
|
17736 | return publicKey.dsa.verify(hashAlgo, r, s, hashed, g, p, q, y);
|
17737 | }
|
17738 | case enums.publicKey.ecdsa: {
|
17739 | const { oid, Q } = publicParams;
|
17740 | const curveSize = new publicKey.elliptic.Curve(oid).payloadSize;
|
17741 |
|
17742 | const r = util.leftPad(signature.r, curveSize);
|
17743 | const s = util.leftPad(signature.s, curveSize);
|
17744 | return publicKey.elliptic.ecdsa.verify(oid, hashAlgo, { r, s }, data, Q, hashed);
|
17745 | }
|
17746 | case enums.publicKey.eddsa: {
|
17747 | const { oid, Q } = publicParams;
|
17748 |
|
17749 | return publicKey.elliptic.eddsa.verify(oid, hashAlgo, signature, data, Q, hashed);
|
17750 | }
|
17751 | default:
|
17752 | throw new Error('Invalid signature algorithm.');
|
17753 | }
|
17754 | }
|
17755 |
|
17756 |
|
17757 |
|
17758 |
|
17759 |
|
17760 |
|
17761 |
|
17762 |
|
17763 |
|
17764 |
|
17765 |
|
17766 |
|
17767 |
|
17768 |
|
17769 |
|
17770 | async function sign$4(algo, hashAlgo, publicKeyParams, privateKeyParams, data, hashed) {
|
17771 | if (!publicKeyParams || !privateKeyParams) {
|
17772 | throw new Error('Missing key parameters');
|
17773 | }
|
17774 | switch (algo) {
|
17775 | case enums.publicKey.rsaEncryptSign:
|
17776 | case enums.publicKey.rsaEncrypt:
|
17777 | case enums.publicKey.rsaSign: {
|
17778 | const { n, e } = publicKeyParams;
|
17779 | const { d, p, q, u } = privateKeyParams;
|
17780 | const s = await publicKey.rsa.sign(hashAlgo, data, n, e, d, p, q, u, hashed);
|
17781 | return { s };
|
17782 | }
|
17783 | case enums.publicKey.dsa: {
|
17784 | const { g, p, q } = publicKeyParams;
|
17785 | const { x } = privateKeyParams;
|
17786 | return publicKey.dsa.sign(hashAlgo, hashed, g, p, q, x);
|
17787 | }
|
17788 | case enums.publicKey.elgamal: {
|
17789 | throw new Error('Signing with Elgamal is not defined in the OpenPGP standard.');
|
17790 | }
|
17791 | case enums.publicKey.ecdsa: {
|
17792 | const { oid, Q } = publicKeyParams;
|
17793 | const { d } = privateKeyParams;
|
17794 | return publicKey.elliptic.ecdsa.sign(oid, hashAlgo, data, Q, d, hashed);
|
17795 | }
|
17796 | case enums.publicKey.eddsa: {
|
17797 | const { oid, Q } = publicKeyParams;
|
17798 | const { seed } = privateKeyParams;
|
17799 | return publicKey.elliptic.eddsa.sign(oid, hashAlgo, data, Q, seed, hashed);
|
17800 | }
|
17801 | default:
|
17802 | throw new Error('Invalid signature algorithm.');
|
17803 | }
|
17804 | }
|
17805 |
|
17806 | var signature = Object.freeze({
|
17807 | __proto__: null,
|
17808 | parseSignatureParams: parseSignatureParams,
|
17809 | verify: verify$4,
|
17810 | sign: sign$4
|
17811 | });
|
17812 |
|
17813 |
|
17814 |
|
17815 | class ECDHSymmetricKey {
|
17816 | constructor(data) {
|
17817 | if (typeof data === 'undefined') {
|
17818 | data = new Uint8Array([]);
|
17819 | } else if (util.isString(data)) {
|
17820 | data = util.strToUint8Array(data);
|
17821 | } else {
|
17822 | data = new Uint8Array(data);
|
17823 | }
|
17824 | this.data = data;
|
17825 | }
|
17826 |
|
17827 | |
17828 |
|
17829 |
|
17830 |
|
17831 |
|
17832 | read(input) {
|
17833 | if (input.length >= 1) {
|
17834 | const length = input[0];
|
17835 | if (input.length >= 1 + length) {
|
17836 | this.data = input.subarray(1, 1 + length);
|
17837 | return 1 + this.data.length;
|
17838 | }
|
17839 | }
|
17840 | throw new Error('Invalid symmetric key');
|
17841 | }
|
17842 |
|
17843 | |
17844 |
|
17845 |
|
17846 |
|
17847 | write() {
|
17848 | return util.concatUint8Array([new Uint8Array([this.data.length]), this.data]);
|
17849 | }
|
17850 | }
|
17851 |
|
17852 |
|
17853 |
|
17854 |
|
17855 |
|
17856 |
|
17857 |
|
17858 |
|
17859 |
|
17860 |
|
17861 |
|
17862 |
|
17863 |
|
17864 |
|
17865 |
|
17866 |
|
17867 |
|
17868 |
|
17869 |
|
17870 |
|
17871 |
|
17872 |
|
17873 |
|
17874 |
|
17875 |
|
17876 |
|
17877 |
|
17878 |
|
17879 |
|
17880 |
|
17881 | class KDFParams {
|
17882 | |
17883 |
|
17884 |
|
17885 |
|
17886 | constructor(data) {
|
17887 | if (data) {
|
17888 | const { hash, cipher } = data;
|
17889 | this.hash = hash;
|
17890 | this.cipher = cipher;
|
17891 | } else {
|
17892 | this.hash = null;
|
17893 | this.cipher = null;
|
17894 | }
|
17895 | }
|
17896 |
|
17897 | |
17898 |
|
17899 |
|
17900 |
|
17901 |
|
17902 | read(input) {
|
17903 | if (input.length < 4 || input[0] !== 3 || input[1] !== 1) {
|
17904 | throw new Error('Cannot read KDFParams');
|
17905 | }
|
17906 | this.hash = input[2];
|
17907 | this.cipher = input[3];
|
17908 | return 4;
|
17909 | }
|
17910 |
|
17911 | |
17912 |
|
17913 |
|
17914 |
|
17915 | write() {
|
17916 | return new Uint8Array([3, 1, this.hash, this.cipher]);
|
17917 | }
|
17918 | }
|
17919 |
|
17920 |
|
17921 |
|
17922 |
|
17923 |
|
17924 |
|
17925 |
|
17926 |
|
17927 |
|
17928 |
|
17929 |
|
17930 |
|
17931 |
|
17932 | async function publicKeyEncrypt(algo, publicParams, data, fingerprint) {
|
17933 | switch (algo) {
|
17934 | case enums.publicKey.rsaEncrypt:
|
17935 | case enums.publicKey.rsaEncryptSign: {
|
17936 | const { n, e } = publicParams;
|
17937 | const c = await publicKey.rsa.encrypt(data, n, e);
|
17938 | return { c };
|
17939 | }
|
17940 | case enums.publicKey.elgamal: {
|
17941 | const { p, g, y } = publicParams;
|
17942 | return publicKey.elgamal.encrypt(data, p, g, y);
|
17943 | }
|
17944 | case enums.publicKey.ecdh: {
|
17945 | const { oid, Q, kdfParams } = publicParams;
|
17946 | const { publicKey: V, wrappedKey: C } = await publicKey.elliptic.ecdh.encrypt(
|
17947 | oid, kdfParams, data, Q, fingerprint);
|
17948 | return { V, C: new ECDHSymmetricKey(C) };
|
17949 | }
|
17950 | default:
|
17951 | return [];
|
17952 | }
|
17953 | }
|
17954 |
|
17955 |
|
17956 |
|
17957 |
|
17958 |
|
17959 |
|
17960 |
|
17961 |
|
17962 |
|
17963 |
|
17964 |
|
17965 |
|
17966 | async function publicKeyDecrypt(algo, publicKeyParams, privateKeyParams, sessionKeyParams, fingerprint) {
|
17967 | switch (algo) {
|
17968 | case enums.publicKey.rsaEncryptSign:
|
17969 | case enums.publicKey.rsaEncrypt: {
|
17970 | const { c } = sessionKeyParams;
|
17971 | const { n, e } = publicKeyParams;
|
17972 | const { d, p, q, u } = privateKeyParams;
|
17973 | return publicKey.rsa.decrypt(c, n, e, d, p, q, u);
|
17974 | }
|
17975 | case enums.publicKey.elgamal: {
|
17976 | const { c1, c2 } = sessionKeyParams;
|
17977 | const p = publicKeyParams.p;
|
17978 | const x = privateKeyParams.x;
|
17979 | return publicKey.elgamal.decrypt(c1, c2, p, x);
|
17980 | }
|
17981 | case enums.publicKey.ecdh: {
|
17982 | const { oid, Q, kdfParams } = publicKeyParams;
|
17983 | const { d } = privateKeyParams;
|
17984 | const { V, C } = sessionKeyParams;
|
17985 | return publicKey.elliptic.ecdh.decrypt(
|
17986 | oid, kdfParams, V, C.data, Q, d, fingerprint);
|
17987 | }
|
17988 | default:
|
17989 | throw new Error('Invalid public key encryption algorithm.');
|
17990 | }
|
17991 | }
|
17992 |
|
17993 |
|
17994 |
|
17995 |
|
17996 |
|
17997 |
|
17998 |
|
17999 | function parsePublicKeyParams(algo, bytes) {
|
18000 | let read = 0;
|
18001 | switch (algo) {
|
18002 | case enums.publicKey.rsaEncrypt:
|
18003 | case enums.publicKey.rsaEncryptSign:
|
18004 | case enums.publicKey.rsaSign: {
|
18005 | const n = util.readMPI(bytes.subarray(read)); read += n.length + 2;
|
18006 | const e = util.readMPI(bytes.subarray(read)); read += e.length + 2;
|
18007 | return { read, publicParams: { n, e } };
|
18008 | }
|
18009 | case enums.publicKey.dsa: {
|
18010 | const p = util.readMPI(bytes.subarray(read)); read += p.length + 2;
|
18011 | const q = util.readMPI(bytes.subarray(read)); read += q.length + 2;
|
18012 | const g = util.readMPI(bytes.subarray(read)); read += g.length + 2;
|
18013 | const y = util.readMPI(bytes.subarray(read)); read += y.length + 2;
|
18014 | return { read, publicParams: { p, q, g, y } };
|
18015 | }
|
18016 | case enums.publicKey.elgamal: {
|
18017 | const p = util.readMPI(bytes.subarray(read)); read += p.length + 2;
|
18018 | const g = util.readMPI(bytes.subarray(read)); read += g.length + 2;
|
18019 | const y = util.readMPI(bytes.subarray(read)); read += y.length + 2;
|
18020 | return { read, publicParams: { p, g, y } };
|
18021 | }
|
18022 | case enums.publicKey.ecdsa: {
|
18023 | const oid = new OID(); read += oid.read(bytes);
|
18024 | const Q = util.readMPI(bytes.subarray(read)); read += Q.length + 2;
|
18025 | return { read: read, publicParams: { oid, Q } };
|
18026 | }
|
18027 | case enums.publicKey.eddsa: {
|
18028 | const oid = new OID(); read += oid.read(bytes);
|
18029 | let Q = util.readMPI(bytes.subarray(read)); read += Q.length + 2;
|
18030 | Q = util.leftPad(Q, 33);
|
18031 | return { read: read, publicParams: { oid, Q } };
|
18032 | }
|
18033 | case enums.publicKey.ecdh: {
|
18034 | const oid = new OID(); read += oid.read(bytes);
|
18035 | const Q = util.readMPI(bytes.subarray(read)); read += Q.length + 2;
|
18036 | const kdfParams = new KDFParams(); read += kdfParams.read(bytes.subarray(read));
|
18037 | return { read: read, publicParams: { oid, Q, kdfParams } };
|
18038 | }
|
18039 | default:
|
18040 | throw new Error('Invalid public key encryption algorithm.');
|
18041 | }
|
18042 | }
|
18043 |
|
18044 |
|
18045 |
|
18046 |
|
18047 |
|
18048 |
|
18049 |
|
18050 |
|
18051 | function parsePrivateKeyParams(algo, bytes, publicParams) {
|
18052 | let read = 0;
|
18053 | switch (algo) {
|
18054 | case enums.publicKey.rsaEncrypt:
|
18055 | case enums.publicKey.rsaEncryptSign:
|
18056 | case enums.publicKey.rsaSign: {
|
18057 | const d = util.readMPI(bytes.subarray(read)); read += d.length + 2;
|
18058 | const p = util.readMPI(bytes.subarray(read)); read += p.length + 2;
|
18059 | const q = util.readMPI(bytes.subarray(read)); read += q.length + 2;
|
18060 | const u = util.readMPI(bytes.subarray(read)); read += u.length + 2;
|
18061 | return { read, privateParams: { d, p, q, u } };
|
18062 | }
|
18063 | case enums.publicKey.dsa:
|
18064 | case enums.publicKey.elgamal: {
|
18065 | const x = util.readMPI(bytes.subarray(read)); read += x.length + 2;
|
18066 | return { read, privateParams: { x } };
|
18067 | }
|
18068 | case enums.publicKey.ecdsa:
|
18069 | case enums.publicKey.ecdh: {
|
18070 | const curve = new Curve(publicParams.oid);
|
18071 | let d = util.readMPI(bytes.subarray(read)); read += d.length + 2;
|
18072 | d = util.leftPad(d, curve.payloadSize);
|
18073 | return { read, privateParams: { d } };
|
18074 | }
|
18075 | case enums.publicKey.eddsa: {
|
18076 | let seed = util.readMPI(bytes.subarray(read)); read += seed.length + 2;
|
18077 | seed = util.leftPad(seed, 32);
|
18078 | return { read, privateParams: { seed } };
|
18079 | }
|
18080 | default:
|
18081 | throw new Error('Invalid public key encryption algorithm.');
|
18082 | }
|
18083 | }
|
18084 |
|
18085 |
|
18086 |
|
18087 |
|
18088 |
|
18089 |
|
18090 | function parseEncSessionKeyParams(algo, bytes) {
|
18091 | let read = 0;
|
18092 | switch (algo) {
|
18093 |
|
18094 |
|
18095 | case enums.publicKey.rsaEncrypt:
|
18096 | case enums.publicKey.rsaEncryptSign: {
|
18097 | const c = util.readMPI(bytes.subarray(read));
|
18098 | return { c };
|
18099 | }
|
18100 |
|
18101 |
|
18102 |
|
18103 |
|
18104 | case enums.publicKey.elgamal: {
|
18105 | const c1 = util.readMPI(bytes.subarray(read)); read += c1.length + 2;
|
18106 | const c2 = util.readMPI(bytes.subarray(read));
|
18107 | return { c1, c2 };
|
18108 | }
|
18109 |
|
18110 |
|
18111 |
|
18112 | case enums.publicKey.ecdh: {
|
18113 | const V = util.readMPI(bytes.subarray(read)); read += V.length + 2;
|
18114 | const C = new ECDHSymmetricKey(); C.read(bytes.subarray(read));
|
18115 | return { V, C };
|
18116 | }
|
18117 | default:
|
18118 | throw new Error('Invalid public key encryption algorithm.');
|
18119 | }
|
18120 | }
|
18121 |
|
18122 |
|
18123 |
|
18124 |
|
18125 |
|
18126 |
|
18127 |
|
18128 | function serializeParams(algo, params) {
|
18129 | const orderedParams = Object.keys(params).map(name => {
|
18130 | const param = params[name];
|
18131 | return util.isUint8Array(param) ? util.uint8ArrayToMpi(param) : param.write();
|
18132 | });
|
18133 | return util.concatUint8Array(orderedParams);
|
18134 | }
|
18135 |
|
18136 |
|
18137 |
|
18138 |
|
18139 |
|
18140 |
|
18141 |
|
18142 |
|
18143 |
|
18144 | function generateParams(algo, bits, oid) {
|
18145 | switch (algo) {
|
18146 | case enums.publicKey.rsaEncrypt:
|
18147 | case enums.publicKey.rsaEncryptSign:
|
18148 | case enums.publicKey.rsaSign: {
|
18149 | return publicKey.rsa.generate(bits, 65537).then(({ n, e, d, p, q, u }) => ({
|
18150 | privateParams: { d, p, q, u },
|
18151 | publicParams: { n, e }
|
18152 | }));
|
18153 | }
|
18154 | case enums.publicKey.ecdsa:
|
18155 | return publicKey.elliptic.generate(oid).then(({ oid, Q, secret }) => ({
|
18156 | privateParams: { d: secret },
|
18157 | publicParams: { oid: new OID(oid), Q }
|
18158 | }));
|
18159 | case enums.publicKey.eddsa:
|
18160 | return publicKey.elliptic.generate(oid).then(({ oid, Q, secret }) => ({
|
18161 | privateParams: { seed: secret },
|
18162 | publicParams: { oid: new OID(oid), Q }
|
18163 | }));
|
18164 | case enums.publicKey.ecdh:
|
18165 | return publicKey.elliptic.generate(oid).then(({ oid, Q, secret, hash, cipher }) => ({
|
18166 | privateParams: { d: secret },
|
18167 | publicParams: {
|
18168 | oid: new OID(oid),
|
18169 | Q,
|
18170 | kdfParams: new KDFParams({ hash, cipher })
|
18171 | }
|
18172 | }));
|
18173 | case enums.publicKey.dsa:
|
18174 | case enums.publicKey.elgamal:
|
18175 | throw new Error('Unsupported algorithm for key generation.');
|
18176 | default:
|
18177 | throw new Error('Invalid public key algorithm.');
|
18178 | }
|
18179 | }
|
18180 |
|
18181 |
|
18182 |
|
18183 |
|
18184 |
|
18185 |
|
18186 |
|
18187 |
|
18188 |
|
18189 | async function validateParams$6(algo, publicParams, privateParams) {
|
18190 | if (!publicParams || !privateParams) {
|
18191 | throw new Error('Missing key parameters');
|
18192 | }
|
18193 | switch (algo) {
|
18194 | case enums.publicKey.rsaEncrypt:
|
18195 | case enums.publicKey.rsaEncryptSign:
|
18196 | case enums.publicKey.rsaSign: {
|
18197 | const { n, e } = publicParams;
|
18198 | const { d, p, q, u } = privateParams;
|
18199 | return publicKey.rsa.validateParams(n, e, d, p, q, u);
|
18200 | }
|
18201 | case enums.publicKey.dsa: {
|
18202 | const { p, q, g, y } = publicParams;
|
18203 | const { x } = privateParams;
|
18204 | return publicKey.dsa.validateParams(p, q, g, y, x);
|
18205 | }
|
18206 | case enums.publicKey.elgamal: {
|
18207 | const { p, g, y } = publicParams;
|
18208 | const { x } = privateParams;
|
18209 | return publicKey.elgamal.validateParams(p, g, y, x);
|
18210 | }
|
18211 | case enums.publicKey.ecdsa:
|
18212 | case enums.publicKey.ecdh: {
|
18213 | const algoModule = publicKey.elliptic[enums.read(enums.publicKey, algo)];
|
18214 | const { oid, Q } = publicParams;
|
18215 | const { d } = privateParams;
|
18216 | return algoModule.validateParams(oid, Q, d);
|
18217 | }
|
18218 | case enums.publicKey.eddsa: {
|
18219 | const { oid, Q } = publicParams;
|
18220 | const { seed } = privateParams;
|
18221 | return publicKey.elliptic.eddsa.validateParams(oid, Q, seed);
|
18222 | }
|
18223 | default:
|
18224 | throw new Error('Invalid public key algorithm.');
|
18225 | }
|
18226 | }
|
18227 |
|
18228 |
|
18229 |
|
18230 |
|
18231 |
|
18232 |
|
18233 |
|
18234 |
|
18235 | async function getPrefixRandom(algo) {
|
18236 | const prefixrandom = await getRandomBytes(cipher[algo].blockSize);
|
18237 | const repeat = new Uint8Array([prefixrandom[prefixrandom.length - 2], prefixrandom[prefixrandom.length - 1]]);
|
18238 | return util.concat([prefixrandom, repeat]);
|
18239 | }
|
18240 |
|
18241 |
|
18242 |
|
18243 |
|
18244 |
|
18245 |
|
18246 |
|
18247 |
|
18248 | function generateSessionKey(algo) {
|
18249 | return getRandomBytes(cipher[algo].keySize);
|
18250 | }
|
18251 |
|
18252 | var crypto$1 = Object.freeze({
|
18253 | __proto__: null,
|
18254 | publicKeyEncrypt: publicKeyEncrypt,
|
18255 | publicKeyDecrypt: publicKeyDecrypt,
|
18256 | parsePublicKeyParams: parsePublicKeyParams,
|
18257 | parsePrivateKeyParams: parsePrivateKeyParams,
|
18258 | parseEncSessionKeyParams: parseEncSessionKeyParams,
|
18259 | serializeParams: serializeParams,
|
18260 | generateParams: generateParams,
|
18261 | validateParams: validateParams$6,
|
18262 | getPrefixRandom: getPrefixRandom,
|
18263 | generateSessionKey: generateSessionKey
|
18264 | });
|
18265 |
|
18266 |
|
18267 |
|
18268 |
|
18269 |
|
18270 |
|
18271 |
|
18272 |
|
18273 |
|
18274 |
|
18275 |
|
18276 |
|
18277 |
|
18278 |
|
18279 | const mod = {
|
18280 |
|
18281 | cipher: cipher,
|
18282 |
|
18283 | hash: hash,
|
18284 |
|
18285 | cfb: cfb,
|
18286 |
|
18287 | gcm: GCM,
|
18288 | experimentalGcm: GCM,
|
18289 |
|
18290 | eax: EAX,
|
18291 |
|
18292 | ocb: OCB,
|
18293 |
|
18294 | publicKey: publicKey,
|
18295 |
|
18296 | signature: signature,
|
18297 |
|
18298 | random: random,
|
18299 |
|
18300 | pkcs1: pkcs1,
|
18301 |
|
18302 | pkcs5: pkcs5,
|
18303 |
|
18304 | aes_kw: aes_kw
|
18305 | };
|
18306 |
|
18307 | Object.assign(mod, crypto$1);
|
18308 |
|
18309 | var TYPED_OK = typeof Uint8Array !== "undefined" &&
|
18310 | typeof Uint16Array !== "undefined" &&
|
18311 | typeof Int32Array !== "undefined";
|
18312 |
|
18313 |
|
18314 |
|
18315 | function shrinkBuf(buf, size) {
|
18316 | if (buf.length === size) {
|
18317 | return buf;
|
18318 | }
|
18319 | if (buf.subarray) {
|
18320 | return buf.subarray(0, size);
|
18321 | }
|
18322 | buf.length = size;
|
18323 | return buf;
|
18324 | }
|
18325 |
|
18326 |
|
18327 | const fnTyped = {
|
18328 | arraySet: function (dest, src, src_offs, len, dest_offs) {
|
18329 | if (src.subarray && dest.subarray) {
|
18330 | dest.set(src.subarray(src_offs, src_offs + len), dest_offs);
|
18331 | return;
|
18332 | }
|
18333 |
|
18334 | for (let i = 0; i < len; i++) {
|
18335 | dest[dest_offs + i] = src[src_offs + i];
|
18336 | }
|
18337 | },
|
18338 |
|
18339 | flattenChunks: function (chunks) {
|
18340 | let i, l, len, pos, chunk;
|
18341 |
|
18342 |
|
18343 | len = 0;
|
18344 | for (i = 0, l = chunks.length; i < l; i++) {
|
18345 | len += chunks[i].length;
|
18346 | }
|
18347 |
|
18348 |
|
18349 | const result = new Uint8Array(len);
|
18350 | pos = 0;
|
18351 | for (i = 0, l = chunks.length; i < l; i++) {
|
18352 | chunk = chunks[i];
|
18353 | result.set(chunk, pos);
|
18354 | pos += chunk.length;
|
18355 | }
|
18356 |
|
18357 | return result;
|
18358 | }
|
18359 | };
|
18360 |
|
18361 | const fnUntyped = {
|
18362 | arraySet: function (dest, src, src_offs, len, dest_offs) {
|
18363 | for (let i = 0; i < len; i++) {
|
18364 | dest[dest_offs + i] = src[src_offs + i];
|
18365 | }
|
18366 | },
|
18367 |
|
18368 | flattenChunks: function (chunks) {
|
18369 | return [].concat.apply([], chunks);
|
18370 | }
|
18371 | };
|
18372 |
|
18373 |
|
18374 |
|
18375 |
|
18376 |
|
18377 | let Buf8 = TYPED_OK ? Uint8Array : Array;
|
18378 | let Buf16 = TYPED_OK ? Uint16Array : Array;
|
18379 | let Buf32 = TYPED_OK ? Int32Array : Array;
|
18380 | let flattenChunks = TYPED_OK ? fnTyped.flattenChunks : fnUntyped.flattenChunks;
|
18381 | let arraySet = TYPED_OK ? fnTyped.arraySet : fnUntyped.arraySet;
|
18382 |
|
18383 |
|
18384 |
|
18385 |
|
18386 |
|
18387 |
|
18388 |
|
18389 |
|
18390 |
|
18391 |
|
18392 |
|
18393 |
|
18394 |
|
18395 |
|
18396 |
|
18397 |
|
18398 |
|
18399 |
|
18400 |
|
18401 |
|
18402 |
|
18403 | const Z_NO_FLUSH = 0;
|
18404 | const Z_PARTIAL_FLUSH = 1;
|
18405 | const Z_SYNC_FLUSH = 2;
|
18406 | const Z_FULL_FLUSH = 3;
|
18407 | const Z_FINISH = 4;
|
18408 | const Z_BLOCK = 5;
|
18409 | const Z_TREES = 6;
|
18410 |
|
18411 |
|
18412 |
|
18413 |
|
18414 | const Z_OK = 0;
|
18415 | const Z_STREAM_END = 1;
|
18416 | const Z_NEED_DICT = 2;
|
18417 | const Z_STREAM_ERROR = -2;
|
18418 | const Z_DATA_ERROR = -3;
|
18419 |
|
18420 | const Z_BUF_ERROR = -5;
|
18421 | const Z_DEFAULT_COMPRESSION = -1;
|
18422 |
|
18423 |
|
18424 | const Z_FILTERED = 1;
|
18425 | const Z_HUFFMAN_ONLY = 2;
|
18426 | const Z_RLE = 3;
|
18427 | const Z_FIXED = 4;
|
18428 | const Z_DEFAULT_STRATEGY = 0;
|
18429 |
|
18430 |
|
18431 | const Z_BINARY = 0;
|
18432 | const Z_TEXT = 1;
|
18433 |
|
18434 | const Z_UNKNOWN = 2;
|
18435 |
|
18436 |
|
18437 | const Z_DEFLATED = 8;
|
18438 |
|
18439 |
|
18440 |
|
18441 |
|
18442 |
|
18443 | function zero$1(buf) {
|
18444 | let len = buf.length; while (--len >= 0) {
|
18445 | buf[len] = 0;
|
18446 | }
|
18447 | }
|
18448 |
|
18449 |
|
18450 |
|
18451 | const STORED_BLOCK = 0;
|
18452 | const STATIC_TREES = 1;
|
18453 | const DYN_TREES = 2;
|
18454 |
|
18455 |
|
18456 | const MIN_MATCH = 3;
|
18457 | const MAX_MATCH = 258;
|
18458 |
|
18459 |
|
18460 |
|
18461 |
|
18462 |
|
18463 |
|
18464 |
|
18465 | const LENGTH_CODES = 29;
|
18466 |
|
18467 |
|
18468 | const LITERALS = 256;
|
18469 |
|
18470 |
|
18471 | const L_CODES = LITERALS + 1 + LENGTH_CODES;
|
18472 |
|
18473 |
|
18474 | const D_CODES = 30;
|
18475 |
|
18476 |
|
18477 | const BL_CODES = 19;
|
18478 |
|
18479 |
|
18480 | const HEAP_SIZE = 2 * L_CODES + 1;
|
18481 |
|
18482 |
|
18483 | const MAX_BITS = 15;
|
18484 |
|
18485 |
|
18486 | const Buf_size = 16;
|
18487 |
|
18488 |
|
18489 |
|
18490 |
|
18491 |
|
18492 |
|
18493 |
|
18494 | const MAX_BL_BITS = 7;
|
18495 |
|
18496 |
|
18497 | const END_BLOCK = 256;
|
18498 |
|
18499 |
|
18500 | const REP_3_6 = 16;
|
18501 |
|
18502 |
|
18503 | const REPZ_3_10 = 17;
|
18504 |
|
18505 |
|
18506 | const REPZ_11_138 = 18;
|
18507 |
|
18508 |
|
18509 |
|
18510 | const extra_lbits =
|
18511 | [0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0];
|
18512 |
|
18513 | const extra_dbits =
|
18514 | [0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13];
|
18515 |
|
18516 | const extra_blbits =
|
18517 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7];
|
18518 |
|
18519 | const bl_order =
|
18520 | [16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15];
|
18521 |
|
18522 |
|
18523 |
|
18524 |
|
18525 |
|
18526 |
|
18527 |
|
18528 |
|
18529 |
|
18530 |
|
18531 |
|
18532 |
|
18533 | const DIST_CODE_LEN = 512;
|
18534 |
|
18535 |
|
18536 | const static_ltree = new Array((L_CODES + 2) * 2);
|
18537 | zero$1(static_ltree);
|
18538 |
|
18539 |
|
18540 |
|
18541 |
|
18542 |
|
18543 |
|
18544 | const static_dtree = new Array(D_CODES * 2);
|
18545 | zero$1(static_dtree);
|
18546 |
|
18547 |
|
18548 |
|
18549 |
|
18550 | const _dist_code = new Array(DIST_CODE_LEN);
|
18551 | zero$1(_dist_code);
|
18552 |
|
18553 |
|
18554 |
|
18555 |
|
18556 |
|
18557 | const _length_code = new Array(MAX_MATCH - MIN_MATCH + 1);
|
18558 | zero$1(_length_code);
|
18559 |
|
18560 |
|
18561 | const base_length = new Array(LENGTH_CODES);
|
18562 | zero$1(base_length);
|
18563 |
|
18564 |
|
18565 | const base_dist = new Array(D_CODES);
|
18566 | zero$1(base_dist);
|
18567 |
|
18568 |
|
18569 |
|
18570 | function StaticTreeDesc(static_tree, extra_bits, extra_base, elems, max_length) {
|
18571 |
|
18572 | this.static_tree = static_tree;
|
18573 | this.extra_bits = extra_bits;
|
18574 | this.extra_base = extra_base;
|
18575 | this.elems = elems;
|
18576 | this.max_length = max_length;
|
18577 |
|
18578 |
|
18579 | this.has_stree = static_tree && static_tree.length;
|
18580 | }
|
18581 |
|
18582 |
|
18583 | let static_l_desc;
|
18584 | let static_d_desc;
|
18585 | let static_bl_desc;
|
18586 |
|
18587 |
|
18588 | function TreeDesc(dyn_tree, stat_desc) {
|
18589 | this.dyn_tree = dyn_tree;
|
18590 | this.max_code = 0;
|
18591 | this.stat_desc = stat_desc;
|
18592 | }
|
18593 |
|
18594 |
|
18595 |
|
18596 | function d_code(dist) {
|
18597 | return dist < 256 ? _dist_code[dist] : _dist_code[256 + (dist >>> 7)];
|
18598 | }
|
18599 |
|
18600 |
|
18601 |
|
18602 |
|
18603 |
|
18604 |
|
18605 | function put_short(s, w) {
|
18606 |
|
18607 |
|
18608 | s.pending_buf[s.pending++] = w & 0xff;
|
18609 | s.pending_buf[s.pending++] = w >>> 8 & 0xff;
|
18610 | }
|
18611 |
|
18612 |
|
18613 |
|
18614 |
|
18615 |
|
18616 |
|
18617 | function send_bits(s, value, length) {
|
18618 | if (s.bi_valid > Buf_size - length) {
|
18619 | s.bi_buf |= value << s.bi_valid & 0xffff;
|
18620 | put_short(s, s.bi_buf);
|
18621 | s.bi_buf = value >> Buf_size - s.bi_valid;
|
18622 | s.bi_valid += length - Buf_size;
|
18623 | } else {
|
18624 | s.bi_buf |= value << s.bi_valid & 0xffff;
|
18625 | s.bi_valid += length;
|
18626 | }
|
18627 | }
|
18628 |
|
18629 |
|
18630 | function send_code(s, c, tree) {
|
18631 | send_bits(s, tree[c * 2], tree[c * 2 + 1]);
|
18632 | }
|
18633 |
|
18634 |
|
18635 |
|
18636 |
|
18637 |
|
18638 |
|
18639 |
|
18640 | function bi_reverse(code, len) {
|
18641 | let res = 0;
|
18642 | do {
|
18643 | res |= code & 1;
|
18644 | code >>>= 1;
|
18645 | res <<= 1;
|
18646 | } while (--len > 0);
|
18647 | return res >>> 1;
|
18648 | }
|
18649 |
|
18650 |
|
18651 |
|
18652 |
|
18653 |
|
18654 | function bi_flush(s) {
|
18655 | if (s.bi_valid === 16) {
|
18656 | put_short(s, s.bi_buf);
|
18657 | s.bi_buf = 0;
|
18658 | s.bi_valid = 0;
|
18659 |
|
18660 | } else if (s.bi_valid >= 8) {
|
18661 | s.pending_buf[s.pending++] = s.bi_buf & 0xff;
|
18662 | s.bi_buf >>= 8;
|
18663 | s.bi_valid -= 8;
|
18664 | }
|
18665 | }
|
18666 |
|
18667 |
|
18668 |
|
18669 |
|
18670 |
|
18671 |
|
18672 |
|
18673 |
|
18674 |
|
18675 |
|
18676 |
|
18677 |
|
18678 | function gen_bitlen(s, desc)
|
18679 | // deflate_state *s;
|
18680 | // tree_desc *desc; /* the tree descriptor */
|
18681 | {
|
18682 | const tree = desc.dyn_tree;
|
18683 | const max_code = desc.max_code;
|
18684 | const stree = desc.stat_desc.static_tree;
|
18685 | const has_stree = desc.stat_desc.has_stree;
|
18686 | const extra = desc.stat_desc.extra_bits;
|
18687 | const base = desc.stat_desc.extra_base;
|
18688 | const max_length = desc.stat_desc.max_length;
|
18689 | let h;
|
18690 | let n, m;
|
18691 | let bits;
|
18692 | let xbits;
|
18693 | let f;
|
18694 | let overflow = 0;
|
18695 |
|
18696 | for (bits = 0; bits <= MAX_BITS; bits++) {
|
18697 | s.bl_count[bits] = 0;
|
18698 | }
|
18699 |
|
18700 | |
18701 |
|
18702 |
|
18703 | tree[s.heap[s.heap_max] * 2 + 1] = 0;
|
18704 |
|
18705 | for (h = s.heap_max + 1; h < HEAP_SIZE; h++) {
|
18706 | n = s.heap[h];
|
18707 | bits = tree[tree[n * 2 + 1] * 2 + 1] + 1;
|
18708 | if (bits > max_length) {
|
18709 | bits = max_length;
|
18710 | overflow++;
|
18711 | }
|
18712 | tree[n * 2 + 1] = bits;
|
18713 |
|
18714 |
|
18715 | if (n > max_code) {
|
18716 | continue;
|
18717 | }
|
18718 |
|
18719 | s.bl_count[bits]++;
|
18720 | xbits = 0;
|
18721 | if (n >= base) {
|
18722 | xbits = extra[n - base];
|
18723 | }
|
18724 | f = tree[n * 2];
|
18725 | s.opt_len += f * (bits + xbits);
|
18726 | if (has_stree) {
|
18727 | s.static_len += f * (stree[n * 2 + 1] + xbits);
|
18728 | }
|
18729 | }
|
18730 | if (overflow === 0) {
|
18731 | return;
|
18732 | }
|
18733 |
|
18734 |
|
18735 |
|
18736 |
|
18737 |
|
18738 | do {
|
18739 | bits = max_length - 1;
|
18740 | while (s.bl_count[bits] === 0) {
|
18741 | bits--;
|
18742 | }
|
18743 | s.bl_count[bits]--;
|
18744 | s.bl_count[bits + 1] += 2;
|
18745 | s.bl_count[max_length]--;
|
18746 | |
18747 |
|
18748 |
|
18749 | overflow -= 2;
|
18750 | } while (overflow > 0);
|
18751 |
|
18752 | |
18753 |
|
18754 |
|
18755 |
|
18756 |
|
18757 | for (bits = max_length; bits !== 0; bits--) {
|
18758 | n = s.bl_count[bits];
|
18759 | while (n !== 0) {
|
18760 | m = s.heap[--h];
|
18761 | if (m > max_code) {
|
18762 | continue;
|
18763 | }
|
18764 | if (tree[m * 2 + 1] !== bits) {
|
18765 |
|
18766 | s.opt_len += (bits - tree[m * 2 + 1]) * tree[m * 2];
|
18767 | tree[m * 2 + 1] = bits;
|
18768 | }
|
18769 | n--;
|
18770 | }
|
18771 | }
|
18772 | }
|
18773 |
|
18774 |
|
18775 |
|
18776 |
|
18777 |
|
18778 |
|
18779 |
|
18780 |
|
18781 |
|
18782 |
|
18783 | function gen_codes(tree, max_code, bl_count)
|
18784 | // ct_data *tree; /* the tree to decorate */
|
18785 | // int max_code; /* largest code with non zero frequency */
|
18786 | // ushf *bl_count; /* number of codes at each bit length */
|
18787 | {
|
18788 | const next_code = new Array(MAX_BITS + 1);
|
18789 | let code = 0;
|
18790 | let bits;
|
18791 | let n;
|
18792 |
|
18793 | |
18794 |
|
18795 |
|
18796 | for (bits = 1; bits <= MAX_BITS; bits++) {
|
18797 | next_code[bits] = code = code + bl_count[bits - 1] << 1;
|
18798 | }
|
18799 | |
18800 |
|
18801 |
|
18802 |
|
18803 |
|
18804 |
|
18805 |
|
18806 | for (n = 0; n <= max_code; n++) {
|
18807 | const len = tree[n * 2 + 1];
|
18808 | if (len === 0) {
|
18809 | continue;
|
18810 | }
|
18811 |
|
18812 | tree[n * 2] = bi_reverse(next_code[len]++, len);
|
18813 |
|
18814 |
|
18815 |
|
18816 | }
|
18817 | }
|
18818 |
|
18819 |
|
18820 |
|
18821 |
|
18822 |
|
18823 | function tr_static_init() {
|
18824 | let n;
|
18825 | let bits;
|
18826 | let length;
|
18827 | let code;
|
18828 | let dist;
|
18829 | const bl_count = new Array(MAX_BITS + 1);
|
18830 |
|
18831 |
|
18832 |
|
18833 |
|
18834 |
|
18835 |
|
18836 | |
18837 |
|
18838 |
|
18839 |
|
18840 |
|
18841 |
|
18842 |
|
18843 |
|
18844 |
|
18845 | length = 0;
|
18846 | for (code = 0; code < LENGTH_CODES - 1; code++) {
|
18847 | base_length[code] = length;
|
18848 | for (n = 0; n < 1 << extra_lbits[code]; n++) {
|
18849 | _length_code[length++] = code;
|
18850 | }
|
18851 | }
|
18852 |
|
18853 | |
18854 |
|
18855 |
|
18856 |
|
18857 | _length_code[length - 1] = code;
|
18858 |
|
18859 |
|
18860 | dist = 0;
|
18861 | for (code = 0; code < 16; code++) {
|
18862 | base_dist[code] = dist;
|
18863 | for (n = 0; n < 1 << extra_dbits[code]; n++) {
|
18864 | _dist_code[dist++] = code;
|
18865 | }
|
18866 | }
|
18867 |
|
18868 | dist >>= 7;
|
18869 | for (; code < D_CODES; code++) {
|
18870 | base_dist[code] = dist << 7;
|
18871 | for (n = 0; n < 1 << extra_dbits[code] - 7; n++) {
|
18872 | _dist_code[256 + dist++] = code;
|
18873 | }
|
18874 | }
|
18875 |
|
18876 |
|
18877 |
|
18878 | for (bits = 0; bits <= MAX_BITS; bits++) {
|
18879 | bl_count[bits] = 0;
|
18880 | }
|
18881 |
|
18882 | n = 0;
|
18883 | while (n <= 143) {
|
18884 | static_ltree[n * 2 + 1] = 8;
|
18885 | n++;
|
18886 | bl_count[8]++;
|
18887 | }
|
18888 | while (n <= 255) {
|
18889 | static_ltree[n * 2 + 1] = 9;
|
18890 | n++;
|
18891 | bl_count[9]++;
|
18892 | }
|
18893 | while (n <= 279) {
|
18894 | static_ltree[n * 2 + 1] = 7;
|
18895 | n++;
|
18896 | bl_count[7]++;
|
18897 | }
|
18898 | while (n <= 287) {
|
18899 | static_ltree[n * 2 + 1] = 8;
|
18900 | n++;
|
18901 | bl_count[8]++;
|
18902 | }
|
18903 | |
18904 |
|
18905 |
|
18906 |
|
18907 | gen_codes(static_ltree, L_CODES + 1, bl_count);
|
18908 |
|
18909 |
|
18910 | for (n = 0; n < D_CODES; n++) {
|
18911 | static_dtree[n * 2 + 1] = 5;
|
18912 | static_dtree[n * 2] = bi_reverse(n, 5);
|
18913 | }
|
18914 |
|
18915 |
|
18916 | static_l_desc = new StaticTreeDesc(static_ltree, extra_lbits, LITERALS + 1, L_CODES, MAX_BITS);
|
18917 | static_d_desc = new StaticTreeDesc(static_dtree, extra_dbits, 0, D_CODES, MAX_BITS);
|
18918 | static_bl_desc = new StaticTreeDesc(new Array(0), extra_blbits, 0, BL_CODES, MAX_BL_BITS);
|
18919 |
|
18920 |
|
18921 | }
|
18922 |
|
18923 |
|
18924 |
|
18925 |
|
18926 |
|
18927 | function init_block(s) {
|
18928 | let n;
|
18929 |
|
18930 |
|
18931 | for (n = 0; n < L_CODES; n++) {
|
18932 | s.dyn_ltree[n * 2] = 0;
|
18933 | }
|
18934 | for (n = 0; n < D_CODES; n++) {
|
18935 | s.dyn_dtree[n * 2] = 0;
|
18936 | }
|
18937 | for (n = 0; n < BL_CODES; n++) {
|
18938 | s.bl_tree[n * 2] = 0;
|
18939 | }
|
18940 |
|
18941 | s.dyn_ltree[END_BLOCK * 2] = 1;
|
18942 | s.opt_len = s.static_len = 0;
|
18943 | s.last_lit = s.matches = 0;
|
18944 | }
|
18945 |
|
18946 |
|
18947 |
|
18948 |
|
18949 |
|
18950 | function bi_windup(s) {
|
18951 | if (s.bi_valid > 8) {
|
18952 | put_short(s, s.bi_buf);
|
18953 | } else if (s.bi_valid > 0) {
|
18954 |
|
18955 | s.pending_buf[s.pending++] = s.bi_buf;
|
18956 | }
|
18957 | s.bi_buf = 0;
|
18958 | s.bi_valid = 0;
|
18959 | }
|
18960 |
|
18961 |
|
18962 |
|
18963 |
|
18964 |
|
18965 | function copy_block(s, buf, len, header)
|
18966 | //DeflateState *s;
|
18967 | //charf *buf; /* the input data */
|
18968 | //unsigned len; /* its length */
|
18969 | //int header; /* true if block header must be written */
|
18970 | {
|
18971 | bi_windup(s);
|
18972 |
|
18973 | if (header) {
|
18974 | put_short(s, len);
|
18975 | put_short(s, ~len);
|
18976 | }
|
18977 |
|
18978 |
|
18979 |
|
18980 | arraySet(s.pending_buf, s.window, buf, len, s.pending);
|
18981 | s.pending += len;
|
18982 | }
|
18983 |
|
18984 |
|
18985 |
|
18986 |
|
18987 |
|
18988 | function smaller(tree, n, m, depth) {
|
18989 | const _n2 = n * 2;
|
18990 | const _m2 = m * 2;
|
18991 | return tree[_n2] < tree[_m2] ||
|
18992 | tree[_n2] === tree[_m2] && depth[n] <= depth[m];
|
18993 | }
|
18994 |
|
18995 |
|
18996 |
|
18997 |
|
18998 |
|
18999 |
|
19000 |
|
19001 | function pqdownheap(s, tree, k)
|
19002 | // deflate_state *s;
|
19003 | // ct_data *tree; /* the tree to restore */
|
19004 | // int k; /* node to move down */
|
19005 | {
|
19006 | const v = s.heap[k];
|
19007 | let j = k << 1;
|
19008 | while (j <= s.heap_len) {
|
19009 |
|
19010 | if (j < s.heap_len &&
|
19011 | smaller(tree, s.heap[j + 1], s.heap[j], s.depth)) {
|
19012 | j++;
|
19013 | }
|
19014 |
|
19015 | if (smaller(tree, v, s.heap[j], s.depth)) {
|
19016 | break;
|
19017 | }
|
19018 |
|
19019 |
|
19020 | s.heap[k] = s.heap[j];
|
19021 | k = j;
|
19022 |
|
19023 |
|
19024 | j <<= 1;
|
19025 | }
|
19026 | s.heap[k] = v;
|
19027 | }
|
19028 |
|
19029 |
|
19030 |
|
19031 |
|
19032 |
|
19033 |
|
19034 |
|
19035 |
|
19036 | function compress_block(s, ltree, dtree)
|
19037 | // deflate_state *s;
|
19038 | // const ct_data *ltree; /* literal tree */
|
19039 | // const ct_data *dtree; /* distance tree */
|
19040 | {
|
19041 | let dist;
|
19042 | let lc;
|
19043 | let lx = 0;
|
19044 | let code;
|
19045 | let extra;
|
19046 |
|
19047 | if (s.last_lit !== 0) {
|
19048 | do {
|
19049 | dist = s.pending_buf[s.d_buf + lx * 2] << 8 | s.pending_buf[s.d_buf + lx * 2 + 1];
|
19050 | lc = s.pending_buf[s.l_buf + lx];
|
19051 | lx++;
|
19052 |
|
19053 | if (dist === 0) {
|
19054 | send_code(s, lc, ltree);
|
19055 |
|
19056 | } else {
|
19057 |
|
19058 | code = _length_code[lc];
|
19059 | send_code(s, code + LITERALS + 1, ltree);
|
19060 | extra = extra_lbits[code];
|
19061 | if (extra !== 0) {
|
19062 | lc -= base_length[code];
|
19063 | send_bits(s, lc, extra);
|
19064 | }
|
19065 | dist--;
|
19066 | code = d_code(dist);
|
19067 |
|
19068 |
|
19069 | send_code(s, code, dtree);
|
19070 | extra = extra_dbits[code];
|
19071 | if (extra !== 0) {
|
19072 | dist -= base_dist[code];
|
19073 | send_bits(s, dist, extra);
|
19074 | }
|
19075 | }
|
19076 |
|
19077 |
|
19078 |
|
19079 |
|
19080 |
|
19081 | } while (lx < s.last_lit);
|
19082 | }
|
19083 |
|
19084 | send_code(s, END_BLOCK, ltree);
|
19085 | }
|
19086 |
|
19087 |
|
19088 |
|
19089 |
|
19090 |
|
19091 |
|
19092 |
|
19093 |
|
19094 |
|
19095 |
|
19096 | function build_tree(s, desc)
|
19097 | // deflate_state *s;
|
19098 | // tree_desc *desc; /* the tree descriptor */
|
19099 | {
|
19100 | const tree = desc.dyn_tree;
|
19101 | const stree = desc.stat_desc.static_tree;
|
19102 | const has_stree = desc.stat_desc.has_stree;
|
19103 | const elems = desc.stat_desc.elems;
|
19104 | let n, m;
|
19105 | let max_code = -1;
|
19106 | let node;
|
19107 |
|
19108 | |
19109 |
|
19110 |
|
19111 |
|
19112 | s.heap_len = 0;
|
19113 | s.heap_max = HEAP_SIZE;
|
19114 |
|
19115 | for (n = 0; n < elems; n++) {
|
19116 | if (tree[n * 2] !== 0) {
|
19117 | s.heap[++s.heap_len] = max_code = n;
|
19118 | s.depth[n] = 0;
|
19119 |
|
19120 | } else {
|
19121 | tree[n * 2 + 1] = 0;
|
19122 | }
|
19123 | }
|
19124 |
|
19125 | |
19126 |
|
19127 |
|
19128 |
|
19129 |
|
19130 | while (s.heap_len < 2) {
|
19131 | node = s.heap[++s.heap_len] = max_code < 2 ? ++max_code : 0;
|
19132 | tree[node * 2] = 1;
|
19133 | s.depth[node] = 0;
|
19134 | s.opt_len--;
|
19135 |
|
19136 | if (has_stree) {
|
19137 | s.static_len -= stree[node * 2 + 1];
|
19138 | }
|
19139 |
|
19140 | }
|
19141 | desc.max_code = max_code;
|
19142 |
|
19143 | |
19144 |
|
19145 |
|
19146 | for (n = s.heap_len >> 1; n >= 1; n--) {
|
19147 | pqdownheap(s, tree, n);
|
19148 | }
|
19149 |
|
19150 | |
19151 |
|
19152 |
|
19153 | node = elems;
|
19154 | do {
|
19155 |
|
19156 |
|
19157 | n = s.heap[1];
|
19158 | s.heap[1] = s.heap[s.heap_len--];
|
19159 | pqdownheap(s, tree, 1);
|
19160 |
|
19161 |
|
19162 | m = s.heap[1];
|
19163 |
|
19164 | s.heap[--s.heap_max] = n;
|
19165 | s.heap[--s.heap_max] = m;
|
19166 |
|
19167 |
|
19168 | tree[node * 2] = tree[n * 2] + tree[m * 2];
|
19169 | s.depth[node] = (s.depth[n] >= s.depth[m] ? s.depth[n] : s.depth[m]) + 1;
|
19170 | tree[n * 2 + 1] = tree[m * 2 + 1] = node;
|
19171 |
|
19172 |
|
19173 | s.heap[1] = node++;
|
19174 | pqdownheap(s, tree, 1);
|
19175 |
|
19176 | } while (s.heap_len >= 2);
|
19177 |
|
19178 | s.heap[--s.heap_max] = s.heap[1];
|
19179 |
|
19180 | |
19181 |
|
19182 |
|
19183 | gen_bitlen(s, desc);
|
19184 |
|
19185 |
|
19186 | gen_codes(tree, max_code, s.bl_count);
|
19187 | }
|
19188 |
|
19189 |
|
19190 |
|
19191 |
|
19192 |
|
19193 |
|
19194 | function scan_tree(s, tree, max_code)
|
19195 | // deflate_state *s;
|
19196 | // ct_data *tree; /* the tree to be scanned */
|
19197 | // int max_code; /* and its largest code of non zero frequency */
|
19198 | {
|
19199 | let n;
|
19200 | let prevlen = -1;
|
19201 | let curlen;
|
19202 |
|
19203 | let nextlen = tree[0 * 2 + 1];
|
19204 |
|
19205 | let count = 0;
|
19206 | let max_count = 7;
|
19207 | let min_count = 4;
|
19208 |
|
19209 | if (nextlen === 0) {
|
19210 | max_count = 138;
|
19211 | min_count = 3;
|
19212 | }
|
19213 | tree[(max_code + 1) * 2 + 1] = 0xffff;
|
19214 |
|
19215 | for (n = 0; n <= max_code; n++) {
|
19216 | curlen = nextlen;
|
19217 | nextlen = tree[(n + 1) * 2 + 1];
|
19218 |
|
19219 | if (++count < max_count && curlen === nextlen) {
|
19220 | continue;
|
19221 |
|
19222 | } else if (count < min_count) {
|
19223 | s.bl_tree[curlen * 2] += count;
|
19224 |
|
19225 | } else if (curlen !== 0) {
|
19226 |
|
19227 | if (curlen !== prevlen) {
|
19228 | s.bl_tree[curlen * 2]++;
|
19229 | }
|
19230 | s.bl_tree[REP_3_6 * 2]++;
|
19231 |
|
19232 | } else if (count <= 10) {
|
19233 | s.bl_tree[REPZ_3_10 * 2]++;
|
19234 |
|
19235 | } else {
|
19236 | s.bl_tree[REPZ_11_138 * 2]++;
|
19237 | }
|
19238 |
|
19239 | count = 0;
|
19240 | prevlen = curlen;
|
19241 |
|
19242 | if (nextlen === 0) {
|
19243 | max_count = 138;
|
19244 | min_count = 3;
|
19245 |
|
19246 | } else if (curlen === nextlen) {
|
19247 | max_count = 6;
|
19248 | min_count = 3;
|
19249 |
|
19250 | } else {
|
19251 | max_count = 7;
|
19252 | min_count = 4;
|
19253 | }
|
19254 | }
|
19255 | }
|
19256 |
|
19257 |
|
19258 |
|
19259 |
|
19260 |
|
19261 |
|
19262 | function send_tree(s, tree, max_code)
|
19263 | // deflate_state *s;
|
19264 | // ct_data *tree; /* the tree to be scanned */
|
19265 | // int max_code; /* and its largest code of non zero frequency */
|
19266 | {
|
19267 | let n;
|
19268 | let prevlen = -1;
|
19269 | let curlen;
|
19270 |
|
19271 | let nextlen = tree[0 * 2 + 1];
|
19272 |
|
19273 | let count = 0;
|
19274 | let max_count = 7;
|
19275 | let min_count = 4;
|
19276 |
|
19277 |
|
19278 | if (nextlen === 0) {
|
19279 | max_count = 138;
|
19280 | min_count = 3;
|
19281 | }
|
19282 |
|
19283 | for (n = 0; n <= max_code; n++) {
|
19284 | curlen = nextlen;
|
19285 | nextlen = tree[(n + 1) * 2 + 1];
|
19286 |
|
19287 | if (++count < max_count && curlen === nextlen) {
|
19288 | continue;
|
19289 |
|
19290 | } else if (count < min_count) {
|
19291 | do {
|
19292 | send_code(s, curlen, s.bl_tree);
|
19293 | } while (--count !== 0);
|
19294 |
|
19295 | } else if (curlen !== 0) {
|
19296 | if (curlen !== prevlen) {
|
19297 | send_code(s, curlen, s.bl_tree);
|
19298 | count--;
|
19299 | }
|
19300 |
|
19301 | send_code(s, REP_3_6, s.bl_tree);
|
19302 | send_bits(s, count - 3, 2);
|
19303 |
|
19304 | } else if (count <= 10) {
|
19305 | send_code(s, REPZ_3_10, s.bl_tree);
|
19306 | send_bits(s, count - 3, 3);
|
19307 |
|
19308 | } else {
|
19309 | send_code(s, REPZ_11_138, s.bl_tree);
|
19310 | send_bits(s, count - 11, 7);
|
19311 | }
|
19312 |
|
19313 | count = 0;
|
19314 | prevlen = curlen;
|
19315 | if (nextlen === 0) {
|
19316 | max_count = 138;
|
19317 | min_count = 3;
|
19318 |
|
19319 | } else if (curlen === nextlen) {
|
19320 | max_count = 6;
|
19321 | min_count = 3;
|
19322 |
|
19323 | } else {
|
19324 | max_count = 7;
|
19325 | min_count = 4;
|
19326 | }
|
19327 | }
|
19328 | }
|
19329 |
|
19330 |
|
19331 |
|
19332 |
|
19333 |
|
19334 |
|
19335 | function build_bl_tree(s) {
|
19336 | let max_blindex;
|
19337 |
|
19338 |
|
19339 | scan_tree(s, s.dyn_ltree, s.l_desc.max_code);
|
19340 | scan_tree(s, s.dyn_dtree, s.d_desc.max_code);
|
19341 |
|
19342 |
|
19343 | build_tree(s, s.bl_desc);
|
19344 | |
19345 |
|
19346 |
|
19347 |
|
19348 | |
19349 |
|
19350 |
|
19351 |
|
19352 | for (max_blindex = BL_CODES - 1; max_blindex >= 3; max_blindex--) {
|
19353 | if (s.bl_tree[bl_order[max_blindex] * 2 + 1] !== 0) {
|
19354 | break;
|
19355 | }
|
19356 | }
|
19357 |
|
19358 | s.opt_len += 3 * (max_blindex + 1) + 5 + 5 + 4;
|
19359 |
|
19360 |
|
19361 |
|
19362 | return max_blindex;
|
19363 | }
|
19364 |
|
19365 |
|
19366 |
|
19367 |
|
19368 |
|
19369 |
|
19370 |
|
19371 | function send_all_trees(s, lcodes, dcodes, blcodes)
|
19372 | // deflate_state *s;
|
19373 | // int lcodes, dcodes, blcodes; /* number of codes for each tree */
|
19374 | {
|
19375 | let rank;
|
19376 |
|
19377 |
|
19378 |
|
19379 |
|
19380 |
|
19381 | send_bits(s, lcodes - 257, 5);
|
19382 | send_bits(s, dcodes - 1, 5);
|
19383 | send_bits(s, blcodes - 4, 4);
|
19384 | for (rank = 0; rank < blcodes; rank++) {
|
19385 |
|
19386 | send_bits(s, s.bl_tree[bl_order[rank] * 2 + 1], 3);
|
19387 | }
|
19388 |
|
19389 |
|
19390 | send_tree(s, s.dyn_ltree, lcodes - 1);
|
19391 |
|
19392 |
|
19393 | send_tree(s, s.dyn_dtree, dcodes - 1);
|
19394 |
|
19395 | }
|
19396 |
|
19397 |
|
19398 |
|
19399 |
|
19400 |
|
19401 |
|
19402 |
|
19403 |
|
19404 |
|
19405 |
|
19406 |
|
19407 |
|
19408 |
|
19409 |
|
19410 |
|
19411 | function detect_data_type(s) {
|
19412 | |
19413 |
|
19414 |
|
19415 |
|
19416 | let black_mask = 0xf3ffc07f;
|
19417 | let n;
|
19418 |
|
19419 |
|
19420 | for (n = 0; n <= 31; n++, black_mask >>>= 1) {
|
19421 | if (black_mask & 1 && s.dyn_ltree[n * 2] !== 0) {
|
19422 | return Z_BINARY;
|
19423 | }
|
19424 | }
|
19425 |
|
19426 |
|
19427 | if (s.dyn_ltree[9 * 2] !== 0 || s.dyn_ltree[10 * 2] !== 0 ||
|
19428 | s.dyn_ltree[13 * 2] !== 0) {
|
19429 | return Z_TEXT;
|
19430 | }
|
19431 | for (n = 32; n < LITERALS; n++) {
|
19432 | if (s.dyn_ltree[n * 2] !== 0) {
|
19433 | return Z_TEXT;
|
19434 | }
|
19435 | }
|
19436 |
|
19437 | |
19438 |
|
19439 |
|
19440 | return Z_BINARY;
|
19441 | }
|
19442 |
|
19443 |
|
19444 | let static_init_done = false;
|
19445 |
|
19446 |
|
19447 |
|
19448 |
|
19449 | function _tr_init(s) {
|
19450 |
|
19451 | if (!static_init_done) {
|
19452 | tr_static_init();
|
19453 | static_init_done = true;
|
19454 | }
|
19455 |
|
19456 | s.l_desc = new TreeDesc(s.dyn_ltree, static_l_desc);
|
19457 | s.d_desc = new TreeDesc(s.dyn_dtree, static_d_desc);
|
19458 | s.bl_desc = new TreeDesc(s.bl_tree, static_bl_desc);
|
19459 |
|
19460 | s.bi_buf = 0;
|
19461 | s.bi_valid = 0;
|
19462 |
|
19463 |
|
19464 | init_block(s);
|
19465 | }
|
19466 |
|
19467 |
|
19468 |
|
19469 |
|
19470 |
|
19471 | function _tr_stored_block(s, buf, stored_len, last)
|
19472 | //DeflateState *s;
|
19473 | //charf *buf; /* input block */
|
19474 | //ulg stored_len; /* length of input block */
|
19475 | //int last; /* one if this is the last block for a file */
|
19476 | {
|
19477 | send_bits(s, (STORED_BLOCK << 1) + (last ? 1 : 0), 3);
|
19478 | copy_block(s, buf, stored_len, true);
|
19479 | }
|
19480 |
|
19481 |
|
19482 |
|
19483 |
|
19484 |
|
19485 |
|
19486 | function _tr_align(s) {
|
19487 | send_bits(s, STATIC_TREES << 1, 3);
|
19488 | send_code(s, END_BLOCK, static_ltree);
|
19489 | bi_flush(s);
|
19490 | }
|
19491 |
|
19492 |
|
19493 |
|
19494 |
|
19495 |
|
19496 |
|
19497 | function _tr_flush_block(s, buf, stored_len, last)
|
19498 | //DeflateState *s;
|
19499 | //charf *buf; /* input block, or NULL if too old */
|
19500 | //ulg stored_len; /* length of input block */
|
19501 | //int last; /* one if this is the last block for a file */
|
19502 | {
|
19503 | let opt_lenb, static_lenb;
|
19504 | let max_blindex = 0;
|
19505 |
|
19506 |
|
19507 | if (s.level > 0) {
|
19508 |
|
19509 |
|
19510 | if (s.strm.data_type === Z_UNKNOWN) {
|
19511 | s.strm.data_type = detect_data_type(s);
|
19512 | }
|
19513 |
|
19514 |
|
19515 | build_tree(s, s.l_desc);
|
19516 |
|
19517 |
|
19518 |
|
19519 | build_tree(s, s.d_desc);
|
19520 |
|
19521 |
|
19522 | |
19523 |
|
19524 |
|
19525 |
|
19526 | |
19527 |
|
19528 |
|
19529 | max_blindex = build_bl_tree(s);
|
19530 |
|
19531 |
|
19532 | opt_lenb = s.opt_len + 3 + 7 >>> 3;
|
19533 | static_lenb = s.static_len + 3 + 7 >>> 3;
|
19534 |
|
19535 |
|
19536 |
|
19537 |
|
19538 |
|
19539 | if (static_lenb <= opt_lenb) {
|
19540 | opt_lenb = static_lenb;
|
19541 | }
|
19542 |
|
19543 | } else {
|
19544 |
|
19545 | opt_lenb = static_lenb = stored_len + 5;
|
19546 | }
|
19547 |
|
19548 | if (stored_len + 4 <= opt_lenb && buf !== -1) {
|
19549 |
|
19550 |
|
19551 | |
19552 |
|
19553 |
|
19554 |
|
19555 |
|
19556 |
|
19557 | _tr_stored_block(s, buf, stored_len, last);
|
19558 |
|
19559 | } else if (s.strategy === Z_FIXED || static_lenb === opt_lenb) {
|
19560 |
|
19561 | send_bits(s, (STATIC_TREES << 1) + (last ? 1 : 0), 3);
|
19562 | compress_block(s, static_ltree, static_dtree);
|
19563 |
|
19564 | } else {
|
19565 | send_bits(s, (DYN_TREES << 1) + (last ? 1 : 0), 3);
|
19566 | send_all_trees(s, s.l_desc.max_code + 1, s.d_desc.max_code + 1, max_blindex + 1);
|
19567 | compress_block(s, s.dyn_ltree, s.dyn_dtree);
|
19568 | }
|
19569 |
|
19570 | |
19571 |
|
19572 |
|
19573 | init_block(s);
|
19574 |
|
19575 | if (last) {
|
19576 | bi_windup(s);
|
19577 | }
|
19578 |
|
19579 |
|
19580 | }
|
19581 |
|
19582 |
|
19583 |
|
19584 |
|
19585 |
|
19586 | function _tr_tally(s, dist, lc)
|
19587 | // deflate_state *s;
|
19588 | // unsigned dist; /* distance of matched string */
|
19589 | // unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */
|
19590 | {
|
19591 |
|
19592 |
|
19593 | s.pending_buf[s.d_buf + s.last_lit * 2] = dist >>> 8 & 0xff;
|
19594 | s.pending_buf[s.d_buf + s.last_lit * 2 + 1] = dist & 0xff;
|
19595 |
|
19596 | s.pending_buf[s.l_buf + s.last_lit] = lc & 0xff;
|
19597 | s.last_lit++;
|
19598 |
|
19599 | if (dist === 0) {
|
19600 |
|
19601 | s.dyn_ltree[lc * 2]++;
|
19602 | } else {
|
19603 | s.matches++;
|
19604 |
|
19605 | dist--;
|
19606 |
|
19607 |
|
19608 |
|
19609 |
|
19610 | s.dyn_ltree[(_length_code[lc] + LITERALS + 1) * 2]++;
|
19611 | s.dyn_dtree[d_code(dist) * 2]++;
|
19612 | }
|
19613 |
|
19614 |
|
19615 |
|
19616 |
|
19617 |
|
19618 |
|
19619 |
|
19620 |
|
19621 |
|
19622 |
|
19623 |
|
19624 |
|
19625 |
|
19626 |
|
19627 |
|
19628 |
|
19629 |
|
19630 |
|
19631 |
|
19632 |
|
19633 |
|
19634 |
|
19635 |
|
19636 |
|
19637 | return s.last_lit === s.lit_bufsize - 1;
|
19638 | |
19639 |
|
19640 |
|
19641 |
|
19642 | }
|
19643 |
|
19644 |
|
19645 |
|
19646 |
|
19647 |
|
19648 |
|
19649 |
|
19650 |
|
19651 |
|
19652 |
|
19653 |
|
19654 |
|
19655 |
|
19656 |
|
19657 |
|
19658 |
|
19659 |
|
19660 |
|
19661 |
|
19662 |
|
19663 |
|
19664 |
|
19665 |
|
19666 |
|
19667 | function adler32(adler, buf, len, pos) {
|
19668 | let s1 = adler & 0xffff |0,
|
19669 | s2 = adler >>> 16 & 0xffff |0,
|
19670 | n = 0;
|
19671 |
|
19672 | while (len !== 0) {
|
19673 |
|
19674 |
|
19675 |
|
19676 | n = len > 2000 ? 2000 : len;
|
19677 | len -= n;
|
19678 |
|
19679 | do {
|
19680 | s1 = s1 + buf[pos++] |0;
|
19681 | s2 = s2 + s1 |0;
|
19682 | } while (--n);
|
19683 |
|
19684 | s1 %= 65521;
|
19685 | s2 %= 65521;
|
19686 | }
|
19687 |
|
19688 | return s1 | s2 << 16 |0;
|
19689 | }
|
19690 |
|
19691 |
|
19692 |
|
19693 |
|
19694 |
|
19695 |
|
19696 |
|
19697 |
|
19698 |
|
19699 |
|
19700 |
|
19701 |
|
19702 |
|
19703 |
|
19704 |
|
19705 |
|
19706 |
|
19707 |
|
19708 |
|
19709 |
|
19710 |
|
19711 |
|
19712 |
|
19713 |
|
19714 |
|
19715 | function makeTable() {
|
19716 | let c;
|
19717 | const table = [];
|
19718 |
|
19719 | for (let n = 0; n < 256; n++) {
|
19720 | c = n;
|
19721 | for (let k = 0; k < 8; k++) {
|
19722 | c = c & 1 ? 0xEDB88320 ^ c >>> 1 : c >>> 1;
|
19723 | }
|
19724 | table[n] = c;
|
19725 | }
|
19726 |
|
19727 | return table;
|
19728 | }
|
19729 |
|
19730 |
|
19731 | const crcTable = makeTable();
|
19732 |
|
19733 |
|
19734 | function crc32(crc, buf, len, pos) {
|
19735 | const t = crcTable,
|
19736 | end = pos + len;
|
19737 |
|
19738 | crc ^= -1;
|
19739 |
|
19740 | for (let i = pos; i < end; i++) {
|
19741 | crc = crc >>> 8 ^ t[(crc ^ buf[i]) & 0xFF];
|
19742 | }
|
19743 |
|
19744 | return crc ^ -1;
|
19745 | }
|
19746 |
|
19747 |
|
19748 |
|
19749 |
|
19750 |
|
19751 |
|
19752 |
|
19753 |
|
19754 |
|
19755 |
|
19756 |
|
19757 |
|
19758 |
|
19759 |
|
19760 |
|
19761 |
|
19762 |
|
19763 |
|
19764 |
|
19765 |
|
19766 | var msg = {
|
19767 | 2: "need dictionary",
|
19768 | 1: "stream end",
|
19769 | 0: "",
|
19770 | "-1": "file error",
|
19771 | "-2": "stream error",
|
19772 | "-3": "data error",
|
19773 | "-4": "insufficient memory",
|
19774 | "-5": "buffer error",
|
19775 | "-6": "incompatible version"
|
19776 | };
|
19777 |
|
19778 |
|
19779 |
|
19780 |
|
19781 | const MAX_MEM_LEVEL = 9;
|
19782 |
|
19783 |
|
19784 | const LENGTH_CODES$1 = 29;
|
19785 |
|
19786 | const LITERALS$1 = 256;
|
19787 |
|
19788 | const L_CODES$1 = LITERALS$1 + 1 + LENGTH_CODES$1;
|
19789 |
|
19790 | const D_CODES$1 = 30;
|
19791 |
|
19792 | const BL_CODES$1 = 19;
|
19793 |
|
19794 | const HEAP_SIZE$1 = 2 * L_CODES$1 + 1;
|
19795 |
|
19796 | const MAX_BITS$1 = 15;
|
19797 |
|
19798 |
|
19799 | const MIN_MATCH$1 = 3;
|
19800 | const MAX_MATCH$1 = 258;
|
19801 | const MIN_LOOKAHEAD = (MAX_MATCH$1 + MIN_MATCH$1 + 1);
|
19802 |
|
19803 | const PRESET_DICT = 0x20;
|
19804 |
|
19805 | const INIT_STATE = 42;
|
19806 | const EXTRA_STATE = 69;
|
19807 | const NAME_STATE = 73;
|
19808 | const COMMENT_STATE = 91;
|
19809 | const HCRC_STATE = 103;
|
19810 | const BUSY_STATE = 113;
|
19811 | const FINISH_STATE = 666;
|
19812 |
|
19813 | const BS_NEED_MORE = 1;
|
19814 | const BS_BLOCK_DONE = 2;
|
19815 | const BS_FINISH_STARTED = 3;
|
19816 | const BS_FINISH_DONE = 4;
|
19817 |
|
19818 | const OS_CODE = 0x03;
|
19819 |
|
19820 | function err(strm, errorCode) {
|
19821 | strm.msg = msg[errorCode];
|
19822 | return errorCode;
|
19823 | }
|
19824 |
|
19825 | function rank(f) {
|
19826 | return ((f) << 1) - ((f) > 4 ? 9 : 0);
|
19827 | }
|
19828 |
|
19829 | function zero$2(buf) { let len = buf.length; while (--len >= 0) { buf[len] = 0; } }
|
19830 |
|
19831 |
|
19832 |
|
19833 |
|
19834 |
|
19835 |
|
19836 |
|
19837 |
|
19838 | function flush_pending(strm) {
|
19839 | const s = strm.state;
|
19840 |
|
19841 |
|
19842 | let len = s.pending;
|
19843 | if (len > strm.avail_out) {
|
19844 | len = strm.avail_out;
|
19845 | }
|
19846 | if (len === 0) { return; }
|
19847 |
|
19848 | arraySet(strm.output, s.pending_buf, s.pending_out, len, strm.next_out);
|
19849 | strm.next_out += len;
|
19850 | s.pending_out += len;
|
19851 | strm.total_out += len;
|
19852 | strm.avail_out -= len;
|
19853 | s.pending -= len;
|
19854 | if (s.pending === 0) {
|
19855 | s.pending_out = 0;
|
19856 | }
|
19857 | }
|
19858 |
|
19859 |
|
19860 | function flush_block_only(s, last) {
|
19861 | _tr_flush_block(s, (s.block_start >= 0 ? s.block_start : -1), s.strstart - s.block_start, last);
|
19862 | s.block_start = s.strstart;
|
19863 | flush_pending(s.strm);
|
19864 | }
|
19865 |
|
19866 |
|
19867 | function put_byte(s, b) {
|
19868 | s.pending_buf[s.pending++] = b;
|
19869 | }
|
19870 |
|
19871 |
|
19872 |
|
19873 |
|
19874 |
|
19875 |
|
19876 |
|
19877 | function putShortMSB(s, b) {
|
19878 |
|
19879 |
|
19880 | s.pending_buf[s.pending++] = (b >>> 8) & 0xff;
|
19881 | s.pending_buf[s.pending++] = b & 0xff;
|
19882 | }
|
19883 |
|
19884 |
|
19885 |
|
19886 |
|
19887 |
|
19888 |
|
19889 |
|
19890 |
|
19891 |
|
19892 | function read_buf(strm, buf, start, size) {
|
19893 | let len = strm.avail_in;
|
19894 |
|
19895 | if (len > size) { len = size; }
|
19896 | if (len === 0) { return 0; }
|
19897 |
|
19898 | strm.avail_in -= len;
|
19899 |
|
19900 |
|
19901 | arraySet(buf, strm.input, strm.next_in, len, start);
|
19902 | if (strm.state.wrap === 1) {
|
19903 | strm.adler = adler32(strm.adler, buf, len, start);
|
19904 | }
|
19905 |
|
19906 | else if (strm.state.wrap === 2) {
|
19907 | strm.adler = crc32(strm.adler, buf, len, start);
|
19908 | }
|
19909 |
|
19910 | strm.next_in += len;
|
19911 | strm.total_in += len;
|
19912 |
|
19913 | return len;
|
19914 | }
|
19915 |
|
19916 |
|
19917 |
|
19918 |
|
19919 |
|
19920 |
|
19921 |
|
19922 |
|
19923 |
|
19924 |
|
19925 |
|
19926 | function longest_match(s, cur_match) {
|
19927 | let chain_length = s.max_chain_length;
|
19928 | let scan = s.strstart;
|
19929 | let match;
|
19930 | let len;
|
19931 | let best_len = s.prev_length;
|
19932 | let nice_match = s.nice_match;
|
19933 | const limit = (s.strstart > (s.w_size - MIN_LOOKAHEAD)) ?
|
19934 | s.strstart - (s.w_size - MIN_LOOKAHEAD) : 0;
|
19935 |
|
19936 | const _win = s.window;
|
19937 |
|
19938 | const wmask = s.w_mask;
|
19939 | const prev = s.prev;
|
19940 |
|
19941 | |
19942 |
|
19943 |
|
19944 |
|
19945 | const strend = s.strstart + MAX_MATCH$1;
|
19946 | let scan_end1 = _win[scan + best_len - 1];
|
19947 | let scan_end = _win[scan + best_len];
|
19948 |
|
19949 | |
19950 |
|
19951 |
|
19952 |
|
19953 |
|
19954 |
|
19955 | if (s.prev_length >= s.good_match) {
|
19956 | chain_length >>= 2;
|
19957 | }
|
19958 | |
19959 |
|
19960 |
|
19961 | if (nice_match > s.lookahead) { nice_match = s.lookahead; }
|
19962 |
|
19963 |
|
19964 |
|
19965 | do {
|
19966 |
|
19967 | match = cur_match;
|
19968 |
|
19969 | |
19970 |
|
19971 |
|
19972 |
|
19973 |
|
19974 |
|
19975 |
|
19976 |
|
19977 |
|
19978 | if (_win[match + best_len] !== scan_end ||
|
19979 | _win[match + best_len - 1] !== scan_end1 ||
|
19980 | _win[match] !== _win[scan] ||
|
19981 | _win[++match] !== _win[scan + 1]) {
|
19982 | continue;
|
19983 | }
|
19984 |
|
19985 | |
19986 |
|
19987 |
|
19988 |
|
19989 |
|
19990 |
|
19991 | scan += 2;
|
19992 | match++;
|
19993 |
|
19994 |
|
19995 | |
19996 |
|
19997 |
|
19998 | do {
|
19999 |
|
20000 | } while (_win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
|
20001 | _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
|
20002 | _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
|
20003 | _win[++scan] === _win[++match] && _win[++scan] === _win[++match] &&
|
20004 | scan < strend);
|
20005 |
|
20006 |
|
20007 |
|
20008 | len = MAX_MATCH$1 - (strend - scan);
|
20009 | scan = strend - MAX_MATCH$1;
|
20010 |
|
20011 | if (len > best_len) {
|
20012 | s.match_start = cur_match;
|
20013 | best_len = len;
|
20014 | if (len >= nice_match) {
|
20015 | break;
|
20016 | }
|
20017 | scan_end1 = _win[scan + best_len - 1];
|
20018 | scan_end = _win[scan + best_len];
|
20019 | }
|
20020 | } while ((cur_match = prev[cur_match & wmask]) > limit && --chain_length !== 0);
|
20021 |
|
20022 | if (best_len <= s.lookahead) {
|
20023 | return best_len;
|
20024 | }
|
20025 | return s.lookahead;
|
20026 | }
|
20027 |
|
20028 |
|
20029 |
|
20030 |
|
20031 |
|
20032 |
|
20033 |
|
20034 |
|
20035 |
|
20036 |
|
20037 |
|
20038 |
|
20039 | function fill_window(s) {
|
20040 | const _w_size = s.w_size;
|
20041 | let p, n, m, more, str;
|
20042 |
|
20043 |
|
20044 |
|
20045 | do {
|
20046 | more = s.window_size - s.lookahead - s.strstart;
|
20047 |
|
20048 |
|
20049 |
|
20050 |
|
20051 |
|
20052 |
|
20053 |
|
20054 |
|
20055 |
|
20056 |
|
20057 |
|
20058 |
|
20059 |
|
20060 |
|
20061 |
|
20062 |
|
20063 | |
20064 |
|
20065 |
|
20066 | if (s.strstart >= _w_size + (_w_size - MIN_LOOKAHEAD)) {
|
20067 |
|
20068 | arraySet(s.window, s.window, _w_size, _w_size, 0);
|
20069 | s.match_start -= _w_size;
|
20070 | s.strstart -= _w_size;
|
20071 |
|
20072 | s.block_start -= _w_size;
|
20073 |
|
20074 | |
20075 |
|
20076 |
|
20077 |
|
20078 |
|
20079 |
|
20080 |
|
20081 | n = s.hash_size;
|
20082 | p = n;
|
20083 | do {
|
20084 | m = s.head[--p];
|
20085 | s.head[p] = (m >= _w_size ? m - _w_size : 0);
|
20086 | } while (--n);
|
20087 |
|
20088 | n = _w_size;
|
20089 | p = n;
|
20090 | do {
|
20091 | m = s.prev[--p];
|
20092 | s.prev[p] = (m >= _w_size ? m - _w_size : 0);
|
20093 | |
20094 |
|
20095 |
|
20096 | } while (--n);
|
20097 |
|
20098 | more += _w_size;
|
20099 | }
|
20100 | if (s.strm.avail_in === 0) {
|
20101 | break;
|
20102 | }
|
20103 |
|
20104 | |
20105 |
|
20106 |
|
20107 |
|
20108 |
|
20109 |
|
20110 |
|
20111 |
|
20112 |
|
20113 |
|
20114 |
|
20115 |
|
20116 | n = read_buf(s.strm, s.window, s.strstart + s.lookahead, more);
|
20117 | s.lookahead += n;
|
20118 |
|
20119 |
|
20120 | if (s.lookahead + s.insert >= MIN_MATCH$1) {
|
20121 | str = s.strstart - s.insert;
|
20122 | s.ins_h = s.window[str];
|
20123 |
|
20124 |
|
20125 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + 1]) & s.hash_mask;
|
20126 |
|
20127 |
|
20128 |
|
20129 | while (s.insert) {
|
20130 |
|
20131 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH$1 - 1]) & s.hash_mask;
|
20132 |
|
20133 | s.prev[str & s.w_mask] = s.head[s.ins_h];
|
20134 | s.head[s.ins_h] = str;
|
20135 | str++;
|
20136 | s.insert--;
|
20137 | if (s.lookahead + s.insert < MIN_MATCH$1) {
|
20138 | break;
|
20139 | }
|
20140 | }
|
20141 | }
|
20142 | |
20143 |
|
20144 |
|
20145 |
|
20146 | } while (s.lookahead < MIN_LOOKAHEAD && s.strm.avail_in !== 0);
|
20147 |
|
20148 | |
20149 |
|
20150 |
|
20151 |
|
20152 |
|
20153 |
|
20154 |
|
20155 |
|
20156 |
|
20157 |
|
20158 |
|
20159 |
|
20160 |
|
20161 |
|
20162 |
|
20163 |
|
20164 |
|
20165 |
|
20166 |
|
20167 |
|
20168 |
|
20169 |
|
20170 |
|
20171 |
|
20172 |
|
20173 |
|
20174 |
|
20175 |
|
20176 |
|
20177 |
|
20178 |
|
20179 |
|
20180 |
|
20181 |
|
20182 |
|
20183 |
|
20184 | }
|
20185 |
|
20186 |
|
20187 |
|
20188 |
|
20189 |
|
20190 |
|
20191 |
|
20192 |
|
20193 |
|
20194 |
|
20195 | function deflate_stored(s, flush) {
|
20196 | |
20197 |
|
20198 |
|
20199 | let max_block_size = 0xffff;
|
20200 |
|
20201 | if (max_block_size > s.pending_buf_size - 5) {
|
20202 | max_block_size = s.pending_buf_size - 5;
|
20203 | }
|
20204 |
|
20205 |
|
20206 | for (; ;) {
|
20207 |
|
20208 | if (s.lookahead <= 1) {
|
20209 |
|
20210 |
|
20211 |
|
20212 |
|
20213 |
|
20214 |
|
20215 |
|
20216 |
|
20217 | fill_window(s);
|
20218 | if (s.lookahead === 0 && flush === Z_NO_FLUSH) {
|
20219 | return BS_NEED_MORE;
|
20220 | }
|
20221 |
|
20222 | if (s.lookahead === 0) {
|
20223 | break;
|
20224 | }
|
20225 |
|
20226 | }
|
20227 |
|
20228 |
|
20229 |
|
20230 | s.strstart += s.lookahead;
|
20231 | s.lookahead = 0;
|
20232 |
|
20233 |
|
20234 | const max_start = s.block_start + max_block_size;
|
20235 |
|
20236 | if (s.strstart === 0 || s.strstart >= max_start) {
|
20237 |
|
20238 | s.lookahead = s.strstart - max_start;
|
20239 | s.strstart = max_start;
|
20240 |
|
20241 | flush_block_only(s, false);
|
20242 | if (s.strm.avail_out === 0) {
|
20243 | return BS_NEED_MORE;
|
20244 | }
|
20245 |
|
20246 |
|
20247 |
|
20248 | }
|
20249 | |
20250 |
|
20251 |
|
20252 | if (s.strstart - s.block_start >= (s.w_size - MIN_LOOKAHEAD)) {
|
20253 |
|
20254 | flush_block_only(s, false);
|
20255 | if (s.strm.avail_out === 0) {
|
20256 | return BS_NEED_MORE;
|
20257 | }
|
20258 |
|
20259 | }
|
20260 | }
|
20261 |
|
20262 | s.insert = 0;
|
20263 |
|
20264 | if (flush === Z_FINISH) {
|
20265 |
|
20266 | flush_block_only(s, true);
|
20267 | if (s.strm.avail_out === 0) {
|
20268 | return BS_FINISH_STARTED;
|
20269 | }
|
20270 |
|
20271 | return BS_FINISH_DONE;
|
20272 | }
|
20273 |
|
20274 | if (s.strstart > s.block_start) {
|
20275 |
|
20276 | flush_block_only(s, false);
|
20277 | if (s.strm.avail_out === 0) {
|
20278 | return BS_NEED_MORE;
|
20279 | }
|
20280 |
|
20281 | }
|
20282 |
|
20283 | return BS_NEED_MORE;
|
20284 | }
|
20285 |
|
20286 |
|
20287 |
|
20288 |
|
20289 |
|
20290 |
|
20291 |
|
20292 |
|
20293 | function deflate_fast(s, flush) {
|
20294 | let hash_head;
|
20295 | let bflush;
|
20296 |
|
20297 | for (; ;) {
|
20298 | |
20299 |
|
20300 |
|
20301 |
|
20302 |
|
20303 | if (s.lookahead < MIN_LOOKAHEAD) {
|
20304 | fill_window(s);
|
20305 | if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
|
20306 | return BS_NEED_MORE;
|
20307 | }
|
20308 | if (s.lookahead === 0) {
|
20309 | break;
|
20310 | }
|
20311 | }
|
20312 |
|
20313 | |
20314 |
|
20315 |
|
20316 | hash_head = 0;
|
20317 | if (s.lookahead >= MIN_MATCH$1) {
|
20318 |
|
20319 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH$1 - 1]) & s.hash_mask;
|
20320 | hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
20321 | s.head[s.ins_h] = s.strstart;
|
20322 |
|
20323 | }
|
20324 |
|
20325 | |
20326 |
|
20327 |
|
20328 | if (hash_head !== 0 && ((s.strstart - hash_head) <= (s.w_size - MIN_LOOKAHEAD))) {
|
20329 | |
20330 |
|
20331 |
|
20332 |
|
20333 | s.match_length = longest_match(s, hash_head);
|
20334 |
|
20335 | }
|
20336 | if (s.match_length >= MIN_MATCH$1) {
|
20337 |
|
20338 |
|
20339 | |
20340 |
|
20341 | bflush = _tr_tally(s, s.strstart - s.match_start, s.match_length - MIN_MATCH$1);
|
20342 |
|
20343 | s.lookahead -= s.match_length;
|
20344 |
|
20345 | |
20346 |
|
20347 |
|
20348 | if (s.match_length <= s.max_lazy_match && s.lookahead >= MIN_MATCH$1) {
|
20349 | s.match_length--;
|
20350 | do {
|
20351 | s.strstart++;
|
20352 |
|
20353 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH$1 - 1]) & s.hash_mask;
|
20354 | hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
20355 | s.head[s.ins_h] = s.strstart;
|
20356 |
|
20357 | |
20358 |
|
20359 |
|
20360 | } while (--s.match_length !== 0);
|
20361 | s.strstart++;
|
20362 | } else {
|
20363 | s.strstart += s.match_length;
|
20364 | s.match_length = 0;
|
20365 | s.ins_h = s.window[s.strstart];
|
20366 |
|
20367 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + 1]) & s.hash_mask;
|
20368 |
|
20369 |
|
20370 |
|
20371 |
|
20372 | |
20373 |
|
20374 |
|
20375 | }
|
20376 | } else {
|
20377 |
|
20378 |
|
20379 |
|
20380 | bflush = _tr_tally(s, 0, s.window[s.strstart]);
|
20381 |
|
20382 | s.lookahead--;
|
20383 | s.strstart++;
|
20384 | }
|
20385 | if (bflush) {
|
20386 |
|
20387 | flush_block_only(s, false);
|
20388 | if (s.strm.avail_out === 0) {
|
20389 | return BS_NEED_MORE;
|
20390 | }
|
20391 |
|
20392 | }
|
20393 | }
|
20394 | s.insert = ((s.strstart < (MIN_MATCH$1 - 1)) ? s.strstart : MIN_MATCH$1 - 1);
|
20395 | if (flush === Z_FINISH) {
|
20396 |
|
20397 | flush_block_only(s, true);
|
20398 | if (s.strm.avail_out === 0) {
|
20399 | return BS_FINISH_STARTED;
|
20400 | }
|
20401 |
|
20402 | return BS_FINISH_DONE;
|
20403 | }
|
20404 | if (s.last_lit) {
|
20405 |
|
20406 | flush_block_only(s, false);
|
20407 | if (s.strm.avail_out === 0) {
|
20408 | return BS_NEED_MORE;
|
20409 | }
|
20410 |
|
20411 | }
|
20412 | return BS_BLOCK_DONE;
|
20413 | }
|
20414 |
|
20415 |
|
20416 |
|
20417 |
|
20418 |
|
20419 |
|
20420 | function deflate_slow(s, flush) {
|
20421 | let hash_head;
|
20422 | let bflush;
|
20423 |
|
20424 | let max_insert;
|
20425 |
|
20426 |
|
20427 | for (; ;) {
|
20428 | |
20429 |
|
20430 |
|
20431 |
|
20432 |
|
20433 | if (s.lookahead < MIN_LOOKAHEAD) {
|
20434 | fill_window(s);
|
20435 | if (s.lookahead < MIN_LOOKAHEAD && flush === Z_NO_FLUSH) {
|
20436 | return BS_NEED_MORE;
|
20437 | }
|
20438 | if (s.lookahead === 0) { break; }
|
20439 | }
|
20440 |
|
20441 | |
20442 |
|
20443 |
|
20444 | hash_head = 0;
|
20445 | if (s.lookahead >= MIN_MATCH$1) {
|
20446 |
|
20447 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH$1 - 1]) & s.hash_mask;
|
20448 | hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
20449 | s.head[s.ins_h] = s.strstart;
|
20450 |
|
20451 | }
|
20452 |
|
20453 | |
20454 |
|
20455 | s.prev_length = s.match_length;
|
20456 | s.prev_match = s.match_start;
|
20457 | s.match_length = MIN_MATCH$1 - 1;
|
20458 |
|
20459 | if (hash_head !== 0 && s.prev_length < s.max_lazy_match &&
|
20460 | s.strstart - hash_head <= (s.w_size - MIN_LOOKAHEAD)) {
|
20461 | |
20462 |
|
20463 |
|
20464 |
|
20465 | s.match_length = longest_match(s, hash_head);
|
20466 |
|
20467 |
|
20468 | if (s.match_length <= 5 &&
|
20469 | (s.strategy === Z_FILTERED || (s.match_length === MIN_MATCH$1 && s.strstart - s.match_start > 4096))) {
|
20470 |
|
20471 | |
20472 |
|
20473 |
|
20474 | s.match_length = MIN_MATCH$1 - 1;
|
20475 | }
|
20476 | }
|
20477 | |
20478 |
|
20479 |
|
20480 | if (s.prev_length >= MIN_MATCH$1 && s.match_length <= s.prev_length) {
|
20481 | max_insert = s.strstart + s.lookahead - MIN_MATCH$1;
|
20482 |
|
20483 |
|
20484 |
|
20485 |
|
20486 | |
20487 |
|
20488 | bflush = _tr_tally(s, s.strstart - 1 - s.prev_match, s.prev_length - MIN_MATCH$1);
|
20489 | |
20490 |
|
20491 |
|
20492 |
|
20493 |
|
20494 | s.lookahead -= s.prev_length - 1;
|
20495 | s.prev_length -= 2;
|
20496 | do {
|
20497 | if (++s.strstart <= max_insert) {
|
20498 |
|
20499 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[s.strstart + MIN_MATCH$1 - 1]) & s.hash_mask;
|
20500 | hash_head = s.prev[s.strstart & s.w_mask] = s.head[s.ins_h];
|
20501 | s.head[s.ins_h] = s.strstart;
|
20502 |
|
20503 | }
|
20504 | } while (--s.prev_length !== 0);
|
20505 | s.match_available = 0;
|
20506 | s.match_length = MIN_MATCH$1 - 1;
|
20507 | s.strstart++;
|
20508 |
|
20509 | if (bflush) {
|
20510 |
|
20511 | flush_block_only(s, false);
|
20512 | if (s.strm.avail_out === 0) {
|
20513 | return BS_NEED_MORE;
|
20514 | }
|
20515 |
|
20516 | }
|
20517 |
|
20518 | } else if (s.match_available) {
|
20519 | |
20520 |
|
20521 |
|
20522 |
|
20523 |
|
20524 |
|
20525 | bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
|
20526 |
|
20527 | if (bflush) {
|
20528 |
|
20529 | flush_block_only(s, false);
|
20530 |
|
20531 | }
|
20532 | s.strstart++;
|
20533 | s.lookahead--;
|
20534 | if (s.strm.avail_out === 0) {
|
20535 | return BS_NEED_MORE;
|
20536 | }
|
20537 | } else {
|
20538 | |
20539 |
|
20540 |
|
20541 | s.match_available = 1;
|
20542 | s.strstart++;
|
20543 | s.lookahead--;
|
20544 | }
|
20545 | }
|
20546 |
|
20547 | if (s.match_available) {
|
20548 |
|
20549 |
|
20550 | bflush = _tr_tally(s, 0, s.window[s.strstart - 1]);
|
20551 |
|
20552 | s.match_available = 0;
|
20553 | }
|
20554 | s.insert = s.strstart < MIN_MATCH$1 - 1 ? s.strstart : MIN_MATCH$1 - 1;
|
20555 | if (flush === Z_FINISH) {
|
20556 |
|
20557 | flush_block_only(s, true);
|
20558 | if (s.strm.avail_out === 0) {
|
20559 | return BS_FINISH_STARTED;
|
20560 | }
|
20561 |
|
20562 | return BS_FINISH_DONE;
|
20563 | }
|
20564 | if (s.last_lit) {
|
20565 |
|
20566 | flush_block_only(s, false);
|
20567 | if (s.strm.avail_out === 0) {
|
20568 | return BS_NEED_MORE;
|
20569 | }
|
20570 |
|
20571 | }
|
20572 |
|
20573 | return BS_BLOCK_DONE;
|
20574 | }
|
20575 |
|
20576 |
|
20577 |
|
20578 |
|
20579 |
|
20580 |
|
20581 |
|
20582 | function deflate_rle(s, flush) {
|
20583 | let bflush;
|
20584 | let prev;
|
20585 | let scan, strend;
|
20586 |
|
20587 | const _win = s.window;
|
20588 |
|
20589 | for (; ;) {
|
20590 | |
20591 |
|
20592 |
|
20593 |
|
20594 | if (s.lookahead <= MAX_MATCH$1) {
|
20595 | fill_window(s);
|
20596 | if (s.lookahead <= MAX_MATCH$1 && flush === Z_NO_FLUSH) {
|
20597 | return BS_NEED_MORE;
|
20598 | }
|
20599 | if (s.lookahead === 0) { break; }
|
20600 | }
|
20601 |
|
20602 |
|
20603 | s.match_length = 0;
|
20604 | if (s.lookahead >= MIN_MATCH$1 && s.strstart > 0) {
|
20605 | scan = s.strstart - 1;
|
20606 | prev = _win[scan];
|
20607 | if (prev === _win[++scan] && prev === _win[++scan] && prev === _win[++scan]) {
|
20608 | strend = s.strstart + MAX_MATCH$1;
|
20609 | do {
|
20610 |
|
20611 | } while (prev === _win[++scan] && prev === _win[++scan] &&
|
20612 | prev === _win[++scan] && prev === _win[++scan] &&
|
20613 | prev === _win[++scan] && prev === _win[++scan] &&
|
20614 | prev === _win[++scan] && prev === _win[++scan] &&
|
20615 | scan < strend);
|
20616 | s.match_length = MAX_MATCH$1 - (strend - scan);
|
20617 | if (s.match_length > s.lookahead) {
|
20618 | s.match_length = s.lookahead;
|
20619 | }
|
20620 | }
|
20621 |
|
20622 | }
|
20623 |
|
20624 |
|
20625 | if (s.match_length >= MIN_MATCH$1) {
|
20626 |
|
20627 |
|
20628 |
|
20629 | bflush = _tr_tally(s, 1, s.match_length - MIN_MATCH$1);
|
20630 |
|
20631 | s.lookahead -= s.match_length;
|
20632 | s.strstart += s.match_length;
|
20633 | s.match_length = 0;
|
20634 | } else {
|
20635 |
|
20636 |
|
20637 |
|
20638 | bflush = _tr_tally(s, 0, s.window[s.strstart]);
|
20639 |
|
20640 | s.lookahead--;
|
20641 | s.strstart++;
|
20642 | }
|
20643 | if (bflush) {
|
20644 |
|
20645 | flush_block_only(s, false);
|
20646 | if (s.strm.avail_out === 0) {
|
20647 | return BS_NEED_MORE;
|
20648 | }
|
20649 |
|
20650 | }
|
20651 | }
|
20652 | s.insert = 0;
|
20653 | if (flush === Z_FINISH) {
|
20654 |
|
20655 | flush_block_only(s, true);
|
20656 | if (s.strm.avail_out === 0) {
|
20657 | return BS_FINISH_STARTED;
|
20658 | }
|
20659 |
|
20660 | return BS_FINISH_DONE;
|
20661 | }
|
20662 | if (s.last_lit) {
|
20663 |
|
20664 | flush_block_only(s, false);
|
20665 | if (s.strm.avail_out === 0) {
|
20666 | return BS_NEED_MORE;
|
20667 | }
|
20668 |
|
20669 | }
|
20670 | return BS_BLOCK_DONE;
|
20671 | }
|
20672 |
|
20673 |
|
20674 |
|
20675 |
|
20676 |
|
20677 | function deflate_huff(s, flush) {
|
20678 | let bflush;
|
20679 |
|
20680 | for (; ;) {
|
20681 |
|
20682 | if (s.lookahead === 0) {
|
20683 | fill_window(s);
|
20684 | if (s.lookahead === 0) {
|
20685 | if (flush === Z_NO_FLUSH) {
|
20686 | return BS_NEED_MORE;
|
20687 | }
|
20688 | break;
|
20689 | }
|
20690 | }
|
20691 |
|
20692 |
|
20693 | s.match_length = 0;
|
20694 |
|
20695 |
|
20696 | bflush = _tr_tally(s, 0, s.window[s.strstart]);
|
20697 | s.lookahead--;
|
20698 | s.strstart++;
|
20699 | if (bflush) {
|
20700 |
|
20701 | flush_block_only(s, false);
|
20702 | if (s.strm.avail_out === 0) {
|
20703 | return BS_NEED_MORE;
|
20704 | }
|
20705 |
|
20706 | }
|
20707 | }
|
20708 | s.insert = 0;
|
20709 | if (flush === Z_FINISH) {
|
20710 |
|
20711 | flush_block_only(s, true);
|
20712 | if (s.strm.avail_out === 0) {
|
20713 | return BS_FINISH_STARTED;
|
20714 | }
|
20715 |
|
20716 | return BS_FINISH_DONE;
|
20717 | }
|
20718 | if (s.last_lit) {
|
20719 |
|
20720 | flush_block_only(s, false);
|
20721 | if (s.strm.avail_out === 0) {
|
20722 | return BS_NEED_MORE;
|
20723 | }
|
20724 |
|
20725 | }
|
20726 | return BS_BLOCK_DONE;
|
20727 | }
|
20728 |
|
20729 |
|
20730 |
|
20731 |
|
20732 |
|
20733 |
|
20734 | class Config {
|
20735 | constructor(good_length, max_lazy, nice_length, max_chain, func) {
|
20736 | this.good_length = good_length;
|
20737 | this.max_lazy = max_lazy;
|
20738 | this.nice_length = nice_length;
|
20739 | this.max_chain = max_chain;
|
20740 | this.func = func;
|
20741 | }
|
20742 | }
|
20743 | const configuration_table = [
|
20744 |
|
20745 | new Config(0, 0, 0, 0, deflate_stored),
|
20746 | new Config(4, 4, 8, 4, deflate_fast),
|
20747 | new Config(4, 5, 16, 8, deflate_fast),
|
20748 | new Config(4, 6, 32, 32, deflate_fast),
|
20749 |
|
20750 | new Config(4, 4, 16, 16, deflate_slow),
|
20751 | new Config(8, 16, 32, 32, deflate_slow),
|
20752 | new Config(8, 16, 128, 128, deflate_slow),
|
20753 | new Config(8, 32, 128, 256, deflate_slow),
|
20754 | new Config(32, 128, 258, 1024, deflate_slow),
|
20755 | new Config(32, 258, 258, 4096, deflate_slow)
|
20756 | ];
|
20757 |
|
20758 |
|
20759 |
|
20760 |
|
20761 |
|
20762 | function lm_init(s) {
|
20763 | s.window_size = 2 * s.w_size;
|
20764 |
|
20765 |
|
20766 | zero$2(s.head);
|
20767 |
|
20768 | |
20769 |
|
20770 | s.max_lazy_match = configuration_table[s.level].max_lazy;
|
20771 | s.good_match = configuration_table[s.level].good_length;
|
20772 | s.nice_match = configuration_table[s.level].nice_length;
|
20773 | s.max_chain_length = configuration_table[s.level].max_chain;
|
20774 |
|
20775 | s.strstart = 0;
|
20776 | s.block_start = 0;
|
20777 | s.lookahead = 0;
|
20778 | s.insert = 0;
|
20779 | s.match_length = s.prev_length = MIN_MATCH$1 - 1;
|
20780 | s.match_available = 0;
|
20781 | s.ins_h = 0;
|
20782 | }
|
20783 |
|
20784 | class DeflateState {
|
20785 | constructor() {
|
20786 | this.strm = null;
|
20787 | this.status = 0;
|
20788 | this.pending_buf = null;
|
20789 | this.pending_buf_size = 0;
|
20790 | this.pending_out = 0;
|
20791 | this.pending = 0;
|
20792 | this.wrap = 0;
|
20793 | this.gzhead = null;
|
20794 | this.gzindex = 0;
|
20795 | this.method = Z_DEFLATED;
|
20796 | this.last_flush = -1;
|
20797 |
|
20798 | this.w_size = 0;
|
20799 | this.w_bits = 0;
|
20800 | this.w_mask = 0;
|
20801 |
|
20802 | this.window = null;
|
20803 | |
20804 |
|
20805 |
|
20806 |
|
20807 |
|
20808 |
|
20809 |
|
20810 | this.window_size = 0;
|
20811 | |
20812 |
|
20813 |
|
20814 |
|
20815 | this.prev = null;
|
20816 | |
20817 |
|
20818 |
|
20819 |
|
20820 |
|
20821 | this.head = null;
|
20822 |
|
20823 | this.ins_h = 0;
|
20824 | this.hash_size = 0;
|
20825 | this.hash_bits = 0;
|
20826 | this.hash_mask = 0;
|
20827 |
|
20828 | this.hash_shift = 0;
|
20829 | |
20830 |
|
20831 |
|
20832 |
|
20833 |
|
20834 |
|
20835 | this.block_start = 0;
|
20836 | |
20837 |
|
20838 |
|
20839 |
|
20840 | this.match_length = 0;
|
20841 | this.prev_match = 0;
|
20842 | this.match_available = 0;
|
20843 | this.strstart = 0;
|
20844 | this.match_start = 0;
|
20845 | this.lookahead = 0;
|
20846 |
|
20847 | this.prev_length = 0;
|
20848 | |
20849 |
|
20850 |
|
20851 |
|
20852 | this.max_chain_length = 0;
|
20853 | |
20854 |
|
20855 |
|
20856 |
|
20857 |
|
20858 | this.max_lazy_match = 0;
|
20859 | |
20860 |
|
20861 |
|
20862 |
|
20863 |
|
20864 |
|
20865 | |
20866 |
|
20867 |
|
20868 |
|
20869 |
|
20870 | this.level = 0;
|
20871 | this.strategy = 0;
|
20872 |
|
20873 | this.good_match = 0;
|
20874 |
|
20875 |
|
20876 | this.nice_match = 0;
|
20877 |
|
20878 |
|
20879 |
|
20880 |
|
20881 |
|
20882 |
|
20883 |
|
20884 |
|
20885 |
|
20886 |
|
20887 |
|
20888 | this.dyn_ltree = new Buf16(HEAP_SIZE$1 * 2);
|
20889 | this.dyn_dtree = new Buf16((2 * D_CODES$1 + 1) * 2);
|
20890 | this.bl_tree = new Buf16((2 * BL_CODES$1 + 1) * 2);
|
20891 | zero$2(this.dyn_ltree);
|
20892 | zero$2(this.dyn_dtree);
|
20893 | zero$2(this.bl_tree);
|
20894 |
|
20895 | this.l_desc = null;
|
20896 | this.d_desc = null;
|
20897 | this.bl_desc = null;
|
20898 |
|
20899 |
|
20900 | this.bl_count = new Buf16(MAX_BITS$1 + 1);
|
20901 |
|
20902 |
|
20903 |
|
20904 | this.heap = new Buf16(2 * L_CODES$1 + 1);
|
20905 | zero$2(this.heap);
|
20906 |
|
20907 | this.heap_len = 0;
|
20908 | this.heap_max = 0;
|
20909 | |
20910 |
|
20911 |
|
20912 |
|
20913 | this.depth = new Buf16(2 * L_CODES$1 + 1);
|
20914 | zero$2(this.depth);
|
20915 | |
20916 |
|
20917 |
|
20918 | this.l_buf = 0;
|
20919 |
|
20920 | this.lit_bufsize = 0;
|
20921 | |
20922 |
|
20923 |
|
20924 |
|
20925 |
|
20926 |
|
20927 |
|
20928 |
|
20929 |
|
20930 |
|
20931 |
|
20932 |
|
20933 |
|
20934 |
|
20935 |
|
20936 |
|
20937 |
|
20938 |
|
20939 |
|
20940 | this.last_lit = 0;
|
20941 |
|
20942 | this.d_buf = 0;
|
20943 | |
20944 |
|
20945 |
|
20946 |
|
20947 |
|
20948 | this.opt_len = 0;
|
20949 | this.static_len = 0;
|
20950 | this.matches = 0;
|
20951 | this.insert = 0;
|
20952 |
|
20953 |
|
20954 | this.bi_buf = 0;
|
20955 | |
20956 |
|
20957 |
|
20958 | this.bi_valid = 0;
|
20959 | |
20960 |
|
20961 |
|
20962 |
|
20963 |
|
20964 |
|
20965 |
|
20966 | |
20967 |
|
20968 |
|
20969 |
|
20970 |
|
20971 | }
|
20972 | }
|
20973 |
|
20974 | function deflateResetKeep(strm) {
|
20975 | let s;
|
20976 |
|
20977 | if (!strm || !strm.state) {
|
20978 | return err(strm, Z_STREAM_ERROR);
|
20979 | }
|
20980 |
|
20981 | strm.total_in = strm.total_out = 0;
|
20982 | strm.data_type = Z_UNKNOWN;
|
20983 |
|
20984 | s = strm.state;
|
20985 | s.pending = 0;
|
20986 | s.pending_out = 0;
|
20987 |
|
20988 | if (s.wrap < 0) {
|
20989 | s.wrap = -s.wrap;
|
20990 |
|
20991 | }
|
20992 | s.status = (s.wrap ? INIT_STATE : BUSY_STATE);
|
20993 | strm.adler = (s.wrap === 2) ?
|
20994 | 0
|
20995 | :
|
20996 | 1;
|
20997 | s.last_flush = Z_NO_FLUSH;
|
20998 | _tr_init(s);
|
20999 | return Z_OK;
|
21000 | }
|
21001 |
|
21002 |
|
21003 | function deflateReset(strm) {
|
21004 | const ret = deflateResetKeep(strm);
|
21005 | if (ret === Z_OK) {
|
21006 | lm_init(strm.state);
|
21007 | }
|
21008 | return ret;
|
21009 | }
|
21010 |
|
21011 |
|
21012 | function deflateSetHeader(strm, head) {
|
21013 | if (!strm || !strm.state) { return Z_STREAM_ERROR; }
|
21014 | if (strm.state.wrap !== 2) { return Z_STREAM_ERROR; }
|
21015 | strm.state.gzhead = head;
|
21016 | return Z_OK;
|
21017 | }
|
21018 |
|
21019 |
|
21020 | function deflateInit2(strm, level, method, windowBits, memLevel, strategy) {
|
21021 | if (!strm) {
|
21022 | return Z_STREAM_ERROR;
|
21023 | }
|
21024 | let wrap = 1;
|
21025 |
|
21026 | if (level === Z_DEFAULT_COMPRESSION) {
|
21027 | level = 6;
|
21028 | }
|
21029 |
|
21030 | if (windowBits < 0) {
|
21031 | wrap = 0;
|
21032 | windowBits = -windowBits;
|
21033 | }
|
21034 |
|
21035 | else if (windowBits > 15) {
|
21036 | wrap = 2;
|
21037 | windowBits -= 16;
|
21038 | }
|
21039 |
|
21040 |
|
21041 | if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method !== Z_DEFLATED ||
|
21042 | windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
|
21043 | strategy < 0 || strategy > Z_FIXED) {
|
21044 | return err(strm, Z_STREAM_ERROR);
|
21045 | }
|
21046 |
|
21047 |
|
21048 | if (windowBits === 8) {
|
21049 | windowBits = 9;
|
21050 | }
|
21051 |
|
21052 |
|
21053 | const s = new DeflateState();
|
21054 |
|
21055 | strm.state = s;
|
21056 | s.strm = strm;
|
21057 |
|
21058 | s.wrap = wrap;
|
21059 | s.gzhead = null;
|
21060 | s.w_bits = windowBits;
|
21061 | s.w_size = 1 << s.w_bits;
|
21062 | s.w_mask = s.w_size - 1;
|
21063 |
|
21064 | s.hash_bits = memLevel + 7;
|
21065 | s.hash_size = 1 << s.hash_bits;
|
21066 | s.hash_mask = s.hash_size - 1;
|
21067 | s.hash_shift = ~~((s.hash_bits + MIN_MATCH$1 - 1) / MIN_MATCH$1);
|
21068 | s.window = new Buf8(s.w_size * 2);
|
21069 | s.head = new Buf16(s.hash_size);
|
21070 | s.prev = new Buf16(s.w_size);
|
21071 |
|
21072 |
|
21073 |
|
21074 |
|
21075 | s.lit_bufsize = 1 << (memLevel + 6);
|
21076 |
|
21077 | s.pending_buf_size = s.lit_bufsize * 4;
|
21078 |
|
21079 |
|
21080 |
|
21081 | s.pending_buf = new Buf8(s.pending_buf_size);
|
21082 |
|
21083 |
|
21084 |
|
21085 | s.d_buf = 1 * s.lit_bufsize;
|
21086 |
|
21087 |
|
21088 | s.l_buf = (1 + 2) * s.lit_bufsize;
|
21089 |
|
21090 | s.level = level;
|
21091 | s.strategy = strategy;
|
21092 | s.method = method;
|
21093 |
|
21094 | return deflateReset(strm);
|
21095 | }
|
21096 |
|
21097 |
|
21098 | function deflate(strm, flush) {
|
21099 | let old_flush, s;
|
21100 | let beg, val;
|
21101 |
|
21102 | if (!strm || !strm.state ||
|
21103 | flush > Z_BLOCK || flush < 0) {
|
21104 | return strm ? err(strm, Z_STREAM_ERROR) : Z_STREAM_ERROR;
|
21105 | }
|
21106 |
|
21107 | s = strm.state;
|
21108 |
|
21109 | if (!strm.output ||
|
21110 | (!strm.input && strm.avail_in !== 0) ||
|
21111 | (s.status === FINISH_STATE && flush !== Z_FINISH)) {
|
21112 | return err(strm, (strm.avail_out === 0) ? Z_BUF_ERROR : Z_STREAM_ERROR);
|
21113 | }
|
21114 |
|
21115 | s.strm = strm;
|
21116 | old_flush = s.last_flush;
|
21117 | s.last_flush = flush;
|
21118 |
|
21119 |
|
21120 | if (s.status === INIT_STATE) {
|
21121 |
|
21122 | if (s.wrap === 2) {
|
21123 | strm.adler = 0;
|
21124 | put_byte(s, 31);
|
21125 | put_byte(s, 139);
|
21126 | put_byte(s, 8);
|
21127 | if (!s.gzhead) {
|
21128 | put_byte(s, 0);
|
21129 | put_byte(s, 0);
|
21130 | put_byte(s, 0);
|
21131 | put_byte(s, 0);
|
21132 | put_byte(s, 0);
|
21133 | put_byte(s, s.level === 9 ? 2 :
|
21134 | (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
|
21135 | 4 : 0));
|
21136 | put_byte(s, OS_CODE);
|
21137 | s.status = BUSY_STATE;
|
21138 | }
|
21139 | else {
|
21140 | put_byte(s, (s.gzhead.text ? 1 : 0) +
|
21141 | (s.gzhead.hcrc ? 2 : 0) +
|
21142 | (!s.gzhead.extra ? 0 : 4) +
|
21143 | (!s.gzhead.name ? 0 : 8) +
|
21144 | (!s.gzhead.comment ? 0 : 16)
|
21145 | );
|
21146 | put_byte(s, s.gzhead.time & 0xff);
|
21147 | put_byte(s, (s.gzhead.time >> 8) & 0xff);
|
21148 | put_byte(s, (s.gzhead.time >> 16) & 0xff);
|
21149 | put_byte(s, (s.gzhead.time >> 24) & 0xff);
|
21150 | put_byte(s, s.level === 9 ? 2 :
|
21151 | (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2 ?
|
21152 | 4 : 0));
|
21153 | put_byte(s, s.gzhead.os & 0xff);
|
21154 | if (s.gzhead.extra && s.gzhead.extra.length) {
|
21155 | put_byte(s, s.gzhead.extra.length & 0xff);
|
21156 | put_byte(s, (s.gzhead.extra.length >> 8) & 0xff);
|
21157 | }
|
21158 | if (s.gzhead.hcrc) {
|
21159 | strm.adler = crc32(strm.adler, s.pending_buf, s.pending, 0);
|
21160 | }
|
21161 | s.gzindex = 0;
|
21162 | s.status = EXTRA_STATE;
|
21163 | }
|
21164 | }
|
21165 | else
|
21166 | {
|
21167 | let header = (Z_DEFLATED + ((s.w_bits - 8) << 4)) << 8;
|
21168 | let level_flags = -1;
|
21169 |
|
21170 | if (s.strategy >= Z_HUFFMAN_ONLY || s.level < 2) {
|
21171 | level_flags = 0;
|
21172 | } else if (s.level < 6) {
|
21173 | level_flags = 1;
|
21174 | } else if (s.level === 6) {
|
21175 | level_flags = 2;
|
21176 | } else {
|
21177 | level_flags = 3;
|
21178 | }
|
21179 | header |= (level_flags << 6);
|
21180 | if (s.strstart !== 0) { header |= PRESET_DICT; }
|
21181 | header += 31 - (header % 31);
|
21182 |
|
21183 | s.status = BUSY_STATE;
|
21184 | putShortMSB(s, header);
|
21185 |
|
21186 |
|
21187 | if (s.strstart !== 0) {
|
21188 | putShortMSB(s, strm.adler >>> 16);
|
21189 | putShortMSB(s, strm.adler & 0xffff);
|
21190 | }
|
21191 | strm.adler = 1;
|
21192 | }
|
21193 | }
|
21194 |
|
21195 |
|
21196 | if (s.status === EXTRA_STATE) {
|
21197 | if (s.gzhead.extra) {
|
21198 | beg = s.pending;
|
21199 |
|
21200 | while (s.gzindex < (s.gzhead.extra.length & 0xffff)) {
|
21201 | if (s.pending === s.pending_buf_size) {
|
21202 | if (s.gzhead.hcrc && s.pending > beg) {
|
21203 | strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
|
21204 | }
|
21205 | flush_pending(strm);
|
21206 | beg = s.pending;
|
21207 | if (s.pending === s.pending_buf_size) {
|
21208 | break;
|
21209 | }
|
21210 | }
|
21211 | put_byte(s, s.gzhead.extra[s.gzindex] & 0xff);
|
21212 | s.gzindex++;
|
21213 | }
|
21214 | if (s.gzhead.hcrc && s.pending > beg) {
|
21215 | strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
|
21216 | }
|
21217 | if (s.gzindex === s.gzhead.extra.length) {
|
21218 | s.gzindex = 0;
|
21219 | s.status = NAME_STATE;
|
21220 | }
|
21221 | }
|
21222 | else {
|
21223 | s.status = NAME_STATE;
|
21224 | }
|
21225 | }
|
21226 | if (s.status === NAME_STATE) {
|
21227 | if (s.gzhead.name) {
|
21228 | beg = s.pending;
|
21229 |
|
21230 |
|
21231 | do {
|
21232 | if (s.pending === s.pending_buf_size) {
|
21233 | if (s.gzhead.hcrc && s.pending > beg) {
|
21234 | strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
|
21235 | }
|
21236 | flush_pending(strm);
|
21237 | beg = s.pending;
|
21238 | if (s.pending === s.pending_buf_size) {
|
21239 | val = 1;
|
21240 | break;
|
21241 | }
|
21242 | }
|
21243 |
|
21244 | if (s.gzindex < s.gzhead.name.length) {
|
21245 | val = s.gzhead.name.charCodeAt(s.gzindex++) & 0xff;
|
21246 | } else {
|
21247 | val = 0;
|
21248 | }
|
21249 | put_byte(s, val);
|
21250 | } while (val !== 0);
|
21251 |
|
21252 | if (s.gzhead.hcrc && s.pending > beg) {
|
21253 | strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
|
21254 | }
|
21255 | if (val === 0) {
|
21256 | s.gzindex = 0;
|
21257 | s.status = COMMENT_STATE;
|
21258 | }
|
21259 | }
|
21260 | else {
|
21261 | s.status = COMMENT_STATE;
|
21262 | }
|
21263 | }
|
21264 | if (s.status === COMMENT_STATE) {
|
21265 | if (s.gzhead.comment) {
|
21266 | beg = s.pending;
|
21267 |
|
21268 |
|
21269 | do {
|
21270 | if (s.pending === s.pending_buf_size) {
|
21271 | if (s.gzhead.hcrc && s.pending > beg) {
|
21272 | strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
|
21273 | }
|
21274 | flush_pending(strm);
|
21275 | beg = s.pending;
|
21276 | if (s.pending === s.pending_buf_size) {
|
21277 | val = 1;
|
21278 | break;
|
21279 | }
|
21280 | }
|
21281 |
|
21282 | if (s.gzindex < s.gzhead.comment.length) {
|
21283 | val = s.gzhead.comment.charCodeAt(s.gzindex++) & 0xff;
|
21284 | } else {
|
21285 | val = 0;
|
21286 | }
|
21287 | put_byte(s, val);
|
21288 | } while (val !== 0);
|
21289 |
|
21290 | if (s.gzhead.hcrc && s.pending > beg) {
|
21291 | strm.adler = crc32(strm.adler, s.pending_buf, s.pending - beg, beg);
|
21292 | }
|
21293 | if (val === 0) {
|
21294 | s.status = HCRC_STATE;
|
21295 | }
|
21296 | }
|
21297 | else {
|
21298 | s.status = HCRC_STATE;
|
21299 | }
|
21300 | }
|
21301 | if (s.status === HCRC_STATE) {
|
21302 | if (s.gzhead.hcrc) {
|
21303 | if (s.pending + 2 > s.pending_buf_size) {
|
21304 | flush_pending(strm);
|
21305 | }
|
21306 | if (s.pending + 2 <= s.pending_buf_size) {
|
21307 | put_byte(s, strm.adler & 0xff);
|
21308 | put_byte(s, (strm.adler >> 8) & 0xff);
|
21309 | strm.adler = 0;
|
21310 | s.status = BUSY_STATE;
|
21311 | }
|
21312 | }
|
21313 | else {
|
21314 | s.status = BUSY_STATE;
|
21315 | }
|
21316 | }
|
21317 |
|
21318 |
|
21319 |
|
21320 | if (s.pending !== 0) {
|
21321 | flush_pending(strm);
|
21322 | if (strm.avail_out === 0) {
|
21323 | |
21324 |
|
21325 |
|
21326 |
|
21327 |
|
21328 |
|
21329 | s.last_flush = -1;
|
21330 | return Z_OK;
|
21331 | }
|
21332 |
|
21333 | |
21334 |
|
21335 |
|
21336 |
|
21337 | } else if (strm.avail_in === 0 && rank(flush) <= rank(old_flush) &&
|
21338 | flush !== Z_FINISH) {
|
21339 | return err(strm, Z_BUF_ERROR);
|
21340 | }
|
21341 |
|
21342 |
|
21343 | if (s.status === FINISH_STATE && strm.avail_in !== 0) {
|
21344 | return err(strm, Z_BUF_ERROR);
|
21345 | }
|
21346 |
|
21347 | |
21348 |
|
21349 | if (strm.avail_in !== 0 || s.lookahead !== 0 ||
|
21350 | (flush !== Z_NO_FLUSH && s.status !== FINISH_STATE)) {
|
21351 | var bstate = (s.strategy === Z_HUFFMAN_ONLY) ? deflate_huff(s, flush) :
|
21352 | (s.strategy === Z_RLE ? deflate_rle(s, flush) :
|
21353 | configuration_table[s.level].func(s, flush));
|
21354 |
|
21355 | if (bstate === BS_FINISH_STARTED || bstate === BS_FINISH_DONE) {
|
21356 | s.status = FINISH_STATE;
|
21357 | }
|
21358 | if (bstate === BS_NEED_MORE || bstate === BS_FINISH_STARTED) {
|
21359 | if (strm.avail_out === 0) {
|
21360 | s.last_flush = -1;
|
21361 |
|
21362 | }
|
21363 | return Z_OK;
|
21364 | |
21365 |
|
21366 |
|
21367 |
|
21368 |
|
21369 |
|
21370 |
|
21371 | }
|
21372 | if (bstate === BS_BLOCK_DONE) {
|
21373 | if (flush === Z_PARTIAL_FLUSH) {
|
21374 | _tr_align(s);
|
21375 | }
|
21376 | else if (flush !== Z_BLOCK) {
|
21377 |
|
21378 | _tr_stored_block(s, 0, 0, false);
|
21379 | |
21380 |
|
21381 |
|
21382 | if (flush === Z_FULL_FLUSH) {
|
21383 |
|
21384 | zero$2(s.head);
|
21385 |
|
21386 | if (s.lookahead === 0) {
|
21387 | s.strstart = 0;
|
21388 | s.block_start = 0;
|
21389 | s.insert = 0;
|
21390 | }
|
21391 | }
|
21392 | }
|
21393 | flush_pending(strm);
|
21394 | if (strm.avail_out === 0) {
|
21395 | s.last_flush = -1;
|
21396 | return Z_OK;
|
21397 | }
|
21398 | }
|
21399 | }
|
21400 |
|
21401 |
|
21402 |
|
21403 | if (flush !== Z_FINISH) { return Z_OK; }
|
21404 | if (s.wrap <= 0) { return Z_STREAM_END; }
|
21405 |
|
21406 |
|
21407 | if (s.wrap === 2) {
|
21408 | put_byte(s, strm.adler & 0xff);
|
21409 | put_byte(s, (strm.adler >> 8) & 0xff);
|
21410 | put_byte(s, (strm.adler >> 16) & 0xff);
|
21411 | put_byte(s, (strm.adler >> 24) & 0xff);
|
21412 | put_byte(s, strm.total_in & 0xff);
|
21413 | put_byte(s, (strm.total_in >> 8) & 0xff);
|
21414 | put_byte(s, (strm.total_in >> 16) & 0xff);
|
21415 | put_byte(s, (strm.total_in >> 24) & 0xff);
|
21416 | }
|
21417 | else {
|
21418 | putShortMSB(s, strm.adler >>> 16);
|
21419 | putShortMSB(s, strm.adler & 0xffff);
|
21420 | }
|
21421 |
|
21422 | flush_pending(strm);
|
21423 | |
21424 |
|
21425 |
|
21426 | if (s.wrap > 0) { s.wrap = -s.wrap; }
|
21427 |
|
21428 | return s.pending !== 0 ? Z_OK : Z_STREAM_END;
|
21429 | }
|
21430 |
|
21431 | function deflateEnd(strm) {
|
21432 | let status;
|
21433 |
|
21434 | if (!strm || !strm.state) {
|
21435 | return Z_STREAM_ERROR;
|
21436 | }
|
21437 |
|
21438 | status = strm.state.status;
|
21439 | if (status !== INIT_STATE &&
|
21440 | status !== EXTRA_STATE &&
|
21441 | status !== NAME_STATE &&
|
21442 | status !== COMMENT_STATE &&
|
21443 | status !== HCRC_STATE &&
|
21444 | status !== BUSY_STATE &&
|
21445 | status !== FINISH_STATE
|
21446 | ) {
|
21447 | return err(strm, Z_STREAM_ERROR);
|
21448 | }
|
21449 |
|
21450 | strm.state = null;
|
21451 |
|
21452 | return status === BUSY_STATE ? err(strm, Z_DATA_ERROR) : Z_OK;
|
21453 | }
|
21454 |
|
21455 |
|
21456 |
|
21457 |
|
21458 |
|
21459 |
|
21460 | function deflateSetDictionary(strm, dictionary) {
|
21461 | const dictLength = dictionary.length;
|
21462 |
|
21463 | let s;
|
21464 | let str, n;
|
21465 | let wrap;
|
21466 | let avail;
|
21467 | let next;
|
21468 | let input;
|
21469 | let tmpDict;
|
21470 |
|
21471 | if (!strm || !strm.state) {
|
21472 | return Z_STREAM_ERROR;
|
21473 | }
|
21474 |
|
21475 | s = strm.state;
|
21476 | wrap = s.wrap;
|
21477 |
|
21478 | if (wrap === 2 || (wrap === 1 && s.status !== INIT_STATE) || s.lookahead) {
|
21479 | return Z_STREAM_ERROR;
|
21480 | }
|
21481 |
|
21482 |
|
21483 | if (wrap === 1) {
|
21484 |
|
21485 | strm.adler = adler32(strm.adler, dictionary, dictLength, 0);
|
21486 | }
|
21487 |
|
21488 | s.wrap = 0;
|
21489 |
|
21490 |
|
21491 | if (dictLength >= s.w_size) {
|
21492 | if (wrap === 0) {
|
21493 |
|
21494 | zero$2(s.head);
|
21495 | s.strstart = 0;
|
21496 | s.block_start = 0;
|
21497 | s.insert = 0;
|
21498 | }
|
21499 |
|
21500 |
|
21501 | tmpDict = new Buf8(s.w_size);
|
21502 | arraySet(tmpDict, dictionary, dictLength - s.w_size, s.w_size, 0);
|
21503 | dictionary = tmpDict;
|
21504 | dictLength = s.w_size;
|
21505 | }
|
21506 |
|
21507 | avail = strm.avail_in;
|
21508 | next = strm.next_in;
|
21509 | input = strm.input;
|
21510 | strm.avail_in = dictLength;
|
21511 | strm.next_in = 0;
|
21512 | strm.input = dictionary;
|
21513 | fill_window(s);
|
21514 | while (s.lookahead >= MIN_MATCH$1) {
|
21515 | str = s.strstart;
|
21516 | n = s.lookahead - (MIN_MATCH$1 - 1);
|
21517 | do {
|
21518 |
|
21519 | s.ins_h = ((s.ins_h << s.hash_shift) ^ s.window[str + MIN_MATCH$1 - 1]) & s.hash_mask;
|
21520 |
|
21521 | s.prev[str & s.w_mask] = s.head[s.ins_h];
|
21522 |
|
21523 | s.head[s.ins_h] = str;
|
21524 | str++;
|
21525 | } while (--n);
|
21526 | s.strstart = str;
|
21527 | s.lookahead = MIN_MATCH$1 - 1;
|
21528 | fill_window(s);
|
21529 | }
|
21530 | s.strstart += s.lookahead;
|
21531 | s.block_start = s.strstart;
|
21532 | s.insert = s.lookahead;
|
21533 | s.lookahead = 0;
|
21534 | s.match_length = s.prev_length = MIN_MATCH$1 - 1;
|
21535 | s.match_available = 0;
|
21536 | strm.next_in = next;
|
21537 | strm.input = input;
|
21538 | strm.avail_in = avail;
|
21539 | s.wrap = wrap;
|
21540 | return Z_OK;
|
21541 | }
|
21542 |
|
21543 |
|
21544 |
|
21545 |
|
21546 |
|
21547 |
|
21548 |
|
21549 |
|
21550 |
|
21551 |
|
21552 |
|
21553 |
|
21554 | try {
|
21555 | String.fromCharCode.apply(null, [ 0 ]);
|
21556 | } catch (__) {
|
21557 | }
|
21558 | try {
|
21559 | String.fromCharCode.apply(null, new Uint8Array(1));
|
21560 | } catch (__) {
|
21561 | }
|
21562 |
|
21563 |
|
21564 |
|
21565 |
|
21566 |
|
21567 | const _utf8len = new Buf8(256);
|
21568 | for (let q = 0; q < 256; q++) {
|
21569 | _utf8len[q] = q >= 252 ? 6 : q >= 248 ? 5 : q >= 240 ? 4 : q >= 224 ? 3 : q >= 192 ? 2 : 1;
|
21570 | }
|
21571 | _utf8len[254] = _utf8len[254] = 1;
|
21572 |
|
21573 |
|
21574 |
|
21575 | function string2buf (str) {
|
21576 | let c, c2, m_pos, i, buf_len = 0;
|
21577 | const str_len = str.length;
|
21578 |
|
21579 |
|
21580 | for (m_pos = 0; m_pos < str_len; m_pos++) {
|
21581 | c = str.charCodeAt(m_pos);
|
21582 | if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
|
21583 | c2 = str.charCodeAt(m_pos + 1);
|
21584 | if ((c2 & 0xfc00) === 0xdc00) {
|
21585 | c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00);
|
21586 | m_pos++;
|
21587 | }
|
21588 | }
|
21589 | buf_len += c < 0x80 ? 1 : c < 0x800 ? 2 : c < 0x10000 ? 3 : 4;
|
21590 | }
|
21591 |
|
21592 |
|
21593 | const buf = new Buf8(buf_len);
|
21594 |
|
21595 |
|
21596 | for (i = 0, m_pos = 0; i < buf_len; m_pos++) {
|
21597 | c = str.charCodeAt(m_pos);
|
21598 | if ((c & 0xfc00) === 0xd800 && m_pos + 1 < str_len) {
|
21599 | c2 = str.charCodeAt(m_pos + 1);
|
21600 | if ((c2 & 0xfc00) === 0xdc00) {
|
21601 | c = 0x10000 + (c - 0xd800 << 10) + (c2 - 0xdc00);
|
21602 | m_pos++;
|
21603 | }
|
21604 | }
|
21605 | if (c < 0x80) {
|
21606 |
|
21607 | buf[i++] = c;
|
21608 | } else if (c < 0x800) {
|
21609 |
|
21610 | buf[i++] = 0xC0 | c >>> 6;
|
21611 | buf[i++] = 0x80 | c & 0x3f;
|
21612 | } else if (c < 0x10000) {
|
21613 |
|
21614 | buf[i++] = 0xE0 | c >>> 12;
|
21615 | buf[i++] = 0x80 | c >>> 6 & 0x3f;
|
21616 | buf[i++] = 0x80 | c & 0x3f;
|
21617 | } else {
|
21618 |
|
21619 | buf[i++] = 0xf0 | c >>> 18;
|
21620 | buf[i++] = 0x80 | c >>> 12 & 0x3f;
|
21621 | buf[i++] = 0x80 | c >>> 6 & 0x3f;
|
21622 | buf[i++] = 0x80 | c & 0x3f;
|
21623 | }
|
21624 | }
|
21625 |
|
21626 | return buf;
|
21627 | }
|
21628 |
|
21629 |
|
21630 |
|
21631 | function binstring2buf (str) {
|
21632 | const buf = new Buf8(str.length);
|
21633 | for (let i = 0, len = buf.length; i < len; i++) {
|
21634 | buf[i] = str.charCodeAt(i);
|
21635 | }
|
21636 | return buf;
|
21637 | }
|
21638 |
|
21639 |
|
21640 |
|
21641 |
|
21642 |
|
21643 |
|
21644 |
|
21645 |
|
21646 |
|
21647 |
|
21648 |
|
21649 |
|
21650 |
|
21651 |
|
21652 |
|
21653 |
|
21654 |
|
21655 |
|
21656 |
|
21657 |
|
21658 | class ZStream {
|
21659 | constructor() {
|
21660 |
|
21661 | this.input = null;
|
21662 | this.next_in = 0;
|
21663 |
|
21664 | this.avail_in = 0;
|
21665 |
|
21666 | this.total_in = 0;
|
21667 |
|
21668 | this.output = null;
|
21669 | this.next_out = 0;
|
21670 |
|
21671 | this.avail_out = 0;
|
21672 |
|
21673 | this.total_out = 0;
|
21674 |
|
21675 | this.msg = '';
|
21676 |
|
21677 | this.state = null;
|
21678 |
|
21679 | this.data_type = 2;
|
21680 |
|
21681 | this.adler = 0;
|
21682 | }
|
21683 | }
|
21684 |
|
21685 |
|
21686 |
|
21687 |
|
21688 |
|
21689 |
|
21690 |
|
21691 |
|
21692 |
|
21693 |
|
21694 |
|
21695 |
|
21696 |
|
21697 |
|
21698 |
|
21699 |
|
21700 |
|
21701 |
|
21702 |
|
21703 |
|
21704 |
|
21705 |
|
21706 |
|
21707 |
|
21708 |
|
21709 |
|
21710 |
|
21711 |
|
21712 |
|
21713 |
|
21714 |
|
21715 |
|
21716 |
|
21717 |
|
21718 |
|
21719 |
|
21720 |
|
21721 |
|
21722 |
|
21723 |
|
21724 |
|
21725 |
|
21726 |
|
21727 |
|
21728 |
|
21729 |
|
21730 |
|
21731 |
|
21732 |
|
21733 |
|
21734 |
|
21735 |
|
21736 |
|
21737 |
|
21738 |
|
21739 |
|
21740 |
|
21741 |
|
21742 |
|
21743 |
|
21744 |
|
21745 |
|
21746 |
|
21747 |
|
21748 |
|
21749 |
|
21750 |
|
21751 |
|
21752 |
|
21753 |
|
21754 |
|
21755 |
|
21756 |
|
21757 |
|
21758 |
|
21759 |
|
21760 |
|
21761 |
|
21762 |
|
21763 |
|
21764 |
|
21765 |
|
21766 |
|
21767 |
|
21768 |
|
21769 |
|
21770 |
|
21771 |
|
21772 |
|
21773 |
|
21774 |
|
21775 |
|
21776 |
|
21777 |
|
21778 | class Deflate {
|
21779 | constructor(options) {
|
21780 | this.options = {
|
21781 | level: Z_DEFAULT_COMPRESSION,
|
21782 | method: Z_DEFLATED,
|
21783 | chunkSize: 16384,
|
21784 | windowBits: 15,
|
21785 | memLevel: 8,
|
21786 | strategy: Z_DEFAULT_STRATEGY,
|
21787 | ...(options || {})
|
21788 | };
|
21789 |
|
21790 | const opt = this.options;
|
21791 |
|
21792 | if (opt.raw && (opt.windowBits > 0)) {
|
21793 | opt.windowBits = -opt.windowBits;
|
21794 | }
|
21795 |
|
21796 | else if (opt.gzip && (opt.windowBits > 0) && (opt.windowBits < 16)) {
|
21797 | opt.windowBits += 16;
|
21798 | }
|
21799 |
|
21800 | this.err = 0;
|
21801 | this.msg = '';
|
21802 | this.ended = false;
|
21803 | this.chunks = [];
|
21804 |
|
21805 | this.strm = new ZStream();
|
21806 | this.strm.avail_out = 0;
|
21807 |
|
21808 | var status = deflateInit2(
|
21809 | this.strm,
|
21810 | opt.level,
|
21811 | opt.method,
|
21812 | opt.windowBits,
|
21813 | opt.memLevel,
|
21814 | opt.strategy
|
21815 | );
|
21816 |
|
21817 | if (status !== Z_OK) {
|
21818 | throw new Error(msg[status]);
|
21819 | }
|
21820 |
|
21821 | if (opt.header) {
|
21822 | deflateSetHeader(this.strm, opt.header);
|
21823 | }
|
21824 |
|
21825 | if (opt.dictionary) {
|
21826 | let dict;
|
21827 |
|
21828 | if (typeof opt.dictionary === 'string') {
|
21829 |
|
21830 | dict = string2buf(opt.dictionary);
|
21831 | } else if (opt.dictionary instanceof ArrayBuffer) {
|
21832 | dict = new Uint8Array(opt.dictionary);
|
21833 | } else {
|
21834 | dict = opt.dictionary;
|
21835 | }
|
21836 |
|
21837 | status = deflateSetDictionary(this.strm, dict);
|
21838 |
|
21839 | if (status !== Z_OK) {
|
21840 | throw new Error(msg[status]);
|
21841 | }
|
21842 |
|
21843 | this._dict_set = true;
|
21844 | }
|
21845 | }
|
21846 |
|
21847 | |
21848 |
|
21849 |
|
21850 |
|
21851 |
|
21852 |
|
21853 |
|
21854 |
|
21855 |
|
21856 |
|
21857 |
|
21858 |
|
21859 |
|
21860 |
|
21861 |
|
21862 |
|
21863 |
|
21864 |
|
21865 |
|
21866 |
|
21867 |
|
21868 |
|
21869 |
|
21870 |
|
21871 |
|
21872 |
|
21873 |
|
21874 |
|
21875 |
|
21876 | push(data, mode) {
|
21877 | const { strm, options: { chunkSize } } = this;
|
21878 | var status, _mode;
|
21879 |
|
21880 | if (this.ended) { return false; }
|
21881 |
|
21882 | _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
|
21883 |
|
21884 |
|
21885 | if (typeof data === 'string') {
|
21886 |
|
21887 | strm.input = string2buf(data);
|
21888 | } else if (data instanceof ArrayBuffer) {
|
21889 | strm.input = new Uint8Array(data);
|
21890 | } else {
|
21891 | strm.input = data;
|
21892 | }
|
21893 |
|
21894 | strm.next_in = 0;
|
21895 | strm.avail_in = strm.input.length;
|
21896 |
|
21897 | do {
|
21898 | if (strm.avail_out === 0) {
|
21899 | strm.output = new Buf8(chunkSize);
|
21900 | strm.next_out = 0;
|
21901 | strm.avail_out = chunkSize;
|
21902 | }
|
21903 | status = deflate(strm, _mode);
|
21904 |
|
21905 | if (status !== Z_STREAM_END && status !== Z_OK) {
|
21906 | this.onEnd(status);
|
21907 | this.ended = true;
|
21908 | return false;
|
21909 | }
|
21910 | if (strm.avail_out === 0 || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) {
|
21911 | this.onData(shrinkBuf(strm.output, strm.next_out));
|
21912 | }
|
21913 | } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
|
21914 |
|
21915 |
|
21916 | if (_mode === Z_FINISH) {
|
21917 | status = deflateEnd(this.strm);
|
21918 | this.onEnd(status);
|
21919 | this.ended = true;
|
21920 | return status === Z_OK;
|
21921 | }
|
21922 |
|
21923 |
|
21924 | if (_mode === Z_SYNC_FLUSH) {
|
21925 | this.onEnd(Z_OK);
|
21926 | strm.avail_out = 0;
|
21927 | return true;
|
21928 | }
|
21929 |
|
21930 | return true;
|
21931 | };
|
21932 | |
21933 |
|
21934 |
|
21935 |
|
21936 |
|
21937 |
|
21938 |
|
21939 |
|
21940 |
|
21941 | onData(chunk) {
|
21942 | this.chunks.push(chunk);
|
21943 | };
|
21944 |
|
21945 | |
21946 |
|
21947 |
|
21948 |
|
21949 |
|
21950 |
|
21951 |
|
21952 |
|
21953 |
|
21954 |
|
21955 | onEnd(status) {
|
21956 |
|
21957 | if (status === Z_OK) {
|
21958 | this.result = flattenChunks(this.chunks);
|
21959 | }
|
21960 | this.chunks = [];
|
21961 | this.err = status;
|
21962 | this.msg = this.strm.msg;
|
21963 | };
|
21964 | }
|
21965 |
|
21966 |
|
21967 |
|
21968 |
|
21969 |
|
21970 |
|
21971 |
|
21972 |
|
21973 |
|
21974 |
|
21975 |
|
21976 |
|
21977 |
|
21978 |
|
21979 |
|
21980 |
|
21981 |
|
21982 |
|
21983 |
|
21984 |
|
21985 |
|
21986 | const BAD = 30;
|
21987 | const TYPE = 12;
|
21988 |
|
21989 |
|
21990 |
|
21991 |
|
21992 |
|
21993 |
|
21994 |
|
21995 |
|
21996 |
|
21997 |
|
21998 |
|
21999 |
|
22000 |
|
22001 |
|
22002 |
|
22003 |
|
22004 |
|
22005 |
|
22006 |
|
22007 |
|
22008 |
|
22009 |
|
22010 |
|
22011 |
|
22012 |
|
22013 |
|
22014 |
|
22015 |
|
22016 |
|
22017 |
|
22018 |
|
22019 |
|
22020 |
|
22021 |
|
22022 |
|
22023 |
|
22024 | function inflate_fast(strm, start) {
|
22025 | let _in;
|
22026 | let _out;
|
22027 |
|
22028 | let hold;
|
22029 | let bits;
|
22030 | let here;
|
22031 | let op;
|
22032 |
|
22033 | let len;
|
22034 | let dist;
|
22035 | let from;
|
22036 | let from_source;
|
22037 |
|
22038 |
|
22039 |
|
22040 |
|
22041 | const state = strm.state;
|
22042 |
|
22043 | _in = strm.next_in;
|
22044 | const input = strm.input;
|
22045 | const last = _in + (strm.avail_in - 5);
|
22046 | _out = strm.next_out;
|
22047 | const output = strm.output;
|
22048 | const beg = _out - (start - strm.avail_out);
|
22049 | const end = _out + (strm.avail_out - 257);
|
22050 |
|
22051 | const dmax = state.dmax;
|
22052 |
|
22053 | const wsize = state.wsize;
|
22054 | const whave = state.whave;
|
22055 | const wnext = state.wnext;
|
22056 | const s_window = state.window;
|
22057 | hold = state.hold;
|
22058 | bits = state.bits;
|
22059 | const lcode = state.lencode;
|
22060 | const dcode = state.distcode;
|
22061 | const lmask = (1 << state.lenbits) - 1;
|
22062 | const dmask = (1 << state.distbits) - 1;
|
22063 |
|
22064 |
|
22065 | |
22066 |
|
22067 |
|
22068 | top:
|
22069 | do {
|
22070 | if (bits < 15) {
|
22071 | hold += input[_in++] << bits;
|
22072 | bits += 8;
|
22073 | hold += input[_in++] << bits;
|
22074 | bits += 8;
|
22075 | }
|
22076 |
|
22077 | here = lcode[hold & lmask];
|
22078 |
|
22079 | dolen:
|
22080 | for (;;) {
|
22081 | op = here >>> 24;
|
22082 | hold >>>= op;
|
22083 | bits -= op;
|
22084 | op = here >>> 16 & 0xff;
|
22085 | if (op === 0) {
|
22086 |
|
22087 |
|
22088 |
|
22089 | output[_out++] = here & 0xffff;
|
22090 | } else if (op & 16) {
|
22091 | len = here & 0xffff;
|
22092 | op &= 15;
|
22093 | if (op) {
|
22094 | if (bits < op) {
|
22095 | hold += input[_in++] << bits;
|
22096 | bits += 8;
|
22097 | }
|
22098 | len += hold & (1 << op) - 1;
|
22099 | hold >>>= op;
|
22100 | bits -= op;
|
22101 | }
|
22102 |
|
22103 | if (bits < 15) {
|
22104 | hold += input[_in++] << bits;
|
22105 | bits += 8;
|
22106 | hold += input[_in++] << bits;
|
22107 | bits += 8;
|
22108 | }
|
22109 | here = dcode[hold & dmask];
|
22110 |
|
22111 | dodist:
|
22112 | for (;;) {
|
22113 | op = here >>> 24;
|
22114 | hold >>>= op;
|
22115 | bits -= op;
|
22116 | op = here >>> 16 & 0xff;
|
22117 |
|
22118 | if (op & 16) {
|
22119 | dist = here & 0xffff;
|
22120 | op &= 15;
|
22121 | if (bits < op) {
|
22122 | hold += input[_in++] << bits;
|
22123 | bits += 8;
|
22124 | if (bits < op) {
|
22125 | hold += input[_in++] << bits;
|
22126 | bits += 8;
|
22127 | }
|
22128 | }
|
22129 | dist += hold & (1 << op) - 1;
|
22130 |
|
22131 | if (dist > dmax) {
|
22132 | strm.msg = "invalid distance too far back";
|
22133 | state.mode = BAD;
|
22134 | break top;
|
22135 | }
|
22136 |
|
22137 | hold >>>= op;
|
22138 | bits -= op;
|
22139 |
|
22140 | op = _out - beg;
|
22141 | if (dist > op) {
|
22142 | op = dist - op;
|
22143 | if (op > whave) {
|
22144 | if (state.sane) {
|
22145 | strm.msg = "invalid distance too far back";
|
22146 | state.mode = BAD;
|
22147 | break top;
|
22148 | }
|
22149 |
|
22150 |
|
22151 |
|
22152 |
|
22153 |
|
22154 |
|
22155 |
|
22156 |
|
22157 |
|
22158 |
|
22159 |
|
22160 |
|
22161 |
|
22162 |
|
22163 |
|
22164 |
|
22165 |
|
22166 |
|
22167 |
|
22168 |
|
22169 |
|
22170 |
|
22171 | }
|
22172 | from = 0;
|
22173 | from_source = s_window;
|
22174 | if (wnext === 0) {
|
22175 | from += wsize - op;
|
22176 | if (op < len) {
|
22177 | len -= op;
|
22178 | do {
|
22179 | output[_out++] = s_window[from++];
|
22180 | } while (--op);
|
22181 | from = _out - dist;
|
22182 | from_source = output;
|
22183 | }
|
22184 | } else if (wnext < op) {
|
22185 | from += wsize + wnext - op;
|
22186 | op -= wnext;
|
22187 | if (op < len) {
|
22188 | len -= op;
|
22189 | do {
|
22190 | output[_out++] = s_window[from++];
|
22191 | } while (--op);
|
22192 | from = 0;
|
22193 | if (wnext < len) {
|
22194 | op = wnext;
|
22195 | len -= op;
|
22196 | do {
|
22197 | output[_out++] = s_window[from++];
|
22198 | } while (--op);
|
22199 | from = _out - dist;
|
22200 | from_source = output;
|
22201 | }
|
22202 | }
|
22203 | } else {
|
22204 | from += wnext - op;
|
22205 | if (op < len) {
|
22206 | len -= op;
|
22207 | do {
|
22208 | output[_out++] = s_window[from++];
|
22209 | } while (--op);
|
22210 | from = _out - dist;
|
22211 | from_source = output;
|
22212 | }
|
22213 | }
|
22214 | while (len > 2) {
|
22215 | output[_out++] = from_source[from++];
|
22216 | output[_out++] = from_source[from++];
|
22217 | output[_out++] = from_source[from++];
|
22218 | len -= 3;
|
22219 | }
|
22220 | if (len) {
|
22221 | output[_out++] = from_source[from++];
|
22222 | if (len > 1) {
|
22223 | output[_out++] = from_source[from++];
|
22224 | }
|
22225 | }
|
22226 | } else {
|
22227 | from = _out - dist;
|
22228 | do {
|
22229 | output[_out++] = output[from++];
|
22230 | output[_out++] = output[from++];
|
22231 | output[_out++] = output[from++];
|
22232 | len -= 3;
|
22233 | } while (len > 2);
|
22234 | if (len) {
|
22235 | output[_out++] = output[from++];
|
22236 | if (len > 1) {
|
22237 | output[_out++] = output[from++];
|
22238 | }
|
22239 | }
|
22240 | }
|
22241 | } else if ((op & 64) === 0) {
|
22242 | here = dcode[(here & 0xffff) + (hold & (1 << op) - 1)];
|
22243 | continue dodist;
|
22244 | } else {
|
22245 | strm.msg = "invalid distance code";
|
22246 | state.mode = BAD;
|
22247 | break top;
|
22248 | }
|
22249 |
|
22250 | break;
|
22251 | }
|
22252 | } else if ((op & 64) === 0) {
|
22253 | here = lcode[(here & 0xffff) + (hold & (1 << op) - 1)];
|
22254 | continue dolen;
|
22255 | } else if (op & 32) {
|
22256 |
|
22257 | state.mode = TYPE;
|
22258 | break top;
|
22259 | } else {
|
22260 | strm.msg = "invalid literal/length code";
|
22261 | state.mode = BAD;
|
22262 | break top;
|
22263 | }
|
22264 |
|
22265 | break;
|
22266 | }
|
22267 | } while (_in < last && _out < end);
|
22268 |
|
22269 |
|
22270 | len = bits >> 3;
|
22271 | _in -= len;
|
22272 | bits -= len << 3;
|
22273 | hold &= (1 << bits) - 1;
|
22274 |
|
22275 |
|
22276 | strm.next_in = _in;
|
22277 | strm.next_out = _out;
|
22278 | strm.avail_in = _in < last ? 5 + (last - _in) : 5 - (_in - last);
|
22279 | strm.avail_out = _out < end ? 257 + (end - _out) : 257 - (_out - end);
|
22280 | state.hold = hold;
|
22281 | state.bits = bits;
|
22282 | return;
|
22283 | }
|
22284 |
|
22285 | const MAXBITS = 15;
|
22286 | const ENOUGH_LENS = 852;
|
22287 | const ENOUGH_DISTS = 592;
|
22288 |
|
22289 |
|
22290 | const CODES = 0;
|
22291 | const LENS = 1;
|
22292 | const DISTS = 2;
|
22293 |
|
22294 | const lbase = [
|
22295 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
22296 | 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
|
22297 | ];
|
22298 |
|
22299 | const lext = [
|
22300 | 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
|
22301 | 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78
|
22302 | ];
|
22303 |
|
22304 | const dbase = [
|
22305 | 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
22306 | 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
22307 | 8193, 12289, 16385, 24577, 0, 0
|
22308 | ];
|
22309 |
|
22310 | const dext = [
|
22311 | 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
|
22312 | 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
|
22313 | 28, 28, 29, 29, 64, 64
|
22314 | ];
|
22315 |
|
22316 | function inflate_table(type, lens, lens_index, codes, table, table_index, work, opts) {
|
22317 | const bits = opts.bits;
|
22318 |
|
22319 |
|
22320 | let len = 0;
|
22321 | let sym = 0;
|
22322 | let min = 0, max = 0;
|
22323 | let root = 0;
|
22324 | let curr = 0;
|
22325 | let drop = 0;
|
22326 | let left = 0;
|
22327 | let used = 0;
|
22328 | let huff = 0;
|
22329 | let incr;
|
22330 | let fill;
|
22331 | let low;
|
22332 | let next;
|
22333 | let base = null;
|
22334 | let base_index = 0;
|
22335 |
|
22336 | let end;
|
22337 | const count = new Buf16(MAXBITS + 1);
|
22338 | const offs = new Buf16(MAXBITS + 1);
|
22339 | let extra = null;
|
22340 | let extra_index = 0;
|
22341 |
|
22342 | let here_bits, here_op, here_val;
|
22343 |
|
22344 | |
22345 |
|
22346 |
|
22347 |
|
22348 |
|
22349 |
|
22350 |
|
22351 |
|
22352 |
|
22353 |
|
22354 |
|
22355 |
|
22356 |
|
22357 |
|
22358 |
|
22359 |
|
22360 |
|
22361 |
|
22362 |
|
22363 |
|
22364 |
|
22365 |
|
22366 |
|
22367 |
|
22368 |
|
22369 |
|
22370 |
|
22371 |
|
22372 |
|
22373 |
|
22374 |
|
22375 |
|
22376 | for (len = 0; len <= MAXBITS; len++) {
|
22377 | count[len] = 0;
|
22378 | }
|
22379 | for (sym = 0; sym < codes; sym++) {
|
22380 | count[lens[lens_index + sym]]++;
|
22381 | }
|
22382 |
|
22383 |
|
22384 | root = bits;
|
22385 | for (max = MAXBITS; max >= 1; max--) {
|
22386 | if (count[max] !== 0) {
|
22387 | break;
|
22388 | }
|
22389 | }
|
22390 | if (root > max) {
|
22391 | root = max;
|
22392 | }
|
22393 | if (max === 0) {
|
22394 |
|
22395 |
|
22396 |
|
22397 | table[table_index++] = 1 << 24 | 64 << 16 | 0;
|
22398 |
|
22399 |
|
22400 |
|
22401 |
|
22402 |
|
22403 | table[table_index++] = 1 << 24 | 64 << 16 | 0;
|
22404 |
|
22405 | opts.bits = 1;
|
22406 | return 0;
|
22407 | }
|
22408 | for (min = 1; min < max; min++) {
|
22409 | if (count[min] !== 0) {
|
22410 | break;
|
22411 | }
|
22412 | }
|
22413 | if (root < min) {
|
22414 | root = min;
|
22415 | }
|
22416 |
|
22417 |
|
22418 | left = 1;
|
22419 | for (len = 1; len <= MAXBITS; len++) {
|
22420 | left <<= 1;
|
22421 | left -= count[len];
|
22422 | if (left < 0) {
|
22423 | return -1;
|
22424 | }
|
22425 | }
|
22426 | if (left > 0 && (type === CODES || max !== 1)) {
|
22427 | return -1;
|
22428 | }
|
22429 |
|
22430 |
|
22431 | offs[1] = 0;
|
22432 | for (len = 1; len < MAXBITS; len++) {
|
22433 | offs[len + 1] = offs[len] + count[len];
|
22434 | }
|
22435 |
|
22436 |
|
22437 | for (sym = 0; sym < codes; sym++) {
|
22438 | if (lens[lens_index + sym] !== 0) {
|
22439 | work[offs[lens[lens_index + sym]]++] = sym;
|
22440 | }
|
22441 | }
|
22442 |
|
22443 | |
22444 |
|
22445 |
|
22446 |
|
22447 |
|
22448 |
|
22449 |
|
22450 |
|
22451 |
|
22452 |
|
22453 |
|
22454 |
|
22455 |
|
22456 |
|
22457 |
|
22458 |
|
22459 |
|
22460 |
|
22461 |
|
22462 |
|
22463 |
|
22464 |
|
22465 |
|
22466 |
|
22467 |
|
22468 |
|
22469 |
|
22470 |
|
22471 |
|
22472 |
|
22473 |
|
22474 |
|
22475 |
|
22476 |
|
22477 | if (type === CODES) {
|
22478 | base = extra = work;
|
22479 | end = 19;
|
22480 |
|
22481 | } else if (type === LENS) {
|
22482 | base = lbase;
|
22483 | base_index -= 257;
|
22484 | extra = lext;
|
22485 | extra_index -= 257;
|
22486 | end = 256;
|
22487 |
|
22488 | } else {
|
22489 | base = dbase;
|
22490 | extra = dext;
|
22491 | end = -1;
|
22492 | }
|
22493 |
|
22494 |
|
22495 | huff = 0;
|
22496 | sym = 0;
|
22497 | len = min;
|
22498 | next = table_index;
|
22499 | curr = root;
|
22500 | drop = 0;
|
22501 | low = -1;
|
22502 | used = 1 << root;
|
22503 | const mask = used - 1;
|
22504 |
|
22505 |
|
22506 | if (type === LENS && used > ENOUGH_LENS ||
|
22507 | type === DISTS && used > ENOUGH_DISTS) {
|
22508 | return 1;
|
22509 | }
|
22510 |
|
22511 |
|
22512 | for (;;) {
|
22513 |
|
22514 | here_bits = len - drop;
|
22515 | if (work[sym] < end) {
|
22516 | here_op = 0;
|
22517 | here_val = work[sym];
|
22518 | } else if (work[sym] > end) {
|
22519 | here_op = extra[extra_index + work[sym]];
|
22520 | here_val = base[base_index + work[sym]];
|
22521 | } else {
|
22522 | here_op = 32 + 64;
|
22523 | here_val = 0;
|
22524 | }
|
22525 |
|
22526 |
|
22527 | incr = 1 << len - drop;
|
22528 | fill = 1 << curr;
|
22529 | min = fill;
|
22530 | do {
|
22531 | fill -= incr;
|
22532 | table[next + (huff >> drop) + fill] = here_bits << 24 | here_op << 16 | here_val |0;
|
22533 | } while (fill !== 0);
|
22534 |
|
22535 |
|
22536 | incr = 1 << len - 1;
|
22537 | while (huff & incr) {
|
22538 | incr >>= 1;
|
22539 | }
|
22540 | if (incr !== 0) {
|
22541 | huff &= incr - 1;
|
22542 | huff += incr;
|
22543 | } else {
|
22544 | huff = 0;
|
22545 | }
|
22546 |
|
22547 |
|
22548 | sym++;
|
22549 | if (--count[len] === 0) {
|
22550 | if (len === max) {
|
22551 | break;
|
22552 | }
|
22553 | len = lens[lens_index + work[sym]];
|
22554 | }
|
22555 |
|
22556 |
|
22557 | if (len > root && (huff & mask) !== low) {
|
22558 |
|
22559 | if (drop === 0) {
|
22560 | drop = root;
|
22561 | }
|
22562 |
|
22563 |
|
22564 | next += min;
|
22565 |
|
22566 |
|
22567 | curr = len - drop;
|
22568 | left = 1 << curr;
|
22569 | while (curr + drop < max) {
|
22570 | left -= count[curr + drop];
|
22571 | if (left <= 0) {
|
22572 | break;
|
22573 | }
|
22574 | curr++;
|
22575 | left <<= 1;
|
22576 | }
|
22577 |
|
22578 |
|
22579 | used += 1 << curr;
|
22580 | if (type === LENS && used > ENOUGH_LENS ||
|
22581 | type === DISTS && used > ENOUGH_DISTS) {
|
22582 | return 1;
|
22583 | }
|
22584 |
|
22585 |
|
22586 | low = huff & mask;
|
22587 | |
22588 |
|
22589 |
|
22590 | table[low] = root << 24 | curr << 16 | next - table_index |0;
|
22591 | }
|
22592 | }
|
22593 |
|
22594 | |
22595 |
|
22596 |
|
22597 | if (huff !== 0) {
|
22598 |
|
22599 |
|
22600 |
|
22601 | table[next + huff] = len - drop << 24 | 64 << 16 |0;
|
22602 | }
|
22603 |
|
22604 |
|
22605 |
|
22606 | opts.bits = root;
|
22607 | return 0;
|
22608 | }
|
22609 |
|
22610 | const CODES$1 = 0;
|
22611 | const LENS$1 = 1;
|
22612 | const DISTS$1 = 2;
|
22613 |
|
22614 |
|
22615 |
|
22616 |
|
22617 |
|
22618 | const HEAD = 1;
|
22619 | const FLAGS = 2;
|
22620 | const TIME = 3;
|
22621 | const OS = 4;
|
22622 | const EXLEN = 5;
|
22623 | const EXTRA = 6;
|
22624 | const NAME = 7;
|
22625 | const COMMENT = 8;
|
22626 | const HCRC = 9;
|
22627 | const DICTID = 10;
|
22628 | const DICT = 11;
|
22629 | const TYPE$1 = 12;
|
22630 | const TYPEDO = 13;
|
22631 | const STORED = 14;
|
22632 | const COPY_ = 15;
|
22633 | const COPY = 16;
|
22634 | const TABLE = 17;
|
22635 | const LENLENS = 18;
|
22636 | const CODELENS = 19;
|
22637 | const LEN_ = 20;
|
22638 | const LEN = 21;
|
22639 | const LENEXT = 22;
|
22640 | const DIST = 23;
|
22641 | const DISTEXT = 24;
|
22642 | const MATCH = 25;
|
22643 | const LIT = 26;
|
22644 | const CHECK = 27;
|
22645 | const LENGTH = 28;
|
22646 | const DONE = 29;
|
22647 | const BAD$1 = 30;
|
22648 |
|
22649 | const SYNC = 32;
|
22650 |
|
22651 |
|
22652 |
|
22653 |
|
22654 |
|
22655 | const ENOUGH_LENS$1 = 852;
|
22656 | const ENOUGH_DISTS$1 = 592;
|
22657 |
|
22658 |
|
22659 | function zswap32(q) {
|
22660 | return (((q >>> 24) & 0xff) +
|
22661 | ((q >>> 8) & 0xff00) +
|
22662 | ((q & 0xff00) << 8) +
|
22663 | ((q & 0xff) << 24));
|
22664 | }
|
22665 |
|
22666 |
|
22667 | class InflateState {
|
22668 | constructor() {
|
22669 | this.mode = 0;
|
22670 | this.last = false;
|
22671 | this.wrap = 0;
|
22672 | this.havedict = false;
|
22673 | this.flags = 0;
|
22674 | this.dmax = 0;
|
22675 | this.check = 0;
|
22676 | this.total = 0;
|
22677 |
|
22678 | this.head = null;
|
22679 |
|
22680 |
|
22681 | this.wbits = 0;
|
22682 | this.wsize = 0;
|
22683 | this.whave = 0;
|
22684 | this.wnext = 0;
|
22685 | this.window = null;
|
22686 |
|
22687 |
|
22688 | this.hold = 0;
|
22689 | this.bits = 0;
|
22690 |
|
22691 |
|
22692 | this.length = 0;
|
22693 | this.offset = 0;
|
22694 |
|
22695 |
|
22696 | this.extra = 0;
|
22697 |
|
22698 |
|
22699 | this.lencode = null;
|
22700 | this.distcode = null;
|
22701 | this.lenbits = 0;
|
22702 | this.distbits = 0;
|
22703 |
|
22704 |
|
22705 | this.ncode = 0;
|
22706 | this.nlen = 0;
|
22707 | this.ndist = 0;
|
22708 | this.have = 0;
|
22709 | this.next = null;
|
22710 |
|
22711 | this.lens = new Buf16(320);
|
22712 | this.work = new Buf16(288);
|
22713 |
|
22714 | |
22715 |
|
22716 |
|
22717 |
|
22718 |
|
22719 | this.lendyn = null;
|
22720 | this.distdyn = null;
|
22721 | this.sane = 0;
|
22722 | this.back = 0;
|
22723 | this.was = 0;
|
22724 | }
|
22725 | }
|
22726 |
|
22727 | function inflateResetKeep(strm) {
|
22728 | let state;
|
22729 |
|
22730 | if (!strm || !strm.state) { return Z_STREAM_ERROR; }
|
22731 | state = strm.state;
|
22732 | strm.total_in = strm.total_out = state.total = 0;
|
22733 | strm.msg = '';
|
22734 | if (state.wrap) {
|
22735 | strm.adler = state.wrap & 1;
|
22736 | }
|
22737 | state.mode = HEAD;
|
22738 | state.last = 0;
|
22739 | state.havedict = 0;
|
22740 | state.dmax = 32768;
|
22741 | state.head = null;
|
22742 | state.hold = 0;
|
22743 | state.bits = 0;
|
22744 |
|
22745 | state.lencode = state.lendyn = new Buf32(ENOUGH_LENS$1);
|
22746 | state.distcode = state.distdyn = new Buf32(ENOUGH_DISTS$1);
|
22747 |
|
22748 | state.sane = 1;
|
22749 | state.back = -1;
|
22750 |
|
22751 | return Z_OK;
|
22752 | }
|
22753 |
|
22754 | function inflateReset(strm) {
|
22755 | let state;
|
22756 |
|
22757 | if (!strm || !strm.state) { return Z_STREAM_ERROR; }
|
22758 | state = strm.state;
|
22759 | state.wsize = 0;
|
22760 | state.whave = 0;
|
22761 | state.wnext = 0;
|
22762 | return inflateResetKeep(strm);
|
22763 |
|
22764 | }
|
22765 |
|
22766 | function inflateReset2(strm, windowBits) {
|
22767 | let wrap;
|
22768 | let state;
|
22769 |
|
22770 |
|
22771 | if (!strm || !strm.state) { return Z_STREAM_ERROR; }
|
22772 | state = strm.state;
|
22773 |
|
22774 |
|
22775 | if (windowBits < 0) {
|
22776 | wrap = 0;
|
22777 | windowBits = -windowBits;
|
22778 | }
|
22779 | else {
|
22780 | wrap = (windowBits >> 4) + 1;
|
22781 | if (windowBits < 48) {
|
22782 | windowBits &= 15;
|
22783 | }
|
22784 | }
|
22785 |
|
22786 |
|
22787 | if (windowBits && (windowBits < 8 || windowBits > 15)) {
|
22788 | return Z_STREAM_ERROR;
|
22789 | }
|
22790 | if (state.window !== null && state.wbits !== windowBits) {
|
22791 | state.window = null;
|
22792 | }
|
22793 |
|
22794 |
|
22795 | state.wrap = wrap;
|
22796 | state.wbits = windowBits;
|
22797 | return inflateReset(strm);
|
22798 | }
|
22799 |
|
22800 | function inflateInit2(strm, windowBits) {
|
22801 | let ret;
|
22802 | let state;
|
22803 |
|
22804 | if (!strm) { return Z_STREAM_ERROR; }
|
22805 |
|
22806 |
|
22807 | state = new InflateState();
|
22808 |
|
22809 |
|
22810 |
|
22811 | strm.state = state;
|
22812 | state.window = null;
|
22813 | ret = inflateReset2(strm, windowBits);
|
22814 | if (ret !== Z_OK) {
|
22815 | strm.state = null;
|
22816 | }
|
22817 | return ret;
|
22818 | }
|
22819 |
|
22820 |
|
22821 |
|
22822 |
|
22823 |
|
22824 |
|
22825 |
|
22826 |
|
22827 |
|
22828 |
|
22829 |
|
22830 |
|
22831 | let virgin = true;
|
22832 |
|
22833 | let lenfix, distfix;
|
22834 |
|
22835 | function fixedtables(state) {
|
22836 |
|
22837 | if (virgin) {
|
22838 | let sym;
|
22839 |
|
22840 | lenfix = new Buf32(512);
|
22841 | distfix = new Buf32(32);
|
22842 |
|
22843 |
|
22844 | sym = 0;
|
22845 | while (sym < 144) { state.lens[sym++] = 8; }
|
22846 | while (sym < 256) { state.lens[sym++] = 9; }
|
22847 | while (sym < 280) { state.lens[sym++] = 7; }
|
22848 | while (sym < 288) { state.lens[sym++] = 8; }
|
22849 |
|
22850 | inflate_table(LENS$1, state.lens, 0, 288, lenfix, 0, state.work, { bits: 9 });
|
22851 |
|
22852 |
|
22853 | sym = 0;
|
22854 | while (sym < 32) { state.lens[sym++] = 5; }
|
22855 |
|
22856 | inflate_table(DISTS$1, state.lens, 0, 32, distfix, 0, state.work, { bits: 5 });
|
22857 |
|
22858 |
|
22859 | virgin = false;
|
22860 | }
|
22861 |
|
22862 | state.lencode = lenfix;
|
22863 | state.lenbits = 9;
|
22864 | state.distcode = distfix;
|
22865 | state.distbits = 5;
|
22866 | }
|
22867 |
|
22868 |
|
22869 |
|
22870 |
|
22871 |
|
22872 |
|
22873 |
|
22874 |
|
22875 |
|
22876 |
|
22877 |
|
22878 |
|
22879 |
|
22880 |
|
22881 |
|
22882 |
|
22883 | function updatewindow(strm, src, end, copy) {
|
22884 | let dist;
|
22885 | const state = strm.state;
|
22886 |
|
22887 |
|
22888 | if (state.window === null) {
|
22889 | state.wsize = 1 << state.wbits;
|
22890 | state.wnext = 0;
|
22891 | state.whave = 0;
|
22892 |
|
22893 | state.window = new Buf8(state.wsize);
|
22894 | }
|
22895 |
|
22896 |
|
22897 | if (copy >= state.wsize) {
|
22898 | arraySet(state.window, src, end - state.wsize, state.wsize, 0);
|
22899 | state.wnext = 0;
|
22900 | state.whave = state.wsize;
|
22901 | }
|
22902 | else {
|
22903 | dist = state.wsize - state.wnext;
|
22904 | if (dist > copy) {
|
22905 | dist = copy;
|
22906 | }
|
22907 |
|
22908 | arraySet(state.window, src, end - copy, dist, state.wnext);
|
22909 | copy -= dist;
|
22910 | if (copy) {
|
22911 |
|
22912 | arraySet(state.window, src, end - copy, copy, 0);
|
22913 | state.wnext = copy;
|
22914 | state.whave = state.wsize;
|
22915 | }
|
22916 | else {
|
22917 | state.wnext += dist;
|
22918 | if (state.wnext === state.wsize) { state.wnext = 0; }
|
22919 | if (state.whave < state.wsize) { state.whave += dist; }
|
22920 | }
|
22921 | }
|
22922 | return 0;
|
22923 | }
|
22924 |
|
22925 | function inflate(strm, flush) {
|
22926 | let state;
|
22927 | let input, output;
|
22928 | let next;
|
22929 | let put;
|
22930 | let have, left;
|
22931 | let hold;
|
22932 | let bits;
|
22933 | let _in, _out;
|
22934 | let copy;
|
22935 | let from;
|
22936 | let from_source;
|
22937 | let here = 0;
|
22938 | let here_bits, here_op, here_val;
|
22939 |
|
22940 | let last_bits, last_op, last_val;
|
22941 | let len;
|
22942 | let ret;
|
22943 | let hbuf = new Buf8(4);
|
22944 | let opts;
|
22945 |
|
22946 | let n;
|
22947 |
|
22948 | const order =
|
22949 | [ 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 ];
|
22950 |
|
22951 |
|
22952 | if (!strm || !strm.state || !strm.output ||
|
22953 | (!strm.input && strm.avail_in !== 0)) {
|
22954 | return Z_STREAM_ERROR;
|
22955 | }
|
22956 |
|
22957 | state = strm.state;
|
22958 | if (state.mode === TYPE$1) { state.mode = TYPEDO; }
|
22959 |
|
22960 |
|
22961 |
|
22962 | put = strm.next_out;
|
22963 | output = strm.output;
|
22964 | left = strm.avail_out;
|
22965 | next = strm.next_in;
|
22966 | input = strm.input;
|
22967 | have = strm.avail_in;
|
22968 | hold = state.hold;
|
22969 | bits = state.bits;
|
22970 |
|
22971 |
|
22972 | _in = have;
|
22973 | _out = left;
|
22974 | ret = Z_OK;
|
22975 |
|
22976 | inf_leave:
|
22977 | for (;;) {
|
22978 | switch (state.mode) {
|
22979 | case HEAD:
|
22980 | if (state.wrap === 0) {
|
22981 | state.mode = TYPEDO;
|
22982 | break;
|
22983 | }
|
22984 |
|
22985 | while (bits < 16) {
|
22986 | if (have === 0) { break inf_leave; }
|
22987 | have--;
|
22988 | hold += input[next++] << bits;
|
22989 | bits += 8;
|
22990 | }
|
22991 |
|
22992 | if ((state.wrap & 2) && hold === 0x8b1f) {
|
22993 | state.check = 0;
|
22994 |
|
22995 | hbuf[0] = hold & 0xff;
|
22996 | hbuf[1] = (hold >>> 8) & 0xff;
|
22997 | state.check = crc32(state.check, hbuf, 2, 0);
|
22998 |
|
22999 |
|
23000 |
|
23001 | hold = 0;
|
23002 | bits = 0;
|
23003 |
|
23004 | state.mode = FLAGS;
|
23005 | break;
|
23006 | }
|
23007 | state.flags = 0;
|
23008 | if (state.head) {
|
23009 | state.head.done = false;
|
23010 | }
|
23011 | if (!(state.wrap & 1) ||
|
23012 | (((hold & 0xff) << 8) + (hold >> 8)) % 31) {
|
23013 | strm.msg = 'incorrect header check';
|
23014 | state.mode = BAD$1;
|
23015 | break;
|
23016 | }
|
23017 | if ((hold & 0x0f) !== Z_DEFLATED) {
|
23018 | strm.msg = 'unknown compression method';
|
23019 | state.mode = BAD$1;
|
23020 | break;
|
23021 | }
|
23022 |
|
23023 | hold >>>= 4;
|
23024 | bits -= 4;
|
23025 |
|
23026 | len = (hold & 0x0f) + 8;
|
23027 | if (state.wbits === 0) {
|
23028 | state.wbits = len;
|
23029 | }
|
23030 | else if (len > state.wbits) {
|
23031 | strm.msg = 'invalid window size';
|
23032 | state.mode = BAD$1;
|
23033 | break;
|
23034 | }
|
23035 | state.dmax = 1 << len;
|
23036 |
|
23037 | strm.adler = state.check = 1;
|
23038 | state.mode = hold & 0x200 ? DICTID : TYPE$1;
|
23039 |
|
23040 | hold = 0;
|
23041 | bits = 0;
|
23042 |
|
23043 | break;
|
23044 | case FLAGS:
|
23045 |
|
23046 | while (bits < 16) {
|
23047 | if (have === 0) { break inf_leave; }
|
23048 | have--;
|
23049 | hold += input[next++] << bits;
|
23050 | bits += 8;
|
23051 | }
|
23052 |
|
23053 | state.flags = hold;
|
23054 | if ((state.flags & 0xff) !== Z_DEFLATED) {
|
23055 | strm.msg = 'unknown compression method';
|
23056 | state.mode = BAD$1;
|
23057 | break;
|
23058 | }
|
23059 | if (state.flags & 0xe000) {
|
23060 | strm.msg = 'unknown header flags set';
|
23061 | state.mode = BAD$1;
|
23062 | break;
|
23063 | }
|
23064 | if (state.head) {
|
23065 | state.head.text = ((hold >> 8) & 1);
|
23066 | }
|
23067 | if (state.flags & 0x0200) {
|
23068 |
|
23069 | hbuf[0] = hold & 0xff;
|
23070 | hbuf[1] = (hold >>> 8) & 0xff;
|
23071 | state.check = crc32(state.check, hbuf, 2, 0);
|
23072 |
|
23073 | }
|
23074 |
|
23075 | hold = 0;
|
23076 | bits = 0;
|
23077 |
|
23078 | state.mode = TIME;
|
23079 |
|
23080 | case TIME:
|
23081 |
|
23082 | while (bits < 32) {
|
23083 | if (have === 0) { break inf_leave; }
|
23084 | have--;
|
23085 | hold += input[next++] << bits;
|
23086 | bits += 8;
|
23087 | }
|
23088 |
|
23089 | if (state.head) {
|
23090 | state.head.time = hold;
|
23091 | }
|
23092 | if (state.flags & 0x0200) {
|
23093 |
|
23094 | hbuf[0] = hold & 0xff;
|
23095 | hbuf[1] = (hold >>> 8) & 0xff;
|
23096 | hbuf[2] = (hold >>> 16) & 0xff;
|
23097 | hbuf[3] = (hold >>> 24) & 0xff;
|
23098 | state.check = crc32(state.check, hbuf, 4, 0);
|
23099 |
|
23100 | }
|
23101 |
|
23102 | hold = 0;
|
23103 | bits = 0;
|
23104 |
|
23105 | state.mode = OS;
|
23106 |
|
23107 | case OS:
|
23108 |
|
23109 | while (bits < 16) {
|
23110 | if (have === 0) { break inf_leave; }
|
23111 | have--;
|
23112 | hold += input[next++] << bits;
|
23113 | bits += 8;
|
23114 | }
|
23115 |
|
23116 | if (state.head) {
|
23117 | state.head.xflags = (hold & 0xff);
|
23118 | state.head.os = (hold >> 8);
|
23119 | }
|
23120 | if (state.flags & 0x0200) {
|
23121 |
|
23122 | hbuf[0] = hold & 0xff;
|
23123 | hbuf[1] = (hold >>> 8) & 0xff;
|
23124 | state.check = crc32(state.check, hbuf, 2, 0);
|
23125 |
|
23126 | }
|
23127 |
|
23128 | hold = 0;
|
23129 | bits = 0;
|
23130 |
|
23131 | state.mode = EXLEN;
|
23132 |
|
23133 | case EXLEN:
|
23134 | if (state.flags & 0x0400) {
|
23135 |
|
23136 | while (bits < 16) {
|
23137 | if (have === 0) { break inf_leave; }
|
23138 | have--;
|
23139 | hold += input[next++] << bits;
|
23140 | bits += 8;
|
23141 | }
|
23142 |
|
23143 | state.length = hold;
|
23144 | if (state.head) {
|
23145 | state.head.extra_len = hold;
|
23146 | }
|
23147 | if (state.flags & 0x0200) {
|
23148 |
|
23149 | hbuf[0] = hold & 0xff;
|
23150 | hbuf[1] = (hold >>> 8) & 0xff;
|
23151 | state.check = crc32(state.check, hbuf, 2, 0);
|
23152 |
|
23153 | }
|
23154 |
|
23155 | hold = 0;
|
23156 | bits = 0;
|
23157 |
|
23158 | }
|
23159 | else if (state.head) {
|
23160 | state.head.extra = null;
|
23161 | }
|
23162 | state.mode = EXTRA;
|
23163 |
|
23164 | case EXTRA:
|
23165 | if (state.flags & 0x0400) {
|
23166 | copy = state.length;
|
23167 | if (copy > have) { copy = have; }
|
23168 | if (copy) {
|
23169 | if (state.head) {
|
23170 | len = state.head.extra_len - state.length;
|
23171 | if (!state.head.extra) {
|
23172 |
|
23173 | state.head.extra = new Array(state.head.extra_len);
|
23174 | }
|
23175 | arraySet(
|
23176 | state.head.extra,
|
23177 | input,
|
23178 | next,
|
23179 |
|
23180 |
|
23181 | copy,
|
23182 |
|
23183 | len
|
23184 | );
|
23185 |
|
23186 |
|
23187 |
|
23188 | }
|
23189 | if (state.flags & 0x0200) {
|
23190 | state.check = crc32(state.check, input, copy, next);
|
23191 | }
|
23192 | have -= copy;
|
23193 | next += copy;
|
23194 | state.length -= copy;
|
23195 | }
|
23196 | if (state.length) { break inf_leave; }
|
23197 | }
|
23198 | state.length = 0;
|
23199 | state.mode = NAME;
|
23200 |
|
23201 | case NAME:
|
23202 | if (state.flags & 0x0800) {
|
23203 | if (have === 0) { break inf_leave; }
|
23204 | copy = 0;
|
23205 | do {
|
23206 |
|
23207 | len = input[next + copy++];
|
23208 |
|
23209 | if (state.head && len &&
|
23210 | (state.length < 65536 )) {
|
23211 | state.head.name += String.fromCharCode(len);
|
23212 | }
|
23213 | } while (len && copy < have);
|
23214 |
|
23215 | if (state.flags & 0x0200) {
|
23216 | state.check = crc32(state.check, input, copy, next);
|
23217 | }
|
23218 | have -= copy;
|
23219 | next += copy;
|
23220 | if (len) { break inf_leave; }
|
23221 | }
|
23222 | else if (state.head) {
|
23223 | state.head.name = null;
|
23224 | }
|
23225 | state.length = 0;
|
23226 | state.mode = COMMENT;
|
23227 |
|
23228 | case COMMENT:
|
23229 | if (state.flags & 0x1000) {
|
23230 | if (have === 0) { break inf_leave; }
|
23231 | copy = 0;
|
23232 | do {
|
23233 | len = input[next + copy++];
|
23234 |
|
23235 | if (state.head && len &&
|
23236 | (state.length < 65536 )) {
|
23237 | state.head.comment += String.fromCharCode(len);
|
23238 | }
|
23239 | } while (len && copy < have);
|
23240 | if (state.flags & 0x0200) {
|
23241 | state.check = crc32(state.check, input, copy, next);
|
23242 | }
|
23243 | have -= copy;
|
23244 | next += copy;
|
23245 | if (len) { break inf_leave; }
|
23246 | }
|
23247 | else if (state.head) {
|
23248 | state.head.comment = null;
|
23249 | }
|
23250 | state.mode = HCRC;
|
23251 |
|
23252 | case HCRC:
|
23253 | if (state.flags & 0x0200) {
|
23254 |
|
23255 | while (bits < 16) {
|
23256 | if (have === 0) { break inf_leave; }
|
23257 | have--;
|
23258 | hold += input[next++] << bits;
|
23259 | bits += 8;
|
23260 | }
|
23261 |
|
23262 | if (hold !== (state.check & 0xffff)) {
|
23263 | strm.msg = 'header crc mismatch';
|
23264 | state.mode = BAD$1;
|
23265 | break;
|
23266 | }
|
23267 |
|
23268 | hold = 0;
|
23269 | bits = 0;
|
23270 |
|
23271 | }
|
23272 | if (state.head) {
|
23273 | state.head.hcrc = ((state.flags >> 9) & 1);
|
23274 | state.head.done = true;
|
23275 | }
|
23276 | strm.adler = state.check = 0;
|
23277 | state.mode = TYPE$1;
|
23278 | break;
|
23279 | case DICTID:
|
23280 |
|
23281 | while (bits < 32) {
|
23282 | if (have === 0) { break inf_leave; }
|
23283 | have--;
|
23284 | hold += input[next++] << bits;
|
23285 | bits += 8;
|
23286 | }
|
23287 |
|
23288 | strm.adler = state.check = zswap32(hold);
|
23289 |
|
23290 | hold = 0;
|
23291 | bits = 0;
|
23292 |
|
23293 | state.mode = DICT;
|
23294 |
|
23295 | case DICT:
|
23296 | if (state.havedict === 0) {
|
23297 |
|
23298 | strm.next_out = put;
|
23299 | strm.avail_out = left;
|
23300 | strm.next_in = next;
|
23301 | strm.avail_in = have;
|
23302 | state.hold = hold;
|
23303 | state.bits = bits;
|
23304 |
|
23305 | return Z_NEED_DICT;
|
23306 | }
|
23307 | strm.adler = state.check = 1;
|
23308 | state.mode = TYPE$1;
|
23309 |
|
23310 | case TYPE$1:
|
23311 | if (flush === Z_BLOCK || flush === Z_TREES) { break inf_leave; }
|
23312 |
|
23313 | case TYPEDO:
|
23314 | if (state.last) {
|
23315 |
|
23316 | hold >>>= bits & 7;
|
23317 | bits -= bits & 7;
|
23318 |
|
23319 | state.mode = CHECK;
|
23320 | break;
|
23321 | }
|
23322 |
|
23323 | while (bits < 3) {
|
23324 | if (have === 0) { break inf_leave; }
|
23325 | have--;
|
23326 | hold += input[next++] << bits;
|
23327 | bits += 8;
|
23328 | }
|
23329 |
|
23330 | state.last = (hold & 0x01);
|
23331 |
|
23332 | hold >>>= 1;
|
23333 | bits -= 1;
|
23334 |
|
23335 |
|
23336 | switch ((hold & 0x03)) {
|
23337 | case 0:
|
23338 |
|
23339 |
|
23340 | state.mode = STORED;
|
23341 | break;
|
23342 | case 1:
|
23343 | fixedtables(state);
|
23344 |
|
23345 |
|
23346 | state.mode = LEN_;
|
23347 | if (flush === Z_TREES) {
|
23348 |
|
23349 | hold >>>= 2;
|
23350 | bits -= 2;
|
23351 |
|
23352 | break inf_leave;
|
23353 | }
|
23354 | break;
|
23355 | case 2:
|
23356 |
|
23357 |
|
23358 | state.mode = TABLE;
|
23359 | break;
|
23360 | case 3:
|
23361 | strm.msg = 'invalid block type';
|
23362 | state.mode = BAD$1;
|
23363 | }
|
23364 |
|
23365 | hold >>>= 2;
|
23366 | bits -= 2;
|
23367 |
|
23368 | break;
|
23369 | case STORED:
|
23370 |
|
23371 | hold >>>= bits & 7;
|
23372 | bits -= bits & 7;
|
23373 |
|
23374 |
|
23375 | while (bits < 32) {
|
23376 | if (have === 0) { break inf_leave; }
|
23377 | have--;
|
23378 | hold += input[next++] << bits;
|
23379 | bits += 8;
|
23380 | }
|
23381 |
|
23382 | if ((hold & 0xffff) !== ((hold >>> 16) ^ 0xffff)) {
|
23383 | strm.msg = 'invalid stored block lengths';
|
23384 | state.mode = BAD$1;
|
23385 | break;
|
23386 | }
|
23387 | state.length = hold & 0xffff;
|
23388 |
|
23389 |
|
23390 |
|
23391 | hold = 0;
|
23392 | bits = 0;
|
23393 |
|
23394 | state.mode = COPY_;
|
23395 | if (flush === Z_TREES) { break inf_leave; }
|
23396 |
|
23397 | case COPY_:
|
23398 | state.mode = COPY;
|
23399 |
|
23400 | case COPY:
|
23401 | copy = state.length;
|
23402 | if (copy) {
|
23403 | if (copy > have) { copy = have; }
|
23404 | if (copy > left) { copy = left; }
|
23405 | if (copy === 0) { break inf_leave; }
|
23406 |
|
23407 | arraySet(output, input, next, copy, put);
|
23408 |
|
23409 | have -= copy;
|
23410 | next += copy;
|
23411 | left -= copy;
|
23412 | put += copy;
|
23413 | state.length -= copy;
|
23414 | break;
|
23415 | }
|
23416 |
|
23417 | state.mode = TYPE$1;
|
23418 | break;
|
23419 | case TABLE:
|
23420 |
|
23421 | while (bits < 14) {
|
23422 | if (have === 0) { break inf_leave; }
|
23423 | have--;
|
23424 | hold += input[next++] << bits;
|
23425 | bits += 8;
|
23426 | }
|
23427 |
|
23428 | state.nlen = (hold & 0x1f) + 257;
|
23429 |
|
23430 | hold >>>= 5;
|
23431 | bits -= 5;
|
23432 |
|
23433 | state.ndist = (hold & 0x1f) + 1;
|
23434 |
|
23435 | hold >>>= 5;
|
23436 | bits -= 5;
|
23437 |
|
23438 | state.ncode = (hold & 0x0f) + 4;
|
23439 |
|
23440 | hold >>>= 4;
|
23441 | bits -= 4;
|
23442 |
|
23443 |
|
23444 | if (state.nlen > 286 || state.ndist > 30) {
|
23445 | strm.msg = 'too many length or distance symbols';
|
23446 | state.mode = BAD$1;
|
23447 | break;
|
23448 | }
|
23449 |
|
23450 |
|
23451 | state.have = 0;
|
23452 | state.mode = LENLENS;
|
23453 |
|
23454 | case LENLENS:
|
23455 | while (state.have < state.ncode) {
|
23456 |
|
23457 | while (bits < 3) {
|
23458 | if (have === 0) { break inf_leave; }
|
23459 | have--;
|
23460 | hold += input[next++] << bits;
|
23461 | bits += 8;
|
23462 | }
|
23463 |
|
23464 | state.lens[order[state.have++]] = (hold & 0x07);
|
23465 |
|
23466 | hold >>>= 3;
|
23467 | bits -= 3;
|
23468 |
|
23469 | }
|
23470 | while (state.have < 19) {
|
23471 | state.lens[order[state.have++]] = 0;
|
23472 | }
|
23473 |
|
23474 |
|
23475 |
|
23476 |
|
23477 | state.lencode = state.lendyn;
|
23478 | state.lenbits = 7;
|
23479 |
|
23480 | opts = { bits: state.lenbits };
|
23481 | ret = inflate_table(CODES$1, state.lens, 0, 19, state.lencode, 0, state.work, opts);
|
23482 | state.lenbits = opts.bits;
|
23483 |
|
23484 | if (ret) {
|
23485 | strm.msg = 'invalid code lengths set';
|
23486 | state.mode = BAD$1;
|
23487 | break;
|
23488 | }
|
23489 |
|
23490 | state.have = 0;
|
23491 | state.mode = CODELENS;
|
23492 |
|
23493 | case CODELENS:
|
23494 | while (state.have < state.nlen + state.ndist) {
|
23495 | for (;;) {
|
23496 | here = state.lencode[hold & ((1 << state.lenbits) - 1)];
|
23497 | here_bits = here >>> 24;
|
23498 | here_op = (here >>> 16) & 0xff;
|
23499 | here_val = here & 0xffff;
|
23500 |
|
23501 | if ((here_bits) <= bits) { break; }
|
23502 |
|
23503 | if (have === 0) { break inf_leave; }
|
23504 | have--;
|
23505 | hold += input[next++] << bits;
|
23506 | bits += 8;
|
23507 |
|
23508 | }
|
23509 | if (here_val < 16) {
|
23510 |
|
23511 | hold >>>= here_bits;
|
23512 | bits -= here_bits;
|
23513 |
|
23514 | state.lens[state.have++] = here_val;
|
23515 | }
|
23516 | else {
|
23517 | if (here_val === 16) {
|
23518 |
|
23519 | n = here_bits + 2;
|
23520 | while (bits < n) {
|
23521 | if (have === 0) { break inf_leave; }
|
23522 | have--;
|
23523 | hold += input[next++] << bits;
|
23524 | bits += 8;
|
23525 | }
|
23526 |
|
23527 |
|
23528 | hold >>>= here_bits;
|
23529 | bits -= here_bits;
|
23530 |
|
23531 | if (state.have === 0) {
|
23532 | strm.msg = 'invalid bit length repeat';
|
23533 | state.mode = BAD$1;
|
23534 | break;
|
23535 | }
|
23536 | len = state.lens[state.have - 1];
|
23537 | copy = 3 + (hold & 0x03);
|
23538 |
|
23539 | hold >>>= 2;
|
23540 | bits -= 2;
|
23541 |
|
23542 | }
|
23543 | else if (here_val === 17) {
|
23544 |
|
23545 | n = here_bits + 3;
|
23546 | while (bits < n) {
|
23547 | if (have === 0) { break inf_leave; }
|
23548 | have--;
|
23549 | hold += input[next++] << bits;
|
23550 | bits += 8;
|
23551 | }
|
23552 |
|
23553 |
|
23554 | hold >>>= here_bits;
|
23555 | bits -= here_bits;
|
23556 |
|
23557 | len = 0;
|
23558 | copy = 3 + (hold & 0x07);
|
23559 |
|
23560 | hold >>>= 3;
|
23561 | bits -= 3;
|
23562 |
|
23563 | }
|
23564 | else {
|
23565 |
|
23566 | n = here_bits + 7;
|
23567 | while (bits < n) {
|
23568 | if (have === 0) { break inf_leave; }
|
23569 | have--;
|
23570 | hold += input[next++] << bits;
|
23571 | bits += 8;
|
23572 | }
|
23573 |
|
23574 |
|
23575 | hold >>>= here_bits;
|
23576 | bits -= here_bits;
|
23577 |
|
23578 | len = 0;
|
23579 | copy = 11 + (hold & 0x7f);
|
23580 |
|
23581 | hold >>>= 7;
|
23582 | bits -= 7;
|
23583 |
|
23584 | }
|
23585 | if (state.have + copy > state.nlen + state.ndist) {
|
23586 | strm.msg = 'invalid bit length repeat';
|
23587 | state.mode = BAD$1;
|
23588 | break;
|
23589 | }
|
23590 | while (copy--) {
|
23591 | state.lens[state.have++] = len;
|
23592 | }
|
23593 | }
|
23594 | }
|
23595 |
|
23596 |
|
23597 | if (state.mode === BAD$1) { break; }
|
23598 |
|
23599 |
|
23600 | if (state.lens[256] === 0) {
|
23601 | strm.msg = 'invalid code -- missing end-of-block';
|
23602 | state.mode = BAD$1;
|
23603 | break;
|
23604 | }
|
23605 |
|
23606 | |
23607 |
|
23608 |
|
23609 | state.lenbits = 9;
|
23610 |
|
23611 | opts = { bits: state.lenbits };
|
23612 | ret = inflate_table(LENS$1, state.lens, 0, state.nlen, state.lencode, 0, state.work, opts);
|
23613 |
|
23614 |
|
23615 | state.lenbits = opts.bits;
|
23616 |
|
23617 |
|
23618 | if (ret) {
|
23619 | strm.msg = 'invalid literal/lengths set';
|
23620 | state.mode = BAD$1;
|
23621 | break;
|
23622 | }
|
23623 |
|
23624 | state.distbits = 6;
|
23625 |
|
23626 |
|
23627 | state.distcode = state.distdyn;
|
23628 | opts = { bits: state.distbits };
|
23629 | ret = inflate_table(DISTS$1, state.lens, state.nlen, state.ndist, state.distcode, 0, state.work, opts);
|
23630 |
|
23631 |
|
23632 | state.distbits = opts.bits;
|
23633 |
|
23634 |
|
23635 | if (ret) {
|
23636 | strm.msg = 'invalid distances set';
|
23637 | state.mode = BAD$1;
|
23638 | break;
|
23639 | }
|
23640 |
|
23641 | state.mode = LEN_;
|
23642 | if (flush === Z_TREES) { break inf_leave; }
|
23643 |
|
23644 | case LEN_:
|
23645 | state.mode = LEN;
|
23646 |
|
23647 | case LEN:
|
23648 | if (have >= 6 && left >= 258) {
|
23649 |
|
23650 | strm.next_out = put;
|
23651 | strm.avail_out = left;
|
23652 | strm.next_in = next;
|
23653 | strm.avail_in = have;
|
23654 | state.hold = hold;
|
23655 | state.bits = bits;
|
23656 |
|
23657 | inflate_fast(strm, _out);
|
23658 |
|
23659 | put = strm.next_out;
|
23660 | output = strm.output;
|
23661 | left = strm.avail_out;
|
23662 | next = strm.next_in;
|
23663 | input = strm.input;
|
23664 | have = strm.avail_in;
|
23665 | hold = state.hold;
|
23666 | bits = state.bits;
|
23667 |
|
23668 |
|
23669 | if (state.mode === TYPE$1) {
|
23670 | state.back = -1;
|
23671 | }
|
23672 | break;
|
23673 | }
|
23674 | state.back = 0;
|
23675 | for (;;) {
|
23676 | here = state.lencode[hold & ((1 << state.lenbits) - 1)];
|
23677 | here_bits = here >>> 24;
|
23678 | here_op = (here >>> 16) & 0xff;
|
23679 | here_val = here & 0xffff;
|
23680 |
|
23681 | if (here_bits <= bits) { break; }
|
23682 |
|
23683 | if (have === 0) { break inf_leave; }
|
23684 | have--;
|
23685 | hold += input[next++] << bits;
|
23686 | bits += 8;
|
23687 |
|
23688 | }
|
23689 | if (here_op && (here_op & 0xf0) === 0) {
|
23690 | last_bits = here_bits;
|
23691 | last_op = here_op;
|
23692 | last_val = here_val;
|
23693 | for (;;) {
|
23694 | here = state.lencode[last_val +
|
23695 | ((hold & ((1 << (last_bits + last_op)) - 1)) >> last_bits)];
|
23696 | here_bits = here >>> 24;
|
23697 | here_op = (here >>> 16) & 0xff;
|
23698 | here_val = here & 0xffff;
|
23699 |
|
23700 | if ((last_bits + here_bits) <= bits) { break; }
|
23701 |
|
23702 | if (have === 0) { break inf_leave; }
|
23703 | have--;
|
23704 | hold += input[next++] << bits;
|
23705 | bits += 8;
|
23706 |
|
23707 | }
|
23708 |
|
23709 | hold >>>= last_bits;
|
23710 | bits -= last_bits;
|
23711 |
|
23712 | state.back += last_bits;
|
23713 | }
|
23714 |
|
23715 | hold >>>= here_bits;
|
23716 | bits -= here_bits;
|
23717 |
|
23718 | state.back += here_bits;
|
23719 | state.length = here_val;
|
23720 | if (here_op === 0) {
|
23721 |
|
23722 |
|
23723 |
|
23724 | state.mode = LIT;
|
23725 | break;
|
23726 | }
|
23727 | if (here_op & 32) {
|
23728 |
|
23729 | state.back = -1;
|
23730 | state.mode = TYPE$1;
|
23731 | break;
|
23732 | }
|
23733 | if (here_op & 64) {
|
23734 | strm.msg = 'invalid literal/length code';
|
23735 | state.mode = BAD$1;
|
23736 | break;
|
23737 | }
|
23738 | state.extra = here_op & 15;
|
23739 | state.mode = LENEXT;
|
23740 |
|
23741 | case LENEXT:
|
23742 | if (state.extra) {
|
23743 |
|
23744 | n = state.extra;
|
23745 | while (bits < n) {
|
23746 | if (have === 0) { break inf_leave; }
|
23747 | have--;
|
23748 | hold += input[next++] << bits;
|
23749 | bits += 8;
|
23750 | }
|
23751 |
|
23752 | state.length += hold & ((1 << state.extra) - 1);
|
23753 |
|
23754 | hold >>>= state.extra;
|
23755 | bits -= state.extra;
|
23756 |
|
23757 | state.back += state.extra;
|
23758 | }
|
23759 |
|
23760 | state.was = state.length;
|
23761 | state.mode = DIST;
|
23762 |
|
23763 | case DIST:
|
23764 | for (;;) {
|
23765 | here = state.distcode[hold & ((1 << state.distbits) - 1)];
|
23766 | here_bits = here >>> 24;
|
23767 | here_op = (here >>> 16) & 0xff;
|
23768 | here_val = here & 0xffff;
|
23769 |
|
23770 | if ((here_bits) <= bits) { break; }
|
23771 |
|
23772 | if (have === 0) { break inf_leave; }
|
23773 | have--;
|
23774 | hold += input[next++] << bits;
|
23775 | bits += 8;
|
23776 |
|
23777 | }
|
23778 | if ((here_op & 0xf0) === 0) {
|
23779 | last_bits = here_bits;
|
23780 | last_op = here_op;
|
23781 | last_val = here_val;
|
23782 | for (;;) {
|
23783 | here = state.distcode[last_val +
|
23784 | ((hold & ((1 << (last_bits + last_op)) - 1)) >> last_bits)];
|
23785 | here_bits = here >>> 24;
|
23786 | here_op = (here >>> 16) & 0xff;
|
23787 | here_val = here & 0xffff;
|
23788 |
|
23789 | if ((last_bits + here_bits) <= bits) { break; }
|
23790 |
|
23791 | if (have === 0) { break inf_leave; }
|
23792 | have--;
|
23793 | hold += input[next++] << bits;
|
23794 | bits += 8;
|
23795 |
|
23796 | }
|
23797 |
|
23798 | hold >>>= last_bits;
|
23799 | bits -= last_bits;
|
23800 |
|
23801 | state.back += last_bits;
|
23802 | }
|
23803 |
|
23804 | hold >>>= here_bits;
|
23805 | bits -= here_bits;
|
23806 |
|
23807 | state.back += here_bits;
|
23808 | if (here_op & 64) {
|
23809 | strm.msg = 'invalid distance code';
|
23810 | state.mode = BAD$1;
|
23811 | break;
|
23812 | }
|
23813 | state.offset = here_val;
|
23814 | state.extra = (here_op) & 15;
|
23815 | state.mode = DISTEXT;
|
23816 |
|
23817 | case DISTEXT:
|
23818 | if (state.extra) {
|
23819 |
|
23820 | n = state.extra;
|
23821 | while (bits < n) {
|
23822 | if (have === 0) { break inf_leave; }
|
23823 | have--;
|
23824 | hold += input[next++] << bits;
|
23825 | bits += 8;
|
23826 | }
|
23827 |
|
23828 | state.offset += hold & ((1 << state.extra) - 1);
|
23829 |
|
23830 | hold >>>= state.extra;
|
23831 | bits -= state.extra;
|
23832 |
|
23833 | state.back += state.extra;
|
23834 | }
|
23835 |
|
23836 | if (state.offset > state.dmax) {
|
23837 | strm.msg = 'invalid distance too far back';
|
23838 | state.mode = BAD$1;
|
23839 | break;
|
23840 | }
|
23841 |
|
23842 |
|
23843 | state.mode = MATCH;
|
23844 |
|
23845 | case MATCH:
|
23846 | if (left === 0) { break inf_leave; }
|
23847 | copy = _out - left;
|
23848 | if (state.offset > copy) {
|
23849 | copy = state.offset - copy;
|
23850 | if (copy > state.whave) {
|
23851 | if (state.sane) {
|
23852 | strm.msg = 'invalid distance too far back';
|
23853 | state.mode = BAD$1;
|
23854 | break;
|
23855 | }
|
23856 |
|
23857 |
|
23858 |
|
23859 |
|
23860 |
|
23861 |
|
23862 |
|
23863 |
|
23864 |
|
23865 |
|
23866 |
|
23867 |
|
23868 |
|
23869 |
|
23870 |
|
23871 | }
|
23872 | if (copy > state.wnext) {
|
23873 | copy -= state.wnext;
|
23874 | from = state.wsize - copy;
|
23875 | }
|
23876 | else {
|
23877 | from = state.wnext - copy;
|
23878 | }
|
23879 | if (copy > state.length) { copy = state.length; }
|
23880 | from_source = state.window;
|
23881 | }
|
23882 | else {
|
23883 | from_source = output;
|
23884 | from = put - state.offset;
|
23885 | copy = state.length;
|
23886 | }
|
23887 | if (copy > left) { copy = left; }
|
23888 | left -= copy;
|
23889 | state.length -= copy;
|
23890 | do {
|
23891 | output[put++] = from_source[from++];
|
23892 | } while (--copy);
|
23893 | if (state.length === 0) { state.mode = LEN; }
|
23894 | break;
|
23895 | case LIT:
|
23896 | if (left === 0) { break inf_leave; }
|
23897 | output[put++] = state.length;
|
23898 | left--;
|
23899 | state.mode = LEN;
|
23900 | break;
|
23901 | case CHECK:
|
23902 | if (state.wrap) {
|
23903 |
|
23904 | while (bits < 32) {
|
23905 | if (have === 0) { break inf_leave; }
|
23906 | have--;
|
23907 |
|
23908 | hold |= input[next++] << bits;
|
23909 | bits += 8;
|
23910 | }
|
23911 |
|
23912 | _out -= left;
|
23913 | strm.total_out += _out;
|
23914 | state.total += _out;
|
23915 | if (_out) {
|
23916 | strm.adler = state.check =
|
23917 |
|
23918 | (state.flags ? crc32(state.check, output, _out, put - _out) : adler32(state.check, output, _out, put - _out));
|
23919 |
|
23920 | }
|
23921 | _out = left;
|
23922 |
|
23923 | if ((state.flags ? hold : zswap32(hold)) !== state.check) {
|
23924 | strm.msg = 'incorrect data check';
|
23925 | state.mode = BAD$1;
|
23926 | break;
|
23927 | }
|
23928 |
|
23929 | hold = 0;
|
23930 | bits = 0;
|
23931 |
|
23932 |
|
23933 | }
|
23934 | state.mode = LENGTH;
|
23935 |
|
23936 | case LENGTH:
|
23937 | if (state.wrap && state.flags) {
|
23938 |
|
23939 | while (bits < 32) {
|
23940 | if (have === 0) { break inf_leave; }
|
23941 | have--;
|
23942 | hold += input[next++] << bits;
|
23943 | bits += 8;
|
23944 | }
|
23945 |
|
23946 | if (hold !== (state.total & 0xffffffff)) {
|
23947 | strm.msg = 'incorrect length check';
|
23948 | state.mode = BAD$1;
|
23949 | break;
|
23950 | }
|
23951 |
|
23952 | hold = 0;
|
23953 | bits = 0;
|
23954 |
|
23955 |
|
23956 | }
|
23957 | state.mode = DONE;
|
23958 |
|
23959 | case DONE:
|
23960 | ret = Z_STREAM_END;
|
23961 | break inf_leave;
|
23962 | case BAD$1:
|
23963 | ret = Z_DATA_ERROR;
|
23964 | break inf_leave;
|
23965 |
|
23966 |
|
23967 | case SYNC:
|
23968 |
|
23969 | default:
|
23970 | return Z_STREAM_ERROR;
|
23971 | }
|
23972 | }
|
23973 |
|
23974 |
|
23975 |
|
23976 | |
23977 |
|
23978 |
|
23979 |
|
23980 |
|
23981 |
|
23982 |
|
23983 |
|
23984 | strm.next_out = put;
|
23985 | strm.avail_out = left;
|
23986 | strm.next_in = next;
|
23987 | strm.avail_in = have;
|
23988 | state.hold = hold;
|
23989 | state.bits = bits;
|
23990 |
|
23991 |
|
23992 | if (state.wsize || (_out !== strm.avail_out && state.mode < BAD$1 &&
|
23993 | (state.mode < CHECK || flush !== Z_FINISH))) {
|
23994 | if (updatewindow(strm, strm.output, strm.next_out, _out - strm.avail_out)) ;
|
23995 | }
|
23996 | _in -= strm.avail_in;
|
23997 | _out -= strm.avail_out;
|
23998 | strm.total_in += _in;
|
23999 | strm.total_out += _out;
|
24000 | state.total += _out;
|
24001 | if (state.wrap && _out) {
|
24002 | strm.adler = state.check =
|
24003 | (state.flags ? crc32(state.check, output, _out, strm.next_out - _out) : adler32(state.check, output, _out, strm.next_out - _out));
|
24004 | }
|
24005 | strm.data_type = state.bits + (state.last ? 64 : 0) +
|
24006 | (state.mode === TYPE$1 ? 128 : 0) +
|
24007 | (state.mode === LEN_ || state.mode === COPY_ ? 256 : 0);
|
24008 | if (((_in === 0 && _out === 0) || flush === Z_FINISH) && ret === Z_OK) {
|
24009 | ret = Z_BUF_ERROR;
|
24010 | }
|
24011 | return ret;
|
24012 | }
|
24013 |
|
24014 | function inflateEnd(strm) {
|
24015 |
|
24016 | if (!strm || !strm.state ) {
|
24017 | return Z_STREAM_ERROR;
|
24018 | }
|
24019 |
|
24020 | const state = strm.state;
|
24021 | if (state.window) {
|
24022 | state.window = null;
|
24023 | }
|
24024 | strm.state = null;
|
24025 | return Z_OK;
|
24026 | }
|
24027 |
|
24028 | function inflateGetHeader(strm, head) {
|
24029 | let state;
|
24030 |
|
24031 |
|
24032 | if (!strm || !strm.state) { return Z_STREAM_ERROR; }
|
24033 | state = strm.state;
|
24034 | if ((state.wrap & 2) === 0) { return Z_STREAM_ERROR; }
|
24035 |
|
24036 |
|
24037 | state.head = head;
|
24038 | head.done = false;
|
24039 | return Z_OK;
|
24040 | }
|
24041 |
|
24042 | function inflateSetDictionary(strm, dictionary) {
|
24043 | const dictLength = dictionary.length;
|
24044 |
|
24045 | let state;
|
24046 | let dictid;
|
24047 |
|
24048 |
|
24049 | if (!strm || !strm.state ) { return Z_STREAM_ERROR; }
|
24050 | state = strm.state;
|
24051 |
|
24052 | if (state.wrap !== 0 && state.mode !== DICT) {
|
24053 | return Z_STREAM_ERROR;
|
24054 | }
|
24055 |
|
24056 |
|
24057 | if (state.mode === DICT) {
|
24058 | dictid = 1;
|
24059 |
|
24060 | dictid = adler32(dictid, dictionary, dictLength, 0);
|
24061 | if (dictid !== state.check) {
|
24062 | return Z_DATA_ERROR;
|
24063 | }
|
24064 | }
|
24065 | |
24066 |
|
24067 | updatewindow(strm, dictionary, dictLength, dictLength);
|
24068 |
|
24069 |
|
24070 |
|
24071 |
|
24072 | state.havedict = 1;
|
24073 |
|
24074 | return Z_OK;
|
24075 | }
|
24076 |
|
24077 |
|
24078 |
|
24079 |
|
24080 |
|
24081 |
|
24082 |
|
24083 |
|
24084 |
|
24085 |
|
24086 |
|
24087 |
|
24088 |
|
24089 |
|
24090 |
|
24091 |
|
24092 |
|
24093 |
|
24094 |
|
24095 |
|
24096 |
|
24097 |
|
24098 |
|
24099 |
|
24100 |
|
24101 |
|
24102 |
|
24103 |
|
24104 |
|
24105 |
|
24106 | class GZheader {
|
24107 | constructor() {
|
24108 |
|
24109 | this.text = 0;
|
24110 |
|
24111 | this.time = 0;
|
24112 |
|
24113 | this.xflags = 0;
|
24114 |
|
24115 | this.os = 0;
|
24116 |
|
24117 | this.extra = null;
|
24118 |
|
24119 | this.extra_len = 0;
|
24120 |
|
24121 |
|
24122 |
|
24123 |
|
24124 |
|
24125 |
|
24126 |
|
24127 |
|
24128 |
|
24129 |
|
24130 | this.name = '';
|
24131 |
|
24132 |
|
24133 |
|
24134 | this.comment = '';
|
24135 |
|
24136 |
|
24137 |
|
24138 | this.hcrc = 0;
|
24139 |
|
24140 | this.done = false;
|
24141 | }
|
24142 | }
|
24143 |
|
24144 |
|
24145 |
|
24146 |
|
24147 |
|
24148 |
|
24149 |
|
24150 |
|
24151 |
|
24152 |
|
24153 |
|
24154 |
|
24155 |
|
24156 |
|
24157 |
|
24158 |
|
24159 |
|
24160 |
|
24161 |
|
24162 |
|
24163 |
|
24164 |
|
24165 |
|
24166 |
|
24167 |
|
24168 |
|
24169 |
|
24170 |
|
24171 |
|
24172 |
|
24173 |
|
24174 |
|
24175 |
|
24176 |
|
24177 |
|
24178 |
|
24179 |
|
24180 |
|
24181 |
|
24182 |
|
24183 |
|
24184 |
|
24185 |
|
24186 |
|
24187 |
|
24188 |
|
24189 |
|
24190 |
|
24191 |
|
24192 |
|
24193 |
|
24194 |
|
24195 |
|
24196 |
|
24197 |
|
24198 |
|
24199 |
|
24200 |
|
24201 |
|
24202 |
|
24203 |
|
24204 |
|
24205 |
|
24206 |
|
24207 |
|
24208 |
|
24209 |
|
24210 |
|
24211 |
|
24212 |
|
24213 |
|
24214 |
|
24215 |
|
24216 |
|
24217 |
|
24218 |
|
24219 |
|
24220 |
|
24221 |
|
24222 |
|
24223 | class Inflate {
|
24224 | constructor(options) {
|
24225 | this.options = {
|
24226 | chunkSize: 16384,
|
24227 | windowBits: 0,
|
24228 | ...(options || {})
|
24229 | };
|
24230 |
|
24231 | const opt = this.options;
|
24232 |
|
24233 |
|
24234 |
|
24235 | if (opt.raw && (opt.windowBits >= 0) && (opt.windowBits < 16)) {
|
24236 | opt.windowBits = -opt.windowBits;
|
24237 | if (opt.windowBits === 0) { opt.windowBits = -15; }
|
24238 | }
|
24239 |
|
24240 |
|
24241 | if ((opt.windowBits >= 0) && (opt.windowBits < 16) &&
|
24242 | !(options && options.windowBits)) {
|
24243 | opt.windowBits += 32;
|
24244 | }
|
24245 |
|
24246 |
|
24247 |
|
24248 | if ((opt.windowBits > 15) && (opt.windowBits < 48)) {
|
24249 |
|
24250 |
|
24251 | if ((opt.windowBits & 15) === 0) {
|
24252 | opt.windowBits |= 15;
|
24253 | }
|
24254 | }
|
24255 |
|
24256 | this.err = 0;
|
24257 | this.msg = '';
|
24258 | this.ended = false;
|
24259 | this.chunks = [];
|
24260 |
|
24261 | this.strm = new ZStream();
|
24262 | this.strm.avail_out = 0;
|
24263 |
|
24264 | let status = inflateInit2(
|
24265 | this.strm,
|
24266 | opt.windowBits
|
24267 | );
|
24268 |
|
24269 | if (status !== Z_OK) {
|
24270 | throw new Error(msg[status]);
|
24271 | }
|
24272 |
|
24273 | this.header = new GZheader();
|
24274 |
|
24275 | inflateGetHeader(this.strm, this.header);
|
24276 |
|
24277 |
|
24278 | if (opt.dictionary) {
|
24279 |
|
24280 | if (typeof opt.dictionary === 'string') {
|
24281 | opt.dictionary = string2buf(opt.dictionary);
|
24282 | } else if (opt.dictionary instanceof ArrayBuffer) {
|
24283 | opt.dictionary = new Uint8Array(opt.dictionary);
|
24284 | }
|
24285 | if (opt.raw) {
|
24286 | status = inflateSetDictionary(this.strm, opt.dictionary);
|
24287 | if (status !== Z_OK) {
|
24288 | throw new Error(msg[status]);
|
24289 | }
|
24290 | }
|
24291 | }
|
24292 | }
|
24293 | |
24294 |
|
24295 |
|
24296 |
|
24297 |
|
24298 |
|
24299 |
|
24300 |
|
24301 |
|
24302 |
|
24303 |
|
24304 |
|
24305 |
|
24306 |
|
24307 |
|
24308 |
|
24309 |
|
24310 |
|
24311 |
|
24312 |
|
24313 |
|
24314 |
|
24315 |
|
24316 |
|
24317 |
|
24318 |
|
24319 |
|
24320 |
|
24321 | push(data, mode) {
|
24322 | const { strm, options: { chunkSize, dictionary } } = this;
|
24323 | let status, _mode;
|
24324 |
|
24325 |
|
24326 |
|
24327 | let allowBufError = false;
|
24328 |
|
24329 | if (this.ended) { return false; }
|
24330 | _mode = (mode === ~~mode) ? mode : ((mode === true) ? Z_FINISH : Z_NO_FLUSH);
|
24331 |
|
24332 |
|
24333 | if (typeof data === 'string') {
|
24334 |
|
24335 | strm.input = binstring2buf(data);
|
24336 | } else if (data instanceof ArrayBuffer) {
|
24337 | strm.input = new Uint8Array(data);
|
24338 | } else {
|
24339 | strm.input = data;
|
24340 | }
|
24341 |
|
24342 | strm.next_in = 0;
|
24343 | strm.avail_in = strm.input.length;
|
24344 |
|
24345 | do {
|
24346 | if (strm.avail_out === 0) {
|
24347 | strm.output = new Buf8(chunkSize);
|
24348 | strm.next_out = 0;
|
24349 | strm.avail_out = chunkSize;
|
24350 | }
|
24351 |
|
24352 | status = inflate(strm, Z_NO_FLUSH);
|
24353 |
|
24354 | if (status === Z_NEED_DICT && dictionary) {
|
24355 | status = inflateSetDictionary(this.strm, dictionary);
|
24356 | }
|
24357 |
|
24358 | if (status === Z_BUF_ERROR && allowBufError === true) {
|
24359 | status = Z_OK;
|
24360 | allowBufError = false;
|
24361 | }
|
24362 |
|
24363 | if (status !== Z_STREAM_END && status !== Z_OK) {
|
24364 | this.onEnd(status);
|
24365 | this.ended = true;
|
24366 | return false;
|
24367 | }
|
24368 |
|
24369 | if (strm.next_out) {
|
24370 | if (strm.avail_out === 0 || status === Z_STREAM_END || (strm.avail_in === 0 && (_mode === Z_FINISH || _mode === Z_SYNC_FLUSH))) {
|
24371 | this.onData(shrinkBuf(strm.output, strm.next_out));
|
24372 | }
|
24373 | }
|
24374 |
|
24375 |
|
24376 |
|
24377 |
|
24378 |
|
24379 |
|
24380 |
|
24381 |
|
24382 | if (strm.avail_in === 0 && strm.avail_out === 0) {
|
24383 | allowBufError = true;
|
24384 | }
|
24385 |
|
24386 | } while ((strm.avail_in > 0 || strm.avail_out === 0) && status !== Z_STREAM_END);
|
24387 |
|
24388 | if (status === Z_STREAM_END) {
|
24389 | _mode = Z_FINISH;
|
24390 | }
|
24391 |
|
24392 |
|
24393 | if (_mode === Z_FINISH) {
|
24394 | status = inflateEnd(this.strm);
|
24395 | this.onEnd(status);
|
24396 | this.ended = true;
|
24397 | return status === Z_OK;
|
24398 | }
|
24399 |
|
24400 |
|
24401 | if (_mode === Z_SYNC_FLUSH) {
|
24402 | this.onEnd(Z_OK);
|
24403 | strm.avail_out = 0;
|
24404 | return true;
|
24405 | }
|
24406 |
|
24407 | return true;
|
24408 | };
|
24409 |
|
24410 | |
24411 |
|
24412 |
|
24413 |
|
24414 |
|
24415 |
|
24416 |
|
24417 |
|
24418 |
|
24419 | onData(chunk) {
|
24420 | this.chunks.push(chunk);
|
24421 | };
|
24422 |
|
24423 |
|
24424 |
|
24425 | |
24426 |
|
24427 |
|
24428 |
|
24429 |
|
24430 |
|
24431 |
|
24432 |
|
24433 |
|
24434 |
|
24435 | onEnd(status) {
|
24436 |
|
24437 | if (status === Z_OK) {
|
24438 | this.result = flattenChunks(this.chunks);
|
24439 | }
|
24440 | this.chunks = [];
|
24441 | this.err = status;
|
24442 | this.msg = this.strm.msg;
|
24443 | };
|
24444 | }
|
24445 |
|
24446 |
|
24447 |
|
24448 |
|
24449 |
|
24450 |
|
24451 |
|
24452 |
|
24453 |
|
24454 |
|
24455 |
|
24456 |
|
24457 |
|
24458 |
|
24459 |
|
24460 |
|
24461 |
|
24462 |
|
24463 |
|
24464 |
|
24465 |
|
24466 |
|
24467 |
|
24468 |
|
24469 |
|
24470 |
|
24471 |
|
24472 |
|
24473 |
|
24474 |
|
24475 | var BITMASK = [0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF];
|
24476 |
|
24477 |
|
24478 | var BitReader = function(stream) {
|
24479 | this.stream = stream;
|
24480 | this.bitOffset = 0;
|
24481 | this.curByte = 0;
|
24482 | this.hasByte = false;
|
24483 | };
|
24484 |
|
24485 | BitReader.prototype._ensureByte = function() {
|
24486 | if (!this.hasByte) {
|
24487 | this.curByte = this.stream.readByte();
|
24488 | this.hasByte = true;
|
24489 | }
|
24490 | };
|
24491 |
|
24492 |
|
24493 | BitReader.prototype.read = function(bits) {
|
24494 | var result = 0;
|
24495 | while (bits > 0) {
|
24496 | this._ensureByte();
|
24497 | var remaining = 8 - this.bitOffset;
|
24498 |
|
24499 | if (bits >= remaining) {
|
24500 | result <<= remaining;
|
24501 | result |= BITMASK[remaining] & this.curByte;
|
24502 | this.hasByte = false;
|
24503 | this.bitOffset = 0;
|
24504 | bits -= remaining;
|
24505 | } else {
|
24506 | result <<= bits;
|
24507 | var shift = remaining - bits;
|
24508 | result |= (this.curByte & (BITMASK[bits] << shift)) >> shift;
|
24509 | this.bitOffset += bits;
|
24510 | bits = 0;
|
24511 | }
|
24512 | }
|
24513 | return result;
|
24514 | };
|
24515 |
|
24516 |
|
24517 | BitReader.prototype.seek = function(pos) {
|
24518 | var n_bit = pos % 8;
|
24519 | var n_byte = (pos - n_bit) / 8;
|
24520 | this.bitOffset = n_bit;
|
24521 | this.stream.seek(n_byte);
|
24522 | this.hasByte = false;
|
24523 | };
|
24524 |
|
24525 |
|
24526 | BitReader.prototype.pi = function() {
|
24527 | var buf = new Uint8Array(6), i;
|
24528 | for (i = 0; i < buf.length; i++) {
|
24529 | buf[i] = this.read(8);
|
24530 | }
|
24531 | return bufToHex(buf);
|
24532 | };
|
24533 |
|
24534 | function bufToHex(buf) {
|
24535 | return Array.prototype.map.call(buf, x => ('00' + x.toString(16)).slice(-2)).join('');
|
24536 | }
|
24537 |
|
24538 | var bitreader = BitReader;
|
24539 |
|
24540 |
|
24541 | var Stream = function() {
|
24542 | };
|
24543 |
|
24544 |
|
24545 |
|
24546 | Stream.prototype.readByte = function() {
|
24547 | throw new Error("abstract method readByte() not implemented");
|
24548 | };
|
24549 |
|
24550 |
|
24551 | Stream.prototype.read = function(buffer, bufOffset, length) {
|
24552 | var bytesRead = 0;
|
24553 | while (bytesRead < length) {
|
24554 | var c = this.readByte();
|
24555 | if (c < 0) {
|
24556 | return (bytesRead===0) ? -1 : bytesRead;
|
24557 | }
|
24558 | buffer[bufOffset++] = c;
|
24559 | bytesRead++;
|
24560 | }
|
24561 | return bytesRead;
|
24562 | };
|
24563 | Stream.prototype.seek = function(new_pos) {
|
24564 | throw new Error("abstract method seek() not implemented");
|
24565 | };
|
24566 |
|
24567 |
|
24568 | Stream.prototype.writeByte = function(_byte) {
|
24569 | throw new Error("abstract method readByte() not implemented");
|
24570 | };
|
24571 | Stream.prototype.write = function(buffer, bufOffset, length) {
|
24572 | var i;
|
24573 | for (i=0; i<length; i++) {
|
24574 | this.writeByte(buffer[bufOffset++]);
|
24575 | }
|
24576 | return length;
|
24577 | };
|
24578 | Stream.prototype.flush = function() {
|
24579 | };
|
24580 |
|
24581 | var stream$1 = Stream;
|
24582 |
|
24583 |
|
24584 |
|
24585 |
|
24586 |
|
24587 |
|
24588 |
|
24589 |
|
24590 |
|
24591 |
|
24592 |
|
24593 |
|
24594 |
|
24595 |
|
24596 |
|
24597 |
|
24598 |
|
24599 |
|
24600 |
|
24601 |
|
24602 |
|
24603 |
|
24604 |
|
24605 |
|
24606 |
|
24607 |
|
24608 |
|
24609 |
|
24610 |
|
24611 |
|
24612 |
|
24613 | var crc32$1 = (function() {
|
24614 |
|
24615 | |
24616 |
|
24617 |
|
24618 | var crc32Lookup = new Uint32Array([
|
24619 | 0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b, 0x1a864db2, 0x1e475005,
|
24620 | 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61, 0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd,
|
24621 | 0x4c11db70, 0x48d0c6c7, 0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
|
24622 | 0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3, 0x709f7b7a, 0x745e66cd,
|
24623 | 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039, 0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5,
|
24624 | 0xbe2b5b58, 0xbaea46ef, 0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
|
24625 | 0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb, 0xceb42022, 0xca753d95,
|
24626 | 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1, 0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d,
|
24627 | 0x34867077, 0x30476dc0, 0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
|
24628 | 0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4, 0x0808d07d, 0x0cc9cdca,
|
24629 | 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde, 0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02,
|
24630 | 0x5e9f46bf, 0x5a5e5b08, 0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
|
24631 | 0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc, 0xb6238b25, 0xb2e29692,
|
24632 | 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6, 0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a,
|
24633 | 0xe0b41de7, 0xe4750050, 0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
|
24634 | 0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34, 0xdc3abded, 0xd8fba05a,
|
24635 | 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637, 0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb,
|
24636 | 0x4f040d56, 0x4bc510e1, 0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
|
24637 | 0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5, 0x3f9b762c, 0x3b5a6b9b,
|
24638 | 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff, 0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623,
|
24639 | 0xf12f560e, 0xf5ee4bb9, 0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
|
24640 | 0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd, 0xcda1f604, 0xc960ebb3,
|
24641 | 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7, 0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b,
|
24642 | 0x9b3660c6, 0x9ff77d71, 0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
|
24643 | 0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2, 0x470cdd2b, 0x43cdc09c,
|
24644 | 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8, 0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24,
|
24645 | 0x119b4be9, 0x155a565e, 0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
|
24646 | 0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a, 0x2d15ebe3, 0x29d4f654,
|
24647 | 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0, 0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c,
|
24648 | 0xe3a1cbc1, 0xe760d676, 0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
|
24649 | 0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662, 0x933eb0bb, 0x97ffad0c,
|
24650 | 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668, 0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
|
24651 | ]);
|
24652 |
|
24653 | var CRC32 = function() {
|
24654 | |
24655 |
|
24656 |
|
24657 | var crc = 0xffffffff;
|
24658 |
|
24659 | |
24660 |
|
24661 |
|
24662 | this.getCRC = function() {
|
24663 | return (~crc) >>> 0;
|
24664 | };
|
24665 |
|
24666 | |
24667 |
|
24668 |
|
24669 |
|
24670 | this.updateCRC = function(value) {
|
24671 | crc = (crc << 8) ^ crc32Lookup[((crc >>> 24) ^ value) & 0xff];
|
24672 | };
|
24673 |
|
24674 | |
24675 |
|
24676 |
|
24677 |
|
24678 |
|
24679 | this.updateCRCRun = function(value, count) {
|
24680 | while (count-- > 0) {
|
24681 | crc = (crc << 8) ^ crc32Lookup[((crc >>> 24) ^ value) & 0xff];
|
24682 | }
|
24683 | };
|
24684 | };
|
24685 | return CRC32;
|
24686 | })();
|
24687 |
|
24688 |
|
24689 |
|
24690 |
|
24691 |
|
24692 |
|
24693 |
|
24694 |
|
24695 |
|
24696 |
|
24697 |
|
24698 |
|
24699 |
|
24700 |
|
24701 |
|
24702 |
|
24703 |
|
24704 |
|
24705 |
|
24706 |
|
24707 |
|
24708 |
|
24709 |
|
24710 |
|
24711 |
|
24712 |
|
24713 |
|
24714 |
|
24715 |
|
24716 |
|
24717 |
|
24718 |
|
24719 |
|
24720 |
|
24721 |
|
24722 |
|
24723 |
|
24724 | var MAX_HUFCODE_BITS = 20;
|
24725 | var MAX_SYMBOLS = 258;
|
24726 | var SYMBOL_RUNA = 0;
|
24727 | var SYMBOL_RUNB = 1;
|
24728 | var MIN_GROUPS = 2;
|
24729 | var MAX_GROUPS = 6;
|
24730 | var GROUP_SIZE = 50;
|
24731 |
|
24732 | var WHOLEPI = "314159265359";
|
24733 | var SQRTPI = "177245385090";
|
24734 |
|
24735 | var mtf = function(array, index) {
|
24736 | var src = array[index], i;
|
24737 | for (i = index; i > 0; i--) {
|
24738 | array[i] = array[i-1];
|
24739 | }
|
24740 | array[0] = src;
|
24741 | return src;
|
24742 | };
|
24743 |
|
24744 | var Err = {
|
24745 | OK: 0,
|
24746 | LAST_BLOCK: -1,
|
24747 | NOT_BZIP_DATA: -2,
|
24748 | UNEXPECTED_INPUT_EOF: -3,
|
24749 | UNEXPECTED_OUTPUT_EOF: -4,
|
24750 | DATA_ERROR: -5,
|
24751 | OUT_OF_MEMORY: -6,
|
24752 | OBSOLETE_INPUT: -7,
|
24753 | END_OF_BLOCK: -8
|
24754 | };
|
24755 | var ErrorMessages = {};
|
24756 | ErrorMessages[Err.LAST_BLOCK] = "Bad file checksum";
|
24757 | ErrorMessages[Err.NOT_BZIP_DATA] = "Not bzip data";
|
24758 | ErrorMessages[Err.UNEXPECTED_INPUT_EOF] = "Unexpected input EOF";
|
24759 | ErrorMessages[Err.UNEXPECTED_OUTPUT_EOF] = "Unexpected output EOF";
|
24760 | ErrorMessages[Err.DATA_ERROR] = "Data error";
|
24761 | ErrorMessages[Err.OUT_OF_MEMORY] = "Out of memory";
|
24762 | ErrorMessages[Err.OBSOLETE_INPUT] = "Obsolete (pre 0.9.5) bzip format not supported.";
|
24763 |
|
24764 | var _throw = function(status, optDetail) {
|
24765 | var msg = ErrorMessages[status] || 'unknown error';
|
24766 | if (optDetail) { msg += ': '+optDetail; }
|
24767 | var e = new TypeError(msg);
|
24768 | e.errorCode = status;
|
24769 | throw e;
|
24770 | };
|
24771 |
|
24772 | var Bunzip = function(inputStream, outputStream) {
|
24773 | this.writePos = this.writeCurrent = this.writeCount = 0;
|
24774 |
|
24775 | this._start_bunzip(inputStream, outputStream);
|
24776 | };
|
24777 | Bunzip.prototype._init_block = function() {
|
24778 | var moreBlocks = this._get_next_block();
|
24779 | if ( !moreBlocks ) {
|
24780 | this.writeCount = -1;
|
24781 | return false;
|
24782 | }
|
24783 | this.blockCRC = new crc32$1();
|
24784 | return true;
|
24785 | };
|
24786 |
|
24787 | Bunzip.prototype._start_bunzip = function(inputStream, outputStream) {
|
24788 |
|
24789 | var buf = new Uint8Array(4);
|
24790 | if (inputStream.read(buf, 0, 4) !== 4 ||
|
24791 | String.fromCharCode(buf[0], buf[1], buf[2]) !== 'BZh')
|
24792 | _throw(Err.NOT_BZIP_DATA, 'bad magic');
|
24793 |
|
24794 | var level = buf[3] - 0x30;
|
24795 | if (level < 1 || level > 9)
|
24796 | _throw(Err.NOT_BZIP_DATA, 'level out of range');
|
24797 |
|
24798 | this.reader = new bitreader(inputStream);
|
24799 |
|
24800 | |
24801 |
|
24802 | this.dbufSize = 100000 * level;
|
24803 | this.nextoutput = 0;
|
24804 | this.outputStream = outputStream;
|
24805 | this.streamCRC = 0;
|
24806 | };
|
24807 | Bunzip.prototype._get_next_block = function() {
|
24808 | var i, j, k;
|
24809 | var reader = this.reader;
|
24810 |
|
24811 | |
24812 |
|
24813 | var h = reader.pi();
|
24814 | if (h === SQRTPI) {
|
24815 | return false;
|
24816 | }
|
24817 | if (h !== WHOLEPI)
|
24818 | _throw(Err.NOT_BZIP_DATA);
|
24819 | this.targetBlockCRC = reader.read(32) >>> 0;
|
24820 | this.streamCRC = (this.targetBlockCRC ^
|
24821 | ((this.streamCRC << 1) | (this.streamCRC>>>31))) >>> 0;
|
24822 | |
24823 |
|
24824 |
|
24825 | if (reader.read(1))
|
24826 | _throw(Err.OBSOLETE_INPUT);
|
24827 | var origPointer = reader.read(24);
|
24828 | if (origPointer > this.dbufSize)
|
24829 | _throw(Err.DATA_ERROR, 'initial position out of bounds');
|
24830 | |
24831 |
|
24832 |
|
24833 |
|
24834 |
|
24835 | var t = reader.read(16);
|
24836 | var symToByte = new Uint8Array(256), symTotal = 0;
|
24837 | for (i = 0; i < 16; i++) {
|
24838 | if (t & (1 << (0xF - i))) {
|
24839 | var o = i * 16;
|
24840 | k = reader.read(16);
|
24841 | for (j = 0; j < 16; j++)
|
24842 | if (k & (1 << (0xF - j)))
|
24843 | symToByte[symTotal++] = o + j;
|
24844 | }
|
24845 | }
|
24846 |
|
24847 |
|
24848 | var groupCount = reader.read(3);
|
24849 | if (groupCount < MIN_GROUPS || groupCount > MAX_GROUPS)
|
24850 | _throw(Err.DATA_ERROR);
|
24851 | |
24852 |
|
24853 |
|
24854 |
|
24855 | var nSelectors = reader.read(15);
|
24856 | if (nSelectors === 0)
|
24857 | _throw(Err.DATA_ERROR);
|
24858 |
|
24859 | var mtfSymbol = new Uint8Array(256);
|
24860 | for (i = 0; i < groupCount; i++)
|
24861 | mtfSymbol[i] = i;
|
24862 |
|
24863 | var selectors = new Uint8Array(nSelectors);
|
24864 |
|
24865 | for (i = 0; i < nSelectors; i++) {
|
24866 |
|
24867 | for (j = 0; reader.read(1); j++)
|
24868 | if (j >= groupCount) _throw(Err.DATA_ERROR);
|
24869 |
|
24870 | selectors[i] = mtf(mtfSymbol, j);
|
24871 | }
|
24872 |
|
24873 | |
24874 |
|
24875 | var symCount = symTotal + 2;
|
24876 | var groups = [], hufGroup;
|
24877 | for (j = 0; j < groupCount; j++) {
|
24878 | var length = new Uint8Array(symCount), temp = new Uint16Array(MAX_HUFCODE_BITS + 1);
|
24879 | |
24880 |
|
24881 |
|
24882 | t = reader.read(5);
|
24883 | for (i = 0; i < symCount; i++) {
|
24884 | for (;;) {
|
24885 | if (t < 1 || t > MAX_HUFCODE_BITS) _throw(Err.DATA_ERROR);
|
24886 | |
24887 |
|
24888 | if(!reader.read(1))
|
24889 | break;
|
24890 | if(!reader.read(1))
|
24891 | t++;
|
24892 | else
|
24893 | t--;
|
24894 | }
|
24895 | length[i] = t;
|
24896 | }
|
24897 |
|
24898 |
|
24899 | var minLen, maxLen;
|
24900 | minLen = maxLen = length[0];
|
24901 | for (i = 1; i < symCount; i++) {
|
24902 | if (length[i] > maxLen)
|
24903 | maxLen = length[i];
|
24904 | else if (length[i] < minLen)
|
24905 | minLen = length[i];
|
24906 | }
|
24907 |
|
24908 | |
24909 |
|
24910 |
|
24911 |
|
24912 |
|
24913 |
|
24914 |
|
24915 |
|
24916 |
|
24917 |
|
24918 | hufGroup = {};
|
24919 | groups.push(hufGroup);
|
24920 | hufGroup.permute = new Uint16Array(MAX_SYMBOLS);
|
24921 | hufGroup.limit = new Uint32Array(MAX_HUFCODE_BITS + 2);
|
24922 | hufGroup.base = new Uint32Array(MAX_HUFCODE_BITS + 1);
|
24923 | hufGroup.minLen = minLen;
|
24924 | hufGroup.maxLen = maxLen;
|
24925 |
|
24926 | var pp = 0;
|
24927 | for (i = minLen; i <= maxLen; i++) {
|
24928 | temp[i] = hufGroup.limit[i] = 0;
|
24929 | for (t = 0; t < symCount; t++)
|
24930 | if (length[t] === i)
|
24931 | hufGroup.permute[pp++] = t;
|
24932 | }
|
24933 |
|
24934 | for (i = 0; i < symCount; i++)
|
24935 | temp[length[i]]++;
|
24936 | |
24937 |
|
24938 |
|
24939 |
|
24940 | pp = t = 0;
|
24941 | for (i = minLen; i < maxLen; i++) {
|
24942 | pp += temp[i];
|
24943 | |
24944 |
|
24945 |
|
24946 |
|
24947 |
|
24948 |
|
24949 | hufGroup.limit[i] = pp - 1;
|
24950 | pp <<= 1;
|
24951 | t += temp[i];
|
24952 | hufGroup.base[i + 1] = pp - t;
|
24953 | }
|
24954 | hufGroup.limit[maxLen + 1] = Number.MAX_VALUE;
|
24955 | hufGroup.limit[maxLen] = pp + temp[maxLen] - 1;
|
24956 | hufGroup.base[minLen] = 0;
|
24957 | }
|
24958 | |
24959 |
|
24960 |
|
24961 |
|
24962 |
|
24963 | var byteCount = new Uint32Array(256);
|
24964 | for (i = 0; i < 256; i++)
|
24965 | mtfSymbol[i] = i;
|
24966 |
|
24967 | var runPos = 0, dbufCount = 0, selector = 0, uc;
|
24968 | var dbuf = this.dbuf = new Uint32Array(this.dbufSize);
|
24969 | symCount = 0;
|
24970 | for (;;) {
|
24971 |
|
24972 | if (!(symCount--)) {
|
24973 | symCount = GROUP_SIZE - 1;
|
24974 | if (selector >= nSelectors) { _throw(Err.DATA_ERROR); }
|
24975 | hufGroup = groups[selectors[selector++]];
|
24976 | }
|
24977 |
|
24978 | i = hufGroup.minLen;
|
24979 | j = reader.read(i);
|
24980 | for (;;i++) {
|
24981 | if (i > hufGroup.maxLen) { _throw(Err.DATA_ERROR); }
|
24982 | if (j <= hufGroup.limit[i])
|
24983 | break;
|
24984 | j = (j << 1) | reader.read(1);
|
24985 | }
|
24986 |
|
24987 | j -= hufGroup.base[i];
|
24988 | if (j < 0 || j >= MAX_SYMBOLS) { _throw(Err.DATA_ERROR); }
|
24989 | var nextSym = hufGroup.permute[j];
|
24990 | |
24991 |
|
24992 |
|
24993 |
|
24994 | if (nextSym === SYMBOL_RUNA || nextSym === SYMBOL_RUNB) {
|
24995 |
|
24996 | if (!runPos){
|
24997 | runPos = 1;
|
24998 | t = 0;
|
24999 | }
|
25000 | |
25001 |
|
25002 |
|
25003 |
|
25004 |
|
25005 |
|
25006 |
|
25007 | if (nextSym === SYMBOL_RUNA)
|
25008 | t += runPos;
|
25009 | else
|
25010 | t += 2 * runPos;
|
25011 | runPos <<= 1;
|
25012 | continue;
|
25013 | }
|
25014 | |
25015 |
|
25016 |
|
25017 |
|
25018 | if (runPos){
|
25019 | runPos = 0;
|
25020 | if (dbufCount + t > this.dbufSize) { _throw(Err.DATA_ERROR); }
|
25021 | uc = symToByte[mtfSymbol[0]];
|
25022 | byteCount[uc] += t;
|
25023 | while (t--)
|
25024 | dbuf[dbufCount++] = uc;
|
25025 | }
|
25026 |
|
25027 | if (nextSym > symTotal)
|
25028 | break;
|
25029 | |
25030 |
|
25031 |
|
25032 |
|
25033 |
|
25034 |
|
25035 |
|
25036 | if (dbufCount >= this.dbufSize) { _throw(Err.DATA_ERROR); }
|
25037 | i = nextSym - 1;
|
25038 | uc = mtf(mtfSymbol, i);
|
25039 | uc = symToByte[uc];
|
25040 |
|
25041 | byteCount[uc]++;
|
25042 | dbuf[dbufCount++] = uc;
|
25043 | }
|
25044 | |
25045 |
|
25046 |
|
25047 |
|
25048 |
|
25049 |
|
25050 | if (origPointer < 0 || origPointer >= dbufCount) { _throw(Err.DATA_ERROR); }
|
25051 |
|
25052 | j = 0;
|
25053 | for (i = 0; i < 256; i++) {
|
25054 | k = j + byteCount[i];
|
25055 | byteCount[i] = j;
|
25056 | j = k;
|
25057 | }
|
25058 |
|
25059 | for (i = 0; i < dbufCount; i++) {
|
25060 | uc = dbuf[i] & 0xff;
|
25061 | dbuf[byteCount[uc]] |= (i << 8);
|
25062 | byteCount[uc]++;
|
25063 | }
|
25064 | |
25065 |
|
25066 |
|
25067 | var pos = 0, current = 0, run = 0;
|
25068 | if (dbufCount) {
|
25069 | pos = dbuf[origPointer];
|
25070 | current = (pos & 0xff);
|
25071 | pos >>= 8;
|
25072 | run = -1;
|
25073 | }
|
25074 | this.writePos = pos;
|
25075 | this.writeCurrent = current;
|
25076 | this.writeCount = dbufCount;
|
25077 | this.writeRun = run;
|
25078 |
|
25079 | return true;
|
25080 | };
|
25081 |
|
25082 |
|
25083 |
|
25084 |
|
25085 |
|
25086 |
|
25087 | Bunzip.prototype._read_bunzip = function(outputBuffer, len) {
|
25088 | var copies, previous, outbyte;
|
25089 | |
25090 |
|
25091 |
|
25092 |
|
25093 | if (this.writeCount < 0) { return 0; }
|
25094 | var dbuf = this.dbuf, pos = this.writePos, current = this.writeCurrent;
|
25095 | var dbufCount = this.writeCount; this.outputsize;
|
25096 | var run = this.writeRun;
|
25097 |
|
25098 | while (dbufCount) {
|
25099 | dbufCount--;
|
25100 | previous = current;
|
25101 | pos = dbuf[pos];
|
25102 | current = pos & 0xff;
|
25103 | pos >>= 8;
|
25104 | if (run++ === 3){
|
25105 | copies = current;
|
25106 | outbyte = previous;
|
25107 | current = -1;
|
25108 | } else {
|
25109 | copies = 1;
|
25110 | outbyte = current;
|
25111 | }
|
25112 | this.blockCRC.updateCRCRun(outbyte, copies);
|
25113 | while (copies--) {
|
25114 | this.outputStream.writeByte(outbyte);
|
25115 | this.nextoutput++;
|
25116 | }
|
25117 | if (current != previous)
|
25118 | run = 0;
|
25119 | }
|
25120 | this.writeCount = dbufCount;
|
25121 |
|
25122 | if (this.blockCRC.getCRC() !== this.targetBlockCRC) {
|
25123 | _throw(Err.DATA_ERROR, "Bad block CRC "+
|
25124 | "(got "+this.blockCRC.getCRC().toString(16)+
|
25125 | " expected "+this.targetBlockCRC.toString(16)+")");
|
25126 | }
|
25127 | return this.nextoutput;
|
25128 | };
|
25129 |
|
25130 | var coerceInputStream = function(input) {
|
25131 | if ('readByte' in input) { return input; }
|
25132 | var inputStream = new stream$1();
|
25133 | inputStream.pos = 0;
|
25134 | inputStream.readByte = function() { return input[this.pos++]; };
|
25135 | inputStream.seek = function(pos) { this.pos = pos; };
|
25136 | inputStream.eof = function() { return this.pos >= input.length; };
|
25137 | return inputStream;
|
25138 | };
|
25139 | var coerceOutputStream = function(output) {
|
25140 | var outputStream = new stream$1();
|
25141 | var resizeOk = true;
|
25142 | if (output) {
|
25143 | if (typeof(output)==='number') {
|
25144 | outputStream.buffer = new Uint8Array(output);
|
25145 | resizeOk = false;
|
25146 | } else if ('writeByte' in output) {
|
25147 | return output;
|
25148 | } else {
|
25149 | outputStream.buffer = output;
|
25150 | resizeOk = false;
|
25151 | }
|
25152 | } else {
|
25153 | outputStream.buffer = new Uint8Array(16384);
|
25154 | }
|
25155 | outputStream.pos = 0;
|
25156 | outputStream.writeByte = function(_byte) {
|
25157 | if (resizeOk && this.pos >= this.buffer.length) {
|
25158 | var newBuffer = new Uint8Array(this.buffer.length*2);
|
25159 | newBuffer.set(this.buffer);
|
25160 | this.buffer = newBuffer;
|
25161 | }
|
25162 | this.buffer[this.pos++] = _byte;
|
25163 | };
|
25164 | outputStream.getBuffer = function() {
|
25165 |
|
25166 | if (this.pos !== this.buffer.length) {
|
25167 | if (!resizeOk)
|
25168 | throw new TypeError('outputsize does not match decoded input');
|
25169 | var newBuffer = new Uint8Array(this.pos);
|
25170 | newBuffer.set(this.buffer.subarray(0, this.pos));
|
25171 | this.buffer = newBuffer;
|
25172 | }
|
25173 | return this.buffer;
|
25174 | };
|
25175 | outputStream._coerced = true;
|
25176 | return outputStream;
|
25177 | };
|
25178 |
|
25179 |
|
25180 |
|
25181 |
|
25182 | const decode$2 = function(input, output, multistream) {
|
25183 |
|
25184 | var inputStream = coerceInputStream(input);
|
25185 | var outputStream = coerceOutputStream(output);
|
25186 |
|
25187 | var bz = new Bunzip(inputStream, outputStream);
|
25188 | while (true) {
|
25189 | if ('eof' in inputStream && inputStream.eof()) break;
|
25190 | if (bz._init_block()) {
|
25191 | bz._read_bunzip();
|
25192 | } else {
|
25193 | var targetStreamCRC = bz.reader.read(32) >>> 0;
|
25194 | if (targetStreamCRC !== bz.streamCRC) {
|
25195 | _throw(Err.DATA_ERROR, "Bad stream CRC "+
|
25196 | "(got "+bz.streamCRC.toString(16)+
|
25197 | " expected "+targetStreamCRC.toString(16)+")");
|
25198 | }
|
25199 | if (multistream &&
|
25200 | 'eof' in inputStream &&
|
25201 | !inputStream.eof()) {
|
25202 |
|
25203 | bz._start_bunzip(inputStream, outputStream);
|
25204 | } else break;
|
25205 | }
|
25206 | }
|
25207 | if ('getBuffer' in outputStream)
|
25208 | return outputStream.getBuffer();
|
25209 | };
|
25210 | const decodeBlock = function(input, pos, output) {
|
25211 |
|
25212 | var inputStream = coerceInputStream(input);
|
25213 | var outputStream = coerceOutputStream(output);
|
25214 | var bz = new Bunzip(inputStream, outputStream);
|
25215 | bz.reader.seek(pos);
|
25216 |
|
25217 | var moreBlocks = bz._get_next_block();
|
25218 | if (moreBlocks) {
|
25219 |
|
25220 | bz.blockCRC = new crc32$1();
|
25221 |
|
25222 |
|
25223 | bz.writeCopies = 0;
|
25224 |
|
25225 |
|
25226 | bz._read_bunzip();
|
25227 |
|
25228 | }
|
25229 | if ('getBuffer' in outputStream)
|
25230 | return outputStream.getBuffer();
|
25231 | };
|
25232 |
|
25233 |
|
25234 |
|
25235 |
|
25236 | const table = function(input, callback, multistream) {
|
25237 |
|
25238 | var inputStream = new stream$1();
|
25239 | inputStream.delegate = coerceInputStream(input);
|
25240 | inputStream.pos = 0;
|
25241 | inputStream.readByte = function() {
|
25242 | this.pos++;
|
25243 | return this.delegate.readByte();
|
25244 | };
|
25245 | if (inputStream.delegate.eof) {
|
25246 | inputStream.eof = inputStream.delegate.eof.bind(inputStream.delegate);
|
25247 | }
|
25248 | var outputStream = new stream$1();
|
25249 | outputStream.pos = 0;
|
25250 | outputStream.writeByte = function() { this.pos++; };
|
25251 |
|
25252 | var bz = new Bunzip(inputStream, outputStream);
|
25253 | var blockSize = bz.dbufSize;
|
25254 | while (true) {
|
25255 | if ('eof' in inputStream && inputStream.eof()) break;
|
25256 |
|
25257 | var position = inputStream.pos*8 + bz.reader.bitOffset;
|
25258 | if (bz.reader.hasByte) { position -= 8; }
|
25259 |
|
25260 | if (bz._init_block()) {
|
25261 | var start = outputStream.pos;
|
25262 | bz._read_bunzip();
|
25263 | callback(position, outputStream.pos - start);
|
25264 | } else {
|
25265 | bz.reader.read(32);
|
25266 | if (multistream &&
|
25267 | 'eof' in inputStream &&
|
25268 | !inputStream.eof()) {
|
25269 |
|
25270 | bz._start_bunzip(inputStream, outputStream);
|
25271 | console.assert(bz.dbufSize === blockSize,
|
25272 | "shouldn't change block size within multistream file");
|
25273 | } else break;
|
25274 | }
|
25275 | }
|
25276 | };
|
25277 |
|
25278 | var lib = {
|
25279 | Bunzip,
|
25280 | Stream: stream$1,
|
25281 | Err,
|
25282 | decode: decode$2,
|
25283 | decodeBlock,
|
25284 | table
|
25285 | };
|
25286 | var lib_4 = lib.decode;
|
25287 |
|
25288 |
|
25289 |
|
25290 |
|
25291 |
|
25292 |
|
25293 |
|
25294 |
|
25295 |
|
25296 |
|
25297 |
|
25298 | class CompressedDataPacket {
|
25299 | |
25300 |
|
25301 |
|
25302 | constructor(config = defaultConfig) {
|
25303 | |
25304 |
|
25305 |
|
25306 |
|
25307 | this.tag = enums.packet.compressedData;
|
25308 | |
25309 |
|
25310 |
|
25311 |
|
25312 | this.packets = null;
|
25313 | |
25314 |
|
25315 |
|
25316 |
|
25317 | this.algorithm = enums.read(enums.compression, config.compression);
|
25318 |
|
25319 | |
25320 |
|
25321 |
|
25322 |
|
25323 | this.compressed = null;
|
25324 |
|
25325 | |
25326 |
|
25327 |
|
25328 | this.deflateLevel = config.deflateLevel;
|
25329 | }
|
25330 |
|
25331 | |
25332 |
|
25333 |
|
25334 |
|
25335 | async read(bytes, config, streaming) {
|
25336 | await stream.parse(bytes, async reader => {
|
25337 |
|
25338 |
|
25339 | this.algorithm = enums.read(enums.compression, await reader.readByte());
|
25340 |
|
25341 |
|
25342 | this.compressed = reader.remainder();
|
25343 |
|
25344 | await this.decompress(streaming);
|
25345 | });
|
25346 | }
|
25347 |
|
25348 |
|
25349 | |
25350 |
|
25351 |
|
25352 |
|
25353 | write() {
|
25354 | if (this.compressed === null) {
|
25355 | this.compress();
|
25356 | }
|
25357 |
|
25358 | return util.concat([new Uint8Array([enums.write(enums.compression, this.algorithm)]), this.compressed]);
|
25359 | }
|
25360 |
|
25361 |
|
25362 | |
25363 |
|
25364 |
|
25365 |
|
25366 | async decompress(streaming) {
|
25367 |
|
25368 | if (!decompress_fns[this.algorithm]) {
|
25369 | throw new Error(this.algorithm + ' decompression not supported');
|
25370 | }
|
25371 |
|
25372 | await this.packets.read(decompress_fns[this.algorithm](this.compressed), {
|
25373 | LiteralDataPacket,
|
25374 | OnePassSignaturePacket,
|
25375 | SignaturePacket
|
25376 | }, streaming);
|
25377 | }
|
25378 |
|
25379 | |
25380 |
|
25381 |
|
25382 | compress() {
|
25383 | if (!compress_fns[this.algorithm]) {
|
25384 | throw new Error(this.algorithm + ' compression not supported');
|
25385 | }
|
25386 |
|
25387 | this.compressed = compress_fns[this.algorithm](this.packets.write(), this.deflateLevel);
|
25388 | }
|
25389 | }
|
25390 |
|
25391 |
|
25392 |
|
25393 |
|
25394 |
|
25395 |
|
25396 |
|
25397 |
|
25398 | const nodeZlib = util.getNodeZlib();
|
25399 |
|
25400 | function uncompressed(data) {
|
25401 | return data;
|
25402 | }
|
25403 |
|
25404 | function node_zlib(func, options = {}) {
|
25405 | return function (data) {
|
25406 | return stream.nodeToWeb(stream.webToNode(data).pipe(func(options)));
|
25407 | };
|
25408 | }
|
25409 |
|
25410 | function pako_zlib(constructor, options = {}) {
|
25411 | return function(data) {
|
25412 | const obj = new constructor(options);
|
25413 | return stream.transform(data, value => {
|
25414 | if (value.length) {
|
25415 | obj.push(value, Z_SYNC_FLUSH);
|
25416 | return obj.result;
|
25417 | }
|
25418 | }, () => {
|
25419 | if (constructor === Deflate) {
|
25420 | obj.push([], Z_FINISH);
|
25421 | return obj.result;
|
25422 | }
|
25423 | });
|
25424 | };
|
25425 | }
|
25426 |
|
25427 | function bzip2(func) {
|
25428 | return function(data) {
|
25429 | return stream.fromAsync(async () => func(await stream.readToEnd(data)));
|
25430 | };
|
25431 | }
|
25432 |
|
25433 | const compress_fns = nodeZlib ? {
|
25434 | zip: (compressed, level) => node_zlib(nodeZlib.createDeflateRaw, { level })(compressed),
|
25435 | zlib: (compressed, level) => node_zlib(nodeZlib.createDeflate, { level })(compressed)
|
25436 | } : {
|
25437 | zip: (compressed, level) => pako_zlib(Deflate, { raw: true, level })(compressed),
|
25438 | zlib: (compressed, level) => pako_zlib(Deflate, { level })(compressed)
|
25439 | };
|
25440 |
|
25441 | const decompress_fns = nodeZlib ? {
|
25442 | uncompressed: uncompressed,
|
25443 | zip: node_zlib(nodeZlib.createInflateRaw),
|
25444 | zlib: node_zlib(nodeZlib.createInflate),
|
25445 | bzip2: bzip2(lib_4)
|
25446 | } : {
|
25447 | uncompressed: uncompressed,
|
25448 | zip: pako_zlib(Inflate, { raw: true }),
|
25449 | zlib: pako_zlib(Inflate),
|
25450 | bzip2: bzip2(lib_4)
|
25451 | };
|
25452 |
|
25453 |
|
25454 |
|
25455 | const VERSION = 1;
|
25456 |
|
25457 |
|
25458 |
|
25459 |
|
25460 |
|
25461 |
|
25462 |
|
25463 |
|
25464 |
|
25465 |
|
25466 |
|
25467 | class SymEncryptedIntegrityProtectedDataPacket {
|
25468 | constructor() {
|
25469 | this.tag = enums.packet.symEncryptedIntegrityProtectedData;
|
25470 | this.version = VERSION;
|
25471 |
|
25472 | this.encrypted = null;
|
25473 | |
25474 |
|
25475 |
|
25476 |
|
25477 |
|
25478 |
|
25479 | this.modification = false;
|
25480 | this.packets = null;
|
25481 | }
|
25482 |
|
25483 | async read(bytes) {
|
25484 | await stream.parse(bytes, async reader => {
|
25485 |
|
25486 |
|
25487 | if (await reader.readByte() !== VERSION) {
|
25488 | throw new Error('Invalid packet version.');
|
25489 | }
|
25490 |
|
25491 |
|
25492 |
|
25493 |
|
25494 | this.encrypted = reader.remainder();
|
25495 | });
|
25496 | }
|
25497 |
|
25498 | write() {
|
25499 | return util.concat([new Uint8Array([VERSION]), this.encrypted]);
|
25500 | }
|
25501 |
|
25502 | |
25503 |
|
25504 |
|
25505 |
|
25506 |
|
25507 |
|
25508 |
|
25509 |
|
25510 |
|
25511 | async encrypt(sessionKeyAlgorithm, key, streaming, config = defaultConfig) {
|
25512 | let bytes = this.packets.write();
|
25513 | if (!streaming) bytes = await stream.readToEnd(bytes);
|
25514 | const prefix = await mod.getPrefixRandom(sessionKeyAlgorithm);
|
25515 | const mdc = new Uint8Array([0xD3, 0x14]);
|
25516 |
|
25517 | const tohash = util.concat([prefix, bytes, mdc]);
|
25518 | const hash = await mod.hash.sha1(stream.passiveClone(tohash));
|
25519 | const plaintext = util.concat([tohash, hash]);
|
25520 |
|
25521 | this.encrypted = await mod.cfb.encrypt(sessionKeyAlgorithm, key, plaintext, new Uint8Array(mod.cipher[sessionKeyAlgorithm].blockSize), config);
|
25522 | return true;
|
25523 | }
|
25524 |
|
25525 | |
25526 |
|
25527 |
|
25528 |
|
25529 |
|
25530 |
|
25531 |
|
25532 |
|
25533 |
|
25534 | async decrypt(sessionKeyAlgorithm, key, streaming, config = defaultConfig) {
|
25535 | let encrypted = stream.clone(this.encrypted);
|
25536 | if (!streaming) encrypted = await stream.readToEnd(encrypted);
|
25537 | const decrypted = await mod.cfb.decrypt(sessionKeyAlgorithm, key, encrypted, new Uint8Array(mod.cipher[sessionKeyAlgorithm].blockSize));
|
25538 |
|
25539 |
|
25540 |
|
25541 | const realHash = stream.slice(stream.passiveClone(decrypted), -20);
|
25542 | const tohash = stream.slice(decrypted, 0, -20);
|
25543 | const verifyHash = Promise.all([
|
25544 | stream.readToEnd(await mod.hash.sha1(stream.passiveClone(tohash))),
|
25545 | stream.readToEnd(realHash)
|
25546 | ]).then(([hash, mdc]) => {
|
25547 | if (!util.equalsUint8Array(hash, mdc)) {
|
25548 | throw new Error('Modification detected.');
|
25549 | }
|
25550 | return new Uint8Array();
|
25551 | });
|
25552 | const bytes = stream.slice(tohash, mod.cipher[sessionKeyAlgorithm].blockSize + 2);
|
25553 | let packetbytes = stream.slice(bytes, 0, -2);
|
25554 | packetbytes = stream.concat([packetbytes, stream.fromAsync(() => verifyHash)]);
|
25555 | if (!util.isStream(encrypted) || !config.allowUnauthenticatedStream) {
|
25556 | packetbytes = await stream.readToEnd(packetbytes);
|
25557 | }
|
25558 | await this.packets.read(packetbytes, {
|
25559 | LiteralDataPacket,
|
25560 | CompressedDataPacket,
|
25561 | OnePassSignaturePacket,
|
25562 | SignaturePacket
|
25563 | }, streaming);
|
25564 | return true;
|
25565 | }
|
25566 | }
|
25567 |
|
25568 |
|
25569 |
|
25570 | const VERSION$1 = 1;
|
25571 |
|
25572 |
|
25573 |
|
25574 |
|
25575 |
|
25576 |
|
25577 |
|
25578 |
|
25579 | class AEADEncryptedDataPacket {
|
25580 | constructor() {
|
25581 | this.tag = enums.packet.AEADEncryptedData;
|
25582 | this.version = VERSION$1;
|
25583 | this.cipherAlgo = null;
|
25584 | this.aeadAlgorithm = 'eax';
|
25585 | this.aeadAlgo = null;
|
25586 | this.chunkSizeByte = null;
|
25587 | this.iv = null;
|
25588 | this.encrypted = null;
|
25589 | this.packets = null;
|
25590 | }
|
25591 |
|
25592 | |
25593 |
|
25594 |
|
25595 |
|
25596 | async read(bytes) {
|
25597 | await stream.parse(bytes, async reader => {
|
25598 | if (await reader.readByte() !== VERSION$1) {
|
25599 | throw new Error('Invalid packet version.');
|
25600 | }
|
25601 | this.cipherAlgo = await reader.readByte();
|
25602 | this.aeadAlgo = await reader.readByte();
|
25603 | this.chunkSizeByte = await reader.readByte();
|
25604 | const mode = mod[enums.read(enums.aead, this.aeadAlgo)];
|
25605 | this.iv = await reader.readBytes(mode.ivLength);
|
25606 | this.encrypted = reader.remainder();
|
25607 | });
|
25608 | }
|
25609 |
|
25610 | |
25611 |
|
25612 |
|
25613 |
|
25614 | write() {
|
25615 | return util.concat([new Uint8Array([this.version, this.cipherAlgo, this.aeadAlgo, this.chunkSizeByte]), this.iv, this.encrypted]);
|
25616 | }
|
25617 |
|
25618 | |
25619 |
|
25620 |
|
25621 |
|
25622 |
|
25623 |
|
25624 |
|
25625 |
|
25626 | async decrypt(sessionKeyAlgorithm, key, streaming) {
|
25627 | await this.packets.read(await this.crypt('decrypt', key, stream.clone(this.encrypted), streaming), {
|
25628 | LiteralDataPacket,
|
25629 | CompressedDataPacket,
|
25630 | OnePassSignaturePacket,
|
25631 | SignaturePacket
|
25632 | }, streaming);
|
25633 | }
|
25634 |
|
25635 | |
25636 |
|
25637 |
|
25638 |
|
25639 |
|
25640 |
|
25641 |
|
25642 |
|
25643 |
|
25644 | async encrypt(sessionKeyAlgorithm, key, streaming, config = defaultConfig) {
|
25645 | this.cipherAlgo = enums.write(enums.symmetric, sessionKeyAlgorithm);
|
25646 | this.aeadAlgo = enums.write(enums.aead, this.aeadAlgorithm);
|
25647 | const mode = mod[enums.read(enums.aead, this.aeadAlgo)];
|
25648 | this.iv = await mod.random.getRandomBytes(mode.ivLength);
|
25649 | this.chunkSizeByte = config.aeadChunkSizeByte;
|
25650 | const data = this.packets.write();
|
25651 | this.encrypted = await this.crypt('encrypt', key, data, streaming);
|
25652 | }
|
25653 |
|
25654 | |
25655 |
|
25656 |
|
25657 |
|
25658 |
|
25659 |
|
25660 |
|
25661 |
|
25662 |
|
25663 | async crypt(fn, key, data, streaming) {
|
25664 | const cipher = enums.read(enums.symmetric, this.cipherAlgo);
|
25665 | const mode = mod[enums.read(enums.aead, this.aeadAlgo)];
|
25666 | const modeInstance = await mode(cipher, key);
|
25667 | const tagLengthIfDecrypting = fn === 'decrypt' ? mode.tagLength : 0;
|
25668 | const tagLengthIfEncrypting = fn === 'encrypt' ? mode.tagLength : 0;
|
25669 | const chunkSize = 2 ** (this.chunkSizeByte + 6) + tagLengthIfDecrypting;
|
25670 | const adataBuffer = new ArrayBuffer(21);
|
25671 | const adataArray = new Uint8Array(adataBuffer, 0, 13);
|
25672 | const adataTagArray = new Uint8Array(adataBuffer);
|
25673 | const adataView = new DataView(adataBuffer);
|
25674 | const chunkIndexArray = new Uint8Array(adataBuffer, 5, 8);
|
25675 | adataArray.set([0xC0 | this.tag, this.version, this.cipherAlgo, this.aeadAlgo, this.chunkSizeByte], 0);
|
25676 | let chunkIndex = 0;
|
25677 | let latestPromise = Promise.resolve();
|
25678 | let cryptedBytes = 0;
|
25679 | let queuedBytes = 0;
|
25680 | const iv = this.iv;
|
25681 | return stream.transformPair(data, async (readable, writable) => {
|
25682 | const reader = stream.getReader(readable);
|
25683 | const buffer = new stream.TransformStream({}, {
|
25684 | highWaterMark: streaming ? util.getHardwareConcurrency() * 2 ** (this.chunkSizeByte + 6) : Infinity,
|
25685 | size: array => array.length
|
25686 | });
|
25687 | stream.pipe(buffer.readable, writable);
|
25688 | const writer = stream.getWriter(buffer.writable);
|
25689 | try {
|
25690 | while (true) {
|
25691 | let chunk = await reader.readBytes(chunkSize + tagLengthIfDecrypting) || new Uint8Array();
|
25692 | const finalChunk = chunk.subarray(chunk.length - tagLengthIfDecrypting);
|
25693 | chunk = chunk.subarray(0, chunk.length - tagLengthIfDecrypting);
|
25694 | let cryptedPromise;
|
25695 | let done;
|
25696 | if (!chunkIndex || chunk.length) {
|
25697 | reader.unshift(finalChunk);
|
25698 | cryptedPromise = modeInstance[fn](chunk, mode.getNonce(iv, chunkIndexArray), adataArray);
|
25699 | queuedBytes += chunk.length - tagLengthIfDecrypting + tagLengthIfEncrypting;
|
25700 | } else {
|
25701 |
|
25702 |
|
25703 |
|
25704 | adataView.setInt32(13 + 4, cryptedBytes);
|
25705 | cryptedPromise = modeInstance[fn](finalChunk, mode.getNonce(iv, chunkIndexArray), adataTagArray);
|
25706 | queuedBytes += tagLengthIfEncrypting;
|
25707 | done = true;
|
25708 | }
|
25709 | cryptedBytes += chunk.length - tagLengthIfDecrypting;
|
25710 |
|
25711 | latestPromise = latestPromise.then(() => cryptedPromise).then(async crypted => {
|
25712 | await writer.ready;
|
25713 | await writer.write(crypted);
|
25714 | queuedBytes -= crypted.length;
|
25715 | }).catch(err => writer.abort(err));
|
25716 | if (done || queuedBytes > writer.desiredSize) {
|
25717 | await latestPromise;
|
25718 | }
|
25719 | if (!done) {
|
25720 | adataView.setInt32(5 + 4, ++chunkIndex);
|
25721 | } else {
|
25722 | await writer.close();
|
25723 | break;
|
25724 | }
|
25725 | }
|
25726 | } catch (e) {
|
25727 | await writer.abort(e);
|
25728 | }
|
25729 | });
|
25730 | }
|
25731 | }
|
25732 |
|
25733 |
|
25734 |
|
25735 |
|
25736 |
|
25737 |
|
25738 |
|
25739 |
|
25740 |
|
25741 |
|
25742 |
|
25743 |
|
25744 |
|
25745 |
|
25746 |
|
25747 |
|
25748 |
|
25749 |
|
25750 |
|
25751 | class PublicKeyEncryptedSessionKeyPacket {
|
25752 | constructor() {
|
25753 | this.tag = enums.packet.publicKeyEncryptedSessionKey;
|
25754 | this.version = 3;
|
25755 |
|
25756 | this.publicKeyId = new Keyid();
|
25757 | this.publicKeyAlgorithm = null;
|
25758 |
|
25759 | this.sessionKey = null;
|
25760 | this.sessionKeyAlgorithm = null;
|
25761 |
|
25762 |
|
25763 | this.encrypted = {};
|
25764 | }
|
25765 |
|
25766 | |
25767 |
|
25768 |
|
25769 |
|
25770 |
|
25771 | read(bytes) {
|
25772 | this.version = bytes[0];
|
25773 | this.publicKeyId.read(bytes.subarray(1, bytes.length));
|
25774 | this.publicKeyAlgorithm = enums.read(enums.publicKey, bytes[9]);
|
25775 |
|
25776 | const algo = enums.write(enums.publicKey, this.publicKeyAlgorithm);
|
25777 | this.encrypted = mod.parseEncSessionKeyParams(algo, bytes.subarray(10));
|
25778 | }
|
25779 |
|
25780 | |
25781 |
|
25782 |
|
25783 |
|
25784 |
|
25785 | write() {
|
25786 | const algo = enums.write(enums.publicKey, this.publicKeyAlgorithm);
|
25787 |
|
25788 | const arr = [
|
25789 | new Uint8Array([this.version]),
|
25790 | this.publicKeyId.write(),
|
25791 | new Uint8Array([enums.write(enums.publicKey, this.publicKeyAlgorithm)]),
|
25792 | mod.serializeParams(algo, this.encrypted)
|
25793 | ];
|
25794 |
|
25795 | return util.concatUint8Array(arr);
|
25796 | }
|
25797 |
|
25798 | |
25799 |
|
25800 |
|
25801 |
|
25802 |
|
25803 |
|
25804 | async encrypt(key) {
|
25805 | const data = util.concatUint8Array([
|
25806 | new Uint8Array([enums.write(enums.symmetric, this.sessionKeyAlgorithm)]),
|
25807 | this.sessionKey,
|
25808 | util.writeChecksum(this.sessionKey)
|
25809 | ]);
|
25810 | const algo = enums.write(enums.publicKey, this.publicKeyAlgorithm);
|
25811 | this.encrypted = await mod.publicKeyEncrypt(
|
25812 | algo, key.publicParams, data, key.getFingerprintBytes());
|
25813 | return true;
|
25814 | }
|
25815 |
|
25816 | |
25817 |
|
25818 |
|
25819 |
|
25820 |
|
25821 |
|
25822 |
|
25823 |
|
25824 |
|
25825 | async decrypt(key) {
|
25826 | const algo = enums.write(enums.publicKey, this.publicKeyAlgorithm);
|
25827 | const keyAlgo = enums.write(enums.publicKey, key.algorithm);
|
25828 |
|
25829 | if (algo !== keyAlgo) {
|
25830 | throw new Error('Decryption error');
|
25831 | }
|
25832 | const decoded = await mod.publicKeyDecrypt(algo, key.publicParams, key.privateParams, this.encrypted, key.getFingerprintBytes());
|
25833 | const checksum = decoded.subarray(decoded.length - 2);
|
25834 | const sessionKey = decoded.subarray(1, decoded.length - 2);
|
25835 | if (!util.equalsUint8Array(checksum, util.writeChecksum(sessionKey))) {
|
25836 | throw new Error('Decryption error');
|
25837 | } else {
|
25838 | this.sessionKey = sessionKey;
|
25839 | this.sessionKeyAlgorithm = enums.read(enums.symmetric, decoded[0]);
|
25840 | }
|
25841 | return true;
|
25842 | }
|
25843 | }
|
25844 |
|
25845 |
|
25846 |
|
25847 | class S2K {
|
25848 | |
25849 |
|
25850 |
|
25851 | constructor(config = defaultConfig) {
|
25852 |
|
25853 | this.algorithm = 'sha256';
|
25854 |
|
25855 | this.type = 'iterated';
|
25856 |
|
25857 | this.c = config.s2kIterationCountByte;
|
25858 | |
25859 |
|
25860 |
|
25861 | this.salt = null;
|
25862 | }
|
25863 |
|
25864 | get_count() {
|
25865 |
|
25866 | const expbias = 6;
|
25867 |
|
25868 | return (16 + (this.c & 15)) << ((this.c >> 4) + expbias);
|
25869 | }
|
25870 |
|
25871 | |
25872 |
|
25873 |
|
25874 |
|
25875 |
|
25876 | read(bytes) {
|
25877 | let i = 0;
|
25878 | this.type = enums.read(enums.s2k, bytes[i++]);
|
25879 | this.algorithm = bytes[i++];
|
25880 | if (this.type !== 'gnu') {
|
25881 | this.algorithm = enums.read(enums.hash, this.algorithm);
|
25882 | }
|
25883 |
|
25884 | switch (this.type) {
|
25885 | case 'simple':
|
25886 | break;
|
25887 |
|
25888 | case 'salted':
|
25889 | this.salt = bytes.subarray(i, i + 8);
|
25890 | i += 8;
|
25891 | break;
|
25892 |
|
25893 | case 'iterated':
|
25894 | this.salt = bytes.subarray(i, i + 8);
|
25895 | i += 8;
|
25896 |
|
25897 |
|
25898 | this.c = bytes[i++];
|
25899 | break;
|
25900 |
|
25901 | case 'gnu':
|
25902 | if (util.uint8ArrayToStr(bytes.subarray(i, i + 3)) === "GNU") {
|
25903 | i += 3;
|
25904 | const gnuExtType = 1000 + bytes[i++];
|
25905 | if (gnuExtType === 1001) {
|
25906 | this.type = 'gnu-dummy';
|
25907 |
|
25908 | } else {
|
25909 | throw new Error("Unknown s2k gnu protection mode.");
|
25910 | }
|
25911 | } else {
|
25912 | throw new Error("Unknown s2k type.");
|
25913 | }
|
25914 | break;
|
25915 |
|
25916 | default:
|
25917 | throw new Error("Unknown s2k type.");
|
25918 | }
|
25919 |
|
25920 | return i;
|
25921 | }
|
25922 |
|
25923 | |
25924 |
|
25925 |
|
25926 |
|
25927 | write() {
|
25928 | if (this.type === 'gnu-dummy') {
|
25929 | return new Uint8Array([101, 0, ...util.strToUint8Array('GNU'), 1]);
|
25930 | }
|
25931 |
|
25932 | const arr = [new Uint8Array([enums.write(enums.s2k, this.type), enums.write(enums.hash, this.algorithm)])];
|
25933 |
|
25934 | switch (this.type) {
|
25935 | case 'simple':
|
25936 | break;
|
25937 | case 'salted':
|
25938 | arr.push(this.salt);
|
25939 | break;
|
25940 | case 'iterated':
|
25941 | arr.push(this.salt);
|
25942 | arr.push(new Uint8Array([this.c]));
|
25943 | break;
|
25944 | case 'gnu':
|
25945 | throw new Error("GNU s2k type not supported.");
|
25946 | default:
|
25947 | throw new Error("Unknown s2k type.");
|
25948 | }
|
25949 |
|
25950 | return util.concatUint8Array(arr);
|
25951 | }
|
25952 |
|
25953 | |
25954 |
|
25955 |
|
25956 |
|
25957 |
|
25958 |
|
25959 |
|
25960 | async produce_key(passphrase, numBytes) {
|
25961 | passphrase = util.encodeUtf8(passphrase);
|
25962 | const algorithm = enums.write(enums.hash, this.algorithm);
|
25963 |
|
25964 | const arr = [];
|
25965 | let rlength = 0;
|
25966 |
|
25967 | let prefixlen = 0;
|
25968 | while (rlength < numBytes) {
|
25969 | let toHash;
|
25970 | switch (this.type) {
|
25971 | case 'simple':
|
25972 | toHash = util.concatUint8Array([new Uint8Array(prefixlen), passphrase]);
|
25973 | break;
|
25974 | case 'salted':
|
25975 | toHash = util.concatUint8Array([new Uint8Array(prefixlen), this.salt, passphrase]);
|
25976 | break;
|
25977 | case 'iterated': {
|
25978 | const data = util.concatUint8Array([this.salt, passphrase]);
|
25979 | let datalen = data.length;
|
25980 | const count = Math.max(this.get_count(), datalen);
|
25981 | toHash = new Uint8Array(prefixlen + count);
|
25982 | toHash.set(data, prefixlen);
|
25983 | for (let pos = prefixlen + datalen; pos < count; pos += datalen, datalen *= 2) {
|
25984 | toHash.copyWithin(pos, prefixlen, pos);
|
25985 | }
|
25986 | break;
|
25987 | }
|
25988 | case 'gnu':
|
25989 | throw new Error("GNU s2k type not supported.");
|
25990 | default:
|
25991 | throw new Error("Unknown s2k type.");
|
25992 | }
|
25993 | const result = await mod.hash.digest(algorithm, toHash);
|
25994 | arr.push(result);
|
25995 | rlength += result.length;
|
25996 | prefixlen++;
|
25997 | }
|
25998 |
|
25999 | return util.concatUint8Array(arr).subarray(0, numBytes);
|
26000 | }
|
26001 | }
|
26002 |
|
26003 |
|
26004 |
|
26005 |
|
26006 |
|
26007 |
|
26008 |
|
26009 |
|
26010 |
|
26011 |
|
26012 |
|
26013 |
|
26014 |
|
26015 |
|
26016 |
|
26017 |
|
26018 | class SymEncryptedSessionKeyPacket {
|
26019 | |
26020 |
|
26021 |
|
26022 | constructor(config = defaultConfig) {
|
26023 | this.tag = enums.packet.symEncryptedSessionKey;
|
26024 | this.version = config.aeadProtect ? 5 : 4;
|
26025 | this.sessionKey = null;
|
26026 | this.sessionKeyEncryptionAlgorithm = null;
|
26027 | this.sessionKeyAlgorithm = 'aes256';
|
26028 | this.aeadAlgorithm = enums.read(enums.aead, config.aeadMode);
|
26029 | this.encrypted = null;
|
26030 | this.s2k = null;
|
26031 | this.iv = null;
|
26032 | }
|
26033 |
|
26034 | |
26035 |
|
26036 |
|
26037 |
|
26038 |
|
26039 | read(bytes) {
|
26040 | let offset = 0;
|
26041 |
|
26042 |
|
26043 | this.version = bytes[offset++];
|
26044 |
|
26045 |
|
26046 | const algo = enums.read(enums.symmetric, bytes[offset++]);
|
26047 |
|
26048 | if (this.version === 5) {
|
26049 |
|
26050 | this.aeadAlgorithm = enums.read(enums.aead, bytes[offset++]);
|
26051 | }
|
26052 |
|
26053 |
|
26054 | this.s2k = new S2K();
|
26055 | offset += this.s2k.read(bytes.subarray(offset, bytes.length));
|
26056 |
|
26057 | if (this.version === 5) {
|
26058 | const mode = mod[this.aeadAlgorithm];
|
26059 |
|
26060 |
|
26061 |
|
26062 | this.iv = bytes.subarray(offset, offset += mode.ivLength);
|
26063 | }
|
26064 |
|
26065 |
|
26066 |
|
26067 | if (this.version === 5 || offset < bytes.length) {
|
26068 | this.encrypted = bytes.subarray(offset, bytes.length);
|
26069 | this.sessionKeyEncryptionAlgorithm = algo;
|
26070 | } else {
|
26071 | this.sessionKeyAlgorithm = algo;
|
26072 | }
|
26073 | }
|
26074 |
|
26075 | |
26076 |
|
26077 |
|
26078 |
|
26079 |
|
26080 | write() {
|
26081 | const algo = this.encrypted === null ?
|
26082 | this.sessionKeyAlgorithm :
|
26083 | this.sessionKeyEncryptionAlgorithm;
|
26084 |
|
26085 | let bytes;
|
26086 |
|
26087 | if (this.version === 5) {
|
26088 | bytes = util.concatUint8Array([new Uint8Array([this.version, enums.write(enums.symmetric, algo), enums.write(enums.aead, this.aeadAlgorithm)]), this.s2k.write(), this.iv, this.encrypted]);
|
26089 | } else {
|
26090 | bytes = util.concatUint8Array([new Uint8Array([this.version, enums.write(enums.symmetric, algo)]), this.s2k.write()]);
|
26091 |
|
26092 | if (this.encrypted !== null) {
|
26093 | bytes = util.concatUint8Array([bytes, this.encrypted]);
|
26094 | }
|
26095 | }
|
26096 |
|
26097 | return bytes;
|
26098 | }
|
26099 |
|
26100 | |
26101 |
|
26102 |
|
26103 |
|
26104 |
|
26105 |
|
26106 | async decrypt(passphrase) {
|
26107 | const algo = this.sessionKeyEncryptionAlgorithm !== null ?
|
26108 | this.sessionKeyEncryptionAlgorithm :
|
26109 | this.sessionKeyAlgorithm;
|
26110 |
|
26111 | const length = mod.cipher[algo].keySize;
|
26112 | const key = await this.s2k.produce_key(passphrase, length);
|
26113 |
|
26114 | if (this.version === 5) {
|
26115 | const mode = mod[this.aeadAlgorithm];
|
26116 | const adata = new Uint8Array([0xC0 | this.tag, this.version, enums.write(enums.symmetric, this.sessionKeyEncryptionAlgorithm), enums.write(enums.aead, this.aeadAlgorithm)]);
|
26117 | const modeInstance = await mode(algo, key);
|
26118 | this.sessionKey = await modeInstance.decrypt(this.encrypted, this.iv, adata);
|
26119 | } else if (this.encrypted !== null) {
|
26120 | const decrypted = await mod.cfb.decrypt(algo, key, this.encrypted, new Uint8Array(mod.cipher[algo].blockSize));
|
26121 |
|
26122 | this.sessionKeyAlgorithm = enums.read(enums.symmetric, decrypted[0]);
|
26123 | this.sessionKey = decrypted.subarray(1, decrypted.length);
|
26124 | } else {
|
26125 | this.sessionKey = key;
|
26126 | }
|
26127 | }
|
26128 |
|
26129 | |
26130 |
|
26131 |
|
26132 |
|
26133 |
|
26134 |
|
26135 |
|
26136 | async encrypt(passphrase, config = defaultConfig) {
|
26137 | const algo = this.sessionKeyEncryptionAlgorithm !== null ?
|
26138 | this.sessionKeyEncryptionAlgorithm :
|
26139 | this.sessionKeyAlgorithm;
|
26140 |
|
26141 | this.sessionKeyEncryptionAlgorithm = algo;
|
26142 |
|
26143 | this.s2k = new S2K(config);
|
26144 | this.s2k.salt = await mod.random.getRandomBytes(8);
|
26145 |
|
26146 | const length = mod.cipher[algo].keySize;
|
26147 | const key = await this.s2k.produce_key(passphrase, length);
|
26148 |
|
26149 | if (this.sessionKey === null) {
|
26150 | this.sessionKey = await mod.generateSessionKey(this.sessionKeyAlgorithm);
|
26151 | }
|
26152 |
|
26153 | if (this.version === 5) {
|
26154 | const mode = mod[this.aeadAlgorithm];
|
26155 | this.iv = await mod.random.getRandomBytes(mode.ivLength);
|
26156 | const adata = new Uint8Array([0xC0 | this.tag, this.version, enums.write(enums.symmetric, this.sessionKeyEncryptionAlgorithm), enums.write(enums.aead, this.aeadAlgorithm)]);
|
26157 | const modeInstance = await mode(algo, key);
|
26158 | this.encrypted = await modeInstance.encrypt(this.sessionKey, this.iv, adata);
|
26159 | } else {
|
26160 | const algo_enum = new Uint8Array([enums.write(enums.symmetric, this.sessionKeyAlgorithm)]);
|
26161 | const private_key = util.concatUint8Array([algo_enum, this.sessionKey]);
|
26162 | this.encrypted = await mod.cfb.encrypt(algo, key, private_key, new Uint8Array(mod.cipher[algo].blockSize), config);
|
26163 | }
|
26164 | }
|
26165 | }
|
26166 |
|
26167 |
|
26168 |
|
26169 |
|
26170 |
|
26171 |
|
26172 |
|
26173 |
|
26174 |
|
26175 |
|
26176 | class LiteralDataPacket {
|
26177 | |
26178 |
|
26179 |
|
26180 | constructor(date = new Date()) {
|
26181 | this.tag = enums.packet.literalData;
|
26182 | this.format = 'utf8';
|
26183 | this.date = util.normalizeDate(date);
|
26184 | this.text = null;
|
26185 | this.data = null;
|
26186 | this.filename = '';
|
26187 | }
|
26188 |
|
26189 | |
26190 |
|
26191 |
|
26192 |
|
26193 |
|
26194 |
|
26195 | setText(text, format = 'utf8') {
|
26196 | this.format = format;
|
26197 | this.text = text;
|
26198 | this.data = null;
|
26199 | }
|
26200 |
|
26201 | |
26202 |
|
26203 |
|
26204 |
|
26205 |
|
26206 |
|
26207 | getText(clone = false) {
|
26208 | if (this.text === null || util.isStream(this.text)) {
|
26209 | this.text = util.decodeUtf8(util.nativeEOL(this.getBytes(clone)));
|
26210 | }
|
26211 | return this.text;
|
26212 | }
|
26213 |
|
26214 | |
26215 |
|
26216 |
|
26217 |
|
26218 |
|
26219 | setBytes(bytes, format) {
|
26220 | this.format = format;
|
26221 | this.data = bytes;
|
26222 | this.text = null;
|
26223 | }
|
26224 |
|
26225 |
|
26226 | |
26227 |
|
26228 |
|
26229 |
|
26230 |
|
26231 | getBytes(clone = false) {
|
26232 | if (this.data === null) {
|
26233 |
|
26234 | this.data = util.canonicalizeEOL(util.encodeUtf8(this.text));
|
26235 | }
|
26236 | if (clone) {
|
26237 | return stream.passiveClone(this.data);
|
26238 | }
|
26239 | return this.data;
|
26240 | }
|
26241 |
|
26242 |
|
26243 | |
26244 |
|
26245 |
|
26246 |
|
26247 | setFilename(filename) {
|
26248 | this.filename = filename;
|
26249 | }
|
26250 |
|
26251 |
|
26252 | |
26253 |
|
26254 |
|
26255 |
|
26256 | getFilename() {
|
26257 | return this.filename;
|
26258 | }
|
26259 |
|
26260 |
|
26261 | |
26262 |
|
26263 |
|
26264 |
|
26265 |
|
26266 |
|
26267 | async read(bytes) {
|
26268 | await stream.parse(bytes, async reader => {
|
26269 |
|
26270 | const format = enums.read(enums.literal, await reader.readByte());
|
26271 |
|
26272 | const filename_len = await reader.readByte();
|
26273 | this.filename = util.decodeUtf8(await reader.readBytes(filename_len));
|
26274 |
|
26275 | this.date = util.readDate(await reader.readBytes(4));
|
26276 |
|
26277 | const data = reader.remainder();
|
26278 |
|
26279 | this.setBytes(data, format);
|
26280 | });
|
26281 | }
|
26282 |
|
26283 | |
26284 |
|
26285 |
|
26286 |
|
26287 |
|
26288 | writeHeader() {
|
26289 | const filename = util.encodeUtf8(this.filename);
|
26290 | const filename_length = new Uint8Array([filename.length]);
|
26291 |
|
26292 | const format = new Uint8Array([enums.write(enums.literal, this.format)]);
|
26293 | const date = util.writeDate(this.date);
|
26294 |
|
26295 | return util.concatUint8Array([format, filename_length, filename, date]);
|
26296 | }
|
26297 |
|
26298 | |
26299 |
|
26300 |
|
26301 |
|
26302 |
|
26303 | write() {
|
26304 | const header = this.writeHeader();
|
26305 | const data = this.getBytes();
|
26306 |
|
26307 | return util.concat([header, data]);
|
26308 | }
|
26309 | }
|
26310 |
|
26311 |
|
26312 |
|
26313 |
|
26314 |
|
26315 |
|
26316 |
|
26317 |
|
26318 |
|
26319 |
|
26320 |
|
26321 |
|
26322 |
|
26323 |
|
26324 | class PublicKeyPacket {
|
26325 | |
26326 |
|
26327 |
|
26328 |
|
26329 | constructor(date = new Date(), config = defaultConfig) {
|
26330 | |
26331 |
|
26332 |
|
26333 |
|
26334 | this.tag = enums.packet.publicKey;
|
26335 | |
26336 |
|
26337 |
|
26338 |
|
26339 | this.version = config.v5Keys ? 5 : 4;
|
26340 | |
26341 |
|
26342 |
|
26343 |
|
26344 | this.created = util.normalizeDate(date);
|
26345 | |
26346 |
|
26347 |
|
26348 |
|
26349 | this.algorithm = null;
|
26350 | |
26351 |
|
26352 |
|
26353 |
|
26354 | this.publicParams = null;
|
26355 | |
26356 |
|
26357 |
|
26358 |
|
26359 | this.expirationTimeV3 = 0;
|
26360 | |
26361 |
|
26362 |
|
26363 |
|
26364 | this.fingerprint = null;
|
26365 | |
26366 |
|
26367 |
|
26368 |
|
26369 | this.keyid = null;
|
26370 | }
|
26371 |
|
26372 | |
26373 |
|
26374 |
|
26375 |
|
26376 |
|
26377 |
|
26378 | read(bytes) {
|
26379 | let pos = 0;
|
26380 |
|
26381 | this.version = bytes[pos++];
|
26382 |
|
26383 | if (this.version === 4 || this.version === 5) {
|
26384 |
|
26385 | this.created = util.readDate(bytes.subarray(pos, pos + 4));
|
26386 | pos += 4;
|
26387 |
|
26388 |
|
26389 | this.algorithm = enums.read(enums.publicKey, bytes[pos++]);
|
26390 | const algo = enums.write(enums.publicKey, this.algorithm);
|
26391 |
|
26392 | if (this.version === 5) {
|
26393 |
|
26394 | pos += 4;
|
26395 | }
|
26396 |
|
26397 |
|
26398 | try {
|
26399 | const { read, publicParams } = mod.parsePublicKeyParams(algo, bytes.subarray(pos));
|
26400 | this.publicParams = publicParams;
|
26401 | pos += read;
|
26402 | } catch (err) {
|
26403 | throw new Error('Error reading MPIs');
|
26404 | }
|
26405 |
|
26406 | return pos;
|
26407 | }
|
26408 | throw new Error('Version ' + this.version + ' of the key packet is unsupported.');
|
26409 | }
|
26410 |
|
26411 | |
26412 |
|
26413 |
|
26414 |
|
26415 | write() {
|
26416 | const arr = [];
|
26417 |
|
26418 | arr.push(new Uint8Array([this.version]));
|
26419 | arr.push(util.writeDate(this.created));
|
26420 |
|
26421 | const algo = enums.write(enums.publicKey, this.algorithm);
|
26422 | arr.push(new Uint8Array([algo]));
|
26423 |
|
26424 | const params = mod.serializeParams(algo, this.publicParams);
|
26425 | if (this.version === 5) {
|
26426 |
|
26427 | arr.push(util.writeNumber(params.length, 4));
|
26428 | }
|
26429 |
|
26430 | arr.push(params);
|
26431 | return util.concatUint8Array(arr);
|
26432 | }
|
26433 |
|
26434 | |
26435 |
|
26436 |
|
26437 | writeForHash(version) {
|
26438 | const bytes = this.writePublicKey();
|
26439 |
|
26440 | if (version === 5) {
|
26441 | return util.concatUint8Array([new Uint8Array([0x9A]), util.writeNumber(bytes.length, 4), bytes]);
|
26442 | }
|
26443 | return util.concatUint8Array([new Uint8Array([0x99]), util.writeNumber(bytes.length, 2), bytes]);
|
26444 | }
|
26445 |
|
26446 | |
26447 |
|
26448 |
|
26449 |
|
26450 | isDecrypted() {
|
26451 | return null;
|
26452 | }
|
26453 |
|
26454 | |
26455 |
|
26456 |
|
26457 |
|
26458 | getCreationTime() {
|
26459 | return this.created;
|
26460 | }
|
26461 |
|
26462 | |
26463 |
|
26464 |
|
26465 |
|
26466 | getKeyId() {
|
26467 | if (this.keyid) {
|
26468 | return this.keyid;
|
26469 | }
|
26470 | this.keyid = new Keyid();
|
26471 | if (this.version === 5) {
|
26472 | this.keyid.read(util.hexToUint8Array(this.getFingerprint()).subarray(0, 8));
|
26473 | } else if (this.version === 4) {
|
26474 | this.keyid.read(util.hexToUint8Array(this.getFingerprint()).subarray(12, 20));
|
26475 | }
|
26476 | return this.keyid;
|
26477 | }
|
26478 |
|
26479 | |
26480 |
|
26481 |
|
26482 |
|
26483 | getFingerprintBytes() {
|
26484 | if (this.fingerprint) {
|
26485 | return this.fingerprint;
|
26486 | }
|
26487 | const toHash = this.writeForHash(this.version);
|
26488 | if (this.version === 5) {
|
26489 | this.fingerprint = Sha256.bytes(toHash);
|
26490 | } else if (this.version === 4) {
|
26491 | this.fingerprint = Sha1.bytes(toHash);
|
26492 | }
|
26493 | return this.fingerprint;
|
26494 | }
|
26495 |
|
26496 | |
26497 |
|
26498 |
|
26499 |
|
26500 | getFingerprint() {
|
26501 | return util.uint8ArrayToHex(this.getFingerprintBytes());
|
26502 | }
|
26503 |
|
26504 | |
26505 |
|
26506 |
|
26507 |
|
26508 | hasSameFingerprintAs(other) {
|
26509 | return this.version === other.version && util.equalsUint8Array(this.writePublicKey(), other.writePublicKey());
|
26510 | }
|
26511 |
|
26512 | |
26513 |
|
26514 |
|
26515 |
|
26516 | getAlgorithmInfo() {
|
26517 | const result = {};
|
26518 | result.algorithm = this.algorithm;
|
26519 |
|
26520 | const modulo = this.publicParams.n || this.publicParams.p;
|
26521 | if (modulo) {
|
26522 | result.bits = modulo.length * 8;
|
26523 | } else {
|
26524 | result.curve = this.publicParams.oid.getName();
|
26525 | }
|
26526 | return result;
|
26527 | }
|
26528 | }
|
26529 |
|
26530 |
|
26531 |
|
26532 |
|
26533 |
|
26534 | PublicKeyPacket.prototype.readPublicKey = PublicKeyPacket.prototype.read;
|
26535 |
|
26536 |
|
26537 |
|
26538 |
|
26539 |
|
26540 | PublicKeyPacket.prototype.writePublicKey = PublicKeyPacket.prototype.write;
|
26541 |
|
26542 |
|
26543 |
|
26544 |
|
26545 |
|
26546 |
|
26547 |
|
26548 |
|
26549 |
|
26550 |
|
26551 |
|
26552 |
|
26553 |
|
26554 | class SymmetricallyEncryptedDataPacket {
|
26555 | constructor() {
|
26556 | |
26557 |
|
26558 |
|
26559 |
|
26560 | this.tag = enums.packet.symmetricallyEncryptedData;
|
26561 | |
26562 |
|
26563 |
|
26564 | this.encrypted = null;
|
26565 | |
26566 |
|
26567 |
|
26568 |
|
26569 | this.packets = null;
|
26570 | }
|
26571 |
|
26572 | read(bytes) {
|
26573 | this.encrypted = bytes;
|
26574 | }
|
26575 |
|
26576 | write() {
|
26577 | return this.encrypted;
|
26578 | }
|
26579 |
|
26580 | |
26581 |
|
26582 |
|
26583 |
|
26584 |
|
26585 |
|
26586 |
|
26587 |
|
26588 |
|
26589 |
|
26590 | async decrypt(sessionKeyAlgorithm, key, streaming, config = defaultConfig) {
|
26591 |
|
26592 | if (!config.allowUnauthenticatedMessages) {
|
26593 | throw new Error('Message is not authenticated.');
|
26594 | }
|
26595 |
|
26596 | const encrypted = await stream.readToEnd(stream.clone(this.encrypted));
|
26597 | const decrypted = await mod.cfb.decrypt(sessionKeyAlgorithm, key,
|
26598 | encrypted.subarray(mod.cipher[sessionKeyAlgorithm].blockSize + 2),
|
26599 | encrypted.subarray(2, mod.cipher[sessionKeyAlgorithm].blockSize + 2)
|
26600 | );
|
26601 |
|
26602 | await this.packets.read(decrypted, {
|
26603 | LiteralDataPacket,
|
26604 | CompressedDataPacket,
|
26605 | OnePassSignaturePacket,
|
26606 | SignaturePacket
|
26607 | }, streaming);
|
26608 | }
|
26609 |
|
26610 | |
26611 |
|
26612 |
|
26613 |
|
26614 |
|
26615 |
|
26616 |
|
26617 |
|
26618 |
|
26619 | async encrypt(algo, key, streaming, config = defaultConfig) {
|
26620 | const data = this.packets.write();
|
26621 |
|
26622 | const prefix = await mod.getPrefixRandom(algo);
|
26623 | const FRE = await mod.cfb.encrypt(algo, key, prefix, new Uint8Array(mod.cipher[algo].blockSize), config);
|
26624 | const ciphertext = await mod.cfb.encrypt(algo, key, data, FRE.subarray(2), config);
|
26625 | this.encrypted = util.concat([FRE, ciphertext]);
|
26626 | }
|
26627 | }
|
26628 |
|
26629 |
|
26630 |
|
26631 |
|
26632 |
|
26633 |
|
26634 |
|
26635 |
|
26636 |
|
26637 |
|
26638 |
|
26639 |
|
26640 |
|
26641 |
|
26642 | class MarkerPacket {
|
26643 | constructor() {
|
26644 | this.tag = enums.packet.marker;
|
26645 | }
|
26646 |
|
26647 | |
26648 |
|
26649 |
|
26650 |
|
26651 |
|
26652 |
|
26653 |
|
26654 |
|
26655 |
|
26656 |
|
26657 |
|
26658 | read(bytes) {
|
26659 | if (bytes[0] === 0x50 &&
|
26660 | bytes[1] === 0x47 &&
|
26661 | bytes[2] === 0x50) {
|
26662 | return true;
|
26663 | }
|
26664 |
|
26665 | return false;
|
26666 | }
|
26667 | }
|
26668 |
|
26669 |
|
26670 |
|
26671 |
|
26672 |
|
26673 |
|
26674 |
|
26675 |
|
26676 |
|
26677 |
|
26678 |
|
26679 | class PublicSubkeyPacket extends PublicKeyPacket {
|
26680 | |
26681 |
|
26682 |
|
26683 |
|
26684 | constructor(date, config) {
|
26685 | super(date, config);
|
26686 | this.tag = enums.packet.publicSubkey;
|
26687 | }
|
26688 | }
|
26689 |
|
26690 |
|
26691 |
|
26692 | function readSimpleLength(bytes) {
|
26693 | let len = 0;
|
26694 | let offset;
|
26695 | const type = bytes[0];
|
26696 |
|
26697 |
|
26698 | if (type < 192) {
|
26699 | [len] = bytes;
|
26700 | offset = 1;
|
26701 | } else if (type < 255) {
|
26702 | len = ((bytes[0] - 192) << 8) + (bytes[1]) + 192;
|
26703 | offset = 2;
|
26704 | } else if (type === 255) {
|
26705 | len = util.readNumber(bytes.subarray(1, 1 + 4));
|
26706 | offset = 5;
|
26707 | }
|
26708 |
|
26709 | return {
|
26710 | len: len,
|
26711 | offset: offset
|
26712 | };
|
26713 | }
|
26714 |
|
26715 |
|
26716 |
|
26717 |
|
26718 |
|
26719 |
|
26720 |
|
26721 |
|
26722 | function writeSimpleLength(length) {
|
26723 | if (length < 192) {
|
26724 | return new Uint8Array([length]);
|
26725 | } else if (length > 191 && length < 8384) {
|
26726 | |
26727 |
|
26728 |
|
26729 |
|
26730 | return new Uint8Array([((length - 192) >> 8) + 192, (length - 192) & 0xFF]);
|
26731 | }
|
26732 | return util.concatUint8Array([new Uint8Array([255]), util.writeNumber(length, 4)]);
|
26733 | }
|
26734 |
|
26735 | function writePartialLength(power) {
|
26736 | if (power < 0 || power > 30) {
|
26737 | throw new Error('Partial Length power must be between 1 and 30');
|
26738 | }
|
26739 | return new Uint8Array([224 + power]);
|
26740 | }
|
26741 |
|
26742 | function writeTag(tag_type) {
|
26743 |
|
26744 | return new Uint8Array([0xC0 | tag_type]);
|
26745 | }
|
26746 |
|
26747 |
|
26748 |
|
26749 |
|
26750 |
|
26751 |
|
26752 |
|
26753 |
|
26754 |
|
26755 | function writeHeader(tag_type, length) {
|
26756 |
|
26757 | return util.concatUint8Array([writeTag(tag_type), writeSimpleLength(length)]);
|
26758 | }
|
26759 |
|
26760 |
|
26761 |
|
26762 |
|
26763 |
|
26764 |
|
26765 | function supportsStreaming(tag_type) {
|
26766 | return [
|
26767 | enums.packet.literalData,
|
26768 | enums.packet.compressedData,
|
26769 | enums.packet.symmetricallyEncryptedData,
|
26770 | enums.packet.symEncryptedIntegrityProtectedData,
|
26771 | enums.packet.AEADEncryptedData
|
26772 | ].includes(tag_type);
|
26773 | }
|
26774 |
|
26775 |
|
26776 |
|
26777 |
|
26778 |
|
26779 |
|
26780 |
|
26781 |
|
26782 | async function readPackets(input, streaming, callback) {
|
26783 | const reader = stream.getReader(input);
|
26784 | let writer;
|
26785 | let callbackReturned;
|
26786 | try {
|
26787 | const peekedBytes = await reader.peekBytes(2);
|
26788 |
|
26789 | if (!peekedBytes || peekedBytes.length < 2 || (peekedBytes[0] & 0x80) === 0) {
|
26790 | throw new Error("Error during parsing. This message / key probably does not conform to a valid OpenPGP format.");
|
26791 | }
|
26792 | const headerByte = await reader.readByte();
|
26793 | let tag = -1;
|
26794 | let format = -1;
|
26795 | let packet_length;
|
26796 |
|
26797 | format = 0;
|
26798 | if ((headerByte & 0x40) !== 0) {
|
26799 | format = 1;
|
26800 | }
|
26801 |
|
26802 | let packet_length_type;
|
26803 | if (format) {
|
26804 |
|
26805 | tag = headerByte & 0x3F;
|
26806 | } else {
|
26807 |
|
26808 | tag = (headerByte & 0x3F) >> 2;
|
26809 | packet_length_type = headerByte & 0x03;
|
26810 | }
|
26811 |
|
26812 | const packetSupportsStreaming = supportsStreaming(tag);
|
26813 | let packet = null;
|
26814 | if (streaming && packetSupportsStreaming) {
|
26815 | const transform = new stream.TransformStream();
|
26816 | writer = stream.getWriter(transform.writable);
|
26817 | packet = transform.readable;
|
26818 | callbackReturned = callback({ tag, packet });
|
26819 | } else {
|
26820 | packet = [];
|
26821 | }
|
26822 |
|
26823 | let wasPartialLength;
|
26824 | do {
|
26825 | if (!format) {
|
26826 |
|
26827 | switch (packet_length_type) {
|
26828 | case 0:
|
26829 |
|
26830 |
|
26831 | packet_length = await reader.readByte();
|
26832 | break;
|
26833 | case 1:
|
26834 |
|
26835 |
|
26836 | packet_length = (await reader.readByte() << 8) | await reader.readByte();
|
26837 | break;
|
26838 | case 2:
|
26839 |
|
26840 |
|
26841 | packet_length = (await reader.readByte() << 24) | (await reader.readByte() << 16) | (await reader.readByte() <<
|
26842 | 8) | await reader.readByte();
|
26843 | break;
|
26844 | default:
|
26845 |
|
26846 |
|
26847 |
|
26848 |
|
26849 |
|
26850 |
|
26851 |
|
26852 |
|
26853 |
|
26854 |
|
26855 | packet_length = Infinity;
|
26856 | break;
|
26857 | }
|
26858 | } else {
|
26859 |
|
26860 | const lengthByte = await reader.readByte();
|
26861 | wasPartialLength = false;
|
26862 | if (lengthByte < 192) {
|
26863 | packet_length = lengthByte;
|
26864 |
|
26865 | } else if (lengthByte >= 192 && lengthByte < 224) {
|
26866 | packet_length = ((lengthByte - 192) << 8) + (await reader.readByte()) + 192;
|
26867 |
|
26868 | } else if (lengthByte > 223 && lengthByte < 255) {
|
26869 | packet_length = 1 << (lengthByte & 0x1F);
|
26870 | wasPartialLength = true;
|
26871 | if (!packetSupportsStreaming) {
|
26872 | throw new TypeError('This packet type does not support partial lengths.');
|
26873 | }
|
26874 |
|
26875 | } else {
|
26876 | packet_length = (await reader.readByte() << 24) | (await reader.readByte() << 16) | (await reader.readByte() <<
|
26877 | 8) | await reader.readByte();
|
26878 | }
|
26879 | }
|
26880 | if (packet_length > 0) {
|
26881 | let bytesRead = 0;
|
26882 | while (true) {
|
26883 | if (writer) await writer.ready;
|
26884 | const { done, value } = await reader.read();
|
26885 | if (done) {
|
26886 | if (packet_length === Infinity) break;
|
26887 | throw new Error('Unexpected end of packet');
|
26888 | }
|
26889 | const chunk = packet_length === Infinity ? value : value.subarray(0, packet_length - bytesRead);
|
26890 | if (writer) await writer.write(chunk);
|
26891 | else packet.push(chunk);
|
26892 | bytesRead += value.length;
|
26893 | if (bytesRead >= packet_length) {
|
26894 | reader.unshift(value.subarray(packet_length - bytesRead + value.length));
|
26895 | break;
|
26896 | }
|
26897 | }
|
26898 | }
|
26899 | } while (wasPartialLength);
|
26900 |
|
26901 |
|
26902 |
|
26903 |
|
26904 |
|
26905 |
|
26906 |
|
26907 |
|
26908 |
|
26909 |
|
26910 |
|
26911 |
|
26912 |
|
26913 |
|
26914 |
|
26915 |
|
26916 |
|
26917 |
|
26918 |
|
26919 |
|
26920 |
|
26921 |
|
26922 |
|
26923 |
|
26924 |
|
26925 |
|
26926 |
|
26927 |
|
26928 |
|
26929 |
|
26930 |
|
26931 |
|
26932 | const nextPacket = await reader.peekBytes(packetSupportsStreaming ? Infinity : 2);
|
26933 | if (writer) {
|
26934 | await writer.ready;
|
26935 | await writer.close();
|
26936 | } else {
|
26937 | packet = util.concatUint8Array(packet);
|
26938 | await callback({ tag, packet });
|
26939 | }
|
26940 | return !nextPacket || !nextPacket.length;
|
26941 | } catch (e) {
|
26942 | if (writer) {
|
26943 | await writer.abort(e);
|
26944 | return true;
|
26945 | } else {
|
26946 | throw e;
|
26947 | }
|
26948 | } finally {
|
26949 | if (writer) {
|
26950 | await callbackReturned;
|
26951 | }
|
26952 | reader.releaseLock();
|
26953 | }
|
26954 | }
|
26955 |
|
26956 |
|
26957 |
|
26958 |
|
26959 |
|
26960 |
|
26961 |
|
26962 |
|
26963 |
|
26964 |
|
26965 |
|
26966 |
|
26967 |
|
26968 |
|
26969 |
|
26970 |
|
26971 |
|
26972 |
|
26973 |
|
26974 |
|
26975 | class UserAttributePacket {
|
26976 | constructor() {
|
26977 | this.tag = enums.packet.userAttribute;
|
26978 | this.attributes = [];
|
26979 | }
|
26980 |
|
26981 | |
26982 |
|
26983 |
|
26984 |
|
26985 | read(bytes) {
|
26986 | let i = 0;
|
26987 | while (i < bytes.length) {
|
26988 | const len = readSimpleLength(bytes.subarray(i, bytes.length));
|
26989 | i += len.offset;
|
26990 |
|
26991 | this.attributes.push(util.uint8ArrayToStr(bytes.subarray(i, i + len.len)));
|
26992 | i += len.len;
|
26993 | }
|
26994 | }
|
26995 |
|
26996 | |
26997 |
|
26998 |
|
26999 |
|
27000 | write() {
|
27001 | const arr = [];
|
27002 | for (let i = 0; i < this.attributes.length; i++) {
|
27003 | arr.push(writeSimpleLength(this.attributes[i].length));
|
27004 | arr.push(util.strToUint8Array(this.attributes[i]));
|
27005 | }
|
27006 | return util.concatUint8Array(arr);
|
27007 | }
|
27008 |
|
27009 | |
27010 |
|
27011 |
|
27012 |
|
27013 |
|
27014 | equals(usrAttr) {
|
27015 | if (!usrAttr || !(usrAttr instanceof UserAttributePacket)) {
|
27016 | return false;
|
27017 | }
|
27018 | return this.attributes.every(function(attr, index) {
|
27019 | return attr === usrAttr.attributes[index];
|
27020 | });
|
27021 | }
|
27022 | }
|
27023 |
|
27024 |
|
27025 |
|
27026 |
|
27027 |
|
27028 |
|
27029 |
|
27030 |
|
27031 |
|
27032 |
|
27033 |
|
27034 | class SignaturePacket {
|
27035 | |
27036 |
|
27037 |
|
27038 | constructor(date = new Date()) {
|
27039 | this.tag = enums.packet.signature;
|
27040 | this.version = 4;
|
27041 | this.signatureType = null;
|
27042 | this.hashAlgorithm = null;
|
27043 | this.publicKeyAlgorithm = null;
|
27044 |
|
27045 | this.signatureData = null;
|
27046 | this.unhashedSubpackets = [];
|
27047 | this.signedHashValue = null;
|
27048 |
|
27049 | this.created = util.normalizeDate(date);
|
27050 | this.signatureExpirationTime = null;
|
27051 | this.signatureNeverExpires = true;
|
27052 | this.exportable = null;
|
27053 | this.trustLevel = null;
|
27054 | this.trustAmount = null;
|
27055 | this.regularExpression = null;
|
27056 | this.revocable = null;
|
27057 | this.keyExpirationTime = null;
|
27058 | this.keyNeverExpires = null;
|
27059 | this.preferredSymmetricAlgorithms = null;
|
27060 | this.revocationKeyClass = null;
|
27061 | this.revocationKeyAlgorithm = null;
|
27062 | this.revocationKeyFingerprint = null;
|
27063 | this.issuerKeyId = new Keyid();
|
27064 | this.rawNotations = [];
|
27065 | this.notations = {};
|
27066 | this.preferredHashAlgorithms = null;
|
27067 | this.preferredCompressionAlgorithms = null;
|
27068 | this.keyServerPreferences = null;
|
27069 | this.preferredKeyServer = null;
|
27070 | this.isPrimaryUserID = null;
|
27071 | this.policyURI = null;
|
27072 | this.keyFlags = null;
|
27073 | this.signersUserId = null;
|
27074 | this.reasonForRevocationFlag = null;
|
27075 | this.reasonForRevocationString = null;
|
27076 | this.features = null;
|
27077 | this.signatureTargetPublicKeyAlgorithm = null;
|
27078 | this.signatureTargetHashAlgorithm = null;
|
27079 | this.signatureTargetHash = null;
|
27080 | this.embeddedSignature = null;
|
27081 | this.issuerKeyVersion = null;
|
27082 | this.issuerFingerprint = null;
|
27083 | this.preferredAeadAlgorithms = null;
|
27084 |
|
27085 | this.verified = null;
|
27086 | this.revoked = null;
|
27087 | }
|
27088 |
|
27089 | |
27090 |
|
27091 |
|
27092 |
|
27093 |
|
27094 | read(bytes) {
|
27095 | let i = 0;
|
27096 | this.version = bytes[i++];
|
27097 |
|
27098 | if (this.version !== 4 && this.version !== 5) {
|
27099 | throw new Error('Version ' + this.version + ' of the signature is unsupported.');
|
27100 | }
|
27101 |
|
27102 | this.signatureType = bytes[i++];
|
27103 | this.publicKeyAlgorithm = bytes[i++];
|
27104 | this.hashAlgorithm = bytes[i++];
|
27105 |
|
27106 |
|
27107 | i += this.read_sub_packets(bytes.subarray(i, bytes.length), true);
|
27108 |
|
27109 |
|
27110 |
|
27111 |
|
27112 |
|
27113 |
|
27114 |
|
27115 | this.signatureData = bytes.subarray(0, i);
|
27116 |
|
27117 |
|
27118 | i += this.read_sub_packets(bytes.subarray(i, bytes.length), false);
|
27119 |
|
27120 |
|
27121 | this.signedHashValue = bytes.subarray(i, i + 2);
|
27122 | i += 2;
|
27123 |
|
27124 | this.params = mod.signature.parseSignatureParams(this.publicKeyAlgorithm, bytes.subarray(i, bytes.length));
|
27125 | }
|
27126 |
|
27127 | |
27128 |
|
27129 |
|
27130 | write_params() {
|
27131 | if (this.params instanceof Promise) {
|
27132 | return stream.fromAsync(
|
27133 | async () => mod.serializeParams(this.publicKeyAlgorithm, await this.params)
|
27134 | );
|
27135 | }
|
27136 | return mod.serializeParams(this.publicKeyAlgorithm, this.params);
|
27137 | }
|
27138 |
|
27139 | write() {
|
27140 | const arr = [];
|
27141 | arr.push(this.signatureData);
|
27142 | arr.push(this.write_unhashed_sub_packets());
|
27143 | arr.push(this.signedHashValue);
|
27144 | arr.push(this.write_params());
|
27145 | return util.concat(arr);
|
27146 | }
|
27147 |
|
27148 | |
27149 |
|
27150 |
|
27151 |
|
27152 |
|
27153 |
|
27154 |
|
27155 |
|
27156 |
|
27157 | async sign(key, data, detached = false, streaming = false) {
|
27158 | const signatureType = enums.write(enums.signature, this.signatureType);
|
27159 | const publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm);
|
27160 | const hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm);
|
27161 |
|
27162 | if (key.version === 5) {
|
27163 | this.version = 5;
|
27164 | }
|
27165 | const arr = [new Uint8Array([this.version, signatureType, publicKeyAlgorithm, hashAlgorithm])];
|
27166 |
|
27167 | this.issuerKeyVersion = key.version;
|
27168 | this.issuerFingerprint = key.getFingerprintBytes();
|
27169 | this.issuerKeyId = key.getKeyId();
|
27170 |
|
27171 |
|
27172 | arr.push(this.write_hashed_sub_packets());
|
27173 |
|
27174 | this.signatureData = util.concat(arr);
|
27175 |
|
27176 | const toHash = this.toHash(signatureType, data, detached);
|
27177 | const hash = await this.hash(signatureType, data, toHash, detached);
|
27178 |
|
27179 | this.signedHashValue = stream.slice(stream.clone(hash), 0, 2);
|
27180 | const signed = async () => mod.signature.sign(
|
27181 | publicKeyAlgorithm, hashAlgorithm, key.publicParams, key.privateParams, toHash, await stream.readToEnd(hash)
|
27182 | );
|
27183 | if (streaming) {
|
27184 | this.params = signed();
|
27185 | } else {
|
27186 | this.params = await signed();
|
27187 |
|
27188 |
|
27189 |
|
27190 |
|
27191 |
|
27192 | this.verified = true;
|
27193 | }
|
27194 | }
|
27195 |
|
27196 | |
27197 |
|
27198 |
|
27199 |
|
27200 | write_hashed_sub_packets() {
|
27201 | const sub = enums.signatureSubpacket;
|
27202 | const arr = [];
|
27203 | let bytes;
|
27204 | if (this.created !== null) {
|
27205 | arr.push(write_sub_packet(sub.signatureCreationTime, util.writeDate(this.created)));
|
27206 | }
|
27207 | if (this.signatureExpirationTime !== null) {
|
27208 | arr.push(write_sub_packet(sub.signatureExpirationTime, util.writeNumber(this.signatureExpirationTime, 4)));
|
27209 | }
|
27210 | if (this.exportable !== null) {
|
27211 | arr.push(write_sub_packet(sub.exportableCertification, new Uint8Array([this.exportable ? 1 : 0])));
|
27212 | }
|
27213 | if (this.trustLevel !== null) {
|
27214 | bytes = new Uint8Array([this.trustLevel, this.trustAmount]);
|
27215 | arr.push(write_sub_packet(sub.trustSignature, bytes));
|
27216 | }
|
27217 | if (this.regularExpression !== null) {
|
27218 | arr.push(write_sub_packet(sub.regularExpression, this.regularExpression));
|
27219 | }
|
27220 | if (this.revocable !== null) {
|
27221 | arr.push(write_sub_packet(sub.revocable, new Uint8Array([this.revocable ? 1 : 0])));
|
27222 | }
|
27223 | if (this.keyExpirationTime !== null) {
|
27224 | arr.push(write_sub_packet(sub.keyExpirationTime, util.writeNumber(this.keyExpirationTime, 4)));
|
27225 | }
|
27226 | if (this.preferredSymmetricAlgorithms !== null) {
|
27227 | bytes = util.strToUint8Array(util.uint8ArrayToStr(this.preferredSymmetricAlgorithms));
|
27228 | arr.push(write_sub_packet(sub.preferredSymmetricAlgorithms, bytes));
|
27229 | }
|
27230 | if (this.revocationKeyClass !== null) {
|
27231 | bytes = new Uint8Array([this.revocationKeyClass, this.revocationKeyAlgorithm]);
|
27232 | bytes = util.concat([bytes, this.revocationKeyFingerprint]);
|
27233 | arr.push(write_sub_packet(sub.revocationKey, bytes));
|
27234 | }
|
27235 | this.rawNotations.forEach(([{ name, value, humanReadable }]) => {
|
27236 | bytes = [new Uint8Array([humanReadable ? 0x80 : 0, 0, 0, 0])];
|
27237 |
|
27238 | bytes.push(util.writeNumber(name.length, 2));
|
27239 |
|
27240 | bytes.push(util.writeNumber(value.length, 2));
|
27241 | bytes.push(util.strToUint8Array(name));
|
27242 | bytes.push(value);
|
27243 | bytes = util.concat(bytes);
|
27244 | arr.push(write_sub_packet(sub.notationData, bytes));
|
27245 | });
|
27246 | if (this.preferredHashAlgorithms !== null) {
|
27247 | bytes = util.strToUint8Array(util.uint8ArrayToStr(this.preferredHashAlgorithms));
|
27248 | arr.push(write_sub_packet(sub.preferredHashAlgorithms, bytes));
|
27249 | }
|
27250 | if (this.preferredCompressionAlgorithms !== null) {
|
27251 | bytes = util.strToUint8Array(util.uint8ArrayToStr(this.preferredCompressionAlgorithms));
|
27252 | arr.push(write_sub_packet(sub.preferredCompressionAlgorithms, bytes));
|
27253 | }
|
27254 | if (this.keyServerPreferences !== null) {
|
27255 | bytes = util.strToUint8Array(util.uint8ArrayToStr(this.keyServerPreferences));
|
27256 | arr.push(write_sub_packet(sub.keyServerPreferences, bytes));
|
27257 | }
|
27258 | if (this.preferredKeyServer !== null) {
|
27259 | arr.push(write_sub_packet(sub.preferredKeyServer, util.strToUint8Array(this.preferredKeyServer)));
|
27260 | }
|
27261 | if (this.isPrimaryUserID !== null) {
|
27262 | arr.push(write_sub_packet(sub.primaryUserId, new Uint8Array([this.isPrimaryUserID ? 1 : 0])));
|
27263 | }
|
27264 | if (this.policyURI !== null) {
|
27265 | arr.push(write_sub_packet(sub.policyUri, util.strToUint8Array(this.policyURI)));
|
27266 | }
|
27267 | if (this.keyFlags !== null) {
|
27268 | bytes = util.strToUint8Array(util.uint8ArrayToStr(this.keyFlags));
|
27269 | arr.push(write_sub_packet(sub.keyFlags, bytes));
|
27270 | }
|
27271 | if (this.signersUserId !== null) {
|
27272 | arr.push(write_sub_packet(sub.signersUserId, util.strToUint8Array(this.signersUserId)));
|
27273 | }
|
27274 | if (this.reasonForRevocationFlag !== null) {
|
27275 | bytes = util.strToUint8Array(String.fromCharCode(this.reasonForRevocationFlag) + this.reasonForRevocationString);
|
27276 | arr.push(write_sub_packet(sub.reasonForRevocation, bytes));
|
27277 | }
|
27278 | if (this.features !== null) {
|
27279 | bytes = util.strToUint8Array(util.uint8ArrayToStr(this.features));
|
27280 | arr.push(write_sub_packet(sub.features, bytes));
|
27281 | }
|
27282 | if (this.signatureTargetPublicKeyAlgorithm !== null) {
|
27283 | bytes = [new Uint8Array([this.signatureTargetPublicKeyAlgorithm, this.signatureTargetHashAlgorithm])];
|
27284 | bytes.push(util.strToUint8Array(this.signatureTargetHash));
|
27285 | bytes = util.concat(bytes);
|
27286 | arr.push(write_sub_packet(sub.signatureTarget, bytes));
|
27287 | }
|
27288 | if (this.preferredAeadAlgorithms !== null) {
|
27289 | bytes = util.strToUint8Array(util.uint8ArrayToStr(this.preferredAeadAlgorithms));
|
27290 | arr.push(write_sub_packet(sub.preferredAeadAlgorithms, bytes));
|
27291 | }
|
27292 |
|
27293 | const result = util.concat(arr);
|
27294 | const length = util.writeNumber(result.length, 2);
|
27295 |
|
27296 | return util.concat([length, result]);
|
27297 | }
|
27298 |
|
27299 | |
27300 |
|
27301 |
|
27302 |
|
27303 | write_unhashed_sub_packets() {
|
27304 | const sub = enums.signatureSubpacket;
|
27305 | const arr = [];
|
27306 | let bytes;
|
27307 | if (!this.issuerKeyId.isNull() && this.issuerKeyVersion !== 5) {
|
27308 |
|
27309 |
|
27310 | arr.push(write_sub_packet(sub.issuer, this.issuerKeyId.write()));
|
27311 | }
|
27312 | if (this.embeddedSignature !== null) {
|
27313 | arr.push(write_sub_packet(sub.embeddedSignature, this.embeddedSignature.write()));
|
27314 | }
|
27315 | if (this.issuerFingerprint !== null) {
|
27316 | bytes = [new Uint8Array([this.issuerKeyVersion]), this.issuerFingerprint];
|
27317 | bytes = util.concat(bytes);
|
27318 | arr.push(write_sub_packet(sub.issuerFingerprint, bytes));
|
27319 | }
|
27320 | this.unhashedSubpackets.forEach(data => {
|
27321 | arr.push(writeSimpleLength(data.length));
|
27322 | arr.push(data);
|
27323 | });
|
27324 |
|
27325 | const result = util.concat(arr);
|
27326 | const length = util.writeNumber(result.length, 2);
|
27327 |
|
27328 | return util.concat([length, result]);
|
27329 | }
|
27330 |
|
27331 |
|
27332 |
|
27333 | read_sub_packet(bytes, trusted = true) {
|
27334 | let mypos = 0;
|
27335 |
|
27336 | const read_array = (prop, bytes) => {
|
27337 | this[prop] = [];
|
27338 |
|
27339 | for (let i = 0; i < bytes.length; i++) {
|
27340 | this[prop].push(bytes[i]);
|
27341 | }
|
27342 | };
|
27343 |
|
27344 |
|
27345 | const critical = bytes[mypos] & 0x80;
|
27346 | const type = bytes[mypos] & 0x7F;
|
27347 |
|
27348 |
|
27349 |
|
27350 |
|
27351 | if (!trusted && ![
|
27352 | enums.signatureSubpacket.issuer,
|
27353 | enums.signatureSubpacket.issuerFingerprint,
|
27354 | enums.signatureSubpacket.embeddedSignature
|
27355 | ].includes(type)) {
|
27356 | this.unhashedSubpackets.push(bytes.subarray(mypos, bytes.length));
|
27357 | return;
|
27358 | }
|
27359 |
|
27360 | mypos++;
|
27361 |
|
27362 |
|
27363 | switch (type) {
|
27364 | case 2:
|
27365 |
|
27366 | this.created = util.readDate(bytes.subarray(mypos, bytes.length));
|
27367 | break;
|
27368 | case 3: {
|
27369 |
|
27370 | const seconds = util.readNumber(bytes.subarray(mypos, bytes.length));
|
27371 |
|
27372 | this.signatureNeverExpires = seconds === 0;
|
27373 | this.signatureExpirationTime = seconds;
|
27374 |
|
27375 | break;
|
27376 | }
|
27377 | case 4:
|
27378 |
|
27379 | this.exportable = bytes[mypos++] === 1;
|
27380 | break;
|
27381 | case 5:
|
27382 |
|
27383 | this.trustLevel = bytes[mypos++];
|
27384 | this.trustAmount = bytes[mypos++];
|
27385 | break;
|
27386 | case 6:
|
27387 |
|
27388 | this.regularExpression = bytes[mypos];
|
27389 | break;
|
27390 | case 7:
|
27391 |
|
27392 | this.revocable = bytes[mypos++] === 1;
|
27393 | break;
|
27394 | case 9: {
|
27395 |
|
27396 | const seconds = util.readNumber(bytes.subarray(mypos, bytes.length));
|
27397 |
|
27398 | this.keyExpirationTime = seconds;
|
27399 | this.keyNeverExpires = seconds === 0;
|
27400 |
|
27401 | break;
|
27402 | }
|
27403 | case 11:
|
27404 |
|
27405 | read_array('preferredSymmetricAlgorithms', bytes.subarray(mypos, bytes.length));
|
27406 | break;
|
27407 | case 12:
|
27408 |
|
27409 |
|
27410 |
|
27411 |
|
27412 | this.revocationKeyClass = bytes[mypos++];
|
27413 | this.revocationKeyAlgorithm = bytes[mypos++];
|
27414 | this.revocationKeyFingerprint = bytes.subarray(mypos, mypos + 20);
|
27415 | break;
|
27416 |
|
27417 | case 16:
|
27418 |
|
27419 | this.issuerKeyId.read(bytes.subarray(mypos, bytes.length));
|
27420 | break;
|
27421 |
|
27422 | case 20: {
|
27423 |
|
27424 | const humanReadable = !!(bytes[mypos] & 0x80);
|
27425 |
|
27426 |
|
27427 | mypos += 4;
|
27428 | const m = util.readNumber(bytes.subarray(mypos, mypos + 2));
|
27429 | mypos += 2;
|
27430 | const n = util.readNumber(bytes.subarray(mypos, mypos + 2));
|
27431 | mypos += 2;
|
27432 |
|
27433 | const name = util.uint8ArrayToStr(bytes.subarray(mypos, mypos + m));
|
27434 | const value = bytes.subarray(mypos + m, mypos + m + n);
|
27435 |
|
27436 | this.rawNotations.push({ name, humanReadable, value, critical });
|
27437 |
|
27438 | if (humanReadable) {
|
27439 | this.notations[name] = util.uint8ArrayToStr(value);
|
27440 | }
|
27441 | break;
|
27442 | }
|
27443 | case 21:
|
27444 |
|
27445 | read_array('preferredHashAlgorithms', bytes.subarray(mypos, bytes.length));
|
27446 | break;
|
27447 | case 22:
|
27448 |
|
27449 | read_array('preferredCompressionAlgorithms', bytes.subarray(mypos, bytes.length));
|
27450 | break;
|
27451 | case 23:
|
27452 |
|
27453 | read_array('keyServerPreferences', bytes.subarray(mypos, bytes.length));
|
27454 | break;
|
27455 | case 24:
|
27456 |
|
27457 | this.preferredKeyServer = util.uint8ArrayToStr(bytes.subarray(mypos, bytes.length));
|
27458 | break;
|
27459 | case 25:
|
27460 |
|
27461 | this.isPrimaryUserID = bytes[mypos++] !== 0;
|
27462 | break;
|
27463 | case 26:
|
27464 |
|
27465 | this.policyURI = util.uint8ArrayToStr(bytes.subarray(mypos, bytes.length));
|
27466 | break;
|
27467 | case 27:
|
27468 |
|
27469 | read_array('keyFlags', bytes.subarray(mypos, bytes.length));
|
27470 | break;
|
27471 | case 28:
|
27472 |
|
27473 | this.signersUserId = util.uint8ArrayToStr(bytes.subarray(mypos, bytes.length));
|
27474 | break;
|
27475 | case 29:
|
27476 |
|
27477 | this.reasonForRevocationFlag = bytes[mypos++];
|
27478 | this.reasonForRevocationString = util.uint8ArrayToStr(bytes.subarray(mypos, bytes.length));
|
27479 | break;
|
27480 | case 30:
|
27481 |
|
27482 | read_array('features', bytes.subarray(mypos, bytes.length));
|
27483 | break;
|
27484 | case 31: {
|
27485 |
|
27486 |
|
27487 | this.signatureTargetPublicKeyAlgorithm = bytes[mypos++];
|
27488 | this.signatureTargetHashAlgorithm = bytes[mypos++];
|
27489 |
|
27490 | const len = mod.getHashByteLength(this.signatureTargetHashAlgorithm);
|
27491 |
|
27492 | this.signatureTargetHash = util.uint8ArrayToStr(bytes.subarray(mypos, mypos + len));
|
27493 | break;
|
27494 | }
|
27495 | case 32:
|
27496 |
|
27497 | this.embeddedSignature = new SignaturePacket();
|
27498 | this.embeddedSignature.read(bytes.subarray(mypos, bytes.length));
|
27499 | break;
|
27500 | case 33:
|
27501 |
|
27502 | this.issuerKeyVersion = bytes[mypos++];
|
27503 | this.issuerFingerprint = bytes.subarray(mypos, bytes.length);
|
27504 | if (this.issuerKeyVersion === 5) {
|
27505 | this.issuerKeyId.read(this.issuerFingerprint);
|
27506 | } else {
|
27507 | this.issuerKeyId.read(this.issuerFingerprint.subarray(-8));
|
27508 | }
|
27509 | break;
|
27510 | case 34:
|
27511 |
|
27512 | read_array.call(this, 'preferredAeadAlgorithms', bytes.subarray(mypos, bytes.length));
|
27513 | break;
|
27514 | default: {
|
27515 | const err = new Error("Unknown signature subpacket type " + type + " @:" + mypos);
|
27516 | if (critical) {
|
27517 | throw err;
|
27518 | } else {
|
27519 | util.printDebug(err);
|
27520 | }
|
27521 | }
|
27522 | }
|
27523 | }
|
27524 |
|
27525 | read_sub_packets(bytes, trusted = true, config) {
|
27526 |
|
27527 | const subpacket_length = util.readNumber(bytes.subarray(0, 2));
|
27528 |
|
27529 | let i = 2;
|
27530 |
|
27531 |
|
27532 | while (i < 2 + subpacket_length) {
|
27533 | const len = readSimpleLength(bytes.subarray(i, bytes.length));
|
27534 | i += len.offset;
|
27535 |
|
27536 | this.read_sub_packet(bytes.subarray(i, i + len.len), trusted, config);
|
27537 |
|
27538 | i += len.len;
|
27539 | }
|
27540 |
|
27541 | return i;
|
27542 | }
|
27543 |
|
27544 |
|
27545 | toSign(type, data) {
|
27546 | const t = enums.signature;
|
27547 |
|
27548 | switch (type) {
|
27549 | case t.binary:
|
27550 | if (data.text !== null) {
|
27551 | return util.encodeUtf8(data.getText(true));
|
27552 | }
|
27553 | return data.getBytes(true);
|
27554 |
|
27555 | case t.text: {
|
27556 | const bytes = data.getBytes(true);
|
27557 |
|
27558 | return util.canonicalizeEOL(bytes);
|
27559 | }
|
27560 | case t.standalone:
|
27561 | return new Uint8Array(0);
|
27562 |
|
27563 | case t.certGeneric:
|
27564 | case t.certPersona:
|
27565 | case t.certCasual:
|
27566 | case t.certPositive:
|
27567 | case t.certRevocation: {
|
27568 | let packet;
|
27569 | let tag;
|
27570 |
|
27571 | if (data.userId) {
|
27572 | tag = 0xB4;
|
27573 | packet = data.userId;
|
27574 | } else if (data.userAttribute) {
|
27575 | tag = 0xD1;
|
27576 | packet = data.userAttribute;
|
27577 | } else {
|
27578 | throw new Error('Either a userId or userAttribute packet needs to be ' +
|
27579 | 'supplied for certification.');
|
27580 | }
|
27581 |
|
27582 | const bytes = packet.write();
|
27583 |
|
27584 | return util.concat([this.toSign(t.key, data),
|
27585 | new Uint8Array([tag]),
|
27586 | util.writeNumber(bytes.length, 4),
|
27587 | bytes]);
|
27588 | }
|
27589 | case t.subkeyBinding:
|
27590 | case t.subkeyRevocation:
|
27591 | case t.keyBinding:
|
27592 | return util.concat([this.toSign(t.key, data), this.toSign(t.key, {
|
27593 | key: data.bind
|
27594 | })]);
|
27595 |
|
27596 | case t.key:
|
27597 | if (data.key === undefined) {
|
27598 | throw new Error('Key packet is required for this signature.');
|
27599 | }
|
27600 | return data.key.writeForHash(this.version);
|
27601 |
|
27602 | case t.keyRevocation:
|
27603 | return this.toSign(t.key, data);
|
27604 | case t.timestamp:
|
27605 | return new Uint8Array(0);
|
27606 | case t.thirdParty:
|
27607 | throw new Error('Not implemented');
|
27608 | default:
|
27609 | throw new Error('Unknown signature type.');
|
27610 | }
|
27611 | }
|
27612 |
|
27613 | calculateTrailer(data, detached) {
|
27614 | let length = 0;
|
27615 | return stream.transform(stream.clone(this.signatureData), value => {
|
27616 | length += value.length;
|
27617 | }, () => {
|
27618 | const arr = [];
|
27619 | if (this.version === 5 && (this.signatureType === enums.signature.binary || this.signatureType === enums.signature.text)) {
|
27620 | if (detached) {
|
27621 | arr.push(new Uint8Array(6));
|
27622 | } else {
|
27623 | arr.push(data.writeHeader());
|
27624 | }
|
27625 | }
|
27626 | arr.push(new Uint8Array([this.version, 0xFF]));
|
27627 | if (this.version === 5) {
|
27628 | arr.push(new Uint8Array(4));
|
27629 | }
|
27630 | arr.push(util.writeNumber(length, 4));
|
27631 |
|
27632 |
|
27633 | return util.concat(arr);
|
27634 | });
|
27635 | }
|
27636 |
|
27637 | toHash(signatureType, data, detached = false) {
|
27638 | const bytes = this.toSign(signatureType, data);
|
27639 |
|
27640 | return util.concat([bytes, this.signatureData, this.calculateTrailer(data, detached)]);
|
27641 | }
|
27642 |
|
27643 | async hash(signatureType, data, toHash, detached = false, streaming = true) {
|
27644 | const hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm);
|
27645 | if (!toHash) toHash = this.toHash(signatureType, data, detached);
|
27646 | if (!streaming && util.isStream(toHash)) {
|
27647 | return stream.fromAsync(async () => this.hash(signatureType, data, await stream.readToEnd(toHash), detached));
|
27648 | }
|
27649 | return mod.hash.digest(hashAlgorithm, toHash);
|
27650 | }
|
27651 |
|
27652 | |
27653 |
|
27654 |
|
27655 |
|
27656 |
|
27657 |
|
27658 |
|
27659 |
|
27660 |
|
27661 |
|
27662 |
|
27663 |
|
27664 | async verify(key, signatureType, data, detached = false, streaming = false, config = defaultConfig) {
|
27665 | const publicKeyAlgorithm = enums.write(enums.publicKey, this.publicKeyAlgorithm);
|
27666 | const hashAlgorithm = enums.write(enums.hash, this.hashAlgorithm);
|
27667 |
|
27668 | if (publicKeyAlgorithm !== enums.write(enums.publicKey, key.algorithm)) {
|
27669 | throw new Error('Public key algorithm used to sign signature does not match issuer key algorithm.');
|
27670 | }
|
27671 |
|
27672 | let toHash;
|
27673 | let hash;
|
27674 | if (this.hashed) {
|
27675 | hash = await this.hashed;
|
27676 | } else {
|
27677 | toHash = this.toHash(signatureType, data, detached);
|
27678 | if (!streaming) toHash = await stream.readToEnd(toHash);
|
27679 | hash = await this.hash(signatureType, data, toHash);
|
27680 | }
|
27681 | hash = await stream.readToEnd(hash);
|
27682 | if (this.signedHashValue[0] !== hash[0] ||
|
27683 | this.signedHashValue[1] !== hash[1]) {
|
27684 | throw new Error('Message digest did not match');
|
27685 | }
|
27686 |
|
27687 | this.params = await this.params;
|
27688 |
|
27689 | const verified = await mod.signature.verify(
|
27690 | publicKeyAlgorithm, hashAlgorithm, this.params, key.publicParams,
|
27691 | toHash, hash
|
27692 | );
|
27693 | if (!verified) {
|
27694 | throw new Error('Signature verification failed');
|
27695 | }
|
27696 | if (config.rejectHashAlgorithms.has(hashAlgorithm)) {
|
27697 | throw new Error('Insecure hash algorithm: ' + enums.read(enums.hash, hashAlgorithm).toUpperCase());
|
27698 | }
|
27699 | if (config.rejectMessageHashAlgorithms.has(hashAlgorithm) &&
|
27700 | [enums.signature.binary, enums.signature.text].includes(this.signatureType)) {
|
27701 | throw new Error('Insecure message hash algorithm: ' + enums.read(enums.hash, hashAlgorithm).toUpperCase());
|
27702 | }
|
27703 | this.rawNotations.forEach(({ name, critical }) => {
|
27704 | if (critical && (config.knownNotations.indexOf(name) < 0)) {
|
27705 | throw new Error(`Unknown critical notation: ${name}`);
|
27706 | }
|
27707 | });
|
27708 | if (this.revocationKeyClass !== null) {
|
27709 | throw new Error('This key is intended to be revoked with an authorized key, which OpenPGP.js does not support.');
|
27710 | }
|
27711 | this.verified = true;
|
27712 | }
|
27713 |
|
27714 | |
27715 |
|
27716 |
|
27717 |
|
27718 |
|
27719 | isExpired(date = new Date()) {
|
27720 | const normDate = util.normalizeDate(date);
|
27721 | if (normDate !== null) {
|
27722 | const expirationTime = this.getExpirationTime();
|
27723 | return !(this.created <= normDate && normDate <= expirationTime);
|
27724 | }
|
27725 | return false;
|
27726 | }
|
27727 |
|
27728 | |
27729 |
|
27730 |
|
27731 |
|
27732 | getExpirationTime() {
|
27733 | return !this.signatureNeverExpires ? new Date(this.created.getTime() + this.signatureExpirationTime * 1000) : Infinity;
|
27734 | }
|
27735 | }
|
27736 |
|
27737 |
|
27738 |
|
27739 |
|
27740 |
|
27741 |
|
27742 |
|
27743 |
|
27744 |
|
27745 |
|
27746 | function write_sub_packet(type, data) {
|
27747 | const arr = [];
|
27748 | arr.push(writeSimpleLength(data.length + 1));
|
27749 | arr.push(new Uint8Array([type]));
|
27750 | arr.push(data);
|
27751 | return util.concat(arr);
|
27752 | }
|
27753 |
|
27754 |
|
27755 |
|
27756 |
|
27757 |
|
27758 |
|
27759 |
|
27760 |
|
27761 |
|
27762 |
|
27763 |
|
27764 |
|
27765 |
|
27766 | class OnePassSignaturePacket {
|
27767 | constructor() {
|
27768 | |
27769 |
|
27770 |
|
27771 |
|
27772 | this.tag = enums.packet.onePassSignature;
|
27773 |
|
27774 | this.version = null;
|
27775 | |
27776 |
|
27777 |
|
27778 |
|
27779 |
|
27780 | this.signatureType = null;
|
27781 | |
27782 |
|
27783 |
|
27784 |
|
27785 | this.hashAlgorithm = null;
|
27786 | |
27787 |
|
27788 |
|
27789 |
|
27790 | this.publicKeyAlgorithm = null;
|
27791 |
|
27792 | this.issuerKeyId = null;
|
27793 | |
27794 |
|
27795 |
|
27796 |
|
27797 |
|
27798 | this.flags = null;
|
27799 | }
|
27800 |
|
27801 | |
27802 |
|
27803 |
|
27804 |
|
27805 |
|
27806 | read(bytes) {
|
27807 | let mypos = 0;
|
27808 |
|
27809 | this.version = bytes[mypos++];
|
27810 |
|
27811 |
|
27812 |
|
27813 | this.signatureType = bytes[mypos++];
|
27814 |
|
27815 |
|
27816 | this.hashAlgorithm = bytes[mypos++];
|
27817 |
|
27818 |
|
27819 | this.publicKeyAlgorithm = bytes[mypos++];
|
27820 |
|
27821 |
|
27822 | this.issuerKeyId = new Keyid();
|
27823 | this.issuerKeyId.read(bytes.subarray(mypos, mypos + 8));
|
27824 | mypos += 8;
|
27825 |
|
27826 |
|
27827 |
|
27828 |
|
27829 |
|
27830 | this.flags = bytes[mypos++];
|
27831 | return this;
|
27832 | }
|
27833 |
|
27834 | |
27835 |
|
27836 |
|
27837 |
|
27838 | write() {
|
27839 | const start = new Uint8Array([3, enums.write(enums.signature, this.signatureType),
|
27840 | enums.write(enums.hash, this.hashAlgorithm),
|
27841 | enums.write(enums.publicKey, this.publicKeyAlgorithm)]);
|
27842 |
|
27843 | const end = new Uint8Array([this.flags]);
|
27844 |
|
27845 | return util.concatUint8Array([start, this.issuerKeyId.write(), end]);
|
27846 | }
|
27847 |
|
27848 | calculateTrailer(...args) {
|
27849 | return stream.fromAsync(async () => SignaturePacket.prototype.calculateTrailer.apply(await this.correspondingSig, args));
|
27850 | }
|
27851 |
|
27852 | async verify() {
|
27853 | const correspondingSig = await this.correspondingSig;
|
27854 | if (!correspondingSig || correspondingSig.tag !== enums.packet.signature) {
|
27855 | throw new Error('Corresponding signature packet missing');
|
27856 | }
|
27857 | if (
|
27858 | correspondingSig.signatureType !== this.signatureType ||
|
27859 | correspondingSig.hashAlgorithm !== this.hashAlgorithm ||
|
27860 | correspondingSig.publicKeyAlgorithm !== this.publicKeyAlgorithm ||
|
27861 | !correspondingSig.issuerKeyId.equals(this.issuerKeyId)
|
27862 | ) {
|
27863 | throw new Error('Corresponding signature packet does not match one-pass signature packet');
|
27864 | }
|
27865 | correspondingSig.hashed = this.hashed;
|
27866 | return correspondingSig.verify.apply(correspondingSig, arguments);
|
27867 | }
|
27868 | }
|
27869 |
|
27870 | OnePassSignaturePacket.prototype.hash = SignaturePacket.prototype.hash;
|
27871 | OnePassSignaturePacket.prototype.toHash = SignaturePacket.prototype.toHash;
|
27872 | OnePassSignaturePacket.prototype.toSign = SignaturePacket.prototype.toSign;
|
27873 |
|
27874 |
|
27875 |
|
27876 |
|
27877 |
|
27878 |
|
27879 |
|
27880 |
|
27881 |
|
27882 | class SecretKeyPacket extends PublicKeyPacket {
|
27883 | |
27884 |
|
27885 |
|
27886 |
|
27887 | constructor(date = new Date(), config = defaultConfig) {
|
27888 | super(date, config);
|
27889 | |
27890 |
|
27891 |
|
27892 |
|
27893 | this.tag = enums.packet.secretKey;
|
27894 | |
27895 |
|
27896 |
|
27897 | this.keyMaterial = null;
|
27898 | |
27899 |
|
27900 |
|
27901 | this.isEncrypted = null;
|
27902 | |
27903 |
|
27904 |
|
27905 |
|
27906 | this.s2k_usage = 0;
|
27907 | |
27908 |
|
27909 |
|
27910 |
|
27911 | this.s2k = null;
|
27912 | |
27913 |
|
27914 |
|
27915 |
|
27916 | this.symmetric = null;
|
27917 | |
27918 |
|
27919 |
|
27920 |
|
27921 | this.aead = null;
|
27922 | |
27923 |
|
27924 |
|
27925 |
|
27926 | this.privateParams = null;
|
27927 | }
|
27928 |
|
27929 |
|
27930 |
|
27931 | |
27932 |
|
27933 |
|
27934 |
|
27935 |
|
27936 | read(bytes) {
|
27937 |
|
27938 | let i = this.readPublicKey(bytes);
|
27939 |
|
27940 |
|
27941 |
|
27942 |
|
27943 |
|
27944 | this.s2k_usage = bytes[i++];
|
27945 |
|
27946 |
|
27947 |
|
27948 | if (this.version === 5) {
|
27949 | i++;
|
27950 | }
|
27951 |
|
27952 |
|
27953 |
|
27954 | if (this.s2k_usage === 255 || this.s2k_usage === 254 || this.s2k_usage === 253) {
|
27955 | this.symmetric = bytes[i++];
|
27956 | this.symmetric = enums.read(enums.symmetric, this.symmetric);
|
27957 |
|
27958 |
|
27959 |
|
27960 | if (this.s2k_usage === 253) {
|
27961 | this.aead = bytes[i++];
|
27962 | this.aead = enums.read(enums.aead, this.aead);
|
27963 | }
|
27964 |
|
27965 |
|
27966 |
|
27967 |
|
27968 | this.s2k = new S2K();
|
27969 | i += this.s2k.read(bytes.subarray(i, bytes.length));
|
27970 |
|
27971 | if (this.s2k.type === 'gnu-dummy') {
|
27972 | return;
|
27973 | }
|
27974 | } else if (this.s2k_usage) {
|
27975 | this.symmetric = this.s2k_usage;
|
27976 | this.symmetric = enums.read(enums.symmetric, this.symmetric);
|
27977 | }
|
27978 |
|
27979 |
|
27980 |
|
27981 |
|
27982 | if (this.s2k_usage) {
|
27983 | this.iv = bytes.subarray(
|
27984 | i,
|
27985 | i + mod.cipher[this.symmetric].blockSize
|
27986 | );
|
27987 |
|
27988 | i += this.iv.length;
|
27989 | }
|
27990 |
|
27991 |
|
27992 |
|
27993 | if (this.version === 5) {
|
27994 | i += 4;
|
27995 | }
|
27996 |
|
27997 |
|
27998 |
|
27999 |
|
28000 | this.keyMaterial = bytes.subarray(i);
|
28001 | this.isEncrypted = !!this.s2k_usage;
|
28002 |
|
28003 | if (!this.isEncrypted) {
|
28004 | const cleartext = this.keyMaterial.subarray(0, -2);
|
28005 | if (!util.equalsUint8Array(util.writeChecksum(cleartext), this.keyMaterial.subarray(-2))) {
|
28006 | throw new Error('Key checksum mismatch');
|
28007 | }
|
28008 | try {
|
28009 | const algo = enums.write(enums.publicKey, this.algorithm);
|
28010 | const { privateParams } = mod.parsePrivateKeyParams(algo, cleartext, this.publicParams);
|
28011 | this.privateParams = privateParams;
|
28012 | } catch (err) {
|
28013 | throw new Error('Error reading MPIs');
|
28014 | }
|
28015 | }
|
28016 | }
|
28017 |
|
28018 | |
28019 |
|
28020 |
|
28021 |
|
28022 | write() {
|
28023 | const arr = [this.writePublicKey()];
|
28024 |
|
28025 | arr.push(new Uint8Array([this.s2k_usage]));
|
28026 |
|
28027 | const optionalFieldsArr = [];
|
28028 |
|
28029 |
|
28030 | if (this.s2k_usage === 255 || this.s2k_usage === 254 || this.s2k_usage === 253) {
|
28031 | optionalFieldsArr.push(enums.write(enums.symmetric, this.symmetric));
|
28032 |
|
28033 |
|
28034 |
|
28035 | if (this.s2k_usage === 253) {
|
28036 | optionalFieldsArr.push(enums.write(enums.aead, this.aead));
|
28037 | }
|
28038 |
|
28039 |
|
28040 |
|
28041 |
|
28042 | optionalFieldsArr.push(...this.s2k.write());
|
28043 | }
|
28044 |
|
28045 |
|
28046 |
|
28047 |
|
28048 | if (this.s2k_usage && this.s2k.type !== 'gnu-dummy') {
|
28049 | optionalFieldsArr.push(...this.iv);
|
28050 | }
|
28051 |
|
28052 | if (this.version === 5) {
|
28053 | arr.push(new Uint8Array([optionalFieldsArr.length]));
|
28054 | }
|
28055 | arr.push(new Uint8Array(optionalFieldsArr));
|
28056 |
|
28057 | if (!this.isDummy()) {
|
28058 | if (!this.s2k_usage) {
|
28059 | const algo = enums.write(enums.publicKey, this.algorithm);
|
28060 | const cleartextParams = mod.serializeParams(algo, this.privateParams);
|
28061 | this.keyMaterial = util.concatUint8Array([
|
28062 | cleartextParams,
|
28063 | util.writeChecksum(cleartextParams)
|
28064 | ]);
|
28065 | }
|
28066 |
|
28067 | if (this.version === 5) {
|
28068 | arr.push(util.writeNumber(this.keyMaterial.length, 4));
|
28069 | }
|
28070 | arr.push(this.keyMaterial);
|
28071 | }
|
28072 |
|
28073 | return util.concatUint8Array(arr);
|
28074 | }
|
28075 |
|
28076 | |
28077 |
|
28078 |
|
28079 |
|
28080 |
|
28081 | isDecrypted() {
|
28082 | return this.isEncrypted === false;
|
28083 | }
|
28084 |
|
28085 | |
28086 |
|
28087 |
|
28088 |
|
28089 | isDummy() {
|
28090 | return !!(this.s2k && this.s2k.type === 'gnu-dummy');
|
28091 | }
|
28092 |
|
28093 | |
28094 |
|
28095 |
|
28096 |
|
28097 |
|
28098 | makeDummy(config = defaultConfig) {
|
28099 | if (this.isDummy()) {
|
28100 | return;
|
28101 | }
|
28102 | if (this.isDecrypted()) {
|
28103 | this.clearPrivateParams();
|
28104 | }
|
28105 | this.isEncrypted = null;
|
28106 | this.keyMaterial = null;
|
28107 | this.s2k = new S2K(config);
|
28108 | this.s2k.algorithm = 0;
|
28109 | this.s2k.c = 0;
|
28110 | this.s2k.type = 'gnu-dummy';
|
28111 | this.s2k_usage = 254;
|
28112 | this.symmetric = 'aes256';
|
28113 | }
|
28114 |
|
28115 | |
28116 |
|
28117 |
|
28118 |
|
28119 |
|
28120 |
|
28121 |
|
28122 |
|
28123 |
|
28124 |
|
28125 | async encrypt(passphrase, config = defaultConfig) {
|
28126 | if (this.isDummy()) {
|
28127 | return;
|
28128 | }
|
28129 |
|
28130 | if (!this.isDecrypted()) {
|
28131 | throw new Error('Key packet is already encrypted');
|
28132 | }
|
28133 |
|
28134 | if (this.isDecrypted() && !passphrase) {
|
28135 | this.s2k_usage = 0;
|
28136 | return;
|
28137 | } else if (!passphrase) {
|
28138 | throw new Error('The key must be decrypted before removing passphrase protection.');
|
28139 | }
|
28140 |
|
28141 | this.s2k = new S2K(config);
|
28142 | this.s2k.salt = await mod.random.getRandomBytes(8);
|
28143 | const algo = enums.write(enums.publicKey, this.algorithm);
|
28144 | const cleartext = mod.serializeParams(algo, this.privateParams);
|
28145 | this.symmetric = 'aes256';
|
28146 | const key = await produceEncryptionKey(this.s2k, passphrase, this.symmetric);
|
28147 | const blockLen = mod.cipher[this.symmetric].blockSize;
|
28148 | this.iv = await mod.random.getRandomBytes(blockLen);
|
28149 |
|
28150 | if (config.aeadProtect) {
|
28151 | this.s2k_usage = 253;
|
28152 | this.aead = 'eax';
|
28153 | const mode = mod[this.aead];
|
28154 | const modeInstance = await mode(this.symmetric, key);
|
28155 | this.keyMaterial = await modeInstance.encrypt(cleartext, this.iv.subarray(0, mode.ivLength), new Uint8Array());
|
28156 | } else {
|
28157 | this.s2k_usage = 254;
|
28158 | this.keyMaterial = await mod.cfb.encrypt(this.symmetric, key, util.concatUint8Array([
|
28159 | cleartext,
|
28160 | await mod.hash.sha1(cleartext, config)
|
28161 | ]), this.iv, config);
|
28162 | }
|
28163 | }
|
28164 |
|
28165 | |
28166 |
|
28167 |
|
28168 |
|
28169 |
|
28170 |
|
28171 |
|
28172 |
|
28173 | async decrypt(passphrase) {
|
28174 | if (this.isDummy()) {
|
28175 | return false;
|
28176 | }
|
28177 |
|
28178 | if (this.isDecrypted()) {
|
28179 | throw new Error('Key packet is already decrypted.');
|
28180 | }
|
28181 |
|
28182 | let key;
|
28183 | if (this.s2k_usage === 254 || this.s2k_usage === 253) {
|
28184 | key = await produceEncryptionKey(this.s2k, passphrase, this.symmetric);
|
28185 | } else if (this.s2k_usage === 255) {
|
28186 | throw new Error('Encrypted private key is authenticated using an insecure two-byte hash');
|
28187 | } else {
|
28188 | throw new Error('Private key is encrypted using an insecure S2K function: unsalted MD5');
|
28189 | }
|
28190 |
|
28191 | let cleartext;
|
28192 | if (this.s2k_usage === 253) {
|
28193 | const mode = mod[this.aead];
|
28194 | try {
|
28195 | const modeInstance = await mode(this.symmetric, key);
|
28196 | cleartext = await modeInstance.decrypt(this.keyMaterial, this.iv.subarray(0, mode.ivLength), new Uint8Array());
|
28197 | } catch (err) {
|
28198 | if (err.message === 'Authentication tag mismatch') {
|
28199 | throw new Error('Incorrect key passphrase: ' + err.message);
|
28200 | }
|
28201 | throw err;
|
28202 | }
|
28203 | } else {
|
28204 | const cleartextWithHash = await mod.cfb.decrypt(this.symmetric, key, this.keyMaterial, this.iv);
|
28205 |
|
28206 | cleartext = cleartextWithHash.subarray(0, -20);
|
28207 | const hash = await mod.hash.sha1(cleartext);
|
28208 |
|
28209 | if (!util.equalsUint8Array(hash, cleartextWithHash.subarray(-20))) {
|
28210 | throw new Error('Incorrect key passphrase');
|
28211 | }
|
28212 | }
|
28213 |
|
28214 | try {
|
28215 | const algo = enums.write(enums.publicKey, this.algorithm);
|
28216 | const { privateParams } = mod.parsePrivateKeyParams(algo, cleartext, this.publicParams);
|
28217 | this.privateParams = privateParams;
|
28218 | } catch (err) {
|
28219 | throw new Error('Error reading MPIs');
|
28220 | }
|
28221 | this.isEncrypted = false;
|
28222 | this.keyMaterial = null;
|
28223 | this.s2k_usage = 0;
|
28224 | }
|
28225 |
|
28226 | |
28227 |
|
28228 |
|
28229 |
|
28230 |
|
28231 | async validate() {
|
28232 | if (this.isDummy()) {
|
28233 | return;
|
28234 | }
|
28235 |
|
28236 | if (!this.isDecrypted()) {
|
28237 | throw new Error('Key is not decrypted');
|
28238 | }
|
28239 |
|
28240 | const algo = enums.write(enums.publicKey, this.algorithm);
|
28241 |
|
28242 | let validParams;
|
28243 | try {
|
28244 |
|
28245 | validParams = await mod.validateParams(algo, this.publicParams, this.privateParams);
|
28246 | } catch (_) {
|
28247 | validParams = false;
|
28248 | }
|
28249 | if (!validParams) {
|
28250 | throw new Error('Key is invalid');
|
28251 | }
|
28252 | }
|
28253 |
|
28254 | async generate(bits, curve) {
|
28255 | const algo = enums.write(enums.publicKey, this.algorithm);
|
28256 | const { privateParams, publicParams } = await mod.generateParams(algo, bits, curve);
|
28257 | this.privateParams = privateParams;
|
28258 | this.publicParams = publicParams;
|
28259 | this.isEncrypted = false;
|
28260 | }
|
28261 |
|
28262 | |
28263 |
|
28264 |
|
28265 | clearPrivateParams() {
|
28266 | if (this.isDummy()) {
|
28267 | return;
|
28268 | }
|
28269 |
|
28270 | Object.keys(this.privateParams).forEach(name => {
|
28271 | const param = this.privateParams[name];
|
28272 | param.fill(0);
|
28273 | delete this.privateParams[name];
|
28274 | });
|
28275 | this.privateParams = null;
|
28276 | this.isEncrypted = true;
|
28277 | }
|
28278 | }
|
28279 |
|
28280 | async function produceEncryptionKey(s2k, passphrase, algorithm) {
|
28281 | return s2k.produce_key(
|
28282 | passphrase,
|
28283 | mod.cipher[algorithm].keySize
|
28284 | );
|
28285 | }
|
28286 |
|
28287 | var emailAddresses = createCommonjsModule(function (module) {
|
28288 |
|
28289 |
|
28290 |
|
28291 |
|
28292 |
|
28293 |
|
28294 |
|
28295 |
|
28296 |
|
28297 |
|
28298 |
|
28299 |
|
28300 |
|
28301 |
|
28302 |
|
28303 |
|
28304 |
|
28305 |
|
28306 |
|
28307 |
|
28308 |
|
28309 |
|
28310 |
|
28311 |
|
28312 |
|
28313 |
|
28314 | (function (global) {
|
28315 |
|
28316 | function parse5322(opts) {
|
28317 |
|
28318 |
|
28319 |
|
28320 | function inStr() { return pos < len; }
|
28321 | function curTok() { return parseString[pos]; }
|
28322 | function getPos() { return pos; }
|
28323 | function setPos(i) { pos = i; }
|
28324 | function nextTok() { pos += 1; }
|
28325 | function initialize() {
|
28326 | pos = 0;
|
28327 | len = parseString.length;
|
28328 | }
|
28329 |
|
28330 |
|
28331 |
|
28332 | function o(name, value) {
|
28333 | return {
|
28334 | name: name,
|
28335 | tokens: value || "",
|
28336 | semantic: value || "",
|
28337 | children: []
|
28338 | };
|
28339 | }
|
28340 |
|
28341 | function wrap(name, ast) {
|
28342 | var n;
|
28343 | if (ast === null) { return null; }
|
28344 | n = o(name);
|
28345 | n.tokens = ast.tokens;
|
28346 | n.semantic = ast.semantic;
|
28347 | n.children.push(ast);
|
28348 | return n;
|
28349 | }
|
28350 |
|
28351 | function add(parent, child) {
|
28352 | if (child !== null) {
|
28353 | parent.tokens += child.tokens;
|
28354 | parent.semantic += child.semantic;
|
28355 | }
|
28356 | parent.children.push(child);
|
28357 | return parent;
|
28358 | }
|
28359 |
|
28360 | function compareToken(fxnCompare) {
|
28361 | var tok;
|
28362 | if (!inStr()) { return null; }
|
28363 | tok = curTok();
|
28364 | if (fxnCompare(tok)) {
|
28365 | nextTok();
|
28366 | return o('token', tok);
|
28367 | }
|
28368 | return null;
|
28369 | }
|
28370 |
|
28371 | function literal(lit) {
|
28372 | return function literalFunc() {
|
28373 | return wrap('literal', compareToken(function (tok) {
|
28374 | return tok === lit;
|
28375 | }));
|
28376 | };
|
28377 | }
|
28378 |
|
28379 | function and() {
|
28380 | var args = arguments;
|
28381 | return function andFunc() {
|
28382 | var i, s, result, start;
|
28383 | start = getPos();
|
28384 | s = o('and');
|
28385 | for (i = 0; i < args.length; i += 1) {
|
28386 | result = args[i]();
|
28387 | if (result === null) {
|
28388 | setPos(start);
|
28389 | return null;
|
28390 | }
|
28391 | add(s, result);
|
28392 | }
|
28393 | return s;
|
28394 | };
|
28395 | }
|
28396 |
|
28397 | function or() {
|
28398 | var args = arguments;
|
28399 | return function orFunc() {
|
28400 | var i, result, start;
|
28401 | start = getPos();
|
28402 | for (i = 0; i < args.length; i += 1) {
|
28403 | result = args[i]();
|
28404 | if (result !== null) {
|
28405 | return result;
|
28406 | }
|
28407 | setPos(start);
|
28408 | }
|
28409 | return null;
|
28410 | };
|
28411 | }
|
28412 |
|
28413 | function opt(prod) {
|
28414 | return function optFunc() {
|
28415 | var result, start;
|
28416 | start = getPos();
|
28417 | result = prod();
|
28418 | if (result !== null) {
|
28419 | return result;
|
28420 | }
|
28421 | else {
|
28422 | setPos(start);
|
28423 | return o('opt');
|
28424 | }
|
28425 | };
|
28426 | }
|
28427 |
|
28428 | function invis(prod) {
|
28429 | return function invisFunc() {
|
28430 | var result = prod();
|
28431 | if (result !== null) {
|
28432 | result.semantic = "";
|
28433 | }
|
28434 | return result;
|
28435 | };
|
28436 | }
|
28437 |
|
28438 | function colwsp(prod) {
|
28439 | return function collapseSemanticWhitespace() {
|
28440 | var result = prod();
|
28441 | if (result !== null && result.semantic.length > 0) {
|
28442 | result.semantic = " ";
|
28443 | }
|
28444 | return result;
|
28445 | };
|
28446 | }
|
28447 |
|
28448 | function star(prod, minimum) {
|
28449 | return function starFunc() {
|
28450 | var s, result, count, start, min;
|
28451 | start = getPos();
|
28452 | s = o('star');
|
28453 | count = 0;
|
28454 | min = minimum === undefined ? 0 : minimum;
|
28455 | while ((result = prod()) !== null) {
|
28456 | count = count + 1;
|
28457 | add(s, result);
|
28458 | }
|
28459 | if (count >= min) {
|
28460 | return s;
|
28461 | }
|
28462 | else {
|
28463 | setPos(start);
|
28464 | return null;
|
28465 | }
|
28466 | };
|
28467 | }
|
28468 |
|
28469 |
|
28470 |
|
28471 |
|
28472 |
|
28473 | function collapseWhitespace(s) {
|
28474 | return s.replace(/([ \t]|\r\n)+/g, ' ').replace(/^\s*/, '').replace(/\s*$/, '');
|
28475 | }
|
28476 |
|
28477 |
|
28478 |
|
28479 |
|
28480 |
|
28481 |
|
28482 |
|
28483 |
|
28484 |
|
28485 |
|
28486 |
|
28487 |
|
28488 |
|
28489 |
|
28490 |
|
28491 | function isUTF8NonAscii(tok) {
|
28492 |
|
28493 |
|
28494 |
|
28495 | return tok.charCodeAt(0) >= 128;
|
28496 | }
|
28497 |
|
28498 |
|
28499 |
|
28500 |
|
28501 |
|
28502 |
|
28503 |
|
28504 |
|
28505 | function cr() { return wrap('cr', literal('\r')()); }
|
28506 |
|
28507 |
|
28508 |
|
28509 | function crlf() { return wrap('crlf', and(cr, lf)()); }
|
28510 |
|
28511 |
|
28512 |
|
28513 | function dquote() { return wrap('dquote', literal('"')()); }
|
28514 |
|
28515 |
|
28516 |
|
28517 | function htab() { return wrap('htab', literal('\t')()); }
|
28518 |
|
28519 |
|
28520 |
|
28521 | function lf() { return wrap('lf', literal('\n')()); }
|
28522 |
|
28523 |
|
28524 | function sp() { return wrap('sp', literal(' ')()); }
|
28525 |
|
28526 |
|
28527 |
|
28528 | function vchar() {
|
28529 | return wrap('vchar', compareToken(function vcharFunc(tok) {
|
28530 | var code = tok.charCodeAt(0);
|
28531 | var accept = (0x21 <= code && code <= 0x7E);
|
28532 | if (opts.rfc6532) {
|
28533 | accept = accept || isUTF8NonAscii(tok);
|
28534 | }
|
28535 | return accept;
|
28536 | }));
|
28537 | }
|
28538 |
|
28539 |
|
28540 |
|
28541 | function wsp() { return wrap('wsp', or(sp, htab)()); }
|
28542 |
|
28543 |
|
28544 |
|
28545 |
|
28546 |
|
28547 |
|
28548 |
|
28549 | function quotedPair() {
|
28550 | var qp = wrap('quoted-pair',
|
28551 | or(
|
28552 | and(literal('\\'), or(vchar, wsp)),
|
28553 | obsQP
|
28554 | )());
|
28555 | if (qp === null) { return null; }
|
28556 |
|
28557 |
|
28558 | qp.semantic = qp.semantic[1];
|
28559 | return qp;
|
28560 | }
|
28561 |
|
28562 |
|
28563 |
|
28564 |
|
28565 | function fws() {
|
28566 | return wrap('fws', or(
|
28567 | obsFws,
|
28568 | and(
|
28569 | opt(and(
|
28570 | star(wsp),
|
28571 | invis(crlf)
|
28572 | )),
|
28573 | star(wsp, 1)
|
28574 | )
|
28575 | )());
|
28576 | }
|
28577 |
|
28578 |
|
28579 |
|
28580 |
|
28581 |
|
28582 | function ctext() {
|
28583 | return wrap('ctext', or(
|
28584 | function ctextFunc1() {
|
28585 | return compareToken(function ctextFunc2(tok) {
|
28586 | var code = tok.charCodeAt(0);
|
28587 | var accept =
|
28588 | (33 <= code && code <= 39) ||
|
28589 | (42 <= code && code <= 91) ||
|
28590 | (93 <= code && code <= 126);
|
28591 | if (opts.rfc6532) {
|
28592 | accept = accept || isUTF8NonAscii(tok);
|
28593 | }
|
28594 | return accept;
|
28595 | });
|
28596 | },
|
28597 | obsCtext
|
28598 | )());
|
28599 | }
|
28600 |
|
28601 |
|
28602 | function ccontent() {
|
28603 | return wrap('ccontent', or(ctext, quotedPair, comment)());
|
28604 | }
|
28605 |
|
28606 |
|
28607 | function comment() {
|
28608 | return wrap('comment', and(
|
28609 | literal('('),
|
28610 | star(and(opt(fws), ccontent)),
|
28611 | opt(fws),
|
28612 | literal(')')
|
28613 | )());
|
28614 | }
|
28615 |
|
28616 |
|
28617 | function cfws() {
|
28618 | return wrap('cfws', or(
|
28619 | and(
|
28620 | star(
|
28621 | and(opt(fws), comment),
|
28622 | 1
|
28623 | ),
|
28624 | opt(fws)
|
28625 | ),
|
28626 | fws
|
28627 | )());
|
28628 | }
|
28629 |
|
28630 |
|
28631 |
|
28632 |
|
28633 |
|
28634 |
|
28635 |
|
28636 |
|
28637 |
|
28638 |
|
28639 |
|
28640 |
|
28641 |
|
28642 |
|
28643 | function atext() {
|
28644 | return wrap('atext', compareToken(function atextFunc(tok) {
|
28645 | var accept =
|
28646 | ('a' <= tok && tok <= 'z') ||
|
28647 | ('A' <= tok && tok <= 'Z') ||
|
28648 | ('0' <= tok && tok <= '9') ||
|
28649 | (['!', '#', '$', '%', '&', '\'', '*', '+', '-', '/',
|
28650 | '=', '?', '^', '_', '`', '{', '|', '}', '~'].indexOf(tok) >= 0);
|
28651 | if (opts.rfc6532) {
|
28652 | accept = accept || isUTF8NonAscii(tok);
|
28653 | }
|
28654 | return accept;
|
28655 | }));
|
28656 | }
|
28657 |
|
28658 |
|
28659 | function atom() {
|
28660 | return wrap('atom', and(colwsp(opt(cfws)), star(atext, 1), colwsp(opt(cfws)))());
|
28661 | }
|
28662 |
|
28663 |
|
28664 | function dotAtomText() {
|
28665 | var s, maybeText;
|
28666 | s = wrap('dot-atom-text', star(atext, 1)());
|
28667 | if (s === null) { return s; }
|
28668 | maybeText = star(and(literal('.'), star(atext, 1)))();
|
28669 | if (maybeText !== null) {
|
28670 | add(s, maybeText);
|
28671 | }
|
28672 | return s;
|
28673 | }
|
28674 |
|
28675 |
|
28676 | function dotAtom() {
|
28677 | return wrap('dot-atom', and(invis(opt(cfws)), dotAtomText, invis(opt(cfws)))());
|
28678 | }
|
28679 |
|
28680 |
|
28681 |
|
28682 |
|
28683 |
|
28684 |
|
28685 |
|
28686 | function qtext() {
|
28687 | return wrap('qtext', or(
|
28688 | function qtextFunc1() {
|
28689 | return compareToken(function qtextFunc2(tok) {
|
28690 | var code = tok.charCodeAt(0);
|
28691 | var accept =
|
28692 | (33 === code) ||
|
28693 | (35 <= code && code <= 91) ||
|
28694 | (93 <= code && code <= 126);
|
28695 | if (opts.rfc6532) {
|
28696 | accept = accept || isUTF8NonAscii(tok);
|
28697 | }
|
28698 | return accept;
|
28699 | });
|
28700 | },
|
28701 | obsQtext
|
28702 | )());
|
28703 | }
|
28704 |
|
28705 |
|
28706 | function qcontent() {
|
28707 | return wrap('qcontent', or(qtext, quotedPair)());
|
28708 | }
|
28709 |
|
28710 |
|
28711 |
|
28712 |
|
28713 | function quotedString() {
|
28714 | return wrap('quoted-string', and(
|
28715 | invis(opt(cfws)),
|
28716 | invis(dquote), star(and(opt(colwsp(fws)), qcontent)), opt(invis(fws)), invis(dquote),
|
28717 | invis(opt(cfws))
|
28718 | )());
|
28719 | }
|
28720 |
|
28721 |
|
28722 |
|
28723 |
|
28724 | function word() {
|
28725 | return wrap('word', or(atom, quotedString)());
|
28726 | }
|
28727 |
|
28728 |
|
28729 | function phrase() {
|
28730 | return wrap('phrase', or(obsPhrase, star(word, 1))());
|
28731 | }
|
28732 |
|
28733 |
|
28734 |
|
28735 | function address() {
|
28736 | return wrap('address', or(mailbox, group)());
|
28737 | }
|
28738 |
|
28739 |
|
28740 | function mailbox() {
|
28741 | return wrap('mailbox', or(nameAddr, addrSpec)());
|
28742 | }
|
28743 |
|
28744 |
|
28745 | function nameAddr() {
|
28746 | return wrap('name-addr', and(opt(displayName), angleAddr)());
|
28747 | }
|
28748 |
|
28749 |
|
28750 |
|
28751 | function angleAddr() {
|
28752 | return wrap('angle-addr', or(
|
28753 | and(
|
28754 | invis(opt(cfws)),
|
28755 | literal('<'),
|
28756 | addrSpec,
|
28757 | literal('>'),
|
28758 | invis(opt(cfws))
|
28759 | ),
|
28760 | obsAngleAddr
|
28761 | )());
|
28762 | }
|
28763 |
|
28764 |
|
28765 | function group() {
|
28766 | return wrap('group', and(
|
28767 | displayName,
|
28768 | literal(':'),
|
28769 | opt(groupList),
|
28770 | literal(';'),
|
28771 | invis(opt(cfws))
|
28772 | )());
|
28773 | }
|
28774 |
|
28775 |
|
28776 | function displayName() {
|
28777 | return wrap('display-name', function phraseFixedSemantic() {
|
28778 | var result = phrase();
|
28779 | if (result !== null) {
|
28780 | result.semantic = collapseWhitespace(result.semantic);
|
28781 | }
|
28782 | return result;
|
28783 | }());
|
28784 | }
|
28785 |
|
28786 |
|
28787 | function mailboxList() {
|
28788 | return wrap('mailbox-list', or(
|
28789 | and(
|
28790 | mailbox,
|
28791 | star(and(literal(','), mailbox))
|
28792 | ),
|
28793 | obsMboxList
|
28794 | )());
|
28795 | }
|
28796 |
|
28797 |
|
28798 | function addressList() {
|
28799 | return wrap('address-list', or(
|
28800 | and(
|
28801 | address,
|
28802 | star(and(literal(','), address))
|
28803 | ),
|
28804 | obsAddrList
|
28805 | )());
|
28806 | }
|
28807 |
|
28808 |
|
28809 | function groupList() {
|
28810 | return wrap('group-list', or(
|
28811 | mailboxList,
|
28812 | invis(cfws),
|
28813 | obsGroupList
|
28814 | )());
|
28815 | }
|
28816 |
|
28817 |
|
28818 |
|
28819 |
|
28820 | function localPart() {
|
28821 |
|
28822 |
|
28823 | return wrap('local-part', or(obsLocalPart, dotAtom, quotedString)());
|
28824 | }
|
28825 |
|
28826 |
|
28827 |
|
28828 |
|
28829 | function dtext() {
|
28830 | return wrap('dtext', or(
|
28831 | function dtextFunc1() {
|
28832 | return compareToken(function dtextFunc2(tok) {
|
28833 | var code = tok.charCodeAt(0);
|
28834 | var accept =
|
28835 | (33 <= code && code <= 90) ||
|
28836 | (94 <= code && code <= 126);
|
28837 | if (opts.rfc6532) {
|
28838 | accept = accept || isUTF8NonAscii(tok);
|
28839 | }
|
28840 | return accept;
|
28841 | });
|
28842 | },
|
28843 | obsDtext
|
28844 | )()
|
28845 | );
|
28846 | }
|
28847 |
|
28848 |
|
28849 | function domainLiteral() {
|
28850 | return wrap('domain-literal', and(
|
28851 | invis(opt(cfws)),
|
28852 | literal('['),
|
28853 | star(and(opt(fws), dtext)),
|
28854 | opt(fws),
|
28855 | literal(']'),
|
28856 | invis(opt(cfws))
|
28857 | )());
|
28858 | }
|
28859 |
|
28860 |
|
28861 | function domain() {
|
28862 | return wrap('domain', function domainCheckTLD() {
|
28863 | var result = or(obsDomain, dotAtom, domainLiteral)();
|
28864 | if (opts.rejectTLD) {
|
28865 | if (result && result.semantic && result.semantic.indexOf('.') < 0) {
|
28866 | return null;
|
28867 | }
|
28868 | }
|
28869 |
|
28870 | if (result) {
|
28871 | result.semantic = result.semantic.replace(/\s+/g, '');
|
28872 | }
|
28873 | return result;
|
28874 | }());
|
28875 | }
|
28876 |
|
28877 |
|
28878 | function addrSpec() {
|
28879 | return wrap('addr-spec', and(
|
28880 | localPart, literal('@'), domain
|
28881 | )());
|
28882 | }
|
28883 |
|
28884 |
|
28885 |
|
28886 |
|
28887 |
|
28888 |
|
28889 |
|
28890 |
|
28891 |
|
28892 | function fromSpec() {
|
28893 | return wrap('from', or(
|
28894 | mailboxList,
|
28895 | addressList
|
28896 | )());
|
28897 | }
|
28898 |
|
28899 |
|
28900 |
|
28901 | function senderSpec() {
|
28902 | return wrap('sender', or(
|
28903 | mailbox,
|
28904 | address
|
28905 | )());
|
28906 | }
|
28907 |
|
28908 |
|
28909 |
|
28910 | function replyToSpec() {
|
28911 | return wrap('reply-to', addressList());
|
28912 | }
|
28913 |
|
28914 |
|
28915 |
|
28916 |
|
28917 |
|
28918 |
|
28919 |
|
28920 |
|
28921 | function obsNoWsCtl() {
|
28922 | return opts.strict ? null : wrap('obs-NO-WS-CTL', compareToken(function (tok) {
|
28923 | var code = tok.charCodeAt(0);
|
28924 | return ((1 <= code && code <= 8) ||
|
28925 | (11 === code || 12 === code) ||
|
28926 | (14 <= code && code <= 31) ||
|
28927 | (127 === code));
|
28928 | }));
|
28929 | }
|
28930 |
|
28931 |
|
28932 | function obsCtext() { return opts.strict ? null : wrap('obs-ctext', obsNoWsCtl()); }
|
28933 |
|
28934 |
|
28935 | function obsQtext() { return opts.strict ? null : wrap('obs-qtext', obsNoWsCtl()); }
|
28936 |
|
28937 |
|
28938 | function obsQP() {
|
28939 | return opts.strict ? null : wrap('obs-qp', and(
|
28940 | literal('\\'),
|
28941 | or(literal('\0'), obsNoWsCtl, lf, cr)
|
28942 | )());
|
28943 | }
|
28944 |
|
28945 |
|
28946 | function obsPhrase() {
|
28947 | if (opts.strict ) return null;
|
28948 | return opts.atInDisplayName ? wrap('obs-phrase', and(
|
28949 | word,
|
28950 | star(or(word, literal('.'), literal('@'), colwsp(cfws)))
|
28951 | )()) :
|
28952 | wrap('obs-phrase', and(
|
28953 | word,
|
28954 | star(or(word, literal('.'), colwsp(cfws)))
|
28955 | )());
|
28956 | }
|
28957 |
|
28958 |
|
28959 |
|
28960 |
|
28961 |
|
28962 | function obsFws() {
|
28963 | return opts.strict ? null : wrap('obs-FWS', star(
|
28964 | and(invis(opt(crlf)), wsp),
|
28965 | 1
|
28966 | )());
|
28967 | }
|
28968 |
|
28969 |
|
28970 |
|
28971 |
|
28972 | function obsAngleAddr() {
|
28973 | return opts.strict ? null : wrap('obs-angle-addr', and(
|
28974 | invis(opt(cfws)),
|
28975 | literal('<'),
|
28976 | obsRoute,
|
28977 | addrSpec,
|
28978 | literal('>'),
|
28979 | invis(opt(cfws))
|
28980 | )());
|
28981 | }
|
28982 |
|
28983 |
|
28984 | function obsRoute() {
|
28985 | return opts.strict ? null : wrap('obs-route', and(
|
28986 | obsDomainList,
|
28987 | literal(':')
|
28988 | )());
|
28989 | }
|
28990 |
|
28991 |
|
28992 |
|
28993 | function obsDomainList() {
|
28994 | return opts.strict ? null : wrap('obs-domain-list', and(
|
28995 | star(or(invis(cfws), literal(','))),
|
28996 | literal('@'),
|
28997 | domain,
|
28998 | star(and(
|
28999 | literal(','),
|
29000 | invis(opt(cfws)),
|
29001 | opt(and(literal('@'), domain))
|
29002 | ))
|
29003 | )());
|
29004 | }
|
29005 |
|
29006 |
|
29007 | function obsMboxList() {
|
29008 | return opts.strict ? null : wrap('obs-mbox-list', and(
|
29009 | star(and(
|
29010 | invis(opt(cfws)),
|
29011 | literal(',')
|
29012 | )),
|
29013 | mailbox,
|
29014 | star(and(
|
29015 | literal(','),
|
29016 | opt(and(
|
29017 | mailbox,
|
29018 | invis(cfws)
|
29019 | ))
|
29020 | ))
|
29021 | )());
|
29022 | }
|
29023 |
|
29024 |
|
29025 | function obsAddrList() {
|
29026 | return opts.strict ? null : wrap('obs-addr-list', and(
|
29027 | star(and(
|
29028 | invis(opt(cfws)),
|
29029 | literal(',')
|
29030 | )),
|
29031 | address,
|
29032 | star(and(
|
29033 | literal(','),
|
29034 | opt(and(
|
29035 | address,
|
29036 | invis(cfws)
|
29037 | ))
|
29038 | ))
|
29039 | )());
|
29040 | }
|
29041 |
|
29042 |
|
29043 | function obsGroupList() {
|
29044 | return opts.strict ? null : wrap('obs-group-list', and(
|
29045 | star(and(
|
29046 | invis(opt(cfws)),
|
29047 | literal(',')
|
29048 | ), 1),
|
29049 | invis(opt(cfws))
|
29050 | )());
|
29051 | }
|
29052 |
|
29053 |
|
29054 | function obsLocalPart() {
|
29055 | return opts.strict ? null : wrap('obs-local-part', and(word, star(and(literal('.'), word)))());
|
29056 | }
|
29057 |
|
29058 |
|
29059 | function obsDomain() {
|
29060 | return opts.strict ? null : wrap('obs-domain', and(atom, star(and(literal('.'), atom)))());
|
29061 | }
|
29062 |
|
29063 |
|
29064 | function obsDtext() {
|
29065 | return opts.strict ? null : wrap('obs-dtext', or(obsNoWsCtl, quotedPair)());
|
29066 | }
|
29067 |
|
29068 |
|
29069 |
|
29070 |
|
29071 |
|
29072 | function findNode(name, root) {
|
29073 | var i, stack, node;
|
29074 | if (root === null || root === undefined) { return null; }
|
29075 | stack = [root];
|
29076 | while (stack.length > 0) {
|
29077 | node = stack.pop();
|
29078 | if (node.name === name) {
|
29079 | return node;
|
29080 | }
|
29081 | for (i = node.children.length - 1; i >= 0; i -= 1) {
|
29082 | stack.push(node.children[i]);
|
29083 | }
|
29084 | }
|
29085 | return null;
|
29086 | }
|
29087 |
|
29088 | function findAllNodes(name, root) {
|
29089 | var i, stack, node, result;
|
29090 | if (root === null || root === undefined) { return null; }
|
29091 | stack = [root];
|
29092 | result = [];
|
29093 | while (stack.length > 0) {
|
29094 | node = stack.pop();
|
29095 | if (node.name === name) {
|
29096 | result.push(node);
|
29097 | }
|
29098 | for (i = node.children.length - 1; i >= 0; i -= 1) {
|
29099 | stack.push(node.children[i]);
|
29100 | }
|
29101 | }
|
29102 | return result;
|
29103 | }
|
29104 |
|
29105 | function findAllNodesNoChildren(names, root) {
|
29106 | var i, stack, node, result, namesLookup;
|
29107 | if (root === null || root === undefined) { return null; }
|
29108 | stack = [root];
|
29109 | result = [];
|
29110 | namesLookup = {};
|
29111 | for (i = 0; i < names.length; i += 1) {
|
29112 | namesLookup[names[i]] = true;
|
29113 | }
|
29114 |
|
29115 | while (stack.length > 0) {
|
29116 | node = stack.pop();
|
29117 | if (node.name in namesLookup) {
|
29118 | result.push(node);
|
29119 |
|
29120 | } else {
|
29121 | for (i = node.children.length - 1; i >= 0; i -= 1) {
|
29122 | stack.push(node.children[i]);
|
29123 | }
|
29124 | }
|
29125 | }
|
29126 | return result;
|
29127 | }
|
29128 |
|
29129 | function giveResult(ast) {
|
29130 | var addresses, groupsAndMailboxes, i, groupOrMailbox, result;
|
29131 | if (ast === null) {
|
29132 | return null;
|
29133 | }
|
29134 | addresses = [];
|
29135 |
|
29136 |
|
29137 | groupsAndMailboxes = findAllNodesNoChildren(['group', 'mailbox'], ast);
|
29138 | for (i = 0; i < groupsAndMailboxes.length; i += 1) {
|
29139 | groupOrMailbox = groupsAndMailboxes[i];
|
29140 | if (groupOrMailbox.name === 'group') {
|
29141 | addresses.push(giveResultGroup(groupOrMailbox));
|
29142 | } else if (groupOrMailbox.name === 'mailbox') {
|
29143 | addresses.push(giveResultMailbox(groupOrMailbox));
|
29144 | }
|
29145 | }
|
29146 |
|
29147 | result = {
|
29148 | ast: ast,
|
29149 | addresses: addresses,
|
29150 | };
|
29151 | if (opts.simple) {
|
29152 | result = simplifyResult(result);
|
29153 | }
|
29154 | if (opts.oneResult) {
|
29155 | return oneResult(result);
|
29156 | }
|
29157 | if (opts.simple) {
|
29158 | return result && result.addresses;
|
29159 | } else {
|
29160 | return result;
|
29161 | }
|
29162 | }
|
29163 |
|
29164 | function giveResultGroup(group) {
|
29165 | var i;
|
29166 | var groupName = findNode('display-name', group);
|
29167 | var groupResultMailboxes = [];
|
29168 | var mailboxes = findAllNodesNoChildren(['mailbox'], group);
|
29169 | for (i = 0; i < mailboxes.length; i += 1) {
|
29170 | groupResultMailboxes.push(giveResultMailbox(mailboxes[i]));
|
29171 | }
|
29172 | return {
|
29173 | node: group,
|
29174 | parts: {
|
29175 | name: groupName,
|
29176 | },
|
29177 | type: group.name,
|
29178 | name: grabSemantic(groupName),
|
29179 | addresses: groupResultMailboxes,
|
29180 | };
|
29181 | }
|
29182 |
|
29183 | function giveResultMailbox(mailbox) {
|
29184 | var name = findNode('display-name', mailbox);
|
29185 | var aspec = findNode('addr-spec', mailbox);
|
29186 | var cfws = findAllNodes('cfws', mailbox);
|
29187 | var comments = findAllNodesNoChildren(['comment'], mailbox);
|
29188 |
|
29189 |
|
29190 | var local = findNode('local-part', aspec);
|
29191 | var domain = findNode('domain', aspec);
|
29192 | return {
|
29193 | node: mailbox,
|
29194 | parts: {
|
29195 | name: name,
|
29196 | address: aspec,
|
29197 | local: local,
|
29198 | domain: domain,
|
29199 | comments: cfws
|
29200 | },
|
29201 | type: mailbox.name,
|
29202 | name: grabSemantic(name),
|
29203 | address: grabSemantic(aspec),
|
29204 | local: grabSemantic(local),
|
29205 | domain: grabSemantic(domain),
|
29206 | comments: concatComments(comments),
|
29207 | groupName: grabSemantic(mailbox.groupName),
|
29208 | };
|
29209 | }
|
29210 |
|
29211 | function grabSemantic(n) {
|
29212 | return n !== null && n !== undefined ? n.semantic : null;
|
29213 | }
|
29214 |
|
29215 | function simplifyResult(result) {
|
29216 | var i;
|
29217 | if (result && result.addresses) {
|
29218 | for (i = 0; i < result.addresses.length; i += 1) {
|
29219 | delete result.addresses[i].node;
|
29220 | }
|
29221 | }
|
29222 | return result;
|
29223 | }
|
29224 |
|
29225 | function concatComments(comments) {
|
29226 | var result = '';
|
29227 | if (comments) {
|
29228 | for (var i = 0; i < comments.length; i += 1) {
|
29229 | result += grabSemantic(comments[i]);
|
29230 | }
|
29231 | }
|
29232 | return result;
|
29233 | }
|
29234 |
|
29235 | function oneResult(result) {
|
29236 | if (!result) { return null; }
|
29237 | if (!opts.partial && result.addresses.length > 1) { return null; }
|
29238 | return result.addresses && result.addresses[0];
|
29239 | }
|
29240 |
|
29241 |
|
29242 |
|
29243 | var parseString, pos, len, parsed, startProduction;
|
29244 |
|
29245 | opts = handleOpts(opts, {});
|
29246 | if (opts === null) { return null; }
|
29247 |
|
29248 | parseString = opts.input;
|
29249 |
|
29250 | startProduction = {
|
29251 | 'address': address,
|
29252 | 'address-list': addressList,
|
29253 | 'angle-addr': angleAddr,
|
29254 | 'from': fromSpec,
|
29255 | 'group': group,
|
29256 | 'mailbox': mailbox,
|
29257 | 'mailbox-list': mailboxList,
|
29258 | 'reply-to': replyToSpec,
|
29259 | 'sender': senderSpec,
|
29260 | }[opts.startAt] || addressList;
|
29261 |
|
29262 | if (!opts.strict) {
|
29263 | initialize();
|
29264 | opts.strict = true;
|
29265 | parsed = startProduction(parseString);
|
29266 | if (opts.partial || !inStr()) {
|
29267 | return giveResult(parsed);
|
29268 | }
|
29269 | opts.strict = false;
|
29270 | }
|
29271 |
|
29272 | initialize();
|
29273 | parsed = startProduction(parseString);
|
29274 | if (!opts.partial && inStr()) { return null; }
|
29275 | return giveResult(parsed);
|
29276 | }
|
29277 |
|
29278 | function parseOneAddressSimple(opts) {
|
29279 | return parse5322(handleOpts(opts, {
|
29280 | oneResult: true,
|
29281 | rfc6532: true,
|
29282 | simple: true,
|
29283 | startAt: 'address-list',
|
29284 | }));
|
29285 | }
|
29286 |
|
29287 | function parseAddressListSimple(opts) {
|
29288 | return parse5322(handleOpts(opts, {
|
29289 | rfc6532: true,
|
29290 | simple: true,
|
29291 | startAt: 'address-list',
|
29292 | }));
|
29293 | }
|
29294 |
|
29295 | function parseFromSimple(opts) {
|
29296 | return parse5322(handleOpts(opts, {
|
29297 | rfc6532: true,
|
29298 | simple: true,
|
29299 | startAt: 'from',
|
29300 | }));
|
29301 | }
|
29302 |
|
29303 | function parseSenderSimple(opts) {
|
29304 | return parse5322(handleOpts(opts, {
|
29305 | oneResult: true,
|
29306 | rfc6532: true,
|
29307 | simple: true,
|
29308 | startAt: 'sender',
|
29309 | }));
|
29310 | }
|
29311 |
|
29312 | function parseReplyToSimple(opts) {
|
29313 | return parse5322(handleOpts(opts, {
|
29314 | rfc6532: true,
|
29315 | simple: true,
|
29316 | startAt: 'reply-to',
|
29317 | }));
|
29318 | }
|
29319 |
|
29320 | function handleOpts(opts, defs) {
|
29321 | function isString(str) {
|
29322 | return Object.prototype.toString.call(str) === '[object String]';
|
29323 | }
|
29324 |
|
29325 | function isObject(o) {
|
29326 | return o === Object(o);
|
29327 | }
|
29328 |
|
29329 | function isNullUndef(o) {
|
29330 | return o === null || o === undefined;
|
29331 | }
|
29332 |
|
29333 | var defaults, o;
|
29334 |
|
29335 | if (isString(opts)) {
|
29336 | opts = { input: opts };
|
29337 | } else if (!isObject(opts)) {
|
29338 | return null;
|
29339 | }
|
29340 |
|
29341 | if (!isString(opts.input)) { return null; }
|
29342 | if (!defs) { return null; }
|
29343 |
|
29344 | defaults = {
|
29345 | oneResult: false,
|
29346 | partial: false,
|
29347 | rejectTLD: false,
|
29348 | rfc6532: false,
|
29349 | simple: false,
|
29350 | startAt: 'address-list',
|
29351 | strict: false,
|
29352 | atInDisplayName: false
|
29353 | };
|
29354 |
|
29355 | for (o in defaults) {
|
29356 | if (isNullUndef(opts[o])) {
|
29357 | opts[o] = !isNullUndef(defs[o]) ? defs[o] : defaults[o];
|
29358 | }
|
29359 | }
|
29360 | return opts;
|
29361 | }
|
29362 |
|
29363 | parse5322.parseOneAddress = parseOneAddressSimple;
|
29364 | parse5322.parseAddressList = parseAddressListSimple;
|
29365 | parse5322.parseFrom = parseFromSimple;
|
29366 | parse5322.parseSender = parseSenderSimple;
|
29367 | parse5322.parseReplyTo = parseReplyToSimple;
|
29368 |
|
29369 | {
|
29370 | module.exports = parse5322;
|
29371 | }
|
29372 |
|
29373 | }());
|
29374 | });
|
29375 |
|
29376 |
|
29377 |
|
29378 |
|
29379 |
|
29380 |
|
29381 |
|
29382 |
|
29383 |
|
29384 |
|
29385 |
|
29386 |
|
29387 | class UserIDPacket {
|
29388 | constructor() {
|
29389 | this.tag = enums.packet.userID;
|
29390 | |
29391 |
|
29392 |
|
29393 |
|
29394 | this.userid = '';
|
29395 |
|
29396 | this.name = '';
|
29397 | this.email = '';
|
29398 | this.comment = '';
|
29399 | }
|
29400 |
|
29401 | |
29402 |
|
29403 |
|
29404 |
|
29405 |
|
29406 |
|
29407 | static fromObject(userId) {
|
29408 | if (util.isString(userId) ||
|
29409 | (userId.name && !util.isString(userId.name)) ||
|
29410 | (userId.email && !util.isEmailAddress(userId.email)) ||
|
29411 | (userId.comment && !util.isString(userId.comment))) {
|
29412 | throw new Error('Invalid user ID format');
|
29413 | }
|
29414 | const packet = new UserIDPacket();
|
29415 | Object.assign(packet, userId);
|
29416 | const components = [];
|
29417 | if (packet.name) components.push(packet.name);
|
29418 | if (packet.comment) components.push(`(${packet.comment})`);
|
29419 | if (packet.email) components.push(`<${packet.email}>`);
|
29420 | packet.userid = components.join(' ');
|
29421 | return packet;
|
29422 | }
|
29423 |
|
29424 | |
29425 |
|
29426 |
|
29427 |
|
29428 | read(bytes, config = defaultConfig) {
|
29429 | const userid = util.decodeUtf8(bytes);
|
29430 | if (userid.length > config.maxUseridLength) {
|
29431 | throw new Error('User ID string is too long');
|
29432 | }
|
29433 | try {
|
29434 | const { name, address: email, comments } = emailAddresses.parseOneAddress({ input: userid, atInDisplayName: true });
|
29435 | this.comment = comments.replace(/^\(|\)$/g, '');
|
29436 | this.name = name;
|
29437 | this.email = email;
|
29438 | } catch (e) {}
|
29439 | this.userid = userid;
|
29440 | }
|
29441 |
|
29442 | |
29443 |
|
29444 |
|
29445 |
|
29446 | write() {
|
29447 | return util.encodeUtf8(this.userid);
|
29448 | }
|
29449 | }
|
29450 |
|
29451 |
|
29452 |
|
29453 |
|
29454 |
|
29455 |
|
29456 |
|
29457 |
|
29458 | class SecretSubkeyPacket extends SecretKeyPacket {
|
29459 | |
29460 |
|
29461 |
|
29462 |
|
29463 | constructor(date = new Date(), config = defaultConfig) {
|
29464 | super(date, config);
|
29465 | this.tag = enums.packet.secretSubkey;
|
29466 | }
|
29467 | }
|
29468 |
|
29469 |
|
29470 |
|
29471 |
|
29472 |
|
29473 |
|
29474 |
|
29475 |
|
29476 |
|
29477 |
|
29478 |
|
29479 |
|
29480 |
|
29481 |
|
29482 |
|
29483 |
|
29484 |
|
29485 |
|
29486 | class TrustPacket {
|
29487 | constructor() {
|
29488 | this.tag = enums.packet.trust;
|
29489 | }
|
29490 |
|
29491 | |
29492 |
|
29493 |
|
29494 |
|
29495 |
|
29496 | read() {}
|
29497 | }
|
29498 |
|
29499 |
|
29500 |
|
29501 |
|
29502 |
|
29503 |
|
29504 |
|
29505 |
|
29506 |
|
29507 |
|
29508 |
|
29509 |
|
29510 |
|
29511 | function newPacketFromTag(tag, allowedPackets) {
|
29512 | const className = packetClassFromTagName(tag);
|
29513 | if (!allowedPackets[className]) {
|
29514 | throw new Error('Packet not allowed in this context: ' + className);
|
29515 | }
|
29516 | return new allowedPackets[className]();
|
29517 | }
|
29518 |
|
29519 |
|
29520 |
|
29521 |
|
29522 |
|
29523 |
|
29524 |
|
29525 | function packetClassFromTagName(tag) {
|
29526 | return tag.substr(0, 1).toUpperCase() + tag.substr(1) + 'Packet';
|
29527 | }
|
29528 |
|
29529 |
|
29530 |
|
29531 |
|
29532 |
|
29533 |
|
29534 |
|
29535 | class PacketList extends Array {
|
29536 | |
29537 |
|
29538 |
|
29539 |
|
29540 | async read(bytes, allowedPackets, streaming, config = defaultConfig) {
|
29541 | this.stream = stream.transformPair(bytes, async (readable, writable) => {
|
29542 | const writer = stream.getWriter(writable);
|
29543 | try {
|
29544 | while (true) {
|
29545 | await writer.ready;
|
29546 | const done = await readPackets(readable, streaming, async parsed => {
|
29547 | try {
|
29548 | const tag = enums.read(enums.packet, parsed.tag);
|
29549 | const packet = newPacketFromTag(tag, allowedPackets);
|
29550 | packet.packets = new PacketList();
|
29551 | packet.fromStream = util.isStream(parsed.packet);
|
29552 | await packet.read(parsed.packet, config, streaming);
|
29553 | await writer.write(packet);
|
29554 | } catch (e) {
|
29555 | if (!config.tolerant || supportsStreaming(parsed.tag)) {
|
29556 |
|
29557 |
|
29558 |
|
29559 | await writer.abort(e);
|
29560 | }
|
29561 | util.printDebugError(e);
|
29562 | }
|
29563 | });
|
29564 | if (done) {
|
29565 | await writer.ready;
|
29566 | await writer.close();
|
29567 | return;
|
29568 | }
|
29569 | }
|
29570 | } catch (e) {
|
29571 | await writer.abort(e);
|
29572 | }
|
29573 | });
|
29574 |
|
29575 |
|
29576 | const reader = stream.getReader(this.stream);
|
29577 | while (true) {
|
29578 | const { done, value } = await reader.read();
|
29579 | if (!done) {
|
29580 | this.push(value);
|
29581 | } else {
|
29582 | this.stream = null;
|
29583 | }
|
29584 | if (done || supportsStreaming(value.tag)) {
|
29585 | break;
|
29586 | }
|
29587 | }
|
29588 | reader.releaseLock();
|
29589 | }
|
29590 |
|
29591 | |
29592 |
|
29593 |
|
29594 |
|
29595 |
|
29596 | write() {
|
29597 | const arr = [];
|
29598 |
|
29599 | for (let i = 0; i < this.length; i++) {
|
29600 | const packetbytes = this[i].write();
|
29601 | if (util.isStream(packetbytes) && supportsStreaming(this[i].tag)) {
|
29602 | let buffer = [];
|
29603 | let bufferLength = 0;
|
29604 | const minLength = 512;
|
29605 | arr.push(writeTag(this[i].tag));
|
29606 | arr.push(stream.transform(packetbytes, value => {
|
29607 | buffer.push(value);
|
29608 | bufferLength += value.length;
|
29609 | if (bufferLength >= minLength) {
|
29610 | const powerOf2 = Math.min(Math.log(bufferLength) / Math.LN2 | 0, 30);
|
29611 | const chunkSize = 2 ** powerOf2;
|
29612 | const bufferConcat = util.concat([writePartialLength(powerOf2)].concat(buffer));
|
29613 | buffer = [bufferConcat.subarray(1 + chunkSize)];
|
29614 | bufferLength = buffer[0].length;
|
29615 | return bufferConcat.subarray(0, 1 + chunkSize);
|
29616 | }
|
29617 | }, () => util.concat([writeSimpleLength(bufferLength)].concat(buffer))));
|
29618 | } else {
|
29619 | if (util.isStream(packetbytes)) {
|
29620 | let length = 0;
|
29621 | arr.push(stream.transform(stream.clone(packetbytes), value => {
|
29622 | length += value.length;
|
29623 | }, () => writeHeader(this[i].tag, length)));
|
29624 | } else {
|
29625 | arr.push(writeHeader(this[i].tag, packetbytes.length));
|
29626 | }
|
29627 | arr.push(packetbytes);
|
29628 | }
|
29629 | }
|
29630 |
|
29631 | return util.concat(arr);
|
29632 | }
|
29633 |
|
29634 | |
29635 |
|
29636 |
|
29637 |
|
29638 |
|
29639 | push(packet) {
|
29640 | if (!packet) {
|
29641 | return;
|
29642 | }
|
29643 |
|
29644 | packet.packets = packet.packets || new PacketList();
|
29645 |
|
29646 | super.push(packet);
|
29647 | }
|
29648 |
|
29649 | |
29650 |
|
29651 |
|
29652 | filterByTag(...args) {
|
29653 | const filtered = new PacketList();
|
29654 |
|
29655 | const handle = tag => packetType => tag === packetType;
|
29656 |
|
29657 | for (let i = 0; i < this.length; i++) {
|
29658 | if (args.some(handle(this[i].tag))) {
|
29659 | filtered.push(this[i]);
|
29660 | }
|
29661 | }
|
29662 |
|
29663 | return filtered;
|
29664 | }
|
29665 |
|
29666 | |
29667 |
|
29668 |
|
29669 |
|
29670 |
|
29671 | findPacket(type) {
|
29672 | return this.find(packet => packet.tag === type);
|
29673 | }
|
29674 |
|
29675 | |
29676 |
|
29677 |
|
29678 | indexOfTag(...args) {
|
29679 | const tagIndex = [];
|
29680 | const that = this;
|
29681 |
|
29682 | const handle = tag => packetType => tag === packetType;
|
29683 |
|
29684 | for (let i = 0; i < this.length; i++) {
|
29685 | if (args.some(handle(that[i].tag))) {
|
29686 | tagIndex.push(i);
|
29687 | }
|
29688 | }
|
29689 | return tagIndex;
|
29690 | }
|
29691 |
|
29692 | |
29693 |
|
29694 |
|
29695 | concat(packetlist) {
|
29696 | if (packetlist) {
|
29697 | for (let i = 0; i < packetlist.length; i++) {
|
29698 | this.push(packetlist[i]);
|
29699 | }
|
29700 | }
|
29701 | return this;
|
29702 | }
|
29703 | }
|
29704 |
|
29705 |
|
29706 |
|
29707 |
|
29708 |
|
29709 |
|
29710 | class Signature {
|
29711 | |
29712 |
|
29713 |
|
29714 | constructor(packetlist) {
|
29715 | this.packets = packetlist || new PacketList();
|
29716 | }
|
29717 |
|
29718 | |
29719 |
|
29720 |
|
29721 |
|
29722 | write() {
|
29723 | return this.packets.write();
|
29724 | }
|
29725 |
|
29726 | |
29727 |
|
29728 |
|
29729 |
|
29730 |
|
29731 | armor(config = defaultConfig) {
|
29732 | return armor(enums.armor.signature, this.write(), undefined, undefined, undefined, config);
|
29733 | }
|
29734 | }
|
29735 |
|
29736 |
|
29737 |
|
29738 |
|
29739 |
|
29740 |
|
29741 |
|
29742 |
|
29743 |
|
29744 |
|
29745 |
|
29746 | async function readSignature({ armoredSignature, binarySignature, config }) {
|
29747 | config = { ...defaultConfig, ...config };
|
29748 | let input = armoredSignature || binarySignature;
|
29749 | if (!input) {
|
29750 | throw new Error('readSignature: must pass options object containing `armoredSignature` or `binarySignature`');
|
29751 | }
|
29752 | if (armoredSignature) {
|
29753 | const { type, data } = await unarmor(input, config);
|
29754 | if (type !== enums.armor.signature) {
|
29755 | throw new Error('Armored text not of type signature');
|
29756 | }
|
29757 | input = data;
|
29758 | }
|
29759 | const packetlist = new PacketList();
|
29760 | await packetlist.read(input, { SignaturePacket }, undefined, config);
|
29761 | return new Signature(packetlist);
|
29762 | }
|
29763 |
|
29764 |
|
29765 |
|
29766 |
|
29767 |
|
29768 |
|
29769 |
|
29770 | const allowedKeyPackets = {
|
29771 | PublicKeyPacket,
|
29772 | PublicSubkeyPacket,
|
29773 | SecretKeyPacket,
|
29774 | SecretSubkeyPacket,
|
29775 | UserIDPacket,
|
29776 | UserAttributePacket,
|
29777 | SignaturePacket
|
29778 | };
|
29779 |
|
29780 | async function generateSecretSubkey(options, config) {
|
29781 | const secretSubkeyPacket = new SecretSubkeyPacket(options.date, config);
|
29782 | secretSubkeyPacket.packets = null;
|
29783 | secretSubkeyPacket.algorithm = enums.read(enums.publicKey, options.algorithm);
|
29784 | await secretSubkeyPacket.generate(options.rsaBits, options.curve);
|
29785 | return secretSubkeyPacket;
|
29786 | }
|
29787 |
|
29788 | async function generateSecretKey(options, config) {
|
29789 | const secretKeyPacket = new SecretKeyPacket(options.date, config);
|
29790 | secretKeyPacket.packets = null;
|
29791 | secretKeyPacket.algorithm = enums.read(enums.publicKey, options.algorithm);
|
29792 | await secretKeyPacket.generate(options.rsaBits, options.curve, options.config);
|
29793 | return secretKeyPacket;
|
29794 | }
|
29795 |
|
29796 |
|
29797 |
|
29798 |
|
29799 |
|
29800 |
|
29801 |
|
29802 |
|
29803 |
|
29804 | async function getLatestValidSignature(signatures, primaryKey, signatureType, dataToVerify, date = new Date(), config) {
|
29805 | let signature;
|
29806 | let exception;
|
29807 | for (let i = signatures.length - 1; i >= 0; i--) {
|
29808 | try {
|
29809 | if (
|
29810 | (!signature || signatures[i].created >= signature.created) &&
|
29811 |
|
29812 | !signatures[i].isExpired(date)
|
29813 | ) {
|
29814 |
|
29815 | signatures[i].verified || await signatures[i].verify(primaryKey, signatureType, dataToVerify, undefined, undefined, config);
|
29816 | signature = signatures[i];
|
29817 | }
|
29818 | } catch (e) {
|
29819 | exception = e;
|
29820 | }
|
29821 | }
|
29822 | if (!signature) {
|
29823 | throw util.wrapError(
|
29824 | `Could not find valid ${enums.read(enums.signature, signatureType)} signature in key ${primaryKey.getKeyId().toHex()}`
|
29825 | .replace('certGeneric ', 'self-')
|
29826 | .replace(/([a-z])([A-Z])/g, (_, $1, $2) => $1 + ' ' + $2.toLowerCase())
|
29827 | , exception);
|
29828 | }
|
29829 | return signature;
|
29830 | }
|
29831 |
|
29832 | function isDataExpired(keyPacket, signature, date = new Date()) {
|
29833 | const normDate = util.normalizeDate(date);
|
29834 | if (normDate !== null) {
|
29835 | const expirationTime = getExpirationTime(keyPacket, signature);
|
29836 | return !(keyPacket.created <= normDate && normDate <= expirationTime) ||
|
29837 | (signature && signature.isExpired(date));
|
29838 | }
|
29839 | return false;
|
29840 | }
|
29841 |
|
29842 |
|
29843 |
|
29844 |
|
29845 |
|
29846 |
|
29847 |
|
29848 |
|
29849 | async function createBindingSignature(subkey, primaryKey, options, config) {
|
29850 | const dataToSign = {};
|
29851 | dataToSign.key = primaryKey;
|
29852 | dataToSign.bind = subkey;
|
29853 | const subkeySignaturePacket = new SignaturePacket(options.date);
|
29854 | subkeySignaturePacket.signatureType = enums.signature.subkeyBinding;
|
29855 | subkeySignaturePacket.publicKeyAlgorithm = primaryKey.algorithm;
|
29856 | subkeySignaturePacket.hashAlgorithm = await getPreferredHashAlgo$1(null, subkey, undefined, undefined, config);
|
29857 | if (options.sign) {
|
29858 | subkeySignaturePacket.keyFlags = [enums.keyFlags.signData];
|
29859 | subkeySignaturePacket.embeddedSignature = await createSignaturePacket(dataToSign, null, subkey, {
|
29860 | signatureType: enums.signature.keyBinding
|
29861 | }, options.date, undefined, undefined, undefined, config);
|
29862 | } else {
|
29863 | subkeySignaturePacket.keyFlags = [enums.keyFlags.encryptCommunication | enums.keyFlags.encryptStorage];
|
29864 | }
|
29865 | if (options.keyExpirationTime > 0) {
|
29866 | subkeySignaturePacket.keyExpirationTime = options.keyExpirationTime;
|
29867 | subkeySignaturePacket.keyNeverExpires = false;
|
29868 | }
|
29869 | await subkeySignaturePacket.sign(primaryKey, dataToSign);
|
29870 | return subkeySignaturePacket;
|
29871 | }
|
29872 |
|
29873 |
|
29874 |
|
29875 |
|
29876 |
|
29877 |
|
29878 |
|
29879 |
|
29880 |
|
29881 |
|
29882 |
|
29883 | async function getPreferredHashAlgo$1(key, keyPacket, date = new Date(), userId = {}, config) {
|
29884 | let hash_algo = config.preferHashAlgorithm;
|
29885 | let pref_algo = hash_algo;
|
29886 | if (key) {
|
29887 | const primaryUser = await key.getPrimaryUser(date, userId, config);
|
29888 | if (primaryUser.selfCertification.preferredHashAlgorithms) {
|
29889 | [pref_algo] = primaryUser.selfCertification.preferredHashAlgorithms;
|
29890 | hash_algo = mod.hash.getHashByteLength(hash_algo) <= mod.hash.getHashByteLength(pref_algo) ?
|
29891 | pref_algo : hash_algo;
|
29892 | }
|
29893 | }
|
29894 | switch (Object.getPrototypeOf(keyPacket)) {
|
29895 | case SecretKeyPacket.prototype:
|
29896 | case PublicKeyPacket.prototype:
|
29897 | case SecretSubkeyPacket.prototype:
|
29898 | case PublicSubkeyPacket.prototype:
|
29899 | switch (keyPacket.algorithm) {
|
29900 | case 'ecdh':
|
29901 | case 'ecdsa':
|
29902 | case 'eddsa':
|
29903 | pref_algo = mod.publicKey.elliptic.getPreferredHashAlgo(keyPacket.publicParams.oid);
|
29904 | }
|
29905 | }
|
29906 | return mod.hash.getHashByteLength(hash_algo) <= mod.hash.getHashByteLength(pref_algo) ?
|
29907 | pref_algo : hash_algo;
|
29908 | }
|
29909 |
|
29910 |
|
29911 |
|
29912 |
|
29913 |
|
29914 |
|
29915 |
|
29916 |
|
29917 |
|
29918 |
|
29919 |
|
29920 | async function getPreferredAlgo(type, keys, date = new Date(), userIds = [], config = defaultConfig) {
|
29921 | const prefProperty = type === 'symmetric' ? 'preferredSymmetricAlgorithms' : 'preferredAeadAlgorithms';
|
29922 | const defaultAlgo = type === 'symmetric' ? enums.symmetric.aes128 : enums.aead.eax;
|
29923 | const prioMap = {};
|
29924 | await Promise.all(keys.map(async function(key, i) {
|
29925 | const primaryUser = await key.getPrimaryUser(date, userIds[i], config);
|
29926 | if (!primaryUser.selfCertification[prefProperty]) {
|
29927 | return defaultAlgo;
|
29928 | }
|
29929 | primaryUser.selfCertification[prefProperty].forEach(function(algo, index) {
|
29930 | const entry = prioMap[algo] || (prioMap[algo] = { prio: 0, count: 0, algo: algo });
|
29931 | entry.prio += 64 >> index;
|
29932 | entry.count++;
|
29933 | });
|
29934 | }));
|
29935 | let prefAlgo = { prio: 0, algo: defaultAlgo };
|
29936 | Object.values(prioMap).forEach(({ prio, count, algo }) => {
|
29937 | try {
|
29938 | if (algo !== enums[type].plaintext &&
|
29939 | algo !== enums[type].idea &&
|
29940 | enums.read(enums[type], algo) &&
|
29941 | count === keys.length &&
|
29942 | prio > prefAlgo.prio) {
|
29943 | prefAlgo = prioMap[algo];
|
29944 | }
|
29945 | } catch (e) {}
|
29946 | });
|
29947 | return prefAlgo.algo;
|
29948 | }
|
29949 |
|
29950 |
|
29951 |
|
29952 |
|
29953 |
|
29954 |
|
29955 |
|
29956 |
|
29957 |
|
29958 |
|
29959 |
|
29960 |
|
29961 |
|
29962 |
|
29963 |
|
29964 | async function createSignaturePacket(dataToSign, privateKey, signingKeyPacket, signatureProperties, date, userId, detached = false, streaming = false, config) {
|
29965 | if (signingKeyPacket.isDummy()) {
|
29966 | throw new Error('Cannot sign with a gnu-dummy key.');
|
29967 | }
|
29968 | if (!signingKeyPacket.isDecrypted()) {
|
29969 | throw new Error('Private key is not decrypted.');
|
29970 | }
|
29971 | const signaturePacket = new SignaturePacket(date);
|
29972 | Object.assign(signaturePacket, signatureProperties);
|
29973 | signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
|
29974 | signaturePacket.hashAlgorithm = await getPreferredHashAlgo$1(privateKey, signingKeyPacket, date, userId, config);
|
29975 | await signaturePacket.sign(signingKeyPacket, dataToSign, detached, streaming);
|
29976 | return signaturePacket;
|
29977 | }
|
29978 |
|
29979 |
|
29980 |
|
29981 |
|
29982 |
|
29983 |
|
29984 |
|
29985 |
|
29986 | async function mergeSignatures(source, dest, attr, checkFn) {
|
29987 | source = source[attr];
|
29988 | if (source) {
|
29989 | if (!dest[attr].length) {
|
29990 | dest[attr] = source;
|
29991 | } else {
|
29992 | await Promise.all(source.map(async function(sourceSig) {
|
29993 | if (!sourceSig.isExpired() && (!checkFn || await checkFn(sourceSig)) &&
|
29994 | !dest[attr].some(function(destSig) {
|
29995 | return util.equalsUint8Array(destSig.write_params(), sourceSig.write_params());
|
29996 | })) {
|
29997 | dest[attr].push(sourceSig);
|
29998 | }
|
29999 | }));
|
30000 | }
|
30001 | }
|
30002 | }
|
30003 |
|
30004 |
|
30005 |
|
30006 |
|
30007 |
|
30008 |
|
30009 |
|
30010 |
|
30011 |
|
30012 |
|
30013 |
|
30014 |
|
30015 |
|
30016 |
|
30017 |
|
30018 |
|
30019 |
|
30020 | async function isDataRevoked(primaryKey, signatureType, dataToVerify, revocations, signature, key, date = new Date(), config) {
|
30021 | key = key || primaryKey;
|
30022 | const normDate = util.normalizeDate(date);
|
30023 | const revocationKeyIds = [];
|
30024 | await Promise.all(revocations.map(async function(revocationSignature) {
|
30025 | try {
|
30026 | if (
|
30027 |
|
30028 |
|
30029 |
|
30030 |
|
30031 |
|
30032 |
|
30033 |
|
30034 |
|
30035 | (!signature || revocationSignature.issuerKeyId.equals(signature.issuerKeyId)) &&
|
30036 | !(config.revocationsExpire && revocationSignature.isExpired(normDate))
|
30037 | ) {
|
30038 | revocationSignature.verified || await revocationSignature.verify(key, signatureType, dataToVerify, undefined, undefined, config);
|
30039 |
|
30040 |
|
30041 | revocationKeyIds.push(revocationSignature.issuerKeyId);
|
30042 | }
|
30043 | } catch (e) {}
|
30044 | }));
|
30045 |
|
30046 | if (signature) {
|
30047 | signature.revoked = revocationKeyIds.some(keyId => keyId.equals(signature.issuerKeyId)) ? true :
|
30048 | signature.revoked || false;
|
30049 | return signature.revoked;
|
30050 | }
|
30051 | return revocationKeyIds.length > 0;
|
30052 | }
|
30053 |
|
30054 | function getExpirationTime(keyPacket, signature) {
|
30055 | let expirationTime;
|
30056 |
|
30057 | if (signature.keyNeverExpires === false) {
|
30058 | expirationTime = keyPacket.created.getTime() + signature.keyExpirationTime * 1000;
|
30059 | }
|
30060 | return expirationTime ? new Date(expirationTime) : Infinity;
|
30061 | }
|
30062 |
|
30063 |
|
30064 |
|
30065 |
|
30066 |
|
30067 |
|
30068 |
|
30069 |
|
30070 |
|
30071 |
|
30072 | async function isAeadSupported(keys, date = new Date(), userIds = [], config = defaultConfig) {
|
30073 | let supported = true;
|
30074 |
|
30075 | await Promise.all(keys.map(async function(key, i) {
|
30076 | const primaryUser = await key.getPrimaryUser(date, userIds[i], config);
|
30077 | if (!primaryUser.selfCertification.features ||
|
30078 | !(primaryUser.selfCertification.features[0] & enums.features.aead)) {
|
30079 | supported = false;
|
30080 | }
|
30081 | }));
|
30082 | return supported;
|
30083 | }
|
30084 |
|
30085 | function sanitizeKeyOptions(options, subkeyDefaults = {}) {
|
30086 | options.type = options.type || subkeyDefaults.type;
|
30087 | options.curve = options.curve || subkeyDefaults.curve;
|
30088 | options.rsaBits = options.rsaBits || subkeyDefaults.rsaBits;
|
30089 | options.keyExpirationTime = options.keyExpirationTime !== undefined ? options.keyExpirationTime : subkeyDefaults.keyExpirationTime;
|
30090 | options.passphrase = util.isString(options.passphrase) ? options.passphrase : subkeyDefaults.passphrase;
|
30091 | options.date = options.date || subkeyDefaults.date;
|
30092 |
|
30093 | options.sign = options.sign || false;
|
30094 |
|
30095 | switch (options.type) {
|
30096 | case 'ecc':
|
30097 | try {
|
30098 | options.curve = enums.write(enums.curve, options.curve);
|
30099 | } catch (e) {
|
30100 | throw new Error('Invalid curve');
|
30101 | }
|
30102 | if (options.curve === enums.curve.ed25519 || options.curve === enums.curve.curve25519) {
|
30103 | options.curve = options.sign ? enums.curve.ed25519 : enums.curve.curve25519;
|
30104 | }
|
30105 | if (options.sign) {
|
30106 | options.algorithm = options.curve === enums.curve.ed25519 ? enums.publicKey.eddsa : enums.publicKey.ecdsa;
|
30107 | } else {
|
30108 | options.algorithm = enums.publicKey.ecdh;
|
30109 | }
|
30110 | break;
|
30111 | case 'rsa':
|
30112 | options.algorithm = enums.publicKey.rsaEncryptSign;
|
30113 | break;
|
30114 | default:
|
30115 | throw new Error(`Unsupported key type ${options.type}`);
|
30116 | }
|
30117 | return options;
|
30118 | }
|
30119 |
|
30120 | function isValidSigningKeyPacket(keyPacket, signature) {
|
30121 | if (!signature.verified || signature.revoked !== false) {
|
30122 | throw new Error('Signature not verified');
|
30123 | }
|
30124 | return keyPacket.algorithm !== enums.read(enums.publicKey, enums.publicKey.rsaEncrypt) &&
|
30125 | keyPacket.algorithm !== enums.read(enums.publicKey, enums.publicKey.elgamal) &&
|
30126 | keyPacket.algorithm !== enums.read(enums.publicKey, enums.publicKey.ecdh) &&
|
30127 | (!signature.keyFlags ||
|
30128 | (signature.keyFlags[0] & enums.keyFlags.signData) !== 0);
|
30129 | }
|
30130 |
|
30131 | function isValidEncryptionKeyPacket(keyPacket, signature) {
|
30132 | if (!signature.verified || signature.revoked !== false) {
|
30133 | throw new Error('Signature not verified');
|
30134 | }
|
30135 | return keyPacket.algorithm !== enums.read(enums.publicKey, enums.publicKey.dsa) &&
|
30136 | keyPacket.algorithm !== enums.read(enums.publicKey, enums.publicKey.rsaSign) &&
|
30137 | keyPacket.algorithm !== enums.read(enums.publicKey, enums.publicKey.ecdsa) &&
|
30138 | keyPacket.algorithm !== enums.read(enums.publicKey, enums.publicKey.eddsa) &&
|
30139 | (!signature.keyFlags ||
|
30140 | (signature.keyFlags[0] & enums.keyFlags.encryptCommunication) !== 0 ||
|
30141 | (signature.keyFlags[0] & enums.keyFlags.encryptStorage) !== 0);
|
30142 | }
|
30143 |
|
30144 | function isValidDecryptionKeyPacket(signature, config) {
|
30145 | if (!signature.verified) {
|
30146 | throw new Error('Signature not verified');
|
30147 | }
|
30148 |
|
30149 | if (config.allowInsecureDecryptionWithSigningKeys) {
|
30150 |
|
30151 | return true;
|
30152 | }
|
30153 |
|
30154 | return !signature.keyFlags ||
|
30155 | (signature.keyFlags[0] & enums.keyFlags.encryptCommunication) !== 0 ||
|
30156 | (signature.keyFlags[0] & enums.keyFlags.encryptStorage) !== 0;
|
30157 | }
|
30158 |
|
30159 |
|
30160 |
|
30161 |
|
30162 |
|
30163 |
|
30164 |
|
30165 |
|
30166 |
|
30167 | class User {
|
30168 | constructor(userPacket) {
|
30169 | if (!(this instanceof User)) {
|
30170 | return new User(userPacket);
|
30171 | }
|
30172 | this.userId = userPacket.tag === enums.packet.userID ? userPacket : null;
|
30173 | this.userAttribute = userPacket.tag === enums.packet.userAttribute ? userPacket : null;
|
30174 | this.selfCertifications = [];
|
30175 | this.otherCertifications = [];
|
30176 | this.revocationSignatures = [];
|
30177 | }
|
30178 |
|
30179 | |
30180 |
|
30181 |
|
30182 |
|
30183 | toPacketlist() {
|
30184 | const packetlist = new PacketList();
|
30185 | packetlist.push(this.userId || this.userAttribute);
|
30186 | packetlist.concat(this.revocationSignatures);
|
30187 | packetlist.concat(this.selfCertifications);
|
30188 | packetlist.concat(this.otherCertifications);
|
30189 | return packetlist;
|
30190 | }
|
30191 |
|
30192 | |
30193 |
|
30194 |
|
30195 |
|
30196 |
|
30197 |
|
30198 |
|
30199 |
|
30200 |
|
30201 | async sign(primaryKey, privateKeys, config) {
|
30202 | const dataToSign = {
|
30203 | userId: this.userId,
|
30204 | userAttribute: this.userAttribute,
|
30205 | key: primaryKey
|
30206 | };
|
30207 | const user = new User(dataToSign.userId || dataToSign.userAttribute);
|
30208 | user.otherCertifications = await Promise.all(privateKeys.map(async function(privateKey) {
|
30209 | if (privateKey.isPublic()) {
|
30210 | throw new Error('Need private key for signing');
|
30211 | }
|
30212 | if (privateKey.hasSameFingerprintAs(primaryKey)) {
|
30213 | throw new Error('Not implemented for self signing');
|
30214 | }
|
30215 | const signingKey = await privateKey.getSigningKey(undefined, undefined, undefined, config);
|
30216 | return createSignaturePacket(dataToSign, privateKey, signingKey.keyPacket, {
|
30217 |
|
30218 | signatureType: enums.signature.certGeneric,
|
30219 | keyFlags: [enums.keyFlags.certifyKeys | enums.keyFlags.signData]
|
30220 | }, undefined, undefined, undefined, undefined, config);
|
30221 | }));
|
30222 | await user.update(this, primaryKey);
|
30223 | return user;
|
30224 | }
|
30225 |
|
30226 | |
30227 |
|
30228 |
|
30229 |
|
30230 |
|
30231 |
|
30232 |
|
30233 |
|
30234 |
|
30235 |
|
30236 |
|
30237 |
|
30238 |
|
30239 |
|
30240 | async isRevoked(primaryKey, certificate, key, date = new Date(), config) {
|
30241 | return isDataRevoked(
|
30242 | primaryKey, enums.signature.certRevocation, {
|
30243 | key: primaryKey,
|
30244 | userId: this.userId,
|
30245 | userAttribute: this.userAttribute
|
30246 | }, this.revocationSignatures, certificate, key, date, config
|
30247 | );
|
30248 | }
|
30249 |
|
30250 | |
30251 |
|
30252 |
|
30253 |
|
30254 |
|
30255 |
|
30256 |
|
30257 |
|
30258 |
|
30259 |
|
30260 |
|
30261 | async verifyCertificate(primaryKey, certificate, keys, date = new Date(), config) {
|
30262 | const that = this;
|
30263 | const keyid = certificate.issuerKeyId;
|
30264 | const dataToVerify = {
|
30265 | userId: this.userId,
|
30266 | userAttribute: this.userAttribute,
|
30267 | key: primaryKey
|
30268 | };
|
30269 | const results = await Promise.all(keys.map(async function(key) {
|
30270 | if (!key.getKeyIds().some(id => id.equals(keyid))) {
|
30271 | return null;
|
30272 | }
|
30273 | const signingKey = await key.getSigningKey(keyid, date, undefined, config);
|
30274 | if (certificate.revoked || await that.isRevoked(primaryKey, certificate, signingKey.keyPacket, date, config)) {
|
30275 | throw new Error('User certificate is revoked');
|
30276 | }
|
30277 | try {
|
30278 | certificate.verified || await certificate.verify(signingKey.keyPacket, enums.signature.certGeneric, dataToVerify, undefined, undefined, config);
|
30279 | } catch (e) {
|
30280 | throw util.wrapError('User certificate is invalid', e);
|
30281 | }
|
30282 | if (certificate.isExpired(date)) {
|
30283 | throw new Error('User certificate is expired');
|
30284 | }
|
30285 | return true;
|
30286 | }));
|
30287 | return results.find(result => result !== null) || null;
|
30288 | }
|
30289 |
|
30290 | |
30291 |
|
30292 |
|
30293 |
|
30294 |
|
30295 |
|
30296 |
|
30297 |
|
30298 |
|
30299 |
|
30300 |
|
30301 | async verifyAllCertifications(primaryKey, keys, date = new Date(), config) {
|
30302 | const that = this;
|
30303 | const certifications = this.selfCertifications.concat(this.otherCertifications);
|
30304 | return Promise.all(certifications.map(async function(certification) {
|
30305 | return {
|
30306 | keyid: certification.issuerKeyId,
|
30307 | valid: await that.verifyCertificate(primaryKey, certification, keys, date, config).catch(() => false)
|
30308 | };
|
30309 | }));
|
30310 | }
|
30311 |
|
30312 | |
30313 |
|
30314 |
|
30315 |
|
30316 |
|
30317 |
|
30318 |
|
30319 |
|
30320 |
|
30321 |
|
30322 |
|
30323 | async verify(primaryKey, date = new Date(), config) {
|
30324 | if (!this.selfCertifications.length) {
|
30325 | throw new Error('No self-certifications');
|
30326 | }
|
30327 | const that = this;
|
30328 | const dataToVerify = {
|
30329 | userId: this.userId,
|
30330 | userAttribute: this.userAttribute,
|
30331 | key: primaryKey
|
30332 | };
|
30333 |
|
30334 | let exception;
|
30335 | for (let i = this.selfCertifications.length - 1; i >= 0; i--) {
|
30336 | try {
|
30337 | const selfCertification = this.selfCertifications[i];
|
30338 | if (selfCertification.revoked || await that.isRevoked(primaryKey, selfCertification, undefined, date, config)) {
|
30339 | throw new Error('Self-certification is revoked');
|
30340 | }
|
30341 | try {
|
30342 | selfCertification.verified || await selfCertification.verify(primaryKey, enums.signature.certGeneric, dataToVerify, undefined, undefined, config);
|
30343 | } catch (e) {
|
30344 | throw util.wrapError('Self-certification is invalid', e);
|
30345 | }
|
30346 | if (selfCertification.isExpired(date)) {
|
30347 | throw new Error('Self-certification is expired');
|
30348 | }
|
30349 | return true;
|
30350 | } catch (e) {
|
30351 | exception = e;
|
30352 | }
|
30353 | }
|
30354 | throw exception;
|
30355 | }
|
30356 |
|
30357 | |
30358 |
|
30359 |
|
30360 |
|
30361 |
|
30362 |
|
30363 |
|
30364 |
|
30365 |
|
30366 | async update(user, primaryKey, config) {
|
30367 | const dataToVerify = {
|
30368 | userId: this.userId,
|
30369 | userAttribute: this.userAttribute,
|
30370 | key: primaryKey
|
30371 | };
|
30372 |
|
30373 | await mergeSignatures(user, this, 'selfCertifications', async function(srcSelfSig) {
|
30374 | try {
|
30375 | srcSelfSig.verified || await srcSelfSig.verify(primaryKey, enums.signature.certGeneric, dataToVerify, undefined, undefined, config);
|
30376 | return true;
|
30377 | } catch (e) {
|
30378 | return false;
|
30379 | }
|
30380 | });
|
30381 |
|
30382 | await mergeSignatures(user, this, 'otherCertifications');
|
30383 |
|
30384 | await mergeSignatures(user, this, 'revocationSignatures', function(srcRevSig) {
|
30385 | return isDataRevoked(primaryKey, enums.signature.certRevocation, dataToVerify, [srcRevSig], undefined, undefined, undefined, config);
|
30386 | });
|
30387 | }
|
30388 | }
|
30389 |
|
30390 |
|
30391 |
|
30392 |
|
30393 |
|
30394 |
|
30395 |
|
30396 |
|
30397 |
|
30398 |
|
30399 |
|
30400 |
|
30401 |
|
30402 |
|
30403 |
|
30404 | class SubKey {
|
30405 | constructor(subKeyPacket) {
|
30406 | if (!(this instanceof SubKey)) {
|
30407 | return new SubKey(subKeyPacket);
|
30408 | }
|
30409 | this.keyPacket = subKeyPacket;
|
30410 | this.bindingSignatures = [];
|
30411 | this.revocationSignatures = [];
|
30412 | }
|
30413 |
|
30414 | |
30415 |
|
30416 |
|
30417 |
|
30418 | toPacketlist() {
|
30419 | const packetlist = new PacketList();
|
30420 | packetlist.push(this.keyPacket);
|
30421 | packetlist.concat(this.revocationSignatures);
|
30422 | packetlist.concat(this.bindingSignatures);
|
30423 | return packetlist;
|
30424 | }
|
30425 |
|
30426 | |
30427 |
|
30428 |
|
30429 |
|
30430 |
|
30431 |
|
30432 |
|
30433 |
|
30434 |
|
30435 |
|
30436 |
|
30437 |
|
30438 |
|
30439 |
|
30440 | async isRevoked(primaryKey, signature, key, date = new Date(), config = defaultConfig) {
|
30441 | return isDataRevoked(
|
30442 | primaryKey, enums.signature.subkeyRevocation, {
|
30443 | key: primaryKey,
|
30444 | bind: this.keyPacket
|
30445 | }, this.revocationSignatures, signature, key, date, config
|
30446 | );
|
30447 | }
|
30448 |
|
30449 | |
30450 |
|
30451 |
|
30452 |
|
30453 |
|
30454 |
|
30455 |
|
30456 |
|
30457 |
|
30458 |
|
30459 |
|
30460 | async verify(primaryKey, date = new Date(), config = defaultConfig) {
|
30461 | const dataToVerify = { key: primaryKey, bind: this.keyPacket };
|
30462 |
|
30463 | const bindingSignature = await getLatestValidSignature(this.bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
30464 |
|
30465 | if (bindingSignature.revoked || await this.isRevoked(primaryKey, bindingSignature, null, date, config)) {
|
30466 | throw new Error('Subkey is revoked');
|
30467 | }
|
30468 |
|
30469 | if (isDataExpired(this.keyPacket, bindingSignature, date)) {
|
30470 | throw new Error('Subkey is expired');
|
30471 | }
|
30472 | return bindingSignature;
|
30473 | }
|
30474 |
|
30475 | |
30476 |
|
30477 |
|
30478 |
|
30479 |
|
30480 |
|
30481 |
|
30482 |
|
30483 |
|
30484 |
|
30485 | async getExpirationTime(primaryKey, date = new Date(), config = defaultConfig) {
|
30486 | const dataToVerify = { key: primaryKey, bind: this.keyPacket };
|
30487 | let bindingSignature;
|
30488 | try {
|
30489 | bindingSignature = await getLatestValidSignature(this.bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
30490 | } catch (e) {
|
30491 | return null;
|
30492 | }
|
30493 | const keyExpiry = getExpirationTime(this.keyPacket, bindingSignature);
|
30494 | const sigExpiry = bindingSignature.getExpirationTime();
|
30495 | return keyExpiry < sigExpiry ? keyExpiry : sigExpiry;
|
30496 | }
|
30497 |
|
30498 | |
30499 |
|
30500 |
|
30501 |
|
30502 |
|
30503 |
|
30504 |
|
30505 |
|
30506 |
|
30507 | async update(subKey, primaryKey, config = defaultConfig) {
|
30508 | if (!this.hasSameFingerprintAs(subKey)) {
|
30509 | throw new Error('SubKey update method: fingerprints of subkeys not equal');
|
30510 | }
|
30511 |
|
30512 | if (this.keyPacket.tag === enums.packet.publicSubkey &&
|
30513 | subKey.keyPacket.tag === enums.packet.secretSubkey) {
|
30514 | this.keyPacket = subKey.keyPacket;
|
30515 | }
|
30516 |
|
30517 | const that = this;
|
30518 | const dataToVerify = { key: primaryKey, bind: that.keyPacket };
|
30519 | await mergeSignatures(subKey, this, 'bindingSignatures', async function(srcBindSig) {
|
30520 | for (let i = 0; i < that.bindingSignatures.length; i++) {
|
30521 | if (that.bindingSignatures[i].issuerKeyId.equals(srcBindSig.issuerKeyId)) {
|
30522 | if (srcBindSig.created > that.bindingSignatures[i].created) {
|
30523 | that.bindingSignatures[i] = srcBindSig;
|
30524 | }
|
30525 | return false;
|
30526 | }
|
30527 | }
|
30528 | try {
|
30529 | srcBindSig.verified || await srcBindSig.verify(primaryKey, enums.signature.subkeyBinding, dataToVerify, undefined, undefined, config);
|
30530 | return true;
|
30531 | } catch (e) {
|
30532 | return false;
|
30533 | }
|
30534 | });
|
30535 |
|
30536 | await mergeSignatures(subKey, this, 'revocationSignatures', function(srcRevSig) {
|
30537 | return isDataRevoked(primaryKey, enums.signature.subkeyRevocation, dataToVerify, [srcRevSig], undefined, undefined, undefined, config);
|
30538 | });
|
30539 | }
|
30540 |
|
30541 | |
30542 |
|
30543 |
|
30544 |
|
30545 |
|
30546 |
|
30547 |
|
30548 |
|
30549 |
|
30550 |
|
30551 |
|
30552 | async revoke(
|
30553 | primaryKey,
|
30554 | {
|
30555 | flag: reasonForRevocationFlag = enums.reasonForRevocation.noReason,
|
30556 | string: reasonForRevocationString = ''
|
30557 | } = {},
|
30558 | date = new Date(),
|
30559 | config = defaultConfig
|
30560 | ) {
|
30561 | const dataToSign = { key: primaryKey, bind: this.keyPacket };
|
30562 | const subKey = new SubKey(this.keyPacket);
|
30563 | subKey.revocationSignatures.push(await createSignaturePacket(dataToSign, null, primaryKey, {
|
30564 | signatureType: enums.signature.subkeyRevocation,
|
30565 | reasonForRevocationFlag: enums.write(enums.reasonForRevocation, reasonForRevocationFlag),
|
30566 | reasonForRevocationString
|
30567 | }, date, undefined, undefined, undefined, config));
|
30568 | await subKey.update(this, primaryKey);
|
30569 | return subKey;
|
30570 | }
|
30571 |
|
30572 | hasSameFingerprintAs(other) {
|
30573 | return this.keyPacket.hasSameFingerprintAs(other.keyPacket || other);
|
30574 | }
|
30575 | }
|
30576 |
|
30577 | ['getKeyId', 'getFingerprint', 'getAlgorithmInfo', 'getCreationTime', 'isDecrypted'].forEach(name => {
|
30578 | SubKey.prototype[name] =
|
30579 | function() {
|
30580 | return this.keyPacket[name]();
|
30581 | };
|
30582 | });
|
30583 |
|
30584 |
|
30585 |
|
30586 |
|
30587 |
|
30588 |
|
30589 |
|
30590 |
|
30591 |
|
30592 |
|
30593 |
|
30594 |
|
30595 | class Key {
|
30596 | |
30597 |
|
30598 |
|
30599 | constructor(packetlist) {
|
30600 | if (!(this instanceof Key)) {
|
30601 | return new Key(packetlist);
|
30602 | }
|
30603 |
|
30604 | this.keyPacket = null;
|
30605 | this.revocationSignatures = [];
|
30606 | this.directSignatures = [];
|
30607 | this.users = [];
|
30608 | this.subKeys = [];
|
30609 | this.packetlist2structure(packetlist);
|
30610 | if (!this.keyPacket) {
|
30611 | throw new Error('Invalid key: need at least key packet');
|
30612 | }
|
30613 | }
|
30614 |
|
30615 | get primaryKey() {
|
30616 | return this.keyPacket;
|
30617 | }
|
30618 |
|
30619 | |
30620 |
|
30621 |
|
30622 |
|
30623 | packetlist2structure(packetlist) {
|
30624 | let user;
|
30625 | let primaryKeyId;
|
30626 | let subKey;
|
30627 | for (let i = 0; i < packetlist.length; i++) {
|
30628 | switch (packetlist[i].tag) {
|
30629 | case enums.packet.publicKey:
|
30630 | case enums.packet.secretKey:
|
30631 | if (this.keyPacket) {
|
30632 | throw new Error('Key block contains multiple keys');
|
30633 | }
|
30634 | this.keyPacket = packetlist[i];
|
30635 | primaryKeyId = this.getKeyId();
|
30636 | break;
|
30637 | case enums.packet.userID:
|
30638 | case enums.packet.userAttribute:
|
30639 | user = new User(packetlist[i]);
|
30640 | this.users.push(user);
|
30641 | break;
|
30642 | case enums.packet.publicSubkey:
|
30643 | case enums.packet.secretSubkey:
|
30644 | user = null;
|
30645 | subKey = new SubKey(packetlist[i]);
|
30646 | this.subKeys.push(subKey);
|
30647 | break;
|
30648 | case enums.packet.signature:
|
30649 | switch (packetlist[i].signatureType) {
|
30650 | case enums.signature.certGeneric:
|
30651 | case enums.signature.certPersona:
|
30652 | case enums.signature.certCasual:
|
30653 | case enums.signature.certPositive:
|
30654 | if (!user) {
|
30655 | util.printDebug('Dropping certification signatures without preceding user packet');
|
30656 | continue;
|
30657 | }
|
30658 | if (packetlist[i].issuerKeyId.equals(primaryKeyId)) {
|
30659 | user.selfCertifications.push(packetlist[i]);
|
30660 | } else {
|
30661 | user.otherCertifications.push(packetlist[i]);
|
30662 | }
|
30663 | break;
|
30664 | case enums.signature.certRevocation:
|
30665 | if (user) {
|
30666 | user.revocationSignatures.push(packetlist[i]);
|
30667 | } else {
|
30668 | this.directSignatures.push(packetlist[i]);
|
30669 | }
|
30670 | break;
|
30671 | case enums.signature.key:
|
30672 | this.directSignatures.push(packetlist[i]);
|
30673 | break;
|
30674 | case enums.signature.subkeyBinding:
|
30675 | if (!subKey) {
|
30676 | util.printDebug('Dropping subkey binding signature without preceding subkey packet');
|
30677 | continue;
|
30678 | }
|
30679 | subKey.bindingSignatures.push(packetlist[i]);
|
30680 | break;
|
30681 | case enums.signature.keyRevocation:
|
30682 | this.revocationSignatures.push(packetlist[i]);
|
30683 | break;
|
30684 | case enums.signature.subkeyRevocation:
|
30685 | if (!subKey) {
|
30686 | util.printDebug('Dropping subkey revocation signature without preceding subkey packet');
|
30687 | continue;
|
30688 | }
|
30689 | subKey.revocationSignatures.push(packetlist[i]);
|
30690 | break;
|
30691 | }
|
30692 | break;
|
30693 | }
|
30694 | }
|
30695 | }
|
30696 |
|
30697 | |
30698 |
|
30699 |
|
30700 |
|
30701 | toPacketlist() {
|
30702 | const packetlist = new PacketList();
|
30703 | packetlist.push(this.keyPacket);
|
30704 | packetlist.concat(this.revocationSignatures);
|
30705 | packetlist.concat(this.directSignatures);
|
30706 | this.users.map(user => packetlist.concat(user.toPacketlist()));
|
30707 | this.subKeys.map(subKey => packetlist.concat(subKey.toPacketlist()));
|
30708 | return packetlist;
|
30709 | }
|
30710 |
|
30711 | |
30712 |
|
30713 |
|
30714 |
|
30715 |
|
30716 | async clone() {
|
30717 | return new Key(this.toPacketlist());
|
30718 | }
|
30719 |
|
30720 | |
30721 |
|
30722 |
|
30723 |
|
30724 |
|
30725 |
|
30726 | getSubkeys(keyId = null) {
|
30727 | const subKeys = [];
|
30728 | this.subKeys.forEach(subKey => {
|
30729 | if (!keyId || subKey.getKeyId().equals(keyId, true)) {
|
30730 | subKeys.push(subKey);
|
30731 | }
|
30732 | });
|
30733 | return subKeys;
|
30734 | }
|
30735 |
|
30736 | |
30737 |
|
30738 |
|
30739 |
|
30740 |
|
30741 |
|
30742 | getKeys(keyId = null) {
|
30743 | const keys = [];
|
30744 | if (!keyId || this.getKeyId().equals(keyId, true)) {
|
30745 | keys.push(this);
|
30746 | }
|
30747 | return keys.concat(this.getSubkeys(keyId));
|
30748 | }
|
30749 |
|
30750 | |
30751 |
|
30752 |
|
30753 |
|
30754 | getKeyIds() {
|
30755 | return this.getKeys().map(key => key.getKeyId());
|
30756 | }
|
30757 |
|
30758 | |
30759 |
|
30760 |
|
30761 |
|
30762 | getUserIds() {
|
30763 | return this.users.map(user => {
|
30764 | return user.userId ? user.userId.userid : null;
|
30765 | }).filter(userid => userid !== null);
|
30766 | }
|
30767 |
|
30768 | |
30769 |
|
30770 |
|
30771 |
|
30772 | isPublic() {
|
30773 | return this.keyPacket.tag === enums.packet.publicKey;
|
30774 | }
|
30775 |
|
30776 | |
30777 |
|
30778 |
|
30779 |
|
30780 | isPrivate() {
|
30781 | return this.keyPacket.tag === enums.packet.secretKey;
|
30782 | }
|
30783 |
|
30784 | |
30785 |
|
30786 |
|
30787 |
|
30788 |
|
30789 | toPublic() {
|
30790 | const packetlist = new PacketList();
|
30791 | const keyPackets = this.toPacketlist();
|
30792 | let bytes;
|
30793 | let pubKeyPacket;
|
30794 | let pubSubkeyPacket;
|
30795 | for (let i = 0; i < keyPackets.length; i++) {
|
30796 | switch (keyPackets[i].tag) {
|
30797 | case enums.packet.secretKey:
|
30798 | bytes = keyPackets[i].writePublicKey();
|
30799 | pubKeyPacket = new PublicKeyPacket();
|
30800 | pubKeyPacket.read(bytes);
|
30801 | packetlist.push(pubKeyPacket);
|
30802 | break;
|
30803 | case enums.packet.secretSubkey:
|
30804 | bytes = keyPackets[i].writePublicKey();
|
30805 | pubSubkeyPacket = new PublicSubkeyPacket();
|
30806 | pubSubkeyPacket.read(bytes);
|
30807 | packetlist.push(pubSubkeyPacket);
|
30808 | break;
|
30809 | default:
|
30810 | packetlist.push(keyPackets[i]);
|
30811 | }
|
30812 | }
|
30813 | return new Key(packetlist);
|
30814 | }
|
30815 |
|
30816 | |
30817 |
|
30818 |
|
30819 |
|
30820 |
|
30821 | armor(config = defaultConfig) {
|
30822 | const type = this.isPublic() ? enums.armor.publicKey : enums.armor.privateKey;
|
30823 | return armor(type, this.toPacketlist().write(), undefined, undefined, undefined, config);
|
30824 | }
|
30825 |
|
30826 | |
30827 |
|
30828 |
|
30829 |
|
30830 |
|
30831 |
|
30832 |
|
30833 |
|
30834 |
|
30835 | async getSigningKey(keyId = null, date = new Date(), userId = {}, config = defaultConfig) {
|
30836 | await this.verifyPrimaryKey(date, userId, config);
|
30837 | const primaryKey = this.keyPacket;
|
30838 | const subKeys = this.subKeys.slice().sort((a, b) => b.keyPacket.created - a.keyPacket.created);
|
30839 | let exception;
|
30840 | for (let i = 0; i < subKeys.length; i++) {
|
30841 | if (!keyId || subKeys[i].getKeyId().equals(keyId)) {
|
30842 | try {
|
30843 | await subKeys[i].verify(primaryKey, date, config);
|
30844 | const dataToVerify = { key: primaryKey, bind: subKeys[i].keyPacket };
|
30845 | const bindingSignature = await getLatestValidSignature(subKeys[i].bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
30846 | if (
|
30847 | bindingSignature &&
|
30848 | bindingSignature.embeddedSignature &&
|
30849 | isValidSigningKeyPacket(subKeys[i].keyPacket, bindingSignature) &&
|
30850 | await getLatestValidSignature([bindingSignature.embeddedSignature], subKeys[i].keyPacket, enums.signature.keyBinding, dataToVerify, date, config)
|
30851 | ) {
|
30852 | return subKeys[i];
|
30853 | }
|
30854 | } catch (e) {
|
30855 | exception = e;
|
30856 | }
|
30857 | }
|
30858 | }
|
30859 | const primaryUser = await this.getPrimaryUser(date, userId, config);
|
30860 | if ((!keyId || primaryKey.getKeyId().equals(keyId)) &&
|
30861 | isValidSigningKeyPacket(primaryKey, primaryUser.selfCertification)) {
|
30862 | return this;
|
30863 | }
|
30864 | throw util.wrapError('Could not find valid signing key packet in key ' + this.getKeyId().toHex(), exception);
|
30865 | }
|
30866 |
|
30867 | |
30868 |
|
30869 |
|
30870 |
|
30871 |
|
30872 |
|
30873 |
|
30874 |
|
30875 |
|
30876 | async getEncryptionKey(keyId, date = new Date(), userId = {}, config = defaultConfig) {
|
30877 | await this.verifyPrimaryKey(date, userId, config);
|
30878 | const primaryKey = this.keyPacket;
|
30879 |
|
30880 | const subKeys = this.subKeys.slice().sort((a, b) => b.keyPacket.created - a.keyPacket.created);
|
30881 | let exception;
|
30882 | for (let i = 0; i < subKeys.length; i++) {
|
30883 | if (!keyId || subKeys[i].getKeyId().equals(keyId)) {
|
30884 | try {
|
30885 | await subKeys[i].verify(primaryKey, date, config);
|
30886 | const dataToVerify = { key: primaryKey, bind: subKeys[i].keyPacket };
|
30887 | const bindingSignature = await getLatestValidSignature(subKeys[i].bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
30888 | if (bindingSignature && isValidEncryptionKeyPacket(subKeys[i].keyPacket, bindingSignature)) {
|
30889 | return subKeys[i];
|
30890 | }
|
30891 | } catch (e) {
|
30892 | exception = e;
|
30893 | }
|
30894 | }
|
30895 | }
|
30896 |
|
30897 | const primaryUser = await this.getPrimaryUser(date, userId, config);
|
30898 | if ((!keyId || primaryKey.getKeyId().equals(keyId)) &&
|
30899 | isValidEncryptionKeyPacket(primaryKey, primaryUser.selfCertification)) {
|
30900 | return this;
|
30901 | }
|
30902 | throw util.wrapError('Could not find valid encryption key packet in key ' + this.getKeyId().toHex(), exception);
|
30903 | }
|
30904 |
|
30905 | |
30906 |
|
30907 |
|
30908 |
|
30909 |
|
30910 |
|
30911 |
|
30912 |
|
30913 |
|
30914 |
|
30915 | async getDecryptionKeys(keyId, date = new Date(), userId = {}, config = defaultConfig) {
|
30916 | const primaryKey = this.keyPacket;
|
30917 | const keys = [];
|
30918 | for (let i = 0; i < this.subKeys.length; i++) {
|
30919 | if (!keyId || this.subKeys[i].getKeyId().equals(keyId, true)) {
|
30920 | try {
|
30921 | const dataToVerify = { key: primaryKey, bind: this.subKeys[i].keyPacket };
|
30922 | const bindingSignature = await getLatestValidSignature(this.subKeys[i].bindingSignatures, primaryKey, enums.signature.subkeyBinding, dataToVerify, date, config);
|
30923 | if (bindingSignature && isValidDecryptionKeyPacket(bindingSignature, config)) {
|
30924 | keys.push(this.subKeys[i]);
|
30925 | }
|
30926 | } catch (e) {}
|
30927 | }
|
30928 | }
|
30929 |
|
30930 |
|
30931 | const primaryUser = await this.getPrimaryUser(date, userId, config);
|
30932 | if ((!keyId || primaryKey.getKeyId().equals(keyId, true)) &&
|
30933 | isValidDecryptionKeyPacket(primaryUser.selfCertification, config)) {
|
30934 | keys.push(this);
|
30935 | }
|
30936 |
|
30937 | return keys;
|
30938 | }
|
30939 |
|
30940 | |
30941 |
|
30942 |
|
30943 |
|
30944 |
|
30945 |
|
30946 |
|
30947 |
|
30948 | async encrypt(passphrases, keyId = null, config = defaultConfig) {
|
30949 | if (!this.isPrivate()) {
|
30950 | throw new Error("Nothing to encrypt in a public key");
|
30951 | }
|
30952 |
|
30953 | const keys = this.getKeys(keyId);
|
30954 | passphrases = util.isArray(passphrases) ? passphrases : new Array(keys.length).fill(passphrases);
|
30955 | if (passphrases.length !== keys.length) {
|
30956 | throw new Error("Invalid number of passphrases for key");
|
30957 | }
|
30958 |
|
30959 | await Promise.all(keys.map(async function(key, i) {
|
30960 | const { keyPacket } = key;
|
30961 | await keyPacket.encrypt(passphrases[i], config);
|
30962 | keyPacket.clearPrivateParams();
|
30963 | }));
|
30964 | }
|
30965 |
|
30966 | |
30967 |
|
30968 |
|
30969 |
|
30970 |
|
30971 |
|
30972 |
|
30973 |
|
30974 | async decrypt(passphrases, keyId = null, config = defaultConfig) {
|
30975 | if (!this.isPrivate()) {
|
30976 | throw new Error("Nothing to decrypt in a public key");
|
30977 | }
|
30978 | passphrases = util.isArray(passphrases) ? passphrases : [passphrases];
|
30979 |
|
30980 | await Promise.all(this.getKeys(keyId).map(async function(key) {
|
30981 | let decrypted = false;
|
30982 | let error = null;
|
30983 | await Promise.all(passphrases.map(async function(passphrase) {
|
30984 | try {
|
30985 | await key.keyPacket.decrypt(passphrase);
|
30986 |
|
30987 | if (keyId) await key.keyPacket.validate();
|
30988 | decrypted = true;
|
30989 | } catch (e) {
|
30990 | error = e;
|
30991 | }
|
30992 | }));
|
30993 | if (!decrypted) {
|
30994 | throw error;
|
30995 | }
|
30996 | }));
|
30997 |
|
30998 | if (!keyId) {
|
30999 |
|
31000 | await this.validate(config);
|
31001 | }
|
31002 | }
|
31003 |
|
31004 | |
31005 |
|
31006 |
|
31007 |
|
31008 | isDecrypted() {
|
31009 | return this.getKeys().some(({ keyPacket }) => keyPacket.isDecrypted());
|
31010 | }
|
31011 |
|
31012 | |
31013 |
|
31014 |
|
31015 |
|
31016 |
|
31017 |
|
31018 |
|
31019 |
|
31020 |
|
31021 |
|
31022 | async validate(config = defaultConfig) {
|
31023 | if (!this.isPrivate()) {
|
31024 | throw new Error("Cannot validate a public key");
|
31025 | }
|
31026 |
|
31027 | let signingKeyPacket;
|
31028 | if (!this.primaryKey.isDummy()) {
|
31029 | signingKeyPacket = this.primaryKey;
|
31030 | } else {
|
31031 | |
31032 |
|
31033 |
|
31034 |
|
31035 | const signingKey = await this.getSigningKey(null, null, undefined, config);
|
31036 |
|
31037 | if (signingKey && !signingKey.keyPacket.isDummy()) {
|
31038 | signingKeyPacket = signingKey.keyPacket;
|
31039 | }
|
31040 | }
|
31041 |
|
31042 | if (signingKeyPacket) {
|
31043 | return signingKeyPacket.validate();
|
31044 | } else {
|
31045 | const keys = this.getKeys();
|
31046 | const allDummies = keys.map(key => key.keyPacket.isDummy()).every(Boolean);
|
31047 | if (allDummies) {
|
31048 | throw new Error("Cannot validate an all-gnu-dummy key");
|
31049 | }
|
31050 |
|
31051 | return Promise.all(keys.map(async key => key.keyPacket.validate()));
|
31052 | }
|
31053 | }
|
31054 |
|
31055 | |
31056 |
|
31057 |
|
31058 | clearPrivateParams() {
|
31059 | if (!this.isPrivate()) {
|
31060 | throw new Error("Can't clear private parameters of a public key");
|
31061 | }
|
31062 | this.getKeys().forEach(({ keyPacket }) => {
|
31063 | if (keyPacket.isDecrypted()) {
|
31064 | keyPacket.clearPrivateParams();
|
31065 | }
|
31066 | });
|
31067 | }
|
31068 |
|
31069 | |
31070 |
|
31071 |
|
31072 |
|
31073 |
|
31074 |
|
31075 |
|
31076 |
|
31077 |
|
31078 |
|
31079 |
|
31080 |
|
31081 | async isRevoked(signature, key, date = new Date(), config = defaultConfig) {
|
31082 | return isDataRevoked(
|
31083 | this.keyPacket, enums.signature.keyRevocation, { key: this.keyPacket }, this.revocationSignatures, signature, key, date, config
|
31084 | );
|
31085 | }
|
31086 |
|
31087 | |
31088 |
|
31089 |
|
31090 |
|
31091 |
|
31092 |
|
31093 |
|
31094 |
|
31095 |
|
31096 | async verifyPrimaryKey(date = new Date(), userId = {}, config = defaultConfig) {
|
31097 | const primaryKey = this.keyPacket;
|
31098 |
|
31099 | if (await this.isRevoked(null, null, date, config)) {
|
31100 | throw new Error('Primary key is revoked');
|
31101 | }
|
31102 |
|
31103 | const { selfCertification } = await this.getPrimaryUser(date, userId, config);
|
31104 |
|
31105 | if (isDataExpired(primaryKey, selfCertification, date)) {
|
31106 | throw new Error('Primary key is expired');
|
31107 | }
|
31108 | }
|
31109 |
|
31110 | |
31111 |
|
31112 |
|
31113 |
|
31114 |
|
31115 |
|
31116 |
|
31117 |
|
31118 |
|
31119 |
|
31120 |
|
31121 |
|
31122 | async getExpirationTime(capabilities, keyId, userId, config = defaultConfig) {
|
31123 | const primaryUser = await this.getPrimaryUser(null, userId, config);
|
31124 | const selfCert = primaryUser.selfCertification;
|
31125 | const keyExpiry = getExpirationTime(this.keyPacket, selfCert);
|
31126 | const sigExpiry = selfCert.getExpirationTime();
|
31127 | let expiry = keyExpiry < sigExpiry ? keyExpiry : sigExpiry;
|
31128 | if (capabilities === 'encrypt' || capabilities === 'encrypt_sign') {
|
31129 | const encryptKey =
|
31130 | await this.getEncryptionKey(keyId, expiry, userId, config).catch(() => {}) ||
|
31131 | await this.getEncryptionKey(keyId, null, userId, config).catch(() => {});
|
31132 | if (!encryptKey) return null;
|
31133 | const encryptExpiry = await encryptKey.getExpirationTime(this.keyPacket, undefined, config);
|
31134 | if (encryptExpiry < expiry) expiry = encryptExpiry;
|
31135 | }
|
31136 | if (capabilities === 'sign' || capabilities === 'encrypt_sign') {
|
31137 | const signKey =
|
31138 | await this.getSigningKey(keyId, expiry, userId, config).catch(() => {}) ||
|
31139 | await this.getSigningKey(keyId, null, userId, config).catch(() => {});
|
31140 | if (!signKey) return null;
|
31141 | const signExpiry = await signKey.getExpirationTime(this.keyPacket, undefined, config);
|
31142 | if (signExpiry < expiry) expiry = signExpiry;
|
31143 | }
|
31144 | return expiry;
|
31145 | }
|
31146 |
|
31147 | |
31148 |
|
31149 |
|
31150 |
|
31151 |
|
31152 |
|
31153 |
|
31154 |
|
31155 |
|
31156 |
|
31157 |
|
31158 | async getPrimaryUser(date = new Date(), userId = {}, config = defaultConfig) {
|
31159 | const primaryKey = this.keyPacket;
|
31160 | const users = [];
|
31161 | let exception;
|
31162 | for (let i = 0; i < this.users.length; i++) {
|
31163 | try {
|
31164 | const user = this.users[i];
|
31165 | if (!user.userId) {
|
31166 | continue;
|
31167 | }
|
31168 | if (
|
31169 | (userId.name !== undefined && user.userId.name !== userId.name) ||
|
31170 | (userId.email !== undefined && user.userId.email !== userId.email) ||
|
31171 | (userId.comment !== undefined && user.userId.comment !== userId.comment)
|
31172 | ) {
|
31173 | throw new Error('Could not find user that matches that user ID');
|
31174 | }
|
31175 | const dataToVerify = { userId: user.userId, key: primaryKey };
|
31176 | const selfCertification = await getLatestValidSignature(user.selfCertifications, primaryKey, enums.signature.certGeneric, dataToVerify, date, config);
|
31177 | users.push({ index: i, user, selfCertification });
|
31178 | } catch (e) {
|
31179 | exception = e;
|
31180 | }
|
31181 | }
|
31182 | if (!users.length) {
|
31183 | throw exception || new Error('Could not find primary user');
|
31184 | }
|
31185 | await Promise.all(users.map(async function (a) {
|
31186 | return a.user.revoked || a.user.isRevoked(primaryKey, a.selfCertification, null, date, config);
|
31187 | }));
|
31188 |
|
31189 | const primaryUser = users.sort(function(a, b) {
|
31190 | const A = a.selfCertification;
|
31191 | const B = b.selfCertification;
|
31192 | return B.revoked - A.revoked || A.isPrimaryUserID - B.isPrimaryUserID || A.created - B.created;
|
31193 | }).pop();
|
31194 | const { user, selfCertification: cert } = primaryUser;
|
31195 | if (cert.revoked || await user.isRevoked(primaryKey, cert, null, date, config)) {
|
31196 | throw new Error('Primary user is revoked');
|
31197 | }
|
31198 | return primaryUser;
|
31199 | }
|
31200 |
|
31201 | |
31202 |
|
31203 |
|
31204 |
|
31205 |
|
31206 |
|
31207 |
|
31208 |
|
31209 |
|
31210 |
|
31211 |
|
31212 |
|
31213 | async update(key, config = defaultConfig) {
|
31214 | if (!this.hasSameFingerprintAs(key)) {
|
31215 | throw new Error('Key update method: fingerprints of keys not equal');
|
31216 | }
|
31217 | if (this.isPublic() && key.isPrivate()) {
|
31218 |
|
31219 | const equal = (this.subKeys.length === key.subKeys.length) &&
|
31220 | (this.subKeys.every(destSubKey => {
|
31221 | return key.subKeys.some(srcSubKey => {
|
31222 | return destSubKey.hasSameFingerprintAs(srcSubKey);
|
31223 | });
|
31224 | }));
|
31225 | if (!equal) {
|
31226 | throw new Error('Cannot update public key with private key if subkey mismatch');
|
31227 | }
|
31228 | this.keyPacket = key.keyPacket;
|
31229 | }
|
31230 |
|
31231 | await mergeSignatures(key, this, 'revocationSignatures', srcRevSig => {
|
31232 | return isDataRevoked(this.keyPacket, enums.signature.keyRevocation, this, [srcRevSig], null, key.keyPacket, undefined, config);
|
31233 | });
|
31234 |
|
31235 | await mergeSignatures(key, this, 'directSignatures');
|
31236 |
|
31237 |
|
31238 | await Promise.all(key.users.map(async srcUser => {
|
31239 | let found = false;
|
31240 | await Promise.all(this.users.map(async dstUser => {
|
31241 | if ((srcUser.userId && dstUser.userId &&
|
31242 | (srcUser.userId.userid === dstUser.userId.userid)) ||
|
31243 | (srcUser.userAttribute && (srcUser.userAttribute.equals(dstUser.userAttribute)))) {
|
31244 | await dstUser.update(srcUser, this.keyPacket, config);
|
31245 | found = true;
|
31246 | }
|
31247 | }));
|
31248 | if (!found) {
|
31249 | this.users.push(srcUser);
|
31250 | }
|
31251 | }));
|
31252 |
|
31253 |
|
31254 | await Promise.all(key.subKeys.map(async srcSubKey => {
|
31255 | let found = false;
|
31256 | await Promise.all(this.subKeys.map(async dstSubKey => {
|
31257 | if (dstSubKey.hasSameFingerprintAs(srcSubKey)) {
|
31258 | await dstSubKey.update(srcSubKey, this.keyPacket, config);
|
31259 | found = true;
|
31260 | }
|
31261 | }));
|
31262 | if (!found) {
|
31263 | this.subKeys.push(srcSubKey);
|
31264 | }
|
31265 | }));
|
31266 | }
|
31267 |
|
31268 | |
31269 |
|
31270 |
|
31271 |
|
31272 |
|
31273 |
|
31274 |
|
31275 |
|
31276 |
|
31277 |
|
31278 | async revoke(
|
31279 | {
|
31280 | flag: reasonForRevocationFlag = enums.reasonForRevocation.noReason,
|
31281 | string: reasonForRevocationString = ''
|
31282 | } = {},
|
31283 | date = new Date(),
|
31284 | config = defaultConfig
|
31285 | ) {
|
31286 | if (this.isPublic()) {
|
31287 | throw new Error('Need private key for revoking');
|
31288 | }
|
31289 | const dataToSign = { key: this.keyPacket };
|
31290 | const key = await this.clone();
|
31291 | key.revocationSignatures.push(await createSignaturePacket(dataToSign, null, this.keyPacket, {
|
31292 | signatureType: enums.signature.keyRevocation,
|
31293 | reasonForRevocationFlag: enums.write(enums.reasonForRevocation, reasonForRevocationFlag),
|
31294 | reasonForRevocationString
|
31295 | }, date, undefined, undefined, undefined, config));
|
31296 | return key;
|
31297 | }
|
31298 |
|
31299 | |
31300 |
|
31301 |
|
31302 |
|
31303 |
|
31304 |
|
31305 |
|
31306 |
|
31307 | async getRevocationCertificate(date = new Date(), config = defaultConfig) {
|
31308 | const dataToVerify = { key: this.keyPacket };
|
31309 | const revocationSignature = await getLatestValidSignature(this.revocationSignatures, this.keyPacket, enums.signature.keyRevocation, dataToVerify, date, config);
|
31310 | const packetlist = new PacketList();
|
31311 | packetlist.push(revocationSignature);
|
31312 | return armor(enums.armor.publicKey, packetlist.write(), null, null, 'This is a revocation certificate');
|
31313 | }
|
31314 |
|
31315 | |
31316 |
|
31317 |
|
31318 |
|
31319 |
|
31320 |
|
31321 |
|
31322 |
|
31323 |
|
31324 | async applyRevocationCertificate(revocationCertificate, config = defaultConfig) {
|
31325 | const input = await unarmor(revocationCertificate, config);
|
31326 | const packetlist = new PacketList();
|
31327 | await packetlist.read(input.data, { SignaturePacket }, undefined, config);
|
31328 | const revocationSignature = packetlist.findPacket(enums.packet.signature);
|
31329 | if (!revocationSignature || revocationSignature.signatureType !== enums.signature.keyRevocation) {
|
31330 | throw new Error('Could not find revocation signature packet');
|
31331 | }
|
31332 | if (!revocationSignature.issuerKeyId.equals(this.getKeyId())) {
|
31333 | throw new Error('Revocation signature does not match key');
|
31334 | }
|
31335 | if (revocationSignature.isExpired()) {
|
31336 | throw new Error('Revocation signature is expired');
|
31337 | }
|
31338 | try {
|
31339 | await revocationSignature.verify(this.keyPacket, enums.signature.keyRevocation, { key: this.keyPacket }, undefined, undefined, config);
|
31340 | } catch (e) {
|
31341 | throw util.wrapError('Could not verify revocation signature', e);
|
31342 | }
|
31343 | const key = await this.clone();
|
31344 | key.revocationSignatures.push(revocationSignature);
|
31345 | return key;
|
31346 | }
|
31347 |
|
31348 | |
31349 |
|
31350 |
|
31351 |
|
31352 |
|
31353 |
|
31354 |
|
31355 |
|
31356 |
|
31357 | async signPrimaryUser(privateKeys, date, userId, config = defaultConfig) {
|
31358 | const { index, user } = await this.getPrimaryUser(date, userId, config);
|
31359 | const userSign = await user.sign(this.keyPacket, privateKeys, config);
|
31360 | const key = await this.clone();
|
31361 | key.users[index] = userSign;
|
31362 | return key;
|
31363 | }
|
31364 |
|
31365 | |
31366 |
|
31367 |
|
31368 |
|
31369 |
|
31370 |
|
31371 |
|
31372 | async signAllUsers(privateKeys, config = defaultConfig) {
|
31373 | const that = this;
|
31374 | const key = await this.clone();
|
31375 | key.users = await Promise.all(this.users.map(function(user) {
|
31376 | return user.sign(that.keyPacket, privateKeys, config);
|
31377 | }));
|
31378 | return key;
|
31379 | }
|
31380 |
|
31381 | |
31382 |
|
31383 |
|
31384 |
|
31385 |
|
31386 |
|
31387 |
|
31388 |
|
31389 |
|
31390 |
|
31391 |
|
31392 |
|
31393 | async verifyPrimaryUser(keys, date, userId, config = defaultConfig) {
|
31394 | const primaryKey = this.keyPacket;
|
31395 | const { user } = await this.getPrimaryUser(date, userId, config);
|
31396 | const results = keys ? await user.verifyAllCertifications(primaryKey, keys, undefined, config) :
|
31397 | [{ keyid: primaryKey.keyid, valid: await user.verify(primaryKey, undefined, config).catch(() => false) }];
|
31398 | return results;
|
31399 | }
|
31400 |
|
31401 | |
31402 |
|
31403 |
|
31404 |
|
31405 |
|
31406 |
|
31407 |
|
31408 |
|
31409 |
|
31410 |
|
31411 |
|
31412 | async verifyAllUsers(keys, config = defaultConfig) {
|
31413 | const results = [];
|
31414 | const primaryKey = this.keyPacket;
|
31415 | await Promise.all(this.users.map(async function(user) {
|
31416 | const signatures = keys ? await user.verifyAllCertifications(primaryKey, keys, undefined, config) :
|
31417 | [{ keyid: primaryKey.keyid, valid: await user.verify(primaryKey, undefined, config).catch(() => false) }];
|
31418 | signatures.forEach(signature => {
|
31419 | results.push({
|
31420 | userid: user.userId.userid,
|
31421 | keyid: signature.keyid,
|
31422 | valid: signature.valid
|
31423 | });
|
31424 | });
|
31425 | }));
|
31426 | return results;
|
31427 | }
|
31428 |
|
31429 | |
31430 |
|
31431 |
|
31432 |
|
31433 |
|
31434 |
|
31435 |
|
31436 |
|
31437 |
|
31438 |
|
31439 |
|
31440 |
|
31441 |
|
31442 | async addSubkey(options = {}) {
|
31443 | const config = { ...defaultConfig, ...options.config };
|
31444 | if (!this.isPrivate()) {
|
31445 | throw new Error("Cannot add a subkey to a public key");
|
31446 | }
|
31447 | if (options.passphrase) {
|
31448 | throw new Error("Subkey could not be encrypted here, please encrypt whole key");
|
31449 | }
|
31450 | if (options.rsaBits < config.minRsaBits) {
|
31451 | throw new Error(`rsaBits should be at least ${config.minRsaBits}, got: ${options.rsaBits}`);
|
31452 | }
|
31453 | const secretKeyPacket = this.primaryKey;
|
31454 | if (secretKeyPacket.isDummy()) {
|
31455 | throw new Error("Cannot add subkey to gnu-dummy primary key");
|
31456 | }
|
31457 | if (!secretKeyPacket.isDecrypted()) {
|
31458 | throw new Error("Key is not decrypted");
|
31459 | }
|
31460 | const defaultOptions = secretKeyPacket.getAlgorithmInfo();
|
31461 | defaultOptions.type = defaultOptions.curve ? 'ecc' : 'rsa';
|
31462 | defaultOptions.rsaBits = defaultOptions.bits || 4096;
|
31463 | defaultOptions.curve = defaultOptions.curve || 'curve25519';
|
31464 | options = sanitizeKeyOptions(options, defaultOptions);
|
31465 | const keyPacket = await generateSecretSubkey(options);
|
31466 | const bindingSignature = await createBindingSignature(keyPacket, secretKeyPacket, options, config);
|
31467 | const packetList = this.toPacketlist();
|
31468 | packetList.push(keyPacket);
|
31469 | packetList.push(bindingSignature);
|
31470 | return new Key(packetList);
|
31471 | }
|
31472 | }
|
31473 |
|
31474 | ['getKeyId', 'getFingerprint', 'getAlgorithmInfo', 'getCreationTime', 'hasSameFingerprintAs'].forEach(name => {
|
31475 | Key.prototype[name] =
|
31476 | SubKey.prototype[name];
|
31477 | });
|
31478 |
|
31479 |
|
31480 |
|
31481 |
|
31482 |
|
31483 |
|
31484 |
|
31485 |
|
31486 |
|
31487 |
|
31488 |
|
31489 |
|
31490 |
|
31491 |
|
31492 |
|
31493 |
|
31494 |
|
31495 |
|
31496 |
|
31497 |
|
31498 |
|
31499 | async function generate$2(options, config) {
|
31500 | options.sign = true;
|
31501 | options = sanitizeKeyOptions(options);
|
31502 | options.subkeys = options.subkeys.map((subkey, index) => sanitizeKeyOptions(options.subkeys[index], options));
|
31503 | let promises = [generateSecretKey(options, config)];
|
31504 | promises = promises.concat(options.subkeys.map(options => generateSecretSubkey(options, config)));
|
31505 | return Promise.all(promises).then(packets => wrapKeyObject(packets[0], packets.slice(1), options, config));
|
31506 | }
|
31507 |
|
31508 |
|
31509 |
|
31510 |
|
31511 |
|
31512 |
|
31513 |
|
31514 |
|
31515 |
|
31516 |
|
31517 |
|
31518 |
|
31519 |
|
31520 |
|
31521 |
|
31522 |
|
31523 | async function reformat(options, config) {
|
31524 | options = sanitize(options);
|
31525 |
|
31526 | if (options.privateKey.primaryKey.isDummy()) {
|
31527 | throw new Error('Cannot reformat a gnu-dummy primary key');
|
31528 | }
|
31529 |
|
31530 | const isDecrypted = options.privateKey.getKeys().every(({ keyPacket }) => keyPacket.isDecrypted());
|
31531 | if (!isDecrypted) {
|
31532 | throw new Error('Key is not decrypted');
|
31533 | }
|
31534 |
|
31535 | const packetlist = options.privateKey.toPacketlist();
|
31536 | let secretKeyPacket;
|
31537 | const secretSubkeyPackets = [];
|
31538 | for (let i = 0; i < packetlist.length; i++) {
|
31539 | if (packetlist[i].tag === enums.packet.secretKey) {
|
31540 | secretKeyPacket = packetlist[i];
|
31541 | } else if (packetlist[i].tag === enums.packet.secretSubkey) {
|
31542 | secretSubkeyPackets.push(packetlist[i]);
|
31543 | }
|
31544 | }
|
31545 | if (!secretKeyPacket) {
|
31546 | throw new Error('Key does not contain a secret key packet');
|
31547 | }
|
31548 |
|
31549 | if (!options.subkeys) {
|
31550 | options.subkeys = await Promise.all(secretSubkeyPackets.map(async secretSubkeyPacket => ({
|
31551 | sign: await options.privateKey.getSigningKey(secretSubkeyPacket.getKeyId(), null, undefined, config).catch(() => {}) &&
|
31552 | !await options.privateKey.getEncryptionKey(secretSubkeyPacket.getKeyId(), null, undefined, config).catch(() => {})
|
31553 | })));
|
31554 | }
|
31555 |
|
31556 | if (options.subkeys.length !== secretSubkeyPackets.length) {
|
31557 | throw new Error('Number of subkey options does not match number of subkeys');
|
31558 | }
|
31559 |
|
31560 | options.subkeys = options.subkeys.map(function(subkey, index) { return sanitize(options.subkeys[index], options); });
|
31561 |
|
31562 | return wrapKeyObject(secretKeyPacket, secretSubkeyPackets, options, config);
|
31563 |
|
31564 | function sanitize(options, subkeyDefaults = {}) {
|
31565 | options.keyExpirationTime = options.keyExpirationTime || subkeyDefaults.keyExpirationTime;
|
31566 | options.passphrase = util.isString(options.passphrase) ? options.passphrase : subkeyDefaults.passphrase;
|
31567 | options.date = options.date || subkeyDefaults.date;
|
31568 |
|
31569 | return options;
|
31570 | }
|
31571 | }
|
31572 |
|
31573 |
|
31574 | async function wrapKeyObject(secretKeyPacket, secretSubkeyPackets, options, config) {
|
31575 |
|
31576 | if (options.passphrase) {
|
31577 | await secretKeyPacket.encrypt(options.passphrase, config);
|
31578 | }
|
31579 |
|
31580 | await Promise.all(secretSubkeyPackets.map(async function(secretSubkeyPacket, index) {
|
31581 | const subkeyPassphrase = options.subkeys[index].passphrase;
|
31582 | if (subkeyPassphrase) {
|
31583 | await secretSubkeyPacket.encrypt(subkeyPassphrase, config);
|
31584 | }
|
31585 | }));
|
31586 |
|
31587 | const packetlist = new PacketList();
|
31588 |
|
31589 | packetlist.push(secretKeyPacket);
|
31590 |
|
31591 | await Promise.all(options.userIds.map(async function(userId, index) {
|
31592 | function createdPreferredAlgos(algos, configAlgo) {
|
31593 | if (configAlgo) {
|
31594 | const configIndex = algos.indexOf(configAlgo);
|
31595 | if (configIndex >= 1) {
|
31596 | algos.splice(configIndex, 1);
|
31597 | }
|
31598 | if (configIndex !== 0) {
|
31599 | algos.unshift(configAlgo);
|
31600 | }
|
31601 | }
|
31602 | return algos;
|
31603 | }
|
31604 |
|
31605 | const userIdPacket = UserIDPacket.fromObject(userId);
|
31606 | const dataToSign = {};
|
31607 | dataToSign.userId = userIdPacket;
|
31608 | dataToSign.key = secretKeyPacket;
|
31609 | const signaturePacket = new SignaturePacket(options.date);
|
31610 | signaturePacket.signatureType = enums.signature.certGeneric;
|
31611 | signaturePacket.publicKeyAlgorithm = secretKeyPacket.algorithm;
|
31612 | signaturePacket.hashAlgorithm = await getPreferredHashAlgo$1(null, secretKeyPacket, undefined, undefined, config);
|
31613 | signaturePacket.keyFlags = [enums.keyFlags.certifyKeys | enums.keyFlags.signData];
|
31614 | signaturePacket.preferredSymmetricAlgorithms = createdPreferredAlgos([
|
31615 |
|
31616 | enums.symmetric.aes256,
|
31617 | enums.symmetric.aes128,
|
31618 | enums.symmetric.aes192
|
31619 | ], config.encryptionCipher);
|
31620 | if (config.aeadProtect) {
|
31621 | signaturePacket.preferredAeadAlgorithms = createdPreferredAlgos([
|
31622 | enums.aead.eax,
|
31623 | enums.aead.ocb
|
31624 | ], config.aeadMode);
|
31625 | }
|
31626 | signaturePacket.preferredHashAlgorithms = createdPreferredAlgos([
|
31627 |
|
31628 | enums.hash.sha256,
|
31629 | enums.hash.sha512
|
31630 | ], config.preferHashAlgorithm);
|
31631 | signaturePacket.preferredCompressionAlgorithms = createdPreferredAlgos([
|
31632 | enums.compression.zlib,
|
31633 | enums.compression.zip,
|
31634 | enums.compression.uncompressed
|
31635 | ], config.compression);
|
31636 | if (index === 0) {
|
31637 | signaturePacket.isPrimaryUserID = true;
|
31638 | }
|
31639 |
|
31640 | signaturePacket.features = [0];
|
31641 | signaturePacket.features[0] |= enums.features.modificationDetection;
|
31642 | if (config.aeadProtect) {
|
31643 | signaturePacket.features[0] |= enums.features.aead;
|
31644 | }
|
31645 | if (config.v5Keys) {
|
31646 | signaturePacket.features[0] |= enums.features.v5Keys;
|
31647 | }
|
31648 | if (options.keyExpirationTime > 0) {
|
31649 | signaturePacket.keyExpirationTime = options.keyExpirationTime;
|
31650 | signaturePacket.keyNeverExpires = false;
|
31651 | }
|
31652 | await signaturePacket.sign(secretKeyPacket, dataToSign);
|
31653 |
|
31654 | return { userIdPacket, signaturePacket };
|
31655 | })).then(list => {
|
31656 | list.forEach(({ userIdPacket, signaturePacket }) => {
|
31657 | packetlist.push(userIdPacket);
|
31658 | packetlist.push(signaturePacket);
|
31659 | });
|
31660 | });
|
31661 |
|
31662 | await Promise.all(secretSubkeyPackets.map(async function(secretSubkeyPacket, index) {
|
31663 | const subkeyOptions = options.subkeys[index];
|
31664 | const subkeySignaturePacket = await createBindingSignature(secretSubkeyPacket, secretKeyPacket, subkeyOptions, config);
|
31665 | return { secretSubkeyPacket, subkeySignaturePacket };
|
31666 | })).then(packets => {
|
31667 | packets.forEach(({ secretSubkeyPacket, subkeySignaturePacket }) => {
|
31668 | packetlist.push(secretSubkeyPacket);
|
31669 | packetlist.push(subkeySignaturePacket);
|
31670 | });
|
31671 | });
|
31672 |
|
31673 |
|
31674 |
|
31675 | const dataToSign = { key: secretKeyPacket };
|
31676 | packetlist.push(await createSignaturePacket(dataToSign, null, secretKeyPacket, {
|
31677 | signatureType: enums.signature.keyRevocation,
|
31678 | reasonForRevocationFlag: enums.reasonForRevocation.noReason,
|
31679 | reasonForRevocationString: ''
|
31680 | }, options.date, undefined, undefined, undefined, config));
|
31681 |
|
31682 |
|
31683 | if (options.passphrase) {
|
31684 | secretKeyPacket.clearPrivateParams();
|
31685 | }
|
31686 |
|
31687 | await Promise.all(secretSubkeyPackets.map(async function(secretSubkeyPacket, index) {
|
31688 | const subkeyPassphrase = options.subkeys[index].passphrase;
|
31689 | if (subkeyPassphrase) {
|
31690 | secretSubkeyPacket.clearPrivateParams();
|
31691 | }
|
31692 | }));
|
31693 |
|
31694 | return new Key(packetlist);
|
31695 | }
|
31696 |
|
31697 |
|
31698 |
|
31699 |
|
31700 |
|
31701 |
|
31702 |
|
31703 |
|
31704 |
|
31705 |
|
31706 |
|
31707 | async function readKey({ armoredKey, binaryKey, config }) {
|
31708 | config = { ...defaultConfig, ...config };
|
31709 | if (!armoredKey && !binaryKey) {
|
31710 | throw new Error('readKey: must pass options object containing `armoredKey` or `binaryKey`');
|
31711 | }
|
31712 | let input;
|
31713 | if (armoredKey) {
|
31714 | const { type, data } = await unarmor(armoredKey, config);
|
31715 | if (!(type === enums.armor.publicKey || type === enums.armor.privateKey)) {
|
31716 | throw new Error('Armored text not of type key');
|
31717 | }
|
31718 | input = data;
|
31719 | } else {
|
31720 | input = binaryKey;
|
31721 | }
|
31722 | const packetlist = new PacketList();
|
31723 | await packetlist.read(input, allowedKeyPackets, undefined, config);
|
31724 | return new Key(packetlist);
|
31725 | }
|
31726 |
|
31727 |
|
31728 |
|
31729 |
|
31730 |
|
31731 |
|
31732 |
|
31733 |
|
31734 |
|
31735 |
|
31736 |
|
31737 | async function readKeys({ armoredKeys, binaryKeys, config }) {
|
31738 | config = { ...defaultConfig, ...config };
|
31739 | let input = armoredKeys || binaryKeys;
|
31740 | if (!input) {
|
31741 | throw new Error('readKeys: must pass options object containing `armoredKeys` or `binaryKeys`');
|
31742 | }
|
31743 | if (armoredKeys) {
|
31744 | const { type, data } = await unarmor(armoredKeys, config);
|
31745 | if (type !== enums.armor.publicKey && type !== enums.armor.privateKey) {
|
31746 | throw new Error('Armored text not of type key');
|
31747 | }
|
31748 | input = data;
|
31749 | }
|
31750 | const keys = [];
|
31751 | const packetlist = new PacketList();
|
31752 | await packetlist.read(input, allowedKeyPackets, undefined, config);
|
31753 | const keyIndex = packetlist.indexOfTag(enums.packet.publicKey, enums.packet.secretKey);
|
31754 | if (keyIndex.length === 0) {
|
31755 | throw new Error('No key packet found');
|
31756 | }
|
31757 | for (let i = 0; i < keyIndex.length; i++) {
|
31758 | const oneKeyList = packetlist.slice(keyIndex[i], keyIndex[i + 1]);
|
31759 | const newKey = new Key(oneKeyList);
|
31760 | keys.push(newKey);
|
31761 | }
|
31762 | return keys;
|
31763 | }
|
31764 |
|
31765 |
|
31766 |
|
31767 |
|
31768 |
|
31769 |
|
31770 |
|
31771 |
|
31772 |
|
31773 | class Message {
|
31774 | |
31775 |
|
31776 |
|
31777 | constructor(packetlist) {
|
31778 | this.packets = packetlist || new PacketList();
|
31779 | }
|
31780 |
|
31781 | |
31782 |
|
31783 |
|
31784 |
|
31785 | getEncryptionKeyIds() {
|
31786 | const keyIds = [];
|
31787 | const pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey);
|
31788 | pkESKeyPacketlist.forEach(function(packet) {
|
31789 | keyIds.push(packet.publicKeyId);
|
31790 | });
|
31791 | return keyIds;
|
31792 | }
|
31793 |
|
31794 | |
31795 |
|
31796 |
|
31797 |
|
31798 | getSigningKeyIds() {
|
31799 | const keyIds = [];
|
31800 | const msg = this.unwrapCompressed();
|
31801 |
|
31802 | const onePassSigList = msg.packets.filterByTag(enums.packet.onePassSignature);
|
31803 | onePassSigList.forEach(function(packet) {
|
31804 | keyIds.push(packet.issuerKeyId);
|
31805 | });
|
31806 |
|
31807 | if (!keyIds.length) {
|
31808 | const signatureList = msg.packets.filterByTag(enums.packet.signature);
|
31809 | signatureList.forEach(function(packet) {
|
31810 | keyIds.push(packet.issuerKeyId);
|
31811 | });
|
31812 | }
|
31813 | return keyIds;
|
31814 | }
|
31815 |
|
31816 | |
31817 |
|
31818 |
|
31819 |
|
31820 |
|
31821 |
|
31822 |
|
31823 |
|
31824 |
|
31825 |
|
31826 | async decrypt(privateKeys, passwords, sessionKeys, streaming, config = defaultConfig) {
|
31827 | const keyObjs = sessionKeys || await this.decryptSessionKeys(privateKeys, passwords, config);
|
31828 |
|
31829 | const symEncryptedPacketlist = this.packets.filterByTag(
|
31830 | enums.packet.symmetricallyEncryptedData,
|
31831 | enums.packet.symEncryptedIntegrityProtectedData,
|
31832 | enums.packet.AEADEncryptedData
|
31833 | );
|
31834 |
|
31835 | if (symEncryptedPacketlist.length === 0) {
|
31836 | return this;
|
31837 | }
|
31838 |
|
31839 | const symEncryptedPacket = symEncryptedPacketlist[0];
|
31840 | let exception = null;
|
31841 | const decryptedPromise = Promise.all(keyObjs.map(async keyObj => {
|
31842 | if (!keyObj || !util.isUint8Array(keyObj.data) || !util.isString(keyObj.algorithm)) {
|
31843 | throw new Error('Invalid session key for decryption.');
|
31844 | }
|
31845 |
|
31846 | try {
|
31847 | await symEncryptedPacket.decrypt(keyObj.algorithm, keyObj.data, streaming, config);
|
31848 | } catch (e) {
|
31849 | util.printDebugError(e);
|
31850 | exception = e;
|
31851 | }
|
31852 | }));
|
31853 |
|
31854 | stream.cancel(symEncryptedPacket.encrypted);
|
31855 | symEncryptedPacket.encrypted = null;
|
31856 | await decryptedPromise;
|
31857 |
|
31858 | if (!symEncryptedPacket.packets || !symEncryptedPacket.packets.length) {
|
31859 | throw exception || new Error('Decryption failed.');
|
31860 | }
|
31861 |
|
31862 | const resultMsg = new Message(symEncryptedPacket.packets);
|
31863 | symEncryptedPacket.packets = new PacketList();
|
31864 |
|
31865 | return resultMsg;
|
31866 | }
|
31867 |
|
31868 | |
31869 |
|
31870 |
|
31871 |
|
31872 |
|
31873 |
|
31874 |
|
31875 |
|
31876 |
|
31877 | async decryptSessionKeys(privateKeys, passwords, config = defaultConfig) {
|
31878 | let keyPackets = [];
|
31879 |
|
31880 | let exception;
|
31881 | if (passwords) {
|
31882 | const symESKeyPacketlist = this.packets.filterByTag(enums.packet.symEncryptedSessionKey);
|
31883 | if (!symESKeyPacketlist) {
|
31884 | throw new Error('No symmetrically encrypted session key packet found.');
|
31885 | }
|
31886 | await Promise.all(passwords.map(async function(password, i) {
|
31887 | let packets;
|
31888 | if (i) {
|
31889 | packets = new PacketList();
|
31890 | await packets.read(symESKeyPacketlist.write(), { SymEncryptedSessionKeyPacket });
|
31891 | } else {
|
31892 | packets = symESKeyPacketlist;
|
31893 | }
|
31894 | await Promise.all(packets.map(async function(keyPacket) {
|
31895 | try {
|
31896 | await keyPacket.decrypt(password);
|
31897 | keyPackets.push(keyPacket);
|
31898 | } catch (err) {
|
31899 | util.printDebugError(err);
|
31900 | }
|
31901 | }));
|
31902 | }));
|
31903 | } else if (privateKeys) {
|
31904 | const pkESKeyPacketlist = this.packets.filterByTag(enums.packet.publicKeyEncryptedSessionKey);
|
31905 | if (!pkESKeyPacketlist) {
|
31906 | throw new Error('No public key encrypted session key packet found.');
|
31907 | }
|
31908 | await Promise.all(pkESKeyPacketlist.map(async function(keyPacket) {
|
31909 | await Promise.all(privateKeys.map(async function(privateKey) {
|
31910 | let algos = [
|
31911 | enums.symmetric.aes256,
|
31912 | enums.symmetric.aes128,
|
31913 | enums.symmetric.tripledes,
|
31914 | enums.symmetric.cast5
|
31915 | ];
|
31916 | try {
|
31917 | const primaryUser = await privateKey.getPrimaryUser(undefined, undefined, config);
|
31918 | if (primaryUser.selfCertification.preferredSymmetricAlgorithms) {
|
31919 | algos = algos.concat(primaryUser.selfCertification.preferredSymmetricAlgorithms);
|
31920 | }
|
31921 | } catch (e) {}
|
31922 |
|
31923 |
|
31924 | const privateKeyPackets = (await privateKey.getDecryptionKeys(keyPacket.publicKeyId, null, undefined, config)).map(key => key.keyPacket);
|
31925 | await Promise.all(privateKeyPackets.map(async function(privateKeyPacket) {
|
31926 | if (!privateKeyPacket || privateKeyPacket.isDummy()) {
|
31927 | return;
|
31928 | }
|
31929 | if (!privateKeyPacket.isDecrypted()) {
|
31930 | throw new Error('Private key is not decrypted.');
|
31931 | }
|
31932 | try {
|
31933 | await keyPacket.decrypt(privateKeyPacket);
|
31934 | if (!algos.includes(enums.write(enums.symmetric, keyPacket.sessionKeyAlgorithm))) {
|
31935 | throw new Error('A non-preferred symmetric algorithm was used.');
|
31936 | }
|
31937 | keyPackets.push(keyPacket);
|
31938 | } catch (err) {
|
31939 | util.printDebugError(err);
|
31940 | exception = err;
|
31941 | }
|
31942 | }));
|
31943 | }));
|
31944 | stream.cancel(keyPacket.encrypted);
|
31945 | keyPacket.encrypted = null;
|
31946 | }));
|
31947 | } else {
|
31948 | throw new Error('No key or password specified.');
|
31949 | }
|
31950 |
|
31951 | if (keyPackets.length) {
|
31952 |
|
31953 | if (keyPackets.length > 1) {
|
31954 | const seen = {};
|
31955 | keyPackets = keyPackets.filter(function(item) {
|
31956 | const k = item.sessionKeyAlgorithm + util.uint8ArrayToStr(item.sessionKey);
|
31957 | if (seen.hasOwnProperty(k)) {
|
31958 | return false;
|
31959 | }
|
31960 | seen[k] = true;
|
31961 | return true;
|
31962 | });
|
31963 | }
|
31964 |
|
31965 | return keyPackets.map(packet => ({ data: packet.sessionKey, algorithm: packet.sessionKeyAlgorithm }));
|
31966 | }
|
31967 | throw exception || new Error('Session key decryption failed.');
|
31968 | }
|
31969 |
|
31970 | |
31971 |
|
31972 |
|
31973 |
|
31974 | getLiteralData() {
|
31975 | const msg = this.unwrapCompressed();
|
31976 | const literal = msg.packets.findPacket(enums.packet.literalData);
|
31977 | return (literal && literal.getBytes()) || null;
|
31978 | }
|
31979 |
|
31980 | |
31981 |
|
31982 |
|
31983 |
|
31984 | getFilename() {
|
31985 | const msg = this.unwrapCompressed();
|
31986 | const literal = msg.packets.findPacket(enums.packet.literalData);
|
31987 | return (literal && literal.getFilename()) || null;
|
31988 | }
|
31989 |
|
31990 | |
31991 |
|
31992 |
|
31993 |
|
31994 | getText() {
|
31995 | const msg = this.unwrapCompressed();
|
31996 | const literal = msg.packets.findPacket(enums.packet.literalData);
|
31997 | if (literal) {
|
31998 | return literal.getText();
|
31999 | }
|
32000 | return null;
|
32001 | }
|
32002 |
|
32003 | |
32004 |
|
32005 |
|
32006 |
|
32007 |
|
32008 |
|
32009 |
|
32010 |
|
32011 |
|
32012 | static async generateSessionKey(keys = [], date = new Date(), userIds = [], config = defaultConfig) {
|
32013 | const algorithm = enums.read(enums.symmetric, await getPreferredAlgo('symmetric', keys, date, userIds, config));
|
32014 | const aeadAlgorithm = config.aeadProtect && await isAeadSupported(keys, date, userIds, config) ?
|
32015 | enums.read(enums.aead, await getPreferredAlgo('aead', keys, date, userIds, config)) :
|
32016 | undefined;
|
32017 | const sessionKeyData = await mod.generateSessionKey(algorithm);
|
32018 | return { data: sessionKeyData, algorithm, aeadAlgorithm };
|
32019 | }
|
32020 |
|
32021 | |
32022 |
|
32023 |
|
32024 |
|
32025 |
|
32026 |
|
32027 |
|
32028 |
|
32029 |
|
32030 |
|
32031 |
|
32032 |
|
32033 |
|
32034 |
|
32035 | async encrypt(keys, passwords, sessionKey, wildcard = false, encryptionKeyIds = [], date = new Date(), userIds = [], streaming, config = defaultConfig) {
|
32036 | if (sessionKey) {
|
32037 | if (!util.isUint8Array(sessionKey.data) || !util.isString(sessionKey.algorithm)) {
|
32038 | throw new Error('Invalid session key for encryption.');
|
32039 | }
|
32040 | } else if (keys && keys.length) {
|
32041 | sessionKey = await Message.generateSessionKey(keys, date, userIds, config);
|
32042 | } else if (passwords && passwords.length) {
|
32043 | sessionKey = await Message.generateSessionKey(undefined, undefined, undefined, config);
|
32044 | } else {
|
32045 | throw new Error('No keys, passwords, or session key provided.');
|
32046 | }
|
32047 |
|
32048 | const { data: sessionKeyData, algorithm, aeadAlgorithm } = sessionKey;
|
32049 |
|
32050 | const msg = await Message.encryptSessionKey(sessionKeyData, algorithm, aeadAlgorithm, keys, passwords, wildcard, encryptionKeyIds, date, userIds, config);
|
32051 |
|
32052 | let symEncryptedPacket;
|
32053 | if (aeadAlgorithm) {
|
32054 | symEncryptedPacket = new AEADEncryptedDataPacket();
|
32055 | symEncryptedPacket.aeadAlgorithm = aeadAlgorithm;
|
32056 | } else {
|
32057 | symEncryptedPacket = new SymEncryptedIntegrityProtectedDataPacket();
|
32058 | }
|
32059 | symEncryptedPacket.packets = this.packets;
|
32060 |
|
32061 | await symEncryptedPacket.encrypt(algorithm, sessionKeyData, streaming, config);
|
32062 |
|
32063 | msg.packets.push(symEncryptedPacket);
|
32064 | symEncryptedPacket.packets = new PacketList();
|
32065 | return msg;
|
32066 | }
|
32067 |
|
32068 | |
32069 |
|
32070 |
|
32071 |
|
32072 |
|
32073 |
|
32074 |
|
32075 |
|
32076 |
|
32077 |
|
32078 |
|
32079 |
|
32080 |
|
32081 |
|
32082 |
|
32083 | static async encryptSessionKey(sessionKey, algorithm, aeadAlgorithm, publicKeys, passwords, wildcard = false, encryptionKeyIds = [], date = new Date(), userIds = [], config = defaultConfig) {
|
32084 | const packetlist = new PacketList();
|
32085 |
|
32086 | if (publicKeys) {
|
32087 | const results = await Promise.all(publicKeys.map(async function(publicKey, i) {
|
32088 | const encryptionKey = await publicKey.getEncryptionKey(encryptionKeyIds[i], date, userIds, config);
|
32089 | const pkESKeyPacket = new PublicKeyEncryptedSessionKeyPacket();
|
32090 | pkESKeyPacket.publicKeyId = wildcard ? Keyid.wildcard() : encryptionKey.getKeyId();
|
32091 | pkESKeyPacket.publicKeyAlgorithm = encryptionKey.keyPacket.algorithm;
|
32092 | pkESKeyPacket.sessionKey = sessionKey;
|
32093 | pkESKeyPacket.sessionKeyAlgorithm = algorithm;
|
32094 | await pkESKeyPacket.encrypt(encryptionKey.keyPacket);
|
32095 | delete pkESKeyPacket.sessionKey;
|
32096 | return pkESKeyPacket;
|
32097 | }));
|
32098 | packetlist.concat(results);
|
32099 | }
|
32100 | if (passwords) {
|
32101 | const testDecrypt = async function(keyPacket, password) {
|
32102 | try {
|
32103 | await keyPacket.decrypt(password);
|
32104 | return 1;
|
32105 | } catch (e) {
|
32106 | return 0;
|
32107 | }
|
32108 | };
|
32109 |
|
32110 | const sum = (accumulator, currentValue) => accumulator + currentValue;
|
32111 |
|
32112 | const encryptPassword = async function(sessionKey, algorithm, aeadAlgorithm, password) {
|
32113 | const symEncryptedSessionKeyPacket = new SymEncryptedSessionKeyPacket(config);
|
32114 | symEncryptedSessionKeyPacket.sessionKey = sessionKey;
|
32115 | symEncryptedSessionKeyPacket.sessionKeyAlgorithm = algorithm;
|
32116 | if (aeadAlgorithm) {
|
32117 | symEncryptedSessionKeyPacket.aeadAlgorithm = aeadAlgorithm;
|
32118 | }
|
32119 | await symEncryptedSessionKeyPacket.encrypt(password, config);
|
32120 |
|
32121 | if (config.passwordCollisionCheck) {
|
32122 | const results = await Promise.all(passwords.map(pwd => testDecrypt(symEncryptedSessionKeyPacket, pwd)));
|
32123 | if (results.reduce(sum) !== 1) {
|
32124 | return encryptPassword(sessionKey, algorithm, password);
|
32125 | }
|
32126 | }
|
32127 |
|
32128 | delete symEncryptedSessionKeyPacket.sessionKey;
|
32129 | return symEncryptedSessionKeyPacket;
|
32130 | };
|
32131 |
|
32132 | const results = await Promise.all(passwords.map(pwd => encryptPassword(sessionKey, algorithm, aeadAlgorithm, pwd)));
|
32133 | packetlist.concat(results);
|
32134 | }
|
32135 |
|
32136 | return new Message(packetlist);
|
32137 | }
|
32138 |
|
32139 | |
32140 |
|
32141 |
|
32142 |
|
32143 |
|
32144 |
|
32145 |
|
32146 |
|
32147 |
|
32148 |
|
32149 |
|
32150 |
|
32151 | async sign(privateKeys = [], signature = null, signingKeyIds = [], date = new Date(), userIds = [], streaming = false, config = defaultConfig) {
|
32152 | const packetlist = new PacketList();
|
32153 |
|
32154 | const literalDataPacket = this.packets.findPacket(enums.packet.literalData);
|
32155 | if (!literalDataPacket) {
|
32156 | throw new Error('No literal data packet to sign.');
|
32157 | }
|
32158 |
|
32159 | let i;
|
32160 | let existingSigPacketlist;
|
32161 |
|
32162 | const signatureType = literalDataPacket.text === null ?
|
32163 | enums.signature.binary : enums.signature.text;
|
32164 |
|
32165 | if (signature) {
|
32166 | existingSigPacketlist = signature.packets.filterByTag(enums.packet.signature);
|
32167 | for (i = existingSigPacketlist.length - 1; i >= 0; i--) {
|
32168 | const signaturePacket = existingSigPacketlist[i];
|
32169 | const onePassSig = new OnePassSignaturePacket();
|
32170 | onePassSig.signatureType = signaturePacket.signatureType;
|
32171 | onePassSig.hashAlgorithm = signaturePacket.hashAlgorithm;
|
32172 | onePassSig.publicKeyAlgorithm = signaturePacket.publicKeyAlgorithm;
|
32173 | onePassSig.issuerKeyId = signaturePacket.issuerKeyId;
|
32174 | if (!privateKeys.length && i === 0) {
|
32175 | onePassSig.flags = 1;
|
32176 | }
|
32177 | packetlist.push(onePassSig);
|
32178 | }
|
32179 | }
|
32180 |
|
32181 | await Promise.all(Array.from(privateKeys).reverse().map(async function (privateKey, i) {
|
32182 | if (privateKey.isPublic()) {
|
32183 | throw new Error('Need private key for signing');
|
32184 | }
|
32185 | const signingKeyId = signingKeyIds[privateKeys.length - 1 - i];
|
32186 | const signingKey = await privateKey.getSigningKey(signingKeyId, date, userIds, config);
|
32187 | const onePassSig = new OnePassSignaturePacket();
|
32188 | onePassSig.signatureType = signatureType;
|
32189 | onePassSig.hashAlgorithm = await getPreferredHashAlgo$1(privateKey, signingKey.keyPacket, date, userIds, config);
|
32190 | onePassSig.publicKeyAlgorithm = signingKey.keyPacket.algorithm;
|
32191 | onePassSig.issuerKeyId = signingKey.getKeyId();
|
32192 | if (i === privateKeys.length - 1) {
|
32193 | onePassSig.flags = 1;
|
32194 | }
|
32195 | return onePassSig;
|
32196 | })).then(onePassSignatureList => {
|
32197 | onePassSignatureList.forEach(onePassSig => packetlist.push(onePassSig));
|
32198 | });
|
32199 |
|
32200 | packetlist.push(literalDataPacket);
|
32201 | packetlist.concat(await createSignaturePackets(literalDataPacket, privateKeys, signature, signingKeyIds, date, userIds, false, streaming, config));
|
32202 |
|
32203 | return new Message(packetlist);
|
32204 | }
|
32205 |
|
32206 | |
32207 |
|
32208 |
|
32209 |
|
32210 |
|
32211 | compress(config = defaultConfig) {
|
32212 | if (config.compression === enums.compression.uncompressed) {
|
32213 | return this;
|
32214 | }
|
32215 |
|
32216 | const compressed = new CompressedDataPacket(config);
|
32217 | compressed.packets = this.packets;
|
32218 |
|
32219 | const packetList = new PacketList();
|
32220 | packetList.push(compressed);
|
32221 |
|
32222 | return new Message(packetList);
|
32223 | }
|
32224 |
|
32225 | |
32226 |
|
32227 |
|
32228 |
|
32229 |
|
32230 |
|
32231 |
|
32232 |
|
32233 |
|
32234 |
|
32235 |
|
32236 |
|
32237 | async signDetached(privateKeys = [], signature = null, signingKeyIds = [], date = new Date(), userIds = [], streaming = false, config = defaultConfig) {
|
32238 | const literalDataPacket = this.packets.findPacket(enums.packet.literalData);
|
32239 | if (!literalDataPacket) {
|
32240 | throw new Error('No literal data packet to sign.');
|
32241 | }
|
32242 | return new Signature(await createSignaturePackets(literalDataPacket, privateKeys, signature, signingKeyIds, date, userIds, true, streaming, config));
|
32243 | }
|
32244 |
|
32245 | |
32246 |
|
32247 |
|
32248 |
|
32249 |
|
32250 |
|
32251 |
|
32252 |
|
32253 |
|
32254 | async verify(keys, date = new Date(), streaming, config = defaultConfig) {
|
32255 | const msg = this.unwrapCompressed();
|
32256 | const literalDataList = msg.packets.filterByTag(enums.packet.literalData);
|
32257 | if (literalDataList.length !== 1) {
|
32258 | throw new Error('Can only verify message with one literal data packet.');
|
32259 | }
|
32260 | if (!streaming) {
|
32261 | msg.packets.concat(await stream.readToEnd(msg.packets.stream, _ => _));
|
32262 | }
|
32263 | const onePassSigList = msg.packets.filterByTag(enums.packet.onePassSignature).reverse();
|
32264 | const signatureList = msg.packets.filterByTag(enums.packet.signature);
|
32265 | if (streaming && onePassSigList.length && !signatureList.length && msg.packets.stream) {
|
32266 | await Promise.all(onePassSigList.map(async onePassSig => {
|
32267 | onePassSig.correspondingSig = new Promise((resolve, reject) => {
|
32268 | onePassSig.correspondingSigResolve = resolve;
|
32269 | onePassSig.correspondingSigReject = reject;
|
32270 | });
|
32271 | onePassSig.signatureData = stream.fromAsync(async () => (await onePassSig.correspondingSig).signatureData);
|
32272 | onePassSig.hashed = stream.readToEnd(await onePassSig.hash(onePassSig.signatureType, literalDataList[0], undefined, false, streaming));
|
32273 | onePassSig.hashed.catch(() => {});
|
32274 | }));
|
32275 | msg.packets.stream = stream.transformPair(msg.packets.stream, async (readable, writable) => {
|
32276 | const reader = stream.getReader(readable);
|
32277 | const writer = stream.getWriter(writable);
|
32278 | try {
|
32279 | for (let i = 0; i < onePassSigList.length; i++) {
|
32280 | const { value: signature } = await reader.read();
|
32281 | onePassSigList[i].correspondingSigResolve(signature);
|
32282 | }
|
32283 | await reader.readToEnd();
|
32284 | await writer.ready;
|
32285 | await writer.close();
|
32286 | } catch (e) {
|
32287 | onePassSigList.forEach(onePassSig => {
|
32288 | onePassSig.correspondingSigReject(e);
|
32289 | });
|
32290 | await writer.abort(e);
|
32291 | }
|
32292 | });
|
32293 | return createVerificationObjects(onePassSigList, literalDataList, keys, date, false, streaming, config);
|
32294 | }
|
32295 | return createVerificationObjects(signatureList, literalDataList, keys, date, false, streaming, config);
|
32296 | }
|
32297 |
|
32298 | |
32299 |
|
32300 |
|
32301 |
|
32302 |
|
32303 |
|
32304 |
|
32305 |
|
32306 |
|
32307 | verifyDetached(signature, keys, date = new Date(), streaming, config = defaultConfig) {
|
32308 | const msg = this.unwrapCompressed();
|
32309 | const literalDataList = msg.packets.filterByTag(enums.packet.literalData);
|
32310 | if (literalDataList.length !== 1) {
|
32311 | throw new Error('Can only verify message with one literal data packet.');
|
32312 | }
|
32313 | const signatureList = signature.packets;
|
32314 | return createVerificationObjects(signatureList, literalDataList, keys, date, true, undefined, config);
|
32315 | }
|
32316 |
|
32317 | |
32318 |
|
32319 |
|
32320 |
|
32321 | unwrapCompressed() {
|
32322 | const compressed = this.packets.filterByTag(enums.packet.compressedData);
|
32323 | if (compressed.length) {
|
32324 | return new Message(compressed[0].packets);
|
32325 | }
|
32326 | return this;
|
32327 | }
|
32328 |
|
32329 | |
32330 |
|
32331 |
|
32332 |
|
32333 | async appendSignature(detachedSignature) {
|
32334 | await this.packets.read(util.isUint8Array(detachedSignature) ? detachedSignature : (await unarmor(detachedSignature)).data, { SignaturePacket });
|
32335 | }
|
32336 |
|
32337 | |
32338 |
|
32339 |
|
32340 |
|
32341 | write() {
|
32342 | return this.packets.write();
|
32343 | }
|
32344 |
|
32345 | |
32346 |
|
32347 |
|
32348 |
|
32349 |
|
32350 | armor(config = defaultConfig) {
|
32351 | return armor(enums.armor.message, this.write(), null, null, null, config);
|
32352 | }
|
32353 |
|
32354 | |
32355 |
|
32356 |
|
32357 |
|
32358 |
|
32359 |
|
32360 |
|
32361 |
|
32362 |
|
32363 | static fromText(text, filename, date = new Date(), type = 'utf8') {
|
32364 | const streamType = util.isStream(text);
|
32365 | if (streamType === 'node') {
|
32366 | text = stream.nodeToWeb(text);
|
32367 | }
|
32368 | const literalDataPacket = new LiteralDataPacket(date);
|
32369 |
|
32370 | literalDataPacket.setText(text, type);
|
32371 | if (filename !== undefined) {
|
32372 | literalDataPacket.setFilename(filename);
|
32373 | }
|
32374 | const literalDataPacketlist = new PacketList();
|
32375 | literalDataPacketlist.push(literalDataPacket);
|
32376 | const message = new Message(literalDataPacketlist);
|
32377 | message.fromStream = streamType;
|
32378 | return message;
|
32379 | }
|
32380 |
|
32381 | |
32382 |
|
32383 |
|
32384 |
|
32385 |
|
32386 |
|
32387 |
|
32388 |
|
32389 |
|
32390 | static fromBinary(bytes, filename, date = new Date(), type = 'binary') {
|
32391 | const streamType = util.isStream(bytes);
|
32392 | if (!util.isUint8Array(bytes) && !streamType) {
|
32393 | throw new Error('Data must be in the form of a Uint8Array or Stream');
|
32394 | }
|
32395 | if (streamType === 'node') {
|
32396 | bytes = stream.nodeToWeb(bytes);
|
32397 | }
|
32398 |
|
32399 | const literalDataPacket = new LiteralDataPacket(date);
|
32400 | literalDataPacket.setBytes(bytes, type);
|
32401 | if (filename !== undefined) {
|
32402 | literalDataPacket.setFilename(filename);
|
32403 | }
|
32404 | const literalDataPacketlist = new PacketList();
|
32405 | literalDataPacketlist.push(literalDataPacket);
|
32406 | const message = new Message(literalDataPacketlist);
|
32407 | message.fromStream = streamType;
|
32408 | return message;
|
32409 | }
|
32410 | }
|
32411 |
|
32412 |
|
32413 |
|
32414 |
|
32415 |
|
32416 |
|
32417 |
|
32418 |
|
32419 |
|
32420 |
|
32421 |
|
32422 |
|
32423 |
|
32424 |
|
32425 |
|
32426 |
|
32427 | async function createSignaturePackets(literalDataPacket, privateKeys, signature = null, signingKeyIds = [], date = new Date(), userIds = [], detached = false, streaming = false, config = defaultConfig) {
|
32428 | const packetlist = new PacketList();
|
32429 |
|
32430 |
|
32431 | const signatureType = literalDataPacket.text === null ?
|
32432 | enums.signature.binary : enums.signature.text;
|
32433 |
|
32434 | await Promise.all(privateKeys.map(async (privateKey, i) => {
|
32435 | const userId = userIds[i];
|
32436 | if (privateKey.isPublic()) {
|
32437 | throw new Error('Need private key for signing');
|
32438 | }
|
32439 | const signingKey = await privateKey.getSigningKey(signingKeyIds[i], date, userId, config);
|
32440 | return createSignaturePacket(literalDataPacket, privateKey, signingKey.keyPacket, { signatureType }, date, userId, detached, streaming, config);
|
32441 | })).then(signatureList => {
|
32442 | signatureList.forEach(signaturePacket => packetlist.push(signaturePacket));
|
32443 | });
|
32444 |
|
32445 | if (signature) {
|
32446 | const existingSigPacketlist = signature.packets.filterByTag(enums.packet.signature);
|
32447 | packetlist.concat(existingSigPacketlist);
|
32448 | }
|
32449 | return packetlist;
|
32450 | }
|
32451 |
|
32452 |
|
32453 |
|
32454 |
|
32455 |
|
32456 |
|
32457 |
|
32458 |
|
32459 |
|
32460 |
|
32461 |
|
32462 |
|
32463 |
|
32464 |
|
32465 |
|
32466 | async function createVerificationObject(signature, literalDataList, keys, date = new Date(), detached = false, streaming = false, config = defaultConfig) {
|
32467 | let primaryKey = null;
|
32468 | let signingKey = null;
|
32469 | await Promise.all(keys.map(async function(key) {
|
32470 |
|
32471 | try {
|
32472 | signingKey = await key.getSigningKey(signature.issuerKeyId, null, undefined, config);
|
32473 | primaryKey = key;
|
32474 | } catch (e) {}
|
32475 | }));
|
32476 |
|
32477 | const signaturePacket = signature.correspondingSig || signature;
|
32478 | const verifiedSig = {
|
32479 | keyid: signature.issuerKeyId,
|
32480 | verified: (async () => {
|
32481 | if (!signingKey) {
|
32482 | return null;
|
32483 | }
|
32484 | await signature.verify(signingKey.keyPacket, signature.signatureType, literalDataList[0], detached, streaming, config);
|
32485 | const sig = await signaturePacket;
|
32486 | if (sig.isExpired(date) || !(
|
32487 | sig.created >= signingKey.getCreationTime() &&
|
32488 | sig.created < await (signingKey === primaryKey ?
|
32489 | signingKey.getExpirationTime(undefined, undefined, undefined, config) :
|
32490 | signingKey.getExpirationTime(primaryKey, date, undefined, config)
|
32491 | )
|
32492 | )) {
|
32493 | throw new Error('Signature is expired');
|
32494 | }
|
32495 | return true;
|
32496 | })(),
|
32497 | signature: (async () => {
|
32498 | const sig = await signaturePacket;
|
32499 | const packetlist = new PacketList();
|
32500 | packetlist.push(sig);
|
32501 | return new Signature(packetlist);
|
32502 | })()
|
32503 | };
|
32504 |
|
32505 |
|
32506 |
|
32507 |
|
32508 |
|
32509 | verifiedSig.signature.catch(() => {});
|
32510 | verifiedSig.verified.catch(() => {});
|
32511 |
|
32512 | return verifiedSig;
|
32513 | }
|
32514 |
|
32515 |
|
32516 |
|
32517 |
|
32518 |
|
32519 |
|
32520 |
|
32521 |
|
32522 |
|
32523 |
|
32524 |
|
32525 |
|
32526 |
|
32527 |
|
32528 |
|
32529 | async function createVerificationObjects(signatureList, literalDataList, keys, date = new Date(), detached = false, streaming = false, config = defaultConfig) {
|
32530 | return Promise.all(signatureList.filter(function(signature) {
|
32531 | return ['text', 'binary'].includes(enums.read(enums.signature, signature.signatureType));
|
32532 | }).map(async function(signature) {
|
32533 | return createVerificationObject(signature, literalDataList, keys, date, detached, streaming, config);
|
32534 | }));
|
32535 | }
|
32536 |
|
32537 |
|
32538 |
|
32539 |
|
32540 |
|
32541 |
|
32542 |
|
32543 |
|
32544 |
|
32545 |
|
32546 |
|
32547 | async function readMessage({ armoredMessage, binaryMessage, config }) {
|
32548 | config = { ...defaultConfig, ...config };
|
32549 | let input = armoredMessage || binaryMessage;
|
32550 | if (!input) {
|
32551 | throw new Error('readMessage: must pass options object containing `armoredMessage` or `binaryMessage`');
|
32552 | }
|
32553 | const streamType = util.isStream(input);
|
32554 | if (streamType === 'node') {
|
32555 | input = stream.nodeToWeb(input);
|
32556 | }
|
32557 | if (armoredMessage) {
|
32558 | const { type, data } = await unarmor(input, config);
|
32559 | if (type !== enums.armor.message) {
|
32560 | throw new Error('Armored text not of type message');
|
32561 | }
|
32562 | input = data;
|
32563 | }
|
32564 | const packetlist = new PacketList();
|
32565 | await packetlist.read(input, {
|
32566 | LiteralDataPacket,
|
32567 | CompressedDataPacket,
|
32568 | AEADEncryptedDataPacket,
|
32569 | SymEncryptedIntegrityProtectedDataPacket,
|
32570 | SymmetricallyEncryptedDataPacket,
|
32571 | PublicKeyEncryptedSessionKeyPacket,
|
32572 | SymEncryptedSessionKeyPacket,
|
32573 | OnePassSignaturePacket,
|
32574 | SignaturePacket
|
32575 | }, streamType, config);
|
32576 | const message = new Message(packetlist);
|
32577 | message.fromStream = streamType;
|
32578 | return message;
|
32579 | }
|
32580 |
|
32581 |
|
32582 |
|
32583 |
|
32584 |
|
32585 |
|
32586 |
|
32587 | class CleartextMessage {
|
32588 | |
32589 |
|
32590 |
|
32591 |
|
32592 | constructor(text, signature) {
|
32593 |
|
32594 | this.text = util.removeTrailingSpaces(text).replace(/\r?\n/g, '\r\n');
|
32595 | if (signature && !(signature instanceof Signature)) {
|
32596 | throw new Error('Invalid signature input');
|
32597 | }
|
32598 | this.signature = signature || new Signature(new PacketList());
|
32599 | }
|
32600 |
|
32601 | |
32602 |
|
32603 |
|
32604 |
|
32605 | getSigningKeyIds() {
|
32606 | const keyIds = [];
|
32607 | const signatureList = this.signature.packets;
|
32608 | signatureList.forEach(function(packet) {
|
32609 | keyIds.push(packet.issuerKeyId);
|
32610 | });
|
32611 | return keyIds;
|
32612 | }
|
32613 |
|
32614 | |
32615 |
|
32616 |
|
32617 |
|
32618 |
|
32619 |
|
32620 |
|
32621 |
|
32622 |
|
32623 |
|
32624 |
|
32625 | async sign(privateKeys, signature = null, signingKeyIds = [], date = new Date(), userIds = [], config = defaultConfig) {
|
32626 | return new CleartextMessage(this.text, await this.signDetached(privateKeys, signature, signingKeyIds, date, userIds, config));
|
32627 | }
|
32628 |
|
32629 | |
32630 |
|
32631 |
|
32632 |
|
32633 |
|
32634 |
|
32635 |
|
32636 |
|
32637 |
|
32638 |
|
32639 |
|
32640 | async signDetached(privateKeys, signature = null, signingKeyIds = [], date = new Date(), userIds = [], config = defaultConfig) {
|
32641 | const literalDataPacket = new LiteralDataPacket();
|
32642 | literalDataPacket.setText(this.text);
|
32643 |
|
32644 | return new Signature(await createSignaturePackets(literalDataPacket, privateKeys, signature, signingKeyIds, date, userIds, true, undefined, config));
|
32645 | }
|
32646 |
|
32647 | |
32648 |
|
32649 |
|
32650 |
|
32651 |
|
32652 |
|
32653 |
|
32654 |
|
32655 | verify(keys, date = new Date(), config = defaultConfig) {
|
32656 | return this.verifyDetached(this.signature, keys, date, config);
|
32657 | }
|
32658 |
|
32659 | |
32660 |
|
32661 |
|
32662 |
|
32663 |
|
32664 |
|
32665 |
|
32666 |
|
32667 | verifyDetached(signature, keys, date = new Date(), config = defaultConfig) {
|
32668 | const signatureList = signature.packets;
|
32669 | const literalDataPacket = new LiteralDataPacket();
|
32670 |
|
32671 | literalDataPacket.setText(this.text);
|
32672 | return createVerificationObjects(signatureList, [literalDataPacket], keys, date, true, undefined, config);
|
32673 | }
|
32674 |
|
32675 | |
32676 |
|
32677 |
|
32678 |
|
32679 | getText() {
|
32680 |
|
32681 | return this.text.replace(/\r\n/g, '\n');
|
32682 | }
|
32683 |
|
32684 | |
32685 |
|
32686 |
|
32687 |
|
32688 |
|
32689 | armor(config = defaultConfig) {
|
32690 | let hashes = this.signature.packets.map(function(packet) {
|
32691 | return enums.read(enums.hash, packet.hashAlgorithm).toUpperCase();
|
32692 | });
|
32693 | hashes = hashes.filter(function(item, i, ar) { return ar.indexOf(item) === i; });
|
32694 | const body = {
|
32695 | hash: hashes.join(),
|
32696 | text: this.text,
|
32697 | data: this.signature.packets.write()
|
32698 | };
|
32699 | return armor(enums.armor.signed, body, undefined, undefined, undefined, config);
|
32700 | }
|
32701 |
|
32702 | |
32703 |
|
32704 |
|
32705 |
|
32706 |
|
32707 | static fromText(text) {
|
32708 | return new CleartextMessage(text);
|
32709 | }
|
32710 | }
|
32711 |
|
32712 |
|
32713 |
|
32714 |
|
32715 |
|
32716 |
|
32717 |
|
32718 |
|
32719 |
|
32720 |
|
32721 |
|
32722 | async function readCleartextMessage({ cleartextMessage, config }) {
|
32723 | config = { ...defaultConfig, ...config };
|
32724 | if (!cleartextMessage) {
|
32725 | throw new Error('readCleartextMessage: must pass options object containing `cleartextMessage`');
|
32726 | }
|
32727 | const input = await unarmor(cleartextMessage);
|
32728 | if (input.type !== enums.armor.signed) {
|
32729 | throw new Error('No cleartext signed message.');
|
32730 | }
|
32731 | const packetlist = new PacketList();
|
32732 | await packetlist.read(input.data, { SignaturePacket }, undefined, config);
|
32733 | verifyHeaders$1(input.headers, packetlist);
|
32734 | const signature = new Signature(packetlist);
|
32735 | return new CleartextMessage(input.text, signature);
|
32736 | }
|
32737 |
|
32738 |
|
32739 |
|
32740 |
|
32741 |
|
32742 |
|
32743 |
|
32744 | function verifyHeaders$1(headers, packetlist) {
|
32745 | const checkHashAlgos = function(hashAlgos) {
|
32746 | const check = packet => algo => packet.hashAlgorithm === algo;
|
32747 |
|
32748 | for (let i = 0; i < packetlist.length; i++) {
|
32749 | if (packetlist[i].tag === enums.packet.signature && !hashAlgos.some(check(packetlist[i]))) {
|
32750 | return false;
|
32751 | }
|
32752 | }
|
32753 | return true;
|
32754 | };
|
32755 |
|
32756 | let oneHeader = null;
|
32757 | let hashAlgos = [];
|
32758 | headers.forEach(function(header) {
|
32759 | oneHeader = header.match(/Hash: (.+)/);
|
32760 | if (oneHeader) {
|
32761 | oneHeader = oneHeader[1].replace(/\s/g, '');
|
32762 | oneHeader = oneHeader.split(',');
|
32763 | oneHeader = oneHeader.map(function(hash) {
|
32764 | hash = hash.toLowerCase();
|
32765 | try {
|
32766 | return enums.write(enums.hash, hash);
|
32767 | } catch (e) {
|
32768 | throw new Error('Unknown hash algorithm in armor header: ' + hash);
|
32769 | }
|
32770 | });
|
32771 | hashAlgos = hashAlgos.concat(oneHeader);
|
32772 | } else {
|
32773 | throw new Error('Only "Hash" header allowed in cleartext signed message');
|
32774 | }
|
32775 | });
|
32776 |
|
32777 | if (!hashAlgos.length && !checkHashAlgos([enums.hash.md5])) {
|
32778 | throw new Error('If no "Hash" header in cleartext signed message, then only MD5 signatures allowed');
|
32779 | } else if (hashAlgos.length && !checkHashAlgos(hashAlgos)) {
|
32780 | throw new Error('Hash algorithm mismatch in armor header and signature');
|
32781 | }
|
32782 | }
|
32783 |
|
32784 |
|
32785 |
|
32786 | let toNativeReadable;
|
32787 | if (globalThis.ReadableStream) {
|
32788 | try {
|
32789 | toNativeReadable = createReadableStreamWrapper(globalThis.ReadableStream);
|
32790 | } catch (e) {}
|
32791 | }
|
32792 |
|
32793 |
|
32794 |
|
32795 |
|
32796 |
|
32797 |
|
32798 |
|
32799 |
|
32800 |
|
32801 |
|
32802 |
|
32803 |
|
32804 |
|
32805 |
|
32806 |
|
32807 |
|
32808 |
|
32809 |
|
32810 |
|
32811 |
|
32812 |
|
32813 |
|
32814 |
|
32815 |
|
32816 |
|
32817 |
|
32818 |
|
32819 |
|
32820 | function generateKey({ userIds = [], passphrase = "", type = "ecc", rsaBits = 4096, curve = "curve25519", keyExpirationTime = 0, date = new Date(), subkeys = [{}], config }) {
|
32821 | config = { ...defaultConfig, ...config };
|
32822 | userIds = toArray$1(userIds);
|
32823 | const options = { userIds, passphrase, type, rsaBits, curve, keyExpirationTime, date, subkeys };
|
32824 | if (type === "rsa" && rsaBits < config.minRsaBits) {
|
32825 | throw new Error(`rsaBits should be at least ${config.minRsaBits}, got: ${rsaBits}`);
|
32826 | }
|
32827 |
|
32828 | return generate$2(options, config).then(async key => {
|
32829 | const revocationCertificate = await key.getRevocationCertificate(date, config);
|
32830 | key.revocationSignatures = [];
|
32831 |
|
32832 | return {
|
32833 |
|
32834 | key: key,
|
32835 | privateKeyArmored: key.armor(config),
|
32836 | publicKeyArmored: key.toPublic().armor(config),
|
32837 | revocationCertificate: revocationCertificate
|
32838 |
|
32839 | };
|
32840 | }).catch(onError.bind(null, 'Error generating keypair'));
|
32841 | }
|
32842 |
|
32843 |
|
32844 |
|
32845 |
|
32846 |
|
32847 |
|
32848 |
|
32849 |
|
32850 |
|
32851 |
|
32852 |
|
32853 |
|
32854 |
|
32855 |
|
32856 | function reformatKey({ privateKey, userIds = [], passphrase = "", keyExpirationTime = 0, date, config }) {
|
32857 | config = { ...defaultConfig, ...config };
|
32858 | userIds = toArray$1(userIds);
|
32859 | const options = { privateKey, userIds, passphrase, keyExpirationTime, date };
|
32860 |
|
32861 | return reformat(options, config).then(async key => {
|
32862 | const revocationCertificate = await key.getRevocationCertificate(date, config);
|
32863 | key.revocationSignatures = [];
|
32864 |
|
32865 | return {
|
32866 |
|
32867 | key: key,
|
32868 | privateKeyArmored: key.armor(config),
|
32869 | publicKeyArmored: key.toPublic().armor(config),
|
32870 | revocationCertificate: revocationCertificate
|
32871 |
|
32872 | };
|
32873 | }).catch(onError.bind(null, 'Error reformatting keypair'));
|
32874 | }
|
32875 |
|
32876 |
|
32877 |
|
32878 |
|
32879 |
|
32880 |
|
32881 |
|
32882 |
|
32883 |
|
32884 |
|
32885 |
|
32886 |
|
32887 |
|
32888 |
|
32889 |
|
32890 |
|
32891 |
|
32892 | function revokeKey({ key, revocationCertificate, reasonForRevocation, config }) {
|
32893 | config = { ...defaultConfig, ...config };
|
32894 | return Promise.resolve().then(() => {
|
32895 | if (revocationCertificate) {
|
32896 | return key.applyRevocationCertificate(revocationCertificate, config);
|
32897 | } else {
|
32898 | return key.revoke(reasonForRevocation, undefined, config);
|
32899 | }
|
32900 | }).then(async key => {
|
32901 | if (key.isPrivate()) {
|
32902 | const publicKey = key.toPublic();
|
32903 | return {
|
32904 | privateKey: key,
|
32905 | privateKeyArmored: key.armor(config),
|
32906 | publicKey: publicKey,
|
32907 | publicKeyArmored: publicKey.armor(config)
|
32908 | };
|
32909 | }
|
32910 | return {
|
32911 | publicKey: key,
|
32912 | publicKeyArmored: key.armor(config)
|
32913 | };
|
32914 | }).catch(onError.bind(null, 'Error revoking key'));
|
32915 | }
|
32916 |
|
32917 |
|
32918 |
|
32919 |
|
32920 |
|
32921 |
|
32922 |
|
32923 |
|
32924 |
|
32925 |
|
32926 |
|
32927 | async function decryptKey({ privateKey, passphrase, config }) {
|
32928 | config = { ...defaultConfig, ...config };
|
32929 | const key = await privateKey.clone();
|
32930 |
|
32931 | key.getKeys().forEach(k => {
|
32932 | k.keyPacket = Object.create(
|
32933 | Object.getPrototypeOf(k.keyPacket),
|
32934 | Object.getOwnPropertyDescriptors(k.keyPacket)
|
32935 | );
|
32936 | });
|
32937 | try {
|
32938 | await key.decrypt(passphrase, undefined, config);
|
32939 | return key;
|
32940 | } catch (err) {
|
32941 | key.clearPrivateParams();
|
32942 | return onError('Error decrypting private key', err);
|
32943 | }
|
32944 | }
|
32945 |
|
32946 |
|
32947 |
|
32948 |
|
32949 |
|
32950 |
|
32951 |
|
32952 |
|
32953 |
|
32954 |
|
32955 |
|
32956 | async function encryptKey({ privateKey, passphrase, config }) {
|
32957 | config = { ...defaultConfig, ...config };
|
32958 | const key = await privateKey.clone();
|
32959 | key.getKeys().forEach(k => {
|
32960 |
|
32961 | k.keyPacket = Object.create(
|
32962 | Object.getPrototypeOf(k.keyPacket),
|
32963 | Object.getOwnPropertyDescriptors(k.keyPacket)
|
32964 | );
|
32965 | if (!k.keyPacket.isDecrypted()) return;
|
32966 |
|
32967 | const privateParams = {};
|
32968 | Object.keys(k.keyPacket.privateParams).forEach(name => {
|
32969 | privateParams[name] = new Uint8Array(k.keyPacket.privateParams[name]);
|
32970 | });
|
32971 | k.keyPacket.privateParams = privateParams;
|
32972 | });
|
32973 | try {
|
32974 | await key.encrypt(passphrase, undefined, config);
|
32975 | return key;
|
32976 | } catch (err) {
|
32977 | key.clearPrivateParams();
|
32978 | return onError('Error encrypting private key', err);
|
32979 | }
|
32980 | }
|
32981 |
|
32982 |
|
32983 |
|
32984 |
|
32985 |
|
32986 |
|
32987 |
|
32988 |
|
32989 |
|
32990 |
|
32991 |
|
32992 |
|
32993 |
|
32994 |
|
32995 |
|
32996 |
|
32997 |
|
32998 |
|
32999 |
|
33000 |
|
33001 |
|
33002 |
|
33003 |
|
33004 |
|
33005 |
|
33006 |
|
33007 |
|
33008 |
|
33009 |
|
33010 |
|
33011 |
|
33012 |
|
33013 | function encrypt$4({ message, publicKeys, privateKeys, passwords, sessionKey, armor = true, streaming = message && message.fromStream, detached = false, signature = null, wildcard = false, signingKeyIds = [], encryptionKeyIds = [], date = new Date(), fromUserIds = [], toUserIds = [], config }) {
|
33014 | config = { ...defaultConfig, ...config };
|
33015 | checkMessage(message); publicKeys = toArray$1(publicKeys); privateKeys = toArray$1(privateKeys); passwords = toArray$1(passwords); fromUserIds = toArray$1(fromUserIds); toUserIds = toArray$1(toUserIds);
|
33016 | if (detached) {
|
33017 | throw new Error("detached option has been removed from openpgp.encrypt. Separately call openpgp.sign instead. Don't forget to remove privateKeys option as well.");
|
33018 | }
|
33019 |
|
33020 | return Promise.resolve().then(async function() {
|
33021 | if (!privateKeys) {
|
33022 | privateKeys = [];
|
33023 | }
|
33024 | if (privateKeys.length || signature) {
|
33025 | message = await message.sign(privateKeys, signature, signingKeyIds, date, fromUserIds, message.fromStream, config);
|
33026 | }
|
33027 | message = message.compress(config);
|
33028 | message = await message.encrypt(publicKeys, passwords, sessionKey, wildcard, encryptionKeyIds, date, toUserIds, streaming, config);
|
33029 | const data = armor ? message.armor(config) : message.write();
|
33030 | return convertStream(data, streaming, armor ? 'utf8' : 'binary');
|
33031 | }).catch(onError.bind(null, 'Error encrypting message'));
|
33032 | }
|
33033 |
|
33034 |
|
33035 |
|
33036 |
|
33037 |
|
33038 |
|
33039 |
|
33040 |
|
33041 |
|
33042 |
|
33043 |
|
33044 |
|
33045 |
|
33046 |
|
33047 |
|
33048 |
|
33049 |
|
33050 |
|
33051 |
|
33052 |
|
33053 |
|
33054 |
|
33055 |
|
33056 |
|
33057 |
|
33058 |
|
33059 |
|
33060 |
|
33061 |
|
33062 |
|
33063 |
|
33064 |
|
33065 | function decrypt$4({ message, privateKeys, passwords, sessionKeys, publicKeys, format = 'utf8', streaming = message && message.fromStream, signature = null, date = new Date(), config }) {
|
33066 | config = { ...defaultConfig, ...config };
|
33067 | checkMessage(message); publicKeys = toArray$1(publicKeys); privateKeys = toArray$1(privateKeys); passwords = toArray$1(passwords); sessionKeys = toArray$1(sessionKeys);
|
33068 |
|
33069 | return message.decrypt(privateKeys, passwords, sessionKeys, streaming, config).then(async function(decrypted) {
|
33070 | if (!publicKeys) {
|
33071 | publicKeys = [];
|
33072 | }
|
33073 |
|
33074 | const result = {};
|
33075 | result.signatures = signature ? await decrypted.verifyDetached(signature, publicKeys, date, streaming, config) : await decrypted.verify(publicKeys, date, streaming, config);
|
33076 | result.data = format === 'binary' ? decrypted.getLiteralData() : decrypted.getText();
|
33077 | result.filename = decrypted.getFilename();
|
33078 | linkStreams(result, message);
|
33079 | result.data = await convertStream(result.data, streaming, format);
|
33080 | if (!streaming) await prepareSignatures(result.signatures);
|
33081 | return result;
|
33082 | }).catch(onError.bind(null, 'Error decrypting message'));
|
33083 | }
|
33084 |
|
33085 |
|
33086 |
|
33087 |
|
33088 |
|
33089 |
|
33090 |
|
33091 |
|
33092 |
|
33093 |
|
33094 |
|
33095 |
|
33096 |
|
33097 |
|
33098 |
|
33099 |
|
33100 |
|
33101 |
|
33102 |
|
33103 |
|
33104 |
|
33105 |
|
33106 |
|
33107 |
|
33108 |
|
33109 | function sign$5({ message, privateKeys, armor = true, streaming = message && message.fromStream, detached = false, signingKeyIds = [], date = new Date(), fromUserIds = [], config }) {
|
33110 | config = { ...defaultConfig, ...config };
|
33111 | checkCleartextOrMessage(message);
|
33112 | if (message instanceof CleartextMessage && !armor) throw new Error("Can't sign non-armored cleartext message");
|
33113 | if (message instanceof CleartextMessage && detached) throw new Error("Can't sign detached cleartext message");
|
33114 | privateKeys = toArray$1(privateKeys); fromUserIds = toArray$1(fromUserIds);
|
33115 |
|
33116 | return Promise.resolve().then(async function() {
|
33117 | let signature;
|
33118 | if (message instanceof CleartextMessage) {
|
33119 | signature = await message.sign(privateKeys, undefined, signingKeyIds, date, fromUserIds, config);
|
33120 | } else if (detached) {
|
33121 | signature = await message.signDetached(privateKeys, undefined, signingKeyIds, date, fromUserIds, message.fromStream, config);
|
33122 | } else {
|
33123 | signature = await message.sign(privateKeys, undefined, signingKeyIds, date, fromUserIds, message.fromStream, config);
|
33124 | }
|
33125 | signature = armor ? signature.armor(config) : signature.write();
|
33126 | if (detached) {
|
33127 | signature = stream.transformPair(message.packets.write(), async (readable, writable) => {
|
33128 | await Promise.all([
|
33129 | stream.pipe(signature, writable),
|
33130 | stream.readToEnd(readable).catch(() => {})
|
33131 | ]);
|
33132 | });
|
33133 | }
|
33134 | return convertStream(signature, streaming, armor ? 'utf8' : 'binary');
|
33135 | }).catch(onError.bind(null, 'Error signing message'));
|
33136 | }
|
33137 |
|
33138 |
|
33139 |
|
33140 |
|
33141 |
|
33142 |
|
33143 |
|
33144 |
|
33145 |
|
33146 |
|
33147 |
|
33148 |
|
33149 |
|
33150 |
|
33151 |
|
33152 |
|
33153 |
|
33154 |
|
33155 |
|
33156 |
|
33157 |
|
33158 |
|
33159 |
|
33160 |
|
33161 |
|
33162 |
|
33163 |
|
33164 | function verify$5({ message, publicKeys, format = 'utf8', streaming = message && message.fromStream, signature = null, date = new Date(), config }) {
|
33165 | config = { ...defaultConfig, ...config };
|
33166 | checkCleartextOrMessage(message);
|
33167 | if (message instanceof CleartextMessage && format === 'binary') throw new Error("Can't return cleartext message data as binary");
|
33168 | publicKeys = toArray$1(publicKeys);
|
33169 |
|
33170 | return Promise.resolve().then(async function() {
|
33171 | const result = {};
|
33172 | if (message instanceof CleartextMessage) {
|
33173 | result.signatures = signature ? await message.verifyDetached(signature, publicKeys, date, config) : await message.verify(publicKeys, date, config);
|
33174 | } else {
|
33175 | result.signatures = signature ? await message.verifyDetached(signature, publicKeys, date, streaming, config) : await message.verify(publicKeys, date, streaming, config);
|
33176 | }
|
33177 | result.data = format === 'binary' ? message.getLiteralData() : message.getText();
|
33178 | if (streaming) linkStreams(result, message);
|
33179 | result.data = await convertStream(result.data, streaming, format);
|
33180 | if (!streaming) await prepareSignatures(result.signatures);
|
33181 | return result;
|
33182 | }).catch(onError.bind(null, 'Error verifying signed message'));
|
33183 | }
|
33184 |
|
33185 |
|
33186 |
|
33187 |
|
33188 |
|
33189 |
|
33190 |
|
33191 |
|
33192 |
|
33193 |
|
33194 |
|
33195 |
|
33196 |
|
33197 |
|
33198 |
|
33199 |
|
33200 |
|
33201 |
|
33202 |
|
33203 | function generateSessionKey$1({ publicKeys, date = new Date(), toUserIds = [], config }) {
|
33204 | config = { ...defaultConfig, ...config };
|
33205 | publicKeys = toArray$1(publicKeys); toUserIds = toArray$1(toUserIds);
|
33206 |
|
33207 | return Promise.resolve().then(async function() {
|
33208 |
|
33209 | return Message.generateSessionKey(publicKeys, date, toUserIds, config);
|
33210 |
|
33211 | }).catch(onError.bind(null, 'Error generating session key'));
|
33212 | }
|
33213 |
|
33214 |
|
33215 |
|
33216 |
|
33217 |
|
33218 |
|
33219 |
|
33220 |
|
33221 |
|
33222 |
|
33223 |
|
33224 |
|
33225 |
|
33226 |
|
33227 |
|
33228 |
|
33229 |
|
33230 |
|
33231 |
|
33232 |
|
33233 | function encryptSessionKey({ data, algorithm, aeadAlgorithm, publicKeys, passwords, armor = true, wildcard = false, encryptionKeyIds = [], date = new Date(), toUserIds = [], config }) {
|
33234 | config = { ...defaultConfig, ...config };
|
33235 | checkBinary(data); checkString(algorithm, 'algorithm'); publicKeys = toArray$1(publicKeys); passwords = toArray$1(passwords); toUserIds = toArray$1(toUserIds);
|
33236 |
|
33237 | return Promise.resolve().then(async function() {
|
33238 |
|
33239 | const message = await Message.encryptSessionKey(data, algorithm, aeadAlgorithm, publicKeys, passwords, wildcard, encryptionKeyIds, date, toUserIds, config);
|
33240 | return armor ? message.armor(config) : message.write();
|
33241 |
|
33242 | }).catch(onError.bind(null, 'Error encrypting session key'));
|
33243 | }
|
33244 |
|
33245 |
|
33246 |
|
33247 |
|
33248 |
|
33249 |
|
33250 |
|
33251 |
|
33252 |
|
33253 |
|
33254 |
|
33255 |
|
33256 |
|
33257 |
|
33258 |
|
33259 | function decryptSessionKeys({ message, privateKeys, passwords, config }) {
|
33260 | config = { ...defaultConfig, ...config };
|
33261 | checkMessage(message); privateKeys = toArray$1(privateKeys); passwords = toArray$1(passwords);
|
33262 |
|
33263 | return Promise.resolve().then(async function() {
|
33264 |
|
33265 | return message.decryptSessionKeys(privateKeys, passwords, config);
|
33266 |
|
33267 | }).catch(onError.bind(null, 'Error decrypting session keys'));
|
33268 | }
|
33269 |
|
33270 |
|
33271 |
|
33272 |
|
33273 |
|
33274 |
|
33275 |
|
33276 |
|
33277 |
|
33278 |
|
33279 |
|
33280 |
|
33281 |
|
33282 | function checkString(data, name) {
|
33283 | if (!util.isString(data)) {
|
33284 | throw new Error('Parameter [' + (name || 'data') + '] must be of type String');
|
33285 | }
|
33286 | }
|
33287 | function checkBinary(data, name) {
|
33288 | if (!util.isUint8Array(data)) {
|
33289 | throw new Error('Parameter [' + (name || 'data') + '] must be of type Uint8Array');
|
33290 | }
|
33291 | }
|
33292 | function checkMessage(message) {
|
33293 | if (!(message instanceof Message)) {
|
33294 | throw new Error('Parameter [message] needs to be of type Message');
|
33295 | }
|
33296 | }
|
33297 | function checkCleartextOrMessage(message) {
|
33298 | if (!(message instanceof CleartextMessage) && !(message instanceof Message)) {
|
33299 | throw new Error('Parameter [message] needs to be of type Message or CleartextMessage');
|
33300 | }
|
33301 | }
|
33302 |
|
33303 |
|
33304 |
|
33305 |
|
33306 |
|
33307 |
|
33308 |
|
33309 | function toArray$1(param) {
|
33310 | if (param && !util.isArray(param)) {
|
33311 | param = [param];
|
33312 | }
|
33313 | return param;
|
33314 | }
|
33315 |
|
33316 |
|
33317 |
|
33318 |
|
33319 |
|
33320 |
|
33321 |
|
33322 |
|
33323 |
|
33324 | async function convertStream(data, streaming, encoding = 'utf8') {
|
33325 | let streamType = util.isStream(data);
|
33326 | if (!streaming && streamType) {
|
33327 | return stream.readToEnd(data);
|
33328 | }
|
33329 | if (streaming && !streamType) {
|
33330 | data = stream.toStream(data);
|
33331 | streamType = util.isStream(data);
|
33332 | }
|
33333 | if (streaming === 'node') {
|
33334 | data = stream.webToNode(data);
|
33335 | if (encoding !== 'binary') data.setEncoding(encoding);
|
33336 | return data;
|
33337 | }
|
33338 | if (streaming === 'web' && streamType === 'ponyfill' && toNativeReadable) {
|
33339 | return toNativeReadable(data);
|
33340 | }
|
33341 | return data;
|
33342 | }
|
33343 |
|
33344 |
|
33345 |
|
33346 |
|
33347 |
|
33348 |
|
33349 |
|
33350 |
|
33351 |
|
33352 | function linkStreams(result, message) {
|
33353 | result.data = stream.transformPair(message.packets.stream, async (readable, writable) => {
|
33354 | await stream.pipe(result.data, writable, {
|
33355 | preventClose: true
|
33356 | });
|
33357 | const writer = stream.getWriter(writable);
|
33358 | try {
|
33359 |
|
33360 | await stream.readToEnd(readable, _ => _);
|
33361 | await writer.close();
|
33362 | } catch (e) {
|
33363 | await writer.abort(e);
|
33364 | }
|
33365 | });
|
33366 | }
|
33367 |
|
33368 |
|
33369 |
|
33370 |
|
33371 |
|
33372 |
|
33373 | async function prepareSignatures(signatures) {
|
33374 | await Promise.all(signatures.map(async signature => {
|
33375 | signature.signature = await signature.signature;
|
33376 | try {
|
33377 | signature.valid = await signature.verified;
|
33378 | } catch (e) {
|
33379 | signature.valid = false;
|
33380 | signature.error = e;
|
33381 | util.printDebugError(e);
|
33382 | }
|
33383 | }));
|
33384 | }
|
33385 |
|
33386 |
|
33387 |
|
33388 |
|
33389 |
|
33390 |
|
33391 |
|
33392 |
|
33393 | function onError(message, error) {
|
33394 |
|
33395 | util.printDebugError(error);
|
33396 |
|
33397 |
|
33398 | try {
|
33399 | error.message = message + ': ' + error.message;
|
33400 | } catch (e) {}
|
33401 |
|
33402 | throw error;
|
33403 | }
|
33404 |
|
33405 | var bn = createCommonjsModule(function (module) {
|
33406 | (function (module, exports) {
|
33407 |
|
33408 |
|
33409 | function assert (val, msg) {
|
33410 | if (!val) throw new Error(msg || 'Assertion failed');
|
33411 | }
|
33412 |
|
33413 |
|
33414 |
|
33415 | function inherits (ctor, superCtor) {
|
33416 | ctor.super_ = superCtor;
|
33417 | var TempCtor = function () {};
|
33418 | TempCtor.prototype = superCtor.prototype;
|
33419 | ctor.prototype = new TempCtor();
|
33420 | ctor.prototype.constructor = ctor;
|
33421 | }
|
33422 |
|
33423 |
|
33424 |
|
33425 | function BN (number, base, endian) {
|
33426 | if (BN.isBN(number)) {
|
33427 | return number;
|
33428 | }
|
33429 |
|
33430 | this.negative = 0;
|
33431 | this.words = null;
|
33432 | this.length = 0;
|
33433 |
|
33434 |
|
33435 | this.red = null;
|
33436 |
|
33437 | if (number !== null) {
|
33438 | if (base === 'le' || base === 'be') {
|
33439 | endian = base;
|
33440 | base = 10;
|
33441 | }
|
33442 |
|
33443 | this._init(number || 0, base || 10, endian || 'be');
|
33444 | }
|
33445 | }
|
33446 | if (typeof module === 'object') {
|
33447 | module.exports = BN;
|
33448 | } else {
|
33449 | exports.BN = BN;
|
33450 | }
|
33451 |
|
33452 | BN.BN = BN;
|
33453 | BN.wordSize = 26;
|
33454 |
|
33455 | var Buffer;
|
33456 | try {
|
33457 | Buffer = void('buffer').Buffer;
|
33458 | } catch (e) {
|
33459 | }
|
33460 |
|
33461 | BN.isBN = function isBN (num) {
|
33462 | if (num instanceof BN) {
|
33463 | return true;
|
33464 | }
|
33465 |
|
33466 | return num !== null && typeof num === 'object' &&
|
33467 | num.constructor.wordSize === BN.wordSize && Array.isArray(num.words);
|
33468 | };
|
33469 |
|
33470 | BN.max = function max (left, right) {
|
33471 | if (left.cmp(right) > 0) return left;
|
33472 | return right;
|
33473 | };
|
33474 |
|
33475 | BN.min = function min (left, right) {
|
33476 | if (left.cmp(right) < 0) return left;
|
33477 | return right;
|
33478 | };
|
33479 |
|
33480 | BN.prototype._init = function init (number, base, endian) {
|
33481 | if (typeof number === 'number') {
|
33482 | return this._initNumber(number, base, endian);
|
33483 | }
|
33484 |
|
33485 | if (typeof number === 'object') {
|
33486 | return this._initArray(number, base, endian);
|
33487 | }
|
33488 |
|
33489 | if (base === 'hex') {
|
33490 | base = 16;
|
33491 | }
|
33492 | assert(base === (base | 0) && base >= 2 && base <= 36);
|
33493 |
|
33494 | number = number.toString().replace(/\s+/g, '');
|
33495 | var start = 0;
|
33496 | if (number[0] === '-') {
|
33497 | start++;
|
33498 | }
|
33499 |
|
33500 | if (base === 16) {
|
33501 | this._parseHex(number, start);
|
33502 | } else {
|
33503 | this._parseBase(number, base, start);
|
33504 | }
|
33505 |
|
33506 | if (number[0] === '-') {
|
33507 | this.negative = 1;
|
33508 | }
|
33509 |
|
33510 | this.strip();
|
33511 |
|
33512 | if (endian !== 'le') return;
|
33513 |
|
33514 | this._initArray(this.toArray(), base, endian);
|
33515 | };
|
33516 |
|
33517 | BN.prototype._initNumber = function _initNumber (number, base, endian) {
|
33518 | if (number < 0) {
|
33519 | this.negative = 1;
|
33520 | number = -number;
|
33521 | }
|
33522 | if (number < 0x4000000) {
|
33523 | this.words = [ number & 0x3ffffff ];
|
33524 | this.length = 1;
|
33525 | } else if (number < 0x10000000000000) {
|
33526 | this.words = [
|
33527 | number & 0x3ffffff,
|
33528 | (number / 0x4000000) & 0x3ffffff
|
33529 | ];
|
33530 | this.length = 2;
|
33531 | } else {
|
33532 | assert(number < 0x20000000000000);
|
33533 | this.words = [
|
33534 | number & 0x3ffffff,
|
33535 | (number / 0x4000000) & 0x3ffffff,
|
33536 | 1
|
33537 | ];
|
33538 | this.length = 3;
|
33539 | }
|
33540 |
|
33541 | if (endian !== 'le') return;
|
33542 |
|
33543 |
|
33544 | this._initArray(this.toArray(), base, endian);
|
33545 | };
|
33546 |
|
33547 | BN.prototype._initArray = function _initArray (number, base, endian) {
|
33548 |
|
33549 | assert(typeof number.length === 'number');
|
33550 | if (number.length <= 0) {
|
33551 | this.words = [ 0 ];
|
33552 | this.length = 1;
|
33553 | return this;
|
33554 | }
|
33555 |
|
33556 | this.length = Math.ceil(number.length / 3);
|
33557 | this.words = new Array(this.length);
|
33558 | for (var i = 0; i < this.length; i++) {
|
33559 | this.words[i] = 0;
|
33560 | }
|
33561 |
|
33562 | var j, w;
|
33563 | var off = 0;
|
33564 | if (endian === 'be') {
|
33565 | for (i = number.length - 1, j = 0; i >= 0; i -= 3) {
|
33566 | w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16);
|
33567 | this.words[j] |= (w << off) & 0x3ffffff;
|
33568 | this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
|
33569 | off += 24;
|
33570 | if (off >= 26) {
|
33571 | off -= 26;
|
33572 | j++;
|
33573 | }
|
33574 | }
|
33575 | } else if (endian === 'le') {
|
33576 | for (i = 0, j = 0; i < number.length; i += 3) {
|
33577 | w = number[i] | (number[i + 1] << 8) | (number[i + 2] << 16);
|
33578 | this.words[j] |= (w << off) & 0x3ffffff;
|
33579 | this.words[j + 1] = (w >>> (26 - off)) & 0x3ffffff;
|
33580 | off += 24;
|
33581 | if (off >= 26) {
|
33582 | off -= 26;
|
33583 | j++;
|
33584 | }
|
33585 | }
|
33586 | }
|
33587 | return this.strip();
|
33588 | };
|
33589 |
|
33590 | function parseHex (str, start, end) {
|
33591 | var r = 0;
|
33592 | var len = Math.min(str.length, end);
|
33593 | for (var i = start; i < len; i++) {
|
33594 | var c = str.charCodeAt(i) - 48;
|
33595 |
|
33596 | r <<= 4;
|
33597 |
|
33598 |
|
33599 | if (c >= 49 && c <= 54) {
|
33600 | r |= c - 49 + 0xa;
|
33601 |
|
33602 |
|
33603 | } else if (c >= 17 && c <= 22) {
|
33604 | r |= c - 17 + 0xa;
|
33605 |
|
33606 |
|
33607 | } else {
|
33608 | r |= c & 0xf;
|
33609 | }
|
33610 | }
|
33611 | return r;
|
33612 | }
|
33613 |
|
33614 | BN.prototype._parseHex = function _parseHex (number, start) {
|
33615 |
|
33616 | this.length = Math.ceil((number.length - start) / 6);
|
33617 | this.words = new Array(this.length);
|
33618 | for (var i = 0; i < this.length; i++) {
|
33619 | this.words[i] = 0;
|
33620 | }
|
33621 |
|
33622 | var j, w;
|
33623 |
|
33624 | var off = 0;
|
33625 | for (i = number.length - 6, j = 0; i >= start; i -= 6) {
|
33626 | w = parseHex(number, i, i + 6);
|
33627 | this.words[j] |= (w << off) & 0x3ffffff;
|
33628 |
|
33629 | this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
|
33630 | off += 24;
|
33631 | if (off >= 26) {
|
33632 | off -= 26;
|
33633 | j++;
|
33634 | }
|
33635 | }
|
33636 | if (i + 6 !== start) {
|
33637 | w = parseHex(number, start, i + 6);
|
33638 | this.words[j] |= (w << off) & 0x3ffffff;
|
33639 | this.words[j + 1] |= w >>> (26 - off) & 0x3fffff;
|
33640 | }
|
33641 | this.strip();
|
33642 | };
|
33643 |
|
33644 | function parseBase (str, start, end, mul) {
|
33645 | var r = 0;
|
33646 | var len = Math.min(str.length, end);
|
33647 | for (var i = start; i < len; i++) {
|
33648 | var c = str.charCodeAt(i) - 48;
|
33649 |
|
33650 | r *= mul;
|
33651 |
|
33652 |
|
33653 | if (c >= 49) {
|
33654 | r += c - 49 + 0xa;
|
33655 |
|
33656 |
|
33657 | } else if (c >= 17) {
|
33658 | r += c - 17 + 0xa;
|
33659 |
|
33660 |
|
33661 | } else {
|
33662 | r += c;
|
33663 | }
|
33664 | }
|
33665 | return r;
|
33666 | }
|
33667 |
|
33668 | BN.prototype._parseBase = function _parseBase (number, base, start) {
|
33669 |
|
33670 | this.words = [ 0 ];
|
33671 | this.length = 1;
|
33672 |
|
33673 |
|
33674 | for (var limbLen = 0, limbPow = 1; limbPow <= 0x3ffffff; limbPow *= base) {
|
33675 | limbLen++;
|
33676 | }
|
33677 | limbLen--;
|
33678 | limbPow = (limbPow / base) | 0;
|
33679 |
|
33680 | var total = number.length - start;
|
33681 | var mod = total % limbLen;
|
33682 | var end = Math.min(total, total - mod) + start;
|
33683 |
|
33684 | var word = 0;
|
33685 | for (var i = start; i < end; i += limbLen) {
|
33686 | word = parseBase(number, i, i + limbLen, base);
|
33687 |
|
33688 | this.imuln(limbPow);
|
33689 | if (this.words[0] + word < 0x4000000) {
|
33690 | this.words[0] += word;
|
33691 | } else {
|
33692 | this._iaddn(word);
|
33693 | }
|
33694 | }
|
33695 |
|
33696 | if (mod !== 0) {
|
33697 | var pow = 1;
|
33698 | word = parseBase(number, i, number.length, base);
|
33699 |
|
33700 | for (i = 0; i < mod; i++) {
|
33701 | pow *= base;
|
33702 | }
|
33703 |
|
33704 | this.imuln(pow);
|
33705 | if (this.words[0] + word < 0x4000000) {
|
33706 | this.words[0] += word;
|
33707 | } else {
|
33708 | this._iaddn(word);
|
33709 | }
|
33710 | }
|
33711 | };
|
33712 |
|
33713 | BN.prototype.copy = function copy (dest) {
|
33714 | dest.words = new Array(this.length);
|
33715 | for (var i = 0; i < this.length; i++) {
|
33716 | dest.words[i] = this.words[i];
|
33717 | }
|
33718 | dest.length = this.length;
|
33719 | dest.negative = this.negative;
|
33720 | dest.red = this.red;
|
33721 | };
|
33722 |
|
33723 | BN.prototype.clone = function clone () {
|
33724 | var r = new BN(null);
|
33725 | this.copy(r);
|
33726 | return r;
|
33727 | };
|
33728 |
|
33729 | BN.prototype._expand = function _expand (size) {
|
33730 | while (this.length < size) {
|
33731 | this.words[this.length++] = 0;
|
33732 | }
|
33733 | return this;
|
33734 | };
|
33735 |
|
33736 |
|
33737 | BN.prototype.strip = function strip () {
|
33738 | while (this.length > 1 && this.words[this.length - 1] === 0) {
|
33739 | this.length--;
|
33740 | }
|
33741 | return this._normSign();
|
33742 | };
|
33743 |
|
33744 | BN.prototype._normSign = function _normSign () {
|
33745 |
|
33746 | if (this.length === 1 && this.words[0] === 0) {
|
33747 | this.negative = 0;
|
33748 | }
|
33749 | return this;
|
33750 | };
|
33751 |
|
33752 | BN.prototype.inspect = function inspect () {
|
33753 | return (this.red ? '<BN-R: ' : '<BN: ') + this.toString(16) + '>';
|
33754 | };
|
33755 |
|
33756 | |
33757 |
|
33758 |
|
33759 |
|
33760 |
|
33761 |
|
33762 |
|
33763 |
|
33764 |
|
33765 |
|
33766 |
|
33767 |
|
33768 |
|
33769 |
|
33770 |
|
33771 |
|
33772 |
|
33773 |
|
33774 |
|
33775 |
|
33776 |
|
33777 |
|
33778 |
|
33779 |
|
33780 |
|
33781 |
|
33782 |
|
33783 |
|
33784 |
|
33785 |
|
33786 | var zeros = [
|
33787 | '',
|
33788 | '0',
|
33789 | '00',
|
33790 | '000',
|
33791 | '0000',
|
33792 | '00000',
|
33793 | '000000',
|
33794 | '0000000',
|
33795 | '00000000',
|
33796 | '000000000',
|
33797 | '0000000000',
|
33798 | '00000000000',
|
33799 | '000000000000',
|
33800 | '0000000000000',
|
33801 | '00000000000000',
|
33802 | '000000000000000',
|
33803 | '0000000000000000',
|
33804 | '00000000000000000',
|
33805 | '000000000000000000',
|
33806 | '0000000000000000000',
|
33807 | '00000000000000000000',
|
33808 | '000000000000000000000',
|
33809 | '0000000000000000000000',
|
33810 | '00000000000000000000000',
|
33811 | '000000000000000000000000',
|
33812 | '0000000000000000000000000'
|
33813 | ];
|
33814 |
|
33815 | var groupSizes = [
|
33816 | 0, 0,
|
33817 | 25, 16, 12, 11, 10, 9, 8,
|
33818 | 8, 7, 7, 7, 7, 6, 6,
|
33819 | 6, 6, 6, 6, 6, 5, 5,
|
33820 | 5, 5, 5, 5, 5, 5, 5,
|
33821 | 5, 5, 5, 5, 5, 5, 5
|
33822 | ];
|
33823 |
|
33824 | var groupBases = [
|
33825 | 0, 0,
|
33826 | 33554432, 43046721, 16777216, 48828125, 60466176, 40353607, 16777216,
|
33827 | 43046721, 10000000, 19487171, 35831808, 62748517, 7529536, 11390625,
|
33828 | 16777216, 24137569, 34012224, 47045881, 64000000, 4084101, 5153632,
|
33829 | 6436343, 7962624, 9765625, 11881376, 14348907, 17210368, 20511149,
|
33830 | 24300000, 28629151, 33554432, 39135393, 45435424, 52521875, 60466176
|
33831 | ];
|
33832 |
|
33833 | BN.prototype.toString = function toString (base, padding) {
|
33834 | base = base || 10;
|
33835 | padding = padding | 0 || 1;
|
33836 |
|
33837 | var out;
|
33838 | if (base === 16 || base === 'hex') {
|
33839 | out = '';
|
33840 | var off = 0;
|
33841 | var carry = 0;
|
33842 | for (var i = 0; i < this.length; i++) {
|
33843 | var w = this.words[i];
|
33844 | var word = (((w << off) | carry) & 0xffffff).toString(16);
|
33845 | carry = (w >>> (24 - off)) & 0xffffff;
|
33846 | if (carry !== 0 || i !== this.length - 1) {
|
33847 | out = zeros[6 - word.length] + word + out;
|
33848 | } else {
|
33849 | out = word + out;
|
33850 | }
|
33851 | off += 2;
|
33852 | if (off >= 26) {
|
33853 | off -= 26;
|
33854 | i--;
|
33855 | }
|
33856 | }
|
33857 | if (carry !== 0) {
|
33858 | out = carry.toString(16) + out;
|
33859 | }
|
33860 | while (out.length % padding !== 0) {
|
33861 | out = '0' + out;
|
33862 | }
|
33863 | if (this.negative !== 0) {
|
33864 | out = '-' + out;
|
33865 | }
|
33866 | return out;
|
33867 | }
|
33868 |
|
33869 | if (base === (base | 0) && base >= 2 && base <= 36) {
|
33870 |
|
33871 | var groupSize = groupSizes[base];
|
33872 |
|
33873 | var groupBase = groupBases[base];
|
33874 | out = '';
|
33875 | var c = this.clone();
|
33876 | c.negative = 0;
|
33877 | while (!c.isZero()) {
|
33878 | var r = c.modn(groupBase).toString(base);
|
33879 | c = c.idivn(groupBase);
|
33880 |
|
33881 | if (!c.isZero()) {
|
33882 | out = zeros[groupSize - r.length] + r + out;
|
33883 | } else {
|
33884 | out = r + out;
|
33885 | }
|
33886 | }
|
33887 | if (this.isZero()) {
|
33888 | out = '0' + out;
|
33889 | }
|
33890 | while (out.length % padding !== 0) {
|
33891 | out = '0' + out;
|
33892 | }
|
33893 | if (this.negative !== 0) {
|
33894 | out = '-' + out;
|
33895 | }
|
33896 | return out;
|
33897 | }
|
33898 |
|
33899 | assert(false, 'Base should be between 2 and 36');
|
33900 | };
|
33901 |
|
33902 | BN.prototype.toNumber = function toNumber () {
|
33903 | var ret = this.words[0];
|
33904 | if (this.length === 2) {
|
33905 | ret += this.words[1] * 0x4000000;
|
33906 | } else if (this.length === 3 && this.words[2] === 0x01) {
|
33907 |
|
33908 | ret += 0x10000000000000 + (this.words[1] * 0x4000000);
|
33909 | } else if (this.length > 2) {
|
33910 | assert(false, 'Number can only safely store up to 53 bits');
|
33911 | }
|
33912 | return (this.negative !== 0) ? -ret : ret;
|
33913 | };
|
33914 |
|
33915 | BN.prototype.toJSON = function toJSON () {
|
33916 | return this.toString(16);
|
33917 | };
|
33918 |
|
33919 | BN.prototype.toBuffer = function toBuffer (endian, length) {
|
33920 | assert(typeof Buffer !== 'undefined');
|
33921 | return this.toArrayLike(Buffer, endian, length);
|
33922 | };
|
33923 |
|
33924 | BN.prototype.toArray = function toArray (endian, length) {
|
33925 | return this.toArrayLike(Array, endian, length);
|
33926 | };
|
33927 |
|
33928 | BN.prototype.toArrayLike = function toArrayLike (ArrayType, endian, length) {
|
33929 | var byteLength = this.byteLength();
|
33930 | var reqLength = length || Math.max(1, byteLength);
|
33931 | assert(byteLength <= reqLength, 'byte array longer than desired length');
|
33932 | assert(reqLength > 0, 'Requested array length <= 0');
|
33933 |
|
33934 | this.strip();
|
33935 | var littleEndian = endian === 'le';
|
33936 | var res = new ArrayType(reqLength);
|
33937 |
|
33938 | var b, i;
|
33939 | var q = this.clone();
|
33940 | if (!littleEndian) {
|
33941 |
|
33942 | for (i = 0; i < reqLength - byteLength; i++) {
|
33943 | res[i] = 0;
|
33944 | }
|
33945 |
|
33946 | for (i = 0; !q.isZero(); i++) {
|
33947 | b = q.andln(0xff);
|
33948 | q.iushrn(8);
|
33949 |
|
33950 | res[reqLength - i - 1] = b;
|
33951 | }
|
33952 | } else {
|
33953 | for (i = 0; !q.isZero(); i++) {
|
33954 | b = q.andln(0xff);
|
33955 | q.iushrn(8);
|
33956 |
|
33957 | res[i] = b;
|
33958 | }
|
33959 |
|
33960 | for (; i < reqLength; i++) {
|
33961 | res[i] = 0;
|
33962 | }
|
33963 | }
|
33964 |
|
33965 | return res;
|
33966 | };
|
33967 |
|
33968 | if (Math.clz32) {
|
33969 | BN.prototype._countBits = function _countBits (w) {
|
33970 | return 32 - Math.clz32(w);
|
33971 | };
|
33972 | } else {
|
33973 | BN.prototype._countBits = function _countBits (w) {
|
33974 | var t = w;
|
33975 | var r = 0;
|
33976 | if (t >= 0x1000) {
|
33977 | r += 13;
|
33978 | t >>>= 13;
|
33979 | }
|
33980 | if (t >= 0x40) {
|
33981 | r += 7;
|
33982 | t >>>= 7;
|
33983 | }
|
33984 | if (t >= 0x8) {
|
33985 | r += 4;
|
33986 | t >>>= 4;
|
33987 | }
|
33988 | if (t >= 0x02) {
|
33989 | r += 2;
|
33990 | t >>>= 2;
|
33991 | }
|
33992 | return r + t;
|
33993 | };
|
33994 | }
|
33995 |
|
33996 | BN.prototype._zeroBits = function _zeroBits (w) {
|
33997 |
|
33998 | if (w === 0) return 26;
|
33999 |
|
34000 | var t = w;
|
34001 | var r = 0;
|
34002 | if ((t & 0x1fff) === 0) {
|
34003 | r += 13;
|
34004 | t >>>= 13;
|
34005 | }
|
34006 | if ((t & 0x7f) === 0) {
|
34007 | r += 7;
|
34008 | t >>>= 7;
|
34009 | }
|
34010 | if ((t & 0xf) === 0) {
|
34011 | r += 4;
|
34012 | t >>>= 4;
|
34013 | }
|
34014 | if ((t & 0x3) === 0) {
|
34015 | r += 2;
|
34016 | t >>>= 2;
|
34017 | }
|
34018 | if ((t & 0x1) === 0) {
|
34019 | r++;
|
34020 | }
|
34021 | return r;
|
34022 | };
|
34023 |
|
34024 |
|
34025 | BN.prototype.bitLength = function bitLength () {
|
34026 | var w = this.words[this.length - 1];
|
34027 | var hi = this._countBits(w);
|
34028 | return (this.length - 1) * 26 + hi;
|
34029 | };
|
34030 |
|
34031 | function toBitArray (num) {
|
34032 | var w = new Array(num.bitLength());
|
34033 |
|
34034 | for (var bit = 0; bit < w.length; bit++) {
|
34035 | var off = (bit / 26) | 0;
|
34036 | var wbit = bit % 26;
|
34037 |
|
34038 | w[bit] = (num.words[off] & (1 << wbit)) >>> wbit;
|
34039 | }
|
34040 |
|
34041 | return w;
|
34042 | }
|
34043 |
|
34044 |
|
34045 | BN.prototype.zeroBits = function zeroBits () {
|
34046 | if (this.isZero()) return 0;
|
34047 |
|
34048 | var r = 0;
|
34049 | for (var i = 0; i < this.length; i++) {
|
34050 | var b = this._zeroBits(this.words[i]);
|
34051 | r += b;
|
34052 | if (b !== 26) break;
|
34053 | }
|
34054 | return r;
|
34055 | };
|
34056 |
|
34057 | BN.prototype.byteLength = function byteLength () {
|
34058 | return Math.ceil(this.bitLength() / 8);
|
34059 | };
|
34060 |
|
34061 | BN.prototype.toTwos = function toTwos (width) {
|
34062 | if (this.negative !== 0) {
|
34063 | return this.abs().inotn(width).iaddn(1);
|
34064 | }
|
34065 | return this.clone();
|
34066 | };
|
34067 |
|
34068 | BN.prototype.fromTwos = function fromTwos (width) {
|
34069 | if (this.testn(width - 1)) {
|
34070 | return this.notn(width).iaddn(1).ineg();
|
34071 | }
|
34072 | return this.clone();
|
34073 | };
|
34074 |
|
34075 | BN.prototype.isNeg = function isNeg () {
|
34076 | return this.negative !== 0;
|
34077 | };
|
34078 |
|
34079 |
|
34080 | BN.prototype.neg = function neg () {
|
34081 | return this.clone().ineg();
|
34082 | };
|
34083 |
|
34084 | BN.prototype.ineg = function ineg () {
|
34085 | if (!this.isZero()) {
|
34086 | this.negative ^= 1;
|
34087 | }
|
34088 |
|
34089 | return this;
|
34090 | };
|
34091 |
|
34092 |
|
34093 | BN.prototype.iuor = function iuor (num) {
|
34094 | while (this.length < num.length) {
|
34095 | this.words[this.length++] = 0;
|
34096 | }
|
34097 |
|
34098 | for (var i = 0; i < num.length; i++) {
|
34099 | this.words[i] = this.words[i] | num.words[i];
|
34100 | }
|
34101 |
|
34102 | return this.strip();
|
34103 | };
|
34104 |
|
34105 | BN.prototype.ior = function ior (num) {
|
34106 | assert((this.negative | num.negative) === 0);
|
34107 | return this.iuor(num);
|
34108 | };
|
34109 |
|
34110 |
|
34111 | BN.prototype.or = function or (num) {
|
34112 | if (this.length > num.length) return this.clone().ior(num);
|
34113 | return num.clone().ior(this);
|
34114 | };
|
34115 |
|
34116 | BN.prototype.uor = function uor (num) {
|
34117 | if (this.length > num.length) return this.clone().iuor(num);
|
34118 | return num.clone().iuor(this);
|
34119 | };
|
34120 |
|
34121 |
|
34122 | BN.prototype.iuand = function iuand (num) {
|
34123 |
|
34124 | var b;
|
34125 | if (this.length > num.length) {
|
34126 | b = num;
|
34127 | } else {
|
34128 | b = this;
|
34129 | }
|
34130 |
|
34131 | for (var i = 0; i < b.length; i++) {
|
34132 | this.words[i] = this.words[i] & num.words[i];
|
34133 | }
|
34134 |
|
34135 | this.length = b.length;
|
34136 |
|
34137 | return this.strip();
|
34138 | };
|
34139 |
|
34140 | BN.prototype.iand = function iand (num) {
|
34141 | assert((this.negative | num.negative) === 0);
|
34142 | return this.iuand(num);
|
34143 | };
|
34144 |
|
34145 |
|
34146 | BN.prototype.and = function and (num) {
|
34147 | if (this.length > num.length) return this.clone().iand(num);
|
34148 | return num.clone().iand(this);
|
34149 | };
|
34150 |
|
34151 | BN.prototype.uand = function uand (num) {
|
34152 | if (this.length > num.length) return this.clone().iuand(num);
|
34153 | return num.clone().iuand(this);
|
34154 | };
|
34155 |
|
34156 |
|
34157 | BN.prototype.iuxor = function iuxor (num) {
|
34158 |
|
34159 | var a;
|
34160 | var b;
|
34161 | if (this.length > num.length) {
|
34162 | a = this;
|
34163 | b = num;
|
34164 | } else {
|
34165 | a = num;
|
34166 | b = this;
|
34167 | }
|
34168 |
|
34169 | for (var i = 0; i < b.length; i++) {
|
34170 | this.words[i] = a.words[i] ^ b.words[i];
|
34171 | }
|
34172 |
|
34173 | if (this !== a) {
|
34174 | for (; i < a.length; i++) {
|
34175 | this.words[i] = a.words[i];
|
34176 | }
|
34177 | }
|
34178 |
|
34179 | this.length = a.length;
|
34180 |
|
34181 | return this.strip();
|
34182 | };
|
34183 |
|
34184 | BN.prototype.ixor = function ixor (num) {
|
34185 | assert((this.negative | num.negative) === 0);
|
34186 | return this.iuxor(num);
|
34187 | };
|
34188 |
|
34189 |
|
34190 | BN.prototype.xor = function xor (num) {
|
34191 | if (this.length > num.length) return this.clone().ixor(num);
|
34192 | return num.clone().ixor(this);
|
34193 | };
|
34194 |
|
34195 | BN.prototype.uxor = function uxor (num) {
|
34196 | if (this.length > num.length) return this.clone().iuxor(num);
|
34197 | return num.clone().iuxor(this);
|
34198 | };
|
34199 |
|
34200 |
|
34201 | BN.prototype.inotn = function inotn (width) {
|
34202 | assert(typeof width === 'number' && width >= 0);
|
34203 |
|
34204 | var bytesNeeded = Math.ceil(width / 26) | 0;
|
34205 | var bitsLeft = width % 26;
|
34206 |
|
34207 |
|
34208 | this._expand(bytesNeeded);
|
34209 |
|
34210 | if (bitsLeft > 0) {
|
34211 | bytesNeeded--;
|
34212 | }
|
34213 |
|
34214 |
|
34215 | for (var i = 0; i < bytesNeeded; i++) {
|
34216 | this.words[i] = ~this.words[i] & 0x3ffffff;
|
34217 | }
|
34218 |
|
34219 |
|
34220 | if (bitsLeft > 0) {
|
34221 | this.words[i] = ~this.words[i] & (0x3ffffff >> (26 - bitsLeft));
|
34222 | }
|
34223 |
|
34224 |
|
34225 | return this.strip();
|
34226 | };
|
34227 |
|
34228 | BN.prototype.notn = function notn (width) {
|
34229 | return this.clone().inotn(width);
|
34230 | };
|
34231 |
|
34232 |
|
34233 | BN.prototype.setn = function setn (bit, val) {
|
34234 | assert(typeof bit === 'number' && bit >= 0);
|
34235 |
|
34236 | var off = (bit / 26) | 0;
|
34237 | var wbit = bit % 26;
|
34238 |
|
34239 | this._expand(off + 1);
|
34240 |
|
34241 | if (val) {
|
34242 | this.words[off] = this.words[off] | (1 << wbit);
|
34243 | } else {
|
34244 | this.words[off] = this.words[off] & ~(1 << wbit);
|
34245 | }
|
34246 |
|
34247 | return this.strip();
|
34248 | };
|
34249 |
|
34250 |
|
34251 | BN.prototype.iadd = function iadd (num) {
|
34252 | var r;
|
34253 |
|
34254 |
|
34255 | if (this.negative !== 0 && num.negative === 0) {
|
34256 | this.negative = 0;
|
34257 | r = this.isub(num);
|
34258 | this.negative ^= 1;
|
34259 | return this._normSign();
|
34260 |
|
34261 |
|
34262 | } else if (this.negative === 0 && num.negative !== 0) {
|
34263 | num.negative = 0;
|
34264 | r = this.isub(num);
|
34265 | num.negative = 1;
|
34266 | return r._normSign();
|
34267 | }
|
34268 |
|
34269 |
|
34270 | var a, b;
|
34271 | if (this.length > num.length) {
|
34272 | a = this;
|
34273 | b = num;
|
34274 | } else {
|
34275 | a = num;
|
34276 | b = this;
|
34277 | }
|
34278 |
|
34279 | var carry = 0;
|
34280 | for (var i = 0; i < b.length; i++) {
|
34281 | r = (a.words[i] | 0) + (b.words[i] | 0) + carry;
|
34282 | this.words[i] = r & 0x3ffffff;
|
34283 | carry = r >>> 26;
|
34284 | }
|
34285 | for (; carry !== 0 && i < a.length; i++) {
|
34286 | r = (a.words[i] | 0) + carry;
|
34287 | this.words[i] = r & 0x3ffffff;
|
34288 | carry = r >>> 26;
|
34289 | }
|
34290 |
|
34291 | this.length = a.length;
|
34292 | if (carry !== 0) {
|
34293 | this.words[this.length] = carry;
|
34294 | this.length++;
|
34295 |
|
34296 | } else if (a !== this) {
|
34297 | for (; i < a.length; i++) {
|
34298 | this.words[i] = a.words[i];
|
34299 | }
|
34300 | }
|
34301 |
|
34302 | return this;
|
34303 | };
|
34304 |
|
34305 |
|
34306 | BN.prototype.add = function add (num) {
|
34307 | var res;
|
34308 | if (num.negative !== 0 && this.negative === 0) {
|
34309 | num.negative = 0;
|
34310 | res = this.sub(num);
|
34311 | num.negative ^= 1;
|
34312 | return res;
|
34313 | } else if (num.negative === 0 && this.negative !== 0) {
|
34314 | this.negative = 0;
|
34315 | res = num.sub(this);
|
34316 | this.negative = 1;
|
34317 | return res;
|
34318 | }
|
34319 |
|
34320 | if (this.length > num.length) return this.clone().iadd(num);
|
34321 |
|
34322 | return num.clone().iadd(this);
|
34323 | };
|
34324 |
|
34325 |
|
34326 | BN.prototype.isub = function isub (num) {
|
34327 |
|
34328 | if (num.negative !== 0) {
|
34329 | num.negative = 0;
|
34330 | var r = this.iadd(num);
|
34331 | num.negative = 1;
|
34332 | return r._normSign();
|
34333 |
|
34334 |
|
34335 | } else if (this.negative !== 0) {
|
34336 | this.negative = 0;
|
34337 | this.iadd(num);
|
34338 | this.negative = 1;
|
34339 | return this._normSign();
|
34340 | }
|
34341 |
|
34342 |
|
34343 | var cmp = this.cmp(num);
|
34344 |
|
34345 |
|
34346 | if (cmp === 0) {
|
34347 | this.negative = 0;
|
34348 | this.length = 1;
|
34349 | this.words[0] = 0;
|
34350 | return this;
|
34351 | }
|
34352 |
|
34353 |
|
34354 | var a, b;
|
34355 | if (cmp > 0) {
|
34356 | a = this;
|
34357 | b = num;
|
34358 | } else {
|
34359 | a = num;
|
34360 | b = this;
|
34361 | }
|
34362 |
|
34363 | var carry = 0;
|
34364 | for (var i = 0; i < b.length; i++) {
|
34365 | r = (a.words[i] | 0) - (b.words[i] | 0) + carry;
|
34366 | carry = r >> 26;
|
34367 | this.words[i] = r & 0x3ffffff;
|
34368 | }
|
34369 | for (; carry !== 0 && i < a.length; i++) {
|
34370 | r = (a.words[i] | 0) + carry;
|
34371 | carry = r >> 26;
|
34372 | this.words[i] = r & 0x3ffffff;
|
34373 | }
|
34374 |
|
34375 |
|
34376 | if (carry === 0 && i < a.length && a !== this) {
|
34377 | for (; i < a.length; i++) {
|
34378 | this.words[i] = a.words[i];
|
34379 | }
|
34380 | }
|
34381 |
|
34382 | this.length = Math.max(this.length, i);
|
34383 |
|
34384 | if (a !== this) {
|
34385 | this.negative = 1;
|
34386 | }
|
34387 |
|
34388 | return this.strip();
|
34389 | };
|
34390 |
|
34391 |
|
34392 | BN.prototype.sub = function sub (num) {
|
34393 | return this.clone().isub(num);
|
34394 | };
|
34395 |
|
34396 | function smallMulTo (self, num, out) {
|
34397 | out.negative = num.negative ^ self.negative;
|
34398 | var len = (self.length + num.length) | 0;
|
34399 | out.length = len;
|
34400 | len = (len - 1) | 0;
|
34401 |
|
34402 |
|
34403 | var a = self.words[0] | 0;
|
34404 | var b = num.words[0] | 0;
|
34405 | var r = a * b;
|
34406 |
|
34407 | var lo = r & 0x3ffffff;
|
34408 | var carry = (r / 0x4000000) | 0;
|
34409 | out.words[0] = lo;
|
34410 |
|
34411 | for (var k = 1; k < len; k++) {
|
34412 |
|
34413 |
|
34414 | var ncarry = carry >>> 26;
|
34415 | var rword = carry & 0x3ffffff;
|
34416 | var maxJ = Math.min(k, num.length - 1);
|
34417 | for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
|
34418 | var i = (k - j) | 0;
|
34419 | a = self.words[i] | 0;
|
34420 | b = num.words[j] | 0;
|
34421 | r = a * b + rword;
|
34422 | ncarry += (r / 0x4000000) | 0;
|
34423 | rword = r & 0x3ffffff;
|
34424 | }
|
34425 | out.words[k] = rword | 0;
|
34426 | carry = ncarry | 0;
|
34427 | }
|
34428 | if (carry !== 0) {
|
34429 | out.words[k] = carry | 0;
|
34430 | } else {
|
34431 | out.length--;
|
34432 | }
|
34433 |
|
34434 | return out.strip();
|
34435 | }
|
34436 |
|
34437 |
|
34438 |
|
34439 |
|
34440 | var comb10MulTo = function comb10MulTo (self, num, out) {
|
34441 | var a = self.words;
|
34442 | var b = num.words;
|
34443 | var o = out.words;
|
34444 | var c = 0;
|
34445 | var lo;
|
34446 | var mid;
|
34447 | var hi;
|
34448 | var a0 = a[0] | 0;
|
34449 | var al0 = a0 & 0x1fff;
|
34450 | var ah0 = a0 >>> 13;
|
34451 | var a1 = a[1] | 0;
|
34452 | var al1 = a1 & 0x1fff;
|
34453 | var ah1 = a1 >>> 13;
|
34454 | var a2 = a[2] | 0;
|
34455 | var al2 = a2 & 0x1fff;
|
34456 | var ah2 = a2 >>> 13;
|
34457 | var a3 = a[3] | 0;
|
34458 | var al3 = a3 & 0x1fff;
|
34459 | var ah3 = a3 >>> 13;
|
34460 | var a4 = a[4] | 0;
|
34461 | var al4 = a4 & 0x1fff;
|
34462 | var ah4 = a4 >>> 13;
|
34463 | var a5 = a[5] | 0;
|
34464 | var al5 = a5 & 0x1fff;
|
34465 | var ah5 = a5 >>> 13;
|
34466 | var a6 = a[6] | 0;
|
34467 | var al6 = a6 & 0x1fff;
|
34468 | var ah6 = a6 >>> 13;
|
34469 | var a7 = a[7] | 0;
|
34470 | var al7 = a7 & 0x1fff;
|
34471 | var ah7 = a7 >>> 13;
|
34472 | var a8 = a[8] | 0;
|
34473 | var al8 = a8 & 0x1fff;
|
34474 | var ah8 = a8 >>> 13;
|
34475 | var a9 = a[9] | 0;
|
34476 | var al9 = a9 & 0x1fff;
|
34477 | var ah9 = a9 >>> 13;
|
34478 | var b0 = b[0] | 0;
|
34479 | var bl0 = b0 & 0x1fff;
|
34480 | var bh0 = b0 >>> 13;
|
34481 | var b1 = b[1] | 0;
|
34482 | var bl1 = b1 & 0x1fff;
|
34483 | var bh1 = b1 >>> 13;
|
34484 | var b2 = b[2] | 0;
|
34485 | var bl2 = b2 & 0x1fff;
|
34486 | var bh2 = b2 >>> 13;
|
34487 | var b3 = b[3] | 0;
|
34488 | var bl3 = b3 & 0x1fff;
|
34489 | var bh3 = b3 >>> 13;
|
34490 | var b4 = b[4] | 0;
|
34491 | var bl4 = b4 & 0x1fff;
|
34492 | var bh4 = b4 >>> 13;
|
34493 | var b5 = b[5] | 0;
|
34494 | var bl5 = b5 & 0x1fff;
|
34495 | var bh5 = b5 >>> 13;
|
34496 | var b6 = b[6] | 0;
|
34497 | var bl6 = b6 & 0x1fff;
|
34498 | var bh6 = b6 >>> 13;
|
34499 | var b7 = b[7] | 0;
|
34500 | var bl7 = b7 & 0x1fff;
|
34501 | var bh7 = b7 >>> 13;
|
34502 | var b8 = b[8] | 0;
|
34503 | var bl8 = b8 & 0x1fff;
|
34504 | var bh8 = b8 >>> 13;
|
34505 | var b9 = b[9] | 0;
|
34506 | var bl9 = b9 & 0x1fff;
|
34507 | var bh9 = b9 >>> 13;
|
34508 |
|
34509 | out.negative = self.negative ^ num.negative;
|
34510 | out.length = 19;
|
34511 |
|
34512 | lo = Math.imul(al0, bl0);
|
34513 | mid = Math.imul(al0, bh0);
|
34514 | mid = (mid + Math.imul(ah0, bl0)) | 0;
|
34515 | hi = Math.imul(ah0, bh0);
|
34516 | var w0 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34517 | c = (((hi + (mid >>> 13)) | 0) + (w0 >>> 26)) | 0;
|
34518 | w0 &= 0x3ffffff;
|
34519 |
|
34520 | lo = Math.imul(al1, bl0);
|
34521 | mid = Math.imul(al1, bh0);
|
34522 | mid = (mid + Math.imul(ah1, bl0)) | 0;
|
34523 | hi = Math.imul(ah1, bh0);
|
34524 | lo = (lo + Math.imul(al0, bl1)) | 0;
|
34525 | mid = (mid + Math.imul(al0, bh1)) | 0;
|
34526 | mid = (mid + Math.imul(ah0, bl1)) | 0;
|
34527 | hi = (hi + Math.imul(ah0, bh1)) | 0;
|
34528 | var w1 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34529 | c = (((hi + (mid >>> 13)) | 0) + (w1 >>> 26)) | 0;
|
34530 | w1 &= 0x3ffffff;
|
34531 |
|
34532 | lo = Math.imul(al2, bl0);
|
34533 | mid = Math.imul(al2, bh0);
|
34534 | mid = (mid + Math.imul(ah2, bl0)) | 0;
|
34535 | hi = Math.imul(ah2, bh0);
|
34536 | lo = (lo + Math.imul(al1, bl1)) | 0;
|
34537 | mid = (mid + Math.imul(al1, bh1)) | 0;
|
34538 | mid = (mid + Math.imul(ah1, bl1)) | 0;
|
34539 | hi = (hi + Math.imul(ah1, bh1)) | 0;
|
34540 | lo = (lo + Math.imul(al0, bl2)) | 0;
|
34541 | mid = (mid + Math.imul(al0, bh2)) | 0;
|
34542 | mid = (mid + Math.imul(ah0, bl2)) | 0;
|
34543 | hi = (hi + Math.imul(ah0, bh2)) | 0;
|
34544 | var w2 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34545 | c = (((hi + (mid >>> 13)) | 0) + (w2 >>> 26)) | 0;
|
34546 | w2 &= 0x3ffffff;
|
34547 |
|
34548 | lo = Math.imul(al3, bl0);
|
34549 | mid = Math.imul(al3, bh0);
|
34550 | mid = (mid + Math.imul(ah3, bl0)) | 0;
|
34551 | hi = Math.imul(ah3, bh0);
|
34552 | lo = (lo + Math.imul(al2, bl1)) | 0;
|
34553 | mid = (mid + Math.imul(al2, bh1)) | 0;
|
34554 | mid = (mid + Math.imul(ah2, bl1)) | 0;
|
34555 | hi = (hi + Math.imul(ah2, bh1)) | 0;
|
34556 | lo = (lo + Math.imul(al1, bl2)) | 0;
|
34557 | mid = (mid + Math.imul(al1, bh2)) | 0;
|
34558 | mid = (mid + Math.imul(ah1, bl2)) | 0;
|
34559 | hi = (hi + Math.imul(ah1, bh2)) | 0;
|
34560 | lo = (lo + Math.imul(al0, bl3)) | 0;
|
34561 | mid = (mid + Math.imul(al0, bh3)) | 0;
|
34562 | mid = (mid + Math.imul(ah0, bl3)) | 0;
|
34563 | hi = (hi + Math.imul(ah0, bh3)) | 0;
|
34564 | var w3 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34565 | c = (((hi + (mid >>> 13)) | 0) + (w3 >>> 26)) | 0;
|
34566 | w3 &= 0x3ffffff;
|
34567 |
|
34568 | lo = Math.imul(al4, bl0);
|
34569 | mid = Math.imul(al4, bh0);
|
34570 | mid = (mid + Math.imul(ah4, bl0)) | 0;
|
34571 | hi = Math.imul(ah4, bh0);
|
34572 | lo = (lo + Math.imul(al3, bl1)) | 0;
|
34573 | mid = (mid + Math.imul(al3, bh1)) | 0;
|
34574 | mid = (mid + Math.imul(ah3, bl1)) | 0;
|
34575 | hi = (hi + Math.imul(ah3, bh1)) | 0;
|
34576 | lo = (lo + Math.imul(al2, bl2)) | 0;
|
34577 | mid = (mid + Math.imul(al2, bh2)) | 0;
|
34578 | mid = (mid + Math.imul(ah2, bl2)) | 0;
|
34579 | hi = (hi + Math.imul(ah2, bh2)) | 0;
|
34580 | lo = (lo + Math.imul(al1, bl3)) | 0;
|
34581 | mid = (mid + Math.imul(al1, bh3)) | 0;
|
34582 | mid = (mid + Math.imul(ah1, bl3)) | 0;
|
34583 | hi = (hi + Math.imul(ah1, bh3)) | 0;
|
34584 | lo = (lo + Math.imul(al0, bl4)) | 0;
|
34585 | mid = (mid + Math.imul(al0, bh4)) | 0;
|
34586 | mid = (mid + Math.imul(ah0, bl4)) | 0;
|
34587 | hi = (hi + Math.imul(ah0, bh4)) | 0;
|
34588 | var w4 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34589 | c = (((hi + (mid >>> 13)) | 0) + (w4 >>> 26)) | 0;
|
34590 | w4 &= 0x3ffffff;
|
34591 |
|
34592 | lo = Math.imul(al5, bl0);
|
34593 | mid = Math.imul(al5, bh0);
|
34594 | mid = (mid + Math.imul(ah5, bl0)) | 0;
|
34595 | hi = Math.imul(ah5, bh0);
|
34596 | lo = (lo + Math.imul(al4, bl1)) | 0;
|
34597 | mid = (mid + Math.imul(al4, bh1)) | 0;
|
34598 | mid = (mid + Math.imul(ah4, bl1)) | 0;
|
34599 | hi = (hi + Math.imul(ah4, bh1)) | 0;
|
34600 | lo = (lo + Math.imul(al3, bl2)) | 0;
|
34601 | mid = (mid + Math.imul(al3, bh2)) | 0;
|
34602 | mid = (mid + Math.imul(ah3, bl2)) | 0;
|
34603 | hi = (hi + Math.imul(ah3, bh2)) | 0;
|
34604 | lo = (lo + Math.imul(al2, bl3)) | 0;
|
34605 | mid = (mid + Math.imul(al2, bh3)) | 0;
|
34606 | mid = (mid + Math.imul(ah2, bl3)) | 0;
|
34607 | hi = (hi + Math.imul(ah2, bh3)) | 0;
|
34608 | lo = (lo + Math.imul(al1, bl4)) | 0;
|
34609 | mid = (mid + Math.imul(al1, bh4)) | 0;
|
34610 | mid = (mid + Math.imul(ah1, bl4)) | 0;
|
34611 | hi = (hi + Math.imul(ah1, bh4)) | 0;
|
34612 | lo = (lo + Math.imul(al0, bl5)) | 0;
|
34613 | mid = (mid + Math.imul(al0, bh5)) | 0;
|
34614 | mid = (mid + Math.imul(ah0, bl5)) | 0;
|
34615 | hi = (hi + Math.imul(ah0, bh5)) | 0;
|
34616 | var w5 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34617 | c = (((hi + (mid >>> 13)) | 0) + (w5 >>> 26)) | 0;
|
34618 | w5 &= 0x3ffffff;
|
34619 |
|
34620 | lo = Math.imul(al6, bl0);
|
34621 | mid = Math.imul(al6, bh0);
|
34622 | mid = (mid + Math.imul(ah6, bl0)) | 0;
|
34623 | hi = Math.imul(ah6, bh0);
|
34624 | lo = (lo + Math.imul(al5, bl1)) | 0;
|
34625 | mid = (mid + Math.imul(al5, bh1)) | 0;
|
34626 | mid = (mid + Math.imul(ah5, bl1)) | 0;
|
34627 | hi = (hi + Math.imul(ah5, bh1)) | 0;
|
34628 | lo = (lo + Math.imul(al4, bl2)) | 0;
|
34629 | mid = (mid + Math.imul(al4, bh2)) | 0;
|
34630 | mid = (mid + Math.imul(ah4, bl2)) | 0;
|
34631 | hi = (hi + Math.imul(ah4, bh2)) | 0;
|
34632 | lo = (lo + Math.imul(al3, bl3)) | 0;
|
34633 | mid = (mid + Math.imul(al3, bh3)) | 0;
|
34634 | mid = (mid + Math.imul(ah3, bl3)) | 0;
|
34635 | hi = (hi + Math.imul(ah3, bh3)) | 0;
|
34636 | lo = (lo + Math.imul(al2, bl4)) | 0;
|
34637 | mid = (mid + Math.imul(al2, bh4)) | 0;
|
34638 | mid = (mid + Math.imul(ah2, bl4)) | 0;
|
34639 | hi = (hi + Math.imul(ah2, bh4)) | 0;
|
34640 | lo = (lo + Math.imul(al1, bl5)) | 0;
|
34641 | mid = (mid + Math.imul(al1, bh5)) | 0;
|
34642 | mid = (mid + Math.imul(ah1, bl5)) | 0;
|
34643 | hi = (hi + Math.imul(ah1, bh5)) | 0;
|
34644 | lo = (lo + Math.imul(al0, bl6)) | 0;
|
34645 | mid = (mid + Math.imul(al0, bh6)) | 0;
|
34646 | mid = (mid + Math.imul(ah0, bl6)) | 0;
|
34647 | hi = (hi + Math.imul(ah0, bh6)) | 0;
|
34648 | var w6 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34649 | c = (((hi + (mid >>> 13)) | 0) + (w6 >>> 26)) | 0;
|
34650 | w6 &= 0x3ffffff;
|
34651 |
|
34652 | lo = Math.imul(al7, bl0);
|
34653 | mid = Math.imul(al7, bh0);
|
34654 | mid = (mid + Math.imul(ah7, bl0)) | 0;
|
34655 | hi = Math.imul(ah7, bh0);
|
34656 | lo = (lo + Math.imul(al6, bl1)) | 0;
|
34657 | mid = (mid + Math.imul(al6, bh1)) | 0;
|
34658 | mid = (mid + Math.imul(ah6, bl1)) | 0;
|
34659 | hi = (hi + Math.imul(ah6, bh1)) | 0;
|
34660 | lo = (lo + Math.imul(al5, bl2)) | 0;
|
34661 | mid = (mid + Math.imul(al5, bh2)) | 0;
|
34662 | mid = (mid + Math.imul(ah5, bl2)) | 0;
|
34663 | hi = (hi + Math.imul(ah5, bh2)) | 0;
|
34664 | lo = (lo + Math.imul(al4, bl3)) | 0;
|
34665 | mid = (mid + Math.imul(al4, bh3)) | 0;
|
34666 | mid = (mid + Math.imul(ah4, bl3)) | 0;
|
34667 | hi = (hi + Math.imul(ah4, bh3)) | 0;
|
34668 | lo = (lo + Math.imul(al3, bl4)) | 0;
|
34669 | mid = (mid + Math.imul(al3, bh4)) | 0;
|
34670 | mid = (mid + Math.imul(ah3, bl4)) | 0;
|
34671 | hi = (hi + Math.imul(ah3, bh4)) | 0;
|
34672 | lo = (lo + Math.imul(al2, bl5)) | 0;
|
34673 | mid = (mid + Math.imul(al2, bh5)) | 0;
|
34674 | mid = (mid + Math.imul(ah2, bl5)) | 0;
|
34675 | hi = (hi + Math.imul(ah2, bh5)) | 0;
|
34676 | lo = (lo + Math.imul(al1, bl6)) | 0;
|
34677 | mid = (mid + Math.imul(al1, bh6)) | 0;
|
34678 | mid = (mid + Math.imul(ah1, bl6)) | 0;
|
34679 | hi = (hi + Math.imul(ah1, bh6)) | 0;
|
34680 | lo = (lo + Math.imul(al0, bl7)) | 0;
|
34681 | mid = (mid + Math.imul(al0, bh7)) | 0;
|
34682 | mid = (mid + Math.imul(ah0, bl7)) | 0;
|
34683 | hi = (hi + Math.imul(ah0, bh7)) | 0;
|
34684 | var w7 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34685 | c = (((hi + (mid >>> 13)) | 0) + (w7 >>> 26)) | 0;
|
34686 | w7 &= 0x3ffffff;
|
34687 |
|
34688 | lo = Math.imul(al8, bl0);
|
34689 | mid = Math.imul(al8, bh0);
|
34690 | mid = (mid + Math.imul(ah8, bl0)) | 0;
|
34691 | hi = Math.imul(ah8, bh0);
|
34692 | lo = (lo + Math.imul(al7, bl1)) | 0;
|
34693 | mid = (mid + Math.imul(al7, bh1)) | 0;
|
34694 | mid = (mid + Math.imul(ah7, bl1)) | 0;
|
34695 | hi = (hi + Math.imul(ah7, bh1)) | 0;
|
34696 | lo = (lo + Math.imul(al6, bl2)) | 0;
|
34697 | mid = (mid + Math.imul(al6, bh2)) | 0;
|
34698 | mid = (mid + Math.imul(ah6, bl2)) | 0;
|
34699 | hi = (hi + Math.imul(ah6, bh2)) | 0;
|
34700 | lo = (lo + Math.imul(al5, bl3)) | 0;
|
34701 | mid = (mid + Math.imul(al5, bh3)) | 0;
|
34702 | mid = (mid + Math.imul(ah5, bl3)) | 0;
|
34703 | hi = (hi + Math.imul(ah5, bh3)) | 0;
|
34704 | lo = (lo + Math.imul(al4, bl4)) | 0;
|
34705 | mid = (mid + Math.imul(al4, bh4)) | 0;
|
34706 | mid = (mid + Math.imul(ah4, bl4)) | 0;
|
34707 | hi = (hi + Math.imul(ah4, bh4)) | 0;
|
34708 | lo = (lo + Math.imul(al3, bl5)) | 0;
|
34709 | mid = (mid + Math.imul(al3, bh5)) | 0;
|
34710 | mid = (mid + Math.imul(ah3, bl5)) | 0;
|
34711 | hi = (hi + Math.imul(ah3, bh5)) | 0;
|
34712 | lo = (lo + Math.imul(al2, bl6)) | 0;
|
34713 | mid = (mid + Math.imul(al2, bh6)) | 0;
|
34714 | mid = (mid + Math.imul(ah2, bl6)) | 0;
|
34715 | hi = (hi + Math.imul(ah2, bh6)) | 0;
|
34716 | lo = (lo + Math.imul(al1, bl7)) | 0;
|
34717 | mid = (mid + Math.imul(al1, bh7)) | 0;
|
34718 | mid = (mid + Math.imul(ah1, bl7)) | 0;
|
34719 | hi = (hi + Math.imul(ah1, bh7)) | 0;
|
34720 | lo = (lo + Math.imul(al0, bl8)) | 0;
|
34721 | mid = (mid + Math.imul(al0, bh8)) | 0;
|
34722 | mid = (mid + Math.imul(ah0, bl8)) | 0;
|
34723 | hi = (hi + Math.imul(ah0, bh8)) | 0;
|
34724 | var w8 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34725 | c = (((hi + (mid >>> 13)) | 0) + (w8 >>> 26)) | 0;
|
34726 | w8 &= 0x3ffffff;
|
34727 |
|
34728 | lo = Math.imul(al9, bl0);
|
34729 | mid = Math.imul(al9, bh0);
|
34730 | mid = (mid + Math.imul(ah9, bl0)) | 0;
|
34731 | hi = Math.imul(ah9, bh0);
|
34732 | lo = (lo + Math.imul(al8, bl1)) | 0;
|
34733 | mid = (mid + Math.imul(al8, bh1)) | 0;
|
34734 | mid = (mid + Math.imul(ah8, bl1)) | 0;
|
34735 | hi = (hi + Math.imul(ah8, bh1)) | 0;
|
34736 | lo = (lo + Math.imul(al7, bl2)) | 0;
|
34737 | mid = (mid + Math.imul(al7, bh2)) | 0;
|
34738 | mid = (mid + Math.imul(ah7, bl2)) | 0;
|
34739 | hi = (hi + Math.imul(ah7, bh2)) | 0;
|
34740 | lo = (lo + Math.imul(al6, bl3)) | 0;
|
34741 | mid = (mid + Math.imul(al6, bh3)) | 0;
|
34742 | mid = (mid + Math.imul(ah6, bl3)) | 0;
|
34743 | hi = (hi + Math.imul(ah6, bh3)) | 0;
|
34744 | lo = (lo + Math.imul(al5, bl4)) | 0;
|
34745 | mid = (mid + Math.imul(al5, bh4)) | 0;
|
34746 | mid = (mid + Math.imul(ah5, bl4)) | 0;
|
34747 | hi = (hi + Math.imul(ah5, bh4)) | 0;
|
34748 | lo = (lo + Math.imul(al4, bl5)) | 0;
|
34749 | mid = (mid + Math.imul(al4, bh5)) | 0;
|
34750 | mid = (mid + Math.imul(ah4, bl5)) | 0;
|
34751 | hi = (hi + Math.imul(ah4, bh5)) | 0;
|
34752 | lo = (lo + Math.imul(al3, bl6)) | 0;
|
34753 | mid = (mid + Math.imul(al3, bh6)) | 0;
|
34754 | mid = (mid + Math.imul(ah3, bl6)) | 0;
|
34755 | hi = (hi + Math.imul(ah3, bh6)) | 0;
|
34756 | lo = (lo + Math.imul(al2, bl7)) | 0;
|
34757 | mid = (mid + Math.imul(al2, bh7)) | 0;
|
34758 | mid = (mid + Math.imul(ah2, bl7)) | 0;
|
34759 | hi = (hi + Math.imul(ah2, bh7)) | 0;
|
34760 | lo = (lo + Math.imul(al1, bl8)) | 0;
|
34761 | mid = (mid + Math.imul(al1, bh8)) | 0;
|
34762 | mid = (mid + Math.imul(ah1, bl8)) | 0;
|
34763 | hi = (hi + Math.imul(ah1, bh8)) | 0;
|
34764 | lo = (lo + Math.imul(al0, bl9)) | 0;
|
34765 | mid = (mid + Math.imul(al0, bh9)) | 0;
|
34766 | mid = (mid + Math.imul(ah0, bl9)) | 0;
|
34767 | hi = (hi + Math.imul(ah0, bh9)) | 0;
|
34768 | var w9 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34769 | c = (((hi + (mid >>> 13)) | 0) + (w9 >>> 26)) | 0;
|
34770 | w9 &= 0x3ffffff;
|
34771 |
|
34772 | lo = Math.imul(al9, bl1);
|
34773 | mid = Math.imul(al9, bh1);
|
34774 | mid = (mid + Math.imul(ah9, bl1)) | 0;
|
34775 | hi = Math.imul(ah9, bh1);
|
34776 | lo = (lo + Math.imul(al8, bl2)) | 0;
|
34777 | mid = (mid + Math.imul(al8, bh2)) | 0;
|
34778 | mid = (mid + Math.imul(ah8, bl2)) | 0;
|
34779 | hi = (hi + Math.imul(ah8, bh2)) | 0;
|
34780 | lo = (lo + Math.imul(al7, bl3)) | 0;
|
34781 | mid = (mid + Math.imul(al7, bh3)) | 0;
|
34782 | mid = (mid + Math.imul(ah7, bl3)) | 0;
|
34783 | hi = (hi + Math.imul(ah7, bh3)) | 0;
|
34784 | lo = (lo + Math.imul(al6, bl4)) | 0;
|
34785 | mid = (mid + Math.imul(al6, bh4)) | 0;
|
34786 | mid = (mid + Math.imul(ah6, bl4)) | 0;
|
34787 | hi = (hi + Math.imul(ah6, bh4)) | 0;
|
34788 | lo = (lo + Math.imul(al5, bl5)) | 0;
|
34789 | mid = (mid + Math.imul(al5, bh5)) | 0;
|
34790 | mid = (mid + Math.imul(ah5, bl5)) | 0;
|
34791 | hi = (hi + Math.imul(ah5, bh5)) | 0;
|
34792 | lo = (lo + Math.imul(al4, bl6)) | 0;
|
34793 | mid = (mid + Math.imul(al4, bh6)) | 0;
|
34794 | mid = (mid + Math.imul(ah4, bl6)) | 0;
|
34795 | hi = (hi + Math.imul(ah4, bh6)) | 0;
|
34796 | lo = (lo + Math.imul(al3, bl7)) | 0;
|
34797 | mid = (mid + Math.imul(al3, bh7)) | 0;
|
34798 | mid = (mid + Math.imul(ah3, bl7)) | 0;
|
34799 | hi = (hi + Math.imul(ah3, bh7)) | 0;
|
34800 | lo = (lo + Math.imul(al2, bl8)) | 0;
|
34801 | mid = (mid + Math.imul(al2, bh8)) | 0;
|
34802 | mid = (mid + Math.imul(ah2, bl8)) | 0;
|
34803 | hi = (hi + Math.imul(ah2, bh8)) | 0;
|
34804 | lo = (lo + Math.imul(al1, bl9)) | 0;
|
34805 | mid = (mid + Math.imul(al1, bh9)) | 0;
|
34806 | mid = (mid + Math.imul(ah1, bl9)) | 0;
|
34807 | hi = (hi + Math.imul(ah1, bh9)) | 0;
|
34808 | var w10 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34809 | c = (((hi + (mid >>> 13)) | 0) + (w10 >>> 26)) | 0;
|
34810 | w10 &= 0x3ffffff;
|
34811 |
|
34812 | lo = Math.imul(al9, bl2);
|
34813 | mid = Math.imul(al9, bh2);
|
34814 | mid = (mid + Math.imul(ah9, bl2)) | 0;
|
34815 | hi = Math.imul(ah9, bh2);
|
34816 | lo = (lo + Math.imul(al8, bl3)) | 0;
|
34817 | mid = (mid + Math.imul(al8, bh3)) | 0;
|
34818 | mid = (mid + Math.imul(ah8, bl3)) | 0;
|
34819 | hi = (hi + Math.imul(ah8, bh3)) | 0;
|
34820 | lo = (lo + Math.imul(al7, bl4)) | 0;
|
34821 | mid = (mid + Math.imul(al7, bh4)) | 0;
|
34822 | mid = (mid + Math.imul(ah7, bl4)) | 0;
|
34823 | hi = (hi + Math.imul(ah7, bh4)) | 0;
|
34824 | lo = (lo + Math.imul(al6, bl5)) | 0;
|
34825 | mid = (mid + Math.imul(al6, bh5)) | 0;
|
34826 | mid = (mid + Math.imul(ah6, bl5)) | 0;
|
34827 | hi = (hi + Math.imul(ah6, bh5)) | 0;
|
34828 | lo = (lo + Math.imul(al5, bl6)) | 0;
|
34829 | mid = (mid + Math.imul(al5, bh6)) | 0;
|
34830 | mid = (mid + Math.imul(ah5, bl6)) | 0;
|
34831 | hi = (hi + Math.imul(ah5, bh6)) | 0;
|
34832 | lo = (lo + Math.imul(al4, bl7)) | 0;
|
34833 | mid = (mid + Math.imul(al4, bh7)) | 0;
|
34834 | mid = (mid + Math.imul(ah4, bl7)) | 0;
|
34835 | hi = (hi + Math.imul(ah4, bh7)) | 0;
|
34836 | lo = (lo + Math.imul(al3, bl8)) | 0;
|
34837 | mid = (mid + Math.imul(al3, bh8)) | 0;
|
34838 | mid = (mid + Math.imul(ah3, bl8)) | 0;
|
34839 | hi = (hi + Math.imul(ah3, bh8)) | 0;
|
34840 | lo = (lo + Math.imul(al2, bl9)) | 0;
|
34841 | mid = (mid + Math.imul(al2, bh9)) | 0;
|
34842 | mid = (mid + Math.imul(ah2, bl9)) | 0;
|
34843 | hi = (hi + Math.imul(ah2, bh9)) | 0;
|
34844 | var w11 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34845 | c = (((hi + (mid >>> 13)) | 0) + (w11 >>> 26)) | 0;
|
34846 | w11 &= 0x3ffffff;
|
34847 |
|
34848 | lo = Math.imul(al9, bl3);
|
34849 | mid = Math.imul(al9, bh3);
|
34850 | mid = (mid + Math.imul(ah9, bl3)) | 0;
|
34851 | hi = Math.imul(ah9, bh3);
|
34852 | lo = (lo + Math.imul(al8, bl4)) | 0;
|
34853 | mid = (mid + Math.imul(al8, bh4)) | 0;
|
34854 | mid = (mid + Math.imul(ah8, bl4)) | 0;
|
34855 | hi = (hi + Math.imul(ah8, bh4)) | 0;
|
34856 | lo = (lo + Math.imul(al7, bl5)) | 0;
|
34857 | mid = (mid + Math.imul(al7, bh5)) | 0;
|
34858 | mid = (mid + Math.imul(ah7, bl5)) | 0;
|
34859 | hi = (hi + Math.imul(ah7, bh5)) | 0;
|
34860 | lo = (lo + Math.imul(al6, bl6)) | 0;
|
34861 | mid = (mid + Math.imul(al6, bh6)) | 0;
|
34862 | mid = (mid + Math.imul(ah6, bl6)) | 0;
|
34863 | hi = (hi + Math.imul(ah6, bh6)) | 0;
|
34864 | lo = (lo + Math.imul(al5, bl7)) | 0;
|
34865 | mid = (mid + Math.imul(al5, bh7)) | 0;
|
34866 | mid = (mid + Math.imul(ah5, bl7)) | 0;
|
34867 | hi = (hi + Math.imul(ah5, bh7)) | 0;
|
34868 | lo = (lo + Math.imul(al4, bl8)) | 0;
|
34869 | mid = (mid + Math.imul(al4, bh8)) | 0;
|
34870 | mid = (mid + Math.imul(ah4, bl8)) | 0;
|
34871 | hi = (hi + Math.imul(ah4, bh8)) | 0;
|
34872 | lo = (lo + Math.imul(al3, bl9)) | 0;
|
34873 | mid = (mid + Math.imul(al3, bh9)) | 0;
|
34874 | mid = (mid + Math.imul(ah3, bl9)) | 0;
|
34875 | hi = (hi + Math.imul(ah3, bh9)) | 0;
|
34876 | var w12 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34877 | c = (((hi + (mid >>> 13)) | 0) + (w12 >>> 26)) | 0;
|
34878 | w12 &= 0x3ffffff;
|
34879 |
|
34880 | lo = Math.imul(al9, bl4);
|
34881 | mid = Math.imul(al9, bh4);
|
34882 | mid = (mid + Math.imul(ah9, bl4)) | 0;
|
34883 | hi = Math.imul(ah9, bh4);
|
34884 | lo = (lo + Math.imul(al8, bl5)) | 0;
|
34885 | mid = (mid + Math.imul(al8, bh5)) | 0;
|
34886 | mid = (mid + Math.imul(ah8, bl5)) | 0;
|
34887 | hi = (hi + Math.imul(ah8, bh5)) | 0;
|
34888 | lo = (lo + Math.imul(al7, bl6)) | 0;
|
34889 | mid = (mid + Math.imul(al7, bh6)) | 0;
|
34890 | mid = (mid + Math.imul(ah7, bl6)) | 0;
|
34891 | hi = (hi + Math.imul(ah7, bh6)) | 0;
|
34892 | lo = (lo + Math.imul(al6, bl7)) | 0;
|
34893 | mid = (mid + Math.imul(al6, bh7)) | 0;
|
34894 | mid = (mid + Math.imul(ah6, bl7)) | 0;
|
34895 | hi = (hi + Math.imul(ah6, bh7)) | 0;
|
34896 | lo = (lo + Math.imul(al5, bl8)) | 0;
|
34897 | mid = (mid + Math.imul(al5, bh8)) | 0;
|
34898 | mid = (mid + Math.imul(ah5, bl8)) | 0;
|
34899 | hi = (hi + Math.imul(ah5, bh8)) | 0;
|
34900 | lo = (lo + Math.imul(al4, bl9)) | 0;
|
34901 | mid = (mid + Math.imul(al4, bh9)) | 0;
|
34902 | mid = (mid + Math.imul(ah4, bl9)) | 0;
|
34903 | hi = (hi + Math.imul(ah4, bh9)) | 0;
|
34904 | var w13 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34905 | c = (((hi + (mid >>> 13)) | 0) + (w13 >>> 26)) | 0;
|
34906 | w13 &= 0x3ffffff;
|
34907 |
|
34908 | lo = Math.imul(al9, bl5);
|
34909 | mid = Math.imul(al9, bh5);
|
34910 | mid = (mid + Math.imul(ah9, bl5)) | 0;
|
34911 | hi = Math.imul(ah9, bh5);
|
34912 | lo = (lo + Math.imul(al8, bl6)) | 0;
|
34913 | mid = (mid + Math.imul(al8, bh6)) | 0;
|
34914 | mid = (mid + Math.imul(ah8, bl6)) | 0;
|
34915 | hi = (hi + Math.imul(ah8, bh6)) | 0;
|
34916 | lo = (lo + Math.imul(al7, bl7)) | 0;
|
34917 | mid = (mid + Math.imul(al7, bh7)) | 0;
|
34918 | mid = (mid + Math.imul(ah7, bl7)) | 0;
|
34919 | hi = (hi + Math.imul(ah7, bh7)) | 0;
|
34920 | lo = (lo + Math.imul(al6, bl8)) | 0;
|
34921 | mid = (mid + Math.imul(al6, bh8)) | 0;
|
34922 | mid = (mid + Math.imul(ah6, bl8)) | 0;
|
34923 | hi = (hi + Math.imul(ah6, bh8)) | 0;
|
34924 | lo = (lo + Math.imul(al5, bl9)) | 0;
|
34925 | mid = (mid + Math.imul(al5, bh9)) | 0;
|
34926 | mid = (mid + Math.imul(ah5, bl9)) | 0;
|
34927 | hi = (hi + Math.imul(ah5, bh9)) | 0;
|
34928 | var w14 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34929 | c = (((hi + (mid >>> 13)) | 0) + (w14 >>> 26)) | 0;
|
34930 | w14 &= 0x3ffffff;
|
34931 |
|
34932 | lo = Math.imul(al9, bl6);
|
34933 | mid = Math.imul(al9, bh6);
|
34934 | mid = (mid + Math.imul(ah9, bl6)) | 0;
|
34935 | hi = Math.imul(ah9, bh6);
|
34936 | lo = (lo + Math.imul(al8, bl7)) | 0;
|
34937 | mid = (mid + Math.imul(al8, bh7)) | 0;
|
34938 | mid = (mid + Math.imul(ah8, bl7)) | 0;
|
34939 | hi = (hi + Math.imul(ah8, bh7)) | 0;
|
34940 | lo = (lo + Math.imul(al7, bl8)) | 0;
|
34941 | mid = (mid + Math.imul(al7, bh8)) | 0;
|
34942 | mid = (mid + Math.imul(ah7, bl8)) | 0;
|
34943 | hi = (hi + Math.imul(ah7, bh8)) | 0;
|
34944 | lo = (lo + Math.imul(al6, bl9)) | 0;
|
34945 | mid = (mid + Math.imul(al6, bh9)) | 0;
|
34946 | mid = (mid + Math.imul(ah6, bl9)) | 0;
|
34947 | hi = (hi + Math.imul(ah6, bh9)) | 0;
|
34948 | var w15 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34949 | c = (((hi + (mid >>> 13)) | 0) + (w15 >>> 26)) | 0;
|
34950 | w15 &= 0x3ffffff;
|
34951 |
|
34952 | lo = Math.imul(al9, bl7);
|
34953 | mid = Math.imul(al9, bh7);
|
34954 | mid = (mid + Math.imul(ah9, bl7)) | 0;
|
34955 | hi = Math.imul(ah9, bh7);
|
34956 | lo = (lo + Math.imul(al8, bl8)) | 0;
|
34957 | mid = (mid + Math.imul(al8, bh8)) | 0;
|
34958 | mid = (mid + Math.imul(ah8, bl8)) | 0;
|
34959 | hi = (hi + Math.imul(ah8, bh8)) | 0;
|
34960 | lo = (lo + Math.imul(al7, bl9)) | 0;
|
34961 | mid = (mid + Math.imul(al7, bh9)) | 0;
|
34962 | mid = (mid + Math.imul(ah7, bl9)) | 0;
|
34963 | hi = (hi + Math.imul(ah7, bh9)) | 0;
|
34964 | var w16 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34965 | c = (((hi + (mid >>> 13)) | 0) + (w16 >>> 26)) | 0;
|
34966 | w16 &= 0x3ffffff;
|
34967 |
|
34968 | lo = Math.imul(al9, bl8);
|
34969 | mid = Math.imul(al9, bh8);
|
34970 | mid = (mid + Math.imul(ah9, bl8)) | 0;
|
34971 | hi = Math.imul(ah9, bh8);
|
34972 | lo = (lo + Math.imul(al8, bl9)) | 0;
|
34973 | mid = (mid + Math.imul(al8, bh9)) | 0;
|
34974 | mid = (mid + Math.imul(ah8, bl9)) | 0;
|
34975 | hi = (hi + Math.imul(ah8, bh9)) | 0;
|
34976 | var w17 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34977 | c = (((hi + (mid >>> 13)) | 0) + (w17 >>> 26)) | 0;
|
34978 | w17 &= 0x3ffffff;
|
34979 |
|
34980 | lo = Math.imul(al9, bl9);
|
34981 | mid = Math.imul(al9, bh9);
|
34982 | mid = (mid + Math.imul(ah9, bl9)) | 0;
|
34983 | hi = Math.imul(ah9, bh9);
|
34984 | var w18 = (((c + lo) | 0) + ((mid & 0x1fff) << 13)) | 0;
|
34985 | c = (((hi + (mid >>> 13)) | 0) + (w18 >>> 26)) | 0;
|
34986 | w18 &= 0x3ffffff;
|
34987 | o[0] = w0;
|
34988 | o[1] = w1;
|
34989 | o[2] = w2;
|
34990 | o[3] = w3;
|
34991 | o[4] = w4;
|
34992 | o[5] = w5;
|
34993 | o[6] = w6;
|
34994 | o[7] = w7;
|
34995 | o[8] = w8;
|
34996 | o[9] = w9;
|
34997 | o[10] = w10;
|
34998 | o[11] = w11;
|
34999 | o[12] = w12;
|
35000 | o[13] = w13;
|
35001 | o[14] = w14;
|
35002 | o[15] = w15;
|
35003 | o[16] = w16;
|
35004 | o[17] = w17;
|
35005 | o[18] = w18;
|
35006 | if (c !== 0) {
|
35007 | o[19] = c;
|
35008 | out.length++;
|
35009 | }
|
35010 | return out;
|
35011 | };
|
35012 |
|
35013 |
|
35014 | if (!Math.imul) {
|
35015 | comb10MulTo = smallMulTo;
|
35016 | }
|
35017 |
|
35018 | function bigMulTo (self, num, out) {
|
35019 | out.negative = num.negative ^ self.negative;
|
35020 | out.length = self.length + num.length;
|
35021 |
|
35022 | var carry = 0;
|
35023 | var hncarry = 0;
|
35024 | for (var k = 0; k < out.length - 1; k++) {
|
35025 |
|
35026 |
|
35027 | var ncarry = hncarry;
|
35028 | hncarry = 0;
|
35029 | var rword = carry & 0x3ffffff;
|
35030 | var maxJ = Math.min(k, num.length - 1);
|
35031 | for (var j = Math.max(0, k - self.length + 1); j <= maxJ; j++) {
|
35032 | var i = k - j;
|
35033 | var a = self.words[i] | 0;
|
35034 | var b = num.words[j] | 0;
|
35035 | var r = a * b;
|
35036 |
|
35037 | var lo = r & 0x3ffffff;
|
35038 | ncarry = (ncarry + ((r / 0x4000000) | 0)) | 0;
|
35039 | lo = (lo + rword) | 0;
|
35040 | rword = lo & 0x3ffffff;
|
35041 | ncarry = (ncarry + (lo >>> 26)) | 0;
|
35042 |
|
35043 | hncarry += ncarry >>> 26;
|
35044 | ncarry &= 0x3ffffff;
|
35045 | }
|
35046 | out.words[k] = rword;
|
35047 | carry = ncarry;
|
35048 | ncarry = hncarry;
|
35049 | }
|
35050 | if (carry !== 0) {
|
35051 | out.words[k] = carry;
|
35052 | } else {
|
35053 | out.length--;
|
35054 | }
|
35055 |
|
35056 | return out.strip();
|
35057 | }
|
35058 |
|
35059 | function jumboMulTo (self, num, out) {
|
35060 | var fftm = new FFTM();
|
35061 | return fftm.mulp(self, num, out);
|
35062 | }
|
35063 |
|
35064 | BN.prototype.mulTo = function mulTo (num, out) {
|
35065 | var res;
|
35066 | var len = this.length + num.length;
|
35067 | if (this.length === 10 && num.length === 10) {
|
35068 | res = comb10MulTo(this, num, out);
|
35069 | } else if (len < 63) {
|
35070 | res = smallMulTo(this, num, out);
|
35071 | } else if (len < 1024) {
|
35072 | res = bigMulTo(this, num, out);
|
35073 | } else {
|
35074 | res = jumboMulTo(this, num, out);
|
35075 | }
|
35076 |
|
35077 | return res;
|
35078 | };
|
35079 |
|
35080 |
|
35081 |
|
35082 |
|
35083 | function FFTM (x, y) {
|
35084 | this.x = x;
|
35085 | this.y = y;
|
35086 | }
|
35087 |
|
35088 | FFTM.prototype.makeRBT = function makeRBT (N) {
|
35089 | var t = new Array(N);
|
35090 | var l = BN.prototype._countBits(N) - 1;
|
35091 | for (var i = 0; i < N; i++) {
|
35092 | t[i] = this.revBin(i, l, N);
|
35093 | }
|
35094 |
|
35095 | return t;
|
35096 | };
|
35097 |
|
35098 |
|
35099 | FFTM.prototype.revBin = function revBin (x, l, N) {
|
35100 | if (x === 0 || x === N - 1) return x;
|
35101 |
|
35102 | var rb = 0;
|
35103 | for (var i = 0; i < l; i++) {
|
35104 | rb |= (x & 1) << (l - i - 1);
|
35105 | x >>= 1;
|
35106 | }
|
35107 |
|
35108 | return rb;
|
35109 | };
|
35110 |
|
35111 |
|
35112 |
|
35113 | FFTM.prototype.permute = function permute (rbt, rws, iws, rtws, itws, N) {
|
35114 | for (var i = 0; i < N; i++) {
|
35115 | rtws[i] = rws[rbt[i]];
|
35116 | itws[i] = iws[rbt[i]];
|
35117 | }
|
35118 | };
|
35119 |
|
35120 | FFTM.prototype.transform = function transform (rws, iws, rtws, itws, N, rbt) {
|
35121 | this.permute(rbt, rws, iws, rtws, itws, N);
|
35122 |
|
35123 | for (var s = 1; s < N; s <<= 1) {
|
35124 | var l = s << 1;
|
35125 |
|
35126 | var rtwdf = Math.cos(2 * Math.PI / l);
|
35127 | var itwdf = Math.sin(2 * Math.PI / l);
|
35128 |
|
35129 | for (var p = 0; p < N; p += l) {
|
35130 | var rtwdf_ = rtwdf;
|
35131 | var itwdf_ = itwdf;
|
35132 |
|
35133 | for (var j = 0; j < s; j++) {
|
35134 | var re = rtws[p + j];
|
35135 | var ie = itws[p + j];
|
35136 |
|
35137 | var ro = rtws[p + j + s];
|
35138 | var io = itws[p + j + s];
|
35139 |
|
35140 | var rx = rtwdf_ * ro - itwdf_ * io;
|
35141 |
|
35142 | io = rtwdf_ * io + itwdf_ * ro;
|
35143 | ro = rx;
|
35144 |
|
35145 | rtws[p + j] = re + ro;
|
35146 | itws[p + j] = ie + io;
|
35147 |
|
35148 | rtws[p + j + s] = re - ro;
|
35149 | itws[p + j + s] = ie - io;
|
35150 |
|
35151 |
|
35152 | if (j !== l) {
|
35153 | rx = rtwdf * rtwdf_ - itwdf * itwdf_;
|
35154 |
|
35155 | itwdf_ = rtwdf * itwdf_ + itwdf * rtwdf_;
|
35156 | rtwdf_ = rx;
|
35157 | }
|
35158 | }
|
35159 | }
|
35160 | }
|
35161 | };
|
35162 |
|
35163 | FFTM.prototype.guessLen13b = function guessLen13b (n, m) {
|
35164 | var N = Math.max(m, n) | 1;
|
35165 | var odd = N & 1;
|
35166 | var i = 0;
|
35167 | for (N = N / 2 | 0; N; N = N >>> 1) {
|
35168 | i++;
|
35169 | }
|
35170 |
|
35171 | return 1 << i + 1 + odd;
|
35172 | };
|
35173 |
|
35174 | FFTM.prototype.conjugate = function conjugate (rws, iws, N) {
|
35175 | if (N <= 1) return;
|
35176 |
|
35177 | for (var i = 0; i < N / 2; i++) {
|
35178 | var t = rws[i];
|
35179 |
|
35180 | rws[i] = rws[N - i - 1];
|
35181 | rws[N - i - 1] = t;
|
35182 |
|
35183 | t = iws[i];
|
35184 |
|
35185 | iws[i] = -iws[N - i - 1];
|
35186 | iws[N - i - 1] = -t;
|
35187 | }
|
35188 | };
|
35189 |
|
35190 | FFTM.prototype.normalize13b = function normalize13b (ws, N) {
|
35191 | var carry = 0;
|
35192 | for (var i = 0; i < N / 2; i++) {
|
35193 | var w = Math.round(ws[2 * i + 1] / N) * 0x2000 +
|
35194 | Math.round(ws[2 * i] / N) +
|
35195 | carry;
|
35196 |
|
35197 | ws[i] = w & 0x3ffffff;
|
35198 |
|
35199 | if (w < 0x4000000) {
|
35200 | carry = 0;
|
35201 | } else {
|
35202 | carry = w / 0x4000000 | 0;
|
35203 | }
|
35204 | }
|
35205 |
|
35206 | return ws;
|
35207 | };
|
35208 |
|
35209 | FFTM.prototype.convert13b = function convert13b (ws, len, rws, N) {
|
35210 | var carry = 0;
|
35211 | for (var i = 0; i < len; i++) {
|
35212 | carry = carry + (ws[i] | 0);
|
35213 |
|
35214 | rws[2 * i] = carry & 0x1fff; carry = carry >>> 13;
|
35215 | rws[2 * i + 1] = carry & 0x1fff; carry = carry >>> 13;
|
35216 | }
|
35217 |
|
35218 |
|
35219 | for (i = 2 * len; i < N; ++i) {
|
35220 | rws[i] = 0;
|
35221 | }
|
35222 |
|
35223 | assert(carry === 0);
|
35224 | assert((carry & ~0x1fff) === 0);
|
35225 | };
|
35226 |
|
35227 | FFTM.prototype.stub = function stub (N) {
|
35228 | var ph = new Array(N);
|
35229 | for (var i = 0; i < N; i++) {
|
35230 | ph[i] = 0;
|
35231 | }
|
35232 |
|
35233 | return ph;
|
35234 | };
|
35235 |
|
35236 | FFTM.prototype.mulp = function mulp (x, y, out) {
|
35237 | var N = 2 * this.guessLen13b(x.length, y.length);
|
35238 |
|
35239 | var rbt = this.makeRBT(N);
|
35240 |
|
35241 | var _ = this.stub(N);
|
35242 |
|
35243 | var rws = new Array(N);
|
35244 | var rwst = new Array(N);
|
35245 | var iwst = new Array(N);
|
35246 |
|
35247 | var nrws = new Array(N);
|
35248 | var nrwst = new Array(N);
|
35249 | var niwst = new Array(N);
|
35250 |
|
35251 | var rmws = out.words;
|
35252 | rmws.length = N;
|
35253 |
|
35254 | this.convert13b(x.words, x.length, rws, N);
|
35255 | this.convert13b(y.words, y.length, nrws, N);
|
35256 |
|
35257 | this.transform(rws, _, rwst, iwst, N, rbt);
|
35258 | this.transform(nrws, _, nrwst, niwst, N, rbt);
|
35259 |
|
35260 | for (var i = 0; i < N; i++) {
|
35261 | var rx = rwst[i] * nrwst[i] - iwst[i] * niwst[i];
|
35262 | iwst[i] = rwst[i] * niwst[i] + iwst[i] * nrwst[i];
|
35263 | rwst[i] = rx;
|
35264 | }
|
35265 |
|
35266 | this.conjugate(rwst, iwst, N);
|
35267 | this.transform(rwst, iwst, rmws, _, N, rbt);
|
35268 | this.conjugate(rmws, _, N);
|
35269 | this.normalize13b(rmws, N);
|
35270 |
|
35271 | out.negative = x.negative ^ y.negative;
|
35272 | out.length = x.length + y.length;
|
35273 | return out.strip();
|
35274 | };
|
35275 |
|
35276 |
|
35277 | BN.prototype.mul = function mul (num) {
|
35278 | var out = new BN(null);
|
35279 | out.words = new Array(this.length + num.length);
|
35280 | return this.mulTo(num, out);
|
35281 | };
|
35282 |
|
35283 |
|
35284 | BN.prototype.mulf = function mulf (num) {
|
35285 | var out = new BN(null);
|
35286 | out.words = new Array(this.length + num.length);
|
35287 | return jumboMulTo(this, num, out);
|
35288 | };
|
35289 |
|
35290 |
|
35291 | BN.prototype.imul = function imul (num) {
|
35292 | return this.clone().mulTo(num, this);
|
35293 | };
|
35294 |
|
35295 | BN.prototype.imuln = function imuln (num) {
|
35296 | assert(typeof num === 'number');
|
35297 | assert(num < 0x4000000);
|
35298 |
|
35299 |
|
35300 | var carry = 0;
|
35301 | for (var i = 0; i < this.length; i++) {
|
35302 | var w = (this.words[i] | 0) * num;
|
35303 | var lo = (w & 0x3ffffff) + (carry & 0x3ffffff);
|
35304 | carry >>= 26;
|
35305 | carry += (w / 0x4000000) | 0;
|
35306 |
|
35307 | carry += lo >>> 26;
|
35308 | this.words[i] = lo & 0x3ffffff;
|
35309 | }
|
35310 |
|
35311 | if (carry !== 0) {
|
35312 | this.words[i] = carry;
|
35313 | this.length++;
|
35314 | }
|
35315 |
|
35316 | return this;
|
35317 | };
|
35318 |
|
35319 | BN.prototype.muln = function muln (num) {
|
35320 | return this.clone().imuln(num);
|
35321 | };
|
35322 |
|
35323 |
|
35324 | BN.prototype.sqr = function sqr () {
|
35325 | return this.mul(this);
|
35326 | };
|
35327 |
|
35328 |
|
35329 | BN.prototype.isqr = function isqr () {
|
35330 | return this.imul(this.clone());
|
35331 | };
|
35332 |
|
35333 |
|
35334 | BN.prototype.pow = function pow (num) {
|
35335 | var w = toBitArray(num);
|
35336 | if (w.length === 0) return new BN(1);
|
35337 |
|
35338 |
|
35339 | var res = this;
|
35340 | for (var i = 0; i < w.length; i++, res = res.sqr()) {
|
35341 | if (w[i] !== 0) break;
|
35342 | }
|
35343 |
|
35344 | if (++i < w.length) {
|
35345 | for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) {
|
35346 | if (w[i] === 0) continue;
|
35347 |
|
35348 | res = res.mul(q);
|
35349 | }
|
35350 | }
|
35351 |
|
35352 | return res;
|
35353 | };
|
35354 |
|
35355 |
|
35356 | BN.prototype.iushln = function iushln (bits) {
|
35357 | assert(typeof bits === 'number' && bits >= 0);
|
35358 | var r = bits % 26;
|
35359 | var s = (bits - r) / 26;
|
35360 | var carryMask = (0x3ffffff >>> (26 - r)) << (26 - r);
|
35361 | var i;
|
35362 |
|
35363 | if (r !== 0) {
|
35364 | var carry = 0;
|
35365 |
|
35366 | for (i = 0; i < this.length; i++) {
|
35367 | var newCarry = this.words[i] & carryMask;
|
35368 | var c = ((this.words[i] | 0) - newCarry) << r;
|
35369 | this.words[i] = c | carry;
|
35370 | carry = newCarry >>> (26 - r);
|
35371 | }
|
35372 |
|
35373 | if (carry) {
|
35374 | this.words[i] = carry;
|
35375 | this.length++;
|
35376 | }
|
35377 | }
|
35378 |
|
35379 | if (s !== 0) {
|
35380 | for (i = this.length - 1; i >= 0; i--) {
|
35381 | this.words[i + s] = this.words[i];
|
35382 | }
|
35383 |
|
35384 | for (i = 0; i < s; i++) {
|
35385 | this.words[i] = 0;
|
35386 | }
|
35387 |
|
35388 | this.length += s;
|
35389 | }
|
35390 |
|
35391 | return this.strip();
|
35392 | };
|
35393 |
|
35394 | BN.prototype.ishln = function ishln (bits) {
|
35395 |
|
35396 | assert(this.negative === 0);
|
35397 | return this.iushln(bits);
|
35398 | };
|
35399 |
|
35400 |
|
35401 |
|
35402 |
|
35403 | BN.prototype.iushrn = function iushrn (bits, hint, extended) {
|
35404 | assert(typeof bits === 'number' && bits >= 0);
|
35405 | var h;
|
35406 | if (hint) {
|
35407 | h = (hint - (hint % 26)) / 26;
|
35408 | } else {
|
35409 | h = 0;
|
35410 | }
|
35411 |
|
35412 | var r = bits % 26;
|
35413 | var s = Math.min((bits - r) / 26, this.length);
|
35414 | var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
|
35415 | var maskedWords = extended;
|
35416 |
|
35417 | h -= s;
|
35418 | h = Math.max(0, h);
|
35419 |
|
35420 |
|
35421 | if (maskedWords) {
|
35422 | for (var i = 0; i < s; i++) {
|
35423 | maskedWords.words[i] = this.words[i];
|
35424 | }
|
35425 | maskedWords.length = s;
|
35426 | }
|
35427 |
|
35428 | if (s === 0) ; else if (this.length > s) {
|
35429 | this.length -= s;
|
35430 | for (i = 0; i < this.length; i++) {
|
35431 | this.words[i] = this.words[i + s];
|
35432 | }
|
35433 | } else {
|
35434 | this.words[0] = 0;
|
35435 | this.length = 1;
|
35436 | }
|
35437 |
|
35438 | var carry = 0;
|
35439 | for (i = this.length - 1; i >= 0 && (carry !== 0 || i >= h); i--) {
|
35440 | var word = this.words[i] | 0;
|
35441 | this.words[i] = (carry << (26 - r)) | (word >>> r);
|
35442 | carry = word & mask;
|
35443 | }
|
35444 |
|
35445 |
|
35446 | if (maskedWords && carry !== 0) {
|
35447 | maskedWords.words[maskedWords.length++] = carry;
|
35448 | }
|
35449 |
|
35450 | if (this.length === 0) {
|
35451 | this.words[0] = 0;
|
35452 | this.length = 1;
|
35453 | }
|
35454 |
|
35455 | return this.strip();
|
35456 | };
|
35457 |
|
35458 | BN.prototype.ishrn = function ishrn (bits, hint, extended) {
|
35459 |
|
35460 | assert(this.negative === 0);
|
35461 | return this.iushrn(bits, hint, extended);
|
35462 | };
|
35463 |
|
35464 |
|
35465 | BN.prototype.shln = function shln (bits) {
|
35466 | return this.clone().ishln(bits);
|
35467 | };
|
35468 |
|
35469 | BN.prototype.ushln = function ushln (bits) {
|
35470 | return this.clone().iushln(bits);
|
35471 | };
|
35472 |
|
35473 |
|
35474 | BN.prototype.shrn = function shrn (bits) {
|
35475 | return this.clone().ishrn(bits);
|
35476 | };
|
35477 |
|
35478 | BN.prototype.ushrn = function ushrn (bits) {
|
35479 | return this.clone().iushrn(bits);
|
35480 | };
|
35481 |
|
35482 |
|
35483 | BN.prototype.testn = function testn (bit) {
|
35484 | assert(typeof bit === 'number' && bit >= 0);
|
35485 | var r = bit % 26;
|
35486 | var s = (bit - r) / 26;
|
35487 | var q = 1 << r;
|
35488 |
|
35489 |
|
35490 | if (this.length <= s) return false;
|
35491 |
|
35492 |
|
35493 | var w = this.words[s];
|
35494 |
|
35495 | return !!(w & q);
|
35496 | };
|
35497 |
|
35498 |
|
35499 | BN.prototype.imaskn = function imaskn (bits) {
|
35500 | assert(typeof bits === 'number' && bits >= 0);
|
35501 | var r = bits % 26;
|
35502 | var s = (bits - r) / 26;
|
35503 |
|
35504 | assert(this.negative === 0, 'imaskn works only with positive numbers');
|
35505 |
|
35506 | if (this.length <= s) {
|
35507 | return this;
|
35508 | }
|
35509 |
|
35510 | if (r !== 0) {
|
35511 | s++;
|
35512 | }
|
35513 | this.length = Math.min(s, this.length);
|
35514 |
|
35515 | if (r !== 0) {
|
35516 | var mask = 0x3ffffff ^ ((0x3ffffff >>> r) << r);
|
35517 | this.words[this.length - 1] &= mask;
|
35518 | }
|
35519 |
|
35520 | return this.strip();
|
35521 | };
|
35522 |
|
35523 |
|
35524 | BN.prototype.maskn = function maskn (bits) {
|
35525 | return this.clone().imaskn(bits);
|
35526 | };
|
35527 |
|
35528 |
|
35529 | BN.prototype.iaddn = function iaddn (num) {
|
35530 | assert(typeof num === 'number');
|
35531 | assert(num < 0x4000000);
|
35532 | if (num < 0) return this.isubn(-num);
|
35533 |
|
35534 |
|
35535 | if (this.negative !== 0) {
|
35536 | if (this.length === 1 && (this.words[0] | 0) < num) {
|
35537 | this.words[0] = num - (this.words[0] | 0);
|
35538 | this.negative = 0;
|
35539 | return this;
|
35540 | }
|
35541 |
|
35542 | this.negative = 0;
|
35543 | this.isubn(num);
|
35544 | this.negative = 1;
|
35545 | return this;
|
35546 | }
|
35547 |
|
35548 |
|
35549 | return this._iaddn(num);
|
35550 | };
|
35551 |
|
35552 | BN.prototype._iaddn = function _iaddn (num) {
|
35553 | this.words[0] += num;
|
35554 |
|
35555 |
|
35556 | for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) {
|
35557 | this.words[i] -= 0x4000000;
|
35558 | if (i === this.length - 1) {
|
35559 | this.words[i + 1] = 1;
|
35560 | } else {
|
35561 | this.words[i + 1]++;
|
35562 | }
|
35563 | }
|
35564 | this.length = Math.max(this.length, i + 1);
|
35565 |
|
35566 | return this;
|
35567 | };
|
35568 |
|
35569 |
|
35570 | BN.prototype.isubn = function isubn (num) {
|
35571 | assert(typeof num === 'number');
|
35572 | assert(num < 0x4000000);
|
35573 | if (num < 0) return this.iaddn(-num);
|
35574 |
|
35575 | if (this.negative !== 0) {
|
35576 | this.negative = 0;
|
35577 | this.iaddn(num);
|
35578 | this.negative = 1;
|
35579 | return this;
|
35580 | }
|
35581 |
|
35582 | this.words[0] -= num;
|
35583 |
|
35584 | if (this.length === 1 && this.words[0] < 0) {
|
35585 | this.words[0] = -this.words[0];
|
35586 | this.negative = 1;
|
35587 | } else {
|
35588 |
|
35589 | for (var i = 0; i < this.length && this.words[i] < 0; i++) {
|
35590 | this.words[i] += 0x4000000;
|
35591 | this.words[i + 1] -= 1;
|
35592 | }
|
35593 | }
|
35594 |
|
35595 | return this.strip();
|
35596 | };
|
35597 |
|
35598 | BN.prototype.addn = function addn (num) {
|
35599 | return this.clone().iaddn(num);
|
35600 | };
|
35601 |
|
35602 | BN.prototype.subn = function subn (num) {
|
35603 | return this.clone().isubn(num);
|
35604 | };
|
35605 |
|
35606 | BN.prototype.iabs = function iabs () {
|
35607 | this.negative = 0;
|
35608 |
|
35609 | return this;
|
35610 | };
|
35611 |
|
35612 | BN.prototype.abs = function abs () {
|
35613 | return this.clone().iabs();
|
35614 | };
|
35615 |
|
35616 | BN.prototype._ishlnsubmul = function _ishlnsubmul (num, mul, shift) {
|
35617 | var len = num.length + shift;
|
35618 | var i;
|
35619 |
|
35620 | this._expand(len);
|
35621 |
|
35622 | var w;
|
35623 | var carry = 0;
|
35624 | for (i = 0; i < num.length; i++) {
|
35625 | w = (this.words[i + shift] | 0) + carry;
|
35626 | var right = (num.words[i] | 0) * mul;
|
35627 | w -= right & 0x3ffffff;
|
35628 | carry = (w >> 26) - ((right / 0x4000000) | 0);
|
35629 | this.words[i + shift] = w & 0x3ffffff;
|
35630 | }
|
35631 | for (; i < this.length - shift; i++) {
|
35632 | w = (this.words[i + shift] | 0) + carry;
|
35633 | carry = w >> 26;
|
35634 | this.words[i + shift] = w & 0x3ffffff;
|
35635 | }
|
35636 |
|
35637 | if (carry === 0) return this.strip();
|
35638 |
|
35639 |
|
35640 | assert(carry === -1);
|
35641 | carry = 0;
|
35642 | for (i = 0; i < this.length; i++) {
|
35643 | w = -(this.words[i] | 0) + carry;
|
35644 | carry = w >> 26;
|
35645 | this.words[i] = w & 0x3ffffff;
|
35646 | }
|
35647 | this.negative = 1;
|
35648 |
|
35649 | return this.strip();
|
35650 | };
|
35651 |
|
35652 | BN.prototype._wordDiv = function _wordDiv (num, mode) {
|
35653 | var shift = this.length - num.length;
|
35654 |
|
35655 | var a = this.clone();
|
35656 | var b = num;
|
35657 |
|
35658 |
|
35659 | var bhi = b.words[b.length - 1] | 0;
|
35660 | var bhiBits = this._countBits(bhi);
|
35661 | shift = 26 - bhiBits;
|
35662 | if (shift !== 0) {
|
35663 | b = b.ushln(shift);
|
35664 | a.iushln(shift);
|
35665 | bhi = b.words[b.length - 1] | 0;
|
35666 | }
|
35667 |
|
35668 |
|
35669 | var m = a.length - b.length;
|
35670 | var q;
|
35671 |
|
35672 | if (mode !== 'mod') {
|
35673 | q = new BN(null);
|
35674 | q.length = m + 1;
|
35675 | q.words = new Array(q.length);
|
35676 | for (var i = 0; i < q.length; i++) {
|
35677 | q.words[i] = 0;
|
35678 | }
|
35679 | }
|
35680 |
|
35681 | var diff = a.clone()._ishlnsubmul(b, 1, m);
|
35682 | if (diff.negative === 0) {
|
35683 | a = diff;
|
35684 | if (q) {
|
35685 | q.words[m] = 1;
|
35686 | }
|
35687 | }
|
35688 |
|
35689 | for (var j = m - 1; j >= 0; j--) {
|
35690 | var qj = (a.words[b.length + j] | 0) * 0x4000000 +
|
35691 | (a.words[b.length + j - 1] | 0);
|
35692 |
|
35693 |
|
35694 |
|
35695 | qj = Math.min((qj / bhi) | 0, 0x3ffffff);
|
35696 |
|
35697 | a._ishlnsubmul(b, qj, j);
|
35698 | while (a.negative !== 0) {
|
35699 | qj--;
|
35700 | a.negative = 0;
|
35701 | a._ishlnsubmul(b, 1, j);
|
35702 | if (!a.isZero()) {
|
35703 | a.negative ^= 1;
|
35704 | }
|
35705 | }
|
35706 | if (q) {
|
35707 | q.words[j] = qj;
|
35708 | }
|
35709 | }
|
35710 | if (q) {
|
35711 | q.strip();
|
35712 | }
|
35713 | a.strip();
|
35714 |
|
35715 |
|
35716 | if (mode !== 'div' && shift !== 0) {
|
35717 | a.iushrn(shift);
|
35718 | }
|
35719 |
|
35720 | return {
|
35721 | div: q || null,
|
35722 | mod: a
|
35723 | };
|
35724 | };
|
35725 |
|
35726 |
|
35727 |
|
35728 |
|
35729 |
|
35730 | BN.prototype.divmod = function divmod (num, mode, positive) {
|
35731 | assert(!num.isZero());
|
35732 |
|
35733 | if (this.isZero()) {
|
35734 | return {
|
35735 | div: new BN(0),
|
35736 | mod: new BN(0)
|
35737 | };
|
35738 | }
|
35739 |
|
35740 | var div, mod, res;
|
35741 | if (this.negative !== 0 && num.negative === 0) {
|
35742 | res = this.neg().divmod(num, mode);
|
35743 |
|
35744 | if (mode !== 'mod') {
|
35745 | div = res.div.neg();
|
35746 | }
|
35747 |
|
35748 | if (mode !== 'div') {
|
35749 | mod = res.mod.neg();
|
35750 | if (positive && mod.negative !== 0) {
|
35751 | mod.iadd(num);
|
35752 | }
|
35753 | }
|
35754 |
|
35755 | return {
|
35756 | div: div,
|
35757 | mod: mod
|
35758 | };
|
35759 | }
|
35760 |
|
35761 | if (this.negative === 0 && num.negative !== 0) {
|
35762 | res = this.divmod(num.neg(), mode);
|
35763 |
|
35764 | if (mode !== 'mod') {
|
35765 | div = res.div.neg();
|
35766 | }
|
35767 |
|
35768 | return {
|
35769 | div: div,
|
35770 | mod: res.mod
|
35771 | };
|
35772 | }
|
35773 |
|
35774 | if ((this.negative & num.negative) !== 0) {
|
35775 | res = this.neg().divmod(num.neg(), mode);
|
35776 |
|
35777 | if (mode !== 'div') {
|
35778 | mod = res.mod.neg();
|
35779 | if (positive && mod.negative !== 0) {
|
35780 | mod.isub(num);
|
35781 | }
|
35782 | }
|
35783 |
|
35784 | return {
|
35785 | div: res.div,
|
35786 | mod: mod
|
35787 | };
|
35788 | }
|
35789 |
|
35790 |
|
35791 |
|
35792 |
|
35793 | if (num.length > this.length || this.cmp(num) < 0) {
|
35794 | return {
|
35795 | div: new BN(0),
|
35796 | mod: this
|
35797 | };
|
35798 | }
|
35799 |
|
35800 |
|
35801 | if (num.length === 1) {
|
35802 | if (mode === 'div') {
|
35803 | return {
|
35804 | div: this.divn(num.words[0]),
|
35805 | mod: null
|
35806 | };
|
35807 | }
|
35808 |
|
35809 | if (mode === 'mod') {
|
35810 | return {
|
35811 | div: null,
|
35812 | mod: new BN(this.modn(num.words[0]))
|
35813 | };
|
35814 | }
|
35815 |
|
35816 | return {
|
35817 | div: this.divn(num.words[0]),
|
35818 | mod: new BN(this.modn(num.words[0]))
|
35819 | };
|
35820 | }
|
35821 |
|
35822 | return this._wordDiv(num, mode);
|
35823 | };
|
35824 |
|
35825 |
|
35826 | BN.prototype.div = function div (num) {
|
35827 | return this.divmod(num, 'div', false).div;
|
35828 | };
|
35829 |
|
35830 |
|
35831 | BN.prototype.mod = function mod (num) {
|
35832 | return this.divmod(num, 'mod', false).mod;
|
35833 | };
|
35834 |
|
35835 | BN.prototype.umod = function umod (num) {
|
35836 | return this.divmod(num, 'mod', true).mod;
|
35837 | };
|
35838 |
|
35839 |
|
35840 | BN.prototype.divRound = function divRound (num) {
|
35841 | var dm = this.divmod(num);
|
35842 |
|
35843 |
|
35844 | if (dm.mod.isZero()) return dm.div;
|
35845 |
|
35846 | var mod = dm.div.negative !== 0 ? dm.mod.isub(num) : dm.mod;
|
35847 |
|
35848 | var half = num.ushrn(1);
|
35849 | var r2 = num.andln(1);
|
35850 | var cmp = mod.cmp(half);
|
35851 |
|
35852 |
|
35853 | if (cmp < 0 || r2 === 1 && cmp === 0) return dm.div;
|
35854 |
|
35855 |
|
35856 | return dm.div.negative !== 0 ? dm.div.isubn(1) : dm.div.iaddn(1);
|
35857 | };
|
35858 |
|
35859 | BN.prototype.modn = function modn (num) {
|
35860 | assert(num <= 0x3ffffff);
|
35861 | var p = (1 << 26) % num;
|
35862 |
|
35863 | var acc = 0;
|
35864 | for (var i = this.length - 1; i >= 0; i--) {
|
35865 | acc = (p * acc + (this.words[i] | 0)) % num;
|
35866 | }
|
35867 |
|
35868 | return acc;
|
35869 | };
|
35870 |
|
35871 |
|
35872 | BN.prototype.idivn = function idivn (num) {
|
35873 | assert(num <= 0x3ffffff);
|
35874 |
|
35875 | var carry = 0;
|
35876 | for (var i = this.length - 1; i >= 0; i--) {
|
35877 | var w = (this.words[i] | 0) + carry * 0x4000000;
|
35878 | this.words[i] = (w / num) | 0;
|
35879 | carry = w % num;
|
35880 | }
|
35881 |
|
35882 | return this.strip();
|
35883 | };
|
35884 |
|
35885 | BN.prototype.divn = function divn (num) {
|
35886 | return this.clone().idivn(num);
|
35887 | };
|
35888 |
|
35889 | BN.prototype.egcd = function egcd (p) {
|
35890 | assert(p.negative === 0);
|
35891 | assert(!p.isZero());
|
35892 |
|
35893 | var x = this;
|
35894 | var y = p.clone();
|
35895 |
|
35896 | if (x.negative !== 0) {
|
35897 | x = x.umod(p);
|
35898 | } else {
|
35899 | x = x.clone();
|
35900 | }
|
35901 |
|
35902 |
|
35903 | var A = new BN(1);
|
35904 | var B = new BN(0);
|
35905 |
|
35906 |
|
35907 | var C = new BN(0);
|
35908 | var D = new BN(1);
|
35909 |
|
35910 | var g = 0;
|
35911 |
|
35912 | while (x.isEven() && y.isEven()) {
|
35913 | x.iushrn(1);
|
35914 | y.iushrn(1);
|
35915 | ++g;
|
35916 | }
|
35917 |
|
35918 | var yp = y.clone();
|
35919 | var xp = x.clone();
|
35920 |
|
35921 | while (!x.isZero()) {
|
35922 | for (var i = 0, im = 1; (x.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
|
35923 | if (i > 0) {
|
35924 | x.iushrn(i);
|
35925 | while (i-- > 0) {
|
35926 | if (A.isOdd() || B.isOdd()) {
|
35927 | A.iadd(yp);
|
35928 | B.isub(xp);
|
35929 | }
|
35930 |
|
35931 | A.iushrn(1);
|
35932 | B.iushrn(1);
|
35933 | }
|
35934 | }
|
35935 |
|
35936 | for (var j = 0, jm = 1; (y.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
|
35937 | if (j > 0) {
|
35938 | y.iushrn(j);
|
35939 | while (j-- > 0) {
|
35940 | if (C.isOdd() || D.isOdd()) {
|
35941 | C.iadd(yp);
|
35942 | D.isub(xp);
|
35943 | }
|
35944 |
|
35945 | C.iushrn(1);
|
35946 | D.iushrn(1);
|
35947 | }
|
35948 | }
|
35949 |
|
35950 | if (x.cmp(y) >= 0) {
|
35951 | x.isub(y);
|
35952 | A.isub(C);
|
35953 | B.isub(D);
|
35954 | } else {
|
35955 | y.isub(x);
|
35956 | C.isub(A);
|
35957 | D.isub(B);
|
35958 | }
|
35959 | }
|
35960 |
|
35961 | return {
|
35962 | a: C,
|
35963 | b: D,
|
35964 | gcd: y.iushln(g)
|
35965 | };
|
35966 | };
|
35967 |
|
35968 |
|
35969 |
|
35970 |
|
35971 | BN.prototype._invmp = function _invmp (p) {
|
35972 | assert(p.negative === 0);
|
35973 | assert(!p.isZero());
|
35974 |
|
35975 | var a = this;
|
35976 | var b = p.clone();
|
35977 |
|
35978 | if (a.negative !== 0) {
|
35979 | a = a.umod(p);
|
35980 | } else {
|
35981 | a = a.clone();
|
35982 | }
|
35983 |
|
35984 | var x1 = new BN(1);
|
35985 | var x2 = new BN(0);
|
35986 |
|
35987 | var delta = b.clone();
|
35988 |
|
35989 | while (a.cmpn(1) > 0 && b.cmpn(1) > 0) {
|
35990 | for (var i = 0, im = 1; (a.words[0] & im) === 0 && i < 26; ++i, im <<= 1);
|
35991 | if (i > 0) {
|
35992 | a.iushrn(i);
|
35993 | while (i-- > 0) {
|
35994 | if (x1.isOdd()) {
|
35995 | x1.iadd(delta);
|
35996 | }
|
35997 |
|
35998 | x1.iushrn(1);
|
35999 | }
|
36000 | }
|
36001 |
|
36002 | for (var j = 0, jm = 1; (b.words[0] & jm) === 0 && j < 26; ++j, jm <<= 1);
|
36003 | if (j > 0) {
|
36004 | b.iushrn(j);
|
36005 | while (j-- > 0) {
|
36006 | if (x2.isOdd()) {
|
36007 | x2.iadd(delta);
|
36008 | }
|
36009 |
|
36010 | x2.iushrn(1);
|
36011 | }
|
36012 | }
|
36013 |
|
36014 | if (a.cmp(b) >= 0) {
|
36015 | a.isub(b);
|
36016 | x1.isub(x2);
|
36017 | } else {
|
36018 | b.isub(a);
|
36019 | x2.isub(x1);
|
36020 | }
|
36021 | }
|
36022 |
|
36023 | var res;
|
36024 | if (a.cmpn(1) === 0) {
|
36025 | res = x1;
|
36026 | } else {
|
36027 | res = x2;
|
36028 | }
|
36029 |
|
36030 | if (res.cmpn(0) < 0) {
|
36031 | res.iadd(p);
|
36032 | }
|
36033 |
|
36034 | return res;
|
36035 | };
|
36036 |
|
36037 | BN.prototype.gcd = function gcd (num) {
|
36038 | if (this.isZero()) return num.abs();
|
36039 | if (num.isZero()) return this.abs();
|
36040 |
|
36041 | var a = this.clone();
|
36042 | var b = num.clone();
|
36043 | a.negative = 0;
|
36044 | b.negative = 0;
|
36045 |
|
36046 |
|
36047 | for (var shift = 0; a.isEven() && b.isEven(); shift++) {
|
36048 | a.iushrn(1);
|
36049 | b.iushrn(1);
|
36050 | }
|
36051 |
|
36052 | do {
|
36053 | while (a.isEven()) {
|
36054 | a.iushrn(1);
|
36055 | }
|
36056 | while (b.isEven()) {
|
36057 | b.iushrn(1);
|
36058 | }
|
36059 |
|
36060 | var r = a.cmp(b);
|
36061 | if (r < 0) {
|
36062 |
|
36063 | var t = a;
|
36064 | a = b;
|
36065 | b = t;
|
36066 | } else if (r === 0 || b.cmpn(1) === 0) {
|
36067 | break;
|
36068 | }
|
36069 |
|
36070 | a.isub(b);
|
36071 | } while (true);
|
36072 |
|
36073 | return b.iushln(shift);
|
36074 | };
|
36075 |
|
36076 |
|
36077 | BN.prototype.invm = function invm (num) {
|
36078 | return this.egcd(num).a.umod(num);
|
36079 | };
|
36080 |
|
36081 | BN.prototype.isEven = function isEven () {
|
36082 | return (this.words[0] & 1) === 0;
|
36083 | };
|
36084 |
|
36085 | BN.prototype.isOdd = function isOdd () {
|
36086 | return (this.words[0] & 1) === 1;
|
36087 | };
|
36088 |
|
36089 |
|
36090 | BN.prototype.andln = function andln (num) {
|
36091 | return this.words[0] & num;
|
36092 | };
|
36093 |
|
36094 |
|
36095 | BN.prototype.bincn = function bincn (bit) {
|
36096 | assert(typeof bit === 'number');
|
36097 | var r = bit % 26;
|
36098 | var s = (bit - r) / 26;
|
36099 | var q = 1 << r;
|
36100 |
|
36101 |
|
36102 | if (this.length <= s) {
|
36103 | this._expand(s + 1);
|
36104 | this.words[s] |= q;
|
36105 | return this;
|
36106 | }
|
36107 |
|
36108 |
|
36109 | var carry = q;
|
36110 | for (var i = s; carry !== 0 && i < this.length; i++) {
|
36111 | var w = this.words[i] | 0;
|
36112 | w += carry;
|
36113 | carry = w >>> 26;
|
36114 | w &= 0x3ffffff;
|
36115 | this.words[i] = w;
|
36116 | }
|
36117 | if (carry !== 0) {
|
36118 | this.words[i] = carry;
|
36119 | this.length++;
|
36120 | }
|
36121 | return this;
|
36122 | };
|
36123 |
|
36124 | BN.prototype.isZero = function isZero () {
|
36125 | return this.length === 1 && this.words[0] === 0;
|
36126 | };
|
36127 |
|
36128 | BN.prototype.cmpn = function cmpn (num) {
|
36129 | var negative = num < 0;
|
36130 |
|
36131 | if (this.negative !== 0 && !negative) return -1;
|
36132 | if (this.negative === 0 && negative) return 1;
|
36133 |
|
36134 | this.strip();
|
36135 |
|
36136 | var res;
|
36137 | if (this.length > 1) {
|
36138 | res = 1;
|
36139 | } else {
|
36140 | if (negative) {
|
36141 | num = -num;
|
36142 | }
|
36143 |
|
36144 | assert(num <= 0x3ffffff, 'Number is too big');
|
36145 |
|
36146 | var w = this.words[0] | 0;
|
36147 | res = w === num ? 0 : w < num ? -1 : 1;
|
36148 | }
|
36149 | if (this.negative !== 0) return -res | 0;
|
36150 | return res;
|
36151 | };
|
36152 |
|
36153 |
|
36154 |
|
36155 |
|
36156 |
|
36157 | BN.prototype.cmp = function cmp (num) {
|
36158 | if (this.negative !== 0 && num.negative === 0) return -1;
|
36159 | if (this.negative === 0 && num.negative !== 0) return 1;
|
36160 |
|
36161 | var res = this.ucmp(num);
|
36162 | if (this.negative !== 0) return -res | 0;
|
36163 | return res;
|
36164 | };
|
36165 |
|
36166 |
|
36167 | BN.prototype.ucmp = function ucmp (num) {
|
36168 |
|
36169 | if (this.length > num.length) return 1;
|
36170 | if (this.length < num.length) return -1;
|
36171 |
|
36172 | var res = 0;
|
36173 | for (var i = this.length - 1; i >= 0; i--) {
|
36174 | var a = this.words[i] | 0;
|
36175 | var b = num.words[i] | 0;
|
36176 |
|
36177 | if (a === b) continue;
|
36178 | if (a < b) {
|
36179 | res = -1;
|
36180 | } else if (a > b) {
|
36181 | res = 1;
|
36182 | }
|
36183 | break;
|
36184 | }
|
36185 | return res;
|
36186 | };
|
36187 |
|
36188 | BN.prototype.gtn = function gtn (num) {
|
36189 | return this.cmpn(num) === 1;
|
36190 | };
|
36191 |
|
36192 | BN.prototype.gt = function gt (num) {
|
36193 | return this.cmp(num) === 1;
|
36194 | };
|
36195 |
|
36196 | BN.prototype.gten = function gten (num) {
|
36197 | return this.cmpn(num) >= 0;
|
36198 | };
|
36199 |
|
36200 | BN.prototype.gte = function gte (num) {
|
36201 | return this.cmp(num) >= 0;
|
36202 | };
|
36203 |
|
36204 | BN.prototype.ltn = function ltn (num) {
|
36205 | return this.cmpn(num) === -1;
|
36206 | };
|
36207 |
|
36208 | BN.prototype.lt = function lt (num) {
|
36209 | return this.cmp(num) === -1;
|
36210 | };
|
36211 |
|
36212 | BN.prototype.lten = function lten (num) {
|
36213 | return this.cmpn(num) <= 0;
|
36214 | };
|
36215 |
|
36216 | BN.prototype.lte = function lte (num) {
|
36217 | return this.cmp(num) <= 0;
|
36218 | };
|
36219 |
|
36220 | BN.prototype.eqn = function eqn (num) {
|
36221 | return this.cmpn(num) === 0;
|
36222 | };
|
36223 |
|
36224 | BN.prototype.eq = function eq (num) {
|
36225 | return this.cmp(num) === 0;
|
36226 | };
|
36227 |
|
36228 |
|
36229 |
|
36230 |
|
36231 |
|
36232 | BN.red = function red (num) {
|
36233 | return new Red(num);
|
36234 | };
|
36235 |
|
36236 | BN.prototype.toRed = function toRed (ctx) {
|
36237 | assert(!this.red, 'Already a number in reduction context');
|
36238 | assert(this.negative === 0, 'red works only with positives');
|
36239 | return ctx.convertTo(this)._forceRed(ctx);
|
36240 | };
|
36241 |
|
36242 | BN.prototype.fromRed = function fromRed () {
|
36243 | assert(this.red, 'fromRed works only with numbers in reduction context');
|
36244 | return this.red.convertFrom(this);
|
36245 | };
|
36246 |
|
36247 | BN.prototype._forceRed = function _forceRed (ctx) {
|
36248 | this.red = ctx;
|
36249 | return this;
|
36250 | };
|
36251 |
|
36252 | BN.prototype.forceRed = function forceRed (ctx) {
|
36253 | assert(!this.red, 'Already a number in reduction context');
|
36254 | return this._forceRed(ctx);
|
36255 | };
|
36256 |
|
36257 | BN.prototype.redAdd = function redAdd (num) {
|
36258 | assert(this.red, 'redAdd works only with red numbers');
|
36259 | return this.red.add(this, num);
|
36260 | };
|
36261 |
|
36262 | BN.prototype.redIAdd = function redIAdd (num) {
|
36263 | assert(this.red, 'redIAdd works only with red numbers');
|
36264 | return this.red.iadd(this, num);
|
36265 | };
|
36266 |
|
36267 | BN.prototype.redSub = function redSub (num) {
|
36268 | assert(this.red, 'redSub works only with red numbers');
|
36269 | return this.red.sub(this, num);
|
36270 | };
|
36271 |
|
36272 | BN.prototype.redISub = function redISub (num) {
|
36273 | assert(this.red, 'redISub works only with red numbers');
|
36274 | return this.red.isub(this, num);
|
36275 | };
|
36276 |
|
36277 | BN.prototype.redShl = function redShl (num) {
|
36278 | assert(this.red, 'redShl works only with red numbers');
|
36279 | return this.red.shl(this, num);
|
36280 | };
|
36281 |
|
36282 | BN.prototype.redMul = function redMul (num) {
|
36283 | assert(this.red, 'redMul works only with red numbers');
|
36284 | this.red._verify2(this, num);
|
36285 | return this.red.mul(this, num);
|
36286 | };
|
36287 |
|
36288 | BN.prototype.redIMul = function redIMul (num) {
|
36289 | assert(this.red, 'redMul works only with red numbers');
|
36290 | this.red._verify2(this, num);
|
36291 | return this.red.imul(this, num);
|
36292 | };
|
36293 |
|
36294 | BN.prototype.redSqr = function redSqr () {
|
36295 | assert(this.red, 'redSqr works only with red numbers');
|
36296 | this.red._verify1(this);
|
36297 | return this.red.sqr(this);
|
36298 | };
|
36299 |
|
36300 | BN.prototype.redISqr = function redISqr () {
|
36301 | assert(this.red, 'redISqr works only with red numbers');
|
36302 | this.red._verify1(this);
|
36303 | return this.red.isqr(this);
|
36304 | };
|
36305 |
|
36306 |
|
36307 | BN.prototype.redSqrt = function redSqrt () {
|
36308 | assert(this.red, 'redSqrt works only with red numbers');
|
36309 | this.red._verify1(this);
|
36310 | return this.red.sqrt(this);
|
36311 | };
|
36312 |
|
36313 | BN.prototype.redInvm = function redInvm () {
|
36314 | assert(this.red, 'redInvm works only with red numbers');
|
36315 | this.red._verify1(this);
|
36316 | return this.red.invm(this);
|
36317 | };
|
36318 |
|
36319 |
|
36320 | BN.prototype.redNeg = function redNeg () {
|
36321 | assert(this.red, 'redNeg works only with red numbers');
|
36322 | this.red._verify1(this);
|
36323 | return this.red.neg(this);
|
36324 | };
|
36325 |
|
36326 | BN.prototype.redPow = function redPow (num) {
|
36327 | assert(this.red && !num.red, 'redPow(normalNum)');
|
36328 | this.red._verify1(this);
|
36329 | return this.red.pow(this, num);
|
36330 | };
|
36331 |
|
36332 |
|
36333 | var primes = {
|
36334 | k256: null,
|
36335 | p224: null,
|
36336 | p192: null,
|
36337 | p25519: null
|
36338 | };
|
36339 |
|
36340 |
|
36341 | function MPrime (name, p) {
|
36342 |
|
36343 | this.name = name;
|
36344 | this.p = new BN(p, 16);
|
36345 | this.n = this.p.bitLength();
|
36346 | this.k = new BN(1).iushln(this.n).isub(this.p);
|
36347 |
|
36348 | this.tmp = this._tmp();
|
36349 | }
|
36350 |
|
36351 | MPrime.prototype._tmp = function _tmp () {
|
36352 | var tmp = new BN(null);
|
36353 | tmp.words = new Array(Math.ceil(this.n / 13));
|
36354 | return tmp;
|
36355 | };
|
36356 |
|
36357 | MPrime.prototype.ireduce = function ireduce (num) {
|
36358 |
|
36359 |
|
36360 | var r = num;
|
36361 | var rlen;
|
36362 |
|
36363 | do {
|
36364 | this.split(r, this.tmp);
|
36365 | r = this.imulK(r);
|
36366 | r = r.iadd(this.tmp);
|
36367 | rlen = r.bitLength();
|
36368 | } while (rlen > this.n);
|
36369 |
|
36370 | var cmp = rlen < this.n ? -1 : r.ucmp(this.p);
|
36371 | if (cmp === 0) {
|
36372 | r.words[0] = 0;
|
36373 | r.length = 1;
|
36374 | } else if (cmp > 0) {
|
36375 | r.isub(this.p);
|
36376 | } else {
|
36377 | r.strip();
|
36378 | }
|
36379 |
|
36380 | return r;
|
36381 | };
|
36382 |
|
36383 | MPrime.prototype.split = function split (input, out) {
|
36384 | input.iushrn(this.n, 0, out);
|
36385 | };
|
36386 |
|
36387 | MPrime.prototype.imulK = function imulK (num) {
|
36388 | return num.imul(this.k);
|
36389 | };
|
36390 |
|
36391 | function K256 () {
|
36392 | MPrime.call(
|
36393 | this,
|
36394 | 'k256',
|
36395 | 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f');
|
36396 | }
|
36397 | inherits(K256, MPrime);
|
36398 |
|
36399 | K256.prototype.split = function split (input, output) {
|
36400 |
|
36401 | var mask = 0x3fffff;
|
36402 |
|
36403 | var outLen = Math.min(input.length, 9);
|
36404 | for (var i = 0; i < outLen; i++) {
|
36405 | output.words[i] = input.words[i];
|
36406 | }
|
36407 | output.length = outLen;
|
36408 |
|
36409 | if (input.length <= 9) {
|
36410 | input.words[0] = 0;
|
36411 | input.length = 1;
|
36412 | return;
|
36413 | }
|
36414 |
|
36415 |
|
36416 | var prev = input.words[9];
|
36417 | output.words[output.length++] = prev & mask;
|
36418 |
|
36419 | for (i = 10; i < input.length; i++) {
|
36420 | var next = input.words[i] | 0;
|
36421 | input.words[i - 10] = ((next & mask) << 4) | (prev >>> 22);
|
36422 | prev = next;
|
36423 | }
|
36424 | prev >>>= 22;
|
36425 | input.words[i - 10] = prev;
|
36426 | if (prev === 0 && input.length > 10) {
|
36427 | input.length -= 10;
|
36428 | } else {
|
36429 | input.length -= 9;
|
36430 | }
|
36431 | };
|
36432 |
|
36433 | K256.prototype.imulK = function imulK (num) {
|
36434 |
|
36435 | num.words[num.length] = 0;
|
36436 | num.words[num.length + 1] = 0;
|
36437 | num.length += 2;
|
36438 |
|
36439 |
|
36440 | var lo = 0;
|
36441 | for (var i = 0; i < num.length; i++) {
|
36442 | var w = num.words[i] | 0;
|
36443 | lo += w * 0x3d1;
|
36444 | num.words[i] = lo & 0x3ffffff;
|
36445 | lo = w * 0x40 + ((lo / 0x4000000) | 0);
|
36446 | }
|
36447 |
|
36448 |
|
36449 | if (num.words[num.length - 1] === 0) {
|
36450 | num.length--;
|
36451 | if (num.words[num.length - 1] === 0) {
|
36452 | num.length--;
|
36453 | }
|
36454 | }
|
36455 | return num;
|
36456 | };
|
36457 |
|
36458 | function P224 () {
|
36459 | MPrime.call(
|
36460 | this,
|
36461 | 'p224',
|
36462 | 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001');
|
36463 | }
|
36464 | inherits(P224, MPrime);
|
36465 |
|
36466 | function P192 () {
|
36467 | MPrime.call(
|
36468 | this,
|
36469 | 'p192',
|
36470 | 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff');
|
36471 | }
|
36472 | inherits(P192, MPrime);
|
36473 |
|
36474 | function P25519 () {
|
36475 |
|
36476 | MPrime.call(
|
36477 | this,
|
36478 | '25519',
|
36479 | '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed');
|
36480 | }
|
36481 | inherits(P25519, MPrime);
|
36482 |
|
36483 | P25519.prototype.imulK = function imulK (num) {
|
36484 |
|
36485 | var carry = 0;
|
36486 | for (var i = 0; i < num.length; i++) {
|
36487 | var hi = (num.words[i] | 0) * 0x13 + carry;
|
36488 | var lo = hi & 0x3ffffff;
|
36489 | hi >>>= 26;
|
36490 |
|
36491 | num.words[i] = lo;
|
36492 | carry = hi;
|
36493 | }
|
36494 | if (carry !== 0) {
|
36495 | num.words[num.length++] = carry;
|
36496 | }
|
36497 | return num;
|
36498 | };
|
36499 |
|
36500 |
|
36501 | BN._prime = function prime (name) {
|
36502 |
|
36503 | if (primes[name]) return primes[name];
|
36504 |
|
36505 | var prime;
|
36506 | if (name === 'k256') {
|
36507 | prime = new K256();
|
36508 | } else if (name === 'p224') {
|
36509 | prime = new P224();
|
36510 | } else if (name === 'p192') {
|
36511 | prime = new P192();
|
36512 | } else if (name === 'p25519') {
|
36513 | prime = new P25519();
|
36514 | } else {
|
36515 | throw new Error('Unknown prime ' + name);
|
36516 | }
|
36517 | primes[name] = prime;
|
36518 |
|
36519 | return prime;
|
36520 | };
|
36521 |
|
36522 |
|
36523 |
|
36524 |
|
36525 | function Red (m) {
|
36526 | if (typeof m === 'string') {
|
36527 | var prime = BN._prime(m);
|
36528 | this.m = prime.p;
|
36529 | this.prime = prime;
|
36530 | } else {
|
36531 | assert(m.gtn(1), 'modulus must be greater than 1');
|
36532 | this.m = m;
|
36533 | this.prime = null;
|
36534 | }
|
36535 | }
|
36536 |
|
36537 | Red.prototype._verify1 = function _verify1 (a) {
|
36538 | assert(a.negative === 0, 'red works only with positives');
|
36539 | assert(a.red, 'red works only with red numbers');
|
36540 | };
|
36541 |
|
36542 | Red.prototype._verify2 = function _verify2 (a, b) {
|
36543 | assert((a.negative | b.negative) === 0, 'red works only with positives');
|
36544 | assert(a.red && a.red === b.red,
|
36545 | 'red works only with red numbers');
|
36546 | };
|
36547 |
|
36548 | Red.prototype.imod = function imod (a) {
|
36549 | if (this.prime) return this.prime.ireduce(a)._forceRed(this);
|
36550 | return a.umod(this.m)._forceRed(this);
|
36551 | };
|
36552 |
|
36553 | Red.prototype.neg = function neg (a) {
|
36554 | if (a.isZero()) {
|
36555 | return a.clone();
|
36556 | }
|
36557 |
|
36558 | return this.m.sub(a)._forceRed(this);
|
36559 | };
|
36560 |
|
36561 | Red.prototype.add = function add (a, b) {
|
36562 | this._verify2(a, b);
|
36563 |
|
36564 | var res = a.add(b);
|
36565 | if (res.cmp(this.m) >= 0) {
|
36566 | res.isub(this.m);
|
36567 | }
|
36568 | return res._forceRed(this);
|
36569 | };
|
36570 |
|
36571 | Red.prototype.iadd = function iadd (a, b) {
|
36572 | this._verify2(a, b);
|
36573 |
|
36574 | var res = a.iadd(b);
|
36575 | if (res.cmp(this.m) >= 0) {
|
36576 | res.isub(this.m);
|
36577 | }
|
36578 | return res;
|
36579 | };
|
36580 |
|
36581 | Red.prototype.sub = function sub (a, b) {
|
36582 | this._verify2(a, b);
|
36583 |
|
36584 | var res = a.sub(b);
|
36585 | if (res.cmpn(0) < 0) {
|
36586 | res.iadd(this.m);
|
36587 | }
|
36588 | return res._forceRed(this);
|
36589 | };
|
36590 |
|
36591 | Red.prototype.isub = function isub (a, b) {
|
36592 | this._verify2(a, b);
|
36593 |
|
36594 | var res = a.isub(b);
|
36595 | if (res.cmpn(0) < 0) {
|
36596 | res.iadd(this.m);
|
36597 | }
|
36598 | return res;
|
36599 | };
|
36600 |
|
36601 | Red.prototype.shl = function shl (a, num) {
|
36602 | this._verify1(a);
|
36603 | return this.imod(a.ushln(num));
|
36604 | };
|
36605 |
|
36606 | Red.prototype.imul = function imul (a, b) {
|
36607 | this._verify2(a, b);
|
36608 | return this.imod(a.imul(b));
|
36609 | };
|
36610 |
|
36611 | Red.prototype.mul = function mul (a, b) {
|
36612 | this._verify2(a, b);
|
36613 | return this.imod(a.mul(b));
|
36614 | };
|
36615 |
|
36616 | Red.prototype.isqr = function isqr (a) {
|
36617 | return this.imul(a, a.clone());
|
36618 | };
|
36619 |
|
36620 | Red.prototype.sqr = function sqr (a) {
|
36621 | return this.mul(a, a);
|
36622 | };
|
36623 |
|
36624 | Red.prototype.sqrt = function sqrt (a) {
|
36625 | if (a.isZero()) return a.clone();
|
36626 |
|
36627 | var mod3 = this.m.andln(3);
|
36628 | assert(mod3 % 2 === 1);
|
36629 |
|
36630 |
|
36631 | if (mod3 === 3) {
|
36632 | var pow = this.m.add(new BN(1)).iushrn(2);
|
36633 | return this.pow(a, pow);
|
36634 | }
|
36635 |
|
36636 |
|
36637 |
|
36638 |
|
36639 | var q = this.m.subn(1);
|
36640 | var s = 0;
|
36641 | while (!q.isZero() && q.andln(1) === 0) {
|
36642 | s++;
|
36643 | q.iushrn(1);
|
36644 | }
|
36645 | assert(!q.isZero());
|
36646 |
|
36647 | var one = new BN(1).toRed(this);
|
36648 | var nOne = one.redNeg();
|
36649 |
|
36650 |
|
36651 |
|
36652 | var lpow = this.m.subn(1).iushrn(1);
|
36653 | var z = this.m.bitLength();
|
36654 | z = new BN(2 * z * z).toRed(this);
|
36655 |
|
36656 | while (this.pow(z, lpow).cmp(nOne) !== 0) {
|
36657 | z.redIAdd(nOne);
|
36658 | }
|
36659 |
|
36660 | var c = this.pow(z, q);
|
36661 | var r = this.pow(a, q.addn(1).iushrn(1));
|
36662 | var t = this.pow(a, q);
|
36663 | var m = s;
|
36664 | while (t.cmp(one) !== 0) {
|
36665 | var tmp = t;
|
36666 | for (var i = 0; tmp.cmp(one) !== 0; i++) {
|
36667 | tmp = tmp.redSqr();
|
36668 | }
|
36669 | assert(i < m);
|
36670 | var b = this.pow(c, new BN(1).iushln(m - i - 1));
|
36671 |
|
36672 | r = r.redMul(b);
|
36673 | c = b.redSqr();
|
36674 | t = t.redMul(c);
|
36675 | m = i;
|
36676 | }
|
36677 |
|
36678 | return r;
|
36679 | };
|
36680 |
|
36681 | Red.prototype.invm = function invm (a) {
|
36682 | var inv = a._invmp(this.m);
|
36683 | if (inv.negative !== 0) {
|
36684 | inv.negative = 0;
|
36685 | return this.imod(inv).redNeg();
|
36686 | } else {
|
36687 | return this.imod(inv);
|
36688 | }
|
36689 | };
|
36690 |
|
36691 | Red.prototype.pow = function pow (a, num) {
|
36692 | if (num.isZero()) return new BN(1).toRed(this);
|
36693 | if (num.cmpn(1) === 0) return a.clone();
|
36694 |
|
36695 | var windowSize = 4;
|
36696 | var wnd = new Array(1 << windowSize);
|
36697 | wnd[0] = new BN(1).toRed(this);
|
36698 | wnd[1] = a;
|
36699 | for (var i = 2; i < wnd.length; i++) {
|
36700 | wnd[i] = this.mul(wnd[i - 1], a);
|
36701 | }
|
36702 |
|
36703 | var res = wnd[0];
|
36704 | var current = 0;
|
36705 | var currentLen = 0;
|
36706 | var start = num.bitLength() % 26;
|
36707 | if (start === 0) {
|
36708 | start = 26;
|
36709 | }
|
36710 |
|
36711 | for (i = num.length - 1; i >= 0; i--) {
|
36712 | var word = num.words[i];
|
36713 | for (var j = start - 1; j >= 0; j--) {
|
36714 | var bit = (word >> j) & 1;
|
36715 | if (res !== wnd[0]) {
|
36716 | res = this.sqr(res);
|
36717 | }
|
36718 |
|
36719 | if (bit === 0 && current === 0) {
|
36720 | currentLen = 0;
|
36721 | continue;
|
36722 | }
|
36723 |
|
36724 | current <<= 1;
|
36725 | current |= bit;
|
36726 | currentLen++;
|
36727 | if (currentLen !== windowSize && (i !== 0 || j !== 0)) continue;
|
36728 |
|
36729 | res = this.mul(res, wnd[current]);
|
36730 | currentLen = 0;
|
36731 | current = 0;
|
36732 | }
|
36733 | start = 26;
|
36734 | }
|
36735 |
|
36736 | return res;
|
36737 | };
|
36738 |
|
36739 | Red.prototype.convertTo = function convertTo (num) {
|
36740 | var r = num.umod(this.m);
|
36741 |
|
36742 | return r === num ? r.clone() : r;
|
36743 | };
|
36744 |
|
36745 | Red.prototype.convertFrom = function convertFrom (num) {
|
36746 | var res = num.clone();
|
36747 | res.red = null;
|
36748 | return res;
|
36749 | };
|
36750 |
|
36751 |
|
36752 |
|
36753 |
|
36754 |
|
36755 | BN.mont = function mont (num) {
|
36756 | return new Mont(num);
|
36757 | };
|
36758 |
|
36759 | function Mont (m) {
|
36760 | Red.call(this, m);
|
36761 |
|
36762 | this.shift = this.m.bitLength();
|
36763 | if (this.shift % 26 !== 0) {
|
36764 | this.shift += 26 - (this.shift % 26);
|
36765 | }
|
36766 |
|
36767 | this.r = new BN(1).iushln(this.shift);
|
36768 | this.r2 = this.imod(this.r.sqr());
|
36769 | this.rinv = this.r._invmp(this.m);
|
36770 |
|
36771 | this.minv = this.rinv.mul(this.r).isubn(1).div(this.m);
|
36772 | this.minv = this.minv.umod(this.r);
|
36773 | this.minv = this.r.sub(this.minv);
|
36774 | }
|
36775 | inherits(Mont, Red);
|
36776 |
|
36777 | Mont.prototype.convertTo = function convertTo (num) {
|
36778 | return this.imod(num.ushln(this.shift));
|
36779 | };
|
36780 |
|
36781 | Mont.prototype.convertFrom = function convertFrom (num) {
|
36782 | var r = this.imod(num.mul(this.rinv));
|
36783 | r.red = null;
|
36784 | return r;
|
36785 | };
|
36786 |
|
36787 | Mont.prototype.imul = function imul (a, b) {
|
36788 | if (a.isZero() || b.isZero()) {
|
36789 | a.words[0] = 0;
|
36790 | a.length = 1;
|
36791 | return a;
|
36792 | }
|
36793 |
|
36794 | var t = a.imul(b);
|
36795 | var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
|
36796 | var u = t.isub(c).iushrn(this.shift);
|
36797 | var res = u;
|
36798 |
|
36799 | if (u.cmp(this.m) >= 0) {
|
36800 | res = u.isub(this.m);
|
36801 | } else if (u.cmpn(0) < 0) {
|
36802 | res = u.iadd(this.m);
|
36803 | }
|
36804 |
|
36805 | return res._forceRed(this);
|
36806 | };
|
36807 |
|
36808 | Mont.prototype.mul = function mul (a, b) {
|
36809 | if (a.isZero() || b.isZero()) return new BN(0)._forceRed(this);
|
36810 |
|
36811 | var t = a.mul(b);
|
36812 | var c = t.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m);
|
36813 | var u = t.isub(c).iushrn(this.shift);
|
36814 | var res = u;
|
36815 | if (u.cmp(this.m) >= 0) {
|
36816 | res = u.isub(this.m);
|
36817 | } else if (u.cmpn(0) < 0) {
|
36818 | res = u.iadd(this.m);
|
36819 | }
|
36820 |
|
36821 | return res._forceRed(this);
|
36822 | };
|
36823 |
|
36824 | Mont.prototype.invm = function invm (a) {
|
36825 |
|
36826 | var res = this.imod(a._invmp(this.m).mul(this.r2));
|
36827 | return res._forceRed(this);
|
36828 | };
|
36829 | })(module, commonjsGlobal);
|
36830 | });
|
36831 |
|
36832 | var bn$1 = Object.freeze({
|
36833 | __proto__: null,
|
36834 | 'default': bn,
|
36835 | __moduleExports: bn
|
36836 | });
|
36837 |
|
36838 |
|
36839 |
|
36840 |
|
36841 |
|
36842 |
|
36843 |
|
36844 |
|
36845 |
|
36846 |
|
36847 |
|
36848 |
|
36849 | class BigInteger$1 {
|
36850 | |
36851 |
|
36852 |
|
36853 |
|
36854 |
|
36855 | constructor(n) {
|
36856 | if (n === undefined) {
|
36857 | throw new Error('Invalid BigInteger input');
|
36858 | }
|
36859 |
|
36860 | this.value = new bn(n);
|
36861 | }
|
36862 |
|
36863 | clone() {
|
36864 | const clone = new BigInteger$1(null);
|
36865 | this.value.copy(clone.value);
|
36866 | return clone;
|
36867 | }
|
36868 |
|
36869 | |
36870 |
|
36871 |
|
36872 | iinc() {
|
36873 | this.value.iadd(new bn(1));
|
36874 | return this;
|
36875 | }
|
36876 |
|
36877 | |
36878 |
|
36879 |
|
36880 |
|
36881 | inc() {
|
36882 | return this.clone().iinc();
|
36883 | }
|
36884 |
|
36885 | |
36886 |
|
36887 |
|
36888 | idec() {
|
36889 | this.value.isub(new bn(1));
|
36890 | return this;
|
36891 | }
|
36892 |
|
36893 | |
36894 |
|
36895 |
|
36896 |
|
36897 | dec() {
|
36898 | return this.clone().idec();
|
36899 | }
|
36900 |
|
36901 |
|
36902 | |
36903 |
|
36904 |
|
36905 |
|
36906 | iadd(x) {
|
36907 | this.value.iadd(x.value);
|
36908 | return this;
|
36909 | }
|
36910 |
|
36911 | |
36912 |
|
36913 |
|
36914 |
|
36915 |
|
36916 | add(x) {
|
36917 | return this.clone().iadd(x);
|
36918 | }
|
36919 |
|
36920 | |
36921 |
|
36922 |
|
36923 |
|
36924 | isub(x) {
|
36925 | this.value.isub(x.value);
|
36926 | return this;
|
36927 | }
|
36928 |
|
36929 | |
36930 |
|
36931 |
|
36932 |
|
36933 |
|
36934 | sub(x) {
|
36935 | return this.clone().isub(x);
|
36936 | }
|
36937 |
|
36938 | |
36939 |
|
36940 |
|
36941 |
|
36942 | imul(x) {
|
36943 | this.value.imul(x.value);
|
36944 | return this;
|
36945 | }
|
36946 |
|
36947 | |
36948 |
|
36949 |
|
36950 |
|
36951 |
|
36952 | mul(x) {
|
36953 | return this.clone().imul(x);
|
36954 | }
|
36955 |
|
36956 | |
36957 |
|
36958 |
|
36959 |
|
36960 | imod(m) {
|
36961 | this.value = this.value.umod(m.value);
|
36962 | return this;
|
36963 | }
|
36964 |
|
36965 | |
36966 |
|
36967 |
|
36968 |
|
36969 |
|
36970 | mod(m) {
|
36971 | return this.clone().imod(m);
|
36972 | }
|
36973 |
|
36974 | |
36975 |
|
36976 |
|
36977 |
|
36978 |
|
36979 |
|
36980 |
|
36981 | modExp(e, n) {
|
36982 |
|
36983 |
|
36984 |
|
36985 | const nred = n.isEven() ? bn.red(n.value) : bn.mont(n.value);
|
36986 | const x = this.clone();
|
36987 | x.value = x.value.toRed(nred).redPow(e.value).fromRed();
|
36988 | return x;
|
36989 | }
|
36990 |
|
36991 | |
36992 |
|
36993 |
|
36994 |
|
36995 |
|
36996 |
|
36997 |
|
36998 | modInv(n) {
|
36999 |
|
37000 | if (!this.gcd(n).isOne()) {
|
37001 | throw new Error('Inverse does not exist');
|
37002 | }
|
37003 | return new BigInteger$1(this.value.invm(n.value));
|
37004 | }
|
37005 |
|
37006 | |
37007 |
|
37008 |
|
37009 |
|
37010 |
|
37011 | gcd(n) {
|
37012 | return new BigInteger$1(this.value.gcd(n.value));
|
37013 | }
|
37014 |
|
37015 | |
37016 |
|
37017 |
|
37018 |
|
37019 | ileftShift(x) {
|
37020 | this.value.ishln(x.value.toNumber());
|
37021 | return this;
|
37022 | }
|
37023 |
|
37024 | |
37025 |
|
37026 |
|
37027 |
|
37028 |
|
37029 | leftShift(x) {
|
37030 | return this.clone().ileftShift(x);
|
37031 | }
|
37032 |
|
37033 | |
37034 |
|
37035 |
|
37036 |
|
37037 | irightShift(x) {
|
37038 | this.value.ishrn(x.value.toNumber());
|
37039 | return this;
|
37040 | }
|
37041 |
|
37042 | |
37043 |
|
37044 |
|
37045 |
|
37046 |
|
37047 | rightShift(x) {
|
37048 | return this.clone().irightShift(x);
|
37049 | }
|
37050 |
|
37051 | |
37052 |
|
37053 |
|
37054 |
|
37055 |
|
37056 | equal(x) {
|
37057 | return this.value.eq(x.value);
|
37058 | }
|
37059 |
|
37060 | |
37061 |
|
37062 |
|
37063 |
|
37064 |
|
37065 | lt(x) {
|
37066 | return this.value.lt(x.value);
|
37067 | }
|
37068 |
|
37069 | |
37070 |
|
37071 |
|
37072 |
|
37073 |
|
37074 | lte(x) {
|
37075 | return this.value.lte(x.value);
|
37076 | }
|
37077 |
|
37078 | |
37079 |
|
37080 |
|
37081 |
|
37082 |
|
37083 | gt(x) {
|
37084 | return this.value.gt(x.value);
|
37085 | }
|
37086 |
|
37087 | |
37088 |
|
37089 |
|
37090 |
|
37091 |
|
37092 | gte(x) {
|
37093 | return this.value.gte(x.value);
|
37094 | }
|
37095 |
|
37096 | isZero() {
|
37097 | return this.value.isZero();
|
37098 | }
|
37099 |
|
37100 | isOne() {
|
37101 | return this.value.eq(new bn(1));
|
37102 | }
|
37103 |
|
37104 | isNegative() {
|
37105 | return this.value.isNeg();
|
37106 | }
|
37107 |
|
37108 | isEven() {
|
37109 | return this.value.isEven();
|
37110 | }
|
37111 |
|
37112 | abs() {
|
37113 | const res = this.clone();
|
37114 | res.value = res.value.abs();
|
37115 | return res;
|
37116 | }
|
37117 |
|
37118 | |
37119 |
|
37120 |
|
37121 |
|
37122 | toString() {
|
37123 | return this.value.toString();
|
37124 | }
|
37125 |
|
37126 | |
37127 |
|
37128 |
|
37129 |
|
37130 |
|
37131 | toNumber() {
|
37132 | return this.value.toNumber();
|
37133 | }
|
37134 |
|
37135 | |
37136 |
|
37137 |
|
37138 |
|
37139 |
|
37140 | getBit(i) {
|
37141 | return this.value.testn(i) ? 1 : 0;
|
37142 | }
|
37143 |
|
37144 | |
37145 |
|
37146 |
|
37147 |
|
37148 | bitLength() {
|
37149 | return this.value.bitLength();
|
37150 | }
|
37151 |
|
37152 | |
37153 |
|
37154 |
|
37155 |
|
37156 | byteLength() {
|
37157 | return this.value.byteLength();
|
37158 | }
|
37159 |
|
37160 | |
37161 |
|
37162 |
|
37163 |
|
37164 |
|
37165 |
|
37166 | toUint8Array(endian = 'be', length) {
|
37167 | return this.value.toArrayLike(Uint8Array, endian, length);
|
37168 | }
|
37169 | }
|
37170 |
|
37171 | var bn_interface = Object.freeze({
|
37172 | __proto__: null,
|
37173 | 'default': BigInteger$1
|
37174 | });
|
37175 |
|
37176 | var utils_1 = createCommonjsModule(function (module, exports) {
|
37177 |
|
37178 | var utils = exports;
|
37179 |
|
37180 | function toArray(msg, enc) {
|
37181 | if (Array.isArray(msg))
|
37182 | return msg.slice();
|
37183 | if (!msg)
|
37184 | return [];
|
37185 | var res = [];
|
37186 | if (typeof msg !== 'string') {
|
37187 | for (var i = 0; i < msg.length; i++)
|
37188 | res[i] = msg[i] | 0;
|
37189 | return res;
|
37190 | }
|
37191 | if (enc === 'hex') {
|
37192 | msg = msg.replace(/[^a-z0-9]+/ig, '');
|
37193 | if (msg.length % 2 !== 0)
|
37194 | msg = '0' + msg;
|
37195 | for (var i = 0; i < msg.length; i += 2)
|
37196 | res.push(parseInt(msg[i] + msg[i + 1], 16));
|
37197 | } else {
|
37198 | for (var i = 0; i < msg.length; i++) {
|
37199 | var c = msg.charCodeAt(i);
|
37200 | var hi = c >> 8;
|
37201 | var lo = c & 0xff;
|
37202 | if (hi)
|
37203 | res.push(hi, lo);
|
37204 | else
|
37205 | res.push(lo);
|
37206 | }
|
37207 | }
|
37208 | return res;
|
37209 | }
|
37210 | utils.toArray = toArray;
|
37211 |
|
37212 | function zero2(word) {
|
37213 | if (word.length === 1)
|
37214 | return '0' + word;
|
37215 | else
|
37216 | return word;
|
37217 | }
|
37218 | utils.zero2 = zero2;
|
37219 |
|
37220 | function toHex(msg) {
|
37221 | var res = '';
|
37222 | for (var i = 0; i < msg.length; i++)
|
37223 | res += zero2(msg[i].toString(16));
|
37224 | return res;
|
37225 | }
|
37226 | utils.toHex = toHex;
|
37227 |
|
37228 | utils.encode = function encode(arr, enc) {
|
37229 | if (enc === 'hex')
|
37230 | return toHex(arr);
|
37231 | else
|
37232 | return arr;
|
37233 | };
|
37234 | });
|
37235 |
|
37236 | var utils_1$1 = createCommonjsModule(function (module, exports) {
|
37237 |
|
37238 | var utils = exports;
|
37239 |
|
37240 |
|
37241 |
|
37242 |
|
37243 | utils.assert = minimalisticAssert;
|
37244 | utils.toArray = utils_1.toArray;
|
37245 | utils.zero2 = utils_1.zero2;
|
37246 | utils.toHex = utils_1.toHex;
|
37247 | utils.encode = utils_1.encode;
|
37248 |
|
37249 |
|
37250 | function getNAF(num, w) {
|
37251 | var naf = [];
|
37252 | var ws = 1 << (w + 1);
|
37253 | var k = num.clone();
|
37254 | while (k.cmpn(1) >= 0) {
|
37255 | var z;
|
37256 | if (k.isOdd()) {
|
37257 | var mod = k.andln(ws - 1);
|
37258 | if (mod > (ws >> 1) - 1)
|
37259 | z = (ws >> 1) - mod;
|
37260 | else
|
37261 | z = mod;
|
37262 | k.isubn(z);
|
37263 | } else {
|
37264 | z = 0;
|
37265 | }
|
37266 | naf.push(z);
|
37267 |
|
37268 |
|
37269 | var shift = (k.cmpn(0) !== 0 && k.andln(ws - 1) === 0) ? (w + 1) : 1;
|
37270 | for (var i = 1; i < shift; i++)
|
37271 | naf.push(0);
|
37272 | k.iushrn(shift);
|
37273 | }
|
37274 |
|
37275 | return naf;
|
37276 | }
|
37277 | utils.getNAF = getNAF;
|
37278 |
|
37279 |
|
37280 | function getJSF(k1, k2) {
|
37281 | var jsf = [
|
37282 | [],
|
37283 | []
|
37284 | ];
|
37285 |
|
37286 | k1 = k1.clone();
|
37287 | k2 = k2.clone();
|
37288 | var d1 = 0;
|
37289 | var d2 = 0;
|
37290 | while (k1.cmpn(-d1) > 0 || k2.cmpn(-d2) > 0) {
|
37291 |
|
37292 |
|
37293 | var m14 = (k1.andln(3) + d1) & 3;
|
37294 | var m24 = (k2.andln(3) + d2) & 3;
|
37295 | if (m14 === 3)
|
37296 | m14 = -1;
|
37297 | if (m24 === 3)
|
37298 | m24 = -1;
|
37299 | var u1;
|
37300 | if ((m14 & 1) === 0) {
|
37301 | u1 = 0;
|
37302 | } else {
|
37303 | var m8 = (k1.andln(7) + d1) & 7;
|
37304 | if ((m8 === 3 || m8 === 5) && m24 === 2)
|
37305 | u1 = -m14;
|
37306 | else
|
37307 | u1 = m14;
|
37308 | }
|
37309 | jsf[0].push(u1);
|
37310 |
|
37311 | var u2;
|
37312 | if ((m24 & 1) === 0) {
|
37313 | u2 = 0;
|
37314 | } else {
|
37315 | var m8 = (k2.andln(7) + d2) & 7;
|
37316 | if ((m8 === 3 || m8 === 5) && m14 === 2)
|
37317 | u2 = -m24;
|
37318 | else
|
37319 | u2 = m24;
|
37320 | }
|
37321 | jsf[1].push(u2);
|
37322 |
|
37323 |
|
37324 | if (2 * d1 === u1 + 1)
|
37325 | d1 = 1 - d1;
|
37326 | if (2 * d2 === u2 + 1)
|
37327 | d2 = 1 - d2;
|
37328 | k1.iushrn(1);
|
37329 | k2.iushrn(1);
|
37330 | }
|
37331 |
|
37332 | return jsf;
|
37333 | }
|
37334 | utils.getJSF = getJSF;
|
37335 |
|
37336 | function cachedProperty(obj, name, computer) {
|
37337 | var key = '_' + name;
|
37338 | obj.prototype[name] = function cachedProperty() {
|
37339 | return this[key] !== undefined ? this[key] :
|
37340 | this[key] = computer.call(this);
|
37341 | };
|
37342 | }
|
37343 | utils.cachedProperty = cachedProperty;
|
37344 |
|
37345 | function parseBytes(bytes) {
|
37346 | return typeof bytes === 'string' ? utils.toArray(bytes, 'hex') :
|
37347 | bytes;
|
37348 | }
|
37349 | utils.parseBytes = parseBytes;
|
37350 |
|
37351 | function intFromLE(bytes) {
|
37352 | return new bn(bytes, 'hex', 'le');
|
37353 | }
|
37354 | utils.intFromLE = intFromLE;
|
37355 | });
|
37356 |
|
37357 | var r$1;
|
37358 |
|
37359 | var brorand = function rand(len) {
|
37360 | if (!r$1)
|
37361 | r$1 = new Rand(null);
|
37362 |
|
37363 | return r$1.generate(len);
|
37364 | };
|
37365 |
|
37366 | function Rand(rand) {
|
37367 | this.rand = rand;
|
37368 | }
|
37369 | var Rand_1 = Rand;
|
37370 |
|
37371 | Rand.prototype.generate = function generate(len) {
|
37372 | return this._rand(len);
|
37373 | };
|
37374 |
|
37375 |
|
37376 | Rand.prototype._rand = function _rand(n) {
|
37377 | if (this.rand.getBytes)
|
37378 | return this.rand.getBytes(n);
|
37379 |
|
37380 | var res = new Uint8Array(n);
|
37381 | for (var i = 0; i < res.length; i++)
|
37382 | res[i] = this.rand.getByte();
|
37383 | return res;
|
37384 | };
|
37385 |
|
37386 | if (typeof self === 'object') {
|
37387 | if (self.crypto && self.crypto.getRandomValues) {
|
37388 |
|
37389 | Rand.prototype._rand = function _rand(n) {
|
37390 | var arr = new Uint8Array(n);
|
37391 | self.crypto.getRandomValues(arr);
|
37392 | return arr;
|
37393 | };
|
37394 | } else if (self.msCrypto && self.msCrypto.getRandomValues) {
|
37395 |
|
37396 | Rand.prototype._rand = function _rand(n) {
|
37397 | var arr = new Uint8Array(n);
|
37398 | self.msCrypto.getRandomValues(arr);
|
37399 | return arr;
|
37400 | };
|
37401 |
|
37402 |
|
37403 | } else if (typeof window === 'object') {
|
37404 |
|
37405 | Rand.prototype._rand = function() {
|
37406 | throw new Error('Not implemented yet');
|
37407 | };
|
37408 | }
|
37409 | } else {
|
37410 |
|
37411 | try {
|
37412 | var crypto$2 = void('crypto');
|
37413 | if (typeof crypto$2.randomBytes !== 'function')
|
37414 | throw new Error('Not supported');
|
37415 |
|
37416 | Rand.prototype._rand = function _rand(n) {
|
37417 | return crypto$2.randomBytes(n);
|
37418 | };
|
37419 | } catch (e) {
|
37420 | }
|
37421 | }
|
37422 | brorand.Rand = Rand_1;
|
37423 |
|
37424 | var getNAF = utils_1$1.getNAF;
|
37425 | var getJSF = utils_1$1.getJSF;
|
37426 | var assert$2 = utils_1$1.assert;
|
37427 |
|
37428 | function BaseCurve(type, conf) {
|
37429 | this.type = type;
|
37430 | this.p = new bn(conf.p, 16);
|
37431 |
|
37432 |
|
37433 | this.red = conf.prime ? bn.red(conf.prime) : bn.mont(this.p);
|
37434 |
|
37435 |
|
37436 | this.zero = new bn(0).toRed(this.red);
|
37437 | this.one = new bn(1).toRed(this.red);
|
37438 | this.two = new bn(2).toRed(this.red);
|
37439 |
|
37440 |
|
37441 | this.n = conf.n && new bn(conf.n, 16);
|
37442 | this.g = conf.g && this.pointFromJSON(conf.g, conf.gRed);
|
37443 |
|
37444 |
|
37445 | this._wnafT1 = new Array(4);
|
37446 | this._wnafT2 = new Array(4);
|
37447 | this._wnafT3 = new Array(4);
|
37448 | this._wnafT4 = new Array(4);
|
37449 |
|
37450 |
|
37451 | var adjustCount = this.n && this.p.div(this.n);
|
37452 | if (!adjustCount || adjustCount.cmpn(100) > 0) {
|
37453 | this.redN = null;
|
37454 | } else {
|
37455 | this._maxwellTrick = true;
|
37456 | this.redN = this.n.toRed(this.red);
|
37457 | }
|
37458 | }
|
37459 | var base = BaseCurve;
|
37460 |
|
37461 | BaseCurve.prototype.point = function point() {
|
37462 | throw new Error('Not implemented');
|
37463 | };
|
37464 |
|
37465 | BaseCurve.prototype.validate = function validate() {
|
37466 | throw new Error('Not implemented');
|
37467 | };
|
37468 |
|
37469 | BaseCurve.prototype._fixedNafMul = function _fixedNafMul(p, k) {
|
37470 | assert$2(p.precomputed);
|
37471 | var doubles = p._getDoubles();
|
37472 |
|
37473 | var naf = getNAF(k, 1);
|
37474 | var I = (1 << (doubles.step + 1)) - (doubles.step % 2 === 0 ? 2 : 1);
|
37475 | I /= 3;
|
37476 |
|
37477 |
|
37478 | var repr = [];
|
37479 | for (var j = 0; j < naf.length; j += doubles.step) {
|
37480 | var nafW = 0;
|
37481 | for (var k = j + doubles.step - 1; k >= j; k--)
|
37482 | nafW = (nafW << 1) + naf[k];
|
37483 | repr.push(nafW);
|
37484 | }
|
37485 |
|
37486 | var a = this.jpoint(null, null, null);
|
37487 | var b = this.jpoint(null, null, null);
|
37488 | for (var i = I; i > 0; i--) {
|
37489 | for (var j = 0; j < repr.length; j++) {
|
37490 | var nafW = repr[j];
|
37491 | if (nafW === i)
|
37492 | b = b.mixedAdd(doubles.points[j]);
|
37493 | else if (nafW === -i)
|
37494 | b = b.mixedAdd(doubles.points[j].neg());
|
37495 | }
|
37496 | a = a.add(b);
|
37497 | }
|
37498 | return a.toP();
|
37499 | };
|
37500 |
|
37501 | BaseCurve.prototype._wnafMul = function _wnafMul(p, k) {
|
37502 | var w = 4;
|
37503 |
|
37504 |
|
37505 | var nafPoints = p._getNAFPoints(w);
|
37506 | w = nafPoints.wnd;
|
37507 | var wnd = nafPoints.points;
|
37508 |
|
37509 |
|
37510 | var naf = getNAF(k, w);
|
37511 |
|
37512 |
|
37513 | var acc = this.jpoint(null, null, null);
|
37514 | for (var i = naf.length - 1; i >= 0; i--) {
|
37515 |
|
37516 | for (var k = 0; i >= 0 && naf[i] === 0; i--)
|
37517 | k++;
|
37518 | if (i >= 0)
|
37519 | k++;
|
37520 | acc = acc.dblp(k);
|
37521 |
|
37522 | if (i < 0)
|
37523 | break;
|
37524 | var z = naf[i];
|
37525 | assert$2(z !== 0);
|
37526 | if (p.type === 'affine') {
|
37527 |
|
37528 | if (z > 0)
|
37529 | acc = acc.mixedAdd(wnd[(z - 1) >> 1]);
|
37530 | else
|
37531 | acc = acc.mixedAdd(wnd[(-z - 1) >> 1].neg());
|
37532 | } else {
|
37533 |
|
37534 | if (z > 0)
|
37535 | acc = acc.add(wnd[(z - 1) >> 1]);
|
37536 | else
|
37537 | acc = acc.add(wnd[(-z - 1) >> 1].neg());
|
37538 | }
|
37539 | }
|
37540 | return p.type === 'affine' ? acc.toP() : acc;
|
37541 | };
|
37542 |
|
37543 | BaseCurve.prototype._wnafMulAdd = function _wnafMulAdd(defW,
|
37544 | points,
|
37545 | coeffs,
|
37546 | len,
|
37547 | jacobianResult) {
|
37548 | var wndWidth = this._wnafT1;
|
37549 | var wnd = this._wnafT2;
|
37550 | var naf = this._wnafT3;
|
37551 |
|
37552 |
|
37553 | var max = 0;
|
37554 | for (var i = 0; i < len; i++) {
|
37555 | var p = points[i];
|
37556 | var nafPoints = p._getNAFPoints(defW);
|
37557 | wndWidth[i] = nafPoints.wnd;
|
37558 | wnd[i] = nafPoints.points;
|
37559 | }
|
37560 |
|
37561 |
|
37562 | for (var i = len - 1; i >= 1; i -= 2) {
|
37563 | var a = i - 1;
|
37564 | var b = i;
|
37565 | if (wndWidth[a] !== 1 || wndWidth[b] !== 1) {
|
37566 | naf[a] = getNAF(coeffs[a], wndWidth[a]);
|
37567 | naf[b] = getNAF(coeffs[b], wndWidth[b]);
|
37568 | max = Math.max(naf[a].length, max);
|
37569 | max = Math.max(naf[b].length, max);
|
37570 | continue;
|
37571 | }
|
37572 |
|
37573 | var comb = [
|
37574 | points[a],
|
37575 | null,
|
37576 | null,
|
37577 | points[b]
|
37578 | ];
|
37579 |
|
37580 |
|
37581 | if (points[a].y.cmp(points[b].y) === 0) {
|
37582 | comb[1] = points[a].add(points[b]);
|
37583 | comb[2] = points[a].toJ().mixedAdd(points[b].neg());
|
37584 | } else if (points[a].y.cmp(points[b].y.redNeg()) === 0) {
|
37585 | comb[1] = points[a].toJ().mixedAdd(points[b]);
|
37586 | comb[2] = points[a].add(points[b].neg());
|
37587 | } else {
|
37588 | comb[1] = points[a].toJ().mixedAdd(points[b]);
|
37589 | comb[2] = points[a].toJ().mixedAdd(points[b].neg());
|
37590 | }
|
37591 |
|
37592 | var index = [
|
37593 | -3,
|
37594 | -1,
|
37595 | -5,
|
37596 | -7,
|
37597 | 0,
|
37598 | 7,
|
37599 | 5,
|
37600 | 1,
|
37601 | 3
|
37602 | ];
|
37603 |
|
37604 | var jsf = getJSF(coeffs[a], coeffs[b]);
|
37605 | max = Math.max(jsf[0].length, max);
|
37606 | naf[a] = new Array(max);
|
37607 | naf[b] = new Array(max);
|
37608 | for (var j = 0; j < max; j++) {
|
37609 | var ja = jsf[0][j] | 0;
|
37610 | var jb = jsf[1][j] | 0;
|
37611 |
|
37612 | naf[a][j] = index[(ja + 1) * 3 + (jb + 1)];
|
37613 | naf[b][j] = 0;
|
37614 | wnd[a] = comb;
|
37615 | }
|
37616 | }
|
37617 |
|
37618 | var acc = this.jpoint(null, null, null);
|
37619 | var tmp = this._wnafT4;
|
37620 | for (var i = max; i >= 0; i--) {
|
37621 | var k = 0;
|
37622 |
|
37623 | while (i >= 0) {
|
37624 | var zero = true;
|
37625 | for (var j = 0; j < len; j++) {
|
37626 | tmp[j] = naf[j][i] | 0;
|
37627 | if (tmp[j] !== 0)
|
37628 | zero = false;
|
37629 | }
|
37630 | if (!zero)
|
37631 | break;
|
37632 | k++;
|
37633 | i--;
|
37634 | }
|
37635 | if (i >= 0)
|
37636 | k++;
|
37637 | acc = acc.dblp(k);
|
37638 | if (i < 0)
|
37639 | break;
|
37640 |
|
37641 | for (var j = 0; j < len; j++) {
|
37642 | var z = tmp[j];
|
37643 | var p;
|
37644 | if (z === 0)
|
37645 | continue;
|
37646 | else if (z > 0)
|
37647 | p = wnd[j][(z - 1) >> 1];
|
37648 | else if (z < 0)
|
37649 | p = wnd[j][(-z - 1) >> 1].neg();
|
37650 |
|
37651 | if (p.type === 'affine')
|
37652 | acc = acc.mixedAdd(p);
|
37653 | else
|
37654 | acc = acc.add(p);
|
37655 | }
|
37656 | }
|
37657 |
|
37658 | for (var i = 0; i < len; i++)
|
37659 | wnd[i] = null;
|
37660 |
|
37661 | if (jacobianResult)
|
37662 | return acc;
|
37663 | else
|
37664 | return acc.toP();
|
37665 | };
|
37666 |
|
37667 | function BasePoint(curve, type) {
|
37668 | this.curve = curve;
|
37669 | this.type = type;
|
37670 | this.precomputed = null;
|
37671 | }
|
37672 | BaseCurve.BasePoint = BasePoint;
|
37673 |
|
37674 | BasePoint.prototype.eq = function eq(/*other*/) {
|
37675 | throw new Error('Not implemented');
|
37676 | };
|
37677 |
|
37678 | BasePoint.prototype.validate = function validate() {
|
37679 | return this.curve.validate(this);
|
37680 | };
|
37681 |
|
37682 | BaseCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
|
37683 | bytes = utils_1$1.toArray(bytes, enc);
|
37684 |
|
37685 | var len = this.p.byteLength();
|
37686 |
|
37687 |
|
37688 | if ((bytes[0] === 0x04 || bytes[0] === 0x06 || bytes[0] === 0x07) &&
|
37689 | bytes.length - 1 === 2 * len) {
|
37690 | if (bytes[0] === 0x06)
|
37691 | assert$2(bytes[bytes.length - 1] % 2 === 0);
|
37692 | else if (bytes[0] === 0x07)
|
37693 | assert$2(bytes[bytes.length - 1] % 2 === 1);
|
37694 |
|
37695 | var res = this.point(bytes.slice(1, 1 + len),
|
37696 | bytes.slice(1 + len, 1 + 2 * len));
|
37697 |
|
37698 | return res;
|
37699 | } else if ((bytes[0] === 0x02 || bytes[0] === 0x03) &&
|
37700 | bytes.length - 1 === len) {
|
37701 | return this.pointFromX(bytes.slice(1, 1 + len), bytes[0] === 0x03);
|
37702 | }
|
37703 | throw new Error('Unknown point format');
|
37704 | };
|
37705 |
|
37706 | BasePoint.prototype.encodeCompressed = function encodeCompressed(enc) {
|
37707 | return this.encode(enc, true);
|
37708 | };
|
37709 |
|
37710 | BasePoint.prototype._encode = function _encode(compact) {
|
37711 | var len = this.curve.p.byteLength();
|
37712 | var x = this.getX().toArray('be', len);
|
37713 |
|
37714 | if (compact)
|
37715 | return [ this.getY().isEven() ? 0x02 : 0x03 ].concat(x);
|
37716 |
|
37717 | return [ 0x04 ].concat(x, this.getY().toArray('be', len)) ;
|
37718 | };
|
37719 |
|
37720 | BasePoint.prototype.encode = function encode(enc, compact) {
|
37721 | return utils_1$1.encode(this._encode(compact), enc);
|
37722 | };
|
37723 |
|
37724 | BasePoint.prototype.precompute = function precompute(power) {
|
37725 | if (this.precomputed)
|
37726 | return this;
|
37727 |
|
37728 | var precomputed = {
|
37729 | doubles: null,
|
37730 | naf: null,
|
37731 | beta: null
|
37732 | };
|
37733 | precomputed.naf = this._getNAFPoints(8);
|
37734 | precomputed.doubles = this._getDoubles(4, power);
|
37735 | precomputed.beta = this._getBeta();
|
37736 | this.precomputed = precomputed;
|
37737 |
|
37738 | return this;
|
37739 | };
|
37740 |
|
37741 | BasePoint.prototype._hasDoubles = function _hasDoubles(k) {
|
37742 | if (!this.precomputed)
|
37743 | return false;
|
37744 |
|
37745 | var doubles = this.precomputed.doubles;
|
37746 | if (!doubles)
|
37747 | return false;
|
37748 |
|
37749 | return doubles.points.length >= Math.ceil((k.bitLength() + 1) / doubles.step);
|
37750 | };
|
37751 |
|
37752 | BasePoint.prototype._getDoubles = function _getDoubles(step, power) {
|
37753 | if (this.precomputed && this.precomputed.doubles)
|
37754 | return this.precomputed.doubles;
|
37755 |
|
37756 | var doubles = [ this ];
|
37757 | var acc = this;
|
37758 | for (var i = 0; i < power; i += step) {
|
37759 | for (var j = 0; j < step; j++)
|
37760 | acc = acc.dbl();
|
37761 | doubles.push(acc);
|
37762 | }
|
37763 | return {
|
37764 | step: step,
|
37765 | points: doubles
|
37766 | };
|
37767 | };
|
37768 |
|
37769 | BasePoint.prototype._getNAFPoints = function _getNAFPoints(wnd) {
|
37770 | if (this.precomputed && this.precomputed.naf)
|
37771 | return this.precomputed.naf;
|
37772 |
|
37773 | var res = [ this ];
|
37774 | var max = (1 << wnd) - 1;
|
37775 | var dbl = max === 1 ? null : this.dbl();
|
37776 | for (var i = 1; i < max; i++)
|
37777 | res[i] = res[i - 1].add(dbl);
|
37778 | return {
|
37779 | wnd: wnd,
|
37780 | points: res
|
37781 | };
|
37782 | };
|
37783 |
|
37784 | BasePoint.prototype._getBeta = function _getBeta() {
|
37785 | return null;
|
37786 | };
|
37787 |
|
37788 | BasePoint.prototype.dblp = function dblp(k) {
|
37789 | var r = this;
|
37790 | for (var i = 0; i < k; i++)
|
37791 | r = r.dbl();
|
37792 | return r;
|
37793 | };
|
37794 |
|
37795 | var assert$3 = utils_1$1.assert;
|
37796 |
|
37797 | function ShortCurve(conf) {
|
37798 | base.call(this, 'short', conf);
|
37799 |
|
37800 | this.a = new bn(conf.a, 16).toRed(this.red);
|
37801 | this.b = new bn(conf.b, 16).toRed(this.red);
|
37802 | this.tinv = this.two.redInvm();
|
37803 |
|
37804 | this.zeroA = this.a.fromRed().cmpn(0) === 0;
|
37805 | this.threeA = this.a.fromRed().sub(this.p).cmpn(-3) === 0;
|
37806 |
|
37807 |
|
37808 | this.endo = this._getEndomorphism(conf);
|
37809 | this._endoWnafT1 = new Array(4);
|
37810 | this._endoWnafT2 = new Array(4);
|
37811 | }
|
37812 | inherits_browser(ShortCurve, base);
|
37813 | var short_1 = ShortCurve;
|
37814 |
|
37815 | ShortCurve.prototype._getEndomorphism = function _getEndomorphism(conf) {
|
37816 |
|
37817 | if (!this.zeroA || !this.g || !this.n || this.p.modn(3) !== 1)
|
37818 | return;
|
37819 |
|
37820 |
|
37821 | var beta;
|
37822 | var lambda;
|
37823 | if (conf.beta) {
|
37824 | beta = new bn(conf.beta, 16).toRed(this.red);
|
37825 | } else {
|
37826 | var betas = this._getEndoRoots(this.p);
|
37827 |
|
37828 | beta = betas[0].cmp(betas[1]) < 0 ? betas[0] : betas[1];
|
37829 | beta = beta.toRed(this.red);
|
37830 | }
|
37831 | if (conf.lambda) {
|
37832 | lambda = new bn(conf.lambda, 16);
|
37833 | } else {
|
37834 |
|
37835 | var lambdas = this._getEndoRoots(this.n);
|
37836 | if (this.g.mul(lambdas[0]).x.cmp(this.g.x.redMul(beta)) === 0) {
|
37837 | lambda = lambdas[0];
|
37838 | } else {
|
37839 | lambda = lambdas[1];
|
37840 | assert$3(this.g.mul(lambda).x.cmp(this.g.x.redMul(beta)) === 0);
|
37841 | }
|
37842 | }
|
37843 |
|
37844 |
|
37845 | var basis;
|
37846 | if (conf.basis) {
|
37847 | basis = conf.basis.map(function(vec) {
|
37848 | return {
|
37849 | a: new bn(vec.a, 16),
|
37850 | b: new bn(vec.b, 16)
|
37851 | };
|
37852 | });
|
37853 | } else {
|
37854 | basis = this._getEndoBasis(lambda);
|
37855 | }
|
37856 |
|
37857 | return {
|
37858 | beta: beta,
|
37859 | lambda: lambda,
|
37860 | basis: basis
|
37861 | };
|
37862 | };
|
37863 |
|
37864 | ShortCurve.prototype._getEndoRoots = function _getEndoRoots(num) {
|
37865 |
|
37866 |
|
37867 |
|
37868 | var red = num === this.p ? this.red : bn.mont(num);
|
37869 | var tinv = new bn(2).toRed(red).redInvm();
|
37870 | var ntinv = tinv.redNeg();
|
37871 |
|
37872 | var s = new bn(3).toRed(red).redNeg().redSqrt().redMul(tinv);
|
37873 |
|
37874 | var l1 = ntinv.redAdd(s).fromRed();
|
37875 | var l2 = ntinv.redSub(s).fromRed();
|
37876 | return [ l1, l2 ];
|
37877 | };
|
37878 |
|
37879 | ShortCurve.prototype._getEndoBasis = function _getEndoBasis(lambda) {
|
37880 |
|
37881 | var aprxSqrt = this.n.ushrn(Math.floor(this.n.bitLength() / 2));
|
37882 |
|
37883 |
|
37884 |
|
37885 | var u = lambda;
|
37886 | var v = this.n.clone();
|
37887 | var x1 = new bn(1);
|
37888 | var y1 = new bn(0);
|
37889 | var x2 = new bn(0);
|
37890 | var y2 = new bn(1);
|
37891 |
|
37892 |
|
37893 | var a0;
|
37894 | var b0;
|
37895 |
|
37896 | var a1;
|
37897 | var b1;
|
37898 |
|
37899 | var a2;
|
37900 | var b2;
|
37901 |
|
37902 | var prevR;
|
37903 | var i = 0;
|
37904 | var r;
|
37905 | var x;
|
37906 | while (u.cmpn(0) !== 0) {
|
37907 | var q = v.div(u);
|
37908 | r = v.sub(q.mul(u));
|
37909 | x = x2.sub(q.mul(x1));
|
37910 | var y = y2.sub(q.mul(y1));
|
37911 |
|
37912 | if (!a1 && r.cmp(aprxSqrt) < 0) {
|
37913 | a0 = prevR.neg();
|
37914 | b0 = x1;
|
37915 | a1 = r.neg();
|
37916 | b1 = x;
|
37917 | } else if (a1 && ++i === 2) {
|
37918 | break;
|
37919 | }
|
37920 | prevR = r;
|
37921 |
|
37922 | v = u;
|
37923 | u = r;
|
37924 | x2 = x1;
|
37925 | x1 = x;
|
37926 | y2 = y1;
|
37927 | y1 = y;
|
37928 | }
|
37929 | a2 = r.neg();
|
37930 | b2 = x;
|
37931 |
|
37932 | var len1 = a1.sqr().add(b1.sqr());
|
37933 | var len2 = a2.sqr().add(b2.sqr());
|
37934 | if (len2.cmp(len1) >= 0) {
|
37935 | a2 = a0;
|
37936 | b2 = b0;
|
37937 | }
|
37938 |
|
37939 |
|
37940 | if (a1.negative) {
|
37941 | a1 = a1.neg();
|
37942 | b1 = b1.neg();
|
37943 | }
|
37944 | if (a2.negative) {
|
37945 | a2 = a2.neg();
|
37946 | b2 = b2.neg();
|
37947 | }
|
37948 |
|
37949 | return [
|
37950 | { a: a1, b: b1 },
|
37951 | { a: a2, b: b2 }
|
37952 | ];
|
37953 | };
|
37954 |
|
37955 | ShortCurve.prototype._endoSplit = function _endoSplit(k) {
|
37956 | var basis = this.endo.basis;
|
37957 | var v1 = basis[0];
|
37958 | var v2 = basis[1];
|
37959 |
|
37960 | var c1 = v2.b.mul(k).divRound(this.n);
|
37961 | var c2 = v1.b.neg().mul(k).divRound(this.n);
|
37962 |
|
37963 | var p1 = c1.mul(v1.a);
|
37964 | var p2 = c2.mul(v2.a);
|
37965 | var q1 = c1.mul(v1.b);
|
37966 | var q2 = c2.mul(v2.b);
|
37967 |
|
37968 |
|
37969 | var k1 = k.sub(p1).sub(p2);
|
37970 | var k2 = q1.add(q2).neg();
|
37971 | return { k1: k1, k2: k2 };
|
37972 | };
|
37973 |
|
37974 | ShortCurve.prototype.pointFromX = function pointFromX(x, odd) {
|
37975 | x = new bn(x, 16);
|
37976 | if (!x.red)
|
37977 | x = x.toRed(this.red);
|
37978 |
|
37979 | var y2 = x.redSqr().redMul(x).redIAdd(x.redMul(this.a)).redIAdd(this.b);
|
37980 | var y = y2.redSqrt();
|
37981 | if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
|
37982 | throw new Error('invalid point');
|
37983 |
|
37984 |
|
37985 |
|
37986 | var isOdd = y.fromRed().isOdd();
|
37987 | if (odd && !isOdd || !odd && isOdd)
|
37988 | y = y.redNeg();
|
37989 |
|
37990 | return this.point(x, y);
|
37991 | };
|
37992 |
|
37993 | ShortCurve.prototype.validate = function validate(point) {
|
37994 | if (point.inf)
|
37995 | return true;
|
37996 |
|
37997 | var x = point.x;
|
37998 | var y = point.y;
|
37999 |
|
38000 | var ax = this.a.redMul(x);
|
38001 | var rhs = x.redSqr().redMul(x).redIAdd(ax).redIAdd(this.b);
|
38002 | return y.redSqr().redISub(rhs).cmpn(0) === 0;
|
38003 | };
|
38004 |
|
38005 | ShortCurve.prototype._endoWnafMulAdd =
|
38006 | function _endoWnafMulAdd(points, coeffs, jacobianResult) {
|
38007 | var npoints = this._endoWnafT1;
|
38008 | var ncoeffs = this._endoWnafT2;
|
38009 | for (var i = 0; i < points.length; i++) {
|
38010 | var split = this._endoSplit(coeffs[i]);
|
38011 | var p = points[i];
|
38012 | var beta = p._getBeta();
|
38013 |
|
38014 | if (split.k1.negative) {
|
38015 | split.k1.ineg();
|
38016 | p = p.neg(true);
|
38017 | }
|
38018 | if (split.k2.negative) {
|
38019 | split.k2.ineg();
|
38020 | beta = beta.neg(true);
|
38021 | }
|
38022 |
|
38023 | npoints[i * 2] = p;
|
38024 | npoints[i * 2 + 1] = beta;
|
38025 | ncoeffs[i * 2] = split.k1;
|
38026 | ncoeffs[i * 2 + 1] = split.k2;
|
38027 | }
|
38028 | var res = this._wnafMulAdd(1, npoints, ncoeffs, i * 2, jacobianResult);
|
38029 |
|
38030 |
|
38031 | for (var j = 0; j < i * 2; j++) {
|
38032 | npoints[j] = null;
|
38033 | ncoeffs[j] = null;
|
38034 | }
|
38035 | return res;
|
38036 | };
|
38037 |
|
38038 | function Point(curve, x, y, isRed) {
|
38039 | base.BasePoint.call(this, curve, 'affine');
|
38040 | if (x === null && y === null) {
|
38041 | this.x = null;
|
38042 | this.y = null;
|
38043 | this.inf = true;
|
38044 | } else {
|
38045 | this.x = new bn(x, 16);
|
38046 | this.y = new bn(y, 16);
|
38047 |
|
38048 | if (isRed) {
|
38049 | this.x.forceRed(this.curve.red);
|
38050 | this.y.forceRed(this.curve.red);
|
38051 | }
|
38052 | if (!this.x.red)
|
38053 | this.x = this.x.toRed(this.curve.red);
|
38054 | if (!this.y.red)
|
38055 | this.y = this.y.toRed(this.curve.red);
|
38056 | this.inf = false;
|
38057 | }
|
38058 | }
|
38059 | inherits_browser(Point, base.BasePoint);
|
38060 |
|
38061 | ShortCurve.prototype.point = function point(x, y, isRed) {
|
38062 | return new Point(this, x, y, isRed);
|
38063 | };
|
38064 |
|
38065 | ShortCurve.prototype.pointFromJSON = function pointFromJSON(obj, red) {
|
38066 | return Point.fromJSON(this, obj, red);
|
38067 | };
|
38068 |
|
38069 | Point.prototype._getBeta = function _getBeta() {
|
38070 | if (!this.curve.endo)
|
38071 | return;
|
38072 |
|
38073 | var pre = this.precomputed;
|
38074 | if (pre && pre.beta)
|
38075 | return pre.beta;
|
38076 |
|
38077 | var beta = this.curve.point(this.x.redMul(this.curve.endo.beta), this.y);
|
38078 | if (pre) {
|
38079 | var curve = this.curve;
|
38080 | var endoMul = function(p) {
|
38081 | return curve.point(p.x.redMul(curve.endo.beta), p.y);
|
38082 | };
|
38083 | pre.beta = beta;
|
38084 | beta.precomputed = {
|
38085 | beta: null,
|
38086 | naf: pre.naf && {
|
38087 | wnd: pre.naf.wnd,
|
38088 | points: pre.naf.points.map(endoMul)
|
38089 | },
|
38090 | doubles: pre.doubles && {
|
38091 | step: pre.doubles.step,
|
38092 | points: pre.doubles.points.map(endoMul)
|
38093 | }
|
38094 | };
|
38095 | }
|
38096 | return beta;
|
38097 | };
|
38098 |
|
38099 | Point.prototype.toJSON = function toJSON() {
|
38100 | if (!this.precomputed)
|
38101 | return [ this.x, this.y ];
|
38102 |
|
38103 | return [ this.x, this.y, this.precomputed && {
|
38104 | doubles: this.precomputed.doubles && {
|
38105 | step: this.precomputed.doubles.step,
|
38106 | points: this.precomputed.doubles.points.slice(1)
|
38107 | },
|
38108 | naf: this.precomputed.naf && {
|
38109 | wnd: this.precomputed.naf.wnd,
|
38110 | points: this.precomputed.naf.points.slice(1)
|
38111 | }
|
38112 | } ];
|
38113 | };
|
38114 |
|
38115 | Point.fromJSON = function fromJSON(curve, obj, red) {
|
38116 | if (typeof obj === 'string')
|
38117 | obj = JSON.parse(obj);
|
38118 | var res = curve.point(obj[0], obj[1], red);
|
38119 | if (!obj[2])
|
38120 | return res;
|
38121 |
|
38122 | function obj2point(obj) {
|
38123 | return curve.point(obj[0], obj[1], red);
|
38124 | }
|
38125 |
|
38126 | var pre = obj[2];
|
38127 | res.precomputed = {
|
38128 | beta: null,
|
38129 | doubles: pre.doubles && {
|
38130 | step: pre.doubles.step,
|
38131 | points: [ res ].concat(pre.doubles.points.map(obj2point))
|
38132 | },
|
38133 | naf: pre.naf && {
|
38134 | wnd: pre.naf.wnd,
|
38135 | points: [ res ].concat(pre.naf.points.map(obj2point))
|
38136 | }
|
38137 | };
|
38138 | return res;
|
38139 | };
|
38140 |
|
38141 | Point.prototype.inspect = function inspect() {
|
38142 | if (this.isInfinity())
|
38143 | return '<EC Point Infinity>';
|
38144 | return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
|
38145 | ' y: ' + this.y.fromRed().toString(16, 2) + '>';
|
38146 | };
|
38147 |
|
38148 | Point.prototype.isInfinity = function isInfinity() {
|
38149 | return this.inf;
|
38150 | };
|
38151 |
|
38152 | Point.prototype.add = function add(p) {
|
38153 |
|
38154 | if (this.inf)
|
38155 | return p;
|
38156 |
|
38157 |
|
38158 | if (p.inf)
|
38159 | return this;
|
38160 |
|
38161 |
|
38162 | if (this.eq(p))
|
38163 | return this.dbl();
|
38164 |
|
38165 |
|
38166 | if (this.neg().eq(p))
|
38167 | return this.curve.point(null, null);
|
38168 |
|
38169 |
|
38170 | if (this.x.cmp(p.x) === 0)
|
38171 | return this.curve.point(null, null);
|
38172 |
|
38173 | var c = this.y.redSub(p.y);
|
38174 | if (c.cmpn(0) !== 0)
|
38175 | c = c.redMul(this.x.redSub(p.x).redInvm());
|
38176 | var nx = c.redSqr().redISub(this.x).redISub(p.x);
|
38177 | var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
|
38178 | return this.curve.point(nx, ny);
|
38179 | };
|
38180 |
|
38181 | Point.prototype.dbl = function dbl() {
|
38182 | if (this.inf)
|
38183 | return this;
|
38184 |
|
38185 |
|
38186 | var ys1 = this.y.redAdd(this.y);
|
38187 | if (ys1.cmpn(0) === 0)
|
38188 | return this.curve.point(null, null);
|
38189 |
|
38190 | var a = this.curve.a;
|
38191 |
|
38192 | var x2 = this.x.redSqr();
|
38193 | var dyinv = ys1.redInvm();
|
38194 | var c = x2.redAdd(x2).redIAdd(x2).redIAdd(a).redMul(dyinv);
|
38195 |
|
38196 | var nx = c.redSqr().redISub(this.x.redAdd(this.x));
|
38197 | var ny = c.redMul(this.x.redSub(nx)).redISub(this.y);
|
38198 | return this.curve.point(nx, ny);
|
38199 | };
|
38200 |
|
38201 | Point.prototype.getX = function getX() {
|
38202 | return this.x.fromRed();
|
38203 | };
|
38204 |
|
38205 | Point.prototype.getY = function getY() {
|
38206 | return this.y.fromRed();
|
38207 | };
|
38208 |
|
38209 | Point.prototype.mul = function mul(k) {
|
38210 | k = new bn(k, 16);
|
38211 | if (this.isInfinity())
|
38212 | return this;
|
38213 | else if (this._hasDoubles(k))
|
38214 | return this.curve._fixedNafMul(this, k);
|
38215 | else if (this.curve.endo)
|
38216 | return this.curve._endoWnafMulAdd([ this ], [ k ]);
|
38217 | else
|
38218 | return this.curve._wnafMul(this, k);
|
38219 | };
|
38220 |
|
38221 | Point.prototype.mulAdd = function mulAdd(k1, p2, k2) {
|
38222 | var points = [ this, p2 ];
|
38223 | var coeffs = [ k1, k2 ];
|
38224 | if (this.curve.endo)
|
38225 | return this.curve._endoWnafMulAdd(points, coeffs);
|
38226 | else
|
38227 | return this.curve._wnafMulAdd(1, points, coeffs, 2);
|
38228 | };
|
38229 |
|
38230 | Point.prototype.jmulAdd = function jmulAdd(k1, p2, k2) {
|
38231 | var points = [ this, p2 ];
|
38232 | var coeffs = [ k1, k2 ];
|
38233 | if (this.curve.endo)
|
38234 | return this.curve._endoWnafMulAdd(points, coeffs, true);
|
38235 | else
|
38236 | return this.curve._wnafMulAdd(1, points, coeffs, 2, true);
|
38237 | };
|
38238 |
|
38239 | Point.prototype.eq = function eq(p) {
|
38240 | return this === p ||
|
38241 | this.inf === p.inf &&
|
38242 | (this.inf || this.x.cmp(p.x) === 0 && this.y.cmp(p.y) === 0);
|
38243 | };
|
38244 |
|
38245 | Point.prototype.neg = function neg(_precompute) {
|
38246 | if (this.inf)
|
38247 | return this;
|
38248 |
|
38249 | var res = this.curve.point(this.x, this.y.redNeg());
|
38250 | if (_precompute && this.precomputed) {
|
38251 | var pre = this.precomputed;
|
38252 | var negate = function(p) {
|
38253 | return p.neg();
|
38254 | };
|
38255 | res.precomputed = {
|
38256 | naf: pre.naf && {
|
38257 | wnd: pre.naf.wnd,
|
38258 | points: pre.naf.points.map(negate)
|
38259 | },
|
38260 | doubles: pre.doubles && {
|
38261 | step: pre.doubles.step,
|
38262 | points: pre.doubles.points.map(negate)
|
38263 | }
|
38264 | };
|
38265 | }
|
38266 | return res;
|
38267 | };
|
38268 |
|
38269 | Point.prototype.toJ = function toJ() {
|
38270 | if (this.inf)
|
38271 | return this.curve.jpoint(null, null, null);
|
38272 |
|
38273 | var res = this.curve.jpoint(this.x, this.y, this.curve.one);
|
38274 | return res;
|
38275 | };
|
38276 |
|
38277 | function JPoint(curve, x, y, z) {
|
38278 | base.BasePoint.call(this, curve, 'jacobian');
|
38279 | if (x === null && y === null && z === null) {
|
38280 | this.x = this.curve.one;
|
38281 | this.y = this.curve.one;
|
38282 | this.z = new bn(0);
|
38283 | } else {
|
38284 | this.x = new bn(x, 16);
|
38285 | this.y = new bn(y, 16);
|
38286 | this.z = new bn(z, 16);
|
38287 | }
|
38288 | if (!this.x.red)
|
38289 | this.x = this.x.toRed(this.curve.red);
|
38290 | if (!this.y.red)
|
38291 | this.y = this.y.toRed(this.curve.red);
|
38292 | if (!this.z.red)
|
38293 | this.z = this.z.toRed(this.curve.red);
|
38294 |
|
38295 | this.zOne = this.z === this.curve.one;
|
38296 | }
|
38297 | inherits_browser(JPoint, base.BasePoint);
|
38298 |
|
38299 | ShortCurve.prototype.jpoint = function jpoint(x, y, z) {
|
38300 | return new JPoint(this, x, y, z);
|
38301 | };
|
38302 |
|
38303 | JPoint.prototype.toP = function toP() {
|
38304 | if (this.isInfinity())
|
38305 | return this.curve.point(null, null);
|
38306 |
|
38307 | var zinv = this.z.redInvm();
|
38308 | var zinv2 = zinv.redSqr();
|
38309 | var ax = this.x.redMul(zinv2);
|
38310 | var ay = this.y.redMul(zinv2).redMul(zinv);
|
38311 |
|
38312 | return this.curve.point(ax, ay);
|
38313 | };
|
38314 |
|
38315 | JPoint.prototype.neg = function neg() {
|
38316 | return this.curve.jpoint(this.x, this.y.redNeg(), this.z);
|
38317 | };
|
38318 |
|
38319 | JPoint.prototype.add = function add(p) {
|
38320 |
|
38321 | if (this.isInfinity())
|
38322 | return p;
|
38323 |
|
38324 |
|
38325 | if (p.isInfinity())
|
38326 | return this;
|
38327 |
|
38328 |
|
38329 | var pz2 = p.z.redSqr();
|
38330 | var z2 = this.z.redSqr();
|
38331 | var u1 = this.x.redMul(pz2);
|
38332 | var u2 = p.x.redMul(z2);
|
38333 | var s1 = this.y.redMul(pz2.redMul(p.z));
|
38334 | var s2 = p.y.redMul(z2.redMul(this.z));
|
38335 |
|
38336 | var h = u1.redSub(u2);
|
38337 | var r = s1.redSub(s2);
|
38338 | if (h.cmpn(0) === 0) {
|
38339 | if (r.cmpn(0) !== 0)
|
38340 | return this.curve.jpoint(null, null, null);
|
38341 | else
|
38342 | return this.dbl();
|
38343 | }
|
38344 |
|
38345 | var h2 = h.redSqr();
|
38346 | var h3 = h2.redMul(h);
|
38347 | var v = u1.redMul(h2);
|
38348 |
|
38349 | var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
|
38350 | var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
|
38351 | var nz = this.z.redMul(p.z).redMul(h);
|
38352 |
|
38353 | return this.curve.jpoint(nx, ny, nz);
|
38354 | };
|
38355 |
|
38356 | JPoint.prototype.mixedAdd = function mixedAdd(p) {
|
38357 |
|
38358 | if (this.isInfinity())
|
38359 | return p.toJ();
|
38360 |
|
38361 |
|
38362 | if (p.isInfinity())
|
38363 | return this;
|
38364 |
|
38365 |
|
38366 | var z2 = this.z.redSqr();
|
38367 | var u1 = this.x;
|
38368 | var u2 = p.x.redMul(z2);
|
38369 | var s1 = this.y;
|
38370 | var s2 = p.y.redMul(z2).redMul(this.z);
|
38371 |
|
38372 | var h = u1.redSub(u2);
|
38373 | var r = s1.redSub(s2);
|
38374 | if (h.cmpn(0) === 0) {
|
38375 | if (r.cmpn(0) !== 0)
|
38376 | return this.curve.jpoint(null, null, null);
|
38377 | else
|
38378 | return this.dbl();
|
38379 | }
|
38380 |
|
38381 | var h2 = h.redSqr();
|
38382 | var h3 = h2.redMul(h);
|
38383 | var v = u1.redMul(h2);
|
38384 |
|
38385 | var nx = r.redSqr().redIAdd(h3).redISub(v).redISub(v);
|
38386 | var ny = r.redMul(v.redISub(nx)).redISub(s1.redMul(h3));
|
38387 | var nz = this.z.redMul(h);
|
38388 |
|
38389 | return this.curve.jpoint(nx, ny, nz);
|
38390 | };
|
38391 |
|
38392 | JPoint.prototype.dblp = function dblp(pow) {
|
38393 | if (pow === 0)
|
38394 | return this;
|
38395 | if (this.isInfinity())
|
38396 | return this;
|
38397 | if (!pow)
|
38398 | return this.dbl();
|
38399 |
|
38400 | if (this.curve.zeroA || this.curve.threeA) {
|
38401 | var r = this;
|
38402 | for (var i = 0; i < pow; i++)
|
38403 | r = r.dbl();
|
38404 | return r;
|
38405 | }
|
38406 |
|
38407 |
|
38408 |
|
38409 | var a = this.curve.a;
|
38410 | var tinv = this.curve.tinv;
|
38411 |
|
38412 | var jx = this.x;
|
38413 | var jy = this.y;
|
38414 | var jz = this.z;
|
38415 | var jz4 = jz.redSqr().redSqr();
|
38416 |
|
38417 |
|
38418 | var jyd = jy.redAdd(jy);
|
38419 | for (var i = 0; i < pow; i++) {
|
38420 | var jx2 = jx.redSqr();
|
38421 | var jyd2 = jyd.redSqr();
|
38422 | var jyd4 = jyd2.redSqr();
|
38423 | var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
|
38424 |
|
38425 | var t1 = jx.redMul(jyd2);
|
38426 | var nx = c.redSqr().redISub(t1.redAdd(t1));
|
38427 | var t2 = t1.redISub(nx);
|
38428 | var dny = c.redMul(t2);
|
38429 | dny = dny.redIAdd(dny).redISub(jyd4);
|
38430 | var nz = jyd.redMul(jz);
|
38431 | if (i + 1 < pow)
|
38432 | jz4 = jz4.redMul(jyd4);
|
38433 |
|
38434 | jx = nx;
|
38435 | jz = nz;
|
38436 | jyd = dny;
|
38437 | }
|
38438 |
|
38439 | return this.curve.jpoint(jx, jyd.redMul(tinv), jz);
|
38440 | };
|
38441 |
|
38442 | JPoint.prototype.dbl = function dbl() {
|
38443 | if (this.isInfinity())
|
38444 | return this;
|
38445 |
|
38446 | if (this.curve.zeroA)
|
38447 | return this._zeroDbl();
|
38448 | else if (this.curve.threeA)
|
38449 | return this._threeDbl();
|
38450 | else
|
38451 | return this._dbl();
|
38452 | };
|
38453 |
|
38454 | JPoint.prototype._zeroDbl = function _zeroDbl() {
|
38455 | var nx;
|
38456 | var ny;
|
38457 | var nz;
|
38458 |
|
38459 | if (this.zOne) {
|
38460 |
|
38461 |
|
38462 |
|
38463 |
|
38464 |
|
38465 | var xx = this.x.redSqr();
|
38466 |
|
38467 | var yy = this.y.redSqr();
|
38468 |
|
38469 | var yyyy = yy.redSqr();
|
38470 |
|
38471 | var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
|
38472 | s = s.redIAdd(s);
|
38473 |
|
38474 | var m = xx.redAdd(xx).redIAdd(xx);
|
38475 |
|
38476 | var t = m.redSqr().redISub(s).redISub(s);
|
38477 |
|
38478 |
|
38479 | var yyyy8 = yyyy.redIAdd(yyyy);
|
38480 | yyyy8 = yyyy8.redIAdd(yyyy8);
|
38481 | yyyy8 = yyyy8.redIAdd(yyyy8);
|
38482 |
|
38483 |
|
38484 | nx = t;
|
38485 |
|
38486 | ny = m.redMul(s.redISub(t)).redISub(yyyy8);
|
38487 |
|
38488 | nz = this.y.redAdd(this.y);
|
38489 | } else {
|
38490 |
|
38491 |
|
38492 |
|
38493 |
|
38494 |
|
38495 | var a = this.x.redSqr();
|
38496 |
|
38497 | var b = this.y.redSqr();
|
38498 |
|
38499 | var c = b.redSqr();
|
38500 |
|
38501 | var d = this.x.redAdd(b).redSqr().redISub(a).redISub(c);
|
38502 | d = d.redIAdd(d);
|
38503 |
|
38504 | var e = a.redAdd(a).redIAdd(a);
|
38505 |
|
38506 | var f = e.redSqr();
|
38507 |
|
38508 |
|
38509 | var c8 = c.redIAdd(c);
|
38510 | c8 = c8.redIAdd(c8);
|
38511 | c8 = c8.redIAdd(c8);
|
38512 |
|
38513 |
|
38514 | nx = f.redISub(d).redISub(d);
|
38515 |
|
38516 | ny = e.redMul(d.redISub(nx)).redISub(c8);
|
38517 |
|
38518 | nz = this.y.redMul(this.z);
|
38519 | nz = nz.redIAdd(nz);
|
38520 | }
|
38521 |
|
38522 | return this.curve.jpoint(nx, ny, nz);
|
38523 | };
|
38524 |
|
38525 | JPoint.prototype._threeDbl = function _threeDbl() {
|
38526 | var nx;
|
38527 | var ny;
|
38528 | var nz;
|
38529 |
|
38530 | if (this.zOne) {
|
38531 |
|
38532 |
|
38533 |
|
38534 |
|
38535 |
|
38536 | var xx = this.x.redSqr();
|
38537 |
|
38538 | var yy = this.y.redSqr();
|
38539 |
|
38540 | var yyyy = yy.redSqr();
|
38541 |
|
38542 | var s = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
|
38543 | s = s.redIAdd(s);
|
38544 |
|
38545 | var m = xx.redAdd(xx).redIAdd(xx).redIAdd(this.curve.a);
|
38546 |
|
38547 | var t = m.redSqr().redISub(s).redISub(s);
|
38548 |
|
38549 | nx = t;
|
38550 |
|
38551 | var yyyy8 = yyyy.redIAdd(yyyy);
|
38552 | yyyy8 = yyyy8.redIAdd(yyyy8);
|
38553 | yyyy8 = yyyy8.redIAdd(yyyy8);
|
38554 | ny = m.redMul(s.redISub(t)).redISub(yyyy8);
|
38555 |
|
38556 | nz = this.y.redAdd(this.y);
|
38557 | } else {
|
38558 |
|
38559 |
|
38560 |
|
38561 |
|
38562 | var delta = this.z.redSqr();
|
38563 |
|
38564 | var gamma = this.y.redSqr();
|
38565 |
|
38566 | var beta = this.x.redMul(gamma);
|
38567 |
|
38568 | var alpha = this.x.redSub(delta).redMul(this.x.redAdd(delta));
|
38569 | alpha = alpha.redAdd(alpha).redIAdd(alpha);
|
38570 |
|
38571 | var beta4 = beta.redIAdd(beta);
|
38572 | beta4 = beta4.redIAdd(beta4);
|
38573 | var beta8 = beta4.redAdd(beta4);
|
38574 | nx = alpha.redSqr().redISub(beta8);
|
38575 |
|
38576 | nz = this.y.redAdd(this.z).redSqr().redISub(gamma).redISub(delta);
|
38577 |
|
38578 | var ggamma8 = gamma.redSqr();
|
38579 | ggamma8 = ggamma8.redIAdd(ggamma8);
|
38580 | ggamma8 = ggamma8.redIAdd(ggamma8);
|
38581 | ggamma8 = ggamma8.redIAdd(ggamma8);
|
38582 | ny = alpha.redMul(beta4.redISub(nx)).redISub(ggamma8);
|
38583 | }
|
38584 |
|
38585 | return this.curve.jpoint(nx, ny, nz);
|
38586 | };
|
38587 |
|
38588 | JPoint.prototype._dbl = function _dbl() {
|
38589 | var a = this.curve.a;
|
38590 |
|
38591 |
|
38592 | var jx = this.x;
|
38593 | var jy = this.y;
|
38594 | var jz = this.z;
|
38595 | var jz4 = jz.redSqr().redSqr();
|
38596 |
|
38597 | var jx2 = jx.redSqr();
|
38598 | var jy2 = jy.redSqr();
|
38599 |
|
38600 | var c = jx2.redAdd(jx2).redIAdd(jx2).redIAdd(a.redMul(jz4));
|
38601 |
|
38602 | var jxd4 = jx.redAdd(jx);
|
38603 | jxd4 = jxd4.redIAdd(jxd4);
|
38604 | var t1 = jxd4.redMul(jy2);
|
38605 | var nx = c.redSqr().redISub(t1.redAdd(t1));
|
38606 | var t2 = t1.redISub(nx);
|
38607 |
|
38608 | var jyd8 = jy2.redSqr();
|
38609 | jyd8 = jyd8.redIAdd(jyd8);
|
38610 | jyd8 = jyd8.redIAdd(jyd8);
|
38611 | jyd8 = jyd8.redIAdd(jyd8);
|
38612 | var ny = c.redMul(t2).redISub(jyd8);
|
38613 | var nz = jy.redAdd(jy).redMul(jz);
|
38614 |
|
38615 | return this.curve.jpoint(nx, ny, nz);
|
38616 | };
|
38617 |
|
38618 | JPoint.prototype.trpl = function trpl() {
|
38619 | if (!this.curve.zeroA)
|
38620 | return this.dbl().add(this);
|
38621 |
|
38622 |
|
38623 |
|
38624 |
|
38625 |
|
38626 | var xx = this.x.redSqr();
|
38627 |
|
38628 | var yy = this.y.redSqr();
|
38629 |
|
38630 | var zz = this.z.redSqr();
|
38631 |
|
38632 | var yyyy = yy.redSqr();
|
38633 |
|
38634 | var m = xx.redAdd(xx).redIAdd(xx);
|
38635 |
|
38636 | var mm = m.redSqr();
|
38637 |
|
38638 | var e = this.x.redAdd(yy).redSqr().redISub(xx).redISub(yyyy);
|
38639 | e = e.redIAdd(e);
|
38640 | e = e.redAdd(e).redIAdd(e);
|
38641 | e = e.redISub(mm);
|
38642 |
|
38643 | var ee = e.redSqr();
|
38644 |
|
38645 | var t = yyyy.redIAdd(yyyy);
|
38646 | t = t.redIAdd(t);
|
38647 | t = t.redIAdd(t);
|
38648 | t = t.redIAdd(t);
|
38649 |
|
38650 | var u = m.redIAdd(e).redSqr().redISub(mm).redISub(ee).redISub(t);
|
38651 |
|
38652 | var yyu4 = yy.redMul(u);
|
38653 | yyu4 = yyu4.redIAdd(yyu4);
|
38654 | yyu4 = yyu4.redIAdd(yyu4);
|
38655 | var nx = this.x.redMul(ee).redISub(yyu4);
|
38656 | nx = nx.redIAdd(nx);
|
38657 | nx = nx.redIAdd(nx);
|
38658 |
|
38659 | var ny = this.y.redMul(u.redMul(t.redISub(u)).redISub(e.redMul(ee)));
|
38660 | ny = ny.redIAdd(ny);
|
38661 | ny = ny.redIAdd(ny);
|
38662 | ny = ny.redIAdd(ny);
|
38663 |
|
38664 | var nz = this.z.redAdd(e).redSqr().redISub(zz).redISub(ee);
|
38665 |
|
38666 | return this.curve.jpoint(nx, ny, nz);
|
38667 | };
|
38668 |
|
38669 | JPoint.prototype.mul = function mul(k, kbase) {
|
38670 | k = new bn(k, kbase);
|
38671 |
|
38672 | return this.curve._wnafMul(this, k);
|
38673 | };
|
38674 |
|
38675 | JPoint.prototype.eq = function eq(p) {
|
38676 | if (p.type === 'affine')
|
38677 | return this.eq(p.toJ());
|
38678 |
|
38679 | if (this === p)
|
38680 | return true;
|
38681 |
|
38682 |
|
38683 | var z2 = this.z.redSqr();
|
38684 | var pz2 = p.z.redSqr();
|
38685 | if (this.x.redMul(pz2).redISub(p.x.redMul(z2)).cmpn(0) !== 0)
|
38686 | return false;
|
38687 |
|
38688 |
|
38689 | var z3 = z2.redMul(this.z);
|
38690 | var pz3 = pz2.redMul(p.z);
|
38691 | return this.y.redMul(pz3).redISub(p.y.redMul(z3)).cmpn(0) === 0;
|
38692 | };
|
38693 |
|
38694 | JPoint.prototype.eqXToP = function eqXToP(x) {
|
38695 | var zs = this.z.redSqr();
|
38696 | var rx = x.toRed(this.curve.red).redMul(zs);
|
38697 | if (this.x.cmp(rx) === 0)
|
38698 | return true;
|
38699 |
|
38700 | var xc = x.clone();
|
38701 | var t = this.curve.redN.redMul(zs);
|
38702 | for (;;) {
|
38703 | xc.iadd(this.curve.n);
|
38704 | if (xc.cmp(this.curve.p) >= 0)
|
38705 | return false;
|
38706 |
|
38707 | rx.redIAdd(t);
|
38708 | if (this.x.cmp(rx) === 0)
|
38709 | return true;
|
38710 | }
|
38711 | };
|
38712 |
|
38713 | JPoint.prototype.inspect = function inspect() {
|
38714 | if (this.isInfinity())
|
38715 | return '<EC JPoint Infinity>';
|
38716 | return '<EC JPoint x: ' + this.x.toString(16, 2) +
|
38717 | ' y: ' + this.y.toString(16, 2) +
|
38718 | ' z: ' + this.z.toString(16, 2) + '>';
|
38719 | };
|
38720 |
|
38721 | JPoint.prototype.isInfinity = function isInfinity() {
|
38722 |
|
38723 | return this.z.cmpn(0) === 0;
|
38724 | };
|
38725 |
|
38726 | function MontCurve(conf) {
|
38727 | base.call(this, 'mont', conf);
|
38728 |
|
38729 | this.a = new bn(conf.a, 16).toRed(this.red);
|
38730 | this.b = new bn(conf.b, 16).toRed(this.red);
|
38731 | this.i4 = new bn(4).toRed(this.red).redInvm();
|
38732 | this.two = new bn(2).toRed(this.red);
|
38733 |
|
38734 |
|
38735 | this.a24 = this.i4.redMul(this.a.redAdd(this.two));
|
38736 | }
|
38737 | inherits_browser(MontCurve, base);
|
38738 | var mont = MontCurve;
|
38739 |
|
38740 | MontCurve.prototype.validate = function validate(point) {
|
38741 | var x = point.normalize().x;
|
38742 | var x2 = x.redSqr();
|
38743 | var rhs = x2.redMul(x).redAdd(x2.redMul(this.a)).redAdd(x);
|
38744 | var y = rhs.redSqrt();
|
38745 |
|
38746 | return y.redSqr().cmp(rhs) === 0;
|
38747 | };
|
38748 |
|
38749 | function Point$1(curve, x, z) {
|
38750 | base.BasePoint.call(this, curve, 'projective');
|
38751 | if (x === null && z === null) {
|
38752 | this.x = this.curve.one;
|
38753 | this.z = this.curve.zero;
|
38754 | } else {
|
38755 | this.x = new bn(x, 16);
|
38756 | this.z = new bn(z, 16);
|
38757 | if (!this.x.red)
|
38758 | this.x = this.x.toRed(this.curve.red);
|
38759 | if (!this.z.red)
|
38760 | this.z = this.z.toRed(this.curve.red);
|
38761 | }
|
38762 | }
|
38763 | inherits_browser(Point$1, base.BasePoint);
|
38764 |
|
38765 | MontCurve.prototype.decodePoint = function decodePoint(bytes, enc) {
|
38766 | var bytes = utils_1$1.toArray(bytes, enc);
|
38767 |
|
38768 |
|
38769 |
|
38770 |
|
38771 | if (bytes.length === 33 && bytes[0] === 0x40)
|
38772 | bytes = bytes.slice(1, 33).reverse();
|
38773 | if (bytes.length !== 32)
|
38774 | throw new Error('Unknown point compression format');
|
38775 | return this.point(bytes, 1);
|
38776 | };
|
38777 |
|
38778 | MontCurve.prototype.point = function point(x, z) {
|
38779 | return new Point$1(this, x, z);
|
38780 | };
|
38781 |
|
38782 | MontCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
|
38783 | return Point$1.fromJSON(this, obj);
|
38784 | };
|
38785 |
|
38786 | Point$1.prototype.precompute = function precompute() {
|
38787 |
|
38788 | };
|
38789 |
|
38790 | Point$1.prototype._encode = function _encode(compact) {
|
38791 | var len = this.curve.p.byteLength();
|
38792 |
|
38793 |
|
38794 |
|
38795 | if (compact) {
|
38796 | return [ 0x40 ].concat(this.getX().toArray('le', len));
|
38797 | } else {
|
38798 | return this.getX().toArray('be', len);
|
38799 | }
|
38800 | };
|
38801 |
|
38802 | Point$1.fromJSON = function fromJSON(curve, obj) {
|
38803 | return new Point$1(curve, obj[0], obj[1] || curve.one);
|
38804 | };
|
38805 |
|
38806 | Point$1.prototype.inspect = function inspect() {
|
38807 | if (this.isInfinity())
|
38808 | return '<EC Point Infinity>';
|
38809 | return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
|
38810 | ' z: ' + this.z.fromRed().toString(16, 2) + '>';
|
38811 | };
|
38812 |
|
38813 | Point$1.prototype.isInfinity = function isInfinity() {
|
38814 |
|
38815 | return this.z.cmpn(0) === 0;
|
38816 | };
|
38817 |
|
38818 | Point$1.prototype.dbl = function dbl() {
|
38819 |
|
38820 |
|
38821 |
|
38822 |
|
38823 | var a = this.x.redAdd(this.z);
|
38824 |
|
38825 | var aa = a.redSqr();
|
38826 |
|
38827 | var b = this.x.redSub(this.z);
|
38828 |
|
38829 | var bb = b.redSqr();
|
38830 |
|
38831 | var c = aa.redSub(bb);
|
38832 |
|
38833 | var nx = aa.redMul(bb);
|
38834 |
|
38835 | var nz = c.redMul(bb.redAdd(this.curve.a24.redMul(c)));
|
38836 | return this.curve.point(nx, nz);
|
38837 | };
|
38838 |
|
38839 | Point$1.prototype.add = function add() {
|
38840 | throw new Error('Not supported on Montgomery curve');
|
38841 | };
|
38842 |
|
38843 | Point$1.prototype.diffAdd = function diffAdd(p, diff) {
|
38844 |
|
38845 |
|
38846 |
|
38847 |
|
38848 | var a = this.x.redAdd(this.z);
|
38849 |
|
38850 | var b = this.x.redSub(this.z);
|
38851 |
|
38852 | var c = p.x.redAdd(p.z);
|
38853 |
|
38854 | var d = p.x.redSub(p.z);
|
38855 |
|
38856 | var da = d.redMul(a);
|
38857 |
|
38858 | var cb = c.redMul(b);
|
38859 |
|
38860 | var nx = diff.z.redMul(da.redAdd(cb).redSqr());
|
38861 |
|
38862 | var nz = diff.x.redMul(da.redISub(cb).redSqr());
|
38863 | return this.curve.point(nx, nz);
|
38864 | };
|
38865 |
|
38866 | Point$1.prototype.mul = function mul(k) {
|
38867 | k = new bn(k, 16);
|
38868 |
|
38869 | var t = k.clone();
|
38870 | var a = this;
|
38871 | var b = this.curve.point(null, null);
|
38872 | var c = this;
|
38873 |
|
38874 | for (var bits = []; t.cmpn(0) !== 0; t.iushrn(1))
|
38875 | bits.push(t.andln(1));
|
38876 |
|
38877 | for (var i = bits.length - 1; i >= 0; i--) {
|
38878 | if (bits[i] === 0) {
|
38879 |
|
38880 | a = a.diffAdd(b, c);
|
38881 |
|
38882 | b = b.dbl();
|
38883 | } else {
|
38884 |
|
38885 | b = a.diffAdd(b, c);
|
38886 |
|
38887 | a = a.dbl();
|
38888 | }
|
38889 | }
|
38890 | return b;
|
38891 | };
|
38892 |
|
38893 | Point$1.prototype.mulAdd = function mulAdd() {
|
38894 | throw new Error('Not supported on Montgomery curve');
|
38895 | };
|
38896 |
|
38897 | Point$1.prototype.jumlAdd = function jumlAdd() {
|
38898 | throw new Error('Not supported on Montgomery curve');
|
38899 | };
|
38900 |
|
38901 | Point$1.prototype.eq = function eq(other) {
|
38902 | return this.getX().cmp(other.getX()) === 0;
|
38903 | };
|
38904 |
|
38905 | Point$1.prototype.normalize = function normalize() {
|
38906 | this.x = this.x.redMul(this.z.redInvm());
|
38907 | this.z = this.curve.one;
|
38908 | return this;
|
38909 | };
|
38910 |
|
38911 | Point$1.prototype.getX = function getX() {
|
38912 |
|
38913 | this.normalize();
|
38914 |
|
38915 | return this.x.fromRed();
|
38916 | };
|
38917 |
|
38918 | var assert$4 = utils_1$1.assert;
|
38919 |
|
38920 | function EdwardsCurve(conf) {
|
38921 |
|
38922 | this.twisted = (conf.a | 0) !== 1;
|
38923 | this.mOneA = this.twisted && (conf.a | 0) === -1;
|
38924 | this.extended = this.mOneA;
|
38925 |
|
38926 | base.call(this, 'edwards', conf);
|
38927 |
|
38928 | this.a = new bn(conf.a, 16).umod(this.red.m);
|
38929 | this.a = this.a.toRed(this.red);
|
38930 | this.c = new bn(conf.c, 16).toRed(this.red);
|
38931 | this.c2 = this.c.redSqr();
|
38932 | this.d = new bn(conf.d, 16).toRed(this.red);
|
38933 | this.dd = this.d.redAdd(this.d);
|
38934 |
|
38935 | assert$4(!this.twisted || this.c.fromRed().cmpn(1) === 0);
|
38936 | this.oneC = (conf.c | 0) === 1;
|
38937 | }
|
38938 | inherits_browser(EdwardsCurve, base);
|
38939 | var edwards = EdwardsCurve;
|
38940 |
|
38941 | EdwardsCurve.prototype._mulA = function _mulA(num) {
|
38942 | if (this.mOneA)
|
38943 | return num.redNeg();
|
38944 | else
|
38945 | return this.a.redMul(num);
|
38946 | };
|
38947 |
|
38948 | EdwardsCurve.prototype._mulC = function _mulC(num) {
|
38949 | if (this.oneC)
|
38950 | return num;
|
38951 | else
|
38952 | return this.c.redMul(num);
|
38953 | };
|
38954 |
|
38955 |
|
38956 | EdwardsCurve.prototype.jpoint = function jpoint(x, y, z, t) {
|
38957 | return this.point(x, y, z, t);
|
38958 | };
|
38959 |
|
38960 | EdwardsCurve.prototype.pointFromX = function pointFromX(x, odd) {
|
38961 | x = new bn(x, 16);
|
38962 | if (!x.red)
|
38963 | x = x.toRed(this.red);
|
38964 |
|
38965 | var x2 = x.redSqr();
|
38966 | var rhs = this.c2.redSub(this.a.redMul(x2));
|
38967 | var lhs = this.one.redSub(this.c2.redMul(this.d).redMul(x2));
|
38968 |
|
38969 | var y2 = rhs.redMul(lhs.redInvm());
|
38970 | var y = y2.redSqrt();
|
38971 | if (y.redSqr().redSub(y2).cmp(this.zero) !== 0)
|
38972 | throw new Error('invalid point');
|
38973 |
|
38974 | var isOdd = y.fromRed().isOdd();
|
38975 | if (odd && !isOdd || !odd && isOdd)
|
38976 | y = y.redNeg();
|
38977 |
|
38978 | return this.point(x, y);
|
38979 | };
|
38980 |
|
38981 | EdwardsCurve.prototype.pointFromY = function pointFromY(y, odd) {
|
38982 | y = new bn(y, 16);
|
38983 | if (!y.red)
|
38984 | y = y.toRed(this.red);
|
38985 |
|
38986 |
|
38987 | var y2 = y.redSqr();
|
38988 | var lhs = y2.redSub(this.c2);
|
38989 | var rhs = y2.redMul(this.d).redMul(this.c2).redSub(this.a);
|
38990 | var x2 = lhs.redMul(rhs.redInvm());
|
38991 |
|
38992 | if (x2.cmp(this.zero) === 0) {
|
38993 | if (odd)
|
38994 | throw new Error('invalid point');
|
38995 | else
|
38996 | return this.point(this.zero, y);
|
38997 | }
|
38998 |
|
38999 | var x = x2.redSqrt();
|
39000 | if (x.redSqr().redSub(x2).cmp(this.zero) !== 0)
|
39001 | throw new Error('invalid point');
|
39002 |
|
39003 | if (x.fromRed().isOdd() !== odd)
|
39004 | x = x.redNeg();
|
39005 |
|
39006 | return this.point(x, y);
|
39007 | };
|
39008 |
|
39009 | EdwardsCurve.prototype.validate = function validate(point) {
|
39010 | if (point.isInfinity())
|
39011 | return true;
|
39012 |
|
39013 |
|
39014 | point.normalize();
|
39015 |
|
39016 | var x2 = point.x.redSqr();
|
39017 | var y2 = point.y.redSqr();
|
39018 | var lhs = x2.redMul(this.a).redAdd(y2);
|
39019 | var rhs = this.c2.redMul(this.one.redAdd(this.d.redMul(x2).redMul(y2)));
|
39020 |
|
39021 | return lhs.cmp(rhs) === 0;
|
39022 | };
|
39023 |
|
39024 | function Point$2(curve, x, y, z, t) {
|
39025 | base.BasePoint.call(this, curve, 'projective');
|
39026 | if (x === null && y === null && z === null) {
|
39027 | this.x = this.curve.zero;
|
39028 | this.y = this.curve.one;
|
39029 | this.z = this.curve.one;
|
39030 | this.t = this.curve.zero;
|
39031 | this.zOne = true;
|
39032 | } else {
|
39033 | this.x = new bn(x, 16);
|
39034 | this.y = new bn(y, 16);
|
39035 | this.z = z ? new bn(z, 16) : this.curve.one;
|
39036 | this.t = t && new bn(t, 16);
|
39037 | if (!this.x.red)
|
39038 | this.x = this.x.toRed(this.curve.red);
|
39039 | if (!this.y.red)
|
39040 | this.y = this.y.toRed(this.curve.red);
|
39041 | if (!this.z.red)
|
39042 | this.z = this.z.toRed(this.curve.red);
|
39043 | if (this.t && !this.t.red)
|
39044 | this.t = this.t.toRed(this.curve.red);
|
39045 | this.zOne = this.z === this.curve.one;
|
39046 |
|
39047 |
|
39048 | if (this.curve.extended && !this.t) {
|
39049 | this.t = this.x.redMul(this.y);
|
39050 | if (!this.zOne)
|
39051 | this.t = this.t.redMul(this.z.redInvm());
|
39052 | }
|
39053 | }
|
39054 | }
|
39055 | inherits_browser(Point$2, base.BasePoint);
|
39056 |
|
39057 | EdwardsCurve.prototype.pointFromJSON = function pointFromJSON(obj) {
|
39058 | return Point$2.fromJSON(this, obj);
|
39059 | };
|
39060 |
|
39061 | EdwardsCurve.prototype.point = function point(x, y, z, t) {
|
39062 | return new Point$2(this, x, y, z, t);
|
39063 | };
|
39064 |
|
39065 | Point$2.fromJSON = function fromJSON(curve, obj) {
|
39066 | return new Point$2(curve, obj[0], obj[1], obj[2]);
|
39067 | };
|
39068 |
|
39069 | Point$2.prototype.inspect = function inspect() {
|
39070 | if (this.isInfinity())
|
39071 | return '<EC Point Infinity>';
|
39072 | return '<EC Point x: ' + this.x.fromRed().toString(16, 2) +
|
39073 | ' y: ' + this.y.fromRed().toString(16, 2) +
|
39074 | ' z: ' + this.z.fromRed().toString(16, 2) + '>';
|
39075 | };
|
39076 |
|
39077 | Point$2.prototype.isInfinity = function isInfinity() {
|
39078 |
|
39079 | return this.x.cmpn(0) === 0 &&
|
39080 | (this.y.cmp(this.z) === 0 ||
|
39081 | (this.zOne && this.y.cmp(this.curve.c) === 0));
|
39082 | };
|
39083 |
|
39084 | Point$2.prototype._extDbl = function _extDbl() {
|
39085 |
|
39086 |
|
39087 |
|
39088 |
|
39089 |
|
39090 | var a = this.x.redSqr();
|
39091 |
|
39092 | var b = this.y.redSqr();
|
39093 |
|
39094 | var c = this.z.redSqr();
|
39095 | c = c.redIAdd(c);
|
39096 |
|
39097 | var d = this.curve._mulA(a);
|
39098 |
|
39099 | var e = this.x.redAdd(this.y).redSqr().redISub(a).redISub(b);
|
39100 |
|
39101 | var g = d.redAdd(b);
|
39102 |
|
39103 | var f = g.redSub(c);
|
39104 |
|
39105 | var h = d.redSub(b);
|
39106 |
|
39107 | var nx = e.redMul(f);
|
39108 |
|
39109 | var ny = g.redMul(h);
|
39110 |
|
39111 | var nt = e.redMul(h);
|
39112 |
|
39113 | var nz = f.redMul(g);
|
39114 | return this.curve.point(nx, ny, nz, nt);
|
39115 | };
|
39116 |
|
39117 | Point$2.prototype._projDbl = function _projDbl() {
|
39118 |
|
39119 |
|
39120 |
|
39121 |
|
39122 |
|
39123 |
|
39124 |
|
39125 | var b = this.x.redAdd(this.y).redSqr();
|
39126 |
|
39127 | var c = this.x.redSqr();
|
39128 |
|
39129 | var d = this.y.redSqr();
|
39130 |
|
39131 | var nx;
|
39132 | var ny;
|
39133 | var nz;
|
39134 | if (this.curve.twisted) {
|
39135 |
|
39136 | var e = this.curve._mulA(c);
|
39137 |
|
39138 | var f = e.redAdd(d);
|
39139 | if (this.zOne) {
|
39140 |
|
39141 | nx = b.redSub(c).redSub(d).redMul(f.redSub(this.curve.two));
|
39142 |
|
39143 | ny = f.redMul(e.redSub(d));
|
39144 |
|
39145 | nz = f.redSqr().redSub(f).redSub(f);
|
39146 | } else {
|
39147 |
|
39148 | var h = this.z.redSqr();
|
39149 |
|
39150 | var j = f.redSub(h).redISub(h);
|
39151 |
|
39152 | nx = b.redSub(c).redISub(d).redMul(j);
|
39153 |
|
39154 | ny = f.redMul(e.redSub(d));
|
39155 |
|
39156 | nz = f.redMul(j);
|
39157 | }
|
39158 | } else {
|
39159 |
|
39160 | var e = c.redAdd(d);
|
39161 |
|
39162 | var h = this.curve._mulC(this.z).redSqr();
|
39163 |
|
39164 | var j = e.redSub(h).redSub(h);
|
39165 |
|
39166 | nx = this.curve._mulC(b.redISub(e)).redMul(j);
|
39167 |
|
39168 | ny = this.curve._mulC(e).redMul(c.redISub(d));
|
39169 |
|
39170 | nz = e.redMul(j);
|
39171 | }
|
39172 | return this.curve.point(nx, ny, nz);
|
39173 | };
|
39174 |
|
39175 | Point$2.prototype.dbl = function dbl() {
|
39176 | if (this.isInfinity())
|
39177 | return this;
|
39178 |
|
39179 |
|
39180 | if (this.curve.extended)
|
39181 | return this._extDbl();
|
39182 | else
|
39183 | return this._projDbl();
|
39184 | };
|
39185 |
|
39186 | Point$2.prototype._extAdd = function _extAdd(p) {
|
39187 |
|
39188 |
|
39189 |
|
39190 |
|
39191 |
|
39192 | var a = this.y.redSub(this.x).redMul(p.y.redSub(p.x));
|
39193 |
|
39194 | var b = this.y.redAdd(this.x).redMul(p.y.redAdd(p.x));
|
39195 |
|
39196 | var c = this.t.redMul(this.curve.dd).redMul(p.t);
|
39197 |
|
39198 | var d = this.z.redMul(p.z.redAdd(p.z));
|
39199 |
|
39200 | var e = b.redSub(a);
|
39201 |
|
39202 | var f = d.redSub(c);
|
39203 |
|
39204 | var g = d.redAdd(c);
|
39205 |
|
39206 | var h = b.redAdd(a);
|
39207 |
|
39208 | var nx = e.redMul(f);
|
39209 |
|
39210 | var ny = g.redMul(h);
|
39211 |
|
39212 | var nt = e.redMul(h);
|
39213 |
|
39214 | var nz = f.redMul(g);
|
39215 | return this.curve.point(nx, ny, nz, nt);
|
39216 | };
|
39217 |
|
39218 | Point$2.prototype._projAdd = function _projAdd(p) {
|
39219 |
|
39220 |
|
39221 |
|
39222 |
|
39223 |
|
39224 |
|
39225 | var a = this.z.redMul(p.z);
|
39226 |
|
39227 | var b = a.redSqr();
|
39228 |
|
39229 | var c = this.x.redMul(p.x);
|
39230 |
|
39231 | var d = this.y.redMul(p.y);
|
39232 |
|
39233 | var e = this.curve.d.redMul(c).redMul(d);
|
39234 |
|
39235 | var f = b.redSub(e);
|
39236 |
|
39237 | var g = b.redAdd(e);
|
39238 |
|
39239 | var tmp = this.x.redAdd(this.y).redMul(p.x.redAdd(p.y)).redISub(c).redISub(d);
|
39240 | var nx = a.redMul(f).redMul(tmp);
|
39241 | var ny;
|
39242 | var nz;
|
39243 | if (this.curve.twisted) {
|
39244 |
|
39245 | ny = a.redMul(g).redMul(d.redSub(this.curve._mulA(c)));
|
39246 |
|
39247 | nz = f.redMul(g);
|
39248 | } else {
|
39249 |
|
39250 | ny = a.redMul(g).redMul(d.redSub(c));
|
39251 |
|
39252 | nz = this.curve._mulC(f).redMul(g);
|
39253 | }
|
39254 | return this.curve.point(nx, ny, nz);
|
39255 | };
|
39256 |
|
39257 | Point$2.prototype.add = function add(p) {
|
39258 | if (this.isInfinity())
|
39259 | return p;
|
39260 | if (p.isInfinity())
|
39261 | return this;
|
39262 |
|
39263 | if (this.curve.extended)
|
39264 | return this._extAdd(p);
|
39265 | else
|
39266 | return this._projAdd(p);
|
39267 | };
|
39268 |
|
39269 | Point$2.prototype.mul = function mul(k) {
|
39270 | if (this._hasDoubles(k))
|
39271 | return this.curve._fixedNafMul(this, k);
|
39272 | else
|
39273 | return this.curve._wnafMul(this, k);
|
39274 | };
|
39275 |
|
39276 | Point$2.prototype.mulAdd = function mulAdd(k1, p, k2) {
|
39277 | return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, false);
|
39278 | };
|
39279 |
|
39280 | Point$2.prototype.jmulAdd = function jmulAdd(k1, p, k2) {
|
39281 | return this.curve._wnafMulAdd(1, [ this, p ], [ k1, k2 ], 2, true);
|
39282 | };
|
39283 |
|
39284 | Point$2.prototype.normalize = function normalize() {
|
39285 | if (this.zOne)
|
39286 | return this;
|
39287 |
|
39288 |
|
39289 | var zi = this.z.redInvm();
|
39290 | this.x = this.x.redMul(zi);
|
39291 | this.y = this.y.redMul(zi);
|
39292 | if (this.t)
|
39293 | this.t = this.t.redMul(zi);
|
39294 | this.z = this.curve.one;
|
39295 | this.zOne = true;
|
39296 | return this;
|
39297 | };
|
39298 |
|
39299 | Point$2.prototype.neg = function neg() {
|
39300 | return this.curve.point(this.x.redNeg(),
|
39301 | this.y,
|
39302 | this.z,
|
39303 | this.t && this.t.redNeg());
|
39304 | };
|
39305 |
|
39306 | Point$2.prototype.getX = function getX() {
|
39307 | this.normalize();
|
39308 | return this.x.fromRed();
|
39309 | };
|
39310 |
|
39311 | Point$2.prototype.getY = function getY() {
|
39312 | this.normalize();
|
39313 | return this.y.fromRed();
|
39314 | };
|
39315 |
|
39316 | Point$2.prototype.eq = function eq(other) {
|
39317 | return this === other ||
|
39318 | this.getX().cmp(other.getX()) === 0 &&
|
39319 | this.getY().cmp(other.getY()) === 0;
|
39320 | };
|
39321 |
|
39322 | Point$2.prototype.eqXToP = function eqXToP(x) {
|
39323 | var rx = x.toRed(this.curve.red).redMul(this.z);
|
39324 | if (this.x.cmp(rx) === 0)
|
39325 | return true;
|
39326 |
|
39327 | var xc = x.clone();
|
39328 | var t = this.curve.redN.redMul(this.z);
|
39329 | for (;;) {
|
39330 | xc.iadd(this.curve.n);
|
39331 | if (xc.cmp(this.curve.p) >= 0)
|
39332 | return false;
|
39333 |
|
39334 | rx.redIAdd(t);
|
39335 | if (this.x.cmp(rx) === 0)
|
39336 | return true;
|
39337 | }
|
39338 | };
|
39339 |
|
39340 |
|
39341 | Point$2.prototype.toP = Point$2.prototype.normalize;
|
39342 | Point$2.prototype.mixedAdd = Point$2.prototype.add;
|
39343 |
|
39344 | var curve_1 = createCommonjsModule(function (module, exports) {
|
39345 |
|
39346 | var curve = exports;
|
39347 |
|
39348 | curve.base = base;
|
39349 | curve.short = short_1;
|
39350 | curve.mont = mont;
|
39351 | curve.edwards = edwards;
|
39352 | });
|
39353 |
|
39354 | var rotl32$2 = utils.rotl32;
|
39355 | var sum32$3 = utils.sum32;
|
39356 | var sum32_5$2 = utils.sum32_5;
|
39357 | var ft_1$1 = common$1.ft_1;
|
39358 | var BlockHash$4 = common.BlockHash;
|
39359 |
|
39360 | var sha1_K = [
|
39361 | 0x5A827999, 0x6ED9EBA1,
|
39362 | 0x8F1BBCDC, 0xCA62C1D6
|
39363 | ];
|
39364 |
|
39365 | function SHA1() {
|
39366 | if (!(this instanceof SHA1))
|
39367 | return new SHA1();
|
39368 |
|
39369 | BlockHash$4.call(this);
|
39370 | this.h = [
|
39371 | 0x67452301, 0xefcdab89, 0x98badcfe,
|
39372 | 0x10325476, 0xc3d2e1f0 ];
|
39373 | this.W = new Array(80);
|
39374 | }
|
39375 |
|
39376 | utils.inherits(SHA1, BlockHash$4);
|
39377 | var _1 = SHA1;
|
39378 |
|
39379 | SHA1.blockSize = 512;
|
39380 | SHA1.outSize = 160;
|
39381 | SHA1.hmacStrength = 80;
|
39382 | SHA1.padLength = 64;
|
39383 |
|
39384 | SHA1.prototype._update = function _update(msg, start) {
|
39385 | var W = this.W;
|
39386 |
|
39387 | for (var i = 0; i < 16; i++)
|
39388 | W[i] = msg[start + i];
|
39389 |
|
39390 | for(; i < W.length; i++)
|
39391 | W[i] = rotl32$2(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], 1);
|
39392 |
|
39393 | var a = this.h[0];
|
39394 | var b = this.h[1];
|
39395 | var c = this.h[2];
|
39396 | var d = this.h[3];
|
39397 | var e = this.h[4];
|
39398 |
|
39399 | for (i = 0; i < W.length; i++) {
|
39400 | var s = ~~(i / 20);
|
39401 | var t = sum32_5$2(rotl32$2(a, 5), ft_1$1(s, b, c, d), e, W[i], sha1_K[s]);
|
39402 | e = d;
|
39403 | d = c;
|
39404 | c = rotl32$2(b, 30);
|
39405 | b = a;
|
39406 | a = t;
|
39407 | }
|
39408 |
|
39409 | this.h[0] = sum32$3(this.h[0], a);
|
39410 | this.h[1] = sum32$3(this.h[1], b);
|
39411 | this.h[2] = sum32$3(this.h[2], c);
|
39412 | this.h[3] = sum32$3(this.h[3], d);
|
39413 | this.h[4] = sum32$3(this.h[4], e);
|
39414 | };
|
39415 |
|
39416 | SHA1.prototype._digest = function digest(enc) {
|
39417 | if (enc === 'hex')
|
39418 | return utils.toHex32(this.h, 'big');
|
39419 | else
|
39420 | return utils.split32(this.h, 'big');
|
39421 | };
|
39422 |
|
39423 | var sha1 = _1;
|
39424 | var sha224 = _224;
|
39425 | var sha256 = _256;
|
39426 | var sha384 = _384;
|
39427 | var sha512 = _512;
|
39428 |
|
39429 | var sha = {
|
39430 | sha1: sha1,
|
39431 | sha224: sha224,
|
39432 | sha256: sha256,
|
39433 | sha384: sha384,
|
39434 | sha512: sha512
|
39435 | };
|
39436 |
|
39437 | function Hmac(hash, key, enc) {
|
39438 | if (!(this instanceof Hmac))
|
39439 | return new Hmac(hash, key, enc);
|
39440 | this.Hash = hash;
|
39441 | this.blockSize = hash.blockSize / 8;
|
39442 | this.outSize = hash.outSize / 8;
|
39443 | this.inner = null;
|
39444 | this.outer = null;
|
39445 |
|
39446 | this._init(utils.toArray(key, enc));
|
39447 | }
|
39448 | var hmac = Hmac;
|
39449 |
|
39450 | Hmac.prototype._init = function init(key) {
|
39451 |
|
39452 | if (key.length > this.blockSize)
|
39453 | key = new this.Hash().update(key).digest();
|
39454 | minimalisticAssert(key.length <= this.blockSize);
|
39455 |
|
39456 |
|
39457 | for (var i = key.length; i < this.blockSize; i++)
|
39458 | key.push(0);
|
39459 |
|
39460 | for (i = 0; i < key.length; i++)
|
39461 | key[i] ^= 0x36;
|
39462 | this.inner = new this.Hash().update(key);
|
39463 |
|
39464 |
|
39465 | for (i = 0; i < key.length; i++)
|
39466 | key[i] ^= 0x6a;
|
39467 | this.outer = new this.Hash().update(key);
|
39468 | };
|
39469 |
|
39470 | Hmac.prototype.update = function update(msg, enc) {
|
39471 | this.inner.update(msg, enc);
|
39472 | return this;
|
39473 | };
|
39474 |
|
39475 | Hmac.prototype.digest = function digest(enc) {
|
39476 | this.outer.update(this.inner.digest());
|
39477 | return this.outer.digest(enc);
|
39478 | };
|
39479 |
|
39480 | var hash_1 = createCommonjsModule(function (module, exports) {
|
39481 | var hash = exports;
|
39482 |
|
39483 | hash.utils = utils;
|
39484 | hash.common = common;
|
39485 | hash.sha = sha;
|
39486 | hash.ripemd = ripemd;
|
39487 | hash.hmac = hmac;
|
39488 |
|
39489 |
|
39490 | hash.sha1 = hash.sha.sha1;
|
39491 | hash.sha256 = hash.sha.sha256;
|
39492 | hash.sha224 = hash.sha.sha224;
|
39493 | hash.sha384 = hash.sha.sha384;
|
39494 | hash.sha512 = hash.sha.sha512;
|
39495 | hash.ripemd160 = hash.ripemd.ripemd160;
|
39496 | });
|
39497 |
|
39498 | var secp256k1 = {
|
39499 | doubles: {
|
39500 | step: 4,
|
39501 | points: [
|
39502 | [
|
39503 | 'e60fce93b59e9ec53011aabc21c23e97b2a31369b87a5ae9c44ee89e2a6dec0a',
|
39504 | 'f7e3507399e595929db99f34f57937101296891e44d23f0be1f32cce69616821'
|
39505 | ],
|
39506 | [
|
39507 | '8282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508',
|
39508 | '11f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf'
|
39509 | ],
|
39510 | [
|
39511 | '175e159f728b865a72f99cc6c6fc846de0b93833fd2222ed73fce5b551e5b739',
|
39512 | 'd3506e0d9e3c79eba4ef97a51ff71f5eacb5955add24345c6efa6ffee9fed695'
|
39513 | ],
|
39514 | [
|
39515 | '363d90d447b00c9c99ceac05b6262ee053441c7e55552ffe526bad8f83ff4640',
|
39516 | '4e273adfc732221953b445397f3363145b9a89008199ecb62003c7f3bee9de9'
|
39517 | ],
|
39518 | [
|
39519 | '8b4b5f165df3c2be8c6244b5b745638843e4a781a15bcd1b69f79a55dffdf80c',
|
39520 | '4aad0a6f68d308b4b3fbd7813ab0da04f9e336546162ee56b3eff0c65fd4fd36'
|
39521 | ],
|
39522 | [
|
39523 | '723cbaa6e5db996d6bf771c00bd548c7b700dbffa6c0e77bcb6115925232fcda',
|
39524 | '96e867b5595cc498a921137488824d6e2660a0653779494801dc069d9eb39f5f'
|
39525 | ],
|
39526 | [
|
39527 | 'eebfa4d493bebf98ba5feec812c2d3b50947961237a919839a533eca0e7dd7fa',
|
39528 | '5d9a8ca3970ef0f269ee7edaf178089d9ae4cdc3a711f712ddfd4fdae1de8999'
|
39529 | ],
|
39530 | [
|
39531 | '100f44da696e71672791d0a09b7bde459f1215a29b3c03bfefd7835b39a48db0',
|
39532 | 'cdd9e13192a00b772ec8f3300c090666b7ff4a18ff5195ac0fbd5cd62bc65a09'
|
39533 | ],
|
39534 | [
|
39535 | 'e1031be262c7ed1b1dc9227a4a04c017a77f8d4464f3b3852c8acde6e534fd2d',
|
39536 | '9d7061928940405e6bb6a4176597535af292dd419e1ced79a44f18f29456a00d'
|
39537 | ],
|
39538 | [
|
39539 | 'feea6cae46d55b530ac2839f143bd7ec5cf8b266a41d6af52d5e688d9094696d',
|
39540 | 'e57c6b6c97dce1bab06e4e12bf3ecd5c981c8957cc41442d3155debf18090088'
|
39541 | ],
|
39542 | [
|
39543 | 'da67a91d91049cdcb367be4be6ffca3cfeed657d808583de33fa978bc1ec6cb1',
|
39544 | '9bacaa35481642bc41f463f7ec9780e5dec7adc508f740a17e9ea8e27a68be1d'
|
39545 | ],
|
39546 | [
|
39547 | '53904faa0b334cdda6e000935ef22151ec08d0f7bb11069f57545ccc1a37b7c0',
|
39548 | '5bc087d0bc80106d88c9eccac20d3c1c13999981e14434699dcb096b022771c8'
|
39549 | ],
|
39550 | [
|
39551 | '8e7bcd0bd35983a7719cca7764ca906779b53a043a9b8bcaeff959f43ad86047',
|
39552 | '10b7770b2a3da4b3940310420ca9514579e88e2e47fd68b3ea10047e8460372a'
|
39553 | ],
|
39554 | [
|
39555 | '385eed34c1cdff21e6d0818689b81bde71a7f4f18397e6690a841e1599c43862',
|
39556 | '283bebc3e8ea23f56701de19e9ebf4576b304eec2086dc8cc0458fe5542e5453'
|
39557 | ],
|
39558 | [
|
39559 | '6f9d9b803ecf191637c73a4413dfa180fddf84a5947fbc9c606ed86c3fac3a7',
|
39560 | '7c80c68e603059ba69b8e2a30e45c4d47ea4dd2f5c281002d86890603a842160'
|
39561 | ],
|
39562 | [
|
39563 | '3322d401243c4e2582a2147c104d6ecbf774d163db0f5e5313b7e0e742d0e6bd',
|
39564 | '56e70797e9664ef5bfb019bc4ddaf9b72805f63ea2873af624f3a2e96c28b2a0'
|
39565 | ],
|
39566 | [
|
39567 | '85672c7d2de0b7da2bd1770d89665868741b3f9af7643397721d74d28134ab83',
|
39568 | '7c481b9b5b43b2eb6374049bfa62c2e5e77f17fcc5298f44c8e3094f790313a6'
|
39569 | ],
|
39570 | [
|
39571 | '948bf809b1988a46b06c9f1919413b10f9226c60f668832ffd959af60c82a0a',
|
39572 | '53a562856dcb6646dc6b74c5d1c3418c6d4dff08c97cd2bed4cb7f88d8c8e589'
|
39573 | ],
|
39574 | [
|
39575 | '6260ce7f461801c34f067ce0f02873a8f1b0e44dfc69752accecd819f38fd8e8',
|
39576 | 'bc2da82b6fa5b571a7f09049776a1ef7ecd292238051c198c1a84e95b2b4ae17'
|
39577 | ],
|
39578 | [
|
39579 | 'e5037de0afc1d8d43d8348414bbf4103043ec8f575bfdc432953cc8d2037fa2d',
|
39580 | '4571534baa94d3b5f9f98d09fb990bddbd5f5b03ec481f10e0e5dc841d755bda'
|
39581 | ],
|
39582 | [
|
39583 | 'e06372b0f4a207adf5ea905e8f1771b4e7e8dbd1c6a6c5b725866a0ae4fce725',
|
39584 | '7a908974bce18cfe12a27bb2ad5a488cd7484a7787104870b27034f94eee31dd'
|
39585 | ],
|
39586 | [
|
39587 | '213c7a715cd5d45358d0bbf9dc0ce02204b10bdde2a3f58540ad6908d0559754',
|
39588 | '4b6dad0b5ae462507013ad06245ba190bb4850f5f36a7eeddff2c27534b458f2'
|
39589 | ],
|
39590 | [
|
39591 | '4e7c272a7af4b34e8dbb9352a5419a87e2838c70adc62cddf0cc3a3b08fbd53c',
|
39592 | '17749c766c9d0b18e16fd09f6def681b530b9614bff7dd33e0b3941817dcaae6'
|
39593 | ],
|
39594 | [
|
39595 | 'fea74e3dbe778b1b10f238ad61686aa5c76e3db2be43057632427e2840fb27b6',
|
39596 | '6e0568db9b0b13297cf674deccb6af93126b596b973f7b77701d3db7f23cb96f'
|
39597 | ],
|
39598 | [
|
39599 | '76e64113f677cf0e10a2570d599968d31544e179b760432952c02a4417bdde39',
|
39600 | 'c90ddf8dee4e95cf577066d70681f0d35e2a33d2b56d2032b4b1752d1901ac01'
|
39601 | ],
|
39602 | [
|
39603 | 'c738c56b03b2abe1e8281baa743f8f9a8f7cc643df26cbee3ab150242bcbb891',
|
39604 | '893fb578951ad2537f718f2eacbfbbbb82314eef7880cfe917e735d9699a84c3'
|
39605 | ],
|
39606 | [
|
39607 | 'd895626548b65b81e264c7637c972877d1d72e5f3a925014372e9f6588f6c14b',
|
39608 | 'febfaa38f2bc7eae728ec60818c340eb03428d632bb067e179363ed75d7d991f'
|
39609 | ],
|
39610 | [
|
39611 | 'b8da94032a957518eb0f6433571e8761ceffc73693e84edd49150a564f676e03',
|
39612 | '2804dfa44805a1e4d7c99cc9762808b092cc584d95ff3b511488e4e74efdf6e7'
|
39613 | ],
|
39614 | [
|
39615 | 'e80fea14441fb33a7d8adab9475d7fab2019effb5156a792f1a11778e3c0df5d',
|
39616 | 'eed1de7f638e00771e89768ca3ca94472d155e80af322ea9fcb4291b6ac9ec78'
|
39617 | ],
|
39618 | [
|
39619 | 'a301697bdfcd704313ba48e51d567543f2a182031efd6915ddc07bbcc4e16070',
|
39620 | '7370f91cfb67e4f5081809fa25d40f9b1735dbf7c0a11a130c0d1a041e177ea1'
|
39621 | ],
|
39622 | [
|
39623 | '90ad85b389d6b936463f9d0512678de208cc330b11307fffab7ac63e3fb04ed4',
|
39624 | 'e507a3620a38261affdcbd9427222b839aefabe1582894d991d4d48cb6ef150'
|
39625 | ],
|
39626 | [
|
39627 | '8f68b9d2f63b5f339239c1ad981f162ee88c5678723ea3351b7b444c9ec4c0da',
|
39628 | '662a9f2dba063986de1d90c2b6be215dbbea2cfe95510bfdf23cbf79501fff82'
|
39629 | ],
|
39630 | [
|
39631 | 'e4f3fb0176af85d65ff99ff9198c36091f48e86503681e3e6686fd5053231e11',
|
39632 | '1e63633ad0ef4f1c1661a6d0ea02b7286cc7e74ec951d1c9822c38576feb73bc'
|
39633 | ],
|
39634 | [
|
39635 | '8c00fa9b18ebf331eb961537a45a4266c7034f2f0d4e1d0716fb6eae20eae29e',
|
39636 | 'efa47267fea521a1a9dc343a3736c974c2fadafa81e36c54e7d2a4c66702414b'
|
39637 | ],
|
39638 | [
|
39639 | 'e7a26ce69dd4829f3e10cec0a9e98ed3143d084f308b92c0997fddfc60cb3e41',
|
39640 | '2a758e300fa7984b471b006a1aafbb18d0a6b2c0420e83e20e8a9421cf2cfd51'
|
39641 | ],
|
39642 | [
|
39643 | 'b6459e0ee3662ec8d23540c223bcbdc571cbcb967d79424f3cf29eb3de6b80ef',
|
39644 | '67c876d06f3e06de1dadf16e5661db3c4b3ae6d48e35b2ff30bf0b61a71ba45'
|
39645 | ],
|
39646 | [
|
39647 | 'd68a80c8280bb840793234aa118f06231d6f1fc67e73c5a5deda0f5b496943e8',
|
39648 | 'db8ba9fff4b586d00c4b1f9177b0e28b5b0e7b8f7845295a294c84266b133120'
|
39649 | ],
|
39650 | [
|
39651 | '324aed7df65c804252dc0270907a30b09612aeb973449cea4095980fc28d3d5d',
|
39652 | '648a365774b61f2ff130c0c35aec1f4f19213b0c7e332843967224af96ab7c84'
|
39653 | ],
|
39654 | [
|
39655 | '4df9c14919cde61f6d51dfdbe5fee5dceec4143ba8d1ca888e8bd373fd054c96',
|
39656 | '35ec51092d8728050974c23a1d85d4b5d506cdc288490192ebac06cad10d5d'
|
39657 | ],
|
39658 | [
|
39659 | '9c3919a84a474870faed8a9c1cc66021523489054d7f0308cbfc99c8ac1f98cd',
|
39660 | 'ddb84f0f4a4ddd57584f044bf260e641905326f76c64c8e6be7e5e03d4fc599d'
|
39661 | ],
|
39662 | [
|
39663 | '6057170b1dd12fdf8de05f281d8e06bb91e1493a8b91d4cc5a21382120a959e5',
|
39664 | '9a1af0b26a6a4807add9a2daf71df262465152bc3ee24c65e899be932385a2a8'
|
39665 | ],
|
39666 | [
|
39667 | 'a576df8e23a08411421439a4518da31880cef0fba7d4df12b1a6973eecb94266',
|
39668 | '40a6bf20e76640b2c92b97afe58cd82c432e10a7f514d9f3ee8be11ae1b28ec8'
|
39669 | ],
|
39670 | [
|
39671 | '7778a78c28dec3e30a05fe9629de8c38bb30d1f5cf9a3a208f763889be58ad71',
|
39672 | '34626d9ab5a5b22ff7098e12f2ff580087b38411ff24ac563b513fc1fd9f43ac'
|
39673 | ],
|
39674 | [
|
39675 | '928955ee637a84463729fd30e7afd2ed5f96274e5ad7e5cb09eda9c06d903ac',
|
39676 | 'c25621003d3f42a827b78a13093a95eeac3d26efa8a8d83fc5180e935bcd091f'
|
39677 | ],
|
39678 | [
|
39679 | '85d0fef3ec6db109399064f3a0e3b2855645b4a907ad354527aae75163d82751',
|
39680 | '1f03648413a38c0be29d496e582cf5663e8751e96877331582c237a24eb1f962'
|
39681 | ],
|
39682 | [
|
39683 | 'ff2b0dce97eece97c1c9b6041798b85dfdfb6d8882da20308f5404824526087e',
|
39684 | '493d13fef524ba188af4c4dc54d07936c7b7ed6fb90e2ceb2c951e01f0c29907'
|
39685 | ],
|
39686 | [
|
39687 | '827fbbe4b1e880ea9ed2b2e6301b212b57f1ee148cd6dd28780e5e2cf856e241',
|
39688 | 'c60f9c923c727b0b71bef2c67d1d12687ff7a63186903166d605b68baec293ec'
|
39689 | ],
|
39690 | [
|
39691 | 'eaa649f21f51bdbae7be4ae34ce6e5217a58fdce7f47f9aa7f3b58fa2120e2b3',
|
39692 | 'be3279ed5bbbb03ac69a80f89879aa5a01a6b965f13f7e59d47a5305ba5ad93d'
|
39693 | ],
|
39694 | [
|
39695 | 'e4a42d43c5cf169d9391df6decf42ee541b6d8f0c9a137401e23632dda34d24f',
|
39696 | '4d9f92e716d1c73526fc99ccfb8ad34ce886eedfa8d8e4f13a7f7131deba9414'
|
39697 | ],
|
39698 | [
|
39699 | '1ec80fef360cbdd954160fadab352b6b92b53576a88fea4947173b9d4300bf19',
|
39700 | 'aeefe93756b5340d2f3a4958a7abbf5e0146e77f6295a07b671cdc1cc107cefd'
|
39701 | ],
|
39702 | [
|
39703 | '146a778c04670c2f91b00af4680dfa8bce3490717d58ba889ddb5928366642be',
|
39704 | 'b318e0ec3354028add669827f9d4b2870aaa971d2f7e5ed1d0b297483d83efd0'
|
39705 | ],
|
39706 | [
|
39707 | 'fa50c0f61d22e5f07e3acebb1aa07b128d0012209a28b9776d76a8793180eef9',
|
39708 | '6b84c6922397eba9b72cd2872281a68a5e683293a57a213b38cd8d7d3f4f2811'
|
39709 | ],
|
39710 | [
|
39711 | 'da1d61d0ca721a11b1a5bf6b7d88e8421a288ab5d5bba5220e53d32b5f067ec2',
|
39712 | '8157f55a7c99306c79c0766161c91e2966a73899d279b48a655fba0f1ad836f1'
|
39713 | ],
|
39714 | [
|
39715 | 'a8e282ff0c9706907215ff98e8fd416615311de0446f1e062a73b0610d064e13',
|
39716 | '7f97355b8db81c09abfb7f3c5b2515888b679a3e50dd6bd6cef7c73111f4cc0c'
|
39717 | ],
|
39718 | [
|
39719 | '174a53b9c9a285872d39e56e6913cab15d59b1fa512508c022f382de8319497c',
|
39720 | 'ccc9dc37abfc9c1657b4155f2c47f9e6646b3a1d8cb9854383da13ac079afa73'
|
39721 | ],
|
39722 | [
|
39723 | '959396981943785c3d3e57edf5018cdbe039e730e4918b3d884fdff09475b7ba',
|
39724 | '2e7e552888c331dd8ba0386a4b9cd6849c653f64c8709385e9b8abf87524f2fd'
|
39725 | ],
|
39726 | [
|
39727 | 'd2a63a50ae401e56d645a1153b109a8fcca0a43d561fba2dbb51340c9d82b151',
|
39728 | 'e82d86fb6443fcb7565aee58b2948220a70f750af484ca52d4142174dcf89405'
|
39729 | ],
|
39730 | [
|
39731 | '64587e2335471eb890ee7896d7cfdc866bacbdbd3839317b3436f9b45617e073',
|
39732 | 'd99fcdd5bf6902e2ae96dd6447c299a185b90a39133aeab358299e5e9faf6589'
|
39733 | ],
|
39734 | [
|
39735 | '8481bde0e4e4d885b3a546d3e549de042f0aa6cea250e7fd358d6c86dd45e458',
|
39736 | '38ee7b8cba5404dd84a25bf39cecb2ca900a79c42b262e556d64b1b59779057e'
|
39737 | ],
|
39738 | [
|
39739 | '13464a57a78102aa62b6979ae817f4637ffcfed3c4b1ce30bcd6303f6caf666b',
|
39740 | '69be159004614580ef7e433453ccb0ca48f300a81d0942e13f495a907f6ecc27'
|
39741 | ],
|
39742 | [
|
39743 | 'bc4a9df5b713fe2e9aef430bcc1dc97a0cd9ccede2f28588cada3a0d2d83f366',
|
39744 | 'd3a81ca6e785c06383937adf4b798caa6e8a9fbfa547b16d758d666581f33c1'
|
39745 | ],
|
39746 | [
|
39747 | '8c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa',
|
39748 | '40a30463a3305193378fedf31f7cc0eb7ae784f0451cb9459e71dc73cbef9482'
|
39749 | ],
|
39750 | [
|
39751 | '8ea9666139527a8c1dd94ce4f071fd23c8b350c5a4bb33748c4ba111faccae0',
|
39752 | '620efabbc8ee2782e24e7c0cfb95c5d735b783be9cf0f8e955af34a30e62b945'
|
39753 | ],
|
39754 | [
|
39755 | 'dd3625faef5ba06074669716bbd3788d89bdde815959968092f76cc4eb9a9787',
|
39756 | '7a188fa3520e30d461da2501045731ca941461982883395937f68d00c644a573'
|
39757 | ],
|
39758 | [
|
39759 | 'f710d79d9eb962297e4f6232b40e8f7feb2bc63814614d692c12de752408221e',
|
39760 | 'ea98e67232d3b3295d3b535532115ccac8612c721851617526ae47a9c77bfc82'
|
39761 | ]
|
39762 | ]
|
39763 | },
|
39764 | naf: {
|
39765 | wnd: 7,
|
39766 | points: [
|
39767 | [
|
39768 | 'f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9',
|
39769 | '388f7b0f632de8140fe337e62a37f3566500a99934c2231b6cb9fd7584b8e672'
|
39770 | ],
|
39771 | [
|
39772 | '2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4',
|
39773 | 'd8ac222636e5e3d6d4dba9dda6c9c426f788271bab0d6840dca87d3aa6ac62d6'
|
39774 | ],
|
39775 | [
|
39776 | '5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc',
|
39777 | '6aebca40ba255960a3178d6d861a54dba813d0b813fde7b5a5082628087264da'
|
39778 | ],
|
39779 | [
|
39780 | 'acd484e2f0c7f65309ad178a9f559abde09796974c57e714c35f110dfc27ccbe',
|
39781 | 'cc338921b0a7d9fd64380971763b61e9add888a4375f8e0f05cc262ac64f9c37'
|
39782 | ],
|
39783 | [
|
39784 | '774ae7f858a9411e5ef4246b70c65aac5649980be5c17891bbec17895da008cb',
|
39785 | 'd984a032eb6b5e190243dd56d7b7b365372db1e2dff9d6a8301d74c9c953c61b'
|
39786 | ],
|
39787 | [
|
39788 | 'f28773c2d975288bc7d1d205c3748651b075fbc6610e58cddeeddf8f19405aa8',
|
39789 | 'ab0902e8d880a89758212eb65cdaf473a1a06da521fa91f29b5cb52db03ed81'
|
39790 | ],
|
39791 | [
|
39792 | 'd7924d4f7d43ea965a465ae3095ff41131e5946f3c85f79e44adbcf8e27e080e',
|
39793 | '581e2872a86c72a683842ec228cc6defea40af2bd896d3a5c504dc9ff6a26b58'
|
39794 | ],
|
39795 | [
|
39796 | 'defdea4cdb677750a420fee807eacf21eb9898ae79b9768766e4faa04a2d4a34',
|
39797 | '4211ab0694635168e997b0ead2a93daeced1f4a04a95c0f6cfb199f69e56eb77'
|
39798 | ],
|
39799 | [
|
39800 | '2b4ea0a797a443d293ef5cff444f4979f06acfebd7e86d277475656138385b6c',
|
39801 | '85e89bc037945d93b343083b5a1c86131a01f60c50269763b570c854e5c09b7a'
|
39802 | ],
|
39803 | [
|
39804 | '352bbf4a4cdd12564f93fa332ce333301d9ad40271f8107181340aef25be59d5',
|
39805 | '321eb4075348f534d59c18259dda3e1f4a1b3b2e71b1039c67bd3d8bcf81998c'
|
39806 | ],
|
39807 | [
|
39808 | '2fa2104d6b38d11b0230010559879124e42ab8dfeff5ff29dc9cdadd4ecacc3f',
|
39809 | '2de1068295dd865b64569335bd5dd80181d70ecfc882648423ba76b532b7d67'
|
39810 | ],
|
39811 | [
|
39812 | '9248279b09b4d68dab21a9b066edda83263c3d84e09572e269ca0cd7f5453714',
|
39813 | '73016f7bf234aade5d1aa71bdea2b1ff3fc0de2a887912ffe54a32ce97cb3402'
|
39814 | ],
|
39815 | [
|
39816 | 'daed4f2be3a8bf278e70132fb0beb7522f570e144bf615c07e996d443dee8729',
|
39817 | 'a69dce4a7d6c98e8d4a1aca87ef8d7003f83c230f3afa726ab40e52290be1c55'
|
39818 | ],
|
39819 | [
|
39820 | 'c44d12c7065d812e8acf28d7cbb19f9011ecd9e9fdf281b0e6a3b5e87d22e7db',
|
39821 | '2119a460ce326cdc76c45926c982fdac0e106e861edf61c5a039063f0e0e6482'
|
39822 | ],
|
39823 | [
|
39824 | '6a245bf6dc698504c89a20cfded60853152b695336c28063b61c65cbd269e6b4',
|
39825 | 'e022cf42c2bd4a708b3f5126f16a24ad8b33ba48d0423b6efd5e6348100d8a82'
|
39826 | ],
|
39827 | [
|
39828 | '1697ffa6fd9de627c077e3d2fe541084ce13300b0bec1146f95ae57f0d0bd6a5',
|
39829 | 'b9c398f186806f5d27561506e4557433a2cf15009e498ae7adee9d63d01b2396'
|
39830 | ],
|
39831 | [
|
39832 | '605bdb019981718b986d0f07e834cb0d9deb8360ffb7f61df982345ef27a7479',
|
39833 | '2972d2de4f8d20681a78d93ec96fe23c26bfae84fb14db43b01e1e9056b8c49'
|
39834 | ],
|
39835 | [
|
39836 | '62d14dab4150bf497402fdc45a215e10dcb01c354959b10cfe31c7e9d87ff33d',
|
39837 | '80fc06bd8cc5b01098088a1950eed0db01aa132967ab472235f5642483b25eaf'
|
39838 | ],
|
39839 | [
|
39840 | '80c60ad0040f27dade5b4b06c408e56b2c50e9f56b9b8b425e555c2f86308b6f',
|
39841 | '1c38303f1cc5c30f26e66bad7fe72f70a65eed4cbe7024eb1aa01f56430bd57a'
|
39842 | ],
|
39843 | [
|
39844 | '7a9375ad6167ad54aa74c6348cc54d344cc5dc9487d847049d5eabb0fa03c8fb',
|
39845 | 'd0e3fa9eca8726909559e0d79269046bdc59ea10c70ce2b02d499ec224dc7f7'
|
39846 | ],
|
39847 | [
|
39848 | 'd528ecd9b696b54c907a9ed045447a79bb408ec39b68df504bb51f459bc3ffc9',
|
39849 | 'eecf41253136e5f99966f21881fd656ebc4345405c520dbc063465b521409933'
|
39850 | ],
|
39851 | [
|
39852 | '49370a4b5f43412ea25f514e8ecdad05266115e4a7ecb1387231808f8b45963',
|
39853 | '758f3f41afd6ed428b3081b0512fd62a54c3f3afbb5b6764b653052a12949c9a'
|
39854 | ],
|
39855 | [
|
39856 | '77f230936ee88cbbd73df930d64702ef881d811e0e1498e2f1c13eb1fc345d74',
|
39857 | '958ef42a7886b6400a08266e9ba1b37896c95330d97077cbbe8eb3c7671c60d6'
|
39858 | ],
|
39859 | [
|
39860 | 'f2dac991cc4ce4b9ea44887e5c7c0bce58c80074ab9d4dbaeb28531b7739f530',
|
39861 | 'e0dedc9b3b2f8dad4da1f32dec2531df9eb5fbeb0598e4fd1a117dba703a3c37'
|
39862 | ],
|
39863 | [
|
39864 | '463b3d9f662621fb1b4be8fbbe2520125a216cdfc9dae3debcba4850c690d45b',
|
39865 | '5ed430d78c296c3543114306dd8622d7c622e27c970a1de31cb377b01af7307e'
|
39866 | ],
|
39867 | [
|
39868 | 'f16f804244e46e2a09232d4aff3b59976b98fac14328a2d1a32496b49998f247',
|
39869 | 'cedabd9b82203f7e13d206fcdf4e33d92a6c53c26e5cce26d6579962c4e31df6'
|
39870 | ],
|
39871 | [
|
39872 | 'caf754272dc84563b0352b7a14311af55d245315ace27c65369e15f7151d41d1',
|
39873 | 'cb474660ef35f5f2a41b643fa5e460575f4fa9b7962232a5c32f908318a04476'
|
39874 | ],
|
39875 | [
|
39876 | '2600ca4b282cb986f85d0f1709979d8b44a09c07cb86d7c124497bc86f082120',
|
39877 | '4119b88753c15bd6a693b03fcddbb45d5ac6be74ab5f0ef44b0be9475a7e4b40'
|
39878 | ],
|
39879 | [
|
39880 | '7635ca72d7e8432c338ec53cd12220bc01c48685e24f7dc8c602a7746998e435',
|
39881 | '91b649609489d613d1d5e590f78e6d74ecfc061d57048bad9e76f302c5b9c61'
|
39882 | ],
|
39883 | [
|
39884 | '754e3239f325570cdbbf4a87deee8a66b7f2b33479d468fbc1a50743bf56cc18',
|
39885 | '673fb86e5bda30fb3cd0ed304ea49a023ee33d0197a695d0c5d98093c536683'
|
39886 | ],
|
39887 | [
|
39888 | 'e3e6bd1071a1e96aff57859c82d570f0330800661d1c952f9fe2694691d9b9e8',
|
39889 | '59c9e0bba394e76f40c0aa58379a3cb6a5a2283993e90c4167002af4920e37f5'
|
39890 | ],
|
39891 | [
|
39892 | '186b483d056a033826ae73d88f732985c4ccb1f32ba35f4b4cc47fdcf04aa6eb',
|
39893 | '3b952d32c67cf77e2e17446e204180ab21fb8090895138b4a4a797f86e80888b'
|
39894 | ],
|
39895 | [
|
39896 | 'df9d70a6b9876ce544c98561f4be4f725442e6d2b737d9c91a8321724ce0963f',
|
39897 | '55eb2dafd84d6ccd5f862b785dc39d4ab157222720ef9da217b8c45cf2ba2417'
|
39898 | ],
|
39899 | [
|
39900 | '5edd5cc23c51e87a497ca815d5dce0f8ab52554f849ed8995de64c5f34ce7143',
|
39901 | 'efae9c8dbc14130661e8cec030c89ad0c13c66c0d17a2905cdc706ab7399a868'
|
39902 | ],
|
39903 | [
|
39904 | '290798c2b6476830da12fe02287e9e777aa3fba1c355b17a722d362f84614fba',
|
39905 | 'e38da76dcd440621988d00bcf79af25d5b29c094db2a23146d003afd41943e7a'
|
39906 | ],
|
39907 | [
|
39908 | 'af3c423a95d9f5b3054754efa150ac39cd29552fe360257362dfdecef4053b45',
|
39909 | 'f98a3fd831eb2b749a93b0e6f35cfb40c8cd5aa667a15581bc2feded498fd9c6'
|
39910 | ],
|
39911 | [
|
39912 | '766dbb24d134e745cccaa28c99bf274906bb66b26dcf98df8d2fed50d884249a',
|
39913 | '744b1152eacbe5e38dcc887980da38b897584a65fa06cedd2c924f97cbac5996'
|
39914 | ],
|
39915 | [
|
39916 | '59dbf46f8c94759ba21277c33784f41645f7b44f6c596a58ce92e666191abe3e',
|
39917 | 'c534ad44175fbc300f4ea6ce648309a042ce739a7919798cd85e216c4a307f6e'
|
39918 | ],
|
39919 | [
|
39920 | 'f13ada95103c4537305e691e74e9a4a8dd647e711a95e73cb62dc6018cfd87b8',
|
39921 | 'e13817b44ee14de663bf4bc808341f326949e21a6a75c2570778419bdaf5733d'
|
39922 | ],
|
39923 | [
|
39924 | '7754b4fa0e8aced06d4167a2c59cca4cda1869c06ebadfb6488550015a88522c',
|
39925 | '30e93e864e669d82224b967c3020b8fa8d1e4e350b6cbcc537a48b57841163a2'
|
39926 | ],
|
39927 | [
|
39928 | '948dcadf5990e048aa3874d46abef9d701858f95de8041d2a6828c99e2262519',
|
39929 | 'e491a42537f6e597d5d28a3224b1bc25df9154efbd2ef1d2cbba2cae5347d57e'
|
39930 | ],
|
39931 | [
|
39932 | '7962414450c76c1689c7b48f8202ec37fb224cf5ac0bfa1570328a8a3d7c77ab',
|
39933 | '100b610ec4ffb4760d5c1fc133ef6f6b12507a051f04ac5760afa5b29db83437'
|
39934 | ],
|
39935 | [
|
39936 | '3514087834964b54b15b160644d915485a16977225b8847bb0dd085137ec47ca',
|
39937 | 'ef0afbb2056205448e1652c48e8127fc6039e77c15c2378b7e7d15a0de293311'
|
39938 | ],
|
39939 | [
|
39940 | 'd3cc30ad6b483e4bc79ce2c9dd8bc54993e947eb8df787b442943d3f7b527eaf',
|
39941 | '8b378a22d827278d89c5e9be8f9508ae3c2ad46290358630afb34db04eede0a4'
|
39942 | ],
|
39943 | [
|
39944 | '1624d84780732860ce1c78fcbfefe08b2b29823db913f6493975ba0ff4847610',
|
39945 | '68651cf9b6da903e0914448c6cd9d4ca896878f5282be4c8cc06e2a404078575'
|
39946 | ],
|
39947 | [
|
39948 | '733ce80da955a8a26902c95633e62a985192474b5af207da6df7b4fd5fc61cd4',
|
39949 | 'f5435a2bd2badf7d485a4d8b8db9fcce3e1ef8e0201e4578c54673bc1dc5ea1d'
|
39950 | ],
|
39951 | [
|
39952 | '15d9441254945064cf1a1c33bbd3b49f8966c5092171e699ef258dfab81c045c',
|
39953 | 'd56eb30b69463e7234f5137b73b84177434800bacebfc685fc37bbe9efe4070d'
|
39954 | ],
|
39955 | [
|
39956 | 'a1d0fcf2ec9de675b612136e5ce70d271c21417c9d2b8aaaac138599d0717940',
|
39957 | 'edd77f50bcb5a3cab2e90737309667f2641462a54070f3d519212d39c197a629'
|
39958 | ],
|
39959 | [
|
39960 | 'e22fbe15c0af8ccc5780c0735f84dbe9a790badee8245c06c7ca37331cb36980',
|
39961 | 'a855babad5cd60c88b430a69f53a1a7a38289154964799be43d06d77d31da06'
|
39962 | ],
|
39963 | [
|
39964 | '311091dd9860e8e20ee13473c1155f5f69635e394704eaa74009452246cfa9b3',
|
39965 | '66db656f87d1f04fffd1f04788c06830871ec5a64feee685bd80f0b1286d8374'
|
39966 | ],
|
39967 | [
|
39968 | '34c1fd04d301be89b31c0442d3e6ac24883928b45a9340781867d4232ec2dbdf',
|
39969 | '9414685e97b1b5954bd46f730174136d57f1ceeb487443dc5321857ba73abee'
|
39970 | ],
|
39971 | [
|
39972 | 'f219ea5d6b54701c1c14de5b557eb42a8d13f3abbcd08affcc2a5e6b049b8d63',
|
39973 | '4cb95957e83d40b0f73af4544cccf6b1f4b08d3c07b27fb8d8c2962a400766d1'
|
39974 | ],
|
39975 | [
|
39976 | 'd7b8740f74a8fbaab1f683db8f45de26543a5490bca627087236912469a0b448',
|
39977 | 'fa77968128d9c92ee1010f337ad4717eff15db5ed3c049b3411e0315eaa4593b'
|
39978 | ],
|
39979 | [
|
39980 | '32d31c222f8f6f0ef86f7c98d3a3335ead5bcd32abdd94289fe4d3091aa824bf',
|
39981 | '5f3032f5892156e39ccd3d7915b9e1da2e6dac9e6f26e961118d14b8462e1661'
|
39982 | ],
|
39983 | [
|
39984 | '7461f371914ab32671045a155d9831ea8793d77cd59592c4340f86cbc18347b5',
|
39985 | '8ec0ba238b96bec0cbdddcae0aa442542eee1ff50c986ea6b39847b3cc092ff6'
|
39986 | ],
|
39987 | [
|
39988 | 'ee079adb1df1860074356a25aa38206a6d716b2c3e67453d287698bad7b2b2d6',
|
39989 | '8dc2412aafe3be5c4c5f37e0ecc5f9f6a446989af04c4e25ebaac479ec1c8c1e'
|
39990 | ],
|
39991 | [
|
39992 | '16ec93e447ec83f0467b18302ee620f7e65de331874c9dc72bfd8616ba9da6b5',
|
39993 | '5e4631150e62fb40d0e8c2a7ca5804a39d58186a50e497139626778e25b0674d'
|
39994 | ],
|
39995 | [
|
39996 | 'eaa5f980c245f6f038978290afa70b6bd8855897f98b6aa485b96065d537bd99',
|
39997 | 'f65f5d3e292c2e0819a528391c994624d784869d7e6ea67fb18041024edc07dc'
|
39998 | ],
|
39999 | [
|
40000 | '78c9407544ac132692ee1910a02439958ae04877151342ea96c4b6b35a49f51',
|
40001 | 'f3e0319169eb9b85d5404795539a5e68fa1fbd583c064d2462b675f194a3ddb4'
|
40002 | ],
|
40003 | [
|
40004 | '494f4be219a1a77016dcd838431aea0001cdc8ae7a6fc688726578d9702857a5',
|
40005 | '42242a969283a5f339ba7f075e36ba2af925ce30d767ed6e55f4b031880d562c'
|
40006 | ],
|
40007 | [
|
40008 | 'a598a8030da6d86c6bc7f2f5144ea549d28211ea58faa70ebf4c1e665c1fe9b5',
|
40009 | '204b5d6f84822c307e4b4a7140737aec23fc63b65b35f86a10026dbd2d864e6b'
|
40010 | ],
|
40011 | [
|
40012 | 'c41916365abb2b5d09192f5f2dbeafec208f020f12570a184dbadc3e58595997',
|
40013 | '4f14351d0087efa49d245b328984989d5caf9450f34bfc0ed16e96b58fa9913'
|
40014 | ],
|
40015 | [
|
40016 | '841d6063a586fa475a724604da03bc5b92a2e0d2e0a36acfe4c73a5514742881',
|
40017 | '73867f59c0659e81904f9a1c7543698e62562d6744c169ce7a36de01a8d6154'
|
40018 | ],
|
40019 | [
|
40020 | '5e95bb399a6971d376026947f89bde2f282b33810928be4ded112ac4d70e20d5',
|
40021 | '39f23f366809085beebfc71181313775a99c9aed7d8ba38b161384c746012865'
|
40022 | ],
|
40023 | [
|
40024 | '36e4641a53948fd476c39f8a99fd974e5ec07564b5315d8bf99471bca0ef2f66',
|
40025 | 'd2424b1b1abe4eb8164227b085c9aa9456ea13493fd563e06fd51cf5694c78fc'
|
40026 | ],
|
40027 | [
|
40028 | '336581ea7bfbbb290c191a2f507a41cf5643842170e914faeab27c2c579f726',
|
40029 | 'ead12168595fe1be99252129b6e56b3391f7ab1410cd1e0ef3dcdcabd2fda224'
|
40030 | ],
|
40031 | [
|
40032 | '8ab89816dadfd6b6a1f2634fcf00ec8403781025ed6890c4849742706bd43ede',
|
40033 | '6fdcef09f2f6d0a044e654aef624136f503d459c3e89845858a47a9129cdd24e'
|
40034 | ],
|
40035 | [
|
40036 | '1e33f1a746c9c5778133344d9299fcaa20b0938e8acff2544bb40284b8c5fb94',
|
40037 | '60660257dd11b3aa9c8ed618d24edff2306d320f1d03010e33a7d2057f3b3b6'
|
40038 | ],
|
40039 | [
|
40040 | '85b7c1dcb3cec1b7ee7f30ded79dd20a0ed1f4cc18cbcfcfa410361fd8f08f31',
|
40041 | '3d98a9cdd026dd43f39048f25a8847f4fcafad1895d7a633c6fed3c35e999511'
|
40042 | ],
|
40043 | [
|
40044 | '29df9fbd8d9e46509275f4b125d6d45d7fbe9a3b878a7af872a2800661ac5f51',
|
40045 | 'b4c4fe99c775a606e2d8862179139ffda61dc861c019e55cd2876eb2a27d84b'
|
40046 | ],
|
40047 | [
|
40048 | 'a0b1cae06b0a847a3fea6e671aaf8adfdfe58ca2f768105c8082b2e449fce252',
|
40049 | 'ae434102edde0958ec4b19d917a6a28e6b72da1834aff0e650f049503a296cf2'
|
40050 | ],
|
40051 | [
|
40052 | '4e8ceafb9b3e9a136dc7ff67e840295b499dfb3b2133e4ba113f2e4c0e121e5',
|
40053 | 'cf2174118c8b6d7a4b48f6d534ce5c79422c086a63460502b827ce62a326683c'
|
40054 | ],
|
40055 | [
|
40056 | 'd24a44e047e19b6f5afb81c7ca2f69080a5076689a010919f42725c2b789a33b',
|
40057 | '6fb8d5591b466f8fc63db50f1c0f1c69013f996887b8244d2cdec417afea8fa3'
|
40058 | ],
|
40059 | [
|
40060 | 'ea01606a7a6c9cdd249fdfcfacb99584001edd28abbab77b5104e98e8e3b35d4',
|
40061 | '322af4908c7312b0cfbfe369f7a7b3cdb7d4494bc2823700cfd652188a3ea98d'
|
40062 | ],
|
40063 | [
|
40064 | 'af8addbf2b661c8a6c6328655eb96651252007d8c5ea31be4ad196de8ce2131f',
|
40065 | '6749e67c029b85f52a034eafd096836b2520818680e26ac8f3dfbcdb71749700'
|
40066 | ],
|
40067 | [
|
40068 | 'e3ae1974566ca06cc516d47e0fb165a674a3dabcfca15e722f0e3450f45889',
|
40069 | '2aeabe7e4531510116217f07bf4d07300de97e4874f81f533420a72eeb0bd6a4'
|
40070 | ],
|
40071 | [
|
40072 | '591ee355313d99721cf6993ffed1e3e301993ff3ed258802075ea8ced397e246',
|
40073 | 'b0ea558a113c30bea60fc4775460c7901ff0b053d25ca2bdeee98f1a4be5d196'
|
40074 | ],
|
40075 | [
|
40076 | '11396d55fda54c49f19aa97318d8da61fa8584e47b084945077cf03255b52984',
|
40077 | '998c74a8cd45ac01289d5833a7beb4744ff536b01b257be4c5767bea93ea57a4'
|
40078 | ],
|
40079 | [
|
40080 | '3c5d2a1ba39c5a1790000738c9e0c40b8dcdfd5468754b6405540157e017aa7a',
|
40081 | 'b2284279995a34e2f9d4de7396fc18b80f9b8b9fdd270f6661f79ca4c81bd257'
|
40082 | ],
|
40083 | [
|
40084 | 'cc8704b8a60a0defa3a99a7299f2e9c3fbc395afb04ac078425ef8a1793cc030',
|
40085 | 'bdd46039feed17881d1e0862db347f8cf395b74fc4bcdc4e940b74e3ac1f1b13'
|
40086 | ],
|
40087 | [
|
40088 | 'c533e4f7ea8555aacd9777ac5cad29b97dd4defccc53ee7ea204119b2889b197',
|
40089 | '6f0a256bc5efdf429a2fb6242f1a43a2d9b925bb4a4b3a26bb8e0f45eb596096'
|
40090 | ],
|
40091 | [
|
40092 | 'c14f8f2ccb27d6f109f6d08d03cc96a69ba8c34eec07bbcf566d48e33da6593',
|
40093 | 'c359d6923bb398f7fd4473e16fe1c28475b740dd098075e6c0e8649113dc3a38'
|
40094 | ],
|
40095 | [
|
40096 | 'a6cbc3046bc6a450bac24789fa17115a4c9739ed75f8f21ce441f72e0b90e6ef',
|
40097 | '21ae7f4680e889bb130619e2c0f95a360ceb573c70603139862afd617fa9b9f'
|
40098 | ],
|
40099 | [
|
40100 | '347d6d9a02c48927ebfb86c1359b1caf130a3c0267d11ce6344b39f99d43cc38',
|
40101 | '60ea7f61a353524d1c987f6ecec92f086d565ab687870cb12689ff1e31c74448'
|
40102 | ],
|
40103 | [
|
40104 | 'da6545d2181db8d983f7dcb375ef5866d47c67b1bf31c8cf855ef7437b72656a',
|
40105 | '49b96715ab6878a79e78f07ce5680c5d6673051b4935bd897fea824b77dc208a'
|
40106 | ],
|
40107 | [
|
40108 | 'c40747cc9d012cb1a13b8148309c6de7ec25d6945d657146b9d5994b8feb1111',
|
40109 | '5ca560753be2a12fc6de6caf2cb489565db936156b9514e1bb5e83037e0fa2d4'
|
40110 | ],
|
40111 | [
|
40112 | '4e42c8ec82c99798ccf3a610be870e78338c7f713348bd34c8203ef4037f3502',
|
40113 | '7571d74ee5e0fb92a7a8b33a07783341a5492144cc54bcc40a94473693606437'
|
40114 | ],
|
40115 | [
|
40116 | '3775ab7089bc6af823aba2e1af70b236d251cadb0c86743287522a1b3b0dedea',
|
40117 | 'be52d107bcfa09d8bcb9736a828cfa7fac8db17bf7a76a2c42ad961409018cf7'
|
40118 | ],
|
40119 | [
|
40120 | 'cee31cbf7e34ec379d94fb814d3d775ad954595d1314ba8846959e3e82f74e26',
|
40121 | '8fd64a14c06b589c26b947ae2bcf6bfa0149ef0be14ed4d80f448a01c43b1c6d'
|
40122 | ],
|
40123 | [
|
40124 | 'b4f9eaea09b6917619f6ea6a4eb5464efddb58fd45b1ebefcdc1a01d08b47986',
|
40125 | '39e5c9925b5a54b07433a4f18c61726f8bb131c012ca542eb24a8ac07200682a'
|
40126 | ],
|
40127 | [
|
40128 | 'd4263dfc3d2df923a0179a48966d30ce84e2515afc3dccc1b77907792ebcc60e',
|
40129 | '62dfaf07a0f78feb30e30d6295853ce189e127760ad6cf7fae164e122a208d54'
|
40130 | ],
|
40131 | [
|
40132 | '48457524820fa65a4f8d35eb6930857c0032acc0a4a2de422233eeda897612c4',
|
40133 | '25a748ab367979d98733c38a1fa1c2e7dc6cc07db2d60a9ae7a76aaa49bd0f77'
|
40134 | ],
|
40135 | [
|
40136 | 'dfeeef1881101f2cb11644f3a2afdfc2045e19919152923f367a1767c11cceda',
|
40137 | 'ecfb7056cf1de042f9420bab396793c0c390bde74b4bbdff16a83ae09a9a7517'
|
40138 | ],
|
40139 | [
|
40140 | '6d7ef6b17543f8373c573f44e1f389835d89bcbc6062ced36c82df83b8fae859',
|
40141 | 'cd450ec335438986dfefa10c57fea9bcc521a0959b2d80bbf74b190dca712d10'
|
40142 | ],
|
40143 | [
|
40144 | 'e75605d59102a5a2684500d3b991f2e3f3c88b93225547035af25af66e04541f',
|
40145 | 'f5c54754a8f71ee540b9b48728473e314f729ac5308b06938360990e2bfad125'
|
40146 | ],
|
40147 | [
|
40148 | 'eb98660f4c4dfaa06a2be453d5020bc99a0c2e60abe388457dd43fefb1ed620c',
|
40149 | '6cb9a8876d9cb8520609af3add26cd20a0a7cd8a9411131ce85f44100099223e'
|
40150 | ],
|
40151 | [
|
40152 | '13e87b027d8514d35939f2e6892b19922154596941888336dc3563e3b8dba942',
|
40153 | 'fef5a3c68059a6dec5d624114bf1e91aac2b9da568d6abeb2570d55646b8adf1'
|
40154 | ],
|
40155 | [
|
40156 | 'ee163026e9fd6fe017c38f06a5be6fc125424b371ce2708e7bf4491691e5764a',
|
40157 | '1acb250f255dd61c43d94ccc670d0f58f49ae3fa15b96623e5430da0ad6c62b2'
|
40158 | ],
|
40159 | [
|
40160 | 'b268f5ef9ad51e4d78de3a750c2dc89b1e626d43505867999932e5db33af3d80',
|
40161 | '5f310d4b3c99b9ebb19f77d41c1dee018cf0d34fd4191614003e945a1216e423'
|
40162 | ],
|
40163 | [
|
40164 | 'ff07f3118a9df035e9fad85eb6c7bfe42b02f01ca99ceea3bf7ffdba93c4750d',
|
40165 | '438136d603e858a3a5c440c38eccbaddc1d2942114e2eddd4740d098ced1f0d8'
|
40166 | ],
|
40167 | [
|
40168 | '8d8b9855c7c052a34146fd20ffb658bea4b9f69e0d825ebec16e8c3ce2b526a1',
|
40169 | 'cdb559eedc2d79f926baf44fb84ea4d44bcf50fee51d7ceb30e2e7f463036758'
|
40170 | ],
|
40171 | [
|
40172 | '52db0b5384dfbf05bfa9d472d7ae26dfe4b851ceca91b1eba54263180da32b63',
|
40173 | 'c3b997d050ee5d423ebaf66a6db9f57b3180c902875679de924b69d84a7b375'
|
40174 | ],
|
40175 | [
|
40176 | 'e62f9490d3d51da6395efd24e80919cc7d0f29c3f3fa48c6fff543becbd43352',
|
40177 | '6d89ad7ba4876b0b22c2ca280c682862f342c8591f1daf5170e07bfd9ccafa7d'
|
40178 | ],
|
40179 | [
|
40180 | '7f30ea2476b399b4957509c88f77d0191afa2ff5cb7b14fd6d8e7d65aaab1193',
|
40181 | 'ca5ef7d4b231c94c3b15389a5f6311e9daff7bb67b103e9880ef4bff637acaec'
|
40182 | ],
|
40183 | [
|
40184 | '5098ff1e1d9f14fb46a210fada6c903fef0fb7b4a1dd1d9ac60a0361800b7a00',
|
40185 | '9731141d81fc8f8084d37c6e7542006b3ee1b40d60dfe5362a5b132fd17ddc0'
|
40186 | ],
|
40187 | [
|
40188 | '32b78c7de9ee512a72895be6b9cbefa6e2f3c4ccce445c96b9f2c81e2778ad58',
|
40189 | 'ee1849f513df71e32efc3896ee28260c73bb80547ae2275ba497237794c8753c'
|
40190 | ],
|
40191 | [
|
40192 | 'e2cb74fddc8e9fbcd076eef2a7c72b0ce37d50f08269dfc074b581550547a4f7',
|
40193 | 'd3aa2ed71c9dd2247a62df062736eb0baddea9e36122d2be8641abcb005cc4a4'
|
40194 | ],
|
40195 | [
|
40196 | '8438447566d4d7bedadc299496ab357426009a35f235cb141be0d99cd10ae3a8',
|
40197 | 'c4e1020916980a4da5d01ac5e6ad330734ef0d7906631c4f2390426b2edd791f'
|
40198 | ],
|
40199 | [
|
40200 | '4162d488b89402039b584c6fc6c308870587d9c46f660b878ab65c82c711d67e',
|
40201 | '67163e903236289f776f22c25fb8a3afc1732f2b84b4e95dbda47ae5a0852649'
|
40202 | ],
|
40203 | [
|
40204 | '3fad3fa84caf0f34f0f89bfd2dcf54fc175d767aec3e50684f3ba4a4bf5f683d',
|
40205 | 'cd1bc7cb6cc407bb2f0ca647c718a730cf71872e7d0d2a53fa20efcdfe61826'
|
40206 | ],
|
40207 | [
|
40208 | '674f2600a3007a00568c1a7ce05d0816c1fb84bf1370798f1c69532faeb1a86b',
|
40209 | '299d21f9413f33b3edf43b257004580b70db57da0b182259e09eecc69e0d38a5'
|
40210 | ],
|
40211 | [
|
40212 | 'd32f4da54ade74abb81b815ad1fb3b263d82d6c692714bcff87d29bd5ee9f08f',
|
40213 | 'f9429e738b8e53b968e99016c059707782e14f4535359d582fc416910b3eea87'
|
40214 | ],
|
40215 | [
|
40216 | '30e4e670435385556e593657135845d36fbb6931f72b08cb1ed954f1e3ce3ff6',
|
40217 | '462f9bce619898638499350113bbc9b10a878d35da70740dc695a559eb88db7b'
|
40218 | ],
|
40219 | [
|
40220 | 'be2062003c51cc3004682904330e4dee7f3dcd10b01e580bf1971b04d4cad297',
|
40221 | '62188bc49d61e5428573d48a74e1c655b1c61090905682a0d5558ed72dccb9bc'
|
40222 | ],
|
40223 | [
|
40224 | '93144423ace3451ed29e0fb9ac2af211cb6e84a601df5993c419859fff5df04a',
|
40225 | '7c10dfb164c3425f5c71a3f9d7992038f1065224f72bb9d1d902a6d13037b47c'
|
40226 | ],
|
40227 | [
|
40228 | 'b015f8044f5fcbdcf21ca26d6c34fb8197829205c7b7d2a7cb66418c157b112c',
|
40229 | 'ab8c1e086d04e813744a655b2df8d5f83b3cdc6faa3088c1d3aea1454e3a1d5f'
|
40230 | ],
|
40231 | [
|
40232 | 'd5e9e1da649d97d89e4868117a465a3a4f8a18de57a140d36b3f2af341a21b52',
|
40233 | '4cb04437f391ed73111a13cc1d4dd0db1693465c2240480d8955e8592f27447a'
|
40234 | ],
|
40235 | [
|
40236 | 'd3ae41047dd7ca065dbf8ed77b992439983005cd72e16d6f996a5316d36966bb',
|
40237 | 'bd1aeb21ad22ebb22a10f0303417c6d964f8cdd7df0aca614b10dc14d125ac46'
|
40238 | ],
|
40239 | [
|
40240 | '463e2763d885f958fc66cdd22800f0a487197d0a82e377b49f80af87c897b065',
|
40241 | 'bfefacdb0e5d0fd7df3a311a94de062b26b80c61fbc97508b79992671ef7ca7f'
|
40242 | ],
|
40243 | [
|
40244 | '7985fdfd127c0567c6f53ec1bb63ec3158e597c40bfe747c83cddfc910641917',
|
40245 | '603c12daf3d9862ef2b25fe1de289aed24ed291e0ec6708703a5bd567f32ed03'
|
40246 | ],
|
40247 | [
|
40248 | '74a1ad6b5f76e39db2dd249410eac7f99e74c59cb83d2d0ed5ff1543da7703e9',
|
40249 | 'cc6157ef18c9c63cd6193d83631bbea0093e0968942e8c33d5737fd790e0db08'
|
40250 | ],
|
40251 | [
|
40252 | '30682a50703375f602d416664ba19b7fc9bab42c72747463a71d0896b22f6da3',
|
40253 | '553e04f6b018b4fa6c8f39e7f311d3176290d0e0f19ca73f17714d9977a22ff8'
|
40254 | ],
|
40255 | [
|
40256 | '9e2158f0d7c0d5f26c3791efefa79597654e7a2b2464f52b1ee6c1347769ef57',
|
40257 | '712fcdd1b9053f09003a3481fa7762e9ffd7c8ef35a38509e2fbf2629008373'
|
40258 | ],
|
40259 | [
|
40260 | '176e26989a43c9cfeba4029c202538c28172e566e3c4fce7322857f3be327d66',
|
40261 | 'ed8cc9d04b29eb877d270b4878dc43c19aefd31f4eee09ee7b47834c1fa4b1c3'
|
40262 | ],
|
40263 | [
|
40264 | '75d46efea3771e6e68abb89a13ad747ecf1892393dfc4f1b7004788c50374da8',
|
40265 | '9852390a99507679fd0b86fd2b39a868d7efc22151346e1a3ca4726586a6bed8'
|
40266 | ],
|
40267 | [
|
40268 | '809a20c67d64900ffb698c4c825f6d5f2310fb0451c869345b7319f645605721',
|
40269 | '9e994980d9917e22b76b061927fa04143d096ccc54963e6a5ebfa5f3f8e286c1'
|
40270 | ],
|
40271 | [
|
40272 | '1b38903a43f7f114ed4500b4eac7083fdefece1cf29c63528d563446f972c180',
|
40273 | '4036edc931a60ae889353f77fd53de4a2708b26b6f5da72ad3394119daf408f9'
|
40274 | ]
|
40275 | ]
|
40276 | }
|
40277 | };
|
40278 |
|
40279 | var curves_1 = createCommonjsModule(function (module, exports) {
|
40280 |
|
40281 | var curves = exports;
|
40282 |
|
40283 |
|
40284 |
|
40285 |
|
40286 |
|
40287 | var assert = utils_1$1.assert;
|
40288 |
|
40289 | function PresetCurve(options) {
|
40290 | if (options.type === 'short')
|
40291 | this.curve = new curve_1.short(options);
|
40292 | else if (options.type === 'edwards')
|
40293 | this.curve = new curve_1.edwards(options);
|
40294 | else if (options.type === 'mont')
|
40295 | this.curve = new curve_1.mont(options);
|
40296 | else throw new Error('Unknown curve type.');
|
40297 | this.g = this.curve.g;
|
40298 | this.n = this.curve.n;
|
40299 | this.hash = options.hash;
|
40300 |
|
40301 | assert(this.g.validate(), 'Invalid curve');
|
40302 | assert(this.g.mul(this.n).isInfinity(), 'Invalid curve, n*G != O');
|
40303 | }
|
40304 | curves.PresetCurve = PresetCurve;
|
40305 |
|
40306 | function defineCurve(name, options) {
|
40307 | Object.defineProperty(curves, name, {
|
40308 | configurable: true,
|
40309 | enumerable: true,
|
40310 | get: function() {
|
40311 | var curve = new PresetCurve(options);
|
40312 | Object.defineProperty(curves, name, {
|
40313 | configurable: true,
|
40314 | enumerable: true,
|
40315 | value: curve
|
40316 | });
|
40317 | return curve;
|
40318 | }
|
40319 | });
|
40320 | }
|
40321 |
|
40322 | defineCurve('p192', {
|
40323 | type: 'short',
|
40324 | prime: 'p192',
|
40325 | p: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff',
|
40326 | a: 'ffffffff ffffffff ffffffff fffffffe ffffffff fffffffc',
|
40327 | b: '64210519 e59c80e7 0fa7e9ab 72243049 feb8deec c146b9b1',
|
40328 | n: 'ffffffff ffffffff ffffffff 99def836 146bc9b1 b4d22831',
|
40329 | hash: hash_1.sha256,
|
40330 | gRed: false,
|
40331 | g: [
|
40332 | '188da80e b03090f6 7cbf20eb 43a18800 f4ff0afd 82ff1012',
|
40333 | '07192b95 ffc8da78 631011ed 6b24cdd5 73f977a1 1e794811'
|
40334 | ]
|
40335 | });
|
40336 |
|
40337 | defineCurve('p224', {
|
40338 | type: 'short',
|
40339 | prime: 'p224',
|
40340 | p: 'ffffffff ffffffff ffffffff ffffffff 00000000 00000000 00000001',
|
40341 | a: 'ffffffff ffffffff ffffffff fffffffe ffffffff ffffffff fffffffe',
|
40342 | b: 'b4050a85 0c04b3ab f5413256 5044b0b7 d7bfd8ba 270b3943 2355ffb4',
|
40343 | n: 'ffffffff ffffffff ffffffff ffff16a2 e0b8f03e 13dd2945 5c5c2a3d',
|
40344 | hash: hash_1.sha256,
|
40345 | gRed: false,
|
40346 | g: [
|
40347 | 'b70e0cbd 6bb4bf7f 321390b9 4a03c1d3 56c21122 343280d6 115c1d21',
|
40348 | 'bd376388 b5f723fb 4c22dfe6 cd4375a0 5a074764 44d58199 85007e34'
|
40349 | ]
|
40350 | });
|
40351 |
|
40352 | defineCurve('p256', {
|
40353 | type: 'short',
|
40354 | prime: null,
|
40355 | p: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff',
|
40356 | a: 'ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff fffffffc',
|
40357 | b: '5ac635d8 aa3a93e7 b3ebbd55 769886bc 651d06b0 cc53b0f6 3bce3c3e 27d2604b',
|
40358 | n: 'ffffffff 00000000 ffffffff ffffffff bce6faad a7179e84 f3b9cac2 fc632551',
|
40359 | hash: hash_1.sha256,
|
40360 | gRed: false,
|
40361 | g: [
|
40362 | '6b17d1f2 e12c4247 f8bce6e5 63a440f2 77037d81 2deb33a0 f4a13945 d898c296',
|
40363 | '4fe342e2 fe1a7f9b 8ee7eb4a 7c0f9e16 2bce3357 6b315ece cbb64068 37bf51f5'
|
40364 | ]
|
40365 | });
|
40366 |
|
40367 | defineCurve('p384', {
|
40368 | type: 'short',
|
40369 | prime: null,
|
40370 | p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
40371 | 'fffffffe ffffffff 00000000 00000000 ffffffff',
|
40372 | a: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
40373 | 'fffffffe ffffffff 00000000 00000000 fffffffc',
|
40374 | b: 'b3312fa7 e23ee7e4 988e056b e3f82d19 181d9c6e fe814112 0314088f ' +
|
40375 | '5013875a c656398d 8a2ed19d 2a85c8ed d3ec2aef',
|
40376 | n: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff c7634d81 ' +
|
40377 | 'f4372ddf 581a0db2 48b0a77a ecec196a ccc52973',
|
40378 | hash: hash_1.sha384,
|
40379 | gRed: false,
|
40380 | g: [
|
40381 | 'aa87ca22 be8b0537 8eb1c71e f320ad74 6e1d3b62 8ba79b98 59f741e0 82542a38 ' +
|
40382 | '5502f25d bf55296c 3a545e38 72760ab7',
|
40383 | '3617de4a 96262c6f 5d9e98bf 9292dc29 f8f41dbd 289a147c e9da3113 b5f0b8c0 ' +
|
40384 | '0a60b1ce 1d7e819d 7a431d7c 90ea0e5f'
|
40385 | ]
|
40386 | });
|
40387 |
|
40388 | defineCurve('p521', {
|
40389 | type: 'short',
|
40390 | prime: null,
|
40391 | p: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
40392 | 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
40393 | 'ffffffff ffffffff ffffffff ffffffff ffffffff',
|
40394 | a: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
40395 | 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
40396 | 'ffffffff ffffffff ffffffff ffffffff fffffffc',
|
40397 | b: '00000051 953eb961 8e1c9a1f 929a21a0 b68540ee a2da725b ' +
|
40398 | '99b315f3 b8b48991 8ef109e1 56193951 ec7e937b 1652c0bd ' +
|
40399 | '3bb1bf07 3573df88 3d2c34f1 ef451fd4 6b503f00',
|
40400 | n: '000001ff ffffffff ffffffff ffffffff ffffffff ffffffff ' +
|
40401 | 'ffffffff ffffffff fffffffa 51868783 bf2f966b 7fcc0148 ' +
|
40402 | 'f709a5d0 3bb5c9b8 899c47ae bb6fb71e 91386409',
|
40403 | hash: hash_1.sha512,
|
40404 | gRed: false,
|
40405 | g: [
|
40406 | '000000c6 858e06b7 0404e9cd 9e3ecb66 2395b442 9c648139 ' +
|
40407 | '053fb521 f828af60 6b4d3dba a14b5e77 efe75928 fe1dc127 ' +
|
40408 | 'a2ffa8de 3348b3c1 856a429b f97e7e31 c2e5bd66',
|
40409 | '00000118 39296a78 9a3bc004 5c8a5fb4 2c7d1bd9 98f54449 ' +
|
40410 | '579b4468 17afbd17 273e662c 97ee7299 5ef42640 c550b901 ' +
|
40411 | '3fad0761 353c7086 a272c240 88be9476 9fd16650'
|
40412 | ]
|
40413 | });
|
40414 |
|
40415 |
|
40416 | defineCurve('curve25519', {
|
40417 | type: 'mont',
|
40418 | prime: 'p25519',
|
40419 | p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
|
40420 | a: '76d06',
|
40421 | b: '1',
|
40422 | n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
|
40423 | cofactor: '8',
|
40424 | hash: hash_1.sha256,
|
40425 | gRed: false,
|
40426 | g: [
|
40427 | '9'
|
40428 | ]
|
40429 | });
|
40430 |
|
40431 | defineCurve('ed25519', {
|
40432 | type: 'edwards',
|
40433 | prime: 'p25519',
|
40434 | p: '7fffffffffffffff ffffffffffffffff ffffffffffffffff ffffffffffffffed',
|
40435 | a: '-1',
|
40436 | c: '1',
|
40437 |
|
40438 | d: '52036cee2b6ffe73 8cc740797779e898 00700a4d4141d8ab 75eb4dca135978a3',
|
40439 | n: '1000000000000000 0000000000000000 14def9dea2f79cd6 5812631a5cf5d3ed',
|
40440 | cofactor: '8',
|
40441 | hash: hash_1.sha256,
|
40442 | gRed: false,
|
40443 | g: [
|
40444 | '216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a',
|
40445 |
|
40446 | '6666666666666666666666666666666666666666666666666666666666666658'
|
40447 | ]
|
40448 | });
|
40449 |
|
40450 |
|
40451 | defineCurve('brainpoolP256r1', {
|
40452 | type: 'short',
|
40453 | prime: null,
|
40454 | p: 'A9FB57DB A1EEA9BC 3E660A90 9D838D72 6E3BF623 D5262028 2013481D 1F6E5377',
|
40455 | a: '7D5A0975 FC2C3057 EEF67530 417AFFE7 FB8055C1 26DC5C6C E94A4B44 F330B5D9',
|
40456 | b: '26DC5C6C E94A4B44 F330B5D9 BBD77CBF 95841629 5CF7E1CE 6BCCDC18 FF8C07B6',
|
40457 | n: 'A9FB57DB A1EEA9BC 3E660A90 9D838D71 8C397AA3 B561A6F7 901E0E82 974856A7',
|
40458 | hash: hash_1.sha256,
|
40459 | gRed: false,
|
40460 | g: [
|
40461 | '8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262',
|
40462 | '547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997'
|
40463 | ]
|
40464 | });
|
40465 |
|
40466 |
|
40467 | defineCurve('brainpoolP384r1', {
|
40468 | type: 'short',
|
40469 | prime: null,
|
40470 | p: '8CB91E82 A3386D28 0F5D6F7E 50E641DF 152F7109 ED5456B4 12B1DA19 7FB71123' +
|
40471 | 'ACD3A729 901D1A71 87470013 3107EC53',
|
40472 | a: '7BC382C6 3D8C150C 3C72080A CE05AFA0 C2BEA28E 4FB22787 139165EF BA91F90F' +
|
40473 | '8AA5814A 503AD4EB 04A8C7DD 22CE2826',
|
40474 | b: '04A8C7DD 22CE2826 8B39B554 16F0447C 2FB77DE1 07DCD2A6 2E880EA5 3EEB62D5' +
|
40475 | '7CB43902 95DBC994 3AB78696 FA504C11',
|
40476 | n: '8CB91E82 A3386D28 0F5D6F7E 50E641DF 152F7109 ED5456B3 1F166E6C AC0425A7' +
|
40477 | 'CF3AB6AF 6B7FC310 3B883202 E9046565',
|
40478 | hash: hash_1.sha384,
|
40479 | gRed: false,
|
40480 | g: [
|
40481 | '1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10' +
|
40482 | 'E8E826E03436D646AAEF87B2E247D4AF1E',
|
40483 | '8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129' +
|
40484 | '280E4646217791811142820341263C5315'
|
40485 | ]
|
40486 | });
|
40487 |
|
40488 |
|
40489 | defineCurve('brainpoolP512r1', {
|
40490 | type: 'short',
|
40491 | prime: null,
|
40492 | p: 'AADD9DB8 DBE9C48B 3FD4E6AE 33C9FC07 CB308DB3 B3C9D20E D6639CCA 70330871' +
|
40493 | '7D4D9B00 9BC66842 AECDA12A E6A380E6 2881FF2F 2D82C685 28AA6056 583A48F3',
|
40494 | a: '7830A331 8B603B89 E2327145 AC234CC5 94CBDD8D 3DF91610 A83441CA EA9863BC' +
|
40495 | '2DED5D5A A8253AA1 0A2EF1C9 8B9AC8B5 7F1117A7 2BF2C7B9 E7C1AC4D 77FC94CA',
|
40496 | b: '3DF91610 A83441CA EA9863BC 2DED5D5A A8253AA1 0A2EF1C9 8B9AC8B5 7F1117A7' +
|
40497 | '2BF2C7B9 E7C1AC4D 77FC94CA DC083E67 984050B7 5EBAE5DD 2809BD63 8016F723',
|
40498 | n: 'AADD9DB8 DBE9C48B 3FD4E6AE 33C9FC07 CB308DB3 B3C9D20E D6639CCA 70330870' +
|
40499 | '553E5C41 4CA92619 41866119 7FAC1047 1DB1D381 085DDADD B5879682 9CA90069',
|
40500 | hash: hash_1.sha512,
|
40501 | gRed: false,
|
40502 | g: [
|
40503 | '81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D009' +
|
40504 | '8EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822',
|
40505 | '7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F81' +
|
40506 | '11B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892'
|
40507 | ]
|
40508 | });
|
40509 |
|
40510 |
|
40511 | var pre;
|
40512 | try {
|
40513 | pre = secp256k1;
|
40514 | } catch (e) {
|
40515 | pre = undefined;
|
40516 | }
|
40517 |
|
40518 | defineCurve('secp256k1', {
|
40519 | type: 'short',
|
40520 | prime: 'k256',
|
40521 | p: 'ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f',
|
40522 | a: '0',
|
40523 | b: '7',
|
40524 | n: 'ffffffff ffffffff ffffffff fffffffe baaedce6 af48a03b bfd25e8c d0364141',
|
40525 | h: '1',
|
40526 | hash: hash_1.sha256,
|
40527 |
|
40528 |
|
40529 | beta: '7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee',
|
40530 | lambda: '5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72',
|
40531 | basis: [
|
40532 | {
|
40533 | a: '3086d221a7d46bcde86c90e49284eb15',
|
40534 | b: '-e4437ed6010e88286f547fa90abfe4c3'
|
40535 | },
|
40536 | {
|
40537 | a: '114ca50f7a8e2f3f657c1108d9d44cfd8',
|
40538 | b: '3086d221a7d46bcde86c90e49284eb15'
|
40539 | }
|
40540 | ],
|
40541 |
|
40542 | gRed: false,
|
40543 | g: [
|
40544 | '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
|
40545 | '483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8',
|
40546 | pre
|
40547 | ]
|
40548 | });
|
40549 | });
|
40550 |
|
40551 | function HmacDRBG(options) {
|
40552 | if (!(this instanceof HmacDRBG))
|
40553 | return new HmacDRBG(options);
|
40554 | this.hash = options.hash;
|
40555 | this.predResist = !!options.predResist;
|
40556 |
|
40557 | this.outLen = this.hash.outSize;
|
40558 | this.minEntropy = options.minEntropy || this.hash.hmacStrength;
|
40559 |
|
40560 | this._reseed = null;
|
40561 | this.reseedInterval = null;
|
40562 | this.K = null;
|
40563 | this.V = null;
|
40564 |
|
40565 | var entropy = utils_1.toArray(options.entropy, options.entropyEnc || 'hex');
|
40566 | var nonce = utils_1.toArray(options.nonce, options.nonceEnc || 'hex');
|
40567 | var pers = utils_1.toArray(options.pers, options.persEnc || 'hex');
|
40568 | minimalisticAssert(entropy.length >= (this.minEntropy / 8),
|
40569 | 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits');
|
40570 | this._init(entropy, nonce, pers);
|
40571 | }
|
40572 | var hmacDrbg = HmacDRBG;
|
40573 |
|
40574 | HmacDRBG.prototype._init = function init(entropy, nonce, pers) {
|
40575 | var seed = entropy.concat(nonce).concat(pers);
|
40576 |
|
40577 | this.K = new Array(this.outLen / 8);
|
40578 | this.V = new Array(this.outLen / 8);
|
40579 | for (var i = 0; i < this.V.length; i++) {
|
40580 | this.K[i] = 0x00;
|
40581 | this.V[i] = 0x01;
|
40582 | }
|
40583 |
|
40584 | this._update(seed);
|
40585 | this._reseed = 1;
|
40586 | this.reseedInterval = 0x1000000000000;
|
40587 | };
|
40588 |
|
40589 | HmacDRBG.prototype._hmac = function hmac() {
|
40590 | return new hash_1.hmac(this.hash, this.K);
|
40591 | };
|
40592 |
|
40593 | HmacDRBG.prototype._update = function update(seed) {
|
40594 | var kmac = this._hmac()
|
40595 | .update(this.V)
|
40596 | .update([ 0x00 ]);
|
40597 | if (seed)
|
40598 | kmac = kmac.update(seed);
|
40599 | this.K = kmac.digest();
|
40600 | this.V = this._hmac().update(this.V).digest();
|
40601 | if (!seed)
|
40602 | return;
|
40603 |
|
40604 | this.K = this._hmac()
|
40605 | .update(this.V)
|
40606 | .update([ 0x01 ])
|
40607 | .update(seed)
|
40608 | .digest();
|
40609 | this.V = this._hmac().update(this.V).digest();
|
40610 | };
|
40611 |
|
40612 | HmacDRBG.prototype.reseed = function reseed(entropy, entropyEnc, add, addEnc) {
|
40613 |
|
40614 | if (typeof entropyEnc !== 'string') {
|
40615 | addEnc = add;
|
40616 | add = entropyEnc;
|
40617 | entropyEnc = null;
|
40618 | }
|
40619 |
|
40620 | entropy = utils_1.toArray(entropy, entropyEnc);
|
40621 | add = utils_1.toArray(add, addEnc);
|
40622 |
|
40623 | minimalisticAssert(entropy.length >= (this.minEntropy / 8),
|
40624 | 'Not enough entropy. Minimum is: ' + this.minEntropy + ' bits');
|
40625 |
|
40626 | this._update(entropy.concat(add || []));
|
40627 | this._reseed = 1;
|
40628 | };
|
40629 |
|
40630 | HmacDRBG.prototype.generate = function generate(len, enc, add, addEnc) {
|
40631 | if (this._reseed > this.reseedInterval)
|
40632 | throw new Error('Reseed is required');
|
40633 |
|
40634 |
|
40635 | if (typeof enc !== 'string') {
|
40636 | addEnc = add;
|
40637 | add = enc;
|
40638 | enc = null;
|
40639 | }
|
40640 |
|
40641 |
|
40642 | if (add) {
|
40643 | add = utils_1.toArray(add, addEnc || 'hex');
|
40644 | this._update(add);
|
40645 | }
|
40646 |
|
40647 | var temp = [];
|
40648 | while (temp.length < len) {
|
40649 | this.V = this._hmac().update(this.V).digest();
|
40650 | temp = temp.concat(this.V);
|
40651 | }
|
40652 |
|
40653 | var res = temp.slice(0, len);
|
40654 | this._update(add);
|
40655 | this._reseed++;
|
40656 | return utils_1.encode(res, enc);
|
40657 | };
|
40658 |
|
40659 | var assert$5 = utils_1$1.assert;
|
40660 |
|
40661 | function KeyPair(ec, options) {
|
40662 | this.ec = ec;
|
40663 | this.priv = null;
|
40664 | this.pub = null;
|
40665 |
|
40666 |
|
40667 | if (options.priv)
|
40668 | this._importPrivate(options.priv, options.privEnc);
|
40669 | if (options.pub)
|
40670 | this._importPublic(options.pub, options.pubEnc);
|
40671 | }
|
40672 | var key = KeyPair;
|
40673 |
|
40674 | KeyPair.fromPublic = function fromPublic(ec, pub, enc) {
|
40675 | if (pub instanceof KeyPair)
|
40676 | return pub;
|
40677 |
|
40678 | return new KeyPair(ec, {
|
40679 | pub: pub,
|
40680 | pubEnc: enc
|
40681 | });
|
40682 | };
|
40683 |
|
40684 | KeyPair.fromPrivate = function fromPrivate(ec, priv, enc) {
|
40685 | if (priv instanceof KeyPair)
|
40686 | return priv;
|
40687 |
|
40688 | return new KeyPair(ec, {
|
40689 | priv: priv,
|
40690 | privEnc: enc
|
40691 | });
|
40692 | };
|
40693 |
|
40694 |
|
40695 | KeyPair.prototype.validate = function validate() {
|
40696 | var pub = this.getPublic();
|
40697 |
|
40698 | if (pub.isInfinity())
|
40699 | return { result: false, reason: 'Invalid public key' };
|
40700 | if (!pub.validate())
|
40701 | return { result: false, reason: 'Public key is not a point' };
|
40702 | if (!pub.mul(this.ec.curve.n).isInfinity())
|
40703 | return { result: false, reason: 'Public key * N != O' };
|
40704 |
|
40705 | return { result: true, reason: null };
|
40706 | };
|
40707 |
|
40708 | KeyPair.prototype.getPublic = function getPublic(enc, compact) {
|
40709 | if (!this.pub)
|
40710 | this.pub = this.ec.g.mul(this.priv);
|
40711 |
|
40712 | if (!enc)
|
40713 | return this.pub;
|
40714 |
|
40715 | return this.pub.encode(enc, compact);
|
40716 | };
|
40717 |
|
40718 | KeyPair.prototype.getPrivate = function getPrivate(enc) {
|
40719 | if (enc === 'hex')
|
40720 | return this.priv.toString(16, 2);
|
40721 | else
|
40722 | return this.priv;
|
40723 | };
|
40724 |
|
40725 | KeyPair.prototype._importPrivate = function _importPrivate(key, enc) {
|
40726 | this.priv = new bn(key, enc || 16);
|
40727 |
|
40728 |
|
40729 |
|
40730 | if (this.ec.curve.type === 'mont') {
|
40731 | var one = this.ec.curve.one;
|
40732 | var mask = one.ushln(255 - 3).sub(one).ushln(3);
|
40733 | this.priv = this.priv.or(one.ushln(255 - 1));
|
40734 | this.priv = this.priv.and(mask);
|
40735 | } else
|
40736 |
|
40737 |
|
40738 | this.priv = this.priv.umod(this.ec.curve.n);
|
40739 | };
|
40740 |
|
40741 | KeyPair.prototype._importPublic = function _importPublic(key, enc) {
|
40742 | if (key.x || key.y) {
|
40743 |
|
40744 |
|
40745 |
|
40746 | if (this.ec.curve.type === 'mont') {
|
40747 | assert$5(key.x, 'Need x coordinate');
|
40748 | } else if (this.ec.curve.type === 'short' ||
|
40749 | this.ec.curve.type === 'edwards') {
|
40750 | assert$5(key.x && key.y, 'Need both x and y coordinate');
|
40751 | }
|
40752 | this.pub = this.ec.curve.point(key.x, key.y);
|
40753 | return;
|
40754 | }
|
40755 | this.pub = this.ec.curve.decodePoint(key, enc);
|
40756 | };
|
40757 |
|
40758 |
|
40759 | KeyPair.prototype.derive = function derive(pub) {
|
40760 | return pub.mul(this.priv).getX();
|
40761 | };
|
40762 |
|
40763 |
|
40764 | KeyPair.prototype.sign = function sign(msg, enc, options) {
|
40765 | return this.ec.sign(msg, this, enc, options);
|
40766 | };
|
40767 |
|
40768 | KeyPair.prototype.verify = function verify(msg, signature) {
|
40769 | return this.ec.verify(msg, signature, this);
|
40770 | };
|
40771 |
|
40772 | KeyPair.prototype.inspect = function inspect() {
|
40773 | return '<Key priv: ' + (this.priv && this.priv.toString(16, 2)) +
|
40774 | ' pub: ' + (this.pub && this.pub.inspect()) + ' >';
|
40775 | };
|
40776 |
|
40777 | var assert$6 = utils_1$1.assert;
|
40778 |
|
40779 | function Signature$1(options, enc) {
|
40780 | if (options instanceof Signature$1)
|
40781 | return options;
|
40782 |
|
40783 | if (this._importDER(options, enc))
|
40784 | return;
|
40785 |
|
40786 | assert$6(options.r && options.s, 'Signature without r or s');
|
40787 | this.r = new bn(options.r, 16);
|
40788 | this.s = new bn(options.s, 16);
|
40789 | if (options.recoveryParam === undefined)
|
40790 | this.recoveryParam = null;
|
40791 | else
|
40792 | this.recoveryParam = options.recoveryParam;
|
40793 | }
|
40794 | var signature$1 = Signature$1;
|
40795 |
|
40796 | function Position() {
|
40797 | this.place = 0;
|
40798 | }
|
40799 |
|
40800 | function getLength(buf, p) {
|
40801 | var initial = buf[p.place++];
|
40802 | if (!(initial & 0x80)) {
|
40803 | return initial;
|
40804 | }
|
40805 | var octetLen = initial & 0xf;
|
40806 | var val = 0;
|
40807 | for (var i = 0, off = p.place; i < octetLen; i++, off++) {
|
40808 | val <<= 8;
|
40809 | val |= buf[off];
|
40810 | }
|
40811 | p.place = off;
|
40812 | return val;
|
40813 | }
|
40814 |
|
40815 | function rmPadding(buf) {
|
40816 | var i = 0;
|
40817 | var len = buf.length - 1;
|
40818 | while (!buf[i] && !(buf[i + 1] & 0x80) && i < len) {
|
40819 | i++;
|
40820 | }
|
40821 | if (i === 0) {
|
40822 | return buf;
|
40823 | }
|
40824 | return buf.slice(i);
|
40825 | }
|
40826 |
|
40827 | Signature$1.prototype._importDER = function _importDER(data, enc) {
|
40828 | data = utils_1$1.toArray(data, enc);
|
40829 | var p = new Position();
|
40830 | if (data[p.place++] !== 0x30) {
|
40831 | return false;
|
40832 | }
|
40833 | var len = getLength(data, p);
|
40834 | if ((len + p.place) !== data.length) {
|
40835 | return false;
|
40836 | }
|
40837 | if (data[p.place++] !== 0x02) {
|
40838 | return false;
|
40839 | }
|
40840 | var rlen = getLength(data, p);
|
40841 | var r = data.slice(p.place, rlen + p.place);
|
40842 | p.place += rlen;
|
40843 | if (data[p.place++] !== 0x02) {
|
40844 | return false;
|
40845 | }
|
40846 | var slen = getLength(data, p);
|
40847 | if (data.length !== slen + p.place) {
|
40848 | return false;
|
40849 | }
|
40850 | var s = data.slice(p.place, slen + p.place);
|
40851 | if (r[0] === 0 && (r[1] & 0x80)) {
|
40852 | r = r.slice(1);
|
40853 | }
|
40854 | if (s[0] === 0 && (s[1] & 0x80)) {
|
40855 | s = s.slice(1);
|
40856 | }
|
40857 |
|
40858 | this.r = new bn(r);
|
40859 | this.s = new bn(s);
|
40860 | this.recoveryParam = null;
|
40861 |
|
40862 | return true;
|
40863 | };
|
40864 |
|
40865 | function constructLength(arr, len) {
|
40866 | if (len < 0x80) {
|
40867 | arr.push(len);
|
40868 | return;
|
40869 | }
|
40870 | var octets = 1 + (Math.log(len) / Math.LN2 >>> 3);
|
40871 | arr.push(octets | 0x80);
|
40872 | while (--octets) {
|
40873 | arr.push((len >>> (octets << 3)) & 0xff);
|
40874 | }
|
40875 | arr.push(len);
|
40876 | }
|
40877 |
|
40878 | Signature$1.prototype.toDER = function toDER(enc) {
|
40879 | var r = this.r.toArray();
|
40880 | var s = this.s.toArray();
|
40881 |
|
40882 |
|
40883 | if (r[0] & 0x80)
|
40884 | r = [ 0 ].concat(r);
|
40885 |
|
40886 | if (s[0] & 0x80)
|
40887 | s = [ 0 ].concat(s);
|
40888 |
|
40889 | r = rmPadding(r);
|
40890 | s = rmPadding(s);
|
40891 |
|
40892 | while (!s[0] && !(s[1] & 0x80)) {
|
40893 | s = s.slice(1);
|
40894 | }
|
40895 | var arr = [ 0x02 ];
|
40896 | constructLength(arr, r.length);
|
40897 | arr = arr.concat(r);
|
40898 | arr.push(0x02);
|
40899 | constructLength(arr, s.length);
|
40900 | var backHalf = arr.concat(s);
|
40901 | var res = [ 0x30 ];
|
40902 | constructLength(res, backHalf.length);
|
40903 | res = res.concat(backHalf);
|
40904 | return utils_1$1.encode(res, enc);
|
40905 | };
|
40906 |
|
40907 | var assert$7 = utils_1$1.assert;
|
40908 |
|
40909 |
|
40910 |
|
40911 |
|
40912 | function EC(options) {
|
40913 | if (!(this instanceof EC))
|
40914 | return new EC(options);
|
40915 |
|
40916 |
|
40917 | if (typeof options === 'string') {
|
40918 | assert$7(curves_1.hasOwnProperty(options), 'Unknown curve ' + options);
|
40919 |
|
40920 | options = curves_1[options];
|
40921 | }
|
40922 |
|
40923 |
|
40924 | if (options instanceof curves_1.PresetCurve)
|
40925 | options = { curve: options };
|
40926 |
|
40927 | this.curve = options.curve.curve;
|
40928 | this.n = this.curve.n;
|
40929 | this.nh = this.n.ushrn(1);
|
40930 | this.g = this.curve.g;
|
40931 |
|
40932 |
|
40933 | this.g = options.curve.g;
|
40934 | this.g.precompute(options.curve.n.bitLength() + 1);
|
40935 |
|
40936 |
|
40937 | this.hash = options.hash || options.curve.hash;
|
40938 | }
|
40939 | var ec = EC;
|
40940 |
|
40941 | EC.prototype.keyPair = function keyPair(options) {
|
40942 | return new key(this, options);
|
40943 | };
|
40944 |
|
40945 | EC.prototype.keyFromPrivate = function keyFromPrivate(priv, enc) {
|
40946 | return key.fromPrivate(this, priv, enc);
|
40947 | };
|
40948 |
|
40949 | EC.prototype.keyFromPublic = function keyFromPublic(pub, enc) {
|
40950 | return key.fromPublic(this, pub, enc);
|
40951 | };
|
40952 |
|
40953 | EC.prototype.genKeyPair = function genKeyPair(options) {
|
40954 | if (!options)
|
40955 | options = {};
|
40956 |
|
40957 |
|
40958 | var drbg = new hmacDrbg({
|
40959 | hash: this.hash,
|
40960 | pers: options.pers,
|
40961 | persEnc: options.persEnc || 'utf8',
|
40962 | entropy: options.entropy || brorand(this.hash.hmacStrength),
|
40963 | entropyEnc: options.entropy && options.entropyEnc || 'utf8',
|
40964 | nonce: this.n.toArray()
|
40965 | });
|
40966 |
|
40967 |
|
40968 | if (this.curve.type === 'mont') {
|
40969 | var priv = new bn(drbg.generate(32));
|
40970 | return this.keyFromPrivate(priv);
|
40971 | }
|
40972 |
|
40973 | var bytes = this.n.byteLength();
|
40974 | var ns2 = this.n.sub(new bn(2));
|
40975 | do {
|
40976 | var priv = new bn(drbg.generate(bytes));
|
40977 | if (priv.cmp(ns2) > 0)
|
40978 | continue;
|
40979 |
|
40980 | priv.iaddn(1);
|
40981 | return this.keyFromPrivate(priv);
|
40982 | } while (true);
|
40983 | };
|
40984 |
|
40985 | EC.prototype._truncateToN = function truncateToN(msg, truncOnly, bitSize) {
|
40986 | bitSize = bitSize || msg.byteLength() * 8;
|
40987 | var delta = bitSize - this.n.bitLength();
|
40988 | if (delta > 0)
|
40989 | msg = msg.ushrn(delta);
|
40990 | if (!truncOnly && msg.cmp(this.n) >= 0)
|
40991 | return msg.sub(this.n);
|
40992 | else
|
40993 | return msg;
|
40994 | };
|
40995 |
|
40996 | EC.prototype.truncateMsg = function truncateMSG(msg) {
|
40997 |
|
40998 | var bitSize;
|
40999 | if (msg instanceof Uint8Array) {
|
41000 | bitSize = msg.byteLength * 8;
|
41001 | msg = this._truncateToN(new bn(msg, 16), false, bitSize);
|
41002 | } else if (typeof msg === 'string') {
|
41003 | bitSize = msg.length * 4;
|
41004 | msg = this._truncateToN(new bn(msg, 16), false, bitSize);
|
41005 | } else {
|
41006 | msg = this._truncateToN(new bn(msg, 16));
|
41007 | }
|
41008 | return msg;
|
41009 | };
|
41010 |
|
41011 | EC.prototype.sign = function sign(msg, key, enc, options) {
|
41012 | if (typeof enc === 'object') {
|
41013 | options = enc;
|
41014 | enc = null;
|
41015 | }
|
41016 | if (!options)
|
41017 | options = {};
|
41018 |
|
41019 | key = this.keyFromPrivate(key, enc);
|
41020 | msg = this.truncateMsg(msg);
|
41021 |
|
41022 |
|
41023 | var bytes = this.n.byteLength();
|
41024 | var bkey = key.getPrivate().toArray('be', bytes);
|
41025 |
|
41026 |
|
41027 | var nonce = msg.toArray('be', bytes);
|
41028 |
|
41029 |
|
41030 | var drbg = new hmacDrbg({
|
41031 | hash: this.hash,
|
41032 | entropy: bkey,
|
41033 | nonce: nonce,
|
41034 | pers: options.pers,
|
41035 | persEnc: options.persEnc || 'utf8'
|
41036 | });
|
41037 |
|
41038 |
|
41039 | var ns1 = this.n.sub(new bn(1));
|
41040 |
|
41041 | for (var iter = 0; true; iter++) {
|
41042 | var k = options.k ?
|
41043 | options.k(iter) :
|
41044 | new bn(drbg.generate(this.n.byteLength()));
|
41045 | k = this._truncateToN(k, true);
|
41046 | if (k.cmpn(1) <= 0 || k.cmp(ns1) >= 0)
|
41047 | continue;
|
41048 |
|
41049 | var kp = this.g.mul(k);
|
41050 | if (kp.isInfinity())
|
41051 | continue;
|
41052 |
|
41053 | var kpX = kp.getX();
|
41054 | var r = kpX.umod(this.n);
|
41055 | if (r.cmpn(0) === 0)
|
41056 | continue;
|
41057 |
|
41058 | var s = k.invm(this.n).mul(r.mul(key.getPrivate()).iadd(msg));
|
41059 | s = s.umod(this.n);
|
41060 | if (s.cmpn(0) === 0)
|
41061 | continue;
|
41062 |
|
41063 | var recoveryParam = (kp.getY().isOdd() ? 1 : 0) |
|
41064 | (kpX.cmp(r) !== 0 ? 2 : 0);
|
41065 |
|
41066 |
|
41067 | if (options.canonical && s.cmp(this.nh) > 0) {
|
41068 | s = this.n.sub(s);
|
41069 | recoveryParam ^= 1;
|
41070 | }
|
41071 |
|
41072 | return new signature$1({ r: r, s: s, recoveryParam: recoveryParam });
|
41073 | }
|
41074 | };
|
41075 |
|
41076 | EC.prototype.verify = function verify(msg, signature, key, enc) {
|
41077 | key = this.keyFromPublic(key, enc);
|
41078 | signature = new signature$1(signature, 'hex');
|
41079 |
|
41080 | var ret = this._verify(this.truncateMsg(msg), signature, key) ||
|
41081 | this._verify(this._truncateToN(new bn(msg, 16)), signature, key);
|
41082 | return ret;
|
41083 | };
|
41084 |
|
41085 | EC.prototype._verify = function _verify(msg, signature, key) {
|
41086 |
|
41087 | var r = signature.r;
|
41088 | var s = signature.s;
|
41089 | if (r.cmpn(1) < 0 || r.cmp(this.n) >= 0)
|
41090 | return false;
|
41091 | if (s.cmpn(1) < 0 || s.cmp(this.n) >= 0)
|
41092 | return false;
|
41093 |
|
41094 |
|
41095 | var sinv = s.invm(this.n);
|
41096 | var u1 = sinv.mul(msg).umod(this.n);
|
41097 | var u2 = sinv.mul(r).umod(this.n);
|
41098 |
|
41099 | if (!this.curve._maxwellTrick) {
|
41100 | var p = this.g.mulAdd(u1, key.getPublic(), u2);
|
41101 | if (p.isInfinity())
|
41102 | return false;
|
41103 |
|
41104 | return p.getX().umod(this.n).cmp(r) === 0;
|
41105 | }
|
41106 |
|
41107 |
|
41108 |
|
41109 |
|
41110 | var p = this.g.jmulAdd(u1, key.getPublic(), u2);
|
41111 | if (p.isInfinity())
|
41112 | return false;
|
41113 |
|
41114 |
|
41115 |
|
41116 |
|
41117 | return p.eqXToP(r);
|
41118 | };
|
41119 |
|
41120 | EC.prototype.recoverPubKey = function(msg, signature, j, enc) {
|
41121 | assert$7((3 & j) === j, 'The recovery param is more than two bits');
|
41122 | signature = new signature$1(signature, enc);
|
41123 |
|
41124 | var n = this.n;
|
41125 | var e = new bn(msg);
|
41126 | var r = signature.r;
|
41127 | var s = signature.s;
|
41128 |
|
41129 |
|
41130 | var isYOdd = j & 1;
|
41131 | var isSecondKey = j >> 1;
|
41132 | if (r.cmp(this.curve.p.umod(this.curve.n)) >= 0 && isSecondKey)
|
41133 | throw new Error('Unable to find sencond key candinate');
|
41134 |
|
41135 |
|
41136 | if (isSecondKey)
|
41137 | r = this.curve.pointFromX(r.add(this.curve.n), isYOdd);
|
41138 | else
|
41139 | r = this.curve.pointFromX(r, isYOdd);
|
41140 |
|
41141 | var rInv = signature.r.invm(n);
|
41142 | var s1 = n.sub(e).mul(rInv).umod(n);
|
41143 | var s2 = s.mul(rInv).umod(n);
|
41144 |
|
41145 |
|
41146 |
|
41147 | return this.g.mulAdd(s1, r, s2);
|
41148 | };
|
41149 |
|
41150 | EC.prototype.getKeyRecoveryParam = function(e, signature, Q, enc) {
|
41151 | signature = new signature$1(signature, enc);
|
41152 | if (signature.recoveryParam !== null)
|
41153 | return signature.recoveryParam;
|
41154 |
|
41155 | for (var i = 0; i < 4; i++) {
|
41156 | var Qprime;
|
41157 | try {
|
41158 | Qprime = this.recoverPubKey(e, signature, i);
|
41159 | } catch (e) {
|
41160 | continue;
|
41161 | }
|
41162 |
|
41163 | if (Qprime.eq(Q))
|
41164 | return i;
|
41165 | }
|
41166 | throw new Error('Unable to find valid recovery factor');
|
41167 | };
|
41168 |
|
41169 | var assert$8 = utils_1$1.assert;
|
41170 | var parseBytes = utils_1$1.parseBytes;
|
41171 | var cachedProperty = utils_1$1.cachedProperty;
|
41172 |
|
41173 |
|
41174 |
|
41175 |
|
41176 |
|
41177 |
|
41178 |
|
41179 |
|
41180 |
|
41181 |
|
41182 | function KeyPair$1(eddsa, params) {
|
41183 | this.eddsa = eddsa;
|
41184 | if (params.hasOwnProperty('secret'))
|
41185 | this._secret = parseBytes(params.secret);
|
41186 | if (eddsa.isPoint(params.pub))
|
41187 | this._pub = params.pub;
|
41188 | else {
|
41189 | this._pubBytes = parseBytes(params.pub);
|
41190 | if (this._pubBytes && this._pubBytes.length === 33 &&
|
41191 | this._pubBytes[0] === 0x40)
|
41192 | this._pubBytes = this._pubBytes.slice(1, 33);
|
41193 | if (this._pubBytes && this._pubBytes.length !== 32)
|
41194 | throw new Error('Unknown point compression format');
|
41195 | }
|
41196 | }
|
41197 |
|
41198 | KeyPair$1.fromPublic = function fromPublic(eddsa, pub) {
|
41199 | if (pub instanceof KeyPair$1)
|
41200 | return pub;
|
41201 | return new KeyPair$1(eddsa, { pub: pub });
|
41202 | };
|
41203 |
|
41204 | KeyPair$1.fromSecret = function fromSecret(eddsa, secret) {
|
41205 | if (secret instanceof KeyPair$1)
|
41206 | return secret;
|
41207 | return new KeyPair$1(eddsa, { secret: secret });
|
41208 | };
|
41209 |
|
41210 | KeyPair$1.prototype.secret = function secret() {
|
41211 | return this._secret;
|
41212 | };
|
41213 |
|
41214 | cachedProperty(KeyPair$1, 'pubBytes', function pubBytes() {
|
41215 | return this.eddsa.encodePoint(this.pub());
|
41216 | });
|
41217 |
|
41218 | cachedProperty(KeyPair$1, 'pub', function pub() {
|
41219 | if (this._pubBytes)
|
41220 | return this.eddsa.decodePoint(this._pubBytes);
|
41221 | return this.eddsa.g.mul(this.priv());
|
41222 | });
|
41223 |
|
41224 | cachedProperty(KeyPair$1, 'privBytes', function privBytes() {
|
41225 | var eddsa = this.eddsa;
|
41226 | var hash = this.hash();
|
41227 | var lastIx = eddsa.encodingLength - 1;
|
41228 |
|
41229 |
|
41230 | var a = hash.slice(0, eddsa.encodingLength);
|
41231 | a[0] &= 248;
|
41232 | a[lastIx] &= 127;
|
41233 | a[lastIx] |= 64;
|
41234 |
|
41235 | return a;
|
41236 | });
|
41237 |
|
41238 | cachedProperty(KeyPair$1, 'priv', function priv() {
|
41239 | return this.eddsa.decodeInt(this.privBytes());
|
41240 | });
|
41241 |
|
41242 | cachedProperty(KeyPair$1, 'hash', function hash() {
|
41243 | return this.eddsa.hash().update(this.secret()).digest();
|
41244 | });
|
41245 |
|
41246 | cachedProperty(KeyPair$1, 'messagePrefix', function messagePrefix() {
|
41247 | return this.hash().slice(this.eddsa.encodingLength);
|
41248 | });
|
41249 |
|
41250 | KeyPair$1.prototype.sign = function sign(message) {
|
41251 | assert$8(this._secret, 'KeyPair can only verify');
|
41252 | return this.eddsa.sign(message, this);
|
41253 | };
|
41254 |
|
41255 | KeyPair$1.prototype.verify = function verify(message, sig) {
|
41256 | return this.eddsa.verify(message, sig, this);
|
41257 | };
|
41258 |
|
41259 | KeyPair$1.prototype.getSecret = function getSecret(enc) {
|
41260 | assert$8(this._secret, 'KeyPair is public only');
|
41261 | return utils_1$1.encode(this.secret(), enc);
|
41262 | };
|
41263 |
|
41264 | KeyPair$1.prototype.getPublic = function getPublic(enc, compact) {
|
41265 | return utils_1$1.encode((compact ? [ 0x40 ] : []).concat(this.pubBytes()), enc);
|
41266 | };
|
41267 |
|
41268 | var key$1 = KeyPair$1;
|
41269 |
|
41270 | var assert$9 = utils_1$1.assert;
|
41271 | var cachedProperty$1 = utils_1$1.cachedProperty;
|
41272 | var parseBytes$1 = utils_1$1.parseBytes;
|
41273 |
|
41274 |
|
41275 |
|
41276 |
|
41277 |
|
41278 |
|
41279 |
|
41280 |
|
41281 |
|
41282 | function Signature$2(eddsa, sig) {
|
41283 | this.eddsa = eddsa;
|
41284 |
|
41285 | if (typeof sig !== 'object')
|
41286 | sig = parseBytes$1(sig);
|
41287 |
|
41288 | if (Array.isArray(sig)) {
|
41289 | sig = {
|
41290 | R: sig.slice(0, eddsa.encodingLength),
|
41291 | S: sig.slice(eddsa.encodingLength)
|
41292 | };
|
41293 | }
|
41294 |
|
41295 | assert$9(sig.R && sig.S, 'Signature without R or S');
|
41296 |
|
41297 | if (eddsa.isPoint(sig.R))
|
41298 | this._R = sig.R;
|
41299 | if (sig.S instanceof bn)
|
41300 | this._S = sig.S;
|
41301 |
|
41302 | this._Rencoded = Array.isArray(sig.R) ? sig.R : sig.Rencoded;
|
41303 | this._Sencoded = Array.isArray(sig.S) ? sig.S : sig.Sencoded;
|
41304 | }
|
41305 |
|
41306 | cachedProperty$1(Signature$2, 'S', function S() {
|
41307 | return this.eddsa.decodeInt(this.Sencoded());
|
41308 | });
|
41309 |
|
41310 | cachedProperty$1(Signature$2, 'R', function R() {
|
41311 | return this.eddsa.decodePoint(this.Rencoded());
|
41312 | });
|
41313 |
|
41314 | cachedProperty$1(Signature$2, 'Rencoded', function Rencoded() {
|
41315 | return this.eddsa.encodePoint(this.R());
|
41316 | });
|
41317 |
|
41318 | cachedProperty$1(Signature$2, 'Sencoded', function Sencoded() {
|
41319 | return this.eddsa.encodeInt(this.S());
|
41320 | });
|
41321 |
|
41322 | Signature$2.prototype.toBytes = function toBytes() {
|
41323 | return this.Rencoded().concat(this.Sencoded());
|
41324 | };
|
41325 |
|
41326 | Signature$2.prototype.toHex = function toHex() {
|
41327 | return utils_1$1.encode(this.toBytes(), 'hex').toUpperCase();
|
41328 | };
|
41329 |
|
41330 | var signature$2 = Signature$2;
|
41331 |
|
41332 | var assert$a = utils_1$1.assert;
|
41333 | var parseBytes$2 = utils_1$1.parseBytes;
|
41334 |
|
41335 |
|
41336 |
|
41337 | function EDDSA(curve) {
|
41338 | assert$a(curve === 'ed25519', 'only tested with ed25519 so far');
|
41339 |
|
41340 | if (!(this instanceof EDDSA))
|
41341 | return new EDDSA(curve);
|
41342 |
|
41343 | var curve = curves_1[curve].curve;
|
41344 | this.curve = curve;
|
41345 | this.g = curve.g;
|
41346 | this.g.precompute(curve.n.bitLength() + 1);
|
41347 |
|
41348 | this.pointClass = curve.point().constructor;
|
41349 | this.encodingLength = Math.ceil(curve.n.bitLength() / 8);
|
41350 | this.hash = hash_1.sha512;
|
41351 | }
|
41352 |
|
41353 | var eddsa$1 = EDDSA;
|
41354 |
|
41355 |
|
41356 |
|
41357 |
|
41358 |
|
41359 |
|
41360 | EDDSA.prototype.sign = function sign(message, secret) {
|
41361 | message = parseBytes$2(message);
|
41362 | var key = this.keyFromSecret(secret);
|
41363 | var r = this.hashInt(key.messagePrefix(), message);
|
41364 | var R = this.g.mul(r);
|
41365 | var Rencoded = this.encodePoint(R);
|
41366 | var s_ = this.hashInt(Rencoded, key.pubBytes(), message)
|
41367 | .mul(key.priv());
|
41368 | var S = r.add(s_).umod(this.curve.n);
|
41369 | return this.makeSignature({ R: R, S: S, Rencoded: Rencoded });
|
41370 | };
|
41371 |
|
41372 |
|
41373 |
|
41374 |
|
41375 |
|
41376 |
|
41377 |
|
41378 | EDDSA.prototype.verify = function verify(message, sig, pub) {
|
41379 | message = parseBytes$2(message);
|
41380 | sig = this.makeSignature(sig);
|
41381 | var key = this.keyFromPublic(pub);
|
41382 | var h = this.hashInt(sig.Rencoded(), key.pubBytes(), message);
|
41383 | var SG = this.g.mul(sig.S());
|
41384 | var RplusAh = sig.R().add(key.pub().mul(h));
|
41385 | return RplusAh.eq(SG);
|
41386 | };
|
41387 |
|
41388 | EDDSA.prototype.hashInt = function hashInt() {
|
41389 | var hash = this.hash();
|
41390 | for (var i = 0; i < arguments.length; i++)
|
41391 | hash.update(arguments[i]);
|
41392 | return utils_1$1.intFromLE(hash.digest()).umod(this.curve.n);
|
41393 | };
|
41394 |
|
41395 | EDDSA.prototype.keyPair = function keyPair(options) {
|
41396 | return new key$1(this, options);
|
41397 | };
|
41398 |
|
41399 | EDDSA.prototype.keyFromPublic = function keyFromPublic(pub) {
|
41400 | return key$1.fromPublic(this, pub);
|
41401 | };
|
41402 |
|
41403 | EDDSA.prototype.keyFromSecret = function keyFromSecret(secret) {
|
41404 | return key$1.fromSecret(this, secret);
|
41405 | };
|
41406 |
|
41407 | EDDSA.prototype.genKeyPair = function genKeyPair(options) {
|
41408 | if (!options)
|
41409 | options = {};
|
41410 |
|
41411 |
|
41412 | var drbg = new hmacDrbg({
|
41413 | hash: this.hash,
|
41414 | pers: options.pers,
|
41415 | persEnc: options.persEnc || 'utf8',
|
41416 | entropy: options.entropy || brorand(this.hash.hmacStrength),
|
41417 | entropyEnc: options.entropy && options.entropyEnc || 'utf8',
|
41418 | nonce: this.curve.n.toArray()
|
41419 | });
|
41420 |
|
41421 | return this.keyFromSecret(drbg.generate(32));
|
41422 | };
|
41423 |
|
41424 | EDDSA.prototype.makeSignature = function makeSignature(sig) {
|
41425 | if (sig instanceof signature$2)
|
41426 | return sig;
|
41427 | return new signature$2(this, sig);
|
41428 | };
|
41429 |
|
41430 |
|
41431 |
|
41432 |
|
41433 |
|
41434 |
|
41435 |
|
41436 |
|
41437 |
|
41438 | EDDSA.prototype.encodePoint = function encodePoint(point) {
|
41439 | var enc = point.getY().toArray('le', this.encodingLength);
|
41440 | enc[this.encodingLength - 1] |= point.getX().isOdd() ? 0x80 : 0;
|
41441 | return enc;
|
41442 | };
|
41443 |
|
41444 | EDDSA.prototype.decodePoint = function decodePoint(bytes) {
|
41445 | bytes = utils_1$1.parseBytes(bytes);
|
41446 |
|
41447 | var lastIx = bytes.length - 1;
|
41448 | var normed = bytes.slice(0, lastIx).concat(bytes[lastIx] & ~0x80);
|
41449 | var xIsOdd = (bytes[lastIx] & 0x80) !== 0;
|
41450 |
|
41451 | var y = utils_1$1.intFromLE(normed);
|
41452 | return this.curve.pointFromY(y, xIsOdd);
|
41453 | };
|
41454 |
|
41455 | EDDSA.prototype.encodeInt = function encodeInt(num) {
|
41456 | return num.toArray('le', this.encodingLength);
|
41457 | };
|
41458 |
|
41459 | EDDSA.prototype.decodeInt = function decodeInt(bytes) {
|
41460 | return utils_1$1.intFromLE(bytes);
|
41461 | };
|
41462 |
|
41463 | EDDSA.prototype.isPoint = function isPoint(val) {
|
41464 | return val instanceof this.pointClass;
|
41465 | };
|
41466 |
|
41467 | var elliptic_1 = createCommonjsModule(function (module, exports) {
|
41468 |
|
41469 | var elliptic = exports;
|
41470 |
|
41471 | elliptic.utils = utils_1$1;
|
41472 | elliptic.rand = brorand;
|
41473 | elliptic.curve = curve_1;
|
41474 | elliptic.curves = curves_1;
|
41475 |
|
41476 |
|
41477 | elliptic.ec = ec;
|
41478 | elliptic.eddsa = eddsa$1;
|
41479 | });
|
41480 |
|
41481 | var elliptic$1 = Object.freeze({
|
41482 | __proto__: null,
|
41483 | 'default': elliptic_1,
|
41484 | __moduleExports: elliptic_1
|
41485 | });
|
41486 |
|
41487 | export { AEADEncryptedDataPacket, CleartextMessage, CompressedDataPacket, Key, LiteralDataPacket, MarkerPacket, Message, OnePassSignaturePacket, PacketList, PublicKeyEncryptedSessionKeyPacket, PublicKeyPacket, PublicSubkeyPacket, SecretKeyPacket, SecretSubkeyPacket, Signature, SignaturePacket, SymEncryptedIntegrityProtectedDataPacket, SymEncryptedSessionKeyPacket, SymmetricallyEncryptedDataPacket, TrustPacket, UserAttributePacket, UserIDPacket, armor, defaultConfig as config, decrypt$4 as decrypt, decryptKey, decryptSessionKeys, encrypt$4 as encrypt, encryptKey, encryptSessionKey, enums, generateKey, generateSessionKey$1 as generateSessionKey, newPacketFromTag, readCleartextMessage, readKey, readKeys, readMessage, readSignature, reformatKey, revokeKey, sign$5 as sign, stream, unarmor, verify$5 as verify };
|