pdfkit 0.20.2 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -0
- package/js/pdfkit.browser.mjs +2409 -433
- package/js/pdfkit.browser.mjs.map +1 -1
- package/js/pdfkit.js +2451 -453
- package/js/pdfkit.js.map +1 -1
- package/js/pdfkit.node.mjs +2443 -449
- package/js/pdfkit.node.mjs.map +1 -1
- package/js/pdfkit.standalone.js +21578 -32704
- package/js/standard-fonts/Symbol.cjs +4 -3
- package/js/standard-fonts/Symbol.mjs +4 -3
- package/js/standard-fonts/ZapfDingbats.cjs +4 -3
- package/js/standard-fonts/ZapfDingbats.mjs +4 -3
- package/package.json +24 -21
- package/.yarn/install-state.gz +0 -0
- package/.yarn/releases/yarn-4.16.0.cjs +0 -944
- package/.yarnrc.yml +0 -10
- package/CHANGELOG.md +0 -198
- package/js/data/Courier-Bold.afm +0 -342
- package/js/data/Courier-BoldOblique.afm +0 -342
- package/js/data/Courier-Oblique.afm +0 -342
- package/js/data/Courier.afm +0 -342
- package/js/data/Helvetica-Bold.afm +0 -2827
- package/js/data/Helvetica-BoldOblique.afm +0 -2827
- package/js/data/Helvetica-Oblique.afm +0 -3051
- package/js/data/Helvetica.afm +0 -3051
- package/js/data/Symbol.afm +0 -213
- package/js/data/Times-Bold.afm +0 -2588
- package/js/data/Times-BoldItalic.afm +0 -2384
- package/js/data/Times-Italic.afm +0 -2667
- package/js/data/Times-Roman.afm +0 -2419
- package/js/data/ZapfDingbats.afm +0 -225
- package/js/pdfkit.browser.js +0 -7530
- package/js/pdfkit.browser.js.map +0 -1
- package/js/pdfkit.browser.old.js +0 -7517
- package/js/pdfkit.browser.old.mjs +0 -7342
- package/js/pdfkit.old.js +0 -7276
- package/jsconfig.json +0 -13
- package/rollup.config.mjs +0 -193
- package/tools/README.md +0 -124
- package/tools/afm-converter.js +0 -317
- package/tools/asset-plugin.mjs +0 -17
- package/tools/binary-plugin.mjs +0 -18
- package/tools/convert-afm-parsed.js +0 -8
- package/tools/convert-afm-runtime.js +0 -8
- package/types/jest.custom-matchers.d.ts +0 -28
- package/vitest.config.js +0 -13
package/js/pdfkit.js
CHANGED
|
@@ -4,17 +4,113 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var module$1 = require('module');
|
|
6
6
|
var stream = require('stream');
|
|
7
|
-
var utils = require('@noble/hashes/utils');
|
|
8
7
|
var zlib$1 = require('zlib');
|
|
9
|
-
var aes = require('@noble/ciphers/aes');
|
|
10
8
|
var fs$1 = require('fs');
|
|
11
9
|
var fontkit = require('fontkit');
|
|
12
|
-
var
|
|
10
|
+
var linebreak = require('linebreak');
|
|
13
11
|
var PNG = require('png-js');
|
|
14
12
|
|
|
15
13
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
16
14
|
var Readable = stream.Readable;
|
|
17
15
|
|
|
16
|
+
function isBytes$1(a) {
|
|
17
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array' && 'BYTES_PER_ELEMENT' in a && a.BYTES_PER_ELEMENT === 1;
|
|
18
|
+
}
|
|
19
|
+
const atitle$1 = title => title ? `"${title}" ` : '';
|
|
20
|
+
function abytes$1(value, length, title = '') {
|
|
21
|
+
if (isBytes$1(value) && (length === undefined)) return value;
|
|
22
|
+
const bytes = isBytes$1(value);
|
|
23
|
+
const ofLen = '';
|
|
24
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
25
|
+
const message = atitle$1(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
26
|
+
if (!bytes) throw new TypeError(message);
|
|
27
|
+
throw new RangeError(message);
|
|
28
|
+
}
|
|
29
|
+
const aobject = (value, label) => {
|
|
30
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) throw new TypeError((label === 'object' ? '' : `"${label}" `) + 'expected object, got type=' + typeof value);
|
|
31
|
+
};
|
|
32
|
+
const aopts = (value, label) => {
|
|
33
|
+
aobject(value, label);
|
|
34
|
+
const proto = Object.getPrototypeOf(value);
|
|
35
|
+
if (proto !== Object.prototype && proto !== null) throw new TypeError(`"${label}" expected plain object`);
|
|
36
|
+
if (Object.hasOwn(value, '__proto__')) throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
37
|
+
};
|
|
38
|
+
function aexists$1(instance, checkFinished = true) {
|
|
39
|
+
if (instance.destroyed) throw new Error('hash was destroyed');
|
|
40
|
+
if (checkFinished && instance.finished) throw new Error('digest() was already called');
|
|
41
|
+
}
|
|
42
|
+
function aoutput$1(out, instance) {
|
|
43
|
+
abytes$1(out, undefined, 'output');
|
|
44
|
+
const min = instance.outputLen;
|
|
45
|
+
if (!(out.length >= min)) {
|
|
46
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function clean$1(...arrays) {
|
|
50
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
51
|
+
arrays[i].fill(0);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function createView$1(arr) {
|
|
55
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
56
|
+
}
|
|
57
|
+
function rotr(word, shift) {
|
|
58
|
+
return word << 32 - shift | word >>> shift;
|
|
59
|
+
}
|
|
60
|
+
function rotl(word, shift) {
|
|
61
|
+
return word << shift | word >>> 32 - shift >>> 0;
|
|
62
|
+
}
|
|
63
|
+
(() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
64
|
+
const hasHexBuiltin = (() => typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
65
|
+
const hexes = Array.from({
|
|
66
|
+
length: 256
|
|
67
|
+
}, (_, i) => i.toString(16).padStart(2, '0'));
|
|
68
|
+
function bytesToHex(bytes) {
|
|
69
|
+
abytes$1(bytes);
|
|
70
|
+
if (hasHexBuiltin) return bytes.toHex();
|
|
71
|
+
let hex = '';
|
|
72
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
73
|
+
hex += hexes[bytes[i]];
|
|
74
|
+
}
|
|
75
|
+
return hex;
|
|
76
|
+
}
|
|
77
|
+
function concatBytes$1(...arrays) {
|
|
78
|
+
let sum = 0;
|
|
79
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
80
|
+
const a = arrays[i];
|
|
81
|
+
abytes$1(a);
|
|
82
|
+
sum += a.length;
|
|
83
|
+
}
|
|
84
|
+
const res = new Uint8Array(sum);
|
|
85
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
86
|
+
const a = arrays[i];
|
|
87
|
+
res.set(a, pad);
|
|
88
|
+
pad += a.length;
|
|
89
|
+
}
|
|
90
|
+
return res;
|
|
91
|
+
}
|
|
92
|
+
function checkOpts(defaults, opts, title = 'opts') {
|
|
93
|
+
aopts(defaults, 'defaults');
|
|
94
|
+
if (opts !== undefined) aopts(opts, title);
|
|
95
|
+
const merged = Object.assign(Object.create(null), defaults, opts);
|
|
96
|
+
return merged;
|
|
97
|
+
}
|
|
98
|
+
function createHasher(hashCons, info = {}) {
|
|
99
|
+
if (typeof hashCons !== 'function') throw new TypeError('"hashCons" expected function, got type=' + typeof hashCons);
|
|
100
|
+
info = checkOpts({}, info, 'info');
|
|
101
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
102
|
+
const tmp = hashCons(undefined);
|
|
103
|
+
hashC.outputLen = tmp.outputLen;
|
|
104
|
+
hashC.blockLen = tmp.blockLen;
|
|
105
|
+
hashC.canXOF = tmp.canXOF;
|
|
106
|
+
hashC.create = opts => hashCons(opts);
|
|
107
|
+
Object.assign(hashC, info);
|
|
108
|
+
return Object.freeze(hashC);
|
|
109
|
+
}
|
|
110
|
+
const oidNist = suffix => ({
|
|
111
|
+
oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix])
|
|
112
|
+
});
|
|
113
|
+
|
|
18
114
|
class PDFAbstractReference {
|
|
19
115
|
toString() {
|
|
20
116
|
throw new Error('Must be implemented by subclasses');
|
|
@@ -184,7 +280,7 @@ class PDFObject {
|
|
|
184
280
|
string = string.replace(escapableRe, c => escapable[c]);
|
|
185
281
|
return `(${string})`;
|
|
186
282
|
} else if (object instanceof Uint8Array) {
|
|
187
|
-
return `<${
|
|
283
|
+
return `<${bytesToHex(object)}>`;
|
|
188
284
|
} else if (object instanceof PDFTree) {
|
|
189
285
|
return object.toString(encryptFn);
|
|
190
286
|
} else if (object instanceof PDFAbstractReference || object instanceof SpotColor) {
|
|
@@ -262,6 +358,12 @@ class PDFReference extends PDFAbstractReference {
|
|
|
262
358
|
this.finalize();
|
|
263
359
|
}
|
|
264
360
|
finalize() {
|
|
361
|
+
const objectStreams = this.document._objectStreams;
|
|
362
|
+
if (objectStreams && !this.buffer.length) {
|
|
363
|
+
objectStreams.add(this);
|
|
364
|
+
this.document._refEnd(this);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
265
367
|
this.offset = this.document._offset;
|
|
266
368
|
const encryptFn = this.document._security ? this.document._security.getEncryptFn(this.id, this.gen) : null;
|
|
267
369
|
if (this.buffer.length) {
|
|
@@ -370,7 +472,7 @@ const DEFAULT_MARGINS = {
|
|
|
370
472
|
bottom: 72,
|
|
371
473
|
right: 72
|
|
372
474
|
};
|
|
373
|
-
const
|
|
475
|
+
const PAGE_SIZES = {
|
|
374
476
|
'4A0': [4767.87, 6740.79],
|
|
375
477
|
'2A0': [3370.39, 4767.87],
|
|
376
478
|
A0: [2383.94, 3370.39],
|
|
@@ -429,7 +531,7 @@ class PDFPage {
|
|
|
429
531
|
this.size = options.size || 'letter';
|
|
430
532
|
this.layout = options.layout || 'portrait';
|
|
431
533
|
this.userUnit = options.userUnit || 1.0;
|
|
432
|
-
const dimensions = Array.isArray(this.size) ? this.size :
|
|
534
|
+
const dimensions = Array.isArray(this.size) ? this.size : PAGE_SIZES[this.size.toUpperCase()];
|
|
433
535
|
this.width = dimensions[this.layout === 'portrait' ? 0 : 1];
|
|
434
536
|
this.height = dimensions[this.layout === 'portrait' ? 1 : 0];
|
|
435
537
|
this.content = this.document.ref();
|
|
@@ -506,7 +608,7 @@ class PDFPage {
|
|
|
506
608
|
|
|
507
609
|
class PDFNameTree extends PDFTree {
|
|
508
610
|
_compareKeys(a, b) {
|
|
509
|
-
return a
|
|
611
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
510
612
|
}
|
|
511
613
|
_keysName() {
|
|
512
614
|
return 'Names';
|
|
@@ -516,125 +618,119 @@ class PDFNameTree extends PDFTree {
|
|
|
516
618
|
}
|
|
517
619
|
}
|
|
518
620
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
}
|
|
530
|
-
function aoutput(out, instance) {
|
|
531
|
-
abytes(out);
|
|
532
|
-
const min = instance.outputLen;
|
|
533
|
-
if (out.length < min) {
|
|
534
|
-
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
|
535
|
-
}
|
|
621
|
+
const U32_MASK64 = (() => BigInt(2 ** 32 - 1))();
|
|
622
|
+
const _32n = BigInt(32);
|
|
623
|
+
function fromBig(n, le = false) {
|
|
624
|
+
if (le) return {
|
|
625
|
+
h: Number(n & U32_MASK64),
|
|
626
|
+
l: Number(n >> _32n & U32_MASK64)
|
|
627
|
+
};
|
|
628
|
+
return {
|
|
629
|
+
h: Number(n >> _32n & U32_MASK64) | 0,
|
|
630
|
+
l: Number(n & U32_MASK64) | 0
|
|
631
|
+
};
|
|
536
632
|
}
|
|
537
|
-
function
|
|
538
|
-
|
|
539
|
-
|
|
633
|
+
function split(lst, le = false) {
|
|
634
|
+
const len = lst.length;
|
|
635
|
+
let Ah = new Uint32Array(len);
|
|
636
|
+
let Al = new Uint32Array(len);
|
|
637
|
+
for (let i = 0; i < len; i++) {
|
|
638
|
+
const {
|
|
639
|
+
h,
|
|
640
|
+
l
|
|
641
|
+
} = fromBig(lst[i], le);
|
|
642
|
+
[Ah[i], Al[i]] = [h, l];
|
|
540
643
|
}
|
|
644
|
+
return [Ah, Al];
|
|
541
645
|
}
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
return word << shift | word >>> 32 - shift >>> 0;
|
|
550
|
-
}
|
|
551
|
-
(() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
552
|
-
(() => typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
553
|
-
Array.from({
|
|
554
|
-
length: 256
|
|
555
|
-
}, (_, i) => i.toString(16).padStart(2, '0'));
|
|
556
|
-
function utf8ToBytes(str) {
|
|
557
|
-
if (typeof str !== 'string') throw new Error('string expected');
|
|
558
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
559
|
-
}
|
|
560
|
-
function toBytes(data) {
|
|
561
|
-
if (typeof data === 'string') data = utf8ToBytes(data);
|
|
562
|
-
abytes(data);
|
|
563
|
-
return data;
|
|
646
|
+
const fromNumH = n => n / 2 ** 32 | 0;
|
|
647
|
+
const fromNumL = n => n >>> 0;
|
|
648
|
+
function setU64FromNum(view, byteOffset, n, isLE) {
|
|
649
|
+
const h = fromNumH(n);
|
|
650
|
+
const l = fromNumL(n);
|
|
651
|
+
view.setUint32(byteOffset, isLE ? l : h, isLE);
|
|
652
|
+
view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
|
|
564
653
|
}
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
654
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
655
|
+
const shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
656
|
+
const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
657
|
+
const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
658
|
+
const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
659
|
+
const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
660
|
+
function add(Ah, Al, Bh, Bl) {
|
|
661
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
662
|
+
return {
|
|
663
|
+
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
664
|
+
l: l | 0
|
|
665
|
+
};
|
|
573
666
|
}
|
|
667
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
668
|
+
const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
669
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
670
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
671
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
672
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
574
673
|
|
|
575
|
-
function setBigUint64(view, byteOffset, value, isLE) {
|
|
576
|
-
if (typeof view.setBigUint64 === 'function') return view.setBigUint64(byteOffset, value, isLE);
|
|
577
|
-
const _32n = BigInt(32);
|
|
578
|
-
const _u32_max = BigInt(0xffffffff);
|
|
579
|
-
const wh = Number(value >> _32n & _u32_max);
|
|
580
|
-
const wl = Number(value & _u32_max);
|
|
581
|
-
const h = isLE ? 4 : 0;
|
|
582
|
-
const l = isLE ? 0 : 4;
|
|
583
|
-
view.setUint32(byteOffset + h, wh, isLE);
|
|
584
|
-
view.setUint32(byteOffset + l, wl, isLE);
|
|
585
|
-
}
|
|
586
674
|
function Chi(a, b, c) {
|
|
587
675
|
return a & b ^ ~a & c;
|
|
588
676
|
}
|
|
589
677
|
function Maj(a, b, c) {
|
|
590
678
|
return a & b ^ a & c ^ b & c;
|
|
591
679
|
}
|
|
592
|
-
class HashMD
|
|
680
|
+
class HashMD {
|
|
681
|
+
blockLen;
|
|
682
|
+
outputLen;
|
|
683
|
+
canXOF = false;
|
|
684
|
+
padOffset;
|
|
685
|
+
isLE;
|
|
686
|
+
buffer;
|
|
687
|
+
view;
|
|
688
|
+
finished = false;
|
|
689
|
+
length = 0;
|
|
690
|
+
pos = 0;
|
|
691
|
+
destroyed = false;
|
|
593
692
|
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
594
|
-
super();
|
|
595
|
-
this.finished = false;
|
|
596
|
-
this.length = 0;
|
|
597
|
-
this.pos = 0;
|
|
598
|
-
this.destroyed = false;
|
|
599
693
|
this.blockLen = blockLen;
|
|
600
694
|
this.outputLen = outputLen;
|
|
601
695
|
this.padOffset = padOffset;
|
|
602
696
|
this.isLE = isLE;
|
|
603
697
|
this.buffer = new Uint8Array(blockLen);
|
|
604
|
-
this.view = createView(this.buffer);
|
|
698
|
+
this.view = createView$1(this.buffer);
|
|
605
699
|
}
|
|
606
700
|
update(data) {
|
|
607
|
-
aexists(this);
|
|
608
|
-
|
|
609
|
-
abytes(data);
|
|
701
|
+
aexists$1(this);
|
|
702
|
+
abytes$1(data);
|
|
610
703
|
const {
|
|
611
704
|
view,
|
|
612
705
|
buffer,
|
|
613
706
|
blockLen
|
|
614
707
|
} = this;
|
|
615
708
|
const len = data.length;
|
|
709
|
+
let processed = false;
|
|
616
710
|
for (let pos = 0; pos < len;) {
|
|
617
711
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
618
712
|
if (take === blockLen) {
|
|
619
|
-
const dataView = createView(data);
|
|
713
|
+
const dataView = createView$1(data);
|
|
620
714
|
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
|
715
|
+
processed = true;
|
|
621
716
|
continue;
|
|
622
717
|
}
|
|
623
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
718
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
624
719
|
this.pos += take;
|
|
625
720
|
pos += take;
|
|
626
721
|
if (this.pos === blockLen) {
|
|
627
722
|
this.process(view, 0);
|
|
628
723
|
this.pos = 0;
|
|
724
|
+
processed = true;
|
|
629
725
|
}
|
|
630
726
|
}
|
|
631
727
|
this.length += data.length;
|
|
632
|
-
this.roundClean();
|
|
728
|
+
if (processed) this.roundClean();
|
|
633
729
|
return this;
|
|
634
730
|
}
|
|
635
731
|
digestInto(out) {
|
|
636
|
-
aexists(this);
|
|
637
|
-
aoutput(out, this);
|
|
732
|
+
aexists$1(this);
|
|
733
|
+
aoutput$1(out, this);
|
|
638
734
|
this.finished = true;
|
|
639
735
|
const {
|
|
640
736
|
buffer,
|
|
@@ -646,20 +742,19 @@ class HashMD extends Hash {
|
|
|
646
742
|
pos
|
|
647
743
|
} = this;
|
|
648
744
|
buffer[pos++] = 0b10000000;
|
|
649
|
-
|
|
745
|
+
buffer.fill(0, pos);
|
|
650
746
|
if (this.padOffset > blockLen - pos) {
|
|
651
747
|
this.process(view, 0);
|
|
652
|
-
|
|
748
|
+
buffer.fill(0);
|
|
653
749
|
}
|
|
654
|
-
|
|
655
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
750
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
|
|
656
751
|
this.process(view, 0);
|
|
657
|
-
|
|
752
|
+
this.roundClean();
|
|
753
|
+
const oview = out === buffer ? view : createView$1(out);
|
|
658
754
|
const len = this.outputLen;
|
|
659
|
-
if (len % 4) throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
660
755
|
const outLen = len / 4;
|
|
661
756
|
const state = this.get();
|
|
662
|
-
if (outLen > state.length) throw new Error('
|
|
757
|
+
if (len % 4 || outLen > state.length) throw new Error('invalid outputLen');
|
|
663
758
|
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
|
664
759
|
}
|
|
665
760
|
digest() {
|
|
@@ -672,11 +767,8 @@ class HashMD extends Hash {
|
|
|
672
767
|
this.destroy();
|
|
673
768
|
return res;
|
|
674
769
|
}
|
|
675
|
-
|
|
676
|
-
to || (to = new this.constructor());
|
|
677
|
-
to.set(...this.get());
|
|
770
|
+
_cloneIntoMeta(to) {
|
|
678
771
|
const {
|
|
679
|
-
blockLen,
|
|
680
772
|
buffer,
|
|
681
773
|
length,
|
|
682
774
|
finished,
|
|
@@ -687,7 +779,7 @@ class HashMD extends Hash {
|
|
|
687
779
|
to.finished = finished;
|
|
688
780
|
to.length = length;
|
|
689
781
|
to.pos = pos;
|
|
690
|
-
if (
|
|
782
|
+
if (pos) to.buffer.set(buffer);
|
|
691
783
|
return to;
|
|
692
784
|
}
|
|
693
785
|
clone() {
|
|
@@ -701,14 +793,14 @@ const SHA512_IV = Uint32Array.from([0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa7
|
|
|
701
793
|
|
|
702
794
|
const SHA1_IV = Uint32Array.from([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]);
|
|
703
795
|
const SHA1_W = new Uint32Array(80);
|
|
704
|
-
class
|
|
796
|
+
class _SHA1 extends HashMD {
|
|
797
|
+
A = SHA1_IV[0] | 0;
|
|
798
|
+
B = SHA1_IV[1] | 0;
|
|
799
|
+
C = SHA1_IV[2] | 0;
|
|
800
|
+
D = SHA1_IV[3] | 0;
|
|
801
|
+
E = SHA1_IV[4] | 0;
|
|
705
802
|
constructor() {
|
|
706
803
|
super(64, 20, 8, false);
|
|
707
|
-
this.A = SHA1_IV[0] | 0;
|
|
708
|
-
this.B = SHA1_IV[1] | 0;
|
|
709
|
-
this.C = SHA1_IV[2] | 0;
|
|
710
|
-
this.D = SHA1_IV[3] | 0;
|
|
711
|
-
this.E = SHA1_IV[4] | 0;
|
|
712
804
|
}
|
|
713
805
|
get() {
|
|
714
806
|
const {
|
|
@@ -727,6 +819,10 @@ class SHA1 extends HashMD {
|
|
|
727
819
|
this.D = D | 0;
|
|
728
820
|
this.E = E | 0;
|
|
729
821
|
}
|
|
822
|
+
_cloneInto(to) {
|
|
823
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
824
|
+
return this._cloneIntoMeta(to);
|
|
825
|
+
}
|
|
730
826
|
process(view, offset) {
|
|
731
827
|
for (let i = 0; i < 16; i++, offset += 4) SHA1_W[i] = view.getUint32(offset, false);
|
|
732
828
|
for (let i = 16; i < 80; i++) SHA1_W[i] = rotl(SHA1_W[i - 3] ^ SHA1_W[i - 8] ^ SHA1_W[i - 14] ^ SHA1_W[i - 16], 1);
|
|
@@ -767,27 +863,34 @@ class SHA1 extends HashMD {
|
|
|
767
863
|
this.set(A, B, C, D, E);
|
|
768
864
|
}
|
|
769
865
|
roundClean() {
|
|
770
|
-
clean(SHA1_W);
|
|
866
|
+
clean$1(SHA1_W);
|
|
771
867
|
}
|
|
772
868
|
destroy() {
|
|
869
|
+
this.destroyed = true;
|
|
773
870
|
this.set(0, 0, 0, 0, 0);
|
|
774
|
-
clean(this.buffer);
|
|
871
|
+
clean$1(this.buffer);
|
|
775
872
|
}
|
|
776
873
|
}
|
|
777
|
-
createHasher(() => new
|
|
874
|
+
createHasher(() => new _SHA1());
|
|
778
875
|
const p32 = Math.pow(2, 32);
|
|
779
876
|
const K = Array.from({
|
|
780
877
|
length: 64
|
|
781
878
|
}, (_, i) => Math.floor(p32 * Math.abs(Math.sin(i + 1))));
|
|
782
879
|
const MD5_IV = SHA1_IV.slice(0, 4);
|
|
783
880
|
const MD5_W = new Uint32Array(16);
|
|
784
|
-
|
|
881
|
+
const MD5_SHIFTS = (() => {
|
|
882
|
+
const S = [[7, 12, 17, 22], [5, 9, 14, 20], [4, 11, 16, 23], [6, 10, 15, 21]];
|
|
883
|
+
return Uint8Array.from({
|
|
884
|
+
length: 64
|
|
885
|
+
}, (_, i) => S[Math.floor(i / 16)][i % 4]);
|
|
886
|
+
})();
|
|
887
|
+
class _MD5 extends HashMD {
|
|
888
|
+
A = MD5_IV[0] | 0;
|
|
889
|
+
B = MD5_IV[1] | 0;
|
|
890
|
+
C = MD5_IV[2] | 0;
|
|
891
|
+
D = MD5_IV[3] | 0;
|
|
785
892
|
constructor() {
|
|
786
893
|
super(64, 16, 8, true);
|
|
787
|
-
this.A = MD5_IV[0] | 0;
|
|
788
|
-
this.B = MD5_IV[1] | 0;
|
|
789
|
-
this.C = MD5_IV[2] | 0;
|
|
790
|
-
this.D = MD5_IV[3] | 0;
|
|
791
894
|
}
|
|
792
895
|
get() {
|
|
793
896
|
const {
|
|
@@ -804,6 +907,10 @@ class MD5 extends HashMD {
|
|
|
804
907
|
this.C = C | 0;
|
|
805
908
|
this.D = D | 0;
|
|
806
909
|
}
|
|
910
|
+
_cloneInto(to) {
|
|
911
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
912
|
+
return this._cloneIntoMeta(to);
|
|
913
|
+
}
|
|
807
914
|
process(view, offset) {
|
|
808
915
|
for (let i = 0; i < 16; i++, offset += 4) MD5_W[i] = view.getUint32(offset, true);
|
|
809
916
|
let {
|
|
@@ -813,29 +920,25 @@ class MD5 extends HashMD {
|
|
|
813
920
|
D
|
|
814
921
|
} = this;
|
|
815
922
|
for (let i = 0; i < 64; i++) {
|
|
816
|
-
let F, g
|
|
923
|
+
let F, g;
|
|
817
924
|
if (i < 16) {
|
|
818
925
|
F = Chi(B, C, D);
|
|
819
926
|
g = i;
|
|
820
|
-
s = [7, 12, 17, 22];
|
|
821
927
|
} else if (i < 32) {
|
|
822
928
|
F = Chi(D, B, C);
|
|
823
929
|
g = (5 * i + 1) % 16;
|
|
824
|
-
s = [5, 9, 14, 20];
|
|
825
930
|
} else if (i < 48) {
|
|
826
931
|
F = B ^ C ^ D;
|
|
827
932
|
g = (3 * i + 5) % 16;
|
|
828
|
-
s = [4, 11, 16, 23];
|
|
829
933
|
} else {
|
|
830
934
|
F = C ^ (B | ~D);
|
|
831
935
|
g = 7 * i % 16;
|
|
832
|
-
s = [6, 10, 15, 21];
|
|
833
936
|
}
|
|
834
937
|
F = F + A + K[i] + MD5_W[g];
|
|
835
938
|
A = D;
|
|
836
939
|
D = C;
|
|
837
940
|
C = B;
|
|
838
|
-
B = B + rotl(F,
|
|
941
|
+
B = B + rotl(F, MD5_SHIFTS[i]);
|
|
839
942
|
}
|
|
840
943
|
A = A + this.A | 0;
|
|
841
944
|
B = B + this.B | 0;
|
|
@@ -844,14 +947,15 @@ class MD5 extends HashMD {
|
|
|
844
947
|
this.set(A, B, C, D);
|
|
845
948
|
}
|
|
846
949
|
roundClean() {
|
|
847
|
-
clean(MD5_W);
|
|
950
|
+
clean$1(MD5_W);
|
|
848
951
|
}
|
|
849
952
|
destroy() {
|
|
953
|
+
this.destroyed = true;
|
|
850
954
|
this.set(0, 0, 0, 0);
|
|
851
|
-
clean(this.buffer);
|
|
955
|
+
clean$1(this.buffer);
|
|
852
956
|
}
|
|
853
957
|
}
|
|
854
|
-
const md5 = createHasher(() => new
|
|
958
|
+
const md5 = createHasher(() => new _MD5());
|
|
855
959
|
const Rho160 = Uint8Array.from([7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8]);
|
|
856
960
|
const Id160 = (() => Uint8Array.from(new Array(16).fill(0).map((_, i) => i)))();
|
|
857
961
|
const Pi160 = (() => Id160.map(i => (9 * i + 5) % 16))();
|
|
@@ -877,14 +981,14 @@ function ripemd_f(group, x, y, z) {
|
|
|
877
981
|
return x ^ (y | ~z);
|
|
878
982
|
}
|
|
879
983
|
const BUF_160 = new Uint32Array(16);
|
|
880
|
-
class
|
|
984
|
+
class _RIPEMD160 extends HashMD {
|
|
985
|
+
h0 = 0x67452301 | 0;
|
|
986
|
+
h1 = 0xefcdab89 | 0;
|
|
987
|
+
h2 = 0x98badcfe | 0;
|
|
988
|
+
h3 = 0x10325476 | 0;
|
|
989
|
+
h4 = 0xc3d2e1f0 | 0;
|
|
881
990
|
constructor() {
|
|
882
991
|
super(64, 20, 8, true);
|
|
883
|
-
this.h0 = 0x67452301 | 0;
|
|
884
|
-
this.h1 = 0xefcdab89 | 0;
|
|
885
|
-
this.h2 = 0x98badcfe | 0;
|
|
886
|
-
this.h3 = 0x10325476 | 0;
|
|
887
|
-
this.h4 = 0xc3d2e1f0 | 0;
|
|
888
992
|
}
|
|
889
993
|
get() {
|
|
890
994
|
const {
|
|
@@ -903,6 +1007,10 @@ class RIPEMD160 extends HashMD {
|
|
|
903
1007
|
this.h3 = h3 | 0;
|
|
904
1008
|
this.h4 = h4 | 0;
|
|
905
1009
|
}
|
|
1010
|
+
_cloneInto(to) {
|
|
1011
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1012
|
+
return this._cloneIntoMeta(to);
|
|
1013
|
+
}
|
|
906
1014
|
process(view, offset) {
|
|
907
1015
|
for (let i = 0; i < 16; i++, offset += 4) BUF_160[i] = view.getUint32(offset, true);
|
|
908
1016
|
let al = this.h0 | 0,
|
|
@@ -935,81 +1043,44 @@ class RIPEMD160 extends HashMD {
|
|
|
935
1043
|
this.set(this.h1 + cl + dr | 0, this.h2 + dl + er | 0, this.h3 + el + ar | 0, this.h4 + al + br | 0, this.h0 + bl + cr | 0);
|
|
936
1044
|
}
|
|
937
1045
|
roundClean() {
|
|
938
|
-
clean(BUF_160);
|
|
1046
|
+
clean$1(BUF_160);
|
|
939
1047
|
}
|
|
940
1048
|
destroy() {
|
|
941
1049
|
this.destroyed = true;
|
|
942
|
-
clean(this.buffer);
|
|
1050
|
+
clean$1(this.buffer);
|
|
943
1051
|
this.set(0, 0, 0, 0, 0);
|
|
944
1052
|
}
|
|
945
1053
|
}
|
|
946
|
-
createHasher(() => new
|
|
1054
|
+
createHasher(() => new _RIPEMD160());
|
|
947
1055
|
|
|
948
1056
|
function md5Hash(data) {
|
|
949
1057
|
return md5.create().update(data).digest();
|
|
950
1058
|
}
|
|
951
1059
|
function md5Hex(data) {
|
|
952
|
-
return
|
|
1060
|
+
return bytesToHex(md5Hash(data));
|
|
953
1061
|
}
|
|
954
1062
|
|
|
955
|
-
const U32_MASK64 = BigInt(2 ** 32 - 1);
|
|
956
|
-
const _32n = BigInt(32);
|
|
957
|
-
function fromBig(n, le = false) {
|
|
958
|
-
if (le) return {
|
|
959
|
-
h: Number(n & U32_MASK64),
|
|
960
|
-
l: Number(n >> _32n & U32_MASK64)
|
|
961
|
-
};
|
|
962
|
-
return {
|
|
963
|
-
h: Number(n >> _32n & U32_MASK64) | 0,
|
|
964
|
-
l: Number(n & U32_MASK64) | 0
|
|
965
|
-
};
|
|
966
|
-
}
|
|
967
|
-
function split(lst, le = false) {
|
|
968
|
-
const len = lst.length;
|
|
969
|
-
let Ah = new Uint32Array(len);
|
|
970
|
-
let Al = new Uint32Array(len);
|
|
971
|
-
for (let i = 0; i < len; i++) {
|
|
972
|
-
const {
|
|
973
|
-
h,
|
|
974
|
-
l
|
|
975
|
-
} = fromBig(lst[i], le);
|
|
976
|
-
[Ah[i], Al[i]] = [h, l];
|
|
977
|
-
}
|
|
978
|
-
return [Ah, Al];
|
|
979
|
-
}
|
|
980
|
-
const shrSH = (h, _l, s) => h >>> s;
|
|
981
|
-
const shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
982
|
-
const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
983
|
-
const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
984
|
-
const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
985
|
-
const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
986
|
-
function add(Ah, Al, Bh, Bl) {
|
|
987
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
988
|
-
return {
|
|
989
|
-
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
990
|
-
l: l | 0
|
|
991
|
-
};
|
|
992
|
-
}
|
|
993
|
-
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
994
|
-
const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
995
|
-
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
996
|
-
const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
997
|
-
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
998
|
-
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
999
|
-
|
|
1000
1063
|
const SHA256_K = Uint32Array.from([0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]);
|
|
1001
1064
|
const SHA256_W = new Uint32Array(64);
|
|
1002
|
-
class
|
|
1003
|
-
|
|
1065
|
+
class SHA2_32B extends HashMD {
|
|
1066
|
+
A = 0;
|
|
1067
|
+
B = 0;
|
|
1068
|
+
C = 0;
|
|
1069
|
+
D = 0;
|
|
1070
|
+
E = 0;
|
|
1071
|
+
F = 0;
|
|
1072
|
+
G = 0;
|
|
1073
|
+
H = 0;
|
|
1074
|
+
constructor(outputLen, IV) {
|
|
1004
1075
|
super(64, outputLen, 8, false);
|
|
1005
|
-
this.A =
|
|
1006
|
-
this.B =
|
|
1007
|
-
this.C =
|
|
1008
|
-
this.D =
|
|
1009
|
-
this.E =
|
|
1010
|
-
this.F =
|
|
1011
|
-
this.G =
|
|
1012
|
-
this.H =
|
|
1076
|
+
this.A = IV[0] | 0;
|
|
1077
|
+
this.B = IV[1] | 0;
|
|
1078
|
+
this.C = IV[2] | 0;
|
|
1079
|
+
this.D = IV[3] | 0;
|
|
1080
|
+
this.E = IV[4] | 0;
|
|
1081
|
+
this.F = IV[5] | 0;
|
|
1082
|
+
this.G = IV[6] | 0;
|
|
1083
|
+
this.H = IV[7] | 0;
|
|
1013
1084
|
}
|
|
1014
1085
|
get() {
|
|
1015
1086
|
const {
|
|
@@ -1034,6 +1105,10 @@ class SHA256 extends HashMD {
|
|
|
1034
1105
|
this.G = G | 0;
|
|
1035
1106
|
this.H = H | 0;
|
|
1036
1107
|
}
|
|
1108
|
+
_cloneInto(to) {
|
|
1109
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1110
|
+
return this._cloneIntoMeta(to);
|
|
1111
|
+
}
|
|
1037
1112
|
process(view, offset) {
|
|
1038
1113
|
for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
|
|
1039
1114
|
for (let i = 16; i < 64; i++) {
|
|
@@ -1078,24 +1153,22 @@ class SHA256 extends HashMD {
|
|
|
1078
1153
|
this.set(A, B, C, D, E, F, G, H);
|
|
1079
1154
|
}
|
|
1080
1155
|
roundClean() {
|
|
1081
|
-
clean(SHA256_W);
|
|
1156
|
+
clean$1(SHA256_W);
|
|
1082
1157
|
}
|
|
1083
1158
|
destroy() {
|
|
1159
|
+
this.destroyed = true;
|
|
1084
1160
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1085
|
-
clean(this.buffer);
|
|
1161
|
+
clean$1(this.buffer);
|
|
1086
1162
|
}
|
|
1087
1163
|
}
|
|
1088
|
-
class
|
|
1164
|
+
class _SHA256 extends SHA2_32B {
|
|
1089
1165
|
constructor() {
|
|
1090
|
-
super(
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
this.F = SHA224_IV[5] | 0;
|
|
1097
|
-
this.G = SHA224_IV[6] | 0;
|
|
1098
|
-
this.H = SHA224_IV[7] | 0;
|
|
1166
|
+
super(32, SHA256_IV);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
class _SHA224 extends SHA2_32B {
|
|
1170
|
+
constructor() {
|
|
1171
|
+
super(28, SHA224_IV);
|
|
1099
1172
|
}
|
|
1100
1173
|
}
|
|
1101
1174
|
const K512 = (() => split(['0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc', '0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118', '0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2', '0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694', '0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65', '0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5', '0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4', '0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70', '0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df', '0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b', '0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30', '0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8', '0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8', '0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3', '0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec', '0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b', '0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178', '0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b', '0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c', '0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'].map(n => BigInt(n))))();
|
|
@@ -1103,25 +1176,41 @@ const SHA512_Kh = (() => K512[0])();
|
|
|
1103
1176
|
const SHA512_Kl = (() => K512[1])();
|
|
1104
1177
|
const SHA512_W_H = new Uint32Array(80);
|
|
1105
1178
|
const SHA512_W_L = new Uint32Array(80);
|
|
1106
|
-
class
|
|
1107
|
-
|
|
1179
|
+
class SHA2_64B extends HashMD {
|
|
1180
|
+
Ah = 0;
|
|
1181
|
+
Al = 0;
|
|
1182
|
+
Bh = 0;
|
|
1183
|
+
Bl = 0;
|
|
1184
|
+
Ch = 0;
|
|
1185
|
+
Cl = 0;
|
|
1186
|
+
Dh = 0;
|
|
1187
|
+
Dl = 0;
|
|
1188
|
+
Eh = 0;
|
|
1189
|
+
El = 0;
|
|
1190
|
+
Fh = 0;
|
|
1191
|
+
Fl = 0;
|
|
1192
|
+
Gh = 0;
|
|
1193
|
+
Gl = 0;
|
|
1194
|
+
Hh = 0;
|
|
1195
|
+
Hl = 0;
|
|
1196
|
+
constructor(outputLen, IV) {
|
|
1108
1197
|
super(128, outputLen, 16, false);
|
|
1109
|
-
this.Ah =
|
|
1110
|
-
this.Al =
|
|
1111
|
-
this.Bh =
|
|
1112
|
-
this.Bl =
|
|
1113
|
-
this.Ch =
|
|
1114
|
-
this.Cl =
|
|
1115
|
-
this.Dh =
|
|
1116
|
-
this.Dl =
|
|
1117
|
-
this.Eh =
|
|
1118
|
-
this.El =
|
|
1119
|
-
this.Fh =
|
|
1120
|
-
this.Fl =
|
|
1121
|
-
this.Gh =
|
|
1122
|
-
this.Gl =
|
|
1123
|
-
this.Hh =
|
|
1124
|
-
this.Hl =
|
|
1198
|
+
this.Ah = IV[0] | 0;
|
|
1199
|
+
this.Al = IV[1] | 0;
|
|
1200
|
+
this.Bh = IV[2] | 0;
|
|
1201
|
+
this.Bl = IV[3] | 0;
|
|
1202
|
+
this.Ch = IV[4] | 0;
|
|
1203
|
+
this.Cl = IV[5] | 0;
|
|
1204
|
+
this.Dh = IV[6] | 0;
|
|
1205
|
+
this.Dl = IV[7] | 0;
|
|
1206
|
+
this.Eh = IV[8] | 0;
|
|
1207
|
+
this.El = IV[9] | 0;
|
|
1208
|
+
this.Fh = IV[10] | 0;
|
|
1209
|
+
this.Fl = IV[11] | 0;
|
|
1210
|
+
this.Gh = IV[12] | 0;
|
|
1211
|
+
this.Gl = IV[13] | 0;
|
|
1212
|
+
this.Hh = IV[14] | 0;
|
|
1213
|
+
this.Hl = IV[15] | 0;
|
|
1125
1214
|
}
|
|
1126
1215
|
get() {
|
|
1127
1216
|
const {
|
|
@@ -1162,6 +1251,10 @@ class SHA512 extends HashMD {
|
|
|
1162
1251
|
this.Hh = Hh | 0;
|
|
1163
1252
|
this.Hl = Hl | 0;
|
|
1164
1253
|
}
|
|
1254
|
+
_cloneInto(to) {
|
|
1255
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1256
|
+
return this._cloneIntoMeta(to);
|
|
1257
|
+
}
|
|
1165
1258
|
process(view, offset) {
|
|
1166
1259
|
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1167
1260
|
SHA512_W_H[i] = view.getUint32(offset);
|
|
@@ -1265,97 +1358,1577 @@ class SHA512 extends HashMD {
|
|
|
1265
1358
|
} = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
1266
1359
|
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
1267
1360
|
}
|
|
1268
|
-
roundClean() {
|
|
1269
|
-
clean(SHA512_W_H, SHA512_W_L);
|
|
1361
|
+
roundClean() {
|
|
1362
|
+
clean$1(SHA512_W_H, SHA512_W_L);
|
|
1363
|
+
}
|
|
1364
|
+
destroy() {
|
|
1365
|
+
this.destroyed = true;
|
|
1366
|
+
clean$1(this.buffer);
|
|
1367
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
class _SHA512 extends SHA2_64B {
|
|
1371
|
+
constructor() {
|
|
1372
|
+
super(64, SHA512_IV);
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
class _SHA384 extends SHA2_64B {
|
|
1376
|
+
constructor() {
|
|
1377
|
+
super(48, SHA384_IV);
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
const T224_IV = Uint32Array.from([0x8c3d37c8, 0x19544da2, 0x73e19966, 0x89dcd4d6, 0x1dfab7ae, 0x32ff9c82, 0x679dd514, 0x582f9fcf, 0x0f6d2b69, 0x7bd44da8, 0x77e36f73, 0x04c48942, 0x3f9d85a8, 0x6a1d36c8, 0x1112e6ad, 0x91d692a1]);
|
|
1381
|
+
const T256_IV = Uint32Array.from([0x22312194, 0xfc2bf72c, 0x9f555fa3, 0xc84c64c2, 0x2393b86b, 0x6f53b151, 0x96387719, 0x5940eabd, 0x96283ee2, 0xa88effe3, 0xbe5e1e25, 0x53863992, 0x2b0199fc, 0x2c85b8aa, 0x0eb72ddc, 0x81c52ca2]);
|
|
1382
|
+
class _SHA512_224 extends SHA2_64B {
|
|
1383
|
+
constructor() {
|
|
1384
|
+
super(28, T224_IV);
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
class _SHA512_256 extends SHA2_64B {
|
|
1388
|
+
constructor() {
|
|
1389
|
+
super(32, T256_IV);
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
const sha256 = createHasher(() => new _SHA256(), oidNist(0x01));
|
|
1393
|
+
createHasher(() => new _SHA224(), oidNist(0x04));
|
|
1394
|
+
createHasher(() => new _SHA512(), oidNist(0x03));
|
|
1395
|
+
createHasher(() => new _SHA384(), oidNist(0x02));
|
|
1396
|
+
createHasher(() => new _SHA512_256(), oidNist(0x06));
|
|
1397
|
+
createHasher(() => new _SHA512_224(), oidNist(0x05));
|
|
1398
|
+
|
|
1399
|
+
function sha256Hash(data) {
|
|
1400
|
+
return sha256(data);
|
|
1401
|
+
}
|
|
1402
|
+
|
|
1403
|
+
function isBytes(a) {
|
|
1404
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array' && 'BYTES_PER_ELEMENT' in a && a.BYTES_PER_ELEMENT === 1;
|
|
1405
|
+
}
|
|
1406
|
+
const atitle = title => title ? `"${title}" ` : '';
|
|
1407
|
+
function abool(value, title = '') {
|
|
1408
|
+
if (typeof value !== 'boolean') throw new TypeError(atitle(title) + 'expected boolean, got type=' + typeof value);
|
|
1409
|
+
return value;
|
|
1410
|
+
}
|
|
1411
|
+
function anumber(n, title = '') {
|
|
1412
|
+
if (typeof n !== 'number') throw new TypeError(atitle(title) + 'expected number, got ' + typeof n);
|
|
1413
|
+
if (!Number.isSafeInteger(n) || n < 0) throw new RangeError(atitle(title) + 'expected integer >= 0, got ' + n);
|
|
1414
|
+
return n;
|
|
1415
|
+
}
|
|
1416
|
+
function abytes(value, length, title = '') {
|
|
1417
|
+
if (isBytes(value) && (length === undefined || value.length === length)) return value;
|
|
1418
|
+
if (length !== undefined) anumber(length, 'length');
|
|
1419
|
+
const bytes = isBytes(value);
|
|
1420
|
+
const ofLen = length !== undefined ? ` of length ${length}` : '';
|
|
1421
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
1422
|
+
const message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
1423
|
+
if (!bytes) throw new TypeError(message);
|
|
1424
|
+
throw new RangeError(message);
|
|
1425
|
+
}
|
|
1426
|
+
function aexists(instance, checkFinished = true) {
|
|
1427
|
+
if (instance.destroyed) throw new Error('hash was destroyed');
|
|
1428
|
+
if (checkFinished && instance.finished) throw new Error('digest() was already called');
|
|
1429
|
+
}
|
|
1430
|
+
function aoutput(out, instance) {
|
|
1431
|
+
abytes(out, undefined, 'output');
|
|
1432
|
+
const min = instance.outputLen;
|
|
1433
|
+
if (!(out.length >= min)) {
|
|
1434
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
function aoutput32(out, instance) {
|
|
1438
|
+
aoutput(out, instance);
|
|
1439
|
+
if (!isAligned32(out)) throw new Error('invalid output, must be aligned');
|
|
1440
|
+
}
|
|
1441
|
+
function u8(arr) {
|
|
1442
|
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1443
|
+
}
|
|
1444
|
+
function u32(arr) {
|
|
1445
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
1446
|
+
}
|
|
1447
|
+
function clean(...arrays) {
|
|
1448
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
1449
|
+
arrays[i].fill(0);
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
function createView(arr) {
|
|
1453
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1454
|
+
}
|
|
1455
|
+
const isLE = (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
1456
|
+
function byteSwap(word) {
|
|
1457
|
+
return word << 24 & 0xff000000 | word << 8 & 0xff0000 | word >>> 8 & 0xff00 | word >>> 24 & 0xff;
|
|
1458
|
+
}
|
|
1459
|
+
const swap8IfBE = isLE ? n => n : n => byteSwap(n) >>> 0;
|
|
1460
|
+
function byteSwap32(arr) {
|
|
1461
|
+
for (let i = 0; i < arr.length; i++) {
|
|
1462
|
+
arr[i] = byteSwap(arr[i]);
|
|
1463
|
+
}
|
|
1464
|
+
return arr;
|
|
1465
|
+
}
|
|
1466
|
+
const swap32IfBE = isLE ? u => u : byteSwap32;
|
|
1467
|
+
(() => typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
1468
|
+
Array.from({
|
|
1469
|
+
length: 256
|
|
1470
|
+
}, (_, i) => i.toString(16).padStart(2, '0'));
|
|
1471
|
+
BigInt(0);
|
|
1472
|
+
function overlapBytes(a, b) {
|
|
1473
|
+
if (!a.byteLength || !b.byteLength) return false;
|
|
1474
|
+
return a.buffer === b.buffer && a.byteOffset < b.byteOffset + b.byteLength && b.byteOffset < a.byteOffset + a.byteLength;
|
|
1475
|
+
}
|
|
1476
|
+
function complexOverlapBytes(input, output) {
|
|
1477
|
+
if (overlapBytes(input, output) && input.byteOffset < output.byteOffset) throw new Error('complex overlap of input and output is not supported');
|
|
1478
|
+
}
|
|
1479
|
+
function concatBytes(...arrays) {
|
|
1480
|
+
let sum = 0;
|
|
1481
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
1482
|
+
const a = arrays[i];
|
|
1483
|
+
abytes(a);
|
|
1484
|
+
sum += a.length;
|
|
1485
|
+
}
|
|
1486
|
+
const res = new Uint8Array(sum);
|
|
1487
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
1488
|
+
const a = arrays[i];
|
|
1489
|
+
res.set(a, pad);
|
|
1490
|
+
pad += a.length;
|
|
1491
|
+
}
|
|
1492
|
+
return res;
|
|
1493
|
+
}
|
|
1494
|
+
function equalBytes(a, b) {
|
|
1495
|
+
a = abytes(a);
|
|
1496
|
+
b = abytes(b);
|
|
1497
|
+
if (a.length !== b.length) return false;
|
|
1498
|
+
let diff = 0;
|
|
1499
|
+
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
1500
|
+
return diff === 0;
|
|
1501
|
+
}
|
|
1502
|
+
function wrapMacConstructor(keyLen, macCons, fromMsg) {
|
|
1503
|
+
const mac = macCons;
|
|
1504
|
+
const getArgs = fromMsg || (() => []);
|
|
1505
|
+
const macC = (msg, key) => mac(key, ...getArgs(msg)).update(msg).digest();
|
|
1506
|
+
const tmp = mac(new Uint8Array(keyLen), ...getArgs(new Uint8Array(0)));
|
|
1507
|
+
macC.outputLen = tmp.outputLen;
|
|
1508
|
+
macC.blockLen = tmp.blockLen;
|
|
1509
|
+
macC.create = (key, ...args) => mac(key, ...args);
|
|
1510
|
+
return macC;
|
|
1511
|
+
}
|
|
1512
|
+
const wrapCipher = (params, constructor) => {
|
|
1513
|
+
function wrappedCipher(key, ...args) {
|
|
1514
|
+
abytes(key, undefined, 'key');
|
|
1515
|
+
if (params.nonceLength !== undefined) {
|
|
1516
|
+
const nonce = args[0];
|
|
1517
|
+
abytes(nonce, params.varSizeNonce ? undefined : params.nonceLength, 'nonce');
|
|
1518
|
+
}
|
|
1519
|
+
const tagl = params.tagLength;
|
|
1520
|
+
const aadStart = params.nonceLength !== undefined ? 1 : 0;
|
|
1521
|
+
if (!params.withAAD) {
|
|
1522
|
+
for (let i = aadStart; i < args.length; i++) if (isBytes(args[i])) throw new Error('AAD not supported');
|
|
1523
|
+
}
|
|
1524
|
+
if (params.withAAD && args[aadStart] !== undefined) abytes(args[aadStart], undefined, 'AAD');
|
|
1525
|
+
const cipher = constructor(key, ...args);
|
|
1526
|
+
const checkOutput = (fnLength, output) => {
|
|
1527
|
+
if (output !== undefined) {
|
|
1528
|
+
if (fnLength !== 2) throw new Error('cipher output not supported');
|
|
1529
|
+
abytes(output, undefined, 'output');
|
|
1530
|
+
}
|
|
1531
|
+
};
|
|
1532
|
+
let called = false;
|
|
1533
|
+
const wrCipher = {
|
|
1534
|
+
encrypt(data, output) {
|
|
1535
|
+
if (called) throw new Error('cannot encrypt() twice with same key + nonce');
|
|
1536
|
+
called = true;
|
|
1537
|
+
abytes(data, undefined, 'data');
|
|
1538
|
+
checkOutput(cipher.encrypt.length, output);
|
|
1539
|
+
return cipher.encrypt(data, output);
|
|
1540
|
+
},
|
|
1541
|
+
decrypt(data, output) {
|
|
1542
|
+
abytes(data, undefined, 'data');
|
|
1543
|
+
if (tagl && data.length < tagl) throw new Error('"ciphertext" expected length >= tagLength=' + tagl);
|
|
1544
|
+
checkOutput(cipher.decrypt.length, output);
|
|
1545
|
+
return cipher.decrypt(data, output);
|
|
1546
|
+
}
|
|
1547
|
+
};
|
|
1548
|
+
return wrCipher;
|
|
1549
|
+
}
|
|
1550
|
+
Object.assign(wrappedCipher, params);
|
|
1551
|
+
return wrappedCipher;
|
|
1552
|
+
};
|
|
1553
|
+
function getOutput(expectedLength, out, onlyAligned = true) {
|
|
1554
|
+
if (out === undefined) return new Uint8Array(expectedLength);
|
|
1555
|
+
abytes(out, expectedLength, 'output');
|
|
1556
|
+
if (onlyAligned && !isAligned32(out)) throw new Error('invalid output, must be aligned');
|
|
1557
|
+
return out;
|
|
1558
|
+
}
|
|
1559
|
+
function u64Lengths(dataLength, aadLength, isLE) {
|
|
1560
|
+
anumber(dataLength);
|
|
1561
|
+
anumber(aadLength);
|
|
1562
|
+
abool(isLE);
|
|
1563
|
+
const num = new Uint8Array(16);
|
|
1564
|
+
const view = createView(num);
|
|
1565
|
+
view.setBigUint64(0, BigInt(aadLength), isLE);
|
|
1566
|
+
view.setBigUint64(8, BigInt(dataLength), isLE);
|
|
1567
|
+
return num;
|
|
1568
|
+
}
|
|
1569
|
+
function isAligned32(bytes) {
|
|
1570
|
+
return bytes.byteOffset % 4 === 0;
|
|
1571
|
+
}
|
|
1572
|
+
function copyBytes(bytes) {
|
|
1573
|
+
return Uint8Array.from(abytes(bytes));
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
const BLOCK_SIZE$1 = 16;
|
|
1577
|
+
const ZEROS16 = new Uint8Array(16);
|
|
1578
|
+
const ZEROS32 = u32(ZEROS16);
|
|
1579
|
+
const POLY$1 = 0xe1;
|
|
1580
|
+
const mul2$1 = (s0, s1, s2, s3) => {
|
|
1581
|
+
const hiBit = s3 & 1;
|
|
1582
|
+
return {
|
|
1583
|
+
s3: s2 << 31 | s3 >>> 1,
|
|
1584
|
+
s2: s1 << 31 | s2 >>> 1,
|
|
1585
|
+
s1: s0 << 31 | s1 >>> 1,
|
|
1586
|
+
s0: s0 >>> 1 ^ POLY$1 << 24 & -(hiBit & 1)
|
|
1587
|
+
};
|
|
1588
|
+
};
|
|
1589
|
+
const swapLE = n => (n >>> 0 & 0xff) << 24 | (n >>> 8 & 0xff) << 16 | (n >>> 16 & 0xff) << 8 | n >>> 24 & 0xff | 0;
|
|
1590
|
+
const swap8IfLE = n => swap8IfBE(swapLE(n));
|
|
1591
|
+
function _toGHASHKey(k) {
|
|
1592
|
+
k.reverse();
|
|
1593
|
+
const hiBit = k[15] & 1;
|
|
1594
|
+
let carry = 0;
|
|
1595
|
+
for (let i = 0; i < k.length; i++) {
|
|
1596
|
+
const t = k[i];
|
|
1597
|
+
k[i] = t >>> 1 | carry;
|
|
1598
|
+
carry = (t & 1) << 7;
|
|
1599
|
+
}
|
|
1600
|
+
k[0] ^= -hiBit & 0xe1;
|
|
1601
|
+
return k;
|
|
1602
|
+
}
|
|
1603
|
+
const estimateWindow = bytes => {
|
|
1604
|
+
if (bytes > 64 * 1024) return 8;
|
|
1605
|
+
if (bytes > 1024) return 4;
|
|
1606
|
+
return 2;
|
|
1607
|
+
};
|
|
1608
|
+
class GHASH {
|
|
1609
|
+
blockLen = BLOCK_SIZE$1;
|
|
1610
|
+
outputLen = BLOCK_SIZE$1;
|
|
1611
|
+
s0 = 0;
|
|
1612
|
+
s1 = 0;
|
|
1613
|
+
s2 = 0;
|
|
1614
|
+
s3 = 0;
|
|
1615
|
+
finished = false;
|
|
1616
|
+
destroyed = false;
|
|
1617
|
+
t;
|
|
1618
|
+
W;
|
|
1619
|
+
windowSize;
|
|
1620
|
+
constructor(key, expectedLength) {
|
|
1621
|
+
abytes(key, 16, 'key');
|
|
1622
|
+
key = copyBytes(key);
|
|
1623
|
+
const kView = createView(key);
|
|
1624
|
+
let k0 = kView.getUint32(0, false);
|
|
1625
|
+
let k1 = kView.getUint32(4, false);
|
|
1626
|
+
let k2 = kView.getUint32(8, false);
|
|
1627
|
+
let k3 = kView.getUint32(12, false);
|
|
1628
|
+
const doubles = [];
|
|
1629
|
+
for (let i = 0; i < 128; i++) {
|
|
1630
|
+
doubles.push({
|
|
1631
|
+
s0: swapLE(k0),
|
|
1632
|
+
s1: swapLE(k1),
|
|
1633
|
+
s2: swapLE(k2),
|
|
1634
|
+
s3: swapLE(k3)
|
|
1635
|
+
});
|
|
1636
|
+
({
|
|
1637
|
+
s0: k0,
|
|
1638
|
+
s1: k1,
|
|
1639
|
+
s2: k2,
|
|
1640
|
+
s3: k3
|
|
1641
|
+
} = mul2$1(k0, k1, k2, k3));
|
|
1642
|
+
}
|
|
1643
|
+
const W = estimateWindow(expectedLength || 1024);
|
|
1644
|
+
if (![1, 2, 4, 8].includes(W)) throw new Error('ghash: invalid window size, expected 2, 4 or 8');
|
|
1645
|
+
this.W = W;
|
|
1646
|
+
const bits = 128;
|
|
1647
|
+
const windows = bits / W;
|
|
1648
|
+
const windowSize = this.windowSize = 2 ** W;
|
|
1649
|
+
const items = [];
|
|
1650
|
+
for (let w = 0; w < windows; w++) {
|
|
1651
|
+
for (let byte = 0; byte < windowSize; byte++) {
|
|
1652
|
+
let s0 = 0,
|
|
1653
|
+
s1 = 0,
|
|
1654
|
+
s2 = 0,
|
|
1655
|
+
s3 = 0;
|
|
1656
|
+
for (let j = 0; j < W; j++) {
|
|
1657
|
+
const bit = byte >>> W - j - 1 & 1;
|
|
1658
|
+
if (!bit) continue;
|
|
1659
|
+
const {
|
|
1660
|
+
s0: d0,
|
|
1661
|
+
s1: d1,
|
|
1662
|
+
s2: d2,
|
|
1663
|
+
s3: d3
|
|
1664
|
+
} = doubles[W * w + j];
|
|
1665
|
+
s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
|
|
1666
|
+
}
|
|
1667
|
+
items.push({
|
|
1668
|
+
s0,
|
|
1669
|
+
s1,
|
|
1670
|
+
s2,
|
|
1671
|
+
s3
|
|
1672
|
+
});
|
|
1673
|
+
}
|
|
1674
|
+
}
|
|
1675
|
+
this.t = items;
|
|
1676
|
+
}
|
|
1677
|
+
_updateBlock(s0, s1, s2, s3) {
|
|
1678
|
+
s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
|
|
1679
|
+
const {
|
|
1680
|
+
W,
|
|
1681
|
+
t,
|
|
1682
|
+
windowSize
|
|
1683
|
+
} = this;
|
|
1684
|
+
let o0 = 0,
|
|
1685
|
+
o1 = 0,
|
|
1686
|
+
o2 = 0,
|
|
1687
|
+
o3 = 0;
|
|
1688
|
+
const mask = (1 << W) - 1;
|
|
1689
|
+
let w = 0;
|
|
1690
|
+
for (const num of [s0, s1, s2, s3]) {
|
|
1691
|
+
for (let bytePos = 0; bytePos < 4; bytePos++) {
|
|
1692
|
+
const byte = num >>> 8 * bytePos & 0xff;
|
|
1693
|
+
for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
|
|
1694
|
+
const bit = byte >>> W * bitPos & mask;
|
|
1695
|
+
const {
|
|
1696
|
+
s0: e0,
|
|
1697
|
+
s1: e1,
|
|
1698
|
+
s2: e2,
|
|
1699
|
+
s3: e3
|
|
1700
|
+
} = t[w * windowSize + bit];
|
|
1701
|
+
o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
|
|
1702
|
+
w += 1;
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
this.s0 = o0;
|
|
1707
|
+
this.s1 = o1;
|
|
1708
|
+
this.s2 = o2;
|
|
1709
|
+
this.s3 = o3;
|
|
1710
|
+
}
|
|
1711
|
+
update(data) {
|
|
1712
|
+
aexists(this);
|
|
1713
|
+
abytes(data);
|
|
1714
|
+
data = copyBytes(data);
|
|
1715
|
+
const b32 = u32(data);
|
|
1716
|
+
const blocks = Math.floor(data.length / BLOCK_SIZE$1);
|
|
1717
|
+
const left = data.length % BLOCK_SIZE$1;
|
|
1718
|
+
for (let i = 0; i < blocks; i++) {
|
|
1719
|
+
this._updateBlock(swap8IfBE(b32[i * 4 + 0]), swap8IfBE(b32[i * 4 + 1]), swap8IfBE(b32[i * 4 + 2]), swap8IfBE(b32[i * 4 + 3]));
|
|
1720
|
+
}
|
|
1721
|
+
if (left) {
|
|
1722
|
+
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
|
|
1723
|
+
this._updateBlock(swap8IfBE(ZEROS32[0]), swap8IfBE(ZEROS32[1]), swap8IfBE(ZEROS32[2]), swap8IfBE(ZEROS32[3]));
|
|
1724
|
+
clean(ZEROS32);
|
|
1725
|
+
}
|
|
1726
|
+
return this;
|
|
1727
|
+
}
|
|
1728
|
+
destroy() {
|
|
1729
|
+
this.destroyed = true;
|
|
1730
|
+
const {
|
|
1731
|
+
t
|
|
1732
|
+
} = this;
|
|
1733
|
+
for (const elm of t) {
|
|
1734
|
+
elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
digestInto(out) {
|
|
1738
|
+
aexists(this);
|
|
1739
|
+
aoutput32(out, this);
|
|
1740
|
+
this.finished = true;
|
|
1741
|
+
const {
|
|
1742
|
+
s0,
|
|
1743
|
+
s1,
|
|
1744
|
+
s2,
|
|
1745
|
+
s3
|
|
1746
|
+
} = this;
|
|
1747
|
+
const o32 = u32(out);
|
|
1748
|
+
o32[0] = s0;
|
|
1749
|
+
o32[1] = s1;
|
|
1750
|
+
o32[2] = s2;
|
|
1751
|
+
o32[3] = s3;
|
|
1752
|
+
if (!isLE) swap32IfBE(o32.subarray(0, BLOCK_SIZE$1 / 4));
|
|
1753
|
+
}
|
|
1754
|
+
digest() {
|
|
1755
|
+
const res = new Uint8Array(BLOCK_SIZE$1);
|
|
1756
|
+
this.digestInto(res);
|
|
1757
|
+
this.destroy();
|
|
1758
|
+
return res;
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
class Polyval extends GHASH {
|
|
1762
|
+
constructor(key, expectedLength) {
|
|
1763
|
+
abytes(key);
|
|
1764
|
+
const ghKey = _toGHASHKey(copyBytes(key));
|
|
1765
|
+
super(ghKey, expectedLength);
|
|
1766
|
+
clean(ghKey);
|
|
1767
|
+
}
|
|
1768
|
+
update(data) {
|
|
1769
|
+
aexists(this);
|
|
1770
|
+
abytes(data);
|
|
1771
|
+
data = copyBytes(data);
|
|
1772
|
+
const b32 = u32(data);
|
|
1773
|
+
const left = data.length % BLOCK_SIZE$1;
|
|
1774
|
+
const blocks = Math.floor(data.length / BLOCK_SIZE$1);
|
|
1775
|
+
for (let i = 0; i < blocks; i++) {
|
|
1776
|
+
this._updateBlock(swap8IfLE(b32[i * 4 + 3]), swap8IfLE(b32[i * 4 + 2]), swap8IfLE(b32[i * 4 + 1]), swap8IfLE(b32[i * 4 + 0]));
|
|
1777
|
+
}
|
|
1778
|
+
if (left) {
|
|
1779
|
+
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
|
|
1780
|
+
this._updateBlock(swap8IfLE(ZEROS32[3]), swap8IfLE(ZEROS32[2]), swap8IfLE(ZEROS32[1]), swap8IfLE(ZEROS32[0]));
|
|
1781
|
+
clean(ZEROS32);
|
|
1782
|
+
}
|
|
1783
|
+
return this;
|
|
1784
|
+
}
|
|
1785
|
+
digestInto(out) {
|
|
1786
|
+
aexists(this);
|
|
1787
|
+
aoutput32(out, this);
|
|
1788
|
+
this.finished = true;
|
|
1789
|
+
const view = out.subarray(0, this.outputLen);
|
|
1790
|
+
const {
|
|
1791
|
+
s0,
|
|
1792
|
+
s1,
|
|
1793
|
+
s2,
|
|
1794
|
+
s3
|
|
1795
|
+
} = this;
|
|
1796
|
+
const o32 = u32(view);
|
|
1797
|
+
o32[0] = s0;
|
|
1798
|
+
o32[1] = s1;
|
|
1799
|
+
o32[2] = s2;
|
|
1800
|
+
o32[3] = s3;
|
|
1801
|
+
swap32IfBE(o32);
|
|
1802
|
+
view.reverse();
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
const ghash = wrapMacConstructor(16, (key, expectedLength) => new GHASH(key, expectedLength), msg => [msg.length]);
|
|
1806
|
+
const polyval = wrapMacConstructor(16, (key, expectedLength) => new Polyval(key, expectedLength), msg => [msg.length]);
|
|
1807
|
+
|
|
1808
|
+
const BLOCK_SIZE = 16;
|
|
1809
|
+
const BLOCK_SIZE32 = 4;
|
|
1810
|
+
const EMPTY_BLOCK = new Uint8Array(BLOCK_SIZE);
|
|
1811
|
+
const ONE_BLOCK = Uint8Array.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]);
|
|
1812
|
+
const POLY = 0x11b;
|
|
1813
|
+
function validateKeyLength(key) {
|
|
1814
|
+
if (![16, 24, 32].includes(key.length)) throw new Error('"aes key" expected Uint8Array of length 16/24/32, got length=' + key.length);
|
|
1815
|
+
}
|
|
1816
|
+
function mul2(n) {
|
|
1817
|
+
return n << 1 ^ POLY & -(n >> 7);
|
|
1818
|
+
}
|
|
1819
|
+
function mul(a, b) {
|
|
1820
|
+
let res = 0;
|
|
1821
|
+
for (; b > 0; b >>= 1) {
|
|
1822
|
+
res ^= a & -(b & 1);
|
|
1823
|
+
a = mul2(a);
|
|
1824
|
+
}
|
|
1825
|
+
return res;
|
|
1826
|
+
}
|
|
1827
|
+
const sbox = (() => {
|
|
1828
|
+
const t = new Uint8Array(256);
|
|
1829
|
+
for (let i = 0, x = 1; i < 256; i++, x ^= mul2(x)) t[i] = x;
|
|
1830
|
+
const box = new Uint8Array(256);
|
|
1831
|
+
box[0] = 0x63;
|
|
1832
|
+
for (let i = 0; i < 255; i++) {
|
|
1833
|
+
let x = t[255 - i];
|
|
1834
|
+
x |= x << 8;
|
|
1835
|
+
box[t[i]] = (x ^ x >> 4 ^ x >> 5 ^ x >> 6 ^ x >> 7 ^ 0x63) & 0xff;
|
|
1836
|
+
}
|
|
1837
|
+
clean(t);
|
|
1838
|
+
return box;
|
|
1839
|
+
})();
|
|
1840
|
+
const invSbox = sbox.map((_, j) => sbox.indexOf(j));
|
|
1841
|
+
const rotr32_8 = n => n << 24 | n >>> 8;
|
|
1842
|
+
const rotl32_8 = n => n << 8 | n >>> 24;
|
|
1843
|
+
function genTtable(sbox, fn) {
|
|
1844
|
+
if (sbox.length !== 256) throw new Error('wrong sbox length');
|
|
1845
|
+
const T0 = new Uint32Array(256).map((_, j) => fn(sbox[j]));
|
|
1846
|
+
const T1 = T0.map(rotl32_8);
|
|
1847
|
+
const T2 = T1.map(rotl32_8);
|
|
1848
|
+
const T3 = T2.map(rotl32_8);
|
|
1849
|
+
const T01 = new Uint32Array(256 * 256);
|
|
1850
|
+
const T23 = new Uint32Array(256 * 256);
|
|
1851
|
+
const sbox2 = new Uint16Array(256 * 256);
|
|
1852
|
+
for (let i = 0; i < 256; i++) {
|
|
1853
|
+
for (let j = 0; j < 256; j++) {
|
|
1854
|
+
const idx = i * 256 + j;
|
|
1855
|
+
T01[idx] = T0[i] ^ T1[j];
|
|
1856
|
+
T23[idx] = T2[i] ^ T3[j];
|
|
1857
|
+
sbox2[idx] = sbox[i] << 8 | sbox[j];
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
return {
|
|
1861
|
+
sbox,
|
|
1862
|
+
sbox2,
|
|
1863
|
+
T0,
|
|
1864
|
+
T1,
|
|
1865
|
+
T2,
|
|
1866
|
+
T3,
|
|
1867
|
+
T01,
|
|
1868
|
+
T23
|
|
1869
|
+
};
|
|
1870
|
+
}
|
|
1871
|
+
const tableEncoding = genTtable(sbox, s => mul(s, 3) << 24 | s << 16 | s << 8 | mul(s, 2));
|
|
1872
|
+
const tableDecoding = genTtable(invSbox, s => mul(s, 11) << 24 | mul(s, 13) << 16 | mul(s, 9) << 8 | mul(s, 14));
|
|
1873
|
+
const xPowers = (() => {
|
|
1874
|
+
const p = new Uint8Array(16);
|
|
1875
|
+
for (let i = 0, x = 1; i < 16; i++, x = mul2(x)) p[i] = x;
|
|
1876
|
+
return p;
|
|
1877
|
+
})();
|
|
1878
|
+
function expandKeyLE(key) {
|
|
1879
|
+
abytes(key);
|
|
1880
|
+
const len = key.length;
|
|
1881
|
+
validateKeyLength(key);
|
|
1882
|
+
const {
|
|
1883
|
+
sbox2
|
|
1884
|
+
} = tableEncoding;
|
|
1885
|
+
const toClean = [];
|
|
1886
|
+
if (!isLE || !isAligned32(key)) toClean.push(key = copyBytes(key));
|
|
1887
|
+
const k32 = swap32IfBE(u32(key));
|
|
1888
|
+
const Nk = k32.length;
|
|
1889
|
+
const subByte = n => applySbox(sbox2, n, n, n, n);
|
|
1890
|
+
const xk = new Uint32Array(len + 28);
|
|
1891
|
+
xk.set(k32);
|
|
1892
|
+
for (let i = Nk; i < xk.length; i++) {
|
|
1893
|
+
let t = xk[i - 1];
|
|
1894
|
+
if (i % Nk === 0) t = subByte(rotr32_8(t)) ^ xPowers[i / Nk - 1];else if (Nk > 6 && i % Nk === 4) t = subByte(t);
|
|
1895
|
+
xk[i] = xk[i - Nk] ^ t;
|
|
1896
|
+
}
|
|
1897
|
+
clean(...toClean);
|
|
1898
|
+
return xk;
|
|
1899
|
+
}
|
|
1900
|
+
function expandKeyDecLE(key) {
|
|
1901
|
+
const encKey = expandKeyLE(key);
|
|
1902
|
+
const xk = encKey.slice();
|
|
1903
|
+
const Nk = encKey.length;
|
|
1904
|
+
const {
|
|
1905
|
+
sbox2
|
|
1906
|
+
} = tableEncoding;
|
|
1907
|
+
const {
|
|
1908
|
+
T0,
|
|
1909
|
+
T1,
|
|
1910
|
+
T2,
|
|
1911
|
+
T3
|
|
1912
|
+
} = tableDecoding;
|
|
1913
|
+
for (let i = 0; i < Nk; i += 4) {
|
|
1914
|
+
for (let j = 0; j < 4; j++) xk[i + j] = encKey[Nk - i - 4 + j];
|
|
1915
|
+
}
|
|
1916
|
+
clean(encKey);
|
|
1917
|
+
for (let i = 4; i < Nk - 4; i++) {
|
|
1918
|
+
const x = xk[i];
|
|
1919
|
+
const w = applySbox(sbox2, x, x, x, x);
|
|
1920
|
+
xk[i] = T0[w & 0xff] ^ T1[w >>> 8 & 0xff] ^ T2[w >>> 16 & 0xff] ^ T3[w >>> 24];
|
|
1921
|
+
}
|
|
1922
|
+
return xk;
|
|
1923
|
+
}
|
|
1924
|
+
function apply0123(T01, T23, s0, s1, s2, s3) {
|
|
1925
|
+
return T01[s0 << 8 & 0xff00 | s1 >>> 8 & 0xff] ^ T23[s2 >>> 8 & 0xff00 | s3 >>> 24 & 0xff];
|
|
1926
|
+
}
|
|
1927
|
+
function applySbox(sbox2, s0, s1, s2, s3) {
|
|
1928
|
+
return sbox2[s0 & 0xff | s1 & 0xff00] | sbox2[s2 >>> 16 & 0xff | s3 >>> 16 & 0xff00] << 16;
|
|
1929
|
+
}
|
|
1930
|
+
function encrypt(xk, s0, s1, s2, s3) {
|
|
1931
|
+
const {
|
|
1932
|
+
sbox2,
|
|
1933
|
+
T01,
|
|
1934
|
+
T23
|
|
1935
|
+
} = tableEncoding;
|
|
1936
|
+
let k = 0;
|
|
1937
|
+
s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
|
|
1938
|
+
const rounds = xk.length / 4 - 2;
|
|
1939
|
+
for (let i = 0; i < rounds; i++) {
|
|
1940
|
+
const t0 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
|
|
1941
|
+
const t1 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
|
|
1942
|
+
const t2 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
|
|
1943
|
+
const t3 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
|
|
1944
|
+
s0 = t0, s1 = t1, s2 = t2, s3 = t3;
|
|
1945
|
+
}
|
|
1946
|
+
const t0 = xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3);
|
|
1947
|
+
const t1 = xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0);
|
|
1948
|
+
const t2 = xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1);
|
|
1949
|
+
const t3 = xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2);
|
|
1950
|
+
return {
|
|
1951
|
+
s0: t0,
|
|
1952
|
+
s1: t1,
|
|
1953
|
+
s2: t2,
|
|
1954
|
+
s3: t3
|
|
1955
|
+
};
|
|
1956
|
+
}
|
|
1957
|
+
function decrypt(xk, s0, s1, s2, s3) {
|
|
1958
|
+
const {
|
|
1959
|
+
sbox2,
|
|
1960
|
+
T01,
|
|
1961
|
+
T23
|
|
1962
|
+
} = tableDecoding;
|
|
1963
|
+
let k = 0;
|
|
1964
|
+
s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
|
|
1965
|
+
const rounds = xk.length / 4 - 2;
|
|
1966
|
+
for (let i = 0; i < rounds; i++) {
|
|
1967
|
+
const t0 = xk[k++] ^ apply0123(T01, T23, s0, s3, s2, s1);
|
|
1968
|
+
const t1 = xk[k++] ^ apply0123(T01, T23, s1, s0, s3, s2);
|
|
1969
|
+
const t2 = xk[k++] ^ apply0123(T01, T23, s2, s1, s0, s3);
|
|
1970
|
+
const t3 = xk[k++] ^ apply0123(T01, T23, s3, s2, s1, s0);
|
|
1971
|
+
s0 = t0, s1 = t1, s2 = t2, s3 = t3;
|
|
1972
|
+
}
|
|
1973
|
+
const t0 = xk[k++] ^ applySbox(sbox2, s0, s3, s2, s1);
|
|
1974
|
+
const t1 = xk[k++] ^ applySbox(sbox2, s1, s0, s3, s2);
|
|
1975
|
+
const t2 = xk[k++] ^ applySbox(sbox2, s2, s1, s0, s3);
|
|
1976
|
+
const t3 = xk[k++] ^ applySbox(sbox2, s3, s2, s1, s0);
|
|
1977
|
+
return {
|
|
1978
|
+
s0: t0,
|
|
1979
|
+
s1: t1,
|
|
1980
|
+
s2: t2,
|
|
1981
|
+
s3: t3
|
|
1982
|
+
};
|
|
1983
|
+
}
|
|
1984
|
+
function ctrCounter(xk, nonce, src, dst) {
|
|
1985
|
+
abytes(nonce, BLOCK_SIZE, 'nonce');
|
|
1986
|
+
abytes(src);
|
|
1987
|
+
const srcLen = src.length;
|
|
1988
|
+
dst = getOutput(srcLen, dst);
|
|
1989
|
+
complexOverlapBytes(src, dst);
|
|
1990
|
+
const ctr = nonce;
|
|
1991
|
+
const c32 = u32(ctr);
|
|
1992
|
+
const src32 = u32(src);
|
|
1993
|
+
const dst32 = u32(dst);
|
|
1994
|
+
for (let i = 0; i + 4 <= src32.length; i += 4) {
|
|
1995
|
+
const {
|
|
1996
|
+
s0,
|
|
1997
|
+
s1,
|
|
1998
|
+
s2,
|
|
1999
|
+
s3
|
|
2000
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2001
|
+
dst32[i + 0] = src32[i + 0] ^ swap8IfBE(s0);
|
|
2002
|
+
dst32[i + 1] = src32[i + 1] ^ swap8IfBE(s1);
|
|
2003
|
+
dst32[i + 2] = src32[i + 2] ^ swap8IfBE(s2);
|
|
2004
|
+
dst32[i + 3] = src32[i + 3] ^ swap8IfBE(s3);
|
|
2005
|
+
for (let j = BLOCK_SIZE - 1, carry = 1; j >= 0; j--) {
|
|
2006
|
+
carry = carry + ctr[j] | 0;
|
|
2007
|
+
ctr[j] = carry & 0xff;
|
|
2008
|
+
carry >>>= 8;
|
|
2009
|
+
}
|
|
2010
|
+
}
|
|
2011
|
+
const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
|
|
2012
|
+
if (start < srcLen) {
|
|
2013
|
+
const {
|
|
2014
|
+
s0,
|
|
2015
|
+
s1,
|
|
2016
|
+
s2,
|
|
2017
|
+
s3
|
|
2018
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2019
|
+
const b32 = new Uint32Array([s0, s1, s2, s3]);
|
|
2020
|
+
swap32IfBE(b32);
|
|
2021
|
+
const buf = u8(b32);
|
|
2022
|
+
for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
|
|
2023
|
+
clean(b32);
|
|
2024
|
+
}
|
|
2025
|
+
return dst;
|
|
2026
|
+
}
|
|
2027
|
+
function ctr32(xk, isLE, nonce, src, dst) {
|
|
2028
|
+
abytes(nonce, BLOCK_SIZE, 'nonce');
|
|
2029
|
+
abytes(src);
|
|
2030
|
+
dst = getOutput(src.length, dst);
|
|
2031
|
+
const ctr = nonce;
|
|
2032
|
+
const c32 = u32(ctr);
|
|
2033
|
+
const view = createView(ctr);
|
|
2034
|
+
const src32 = u32(src);
|
|
2035
|
+
const dst32 = u32(dst);
|
|
2036
|
+
const ctrPos = isLE ? 0 : 12;
|
|
2037
|
+
const srcLen = src.length;
|
|
2038
|
+
let ctrNum = view.getUint32(ctrPos, isLE);
|
|
2039
|
+
for (let i = 0; i + 4 <= src32.length; i += 4) {
|
|
2040
|
+
const {
|
|
2041
|
+
s0,
|
|
2042
|
+
s1,
|
|
2043
|
+
s2,
|
|
2044
|
+
s3
|
|
2045
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2046
|
+
dst32[i + 0] = src32[i + 0] ^ swap8IfBE(s0);
|
|
2047
|
+
dst32[i + 1] = src32[i + 1] ^ swap8IfBE(s1);
|
|
2048
|
+
dst32[i + 2] = src32[i + 2] ^ swap8IfBE(s2);
|
|
2049
|
+
dst32[i + 3] = src32[i + 3] ^ swap8IfBE(s3);
|
|
2050
|
+
ctrNum = ctrNum + 1 >>> 0;
|
|
2051
|
+
view.setUint32(ctrPos, ctrNum, isLE);
|
|
2052
|
+
}
|
|
2053
|
+
const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
|
|
2054
|
+
if (start < srcLen) {
|
|
2055
|
+
const {
|
|
2056
|
+
s0,
|
|
2057
|
+
s1,
|
|
2058
|
+
s2,
|
|
2059
|
+
s3
|
|
2060
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2061
|
+
const b32 = new Uint32Array([s0, s1, s2, s3]);
|
|
2062
|
+
swap32IfBE(b32);
|
|
2063
|
+
const buf = u8(b32);
|
|
2064
|
+
for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
|
|
2065
|
+
clean(b32);
|
|
2066
|
+
}
|
|
2067
|
+
return dst;
|
|
2068
|
+
}
|
|
2069
|
+
const ctr = wrapCipher({
|
|
2070
|
+
blockSize: 16,
|
|
2071
|
+
nonceLength: 16
|
|
2072
|
+
}, function aesctr(key, nonce) {
|
|
2073
|
+
function processCtr(buf, dst) {
|
|
2074
|
+
abytes(buf);
|
|
2075
|
+
if (dst !== undefined) {
|
|
2076
|
+
abytes(dst);
|
|
2077
|
+
if (!isAligned32(dst)) throw new Error('unaligned destination');
|
|
2078
|
+
}
|
|
2079
|
+
const xk = expandKeyLE(key);
|
|
2080
|
+
const n = copyBytes(nonce);
|
|
2081
|
+
const toClean = [xk, n];
|
|
2082
|
+
if (!isAligned32(buf)) toClean.push(buf = copyBytes(buf));
|
|
2083
|
+
const out = ctrCounter(xk, n, buf, dst);
|
|
2084
|
+
clean(...toClean);
|
|
2085
|
+
return out;
|
|
2086
|
+
}
|
|
2087
|
+
return {
|
|
2088
|
+
encrypt: (plaintext, dst) => processCtr(plaintext, dst),
|
|
2089
|
+
decrypt: (ciphertext, dst) => processCtr(ciphertext, dst)
|
|
2090
|
+
};
|
|
2091
|
+
});
|
|
2092
|
+
function validateBlockDecrypt(data, dst) {
|
|
2093
|
+
abytes(data);
|
|
2094
|
+
if (data.length % BLOCK_SIZE !== 0) {
|
|
2095
|
+
throw new Error('ciphertext must be multiple of ' + BLOCK_SIZE);
|
|
2096
|
+
}
|
|
2097
|
+
if (dst !== undefined) {
|
|
2098
|
+
getOutput(data.length, dst);
|
|
2099
|
+
complexOverlapBytes(data, dst);
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
function validateBlockEncrypt(plaintext, pkcs5, dst) {
|
|
2103
|
+
abytes(plaintext);
|
|
2104
|
+
let outLen = plaintext.length;
|
|
2105
|
+
const remaining = outLen % BLOCK_SIZE;
|
|
2106
|
+
if (!pkcs5 && remaining !== 0) throw new Error('plaintext must be multiple of ' + BLOCK_SIZE);
|
|
2107
|
+
if (pkcs5) {
|
|
2108
|
+
let left = BLOCK_SIZE - remaining;
|
|
2109
|
+
if (!left) left = BLOCK_SIZE;
|
|
2110
|
+
outLen = outLen + left;
|
|
2111
|
+
}
|
|
2112
|
+
if (dst !== undefined) {
|
|
2113
|
+
getOutput(outLen, dst);
|
|
2114
|
+
complexOverlapBytes(plaintext, dst);
|
|
2115
|
+
}
|
|
2116
|
+
return outLen;
|
|
2117
|
+
}
|
|
2118
|
+
function prepareBlockEncrypt(plaintext, outLen, dst) {
|
|
2119
|
+
if (dst === undefined) dst = new Uint8Array(outLen);
|
|
2120
|
+
if (!isLE || !isAligned32(plaintext)) plaintext = copyBytes(plaintext);
|
|
2121
|
+
const o = u32(dst);
|
|
2122
|
+
return {
|
|
2123
|
+
b: plaintext,
|
|
2124
|
+
o,
|
|
2125
|
+
out: dst
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
function validatePKCS(data, pkcs5) {
|
|
2129
|
+
if (!pkcs5) return data;
|
|
2130
|
+
const len = data.length;
|
|
2131
|
+
if (len === 0) throw new Error('pkcs7: empty ciphertext not allowed');
|
|
2132
|
+
const lastByte = data[len - 1];
|
|
2133
|
+
let valid = 1;
|
|
2134
|
+
valid &= lastByte - 1 >>> 31 ^ 1;
|
|
2135
|
+
valid &= 16 - lastByte >>> 31 ^ 1;
|
|
2136
|
+
for (let i = 0; i < 16; i++) {
|
|
2137
|
+
const shouldCheck = i - lastByte >>> 31;
|
|
2138
|
+
const eq = (data[len - 1 - i] ^ lastByte) === 0 ? 1 : 0;
|
|
2139
|
+
valid &= eq | shouldCheck ^ 1;
|
|
2140
|
+
}
|
|
2141
|
+
if (!valid) throw new Error('aes: bad decrypt');
|
|
2142
|
+
return data.subarray(0, len - lastByte);
|
|
2143
|
+
}
|
|
2144
|
+
function padPCKS(left) {
|
|
2145
|
+
const tmp = new Uint8Array(16);
|
|
2146
|
+
const tmp32 = u32(tmp);
|
|
2147
|
+
tmp.set(left);
|
|
2148
|
+
const paddingByte = BLOCK_SIZE - left.length;
|
|
2149
|
+
for (let i = BLOCK_SIZE - paddingByte; i < BLOCK_SIZE; i++) tmp[i] = paddingByte;
|
|
2150
|
+
return tmp32;
|
|
2151
|
+
}
|
|
2152
|
+
const ecb = wrapCipher({
|
|
2153
|
+
blockSize: 16
|
|
2154
|
+
}, function aesecb(key, opts = {}) {
|
|
2155
|
+
const pkcs5 = !opts.disablePadding;
|
|
2156
|
+
return {
|
|
2157
|
+
encrypt(plaintext, dst) {
|
|
2158
|
+
const outLen = validateBlockEncrypt(plaintext, pkcs5, dst);
|
|
2159
|
+
const xk = expandKeyLE(key);
|
|
2160
|
+
const {
|
|
2161
|
+
b: input,
|
|
2162
|
+
o,
|
|
2163
|
+
out: _out
|
|
2164
|
+
} = prepareBlockEncrypt(plaintext, outLen, dst);
|
|
2165
|
+
const b = u32(input);
|
|
2166
|
+
swap32IfBE(b);
|
|
2167
|
+
let i = 0;
|
|
2168
|
+
for (; i + 4 <= b.length;) {
|
|
2169
|
+
const {
|
|
2170
|
+
s0,
|
|
2171
|
+
s1,
|
|
2172
|
+
s2,
|
|
2173
|
+
s3
|
|
2174
|
+
} = encrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
|
|
2175
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2176
|
+
}
|
|
2177
|
+
if (pkcs5) {
|
|
2178
|
+
const tmp32 = padPCKS(plaintext.subarray(i * 4));
|
|
2179
|
+
swap32IfBE(tmp32);
|
|
2180
|
+
const {
|
|
2181
|
+
s0,
|
|
2182
|
+
s1,
|
|
2183
|
+
s2,
|
|
2184
|
+
s3
|
|
2185
|
+
} = encrypt(xk, tmp32[0], tmp32[1], tmp32[2], tmp32[3]);
|
|
2186
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2187
|
+
clean(tmp32);
|
|
2188
|
+
}
|
|
2189
|
+
swap32IfBE(o);
|
|
2190
|
+
clean(xk);
|
|
2191
|
+
if (input !== plaintext) clean(input);
|
|
2192
|
+
return _out;
|
|
2193
|
+
},
|
|
2194
|
+
decrypt(ciphertext, dst) {
|
|
2195
|
+
validateBlockDecrypt(ciphertext, dst);
|
|
2196
|
+
const xk = expandKeyDecLE(key);
|
|
2197
|
+
if (dst === undefined) dst = new Uint8Array(ciphertext.length);
|
|
2198
|
+
const toClean = [xk];
|
|
2199
|
+
if (!isLE || !isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2200
|
+
const b = u32(ciphertext);
|
|
2201
|
+
const o = u32(dst);
|
|
2202
|
+
swap32IfBE(b);
|
|
2203
|
+
for (let i = 0; i + 4 <= b.length;) {
|
|
2204
|
+
const {
|
|
2205
|
+
s0,
|
|
2206
|
+
s1,
|
|
2207
|
+
s2,
|
|
2208
|
+
s3
|
|
2209
|
+
} = decrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
|
|
2210
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2211
|
+
}
|
|
2212
|
+
swap32IfBE(o);
|
|
2213
|
+
clean(...toClean);
|
|
2214
|
+
return validatePKCS(dst, pkcs5);
|
|
2215
|
+
}
|
|
2216
|
+
};
|
|
2217
|
+
});
|
|
2218
|
+
const cbc = wrapCipher({
|
|
2219
|
+
blockSize: 16,
|
|
2220
|
+
nonceLength: 16
|
|
2221
|
+
}, function aescbc(key, iv, opts = {}) {
|
|
2222
|
+
const pkcs5 = !opts.disablePadding;
|
|
2223
|
+
return {
|
|
2224
|
+
encrypt(plaintext, dst) {
|
|
2225
|
+
const outLen = validateBlockEncrypt(plaintext, pkcs5, dst);
|
|
2226
|
+
const xk = expandKeyLE(key);
|
|
2227
|
+
const {
|
|
2228
|
+
b: input,
|
|
2229
|
+
o,
|
|
2230
|
+
out: _out
|
|
2231
|
+
} = prepareBlockEncrypt(plaintext, outLen, dst);
|
|
2232
|
+
const b = u32(input);
|
|
2233
|
+
swap32IfBE(b);
|
|
2234
|
+
let _iv = iv;
|
|
2235
|
+
const toClean = [xk];
|
|
2236
|
+
if (!isLE || !isAligned32(_iv)) toClean.push(_iv = copyBytes(_iv));
|
|
2237
|
+
const n32 = u32(_iv);
|
|
2238
|
+
swap32IfBE(n32);
|
|
2239
|
+
let s0 = n32[0],
|
|
2240
|
+
s1 = n32[1],
|
|
2241
|
+
s2 = n32[2],
|
|
2242
|
+
s3 = n32[3];
|
|
2243
|
+
let i = 0;
|
|
2244
|
+
for (; i + 4 <= b.length;) {
|
|
2245
|
+
s0 ^= b[i + 0], s1 ^= b[i + 1], s2 ^= b[i + 2], s3 ^= b[i + 3];
|
|
2246
|
+
({
|
|
2247
|
+
s0,
|
|
2248
|
+
s1,
|
|
2249
|
+
s2,
|
|
2250
|
+
s3
|
|
2251
|
+
} = encrypt(xk, s0, s1, s2, s3));
|
|
2252
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2253
|
+
}
|
|
2254
|
+
if (pkcs5) {
|
|
2255
|
+
const tmp32 = padPCKS(plaintext.subarray(i * 4));
|
|
2256
|
+
swap32IfBE(tmp32);
|
|
2257
|
+
s0 ^= tmp32[0], s1 ^= tmp32[1], s2 ^= tmp32[2], s3 ^= tmp32[3];
|
|
2258
|
+
({
|
|
2259
|
+
s0,
|
|
2260
|
+
s1,
|
|
2261
|
+
s2,
|
|
2262
|
+
s3
|
|
2263
|
+
} = encrypt(xk, s0, s1, s2, s3));
|
|
2264
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2265
|
+
clean(tmp32);
|
|
2266
|
+
}
|
|
2267
|
+
swap32IfBE(o);
|
|
2268
|
+
clean(...toClean);
|
|
2269
|
+
if (input !== plaintext) clean(input);
|
|
2270
|
+
return _out;
|
|
2271
|
+
},
|
|
2272
|
+
decrypt(ciphertext, dst) {
|
|
2273
|
+
validateBlockDecrypt(ciphertext, dst);
|
|
2274
|
+
const xk = expandKeyDecLE(key);
|
|
2275
|
+
if (dst === undefined) dst = new Uint8Array(ciphertext.length);
|
|
2276
|
+
let _iv = iv;
|
|
2277
|
+
const toClean = [xk];
|
|
2278
|
+
if (!isLE || !isAligned32(_iv)) toClean.push(_iv = copyBytes(_iv));
|
|
2279
|
+
const n32 = u32(_iv);
|
|
2280
|
+
swap32IfBE(n32);
|
|
2281
|
+
if (!isLE || !isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2282
|
+
const b = u32(ciphertext);
|
|
2283
|
+
const o = u32(dst);
|
|
2284
|
+
swap32IfBE(b);
|
|
2285
|
+
let s0 = n32[0],
|
|
2286
|
+
s1 = n32[1],
|
|
2287
|
+
s2 = n32[2],
|
|
2288
|
+
s3 = n32[3];
|
|
2289
|
+
for (let i = 0; i + 4 <= b.length;) {
|
|
2290
|
+
const ps0 = s0,
|
|
2291
|
+
ps1 = s1,
|
|
2292
|
+
ps2 = s2,
|
|
2293
|
+
ps3 = s3;
|
|
2294
|
+
s0 = b[i + 0], s1 = b[i + 1], s2 = b[i + 2], s3 = b[i + 3];
|
|
2295
|
+
const {
|
|
2296
|
+
s0: o0,
|
|
2297
|
+
s1: o1,
|
|
2298
|
+
s2: o2,
|
|
2299
|
+
s3: o3
|
|
2300
|
+
} = decrypt(xk, s0, s1, s2, s3);
|
|
2301
|
+
o[i++] = o0 ^ ps0, o[i++] = o1 ^ ps1, o[i++] = o2 ^ ps2, o[i++] = o3 ^ ps3;
|
|
2302
|
+
}
|
|
2303
|
+
swap32IfBE(o);
|
|
2304
|
+
clean(...toClean);
|
|
2305
|
+
return validatePKCS(dst, pkcs5);
|
|
2306
|
+
}
|
|
2307
|
+
};
|
|
2308
|
+
});
|
|
2309
|
+
wrapCipher({
|
|
2310
|
+
blockSize: 16,
|
|
2311
|
+
nonceLength: 16
|
|
2312
|
+
}, function aescfb(key, iv) {
|
|
2313
|
+
function processCfb(src, isEncrypt, dst) {
|
|
2314
|
+
abytes(src);
|
|
2315
|
+
const srcLen = src.length;
|
|
2316
|
+
dst = getOutput(srcLen, dst);
|
|
2317
|
+
if (overlapBytes(src, dst)) throw new Error('overlapping src and dst not supported');
|
|
2318
|
+
const xk = expandKeyLE(key);
|
|
2319
|
+
let _iv = iv;
|
|
2320
|
+
const toClean = [xk];
|
|
2321
|
+
if (!isLE || !isAligned32(_iv)) toClean.push(_iv = copyBytes(_iv));
|
|
2322
|
+
if (!isLE || !isAligned32(src)) toClean.push(src = copyBytes(src));
|
|
2323
|
+
const src32 = u32(src);
|
|
2324
|
+
const dst32 = u32(dst);
|
|
2325
|
+
const next32 = isEncrypt ? dst32 : src32;
|
|
2326
|
+
const n32 = u32(_iv);
|
|
2327
|
+
swap32IfBE(src32);
|
|
2328
|
+
swap32IfBE(n32);
|
|
2329
|
+
let s0 = n32[0],
|
|
2330
|
+
s1 = n32[1],
|
|
2331
|
+
s2 = n32[2],
|
|
2332
|
+
s3 = n32[3];
|
|
2333
|
+
for (let i = 0; i + 4 <= src32.length;) {
|
|
2334
|
+
const {
|
|
2335
|
+
s0: e0,
|
|
2336
|
+
s1: e1,
|
|
2337
|
+
s2: e2,
|
|
2338
|
+
s3: e3
|
|
2339
|
+
} = encrypt(xk, s0, s1, s2, s3);
|
|
2340
|
+
dst32[i + 0] = src32[i + 0] ^ e0;
|
|
2341
|
+
dst32[i + 1] = src32[i + 1] ^ e1;
|
|
2342
|
+
dst32[i + 2] = src32[i + 2] ^ e2;
|
|
2343
|
+
dst32[i + 3] = src32[i + 3] ^ e3;
|
|
2344
|
+
s0 = next32[i++], s1 = next32[i++], s2 = next32[i++], s3 = next32[i++];
|
|
2345
|
+
}
|
|
2346
|
+
const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
|
|
2347
|
+
if (start < srcLen) {
|
|
2348
|
+
if (!isLE) swap32IfBE(src32.subarray(start / 4));
|
|
2349
|
+
({
|
|
2350
|
+
s0,
|
|
2351
|
+
s1,
|
|
2352
|
+
s2,
|
|
2353
|
+
s3
|
|
2354
|
+
} = encrypt(xk, s0, s1, s2, s3));
|
|
2355
|
+
const tmp = new Uint32Array([s0, s1, s2, s3]);
|
|
2356
|
+
swap32IfBE(tmp);
|
|
2357
|
+
const buf = u8(tmp);
|
|
2358
|
+
for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
|
|
2359
|
+
clean(buf);
|
|
2360
|
+
}
|
|
2361
|
+
if (!isLE) swap32IfBE(dst32.subarray(0, start / 4));
|
|
2362
|
+
clean(...toClean);
|
|
2363
|
+
return dst;
|
|
2364
|
+
}
|
|
2365
|
+
return {
|
|
2366
|
+
encrypt: (plaintext, dst) => processCfb(plaintext, true, dst),
|
|
2367
|
+
decrypt: (ciphertext, dst) => processCfb(ciphertext, false, dst)
|
|
2368
|
+
};
|
|
2369
|
+
});
|
|
2370
|
+
function computeTag(fn, isLE, key, data, AAD) {
|
|
2371
|
+
const aadLength = AAD ? AAD.length : 0;
|
|
2372
|
+
const h = fn.create(key, data.length + aadLength);
|
|
2373
|
+
if (AAD) h.update(AAD);
|
|
2374
|
+
const num = u64Lengths(8 * data.length, 8 * aadLength, isLE);
|
|
2375
|
+
h.update(data);
|
|
2376
|
+
h.update(num);
|
|
2377
|
+
const res = h.digest();
|
|
2378
|
+
clean(num);
|
|
2379
|
+
return res;
|
|
2380
|
+
}
|
|
2381
|
+
wrapCipher({
|
|
2382
|
+
blockSize: 16,
|
|
2383
|
+
nonceLength: 12,
|
|
2384
|
+
tagLength: 16,
|
|
2385
|
+
withAAD: true,
|
|
2386
|
+
varSizeNonce: true
|
|
2387
|
+
}, function aesgcm(key, nonce, AAD) {
|
|
2388
|
+
if (nonce.length < 8) throw new Error('aes/gcm: invalid nonce length');
|
|
2389
|
+
const tagLength = 16;
|
|
2390
|
+
function _computeTag(authKey, tagMask, data) {
|
|
2391
|
+
const tag = computeTag(ghash, false, authKey, data, AAD);
|
|
2392
|
+
for (let i = 0; i < tagMask.length; i++) tag[i] ^= tagMask[i];
|
|
2393
|
+
return tag;
|
|
2394
|
+
}
|
|
2395
|
+
function deriveKeys() {
|
|
2396
|
+
const xk = expandKeyLE(key);
|
|
2397
|
+
const authKey = EMPTY_BLOCK.slice();
|
|
2398
|
+
const counter = EMPTY_BLOCK.slice();
|
|
2399
|
+
ctr32(xk, false, counter, counter, authKey);
|
|
2400
|
+
if (nonce.length === 12) {
|
|
2401
|
+
counter.set(nonce);
|
|
2402
|
+
} else {
|
|
2403
|
+
const nonceLen = EMPTY_BLOCK.slice();
|
|
2404
|
+
const view = createView(nonceLen);
|
|
2405
|
+
view.setBigUint64(8, BigInt(nonce.length * 8), false);
|
|
2406
|
+
const g = ghash.create(authKey).update(nonce).update(nonceLen);
|
|
2407
|
+
g.digestInto(counter);
|
|
2408
|
+
g.destroy();
|
|
2409
|
+
}
|
|
2410
|
+
const tagMask = ctr32(xk, false, counter, EMPTY_BLOCK);
|
|
2411
|
+
return {
|
|
2412
|
+
xk,
|
|
2413
|
+
authKey,
|
|
2414
|
+
counter,
|
|
2415
|
+
tagMask
|
|
2416
|
+
};
|
|
2417
|
+
}
|
|
2418
|
+
return {
|
|
2419
|
+
encrypt(plaintext) {
|
|
2420
|
+
const {
|
|
2421
|
+
xk,
|
|
2422
|
+
authKey,
|
|
2423
|
+
counter,
|
|
2424
|
+
tagMask
|
|
2425
|
+
} = deriveKeys();
|
|
2426
|
+
const out = new Uint8Array(plaintext.length + tagLength);
|
|
2427
|
+
const toClean = [xk, authKey, counter, tagMask];
|
|
2428
|
+
if (!isAligned32(plaintext)) toClean.push(plaintext = copyBytes(plaintext));
|
|
2429
|
+
ctr32(xk, false, counter, plaintext, out.subarray(0, plaintext.length));
|
|
2430
|
+
const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
|
|
2431
|
+
toClean.push(tag);
|
|
2432
|
+
out.set(tag, plaintext.length);
|
|
2433
|
+
clean(...toClean);
|
|
2434
|
+
return out;
|
|
2435
|
+
},
|
|
2436
|
+
decrypt(ciphertext) {
|
|
2437
|
+
const {
|
|
2438
|
+
xk,
|
|
2439
|
+
authKey,
|
|
2440
|
+
counter,
|
|
2441
|
+
tagMask
|
|
2442
|
+
} = deriveKeys();
|
|
2443
|
+
const toClean = [xk, authKey, tagMask, counter];
|
|
2444
|
+
if (!isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2445
|
+
const data = ciphertext.subarray(0, -tagLength);
|
|
2446
|
+
const passedTag = ciphertext.subarray(-tagLength);
|
|
2447
|
+
const tag = _computeTag(authKey, tagMask, data);
|
|
2448
|
+
toClean.push(tag);
|
|
2449
|
+
if (!equalBytes(tag, passedTag)) {
|
|
2450
|
+
clean(...toClean);
|
|
2451
|
+
throw new Error('aes-gcm: invalid tag');
|
|
2452
|
+
}
|
|
2453
|
+
const out = ctr32(xk, false, counter, data);
|
|
2454
|
+
clean(...toClean);
|
|
2455
|
+
return out;
|
|
2456
|
+
}
|
|
2457
|
+
};
|
|
2458
|
+
});
|
|
2459
|
+
const limit = (name, min, max) => value => {
|
|
2460
|
+
if (!Number.isSafeInteger(value) || min > value || value > max) {
|
|
2461
|
+
const minmax = '[' + min + '..' + max + ']';
|
|
2462
|
+
throw new Error('' + name + ': expected value in range ' + minmax + ', got ' + value);
|
|
2463
|
+
}
|
|
2464
|
+
};
|
|
2465
|
+
wrapCipher({
|
|
2466
|
+
blockSize: 16,
|
|
2467
|
+
nonceLength: 12,
|
|
2468
|
+
tagLength: 16,
|
|
2469
|
+
withAAD: true,
|
|
2470
|
+
varSizeNonce: true
|
|
2471
|
+
}, function aessiv(key, nonce, AAD) {
|
|
2472
|
+
const tagLength = 16;
|
|
2473
|
+
const AAD_LIMIT = limit('AAD', 0, 2 ** 36);
|
|
2474
|
+
const PLAIN_LIMIT = limit('plaintext', 0, 2 ** 36);
|
|
2475
|
+
const NONCE_LIMIT = limit('nonce', 12, 12);
|
|
2476
|
+
const CIPHER_LIMIT = limit('ciphertext', 16, 2 ** 36 + 16);
|
|
2477
|
+
abytes(key);
|
|
2478
|
+
validateKeyLength(key);
|
|
2479
|
+
NONCE_LIMIT(nonce.length);
|
|
2480
|
+
if (AAD !== undefined) AAD_LIMIT(AAD.length);
|
|
2481
|
+
function deriveKeys() {
|
|
2482
|
+
const xk = expandKeyLE(key);
|
|
2483
|
+
const encKey = new Uint8Array(key.length);
|
|
2484
|
+
const authKey = new Uint8Array(16);
|
|
2485
|
+
const toClean = [xk, encKey];
|
|
2486
|
+
let _nonce = nonce;
|
|
2487
|
+
if (!isLE || !isAligned32(_nonce)) toClean.push(_nonce = copyBytes(_nonce));
|
|
2488
|
+
const n32 = u32(_nonce);
|
|
2489
|
+
swap32IfBE(n32);
|
|
2490
|
+
let s0 = 0,
|
|
2491
|
+
s1 = n32[0],
|
|
2492
|
+
s2 = n32[1],
|
|
2493
|
+
s3 = n32[2];
|
|
2494
|
+
let counter = 0;
|
|
2495
|
+
for (const derivedKey of [authKey, encKey].map(u32)) {
|
|
2496
|
+
const d32 = u32(derivedKey);
|
|
2497
|
+
for (let i = 0; i < d32.length; i += 2) {
|
|
2498
|
+
const {
|
|
2499
|
+
s0: o0,
|
|
2500
|
+
s1: o1
|
|
2501
|
+
} = encrypt(xk, s0, s1, s2, s3);
|
|
2502
|
+
d32[i + 0] = o0;
|
|
2503
|
+
d32[i + 1] = o1;
|
|
2504
|
+
s0 = ++counter;
|
|
2505
|
+
}
|
|
2506
|
+
swap32IfBE(d32);
|
|
2507
|
+
}
|
|
2508
|
+
const res = {
|
|
2509
|
+
authKey,
|
|
2510
|
+
encKey: expandKeyLE(encKey)
|
|
2511
|
+
};
|
|
2512
|
+
clean(...toClean);
|
|
2513
|
+
return res;
|
|
2514
|
+
}
|
|
2515
|
+
function _computeTag(encKey, authKey, data) {
|
|
2516
|
+
const tag = computeTag(polyval, true, authKey, data, AAD);
|
|
2517
|
+
for (let i = 0; i < 12; i++) tag[i] ^= nonce[i];
|
|
2518
|
+
tag[15] &= 0x7f;
|
|
2519
|
+
const t32 = u32(tag);
|
|
2520
|
+
swap32IfBE(t32);
|
|
2521
|
+
let s0 = t32[0],
|
|
2522
|
+
s1 = t32[1],
|
|
2523
|
+
s2 = t32[2],
|
|
2524
|
+
s3 = t32[3];
|
|
2525
|
+
({
|
|
2526
|
+
s0,
|
|
2527
|
+
s1,
|
|
2528
|
+
s2,
|
|
2529
|
+
s3
|
|
2530
|
+
} = encrypt(encKey, s0, s1, s2, s3));
|
|
2531
|
+
t32[0] = s0, t32[1] = s1, t32[2] = s2, t32[3] = s3;
|
|
2532
|
+
swap32IfBE(t32);
|
|
2533
|
+
return tag;
|
|
2534
|
+
}
|
|
2535
|
+
function processSiv(encKey, tag, input) {
|
|
2536
|
+
let block = copyBytes(tag);
|
|
2537
|
+
block[15] |= 0x80;
|
|
2538
|
+
const res = ctr32(encKey, true, block, input);
|
|
2539
|
+
clean(block);
|
|
2540
|
+
return res;
|
|
2541
|
+
}
|
|
2542
|
+
return {
|
|
2543
|
+
encrypt(plaintext) {
|
|
2544
|
+
PLAIN_LIMIT(plaintext.length);
|
|
2545
|
+
const {
|
|
2546
|
+
encKey,
|
|
2547
|
+
authKey
|
|
2548
|
+
} = deriveKeys();
|
|
2549
|
+
const tag = _computeTag(encKey, authKey, plaintext);
|
|
2550
|
+
const toClean = [encKey, authKey, tag];
|
|
2551
|
+
if (!isAligned32(plaintext)) toClean.push(plaintext = copyBytes(plaintext));
|
|
2552
|
+
const out = new Uint8Array(plaintext.length + tagLength);
|
|
2553
|
+
out.set(tag, plaintext.length);
|
|
2554
|
+
out.set(processSiv(encKey, tag, plaintext));
|
|
2555
|
+
clean(...toClean);
|
|
2556
|
+
return out;
|
|
2557
|
+
},
|
|
2558
|
+
decrypt(ciphertext) {
|
|
2559
|
+
CIPHER_LIMIT(ciphertext.length);
|
|
2560
|
+
const tag = ciphertext.subarray(-tagLength);
|
|
2561
|
+
const {
|
|
2562
|
+
encKey,
|
|
2563
|
+
authKey
|
|
2564
|
+
} = deriveKeys();
|
|
2565
|
+
const toClean = [encKey, authKey];
|
|
2566
|
+
if (!isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2567
|
+
const plaintext = processSiv(encKey, tag, ciphertext.subarray(0, -tagLength));
|
|
2568
|
+
const expectedTag = _computeTag(encKey, authKey, plaintext);
|
|
2569
|
+
toClean.push(expectedTag);
|
|
2570
|
+
if (!equalBytes(tag, expectedTag)) {
|
|
2571
|
+
clean(plaintext, ...toClean);
|
|
2572
|
+
throw new Error('invalid polyval tag');
|
|
2573
|
+
}
|
|
2574
|
+
clean(...toClean);
|
|
2575
|
+
return plaintext;
|
|
2576
|
+
}
|
|
2577
|
+
};
|
|
2578
|
+
});
|
|
2579
|
+
function isBytes32(a) {
|
|
2580
|
+
return a instanceof Uint32Array || ArrayBuffer.isView(a) && a.constructor.name === 'Uint32Array';
|
|
2581
|
+
}
|
|
2582
|
+
function encryptBlock(xk, block) {
|
|
2583
|
+
abytes(block, 16, 'block');
|
|
2584
|
+
if (!isBytes32(xk)) throw new Error('_encryptBlock accepts result of expandKeyLE');
|
|
2585
|
+
const b32 = u32(block);
|
|
2586
|
+
swap32IfBE(b32);
|
|
2587
|
+
let {
|
|
2588
|
+
s0,
|
|
2589
|
+
s1,
|
|
2590
|
+
s2,
|
|
2591
|
+
s3
|
|
2592
|
+
} = encrypt(xk, b32[0], b32[1], b32[2], b32[3]);
|
|
2593
|
+
b32[0] = s0, b32[1] = s1, b32[2] = s2, b32[3] = s3;
|
|
2594
|
+
swap32IfBE(b32);
|
|
2595
|
+
return block;
|
|
2596
|
+
}
|
|
2597
|
+
function decryptBlock(xk, block) {
|
|
2598
|
+
abytes(block, 16, 'block');
|
|
2599
|
+
if (!isBytes32(xk)) throw new Error('_decryptBlock accepts result of expandKeyLE');
|
|
2600
|
+
const b32 = u32(block);
|
|
2601
|
+
swap32IfBE(b32);
|
|
2602
|
+
let {
|
|
2603
|
+
s0,
|
|
2604
|
+
s1,
|
|
2605
|
+
s2,
|
|
2606
|
+
s3
|
|
2607
|
+
} = decrypt(xk, b32[0], b32[1], b32[2], b32[3]);
|
|
2608
|
+
b32[0] = s0, b32[1] = s1, b32[2] = s2, b32[3] = s3;
|
|
2609
|
+
swap32IfBE(b32);
|
|
2610
|
+
return block;
|
|
2611
|
+
}
|
|
2612
|
+
const AESW = {
|
|
2613
|
+
encrypt(kek, out) {
|
|
2614
|
+
if (out.length >= 2 ** 32) throw new Error('plaintext should be less than 4gb');
|
|
2615
|
+
const xk = expandKeyLE(kek);
|
|
2616
|
+
if (out.length === 16) encryptBlock(xk, out);else {
|
|
2617
|
+
const o32 = u32(out);
|
|
2618
|
+
swap32IfBE(o32);
|
|
2619
|
+
let a0 = o32[0],
|
|
2620
|
+
a1 = o32[1];
|
|
2621
|
+
for (let j = 0, ctr = 1; j < 6; j++) {
|
|
2622
|
+
for (let pos = 2; pos < o32.length; pos += 2, ctr++) {
|
|
2623
|
+
const {
|
|
2624
|
+
s0,
|
|
2625
|
+
s1,
|
|
2626
|
+
s2,
|
|
2627
|
+
s3
|
|
2628
|
+
} = encrypt(xk, a0, a1, o32[pos], o32[pos + 1]);
|
|
2629
|
+
a0 = s0, a1 = s1 ^ byteSwap(ctr), o32[pos] = s2, o32[pos + 1] = s3;
|
|
2630
|
+
}
|
|
2631
|
+
}
|
|
2632
|
+
o32[0] = a0, o32[1] = a1;
|
|
2633
|
+
swap32IfBE(o32);
|
|
2634
|
+
}
|
|
2635
|
+
xk.fill(0);
|
|
2636
|
+
},
|
|
2637
|
+
decrypt(kek, out) {
|
|
2638
|
+
if (out.length - 8 >= 2 ** 32) throw new Error('ciphertext should be less than 4gb');
|
|
2639
|
+
const xk = expandKeyDecLE(kek);
|
|
2640
|
+
const chunks = out.length / 8 - 1;
|
|
2641
|
+
if (chunks === 1) decryptBlock(xk, out);else {
|
|
2642
|
+
const o32 = u32(out);
|
|
2643
|
+
swap32IfBE(o32);
|
|
2644
|
+
let a0 = o32[0],
|
|
2645
|
+
a1 = o32[1];
|
|
2646
|
+
for (let j = 0, ctr = chunks * 6; j < 6; j++) {
|
|
2647
|
+
for (let pos = chunks * 2; pos >= 1; pos -= 2, ctr--) {
|
|
2648
|
+
a1 ^= byteSwap(ctr);
|
|
2649
|
+
const {
|
|
2650
|
+
s0,
|
|
2651
|
+
s1,
|
|
2652
|
+
s2,
|
|
2653
|
+
s3
|
|
2654
|
+
} = decrypt(xk, a0, a1, o32[pos], o32[pos + 1]);
|
|
2655
|
+
a0 = s0, a1 = s1, o32[pos] = s2, o32[pos + 1] = s3;
|
|
2656
|
+
}
|
|
2657
|
+
}
|
|
2658
|
+
o32[0] = a0, o32[1] = a1;
|
|
2659
|
+
swap32IfBE(o32);
|
|
2660
|
+
}
|
|
2661
|
+
xk.fill(0);
|
|
2662
|
+
}
|
|
2663
|
+
};
|
|
2664
|
+
const AESKW_IV = new Uint8Array(8).fill(0xa6);
|
|
2665
|
+
wrapCipher({
|
|
2666
|
+
blockSize: 8
|
|
2667
|
+
}, kek => ({
|
|
2668
|
+
encrypt(plaintext) {
|
|
2669
|
+
if (!plaintext.length || plaintext.length % 8 !== 0) throw new Error('invalid plaintext length');
|
|
2670
|
+
if (plaintext.length === 8) throw new Error('8-byte keys not allowed, use AESKWP');
|
|
2671
|
+
const out = concatBytes(AESKW_IV, plaintext);
|
|
2672
|
+
AESW.encrypt(kek, out);
|
|
2673
|
+
return out;
|
|
2674
|
+
},
|
|
2675
|
+
decrypt(ciphertext) {
|
|
2676
|
+
if (ciphertext.length % 8 !== 0 || ciphertext.length < 3 * 8) throw new Error('invalid ciphertext length');
|
|
2677
|
+
const out = copyBytes(ciphertext);
|
|
2678
|
+
AESW.decrypt(kek, out);
|
|
2679
|
+
if (!equalBytes(out.subarray(0, 8), AESKW_IV)) throw new Error('integrity check failed');
|
|
2680
|
+
out.subarray(0, 8).fill(0);
|
|
2681
|
+
return out.subarray(8);
|
|
2682
|
+
}
|
|
2683
|
+
}));
|
|
2684
|
+
const AESKWP_IV = 0xa65959a6;
|
|
2685
|
+
wrapCipher({
|
|
2686
|
+
blockSize: 8
|
|
2687
|
+
}, kek => ({
|
|
2688
|
+
encrypt(plaintext) {
|
|
2689
|
+
if (!plaintext.length) throw new Error('invalid plaintext length');
|
|
2690
|
+
const padded = Math.ceil(plaintext.length / 8) * 8;
|
|
2691
|
+
const out = new Uint8Array(8 + padded);
|
|
2692
|
+
out.set(plaintext, 8);
|
|
2693
|
+
const out32 = u32(out);
|
|
2694
|
+
out32[0] = swap8IfBE(AESKWP_IV);
|
|
2695
|
+
out32[1] = swap8IfBE(byteSwap(plaintext.length));
|
|
2696
|
+
AESW.encrypt(kek, out);
|
|
2697
|
+
return out;
|
|
2698
|
+
},
|
|
2699
|
+
decrypt(ciphertext) {
|
|
2700
|
+
if (ciphertext.length < 16) throw new Error('invalid ciphertext length');
|
|
2701
|
+
const out = copyBytes(ciphertext);
|
|
2702
|
+
const o32 = u32(out);
|
|
2703
|
+
AESW.decrypt(kek, out);
|
|
2704
|
+
const len = byteSwap(swap8IfBE(o32[1])) >>> 0;
|
|
2705
|
+
const padded = Math.ceil(len / 8) * 8;
|
|
2706
|
+
if (swap8IfBE(o32[0]) !== AESKWP_IV || out.length - 8 !== padded) throw new Error('integrity check failed');
|
|
2707
|
+
for (let i = len; i < padded; i++) if (out[8 + i] !== 0) throw new Error('integrity check failed');
|
|
2708
|
+
out.subarray(0, 8).fill(0);
|
|
2709
|
+
return out.subarray(8, 8 + len);
|
|
2710
|
+
}
|
|
2711
|
+
}));
|
|
2712
|
+
function dbl(block) {
|
|
2713
|
+
let carry = 0;
|
|
2714
|
+
for (let i = BLOCK_SIZE - 1; i >= 0; i--) {
|
|
2715
|
+
const newCarry = (block[i] & 0x80) >>> 7;
|
|
2716
|
+
block[i] = block[i] << 1 | carry;
|
|
2717
|
+
carry = newCarry;
|
|
2718
|
+
}
|
|
2719
|
+
block[BLOCK_SIZE - 1] ^= 0x87 & -carry;
|
|
2720
|
+
return block;
|
|
2721
|
+
}
|
|
2722
|
+
function xorBlock(a, b) {
|
|
2723
|
+
if (a.length !== b.length) throw new Error('xorBlock: blocks must have same length');
|
|
2724
|
+
for (let i = 0; i < a.length; i++) {
|
|
2725
|
+
a[i] = a[i] ^ b[i];
|
|
1270
2726
|
}
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
2727
|
+
return a;
|
|
2728
|
+
}
|
|
2729
|
+
function xorend(a, b) {
|
|
2730
|
+
if (b.length > a.length) {
|
|
2731
|
+
throw new Error('xorend: expected len(B) <= len(A)');
|
|
1274
2732
|
}
|
|
2733
|
+
const offset = a.length - b.length;
|
|
2734
|
+
for (let i = 0; i < b.length; i++) {
|
|
2735
|
+
a[offset + i] = a[offset + i] ^ b[i];
|
|
2736
|
+
}
|
|
2737
|
+
return a;
|
|
1275
2738
|
}
|
|
1276
|
-
class
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
this.
|
|
1292
|
-
this.
|
|
1293
|
-
this.
|
|
1294
|
-
this.
|
|
2739
|
+
class _CMAC {
|
|
2740
|
+
blockLen = BLOCK_SIZE;
|
|
2741
|
+
outputLen = BLOCK_SIZE;
|
|
2742
|
+
buffer;
|
|
2743
|
+
pos;
|
|
2744
|
+
finished;
|
|
2745
|
+
destroyed;
|
|
2746
|
+
k1;
|
|
2747
|
+
k2;
|
|
2748
|
+
x;
|
|
2749
|
+
x32;
|
|
2750
|
+
xk;
|
|
2751
|
+
constructor(key) {
|
|
2752
|
+
abytes(key);
|
|
2753
|
+
validateKeyLength(key);
|
|
2754
|
+
this.xk = expandKeyLE(key);
|
|
2755
|
+
this.buffer = new Uint8Array(BLOCK_SIZE);
|
|
2756
|
+
this.pos = 0;
|
|
2757
|
+
this.finished = false;
|
|
2758
|
+
this.destroyed = false;
|
|
2759
|
+
this.x = new Uint8Array(BLOCK_SIZE);
|
|
2760
|
+
this.x32 = u32(this.x);
|
|
2761
|
+
const L = new Uint8Array(BLOCK_SIZE);
|
|
2762
|
+
encryptBlock(this.xk, L);
|
|
2763
|
+
this.k1 = dbl(L);
|
|
2764
|
+
this.k2 = dbl(new Uint8Array(this.k1));
|
|
2765
|
+
}
|
|
2766
|
+
process(data, pos) {
|
|
2767
|
+
const {
|
|
2768
|
+
x,
|
|
2769
|
+
x32,
|
|
2770
|
+
xk
|
|
2771
|
+
} = this;
|
|
2772
|
+
for (let i = 0; i < BLOCK_SIZE; i++) x[i] ^= data[pos + i];
|
|
2773
|
+
swap32IfBE(x32);
|
|
2774
|
+
const {
|
|
2775
|
+
s0,
|
|
2776
|
+
s1,
|
|
2777
|
+
s2,
|
|
2778
|
+
s3
|
|
2779
|
+
} = encrypt(xk, x32[0], x32[1], x32[2], x32[3]);
|
|
2780
|
+
x32[0] = s0, x32[1] = s1, x32[2] = s2, x32[3] = s3;
|
|
2781
|
+
swap32IfBE(x32);
|
|
2782
|
+
}
|
|
2783
|
+
update(data) {
|
|
2784
|
+
aexists(this);
|
|
2785
|
+
abytes(data);
|
|
2786
|
+
let pos = 0;
|
|
2787
|
+
if (this.pos) {
|
|
2788
|
+
const take = Math.min(BLOCK_SIZE - this.pos, data.length);
|
|
2789
|
+
this.buffer.set(data.subarray(0, take), this.pos);
|
|
2790
|
+
this.pos += take;
|
|
2791
|
+
pos = take;
|
|
2792
|
+
if (this.pos === BLOCK_SIZE && pos < data.length) {
|
|
2793
|
+
this.process(this.buffer, 0);
|
|
2794
|
+
this.pos = 0;
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
while (pos + BLOCK_SIZE < data.length) {
|
|
2798
|
+
this.process(data, pos);
|
|
2799
|
+
pos += BLOCK_SIZE;
|
|
2800
|
+
}
|
|
2801
|
+
if (pos < data.length) {
|
|
2802
|
+
this.buffer.set(data.subarray(pos), 0);
|
|
2803
|
+
this.pos = data.length - pos;
|
|
2804
|
+
}
|
|
2805
|
+
return this;
|
|
2806
|
+
}
|
|
2807
|
+
digestInto(out) {
|
|
2808
|
+
aexists(this);
|
|
2809
|
+
aoutput32(out, this);
|
|
2810
|
+
this.finished = true;
|
|
2811
|
+
const view = out.subarray(0, this.outputLen);
|
|
2812
|
+
let last = new Uint8Array(BLOCK_SIZE);
|
|
2813
|
+
if (this.pos === BLOCK_SIZE) {
|
|
2814
|
+
last.set(this.buffer);
|
|
2815
|
+
xorBlock(last, this.k1);
|
|
2816
|
+
} else {
|
|
2817
|
+
last.set(this.buffer.subarray(0, this.pos));
|
|
2818
|
+
last[this.pos] = 0x80;
|
|
2819
|
+
xorBlock(last, this.k2);
|
|
2820
|
+
}
|
|
2821
|
+
view.set(this.x);
|
|
2822
|
+
xorBlock(view, last);
|
|
2823
|
+
encryptBlock(this.xk, view);
|
|
2824
|
+
clean(last);
|
|
2825
|
+
}
|
|
2826
|
+
digest() {
|
|
2827
|
+
const {
|
|
2828
|
+
buffer,
|
|
2829
|
+
outputLen
|
|
2830
|
+
} = this;
|
|
2831
|
+
this.digestInto(buffer);
|
|
2832
|
+
const res = buffer.slice(0, outputLen);
|
|
2833
|
+
this.destroy();
|
|
2834
|
+
return res;
|
|
2835
|
+
}
|
|
2836
|
+
destroy() {
|
|
2837
|
+
const {
|
|
2838
|
+
buffer,
|
|
2839
|
+
destroyed,
|
|
2840
|
+
x,
|
|
2841
|
+
xk,
|
|
2842
|
+
k1,
|
|
2843
|
+
k2
|
|
2844
|
+
} = this;
|
|
2845
|
+
if (destroyed) return;
|
|
2846
|
+
this.destroyed = true;
|
|
2847
|
+
clean(buffer, x, xk, k1, k2);
|
|
1295
2848
|
}
|
|
1296
2849
|
}
|
|
1297
|
-
const
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
this.Ch = T256_IV[4] | 0;
|
|
1328
|
-
this.Cl = T256_IV[5] | 0;
|
|
1329
|
-
this.Dh = T256_IV[6] | 0;
|
|
1330
|
-
this.Dl = T256_IV[7] | 0;
|
|
1331
|
-
this.Eh = T256_IV[8] | 0;
|
|
1332
|
-
this.El = T256_IV[9] | 0;
|
|
1333
|
-
this.Fh = T256_IV[10] | 0;
|
|
1334
|
-
this.Fl = T256_IV[11] | 0;
|
|
1335
|
-
this.Gh = T256_IV[12] | 0;
|
|
1336
|
-
this.Gl = T256_IV[13] | 0;
|
|
1337
|
-
this.Hh = T256_IV[14] | 0;
|
|
1338
|
-
this.Hl = T256_IV[15] | 0;
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
const sha256 = createHasher(() => new SHA256());
|
|
1342
|
-
createHasher(() => new SHA224());
|
|
1343
|
-
createHasher(() => new SHA512());
|
|
1344
|
-
createHasher(() => new SHA384());
|
|
1345
|
-
createHasher(() => new SHA512_256());
|
|
1346
|
-
createHasher(() => new SHA512_224());
|
|
1347
|
-
|
|
1348
|
-
function sha256Hash(data) {
|
|
1349
|
-
return sha256(data);
|
|
2850
|
+
const cmac = wrapMacConstructor(16, key => new _CMAC(key));
|
|
2851
|
+
function s2v(key, strings) {
|
|
2852
|
+
validateKeyLength(key);
|
|
2853
|
+
const len = strings.length;
|
|
2854
|
+
if (len > 127) {
|
|
2855
|
+
throw new Error('s2v: expected <= 127 inputs');
|
|
2856
|
+
}
|
|
2857
|
+
if (len === 0) return cmac(ONE_BLOCK, key);
|
|
2858
|
+
let d = cmac(EMPTY_BLOCK, key);
|
|
2859
|
+
for (let i = 0; i < len - 1; i++) {
|
|
2860
|
+
dbl(d);
|
|
2861
|
+
const cmacResult = cmac(strings[i], key);
|
|
2862
|
+
xorBlock(d, cmacResult);
|
|
2863
|
+
clean(cmacResult);
|
|
2864
|
+
}
|
|
2865
|
+
const s_n = strings[len - 1];
|
|
2866
|
+
abytes(s_n);
|
|
2867
|
+
let t;
|
|
2868
|
+
if (s_n.byteLength >= BLOCK_SIZE) {
|
|
2869
|
+
t = xorend(Uint8Array.from(s_n), d);
|
|
2870
|
+
} else {
|
|
2871
|
+
const paddedSn = new Uint8Array(BLOCK_SIZE);
|
|
2872
|
+
paddedSn.set(s_n);
|
|
2873
|
+
paddedSn[s_n.length] = 0x80;
|
|
2874
|
+
t = xorBlock(dbl(d), paddedSn);
|
|
2875
|
+
clean(paddedSn);
|
|
2876
|
+
}
|
|
2877
|
+
const result = cmac(t, key);
|
|
2878
|
+
clean(d, t);
|
|
2879
|
+
return result;
|
|
1350
2880
|
}
|
|
2881
|
+
wrapCipher({
|
|
2882
|
+
blockSize: 16,
|
|
2883
|
+
tagLength: 16,
|
|
2884
|
+
withAAD: true
|
|
2885
|
+
}, function aessiv(key, ...AAD) {
|
|
2886
|
+
const PLAIN_LIMIT = limit('plaintext', 0, 2 ** 132);
|
|
2887
|
+
const CIPHER_LIMIT = limit('ciphertext', 16, 2 ** 132 + 16);
|
|
2888
|
+
if (AAD.length > 126) {
|
|
2889
|
+
throw new Error('"AAD" expected <= 126 items');
|
|
2890
|
+
}
|
|
2891
|
+
AAD.forEach(aad => abytes(aad));
|
|
2892
|
+
abytes(key);
|
|
2893
|
+
if (![32, 48, 64].includes(key.length)) throw new Error('"aes key" expected Uint8Array of length 32/48/64, got length=' + key.length);
|
|
2894
|
+
const k1 = key.subarray(0, key.length / 2);
|
|
2895
|
+
const k2 = key.subarray(key.length / 2);
|
|
2896
|
+
return {
|
|
2897
|
+
encrypt(plaintext) {
|
|
2898
|
+
PLAIN_LIMIT(plaintext.length);
|
|
2899
|
+
const v = s2v(k1, [...AAD, plaintext]);
|
|
2900
|
+
const q = Uint8Array.from(v);
|
|
2901
|
+
q[8] &= 0x7f;
|
|
2902
|
+
q[12] &= 0x7f;
|
|
2903
|
+
const c = ctr(k2, q).encrypt(plaintext);
|
|
2904
|
+
return concatBytes(v, c);
|
|
2905
|
+
},
|
|
2906
|
+
decrypt(ciphertext) {
|
|
2907
|
+
CIPHER_LIMIT(ciphertext.length);
|
|
2908
|
+
const v = ciphertext.subarray(0, BLOCK_SIZE);
|
|
2909
|
+
const c = ciphertext.subarray(BLOCK_SIZE);
|
|
2910
|
+
const q = Uint8Array.from(v);
|
|
2911
|
+
q[8] &= 0x7f;
|
|
2912
|
+
q[12] &= 0x7f;
|
|
2913
|
+
const p = ctr(k2, q).decrypt(c);
|
|
2914
|
+
const t = s2v(k1, [...AAD, p]);
|
|
2915
|
+
if (equalBytes(t, v)) {
|
|
2916
|
+
return p;
|
|
2917
|
+
} else {
|
|
2918
|
+
clean(p);
|
|
2919
|
+
throw new Error('invalid siv tag');
|
|
2920
|
+
}
|
|
2921
|
+
}
|
|
2922
|
+
};
|
|
2923
|
+
});
|
|
1351
2924
|
|
|
1352
2925
|
function aesCbcEncrypt(data, key, iv, padding = true) {
|
|
1353
|
-
return
|
|
2926
|
+
return cbc(key, iv, {
|
|
1354
2927
|
disablePadding: !padding
|
|
1355
2928
|
}).encrypt(data);
|
|
1356
2929
|
}
|
|
1357
2930
|
function aesEcbEncrypt(data, key) {
|
|
1358
|
-
return
|
|
2931
|
+
return ecb(key, {
|
|
1359
2932
|
disablePadding: true
|
|
1360
2933
|
}).encrypt(data);
|
|
1361
2934
|
}
|
|
@@ -1484,7 +3057,7 @@ class PDFSecurity {
|
|
|
1484
3057
|
}
|
|
1485
3058
|
infoStr += `${key}: ${info[key].valueOf()}\n`;
|
|
1486
3059
|
}
|
|
1487
|
-
return md5Hash(infoStr);
|
|
3060
|
+
return md5Hash(new TextEncoder().encode(infoStr));
|
|
1488
3061
|
}
|
|
1489
3062
|
static generateRandomWordArray(bytes) {
|
|
1490
3063
|
return randomBytes(bytes);
|
|
@@ -1619,7 +3192,7 @@ class PDFSecurity {
|
|
|
1619
3192
|
let digest;
|
|
1620
3193
|
if (this.version < 5) {
|
|
1621
3194
|
const suffix = new Uint8Array([obj & 0xff, obj >> 8 & 0xff, obj >> 16 & 0xff, gen & 0xff, gen >> 8 & 0xff]);
|
|
1622
|
-
digest =
|
|
3195
|
+
digest = concatBytes$1(this.encryptionKey, suffix);
|
|
1623
3196
|
}
|
|
1624
3197
|
if (this.version === 1 || this.version === 2) {
|
|
1625
3198
|
let key = md5Hash(digest);
|
|
@@ -1630,14 +3203,14 @@ class PDFSecurity {
|
|
|
1630
3203
|
let key;
|
|
1631
3204
|
if (this.version === 4) {
|
|
1632
3205
|
const saltMarker = new Uint8Array([0x73, 0x41, 0x6c, 0x54]);
|
|
1633
|
-
key = md5Hash(
|
|
3206
|
+
key = md5Hash(concatBytes$1(digest, saltMarker));
|
|
1634
3207
|
} else {
|
|
1635
3208
|
key = this.encryptionKey;
|
|
1636
3209
|
}
|
|
1637
3210
|
const iv = PDFSecurity.generateRandomWordArray(16);
|
|
1638
3211
|
return buffer => {
|
|
1639
3212
|
const encrypted = aesCbcEncrypt(buffer, key, iv, true);
|
|
1640
|
-
return
|
|
3213
|
+
return concatBytes$1(iv, encrypted);
|
|
1641
3214
|
};
|
|
1642
3215
|
}
|
|
1643
3216
|
end() {
|
|
@@ -1693,7 +3266,7 @@ function getUserPasswordR2(encryptionKey) {
|
|
|
1693
3266
|
}
|
|
1694
3267
|
function getUserPasswordR3R4(documentId, encryptionKey) {
|
|
1695
3268
|
const key = encryptionKey.slice();
|
|
1696
|
-
let cipher = md5Hash(
|
|
3269
|
+
let cipher = md5Hash(concatBytes$1(processPasswordR2R3R4(), new Uint8Array(documentId)));
|
|
1697
3270
|
for (let i = 0; i < 20; i++) {
|
|
1698
3271
|
const xorKey = new Uint8Array(key.length);
|
|
1699
3272
|
for (let j = 0; j < key.length; j++) {
|
|
@@ -1726,7 +3299,7 @@ function getOwnerPasswordR2R3R4(r, keyBits, paddedUserPassword, paddedOwnerPassw
|
|
|
1726
3299
|
}
|
|
1727
3300
|
function getEncryptionKeyR2R3R4(r, keyBits, documentId, paddedUserPassword, ownerPasswordEntry, permissions) {
|
|
1728
3301
|
const permBytes = new Uint8Array([permissions & 0xff, permissions >> 8 & 0xff, permissions >> 16 & 0xff, permissions >> 24 & 0xff]);
|
|
1729
|
-
let key =
|
|
3302
|
+
let key = concatBytes$1(paddedUserPassword, ownerPasswordEntry, permBytes, new Uint8Array(documentId));
|
|
1730
3303
|
const round = r >= 3 ? 51 : 1;
|
|
1731
3304
|
const keyLen = keyBits / 8;
|
|
1732
3305
|
for (let i = 0; i < round; i++) {
|
|
@@ -1738,22 +3311,22 @@ function getEncryptionKeyR2R3R4(r, keyBits, documentId, paddedUserPassword, owne
|
|
|
1738
3311
|
function getUserPasswordR5(processedUserPassword, generateRandomWordArray) {
|
|
1739
3312
|
const validationSalt = generateRandomWordArray(8);
|
|
1740
3313
|
const keySalt = generateRandomWordArray(8);
|
|
1741
|
-
const hash = sha256Hash(
|
|
1742
|
-
return
|
|
3314
|
+
const hash = sha256Hash(concatBytes$1(processedUserPassword, validationSalt));
|
|
3315
|
+
return concatBytes$1(hash, validationSalt, keySalt);
|
|
1743
3316
|
}
|
|
1744
3317
|
function getUserEncryptionKeyR5(processedUserPassword, userKeySalt, encryptionKey) {
|
|
1745
|
-
const key = sha256Hash(
|
|
3318
|
+
const key = sha256Hash(concatBytes$1(processedUserPassword, userKeySalt));
|
|
1746
3319
|
const iv = new Uint8Array(16);
|
|
1747
3320
|
return aesCbcEncrypt(encryptionKey, key, iv, false);
|
|
1748
3321
|
}
|
|
1749
3322
|
function getOwnerPasswordR5(processedOwnerPassword, userPasswordEntry, generateRandomWordArray) {
|
|
1750
3323
|
const validationSalt = generateRandomWordArray(8);
|
|
1751
3324
|
const keySalt = generateRandomWordArray(8);
|
|
1752
|
-
const hash = sha256Hash(
|
|
1753
|
-
return
|
|
3325
|
+
const hash = sha256Hash(concatBytes$1(processedOwnerPassword, validationSalt, userPasswordEntry));
|
|
3326
|
+
return concatBytes$1(hash, validationSalt, keySalt);
|
|
1754
3327
|
}
|
|
1755
3328
|
function getOwnerEncryptionKeyR5(processedOwnerPassword, ownerKeySalt, userPasswordEntry, encryptionKey) {
|
|
1756
|
-
const key = sha256Hash(
|
|
3329
|
+
const key = sha256Hash(concatBytes$1(processedOwnerPassword, ownerKeySalt, userPasswordEntry));
|
|
1757
3330
|
const iv = new Uint8Array(16);
|
|
1758
3331
|
return aesCbcEncrypt(encryptionKey, key, iv, false);
|
|
1759
3332
|
}
|
|
@@ -1807,6 +3380,104 @@ function processPasswordR5(password = '') {
|
|
|
1807
3380
|
}
|
|
1808
3381
|
const PASSWORD_PADDING = [0x28, 0xbf, 0x4e, 0x5e, 0x4e, 0x75, 0x8a, 0x41, 0x64, 0x00, 0x4e, 0x56, 0xff, 0xfa, 0x01, 0x08, 0x2e, 0x2e, 0x00, 0xb6, 0xd0, 0x68, 0x3e, 0x80, 0x2f, 0x0c, 0xa9, 0xfe, 0x64, 0x53, 0x69, 0x7a];
|
|
1809
3382
|
|
|
3383
|
+
const OBJECTS_PER_STREAM = 200;
|
|
3384
|
+
const ENTRY_WIDTHS = [1, 4, 2];
|
|
3385
|
+
const ENTRY_SIZE = 7;
|
|
3386
|
+
const FREE = 0;
|
|
3387
|
+
const UNCOMPRESSED = 1;
|
|
3388
|
+
const COMPRESSED = 2;
|
|
3389
|
+
class PDFObjectStreams {
|
|
3390
|
+
constructor(document, options) {
|
|
3391
|
+
if (document.version < 1.5) {
|
|
3392
|
+
throw new Error("The objectStreams option requires a pdfVersion of '1.5' or higher");
|
|
3393
|
+
}
|
|
3394
|
+
if (/^PDF\/A-1/.test(options.subset)) {
|
|
3395
|
+
throw new Error('PDF/A-1 does not allow object streams');
|
|
3396
|
+
}
|
|
3397
|
+
if (options.userPassword || options.ownerPassword) {
|
|
3398
|
+
throw new Error('The objectStreams option cannot be used with encryption');
|
|
3399
|
+
}
|
|
3400
|
+
this.document = document;
|
|
3401
|
+
this.pending = [];
|
|
3402
|
+
this.locations = new Map();
|
|
3403
|
+
}
|
|
3404
|
+
add(ref) {
|
|
3405
|
+
this.pending.push({
|
|
3406
|
+
id: ref.id,
|
|
3407
|
+
body: PDFObject.convert(ref.data)
|
|
3408
|
+
});
|
|
3409
|
+
if (this.pending.length >= OBJECTS_PER_STREAM) {
|
|
3410
|
+
this.flush();
|
|
3411
|
+
}
|
|
3412
|
+
}
|
|
3413
|
+
flush() {
|
|
3414
|
+
if (!this.pending.length) {
|
|
3415
|
+
return;
|
|
3416
|
+
}
|
|
3417
|
+
const objects = this.pending;
|
|
3418
|
+
this.pending = [];
|
|
3419
|
+
let offset = 0;
|
|
3420
|
+
const pairs = [];
|
|
3421
|
+
for (const {
|
|
3422
|
+
id,
|
|
3423
|
+
body
|
|
3424
|
+
} of objects) {
|
|
3425
|
+
pairs.push(id, offset);
|
|
3426
|
+
offset += body.length + 1;
|
|
3427
|
+
}
|
|
3428
|
+
const header = pairs.join(' ') + '\n';
|
|
3429
|
+
const stream = this.document.ref({
|
|
3430
|
+
Type: 'ObjStm',
|
|
3431
|
+
N: objects.length,
|
|
3432
|
+
First: header.length
|
|
3433
|
+
});
|
|
3434
|
+
stream.end(fromBinaryString(header + objects.map(o => o.body).join('\n')));
|
|
3435
|
+
objects.forEach(({
|
|
3436
|
+
id
|
|
3437
|
+
}, index) => {
|
|
3438
|
+
this.locations.set(id, [stream.id, index]);
|
|
3439
|
+
});
|
|
3440
|
+
}
|
|
3441
|
+
finalize() {
|
|
3442
|
+
const doc = this.document;
|
|
3443
|
+
this.flush();
|
|
3444
|
+
const xref = doc.ref({
|
|
3445
|
+
Type: 'XRef',
|
|
3446
|
+
Root: doc._root,
|
|
3447
|
+
Info: doc._info,
|
|
3448
|
+
ID: [doc._id, doc._id]
|
|
3449
|
+
});
|
|
3450
|
+
const xrefOffset = doc._offset;
|
|
3451
|
+
if (xrefOffset > 0xffffffff) {
|
|
3452
|
+
throw new Error('The objectStreams option cannot write files over 4 GB');
|
|
3453
|
+
}
|
|
3454
|
+
doc._offsets[xref.id - 1] = xrefOffset;
|
|
3455
|
+
const size = doc._offsets.length + 1;
|
|
3456
|
+
const table = new DataView(new ArrayBuffer(size * ENTRY_SIZE));
|
|
3457
|
+
const put = (id, type, field2, field3) => {
|
|
3458
|
+
const start = id * ENTRY_SIZE;
|
|
3459
|
+
table.setUint8(start, type);
|
|
3460
|
+
table.setUint32(start + 1, field2);
|
|
3461
|
+
table.setUint16(start + 5, field3);
|
|
3462
|
+
};
|
|
3463
|
+
put(0, FREE, 0, 0xffff);
|
|
3464
|
+
for (let id = 1; id < size; id++) {
|
|
3465
|
+
const location = this.locations.get(id);
|
|
3466
|
+
if (location) {
|
|
3467
|
+
put(id, COMPRESSED, ...location);
|
|
3468
|
+
} else {
|
|
3469
|
+
put(id, UNCOMPRESSED, doc._offsets[id - 1], 0);
|
|
3470
|
+
}
|
|
3471
|
+
}
|
|
3472
|
+
xref.data.Size = size;
|
|
3473
|
+
xref.data.W = ENTRY_WIDTHS;
|
|
3474
|
+
xref.end(new Uint8Array(table.buffer));
|
|
3475
|
+
doc._write('startxref');
|
|
3476
|
+
doc._write(`${xrefOffset}`);
|
|
3477
|
+
doc._write('%%EOF');
|
|
3478
|
+
}
|
|
3479
|
+
}
|
|
3480
|
+
|
|
1810
3481
|
const {
|
|
1811
3482
|
number: number$2
|
|
1812
3483
|
} = PDFObject;
|
|
@@ -1845,16 +3516,16 @@ let PDFGradient$1 = class PDFGradient {
|
|
|
1845
3516
|
}
|
|
1846
3517
|
embed(m) {
|
|
1847
3518
|
let fn;
|
|
1848
|
-
|
|
1849
|
-
if (stopsLength === 0) {
|
|
3519
|
+
if (this.stops.length === 0) {
|
|
1850
3520
|
return;
|
|
1851
3521
|
}
|
|
1852
3522
|
this.embedded = true;
|
|
1853
3523
|
this.matrix = m;
|
|
1854
|
-
const last = this.stops[
|
|
3524
|
+
const last = this.stops[this.stops.length - 1];
|
|
1855
3525
|
if (last[0] < 1) {
|
|
1856
3526
|
this.stops.push([1, last[1], last[2]]);
|
|
1857
3527
|
}
|
|
3528
|
+
const stopsLength = this.stops.length;
|
|
1858
3529
|
const bounds = [];
|
|
1859
3530
|
const encode = [];
|
|
1860
3531
|
const stops = [];
|
|
@@ -2638,18 +4309,15 @@ const runners = {
|
|
|
2638
4309
|
return cy += a[3];
|
|
2639
4310
|
},
|
|
2640
4311
|
Q(doc, a) {
|
|
4312
|
+
doc.bezierCurveTo(cx + 2 / 3 * (a[0] - cx), cy + 2 / 3 * (a[1] - cy), a[2] + 2 / 3 * (a[0] - a[2]), a[3] + 2 / 3 * (a[1] - a[3]), a[2], a[3]);
|
|
2641
4313
|
px = a[0];
|
|
2642
4314
|
py = a[1];
|
|
2643
4315
|
cx = a[2];
|
|
2644
4316
|
cy = a[3];
|
|
2645
|
-
return doc
|
|
4317
|
+
return doc;
|
|
2646
4318
|
},
|
|
2647
4319
|
q(doc, a) {
|
|
2648
|
-
|
|
2649
|
-
px = cx + a[0];
|
|
2650
|
-
py = cy + a[1];
|
|
2651
|
-
cx += a[2];
|
|
2652
|
-
return cy += a[3];
|
|
4320
|
+
return this.Q(doc, [a[0] + cx, a[1] + cy, a[2] + cx, a[3] + cy]);
|
|
2653
4321
|
},
|
|
2654
4322
|
T(doc, a) {
|
|
2655
4323
|
if (px === null) {
|
|
@@ -2659,23 +4327,10 @@ const runners = {
|
|
|
2659
4327
|
px = cx - (px - cx);
|
|
2660
4328
|
py = cy - (py - cy);
|
|
2661
4329
|
}
|
|
2662
|
-
|
|
2663
|
-
px = cx - (px - cx);
|
|
2664
|
-
py = cy - (py - cy);
|
|
2665
|
-
cx = a[0];
|
|
2666
|
-
return cy = a[1];
|
|
4330
|
+
return this.Q(doc, [px, py, a[0], a[1]]);
|
|
2667
4331
|
},
|
|
2668
4332
|
t(doc, a) {
|
|
2669
|
-
|
|
2670
|
-
px = cx;
|
|
2671
|
-
py = cy;
|
|
2672
|
-
} else {
|
|
2673
|
-
px = cx - (px - cx);
|
|
2674
|
-
py = cy - (py - cy);
|
|
2675
|
-
}
|
|
2676
|
-
doc.quadraticCurveTo(px, py, cx + a[0], cy + a[1]);
|
|
2677
|
-
cx += a[0];
|
|
2678
|
-
return cy += a[1];
|
|
4333
|
+
return this.T(doc, [cx + a[0], cy + a[1]]);
|
|
2679
4334
|
},
|
|
2680
4335
|
A(doc, a) {
|
|
2681
4336
|
solveArc(doc, cx, cy, a);
|
|
@@ -3174,8 +4829,12 @@ const WIN_ANSI_MAP = {
|
|
|
3174
4829
|
353: 154,
|
|
3175
4830
|
376: 159,
|
|
3176
4831
|
381: 142,
|
|
3177
|
-
382: 158
|
|
4832
|
+
382: 158,
|
|
4833
|
+
8208: 45,
|
|
4834
|
+
8209: 45,
|
|
4835
|
+
8239: 160
|
|
3178
4836
|
};
|
|
4837
|
+
const outputCode = charCode => WIN_ANSI_MAP[charCode] || charCode;
|
|
3179
4838
|
const characters = `\
|
|
3180
4839
|
.notdef .notdef .notdef .notdef
|
|
3181
4840
|
.notdef .notdef .notdef .notdef
|
|
@@ -3260,6 +4919,7 @@ class AFMFont {
|
|
|
3260
4919
|
this.lineGap = this.bbox[3] - this.bbox[1] - (this.ascender - this.descender);
|
|
3261
4920
|
const glyphNames = data.glyphNames.split(' ');
|
|
3262
4921
|
this.glyphWidths = Object.fromEntries(glyphNames.map((name, index) => [name, data.glyphWidths[index]]));
|
|
4922
|
+
this.builtInEncoding = data.glyphCodes ? Object.fromEntries(data.glyphCodes.map((code, index) => [code, glyphNames[index]])) : null;
|
|
3263
4923
|
this.kernPairs = {};
|
|
3264
4924
|
for (let index = 0; index < data.kernPairs.length; index += 2) {
|
|
3265
4925
|
const amount = data.kernPairs[index];
|
|
@@ -3274,23 +4934,31 @@ class AFMFont {
|
|
|
3274
4934
|
}
|
|
3275
4935
|
encodeText(text) {
|
|
3276
4936
|
const res = [];
|
|
3277
|
-
|
|
3278
|
-
|
|
3279
|
-
|
|
3280
|
-
|
|
4937
|
+
let i = 0;
|
|
4938
|
+
while (i < text.length) {
|
|
4939
|
+
const codePoint = text.codePointAt(i);
|
|
4940
|
+
const char = outputCode(codePoint);
|
|
4941
|
+
res.push(char.toString(16).padStart(2, '0'));
|
|
4942
|
+
i += codePoint > 0xffff ? 2 : 1;
|
|
3281
4943
|
}
|
|
3282
4944
|
return res;
|
|
3283
4945
|
}
|
|
3284
4946
|
glyphsForString(string) {
|
|
3285
4947
|
const glyphs = [];
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
4948
|
+
let i = 0;
|
|
4949
|
+
while (i < string.length) {
|
|
4950
|
+
const codePoint = string.codePointAt(i);
|
|
4951
|
+
glyphs.push(this.characterToGlyph(codePoint));
|
|
4952
|
+
i += codePoint > 0xffff ? 2 : 1;
|
|
3289
4953
|
}
|
|
3290
4954
|
return glyphs;
|
|
3291
4955
|
}
|
|
3292
4956
|
characterToGlyph(character) {
|
|
3293
|
-
|
|
4957
|
+
const code = outputCode(character);
|
|
4958
|
+
if (this.builtInEncoding) {
|
|
4959
|
+
return this.builtInEncoding[code] || '.notdef';
|
|
4960
|
+
}
|
|
4961
|
+
return characters[code] || '.notdef';
|
|
3294
4962
|
}
|
|
3295
4963
|
widthOfGlyph(glyph) {
|
|
3296
4964
|
return this.glyphWidths[glyph] || 0;
|
|
@@ -3336,13 +5004,18 @@ class PDFFont {
|
|
|
3336
5004
|
}
|
|
3337
5005
|
}
|
|
3338
5006
|
|
|
5007
|
+
const afmFonts = new WeakMap();
|
|
3339
5008
|
class StandardFont extends PDFFont {
|
|
3340
5009
|
constructor(document, fontData, id) {
|
|
3341
5010
|
super();
|
|
3342
5011
|
this.document = document;
|
|
3343
5012
|
this.name = fontData.name;
|
|
3344
5013
|
this.id = id;
|
|
3345
|
-
this.font =
|
|
5014
|
+
this.font = afmFonts.get(fontData);
|
|
5015
|
+
if (!this.font) {
|
|
5016
|
+
this.font = new AFMFont(fontData);
|
|
5017
|
+
afmFonts.set(fontData, this.font);
|
|
5018
|
+
}
|
|
3346
5019
|
({
|
|
3347
5020
|
ascender: this.ascender,
|
|
3348
5021
|
descender: this.descender,
|
|
@@ -3393,6 +5066,23 @@ class StandardFont extends PDFFont {
|
|
|
3393
5066
|
const toHex = function (num) {
|
|
3394
5067
|
return `0000${num.toString(16)}`.slice(-4);
|
|
3395
5068
|
};
|
|
5069
|
+
const WIN_ANSI_CODE_TO_UNICODE = Object.fromEntries(Object.entries(WIN_ANSI_MAP).map(([codePoint, code]) => [code, Number(codePoint)]));
|
|
5070
|
+
const UNDEFINED_WIN_ANSI_CODES = new Set([129, 141, 143, 144, 157]);
|
|
5071
|
+
const FIRST_WIN_ANSI_CHAR = 32;
|
|
5072
|
+
const LAST_WIN_ANSI_CHAR = 255;
|
|
5073
|
+
function unicodeForWinAnsiCode(code) {
|
|
5074
|
+
if (UNDEFINED_WIN_ANSI_CODES.has(code)) {
|
|
5075
|
+
return null;
|
|
5076
|
+
}
|
|
5077
|
+
return WIN_ANSI_CODE_TO_UNICODE[code] ?? code;
|
|
5078
|
+
}
|
|
5079
|
+
function completeSubsetOf(font) {
|
|
5080
|
+
const subset = font.createSubset();
|
|
5081
|
+
for (let gid = 0; gid < font.numGlyphs; gid++) {
|
|
5082
|
+
subset.includeGlyph(gid);
|
|
5083
|
+
}
|
|
5084
|
+
return subset;
|
|
5085
|
+
}
|
|
3396
5086
|
class EmbeddedFont extends PDFFont {
|
|
3397
5087
|
constructor(document, font, id) {
|
|
3398
5088
|
super();
|
|
@@ -3490,13 +5180,31 @@ class EmbeddedFont extends PDFFont {
|
|
|
3490
5180
|
const scale = size / 1000;
|
|
3491
5181
|
return width * scale;
|
|
3492
5182
|
}
|
|
3493
|
-
|
|
3494
|
-
|
|
5183
|
+
completeRef() {
|
|
5184
|
+
return this.completeDictionary != null ? this.completeDictionary : this.completeDictionary = this.document.ref();
|
|
5185
|
+
}
|
|
5186
|
+
finalize() {
|
|
5187
|
+
if (this.embedded) {
|
|
5188
|
+
return;
|
|
5189
|
+
}
|
|
5190
|
+
if (this.dictionary != null) {
|
|
5191
|
+
this.embed();
|
|
5192
|
+
}
|
|
5193
|
+
if (this.completeDictionary != null) {
|
|
5194
|
+
this.embedComplete();
|
|
5195
|
+
}
|
|
5196
|
+
this.embedded = true;
|
|
5197
|
+
}
|
|
5198
|
+
embedProgram(subset, complete) {
|
|
5199
|
+
const isCFF = subset.cff != null;
|
|
5200
|
+
const fontProgram = subset.encode();
|
|
3495
5201
|
const fontFile = this.document.ref();
|
|
3496
5202
|
if (isCFF) {
|
|
3497
5203
|
fontFile.data.Subtype = 'CIDFontType0C';
|
|
5204
|
+
} else if (complete) {
|
|
5205
|
+
fontFile.data.Length1 = fontProgram.length;
|
|
3498
5206
|
}
|
|
3499
|
-
fontFile.end(
|
|
5207
|
+
fontFile.end(fontProgram);
|
|
3500
5208
|
const familyClass = ((this.font['OS/2'] != null ? this.font['OS/2'].sFamilyClass : undefined) || 0) >> 8;
|
|
3501
5209
|
let flags = 0;
|
|
3502
5210
|
if (this.font.post.isFixedPitch) {
|
|
@@ -3512,8 +5220,9 @@ class EmbeddedFont extends PDFFont {
|
|
|
3512
5220
|
if (this.font.head.macStyle.italic) {
|
|
3513
5221
|
flags |= 1 << 6;
|
|
3514
5222
|
}
|
|
5223
|
+
const postscriptName = this.font.postscriptName?.replaceAll(' ', '_');
|
|
3515
5224
|
const tag = [1, 2, 3, 4, 5, 6].map(i => String.fromCharCode((this.id.charCodeAt(i) || 73) + 17)).join('');
|
|
3516
|
-
const name =
|
|
5225
|
+
const name = complete ? postscriptName : `${tag}+${postscriptName}`;
|
|
3517
5226
|
const {
|
|
3518
5227
|
bbox
|
|
3519
5228
|
} = this.font;
|
|
@@ -3534,7 +5243,7 @@ class EmbeddedFont extends PDFFont {
|
|
|
3534
5243
|
} else {
|
|
3535
5244
|
descriptor.data.FontFile2 = fontFile;
|
|
3536
5245
|
}
|
|
3537
|
-
if (this.document.subset && this.document.subset === 1) {
|
|
5246
|
+
if (!complete && this.document.subset && this.document.subset === 1) {
|
|
3538
5247
|
const maxCID = this.widths.length - 1;
|
|
3539
5248
|
const cidSetBuffer = new Uint8Array(Math.ceil((maxCID + 1) / 8));
|
|
3540
5249
|
for (let cid = 0; cid <= maxCID; cid++) {
|
|
@@ -3548,6 +5257,18 @@ class EmbeddedFont extends PDFFont {
|
|
|
3548
5257
|
descriptor.data.CIDSet = CIDSetRef;
|
|
3549
5258
|
}
|
|
3550
5259
|
descriptor.end();
|
|
5260
|
+
return {
|
|
5261
|
+
descriptor,
|
|
5262
|
+
name,
|
|
5263
|
+
isCFF
|
|
5264
|
+
};
|
|
5265
|
+
}
|
|
5266
|
+
embed() {
|
|
5267
|
+
const {
|
|
5268
|
+
descriptor,
|
|
5269
|
+
name,
|
|
5270
|
+
isCFF
|
|
5271
|
+
} = this.embedProgram(this.subset, false);
|
|
3551
5272
|
const descendantFontData = {
|
|
3552
5273
|
Type: 'Font',
|
|
3553
5274
|
Subtype: 'CIDFontType0',
|
|
@@ -3576,6 +5297,84 @@ class EmbeddedFont extends PDFFont {
|
|
|
3576
5297
|
};
|
|
3577
5298
|
return this.dictionary.end();
|
|
3578
5299
|
}
|
|
5300
|
+
embedComplete() {
|
|
5301
|
+
const {
|
|
5302
|
+
descriptor,
|
|
5303
|
+
name,
|
|
5304
|
+
isCFF
|
|
5305
|
+
} = this.embedProgram(completeSubsetOf(this.font), true);
|
|
5306
|
+
const widths = [];
|
|
5307
|
+
for (let gid = 0; gid < this.font.numGlyphs; gid++) {
|
|
5308
|
+
widths.push(this.font.getGlyph(gid).advanceWidth * this.scale);
|
|
5309
|
+
}
|
|
5310
|
+
const descendantFontData = {
|
|
5311
|
+
Type: 'Font',
|
|
5312
|
+
Subtype: isCFF ? 'CIDFontType0' : 'CIDFontType2',
|
|
5313
|
+
BaseFont: name,
|
|
5314
|
+
CIDSystemInfo: {
|
|
5315
|
+
Registry: new String('Adobe'),
|
|
5316
|
+
Ordering: new String('Identity'),
|
|
5317
|
+
Supplement: 0
|
|
5318
|
+
},
|
|
5319
|
+
FontDescriptor: descriptor,
|
|
5320
|
+
W: [0, widths]
|
|
5321
|
+
};
|
|
5322
|
+
if (!isCFF) {
|
|
5323
|
+
descendantFontData.CIDToGIDMap = 'Identity';
|
|
5324
|
+
}
|
|
5325
|
+
const descendantFont = this.document.ref(descendantFontData);
|
|
5326
|
+
descendantFont.end();
|
|
5327
|
+
this.completeDictionary.data = {
|
|
5328
|
+
Type: 'Font',
|
|
5329
|
+
Subtype: 'Type0',
|
|
5330
|
+
BaseFont: name,
|
|
5331
|
+
Encoding: this.winAnsiToGidCmap(),
|
|
5332
|
+
DescendantFonts: [descendantFont]
|
|
5333
|
+
};
|
|
5334
|
+
return this.completeDictionary.end();
|
|
5335
|
+
}
|
|
5336
|
+
winAnsiToGidCmap() {
|
|
5337
|
+
const cmap = this.document.ref();
|
|
5338
|
+
cmap.data.Type = 'CMap';
|
|
5339
|
+
const entries = [];
|
|
5340
|
+
for (let code = FIRST_WIN_ANSI_CHAR; code <= LAST_WIN_ANSI_CHAR; code++) {
|
|
5341
|
+
const codePoint = unicodeForWinAnsiCode(code);
|
|
5342
|
+
if (codePoint == null || !this.font.hasGlyphForCodePoint(codePoint)) {
|
|
5343
|
+
continue;
|
|
5344
|
+
}
|
|
5345
|
+
const gid = this.font.glyphForCodePoint(codePoint).id;
|
|
5346
|
+
entries.push(`<${code.toString(16).padStart(2, '0')}> ${gid}`);
|
|
5347
|
+
}
|
|
5348
|
+
const chunkSize = 100;
|
|
5349
|
+
const chunks = Math.ceil(entries.length / chunkSize);
|
|
5350
|
+
const ranges = [];
|
|
5351
|
+
for (let i = 0; i < chunks; i++) {
|
|
5352
|
+
const start = i * chunkSize;
|
|
5353
|
+
const end = Math.min((i + 1) * chunkSize, entries.length);
|
|
5354
|
+
ranges.push(`${end - start} begincidchar\n${entries.slice(start, end).join('\n')}\nendcidchar`);
|
|
5355
|
+
}
|
|
5356
|
+
cmap.end(`\
|
|
5357
|
+
/CIDInit /ProcSet findresource begin
|
|
5358
|
+
12 dict begin
|
|
5359
|
+
begincmap
|
|
5360
|
+
/CIDSystemInfo <<
|
|
5361
|
+
/Registry (Adobe)
|
|
5362
|
+
/Ordering (Identity)
|
|
5363
|
+
/Supplement 0
|
|
5364
|
+
>> def
|
|
5365
|
+
/CMapName /Adobe-Identity-WinAnsi def
|
|
5366
|
+
/CMapType 1 def
|
|
5367
|
+
1 begincodespacerange
|
|
5368
|
+
<20> <ff>
|
|
5369
|
+
endcodespacerange
|
|
5370
|
+
${ranges.join('\n')}
|
|
5371
|
+
endcmap
|
|
5372
|
+
CMapName currentdict /CMap defineresource pop
|
|
5373
|
+
end
|
|
5374
|
+
end\
|
|
5375
|
+
`);
|
|
5376
|
+
return cmap;
|
|
5377
|
+
}
|
|
3579
5378
|
toUnicodeCmap() {
|
|
3580
5379
|
const cmap = this.document.ref();
|
|
3581
5380
|
const entries = [];
|
|
@@ -3634,6 +5433,12 @@ const validateFontData = (fontData, expectedName) => {
|
|
|
3634
5433
|
};
|
|
3635
5434
|
const standardFonts = new Map();
|
|
3636
5435
|
const isStandardFont = name => STANDARD_FONT_NAME_SET.has(name);
|
|
5436
|
+
const registerStdFonts = (...fontData) => {
|
|
5437
|
+
for (const data of fontData) {
|
|
5438
|
+
validateFontData(data);
|
|
5439
|
+
standardFonts.set(data.name, data);
|
|
5440
|
+
}
|
|
5441
|
+
};
|
|
3637
5442
|
const registerStdFontLoaders = loaders => {
|
|
3638
5443
|
for (const [name, loader] of Object.entries(loaders)) {
|
|
3639
5444
|
if (!STANDARD_FONT_NAME_SET.has(name) || typeof loader !== 'function') {
|
|
@@ -3694,6 +5499,7 @@ const isEqualFont = (font1, font2) => {
|
|
|
3694
5499
|
var FontsMixin = {
|
|
3695
5500
|
initFonts(defaultFont = 'Helvetica', defaultFontFamily = null, defaultFontSize = 12) {
|
|
3696
5501
|
this._fontFamilies = {};
|
|
5502
|
+
this._fontsBySource = new WeakMap();
|
|
3697
5503
|
this._fontCount = 0;
|
|
3698
5504
|
this._fontSource = defaultFont;
|
|
3699
5505
|
this._fontFamily = defaultFontFamily;
|
|
@@ -3732,12 +5538,19 @@ var FontsMixin = {
|
|
|
3732
5538
|
this._font = font;
|
|
3733
5539
|
return this;
|
|
3734
5540
|
}
|
|
5541
|
+
const bySource = !family && src !== null && typeof src === 'object';
|
|
5542
|
+
if (bySource && (font = this._fontsBySource.get(src))) {
|
|
5543
|
+
this._font = font;
|
|
5544
|
+
return this;
|
|
5545
|
+
}
|
|
3735
5546
|
const id = `F${++this._fontCount}`;
|
|
3736
5547
|
this._font = PDFFontFactory.open(this, src, family, id);
|
|
3737
5548
|
if ((font = this._fontFamilies[this._font.name]) && isEqualFont(this._font, font)) {
|
|
3738
5549
|
this._font = font;
|
|
5550
|
+
if (bySource) this._fontsBySource.set(src, font);
|
|
3739
5551
|
return this;
|
|
3740
5552
|
}
|
|
5553
|
+
if (bySource) this._fontsBySource.set(src, this._font);
|
|
3741
5554
|
if (cacheKey) {
|
|
3742
5555
|
this._fontFamilies[cacheKey] = this._font;
|
|
3743
5556
|
}
|
|
@@ -3856,6 +5669,8 @@ class EventEmitter {
|
|
|
3856
5669
|
|
|
3857
5670
|
const SOFT_HYPHEN = '\u00AD';
|
|
3858
5671
|
const HYPHEN = '-';
|
|
5672
|
+
const WHITESPACE_BREAK = /[\s\u00AD\u200B]/;
|
|
5673
|
+
const isWhitespaceBreak = (text, bk) => bk.required || bk.position >= text.length || WHITESPACE_BREAK.test(text[bk.position - 1]);
|
|
3859
5674
|
class LineWrapper extends EventEmitter {
|
|
3860
5675
|
constructor(document, options) {
|
|
3861
5676
|
super();
|
|
@@ -3873,6 +5688,7 @@ class LineWrapper extends EventEmitter {
|
|
|
3873
5688
|
this.startY = this.document.y;
|
|
3874
5689
|
this.column = 1;
|
|
3875
5690
|
this.ellipsis = options.ellipsis;
|
|
5691
|
+
this.breakAtWhitespaceOnly = options.lineBreak === 'whitespace';
|
|
3876
5692
|
this.continuedX = 0;
|
|
3877
5693
|
this.indentApplied = false;
|
|
3878
5694
|
this.features = options.features;
|
|
@@ -3937,10 +5753,13 @@ class LineWrapper extends EventEmitter {
|
|
|
3937
5753
|
}
|
|
3938
5754
|
eachWord(text, fn) {
|
|
3939
5755
|
let bk;
|
|
3940
|
-
const breaker = new LineBreaker(text);
|
|
5756
|
+
const breaker = new linebreak.LineBreaker(text);
|
|
3941
5757
|
let last = null;
|
|
3942
5758
|
const wordWidths = Object.create(null);
|
|
3943
5759
|
while (bk = breaker.nextBreak()) {
|
|
5760
|
+
if (this.breakAtWhitespaceOnly && !isWhitespaceBreak(text, bk)) {
|
|
5761
|
+
continue;
|
|
5762
|
+
}
|
|
3944
5763
|
var shouldContinue;
|
|
3945
5764
|
let word = text.slice((last != null ? last.position : undefined) || 0, bk.position);
|
|
3946
5765
|
let w = wordWidths[word] != null ? wordWidths[word] : wordWidths[word] = this.wordWidth(word);
|
|
@@ -3950,7 +5769,7 @@ class LineWrapper extends EventEmitter {
|
|
|
3950
5769
|
while (word.length) {
|
|
3951
5770
|
var l, mightGrow;
|
|
3952
5771
|
if (w > this.spaceLeft) {
|
|
3953
|
-
l = Math.ceil(this.spaceLeft / (w / word.length));
|
|
5772
|
+
l = Math.max(0, Math.ceil(this.spaceLeft / (w / word.length)));
|
|
3954
5773
|
w = this.wordWidth(word.slice(0, l));
|
|
3955
5774
|
mightGrow = w <= this.spaceLeft && l < word.length;
|
|
3956
5775
|
} else {
|
|
@@ -4007,6 +5826,9 @@ class LineWrapper extends EventEmitter {
|
|
|
4007
5826
|
if (options.ellipsis != null) {
|
|
4008
5827
|
this.ellipsis = options.ellipsis;
|
|
4009
5828
|
}
|
|
5829
|
+
if (options.lineBreak != null) {
|
|
5830
|
+
this.breakAtWhitespaceOnly = options.lineBreak === 'whitespace';
|
|
5831
|
+
}
|
|
4010
5832
|
const nextY = document.y + document.currentLineHeight(true);
|
|
4011
5833
|
if (document.y > this.maxY || nextY > this.maxY) {
|
|
4012
5834
|
this.nextSection();
|
|
@@ -4176,7 +5998,7 @@ var TextMixin = {
|
|
|
4176
5998
|
options = this._initOptions(x, y, options);
|
|
4177
5999
|
text = text == null ? '' : `${text}`;
|
|
4178
6000
|
if (options.wordSpacing) {
|
|
4179
|
-
text = text.replace(
|
|
6001
|
+
text = text.replace(/[^\S\n]{2,}/g, ' ');
|
|
4180
6002
|
}
|
|
4181
6003
|
const addStructure = () => {
|
|
4182
6004
|
if (options.structParent) {
|
|
@@ -4226,7 +6048,7 @@ var TextMixin = {
|
|
|
4226
6048
|
let contentWidth = 0;
|
|
4227
6049
|
string = String(string ?? '');
|
|
4228
6050
|
if (options.wordSpacing) {
|
|
4229
|
-
string = string.replace(
|
|
6051
|
+
string = string.replace(/[^\S\n]{2,}/g, ' ');
|
|
4230
6052
|
}
|
|
4231
6053
|
if (options.width) {
|
|
4232
6054
|
let wrapper = new LineWrapper(this, options);
|
|
@@ -4242,13 +6064,18 @@ var TextMixin = {
|
|
|
4242
6064
|
const spaceWidth = this.widthOfString(' ') + characterSpacing;
|
|
4243
6065
|
wordSpacing = Math.max(0, (options.lineWidth - textWidth) / Math.max(1, words.length - 1) - spaceWidth);
|
|
4244
6066
|
}
|
|
4245
|
-
contentWidth = Math.max(contentWidth,
|
|
6067
|
+
contentWidth = Math.max(contentWidth, this.widthOfString(text, options) + wordSpacing * (options.wordCount - 1));
|
|
4246
6068
|
}
|
|
4247
6069
|
});
|
|
4248
6070
|
wrapper.wrap(string, options);
|
|
4249
6071
|
} else {
|
|
4250
6072
|
for (let line of string.split('\n')) {
|
|
4251
|
-
|
|
6073
|
+
let lineWidth = this.widthOfString(line, options);
|
|
6074
|
+
if (options.wordSpacing) {
|
|
6075
|
+
const trimmed = line.trim();
|
|
6076
|
+
const space = /\s/g;
|
|
6077
|
+
while (space.test(trimmed)) lineWidth += options.wordSpacing;
|
|
6078
|
+
}
|
|
4252
6079
|
this.y += lineHeight;
|
|
4253
6080
|
contentWidth = Math.max(contentWidth, lineWidth);
|
|
4254
6081
|
}
|
|
@@ -4315,6 +6142,7 @@ var TextMixin = {
|
|
|
4315
6142
|
options = this._initOptions(options);
|
|
4316
6143
|
options.height = Infinity;
|
|
4317
6144
|
const lineGap = options.lineGap || this._lineGap || 0;
|
|
6145
|
+
if (!options.continued && options.width) this._wrapper = null;
|
|
4318
6146
|
this._text(text, this.x, this.y, options, () => {
|
|
4319
6147
|
this.y += this.currentLineHeight(true) + lineGap;
|
|
4320
6148
|
});
|
|
@@ -4331,12 +6159,19 @@ var TextMixin = {
|
|
|
4331
6159
|
const r = options.bulletRadius || unit / 3;
|
|
4332
6160
|
const indent = options.textIndent || (listType === 'bullet' ? r * 5 : unit * 2);
|
|
4333
6161
|
const itemIndent = options.bulletIndent || (listType === 'bullet' ? r * 8 : unit * 2);
|
|
6162
|
+
const {
|
|
6163
|
+
lineWidth,
|
|
6164
|
+
textWidth,
|
|
6165
|
+
wordCount,
|
|
6166
|
+
...labelOptions
|
|
6167
|
+
} = options;
|
|
6168
|
+
labelOptions.align = 'left';
|
|
4334
6169
|
let level = 1;
|
|
4335
6170
|
const items = [];
|
|
4336
6171
|
const levels = [];
|
|
4337
6172
|
const numbers = [];
|
|
4338
6173
|
var flatten = function (list) {
|
|
4339
|
-
let n = 1;
|
|
6174
|
+
let n = level === 1 ? options.start ?? 1 : 1;
|
|
4340
6175
|
for (let i = 0; i < list.length; i++) {
|
|
4341
6176
|
const item = list[i];
|
|
4342
6177
|
if (Array.isArray(item)) {
|
|
@@ -4390,7 +6225,7 @@ var TextMixin = {
|
|
|
4390
6225
|
case 'numbered':
|
|
4391
6226
|
case 'lettered':
|
|
4392
6227
|
var text = formatListLabel(numbers[i - 1], listType);
|
|
4393
|
-
this._fragment(text, this.x - indent, this.y,
|
|
6228
|
+
this._fragment(text, this.x - indent, this.y, labelOptions);
|
|
4394
6229
|
break;
|
|
4395
6230
|
}
|
|
4396
6231
|
if (item && labelType && bodyType) {
|
|
@@ -4478,10 +6313,15 @@ var TextMixin = {
|
|
|
4478
6313
|
switch (align) {
|
|
4479
6314
|
case 'right':
|
|
4480
6315
|
textWidth = this.widthOfString(text.replace(/\s+$/, ''), options);
|
|
6316
|
+
if (wordSpacing) {
|
|
6317
|
+
const gaps = text.trim().split(/\s+/).length - 1;
|
|
6318
|
+
textWidth += wordSpacing * gaps * horizontalScaling / 100;
|
|
6319
|
+
}
|
|
4481
6320
|
x += options.lineWidth - textWidth;
|
|
4482
6321
|
break;
|
|
4483
6322
|
case 'center':
|
|
4484
|
-
|
|
6323
|
+
textWidth = options.textWidth - (characterSpacing + wordSpacing * options.wordCount) * horizontalScaling / 100;
|
|
6324
|
+
x += options.lineWidth / 2 - textWidth / 2;
|
|
4485
6325
|
break;
|
|
4486
6326
|
case 'justify':
|
|
4487
6327
|
words = text.trim().split(/\s+/);
|
|
@@ -4523,7 +6363,8 @@ var TextMixin = {
|
|
|
4523
6363
|
}
|
|
4524
6364
|
dy = dy / 1000 * this._fontSize;
|
|
4525
6365
|
}
|
|
4526
|
-
const
|
|
6366
|
+
const lineTop = y + dy - this._font.ascender / 1000 * this._fontSize;
|
|
6367
|
+
const measuredWidth = this.widthOfString(text, options) - characterSpacing * (text.length - 1);
|
|
4527
6368
|
const measuredWordCount = options.wordCount ?? (text.trim() ? text.trim().split(/\s+/).length : 0);
|
|
4528
6369
|
const renderedWidth = measuredWidth + wordSpacing * (measuredWordCount - 1) + characterSpacing * (text.length - 1);
|
|
4529
6370
|
if (options.link != null) {
|
|
@@ -4531,10 +6372,10 @@ var TextMixin = {
|
|
|
4531
6372
|
if (this._currentStructureElement && this._currentStructureElement.dictionary.data.S === 'Link') {
|
|
4532
6373
|
linkOptions.structParent = this._currentStructureElement;
|
|
4533
6374
|
}
|
|
4534
|
-
this.link(x,
|
|
6375
|
+
this.link(x, lineTop, renderedWidth, this.currentLineHeight(), options.link, linkOptions);
|
|
4535
6376
|
}
|
|
4536
6377
|
if (options.goTo != null) {
|
|
4537
|
-
this.goTo(x,
|
|
6378
|
+
this.goTo(x, lineTop, renderedWidth, this.currentLineHeight(), options.goTo);
|
|
4538
6379
|
}
|
|
4539
6380
|
if (options.destination != null) {
|
|
4540
6381
|
this.addNamedDestination(options.destination, 'XYZ', x, y, null);
|
|
@@ -4544,9 +6385,9 @@ var TextMixin = {
|
|
|
4544
6385
|
if (!options.stroke) {
|
|
4545
6386
|
this.strokeColor(...(this._fillColor || []));
|
|
4546
6387
|
}
|
|
4547
|
-
const lineWidth = this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10);
|
|
6388
|
+
const lineWidth = options.decorationThickness ?? (this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10));
|
|
4548
6389
|
this.lineWidth(lineWidth);
|
|
4549
|
-
let lineY =
|
|
6390
|
+
let lineY = lineTop + this.currentLineHeight() - lineWidth + (options.underlineOffset ?? 0);
|
|
4550
6391
|
this.moveTo(x, lineY);
|
|
4551
6392
|
this.lineTo(x + renderedWidth, lineY);
|
|
4552
6393
|
this.stroke();
|
|
@@ -4557,9 +6398,9 @@ var TextMixin = {
|
|
|
4557
6398
|
if (!options.stroke) {
|
|
4558
6399
|
this.strokeColor(...(this._fillColor || []));
|
|
4559
6400
|
}
|
|
4560
|
-
const lineWidth = this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10);
|
|
6401
|
+
const lineWidth = options.decorationThickness ?? (this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10));
|
|
4561
6402
|
this.lineWidth(lineWidth);
|
|
4562
|
-
let lineY =
|
|
6403
|
+
let lineY = lineTop + this.currentLineHeight() / 2;
|
|
4563
6404
|
this.moveTo(x, lineY);
|
|
4564
6405
|
this.lineTo(x + renderedWidth, lineY);
|
|
4565
6406
|
this.stroke();
|
|
@@ -4820,12 +6661,11 @@ class PNGImage {
|
|
|
4820
6661
|
}
|
|
4821
6662
|
obj.data['Mask'] = mask;
|
|
4822
6663
|
} else if (image.transparency.indexed) {
|
|
4823
|
-
|
|
6664
|
+
this.loadIndexedAlphaChannel();
|
|
4824
6665
|
} else if (hasAlphaChannel) {
|
|
4825
|
-
|
|
4826
|
-
}
|
|
4827
|
-
|
|
4828
|
-
return this.decodeData();
|
|
6666
|
+
this.splitAlphaChannel();
|
|
6667
|
+
} else if (isInterlaced && true) {
|
|
6668
|
+
this.decodeData();
|
|
4829
6669
|
}
|
|
4830
6670
|
this.finalize();
|
|
4831
6671
|
}
|
|
@@ -4846,52 +6686,46 @@ class PNGImage {
|
|
|
4846
6686
|
}
|
|
4847
6687
|
this.obj.end(this.imgData);
|
|
4848
6688
|
this.image = null;
|
|
4849
|
-
|
|
6689
|
+
this.imgData = null;
|
|
4850
6690
|
}
|
|
4851
6691
|
splitAlphaChannel() {
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4863
|
-
|
|
4864
|
-
i += skipByteCount;
|
|
4865
|
-
}
|
|
4866
|
-
alphaChannel[a++] = pixels[i++];
|
|
6692
|
+
const pixels = this.image.decodePixelsSync();
|
|
6693
|
+
let a, p;
|
|
6694
|
+
const colorCount = this.image.colors;
|
|
6695
|
+
const pixelCount = this.width * this.height;
|
|
6696
|
+
const imgData = new Uint8Array(pixelCount * colorCount);
|
|
6697
|
+
const alphaChannel = new Uint8Array(pixelCount);
|
|
6698
|
+
let i = p = a = 0;
|
|
6699
|
+
const len = pixels.length;
|
|
6700
|
+
const skipByteCount = this.image.bits === 16 ? 1 : 0;
|
|
6701
|
+
while (i < len) {
|
|
6702
|
+
for (let colorIndex = 0; colorIndex < colorCount; colorIndex++) {
|
|
6703
|
+
imgData[p++] = pixels[i++];
|
|
4867
6704
|
i += skipByteCount;
|
|
4868
6705
|
}
|
|
4869
|
-
|
|
4870
|
-
|
|
4871
|
-
|
|
4872
|
-
|
|
6706
|
+
alphaChannel[a++] = pixels[i++];
|
|
6707
|
+
i += skipByteCount;
|
|
6708
|
+
}
|
|
6709
|
+
this.imgData = zlib.deflateSync(imgData);
|
|
6710
|
+
this.alphaChannel = zlib.deflateSync(alphaChannel);
|
|
4873
6711
|
}
|
|
4874
6712
|
loadIndexedAlphaChannel() {
|
|
4875
6713
|
const transparency = this.image.transparency.indexed;
|
|
4876
6714
|
const isInterlaced = this.image.interlaceMethod === 1;
|
|
4877
|
-
|
|
4878
|
-
|
|
4879
|
-
|
|
4880
|
-
|
|
4881
|
-
|
|
4882
|
-
|
|
4883
|
-
|
|
4884
|
-
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
return this.finalize();
|
|
4888
|
-
});
|
|
6715
|
+
const pixels = this.image.decodePixelsSync();
|
|
6716
|
+
const alphaChannel = new Uint8Array(this.width * this.height);
|
|
6717
|
+
let i = 0;
|
|
6718
|
+
for (let j = 0, end = pixels.length; j < end; j++) {
|
|
6719
|
+
alphaChannel[i++] = transparency[pixels[j]];
|
|
6720
|
+
}
|
|
6721
|
+
if (isInterlaced) {
|
|
6722
|
+
this.imgData = zlib.deflateSync(pixels);
|
|
6723
|
+
}
|
|
6724
|
+
this.alphaChannel = zlib.deflateSync(alphaChannel);
|
|
4889
6725
|
}
|
|
4890
6726
|
decodeData() {
|
|
4891
|
-
this.image.
|
|
4892
|
-
|
|
4893
|
-
this.finalize();
|
|
4894
|
-
});
|
|
6727
|
+
const pixels = this.image.decodePixelsSync();
|
|
6728
|
+
this.imgData = zlib.deflateSync(pixels);
|
|
4895
6729
|
}
|
|
4896
6730
|
}
|
|
4897
6731
|
|
|
@@ -4974,11 +6808,14 @@ var ImagesMixin = {
|
|
|
4974
6808
|
} else if (options.scale) {
|
|
4975
6809
|
w = width * options.scale;
|
|
4976
6810
|
h = height * options.scale;
|
|
4977
|
-
} else if (options.fit) {
|
|
4978
|
-
[bw, bh] = options.fit;
|
|
6811
|
+
} else if (options.fit || options.scaleDown) {
|
|
6812
|
+
[bw, bh] = options.fit || options.scaleDown;
|
|
4979
6813
|
bp = bw / bh;
|
|
4980
6814
|
ip = width / height;
|
|
4981
|
-
if (
|
|
6815
|
+
if (!options.fit && width <= bw && height <= bh) {
|
|
6816
|
+
w = width;
|
|
6817
|
+
h = height;
|
|
6818
|
+
} else if (ip > bp) {
|
|
4982
6819
|
w = bw;
|
|
4983
6820
|
h = bw / ip;
|
|
4984
6821
|
} else {
|
|
@@ -4997,7 +6834,7 @@ var ImagesMixin = {
|
|
|
4997
6834
|
h = bw / ip;
|
|
4998
6835
|
}
|
|
4999
6836
|
}
|
|
5000
|
-
if (options.fit || options.cover) {
|
|
6837
|
+
if (options.fit || options.scaleDown || options.cover) {
|
|
5001
6838
|
if (options.align === 'center') {
|
|
5002
6839
|
x = x + bw / 2 - w / 2;
|
|
5003
6840
|
} else if (options.align === 'right') {
|
|
@@ -5089,6 +6926,12 @@ var ImagesMixin = {
|
|
|
5089
6926
|
if (options.opacity != null) {
|
|
5090
6927
|
this._doOpacity(options.opacity, null);
|
|
5091
6928
|
}
|
|
6929
|
+
if (options.flipH) {
|
|
6930
|
+
this.transform(-1, 0, 0, 1, 2 * x + w, 0);
|
|
6931
|
+
}
|
|
6932
|
+
if (options.flipV) {
|
|
6933
|
+
this.transform(1, 0, 0, -1, 0, 2 * y + h);
|
|
6934
|
+
}
|
|
5092
6935
|
if (rotateAngle) {
|
|
5093
6936
|
this.rotate(rotateAngle, {
|
|
5094
6937
|
origin: [originX, originY]
|
|
@@ -5115,8 +6958,9 @@ var ImagesMixin = {
|
|
|
5115
6958
|
};
|
|
5116
6959
|
|
|
5117
6960
|
class PDFAnnotationReference {
|
|
5118
|
-
constructor(annotationRef) {
|
|
6961
|
+
constructor(annotationRef, pageRef = annotationRef.document.page.dictionary) {
|
|
5119
6962
|
this.annotationRef = annotationRef;
|
|
6963
|
+
this.pageRef = pageRef;
|
|
5120
6964
|
}
|
|
5121
6965
|
}
|
|
5122
6966
|
|
|
@@ -5144,7 +6988,7 @@ var AnnotationsMixin = {
|
|
|
5144
6988
|
const ref = this.ref(annotations);
|
|
5145
6989
|
this.page.annotations.push(ref);
|
|
5146
6990
|
if (typeof structParent?.add === 'function') {
|
|
5147
|
-
const annotRef = new PDFAnnotationReference(ref);
|
|
6991
|
+
const annotRef = new PDFAnnotationReference(ref, this.page.dictionary);
|
|
5148
6992
|
structParent.add(annotRef);
|
|
5149
6993
|
}
|
|
5150
6994
|
ref.end();
|
|
@@ -5286,16 +7130,15 @@ var AnnotationsMixin = {
|
|
|
5286
7130
|
}
|
|
5287
7131
|
return this.annotate(x, y, w, h, annotationOptions);
|
|
5288
7132
|
},
|
|
5289
|
-
_convertRect(
|
|
5290
|
-
let y2 = y1;
|
|
5291
|
-
y1 += h;
|
|
5292
|
-
let x2 = x1 + w;
|
|
7133
|
+
_convertRect(x, y, w, h) {
|
|
5293
7134
|
const [m0, m1, m2, m3, m4, m5] = this._ctm;
|
|
5294
|
-
|
|
5295
|
-
|
|
5296
|
-
|
|
5297
|
-
|
|
5298
|
-
|
|
7135
|
+
const ox = m0 * x + m2 * y + m4;
|
|
7136
|
+
const oy = m1 * x + m3 * y + m5;
|
|
7137
|
+
const wx = m0 * w;
|
|
7138
|
+
const wy = m1 * w;
|
|
7139
|
+
const hx = m2 * h;
|
|
7140
|
+
const hy = m3 * h;
|
|
7141
|
+
return [ox + (wx < 0 ? wx : 0) + (hx < 0 ? hx : 0), oy + (wy < 0 ? wy : 0) + (hy < 0 ? hy : 0), ox + (wx > 0 ? wx : 0) + (hx > 0 ? hx : 0), oy + (wy > 0 ? wy : 0) + (hy > 0 ? hy : 0)];
|
|
5299
7142
|
}
|
|
5300
7143
|
};
|
|
5301
7144
|
|
|
@@ -5578,11 +7421,10 @@ class PDFStructureElement {
|
|
|
5578
7421
|
});
|
|
5579
7422
|
}
|
|
5580
7423
|
if (child instanceof PDFAnnotationReference) {
|
|
5581
|
-
const pageRef = this.document.page.dictionary;
|
|
5582
7424
|
const objr = {
|
|
5583
7425
|
Type: 'OBJR',
|
|
5584
7426
|
Obj: child.annotationRef,
|
|
5585
|
-
Pg: pageRef
|
|
7427
|
+
Pg: child.pageRef
|
|
5586
7428
|
};
|
|
5587
7429
|
this.dictionary.data.K.push(objr);
|
|
5588
7430
|
}
|
|
@@ -5783,7 +7625,8 @@ const FIELD_JUSTIFY = {
|
|
|
5783
7625
|
};
|
|
5784
7626
|
const VALUE_MAP = {
|
|
5785
7627
|
value: 'V',
|
|
5786
|
-
defaultValue: 'DV'
|
|
7628
|
+
defaultValue: 'DV',
|
|
7629
|
+
alternateName: 'TU'
|
|
5787
7630
|
};
|
|
5788
7631
|
const FORMAT_SPECIAL = {
|
|
5789
7632
|
zip: '0',
|
|
@@ -5834,6 +7677,11 @@ function mapTypeAndFlags(type, userOptions, pdfObject) {
|
|
|
5834
7677
|
pdfObject.Ff = flags;
|
|
5835
7678
|
}
|
|
5836
7679
|
}
|
|
7680
|
+
function mapVisibility(options, pdfObject) {
|
|
7681
|
+
if (options.hidden) {
|
|
7682
|
+
pdfObject.F = 6;
|
|
7683
|
+
}
|
|
7684
|
+
}
|
|
5837
7685
|
function mapJustify(userOptions, pdfObject) {
|
|
5838
7686
|
const result = FIELD_JUSTIFY[userOptions.align];
|
|
5839
7687
|
if (typeof result === 'number' && result !== 0) {
|
|
@@ -5865,6 +7713,28 @@ function mapStrings(options, pdfObject) {
|
|
|
5865
7713
|
pdfObject.MK.CA = new String(options.label);
|
|
5866
7714
|
}
|
|
5867
7715
|
}
|
|
7716
|
+
const ANNOTATION_ACTIONS = {
|
|
7717
|
+
onClick: 'U',
|
|
7718
|
+
onMouseDown: 'D',
|
|
7719
|
+
onMouseEnter: 'E',
|
|
7720
|
+
onMouseExit: 'X',
|
|
7721
|
+
onFocus: 'Fo',
|
|
7722
|
+
onBlur: 'Bl'
|
|
7723
|
+
};
|
|
7724
|
+
function mapActions(options, pdfObject) {
|
|
7725
|
+
for (const [option, key] of Object.entries(ANNOTATION_ACTIONS)) {
|
|
7726
|
+
const action = options[option];
|
|
7727
|
+
if (!action) {
|
|
7728
|
+
continue;
|
|
7729
|
+
}
|
|
7730
|
+
const js = typeof action === 'function' ? `(${action}).call(this);` : action;
|
|
7731
|
+
pdfObject.AA = pdfObject.AA ?? {};
|
|
7732
|
+
pdfObject.AA[key] = {
|
|
7733
|
+
S: 'JavaScript',
|
|
7734
|
+
JS: new String(js)
|
|
7735
|
+
};
|
|
7736
|
+
}
|
|
7737
|
+
}
|
|
5868
7738
|
function mapFormat(options, pdfObject) {
|
|
5869
7739
|
const f = options.format;
|
|
5870
7740
|
if (f?.type) {
|
|
@@ -5904,16 +7774,20 @@ function mapFormat(options, pdfObject) {
|
|
|
5904
7774
|
};
|
|
5905
7775
|
}
|
|
5906
7776
|
}
|
|
7777
|
+
function formFontRef(doc, font) {
|
|
7778
|
+
return doc._acroform.embedFonts && typeof font.completeRef === 'function' ? font.completeRef() : font.ref();
|
|
7779
|
+
}
|
|
5907
7780
|
var AcroFormMixin = {
|
|
5908
|
-
initForm() {
|
|
7781
|
+
initForm(options = {}) {
|
|
5909
7782
|
if (!this._font) {
|
|
5910
7783
|
throw new Error('Must set a font before calling initForm method');
|
|
5911
7784
|
}
|
|
5912
7785
|
this._acroform = {
|
|
5913
7786
|
fonts: {},
|
|
5914
|
-
defaultFont: this._font.name
|
|
7787
|
+
defaultFont: this._font.name,
|
|
7788
|
+
embedFonts: options.embedFonts === true
|
|
5915
7789
|
};
|
|
5916
|
-
this._acroform.fonts[this._font.id] = this._font
|
|
7790
|
+
this._acroform.fonts[this._font.id] = formFontRef(this, this._font);
|
|
5917
7791
|
let data = {
|
|
5918
7792
|
Fields: [],
|
|
5919
7793
|
NeedAppearances: true,
|
|
@@ -5922,7 +7796,7 @@ var AcroFormMixin = {
|
|
|
5922
7796
|
Font: {}
|
|
5923
7797
|
}
|
|
5924
7798
|
};
|
|
5925
|
-
data.DR.Font[this._font.id] = this._font
|
|
7799
|
+
data.DR.Font[this._font.id] = formFontRef(this, this._font);
|
|
5926
7800
|
const AcroForm = this.ref(data);
|
|
5927
7801
|
this._root.data.AcroForm = AcroForm;
|
|
5928
7802
|
return this;
|
|
@@ -5964,7 +7838,10 @@ var AcroFormMixin = {
|
|
|
5964
7838
|
if (fieldDict.F === undefined) {
|
|
5965
7839
|
fieldDict.F = 4;
|
|
5966
7840
|
}
|
|
5967
|
-
this.annotate(x, y, w, h,
|
|
7841
|
+
this.annotate(x, y, w, h, {
|
|
7842
|
+
...fieldDict,
|
|
7843
|
+
structParent: options.structParent
|
|
7844
|
+
});
|
|
5968
7845
|
let annotRef = this.page.annotations[this.page.annotations.length - 1];
|
|
5969
7846
|
return this._addToParent(annotRef);
|
|
5970
7847
|
},
|
|
@@ -6004,11 +7881,13 @@ var AcroFormMixin = {
|
|
|
6004
7881
|
}
|
|
6005
7882
|
const pdfObject = {};
|
|
6006
7883
|
mapTypeAndFlags(type, options, pdfObject);
|
|
7884
|
+
mapVisibility(options, pdfObject);
|
|
6007
7885
|
mapJustify(options, pdfObject);
|
|
6008
7886
|
this._mapFont(options, pdfObject);
|
|
6009
7887
|
mapStrings(options, pdfObject);
|
|
6010
7888
|
this._mapColors(options, pdfObject);
|
|
6011
7889
|
mapFormat(options, pdfObject);
|
|
7890
|
+
mapActions(options, pdfObject);
|
|
6012
7891
|
pdfObject.T = new String(name);
|
|
6013
7892
|
if (options.parent) {
|
|
6014
7893
|
pdfObject.Parent = options.parent;
|
|
@@ -6035,14 +7914,17 @@ var AcroFormMixin = {
|
|
|
6035
7914
|
_font
|
|
6036
7915
|
} = this;
|
|
6037
7916
|
if (_acroform.fonts[_font.id] == null) {
|
|
6038
|
-
_acroform.fonts[_font.id] = _font
|
|
7917
|
+
_acroform.fonts[_font.id] = formFontRef(this, _font);
|
|
6039
7918
|
}
|
|
6040
|
-
|
|
7919
|
+
const isDefaultFont = _acroform.defaultFont === _font.name;
|
|
7920
|
+
if (!isDefaultFont) {
|
|
6041
7921
|
pdfObject.DR = {
|
|
6042
7922
|
Font: {}
|
|
6043
7923
|
};
|
|
7924
|
+
pdfObject.DR.Font[_font.id] = formFontRef(this, _font);
|
|
7925
|
+
}
|
|
7926
|
+
if (!isDefaultFont || options.fontSize != null) {
|
|
6044
7927
|
const fontSize = options.fontSize || 0;
|
|
6045
|
-
pdfObject.DR.Font[_font.id] = _font.ref();
|
|
6046
7928
|
pdfObject.DA = new String(`/${_font.id} ${fontSize} Tf 0 g`);
|
|
6047
7929
|
}
|
|
6048
7930
|
}
|
|
@@ -6142,8 +8024,63 @@ function isSameDate(a, b) {
|
|
|
6142
8024
|
return a.getTime() === b.getTime();
|
|
6143
8025
|
}
|
|
6144
8026
|
|
|
6145
|
-
const
|
|
8027
|
+
const STYLES = {
|
|
8028
|
+
decimal: 'D',
|
|
8029
|
+
upperRoman: 'R',
|
|
8030
|
+
lowerRoman: 'r',
|
|
8031
|
+
upperAlpha: 'A',
|
|
8032
|
+
lowerAlpha: 'a'
|
|
8033
|
+
};
|
|
8034
|
+
var PageLabelsMixin = {
|
|
8035
|
+
addPageLabel(pageIndex, options = {}) {
|
|
8036
|
+
if (!Number.isInteger(pageIndex) || pageIndex < 0) {
|
|
8037
|
+
throw new Error(`addPageLabel(${pageIndex}) invalid, pageIndex must be a non-negative integer`);
|
|
8038
|
+
}
|
|
8039
|
+
const {
|
|
8040
|
+
style,
|
|
8041
|
+
prefix,
|
|
8042
|
+
start
|
|
8043
|
+
} = options;
|
|
8044
|
+
const label = {};
|
|
8045
|
+
if (style != null) {
|
|
8046
|
+
if (!Object.hasOwn(STYLES, style)) {
|
|
8047
|
+
throw new Error(`addPageLabel style ${style} invalid, must be one of ${Object.keys(STYLES).join(', ')}`);
|
|
8048
|
+
}
|
|
8049
|
+
label.S = STYLES[style];
|
|
8050
|
+
}
|
|
8051
|
+
if (prefix != null) {
|
|
8052
|
+
label.P = new String(prefix);
|
|
8053
|
+
}
|
|
8054
|
+
if (start != null) {
|
|
8055
|
+
if (!Number.isInteger(start) || start < 1) {
|
|
8056
|
+
throw new Error(`addPageLabel start ${start} invalid, must be a positive integer`);
|
|
8057
|
+
}
|
|
8058
|
+
label.St = start;
|
|
8059
|
+
}
|
|
8060
|
+
if (!this._root.data.PageLabels) {
|
|
8061
|
+
this._root.data.PageLabels = new PDFNumberTree({
|
|
8062
|
+
limits: false
|
|
8063
|
+
});
|
|
8064
|
+
this._root.data.PageLabels.add(0, {
|
|
8065
|
+
S: STYLES.decimal
|
|
8066
|
+
});
|
|
8067
|
+
}
|
|
8068
|
+
this._root.data.PageLabels.add(pageIndex, label);
|
|
8069
|
+
return this;
|
|
8070
|
+
}
|
|
8071
|
+
};
|
|
8072
|
+
|
|
8073
|
+
let iccProfilePath;
|
|
6146
8074
|
let iccProfile;
|
|
8075
|
+
const getIccProfilePath = () => {
|
|
8076
|
+
if (iccProfilePath === undefined) {
|
|
8077
|
+
if ((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('pdfkit.js', document.baseURI).href)) == null) {
|
|
8078
|
+
throw new Error('pdfkit cannot locate its sRGB ICC profile because import.meta.url ' + 'is not available in this build. Bundle pdfkit as ESM or mark it ' + 'as external in the bundler.');
|
|
8079
|
+
}
|
|
8080
|
+
iccProfilePath = new URL('./data/sRGB_IEC61966_2_1.icc', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('pdfkit.js', document.baseURI).href))).href;
|
|
8081
|
+
}
|
|
8082
|
+
return iccProfilePath;
|
|
8083
|
+
};
|
|
6147
8084
|
var PDFA = {
|
|
6148
8085
|
initPDFA(pSubset) {
|
|
6149
8086
|
if (pSubset.charAt(pSubset.length - 3) === '-') {
|
|
@@ -6160,7 +8097,7 @@ var PDFA = {
|
|
|
6160
8097
|
},
|
|
6161
8098
|
_addColorOutputIntent() {
|
|
6162
8099
|
if (!iccProfile) {
|
|
6163
|
-
iccProfile = fs.readFileSync(
|
|
8100
|
+
iccProfile = fs.readFileSync(getIccProfilePath());
|
|
6164
8101
|
}
|
|
6165
8102
|
const colorProfileRef = this.ref({
|
|
6166
8103
|
Length: iccProfile.length,
|
|
@@ -6541,15 +8478,25 @@ function measure(row, rowIndex) {
|
|
|
6541
8478
|
}
|
|
6542
8479
|
this._rowHeights[rowIndex] = rowHeight;
|
|
6543
8480
|
let newPage = false;
|
|
8481
|
+
let repeatHeaders = false;
|
|
6544
8482
|
if (rowHeight > this.document.page.contentHeight) {
|
|
6545
8483
|
console.warn(new Error(`Row ${rowIndex} requested more than the safe page height, row has been clamped`).stack.slice(7));
|
|
6546
8484
|
this._rowHeights[rowIndex] = this.document.page.maxY() - this._rowYPos[rowIndex];
|
|
6547
8485
|
} else if (this._rowYPos[rowIndex] + rowHeight >= this.document.page.maxY()) {
|
|
6548
|
-
|
|
8486
|
+
let top = this.document.page.margins.top;
|
|
8487
|
+
if (rowIndex >= this._headerRows) {
|
|
8488
|
+
const headerHeight = this._rowHeights.slice(0, this._headerRows).reduce((acc, height) => acc + height, 0);
|
|
8489
|
+
if (this._headerRows > 0 && rowHeight + headerHeight <= this.document.page.contentHeight) {
|
|
8490
|
+
top += headerHeight;
|
|
8491
|
+
repeatHeaders = true;
|
|
8492
|
+
}
|
|
8493
|
+
}
|
|
8494
|
+
this._rowYPos[rowIndex] = top;
|
|
6549
8495
|
newPage = true;
|
|
6550
8496
|
}
|
|
6551
8497
|
return {
|
|
6552
8498
|
newPage,
|
|
8499
|
+
repeatHeaders,
|
|
6553
8500
|
toRender: toRender.map(cell => measureCell.call(this, cell, rowHeight))
|
|
6554
8501
|
};
|
|
6555
8502
|
}
|
|
@@ -6751,6 +8698,22 @@ function renderRow(row, rowIndex) {
|
|
|
6751
8698
|
}
|
|
6752
8699
|
return this._rowYPos[rowIndex] + this._rowHeights[rowIndex];
|
|
6753
8700
|
}
|
|
8701
|
+
function renderHeaderRows(cells) {
|
|
8702
|
+
const doc = this.document;
|
|
8703
|
+
if (this._tableStruct) doc.markContent('Artifact', {
|
|
8704
|
+
type: 'Pagination'
|
|
8705
|
+
});
|
|
8706
|
+
cells.forEach(cell => {
|
|
8707
|
+
let y = doc.page.margins.top;
|
|
8708
|
+
for (let i = 0; i < cell.rowIndex; i++) y += this._rowHeights[i];
|
|
8709
|
+
renderCell.call(this, {
|
|
8710
|
+
...cell,
|
|
8711
|
+
y,
|
|
8712
|
+
textY: y + cell.padding.top
|
|
8713
|
+
});
|
|
8714
|
+
});
|
|
8715
|
+
if (this._tableStruct) doc.endMarkedContent();
|
|
8716
|
+
}
|
|
6754
8717
|
function renderCell(cell, rowStruct) {
|
|
6755
8718
|
const cellRenderer = () => {
|
|
6756
8719
|
if (cell.backgroundColor != null) {
|
|
@@ -6839,6 +8802,8 @@ class PDFTable {
|
|
|
6839
8802
|
this.opts = Object.freeze(opts);
|
|
6840
8803
|
normalizeTable.call(this);
|
|
6841
8804
|
accommodateTable.call(this);
|
|
8805
|
+
this._headerRows = opts.headerRows ?? 0;
|
|
8806
|
+
this._headerCells = [];
|
|
6842
8807
|
this._currRowIndex = 0;
|
|
6843
8808
|
this._ended = false;
|
|
6844
8809
|
if (opts.data) {
|
|
@@ -6855,9 +8820,14 @@ class PDFTable {
|
|
|
6855
8820
|
if (this._currRowIndex === 0) ensure.call(this, row);
|
|
6856
8821
|
const {
|
|
6857
8822
|
newPage,
|
|
8823
|
+
repeatHeaders,
|
|
6858
8824
|
toRender
|
|
6859
8825
|
} = measure.call(this, row, this._currRowIndex);
|
|
6860
8826
|
if (newPage) this.document.continueOnNewPage();
|
|
8827
|
+
if (repeatHeaders) renderHeaderRows.call(this, this._headerCells);
|
|
8828
|
+
if (this._currRowIndex < this._headerRows) {
|
|
8829
|
+
this._headerCells.push(...toRender);
|
|
8830
|
+
}
|
|
6861
8831
|
const yPos = renderRow.call(this, toRender, this._currRowIndex);
|
|
6862
8832
|
this.document.x = this._position.x;
|
|
6863
8833
|
this.document.y = yPos;
|
|
@@ -6913,6 +8883,9 @@ class PDFMetadata {
|
|
|
6913
8883
|
}
|
|
6914
8884
|
}
|
|
6915
8885
|
|
|
8886
|
+
function escapeXML(value) {
|
|
8887
|
+
return String(value).replace(/[^\u0009\u000A\u000D\u0020-\uD7FF\uE000-\uFFFD\u{10000}-\u{10FFFF}]/gu, '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''').replace(/\r/g, '
');
|
|
8888
|
+
}
|
|
6916
8889
|
var MetadataMixin = {
|
|
6917
8890
|
initMetadata() {
|
|
6918
8891
|
this.metadata = new PDFMetadata();
|
|
@@ -6924,7 +8897,7 @@ var MetadataMixin = {
|
|
|
6924
8897
|
this.appendXML(`
|
|
6925
8898
|
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
|
|
6926
8899
|
<xmp:CreateDate>${this.info.CreationDate.toISOString().split('.')[0] + 'Z'}</xmp:CreateDate>
|
|
6927
|
-
<xmp:CreatorTool>${this.info.Creator}</xmp:CreatorTool>
|
|
8900
|
+
<xmp:CreatorTool>${escapeXML(this.info.Creator)}</xmp:CreatorTool>
|
|
6928
8901
|
</rdf:Description>
|
|
6929
8902
|
`);
|
|
6930
8903
|
if (this.info.Title || this.info.Author || this.info.Subject) {
|
|
@@ -6935,7 +8908,7 @@ var MetadataMixin = {
|
|
|
6935
8908
|
this.appendXML(`
|
|
6936
8909
|
<dc:title>
|
|
6937
8910
|
<rdf:Alt>
|
|
6938
|
-
<rdf:li xml:lang="x-default">${this.info.Title}</rdf:li>
|
|
8911
|
+
<rdf:li xml:lang="x-default">${escapeXML(this.info.Title)}</rdf:li>
|
|
6939
8912
|
</rdf:Alt>
|
|
6940
8913
|
</dc:title>
|
|
6941
8914
|
`);
|
|
@@ -6944,7 +8917,7 @@ var MetadataMixin = {
|
|
|
6944
8917
|
this.appendXML(`
|
|
6945
8918
|
<dc:creator>
|
|
6946
8919
|
<rdf:Seq>
|
|
6947
|
-
<rdf:li>${this.info.Author}</rdf:li>
|
|
8920
|
+
<rdf:li>${escapeXML(this.info.Author)}</rdf:li>
|
|
6948
8921
|
</rdf:Seq>
|
|
6949
8922
|
</dc:creator>
|
|
6950
8923
|
`);
|
|
@@ -6953,7 +8926,7 @@ var MetadataMixin = {
|
|
|
6953
8926
|
this.appendXML(`
|
|
6954
8927
|
<dc:description>
|
|
6955
8928
|
<rdf:Alt>
|
|
6956
|
-
<rdf:li xml:lang="x-default">${this.info.Subject}</rdf:li>
|
|
8929
|
+
<rdf:li xml:lang="x-default">${escapeXML(this.info.Subject)}</rdf:li>
|
|
6957
8930
|
</rdf:Alt>
|
|
6958
8931
|
</dc:description>
|
|
6959
8932
|
`);
|
|
@@ -6964,10 +8937,10 @@ var MetadataMixin = {
|
|
|
6964
8937
|
}
|
|
6965
8938
|
this.appendXML(`
|
|
6966
8939
|
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
|
|
6967
|
-
<pdf:Producer>${this.info.Producer}</pdf:Producer>`, false);
|
|
8940
|
+
<pdf:Producer>${escapeXML(this.info.Producer)}</pdf:Producer>`, false);
|
|
6968
8941
|
if (this.info.Keywords) {
|
|
6969
8942
|
this.appendXML(`
|
|
6970
|
-
<pdf:Keywords>${this.info.Keywords}</pdf:Keywords>`, false);
|
|
8943
|
+
<pdf:Keywords>${escapeXML(this.info.Keywords)}</pdf:Keywords>`, false);
|
|
6971
8944
|
}
|
|
6972
8945
|
this.appendXML(`
|
|
6973
8946
|
</rdf:Description>
|
|
@@ -7013,6 +8986,9 @@ class PDFDocument extends Readable {
|
|
|
7013
8986
|
break;
|
|
7014
8987
|
}
|
|
7015
8988
|
this.compress = this.options.compress != null ? this.options.compress : true;
|
|
8989
|
+
if (options.objectStreams) {
|
|
8990
|
+
this._objectStreams = new PDFObjectStreams(this, options);
|
|
8991
|
+
}
|
|
7016
8992
|
this._pageBuffer = [];
|
|
7017
8993
|
this._pageBufferStart = 0;
|
|
7018
8994
|
this._offsets = [];
|
|
@@ -7177,8 +9153,8 @@ class PDFDocument extends Readable {
|
|
|
7177
9153
|
_refEnd(ref) {
|
|
7178
9154
|
this._offsets[ref.id - 1] = ref.offset;
|
|
7179
9155
|
if (--this._waiting === 0 && this._ended) {
|
|
7180
|
-
this._finalize();
|
|
7181
9156
|
this._ended = false;
|
|
9157
|
+
this._finalize();
|
|
7182
9158
|
}
|
|
7183
9159
|
}
|
|
7184
9160
|
end() {
|
|
@@ -7221,6 +9197,11 @@ class PDFDocument extends Readable {
|
|
|
7221
9197
|
}
|
|
7222
9198
|
}
|
|
7223
9199
|
_finalize() {
|
|
9200
|
+
if (this._objectStreams) {
|
|
9201
|
+
this._objectStreams.finalize();
|
|
9202
|
+
this.push(null);
|
|
9203
|
+
return;
|
|
9204
|
+
}
|
|
7224
9205
|
const xRefOffset = this._offset;
|
|
7225
9206
|
this._write('xref');
|
|
7226
9207
|
this._write(`0 ${this._offsets.length + 1}`);
|
|
@@ -7263,33 +9244,50 @@ mixin(OutlineMixin);
|
|
|
7263
9244
|
mixin(MarkingsMixin);
|
|
7264
9245
|
mixin(AcroFormMixin);
|
|
7265
9246
|
mixin(AttachmentsMixin);
|
|
9247
|
+
mixin(PageLabelsMixin);
|
|
7266
9248
|
mixin(SubsetMixin);
|
|
7267
9249
|
mixin(TableMixin);
|
|
7268
9250
|
|
|
7269
|
-
const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('pdfkit.js', document.baseURI).href)));
|
|
7270
|
-
|
|
7271
|
-
Courier: () => require$1('
|
|
7272
|
-
'Courier-Bold': () => require$1('
|
|
7273
|
-
'Courier-BoldOblique': () => require$1('
|
|
7274
|
-
'Courier-Oblique': () => require$1('
|
|
7275
|
-
Helvetica: () => require$1('
|
|
7276
|
-
'Helvetica-Bold': () => require$1('
|
|
7277
|
-
'Helvetica-BoldOblique': () => require$1('
|
|
7278
|
-
'Helvetica-Oblique': () => require$1('
|
|
7279
|
-
Symbol: () => require$1('
|
|
7280
|
-
'Times-Bold': () => require$1('
|
|
7281
|
-
'Times-BoldItalic': () => require$1('
|
|
7282
|
-
'Times-Italic': () => require$1('
|
|
7283
|
-
'Times-Roman': () => require$1('
|
|
7284
|
-
ZapfDingbats: () => require$1('
|
|
7285
|
-
}
|
|
9251
|
+
const require$1 = module$1.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('pdfkit.js', document.baseURI).href)) ?? '/');
|
|
9252
|
+
const loaders = {
|
|
9253
|
+
Courier: () => require$1('./standard-fonts/Courier.cjs'),
|
|
9254
|
+
'Courier-Bold': () => require$1('./standard-fonts/CourierBold.cjs'),
|
|
9255
|
+
'Courier-BoldOblique': () => require$1('./standard-fonts/CourierBoldOblique.cjs'),
|
|
9256
|
+
'Courier-Oblique': () => require$1('./standard-fonts/CourierOblique.cjs'),
|
|
9257
|
+
Helvetica: () => require$1('./standard-fonts/Helvetica.cjs'),
|
|
9258
|
+
'Helvetica-Bold': () => require$1('./standard-fonts/HelveticaBold.cjs'),
|
|
9259
|
+
'Helvetica-BoldOblique': () => require$1('./standard-fonts/HelveticaBoldOblique.cjs'),
|
|
9260
|
+
'Helvetica-Oblique': () => require$1('./standard-fonts/HelveticaOblique.cjs'),
|
|
9261
|
+
Symbol: () => require$1('./standard-fonts/Symbol.cjs'),
|
|
9262
|
+
'Times-Bold': () => require$1('./standard-fonts/TimesBold.cjs'),
|
|
9263
|
+
'Times-BoldItalic': () => require$1('./standard-fonts/TimesBoldItalic.cjs'),
|
|
9264
|
+
'Times-Italic': () => require$1('./standard-fonts/TimesItalic.cjs'),
|
|
9265
|
+
'Times-Roman': () => require$1('./standard-fonts/TimesRoman.cjs'),
|
|
9266
|
+
ZapfDingbats: () => require$1('./standard-fonts/ZapfDingbats.cjs')
|
|
9267
|
+
};
|
|
9268
|
+
registerStdFontLoaders(Object.fromEntries(Object.entries(loaders).map(([name, load]) => [name, () => {
|
|
9269
|
+
try {
|
|
9270
|
+
return load();
|
|
9271
|
+
} catch (error) {
|
|
9272
|
+
if (error?.code !== 'MODULE_NOT_FOUND') {
|
|
9273
|
+
throw error;
|
|
9274
|
+
}
|
|
9275
|
+
throw new Error(`Cannot load the standard font "${name}" from pdfkit's package ` + 'directory, which is not available once pdfkit is bundled ' + 'into another file. Either mark pdfkit as external in the ' + 'bundler, or import the fonts the document uses from ' + '"pdfkit/standard-fonts/<Name>" and pass them to ' + 'registerStdFonts() before creating the document.', {
|
|
9276
|
+
cause: error
|
|
9277
|
+
});
|
|
9278
|
+
}
|
|
9279
|
+
}])));
|
|
7286
9280
|
|
|
7287
9281
|
exports.LineWrapper = LineWrapper;
|
|
9282
|
+
exports.PAGE_SIZES = PAGE_SIZES;
|
|
7288
9283
|
exports.PDFDocument = PDFDocument;
|
|
7289
9284
|
exports.default = PDFDocument;
|
|
7290
9285
|
exports.registerFile = registerFile;
|
|
9286
|
+
exports.registerStdFonts = registerStdFonts;
|
|
7291
9287
|
module.exports = exports.default;
|
|
7292
9288
|
module.exports.PDFDocument = exports.PDFDocument;
|
|
7293
9289
|
module.exports.LineWrapper = exports.LineWrapper;
|
|
9290
|
+
module.exports.PAGE_SIZES = exports.PAGE_SIZES;
|
|
9291
|
+
module.exports.registerStdFonts = exports.registerStdFonts;
|
|
7294
9292
|
module.exports.registerFile = exports.registerFile;
|
|
7295
9293
|
//# sourceMappingURL=pdfkit.js.map
|