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.browser.mjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { bytesToHex, concatBytes } from '@noble/hashes/utils';
|
|
2
1
|
import { zlibSync } from 'fflate';
|
|
3
|
-
import { cbc, ecb } from '@noble/ciphers/aes';
|
|
4
2
|
import { create } from 'fontkit';
|
|
5
|
-
import LineBreaker from 'linebreak';
|
|
3
|
+
import { LineBreaker } from 'linebreak';
|
|
6
4
|
import PNG from 'png-js';
|
|
7
5
|
|
|
8
6
|
class EventEmitter {
|
|
@@ -122,6 +120,104 @@ class Readable extends EventEmitter {
|
|
|
122
120
|
}
|
|
123
121
|
}
|
|
124
122
|
|
|
123
|
+
function isBytes$1(a) {
|
|
124
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array' && 'BYTES_PER_ELEMENT' in a && a.BYTES_PER_ELEMENT === 1;
|
|
125
|
+
}
|
|
126
|
+
const atitle$1 = title => title ? `"${title}" ` : '';
|
|
127
|
+
function abytes$1(value, length, title = '') {
|
|
128
|
+
if (isBytes$1(value) && (length === undefined)) return value;
|
|
129
|
+
const bytes = isBytes$1(value);
|
|
130
|
+
const ofLen = '';
|
|
131
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
132
|
+
const message = atitle$1(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
133
|
+
if (!bytes) throw new TypeError(message);
|
|
134
|
+
throw new RangeError(message);
|
|
135
|
+
}
|
|
136
|
+
const aobject = (value, label) => {
|
|
137
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) throw new TypeError((label === 'object' ? '' : `"${label}" `) + 'expected object, got type=' + typeof value);
|
|
138
|
+
};
|
|
139
|
+
const aopts = (value, label) => {
|
|
140
|
+
aobject(value, label);
|
|
141
|
+
const proto = Object.getPrototypeOf(value);
|
|
142
|
+
if (proto !== Object.prototype && proto !== null) throw new TypeError(`"${label}" expected plain object`);
|
|
143
|
+
if (Object.hasOwn(value, '__proto__')) throw new TypeError(`"${label}.__proto__" is not allowed`);
|
|
144
|
+
};
|
|
145
|
+
function aexists$1(instance, checkFinished = true) {
|
|
146
|
+
if (instance.destroyed) throw new Error('hash was destroyed');
|
|
147
|
+
if (checkFinished && instance.finished) throw new Error('digest() was already called');
|
|
148
|
+
}
|
|
149
|
+
function aoutput$1(out, instance) {
|
|
150
|
+
abytes$1(out, undefined, 'output');
|
|
151
|
+
const min = instance.outputLen;
|
|
152
|
+
if (!(out.length >= min)) {
|
|
153
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
function clean$1(...arrays) {
|
|
157
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
158
|
+
arrays[i].fill(0);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
function createView$1(arr) {
|
|
162
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
163
|
+
}
|
|
164
|
+
function rotr(word, shift) {
|
|
165
|
+
return word << 32 - shift | word >>> shift;
|
|
166
|
+
}
|
|
167
|
+
function rotl(word, shift) {
|
|
168
|
+
return word << shift | word >>> 32 - shift >>> 0;
|
|
169
|
+
}
|
|
170
|
+
(() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
171
|
+
const hasHexBuiltin = (() => typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
172
|
+
const hexes = Array.from({
|
|
173
|
+
length: 256
|
|
174
|
+
}, (_, i) => i.toString(16).padStart(2, '0'));
|
|
175
|
+
function bytesToHex(bytes) {
|
|
176
|
+
abytes$1(bytes);
|
|
177
|
+
if (hasHexBuiltin) return bytes.toHex();
|
|
178
|
+
let hex = '';
|
|
179
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
180
|
+
hex += hexes[bytes[i]];
|
|
181
|
+
}
|
|
182
|
+
return hex;
|
|
183
|
+
}
|
|
184
|
+
function concatBytes$1(...arrays) {
|
|
185
|
+
let sum = 0;
|
|
186
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
187
|
+
const a = arrays[i];
|
|
188
|
+
abytes$1(a);
|
|
189
|
+
sum += a.length;
|
|
190
|
+
}
|
|
191
|
+
const res = new Uint8Array(sum);
|
|
192
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
193
|
+
const a = arrays[i];
|
|
194
|
+
res.set(a, pad);
|
|
195
|
+
pad += a.length;
|
|
196
|
+
}
|
|
197
|
+
return res;
|
|
198
|
+
}
|
|
199
|
+
function checkOpts(defaults, opts, title = 'opts') {
|
|
200
|
+
aopts(defaults, 'defaults');
|
|
201
|
+
if (opts !== undefined) aopts(opts, title);
|
|
202
|
+
const merged = Object.assign(Object.create(null), defaults, opts);
|
|
203
|
+
return merged;
|
|
204
|
+
}
|
|
205
|
+
function createHasher(hashCons, info = {}) {
|
|
206
|
+
if (typeof hashCons !== 'function') throw new TypeError('"hashCons" expected function, got type=' + typeof hashCons);
|
|
207
|
+
info = checkOpts({}, info, 'info');
|
|
208
|
+
const hashC = (msg, opts) => hashCons(opts).update(msg).digest();
|
|
209
|
+
const tmp = hashCons(undefined);
|
|
210
|
+
hashC.outputLen = tmp.outputLen;
|
|
211
|
+
hashC.blockLen = tmp.blockLen;
|
|
212
|
+
hashC.canXOF = tmp.canXOF;
|
|
213
|
+
hashC.create = opts => hashCons(opts);
|
|
214
|
+
Object.assign(hashC, info);
|
|
215
|
+
return Object.freeze(hashC);
|
|
216
|
+
}
|
|
217
|
+
const oidNist = suffix => ({
|
|
218
|
+
oid: Uint8Array.from([0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, suffix])
|
|
219
|
+
});
|
|
220
|
+
|
|
125
221
|
class PDFAbstractReference {
|
|
126
222
|
toString() {
|
|
127
223
|
throw new Error('Must be implemented by subclasses');
|
|
@@ -369,6 +465,12 @@ class PDFReference extends PDFAbstractReference {
|
|
|
369
465
|
this.finalize();
|
|
370
466
|
}
|
|
371
467
|
finalize() {
|
|
468
|
+
const objectStreams = this.document._objectStreams;
|
|
469
|
+
if (objectStreams && !this.buffer.length) {
|
|
470
|
+
objectStreams.add(this);
|
|
471
|
+
this.document._refEnd(this);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
372
474
|
this.offset = this.document._offset;
|
|
373
475
|
const encryptFn = this.document._security ? this.document._security.getEncryptFn(this.id, this.gen) : null;
|
|
374
476
|
if (this.buffer.length) {
|
|
@@ -477,7 +579,7 @@ const DEFAULT_MARGINS = {
|
|
|
477
579
|
bottom: 72,
|
|
478
580
|
right: 72
|
|
479
581
|
};
|
|
480
|
-
const
|
|
582
|
+
const PAGE_SIZES = {
|
|
481
583
|
'4A0': [4767.87, 6740.79],
|
|
482
584
|
'2A0': [3370.39, 4767.87],
|
|
483
585
|
A0: [2383.94, 3370.39],
|
|
@@ -536,7 +638,7 @@ class PDFPage {
|
|
|
536
638
|
this.size = options.size || 'letter';
|
|
537
639
|
this.layout = options.layout || 'portrait';
|
|
538
640
|
this.userUnit = options.userUnit || 1.0;
|
|
539
|
-
const dimensions = Array.isArray(this.size) ? this.size :
|
|
641
|
+
const dimensions = Array.isArray(this.size) ? this.size : PAGE_SIZES[this.size.toUpperCase()];
|
|
540
642
|
this.width = dimensions[this.layout === 'portrait' ? 0 : 1];
|
|
541
643
|
this.height = dimensions[this.layout === 'portrait' ? 1 : 0];
|
|
542
644
|
this.content = this.document.ref();
|
|
@@ -613,7 +715,7 @@ class PDFPage {
|
|
|
613
715
|
|
|
614
716
|
class PDFNameTree extends PDFTree {
|
|
615
717
|
_compareKeys(a, b) {
|
|
616
|
-
return a
|
|
718
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
617
719
|
}
|
|
618
720
|
_keysName() {
|
|
619
721
|
return 'Names';
|
|
@@ -623,125 +725,119 @@ class PDFNameTree extends PDFTree {
|
|
|
623
725
|
}
|
|
624
726
|
}
|
|
625
727
|
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
}
|
|
637
|
-
function aoutput(out, instance) {
|
|
638
|
-
abytes(out);
|
|
639
|
-
const min = instance.outputLen;
|
|
640
|
-
if (out.length < min) {
|
|
641
|
-
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
|
642
|
-
}
|
|
728
|
+
const U32_MASK64 = (() => BigInt(2 ** 32 - 1))();
|
|
729
|
+
const _32n = BigInt(32);
|
|
730
|
+
function fromBig(n, le = false) {
|
|
731
|
+
if (le) return {
|
|
732
|
+
h: Number(n & U32_MASK64),
|
|
733
|
+
l: Number(n >> _32n & U32_MASK64)
|
|
734
|
+
};
|
|
735
|
+
return {
|
|
736
|
+
h: Number(n >> _32n & U32_MASK64) | 0,
|
|
737
|
+
l: Number(n & U32_MASK64) | 0
|
|
738
|
+
};
|
|
643
739
|
}
|
|
644
|
-
function
|
|
645
|
-
|
|
646
|
-
|
|
740
|
+
function split(lst, le = false) {
|
|
741
|
+
const len = lst.length;
|
|
742
|
+
let Ah = new Uint32Array(len);
|
|
743
|
+
let Al = new Uint32Array(len);
|
|
744
|
+
for (let i = 0; i < len; i++) {
|
|
745
|
+
const {
|
|
746
|
+
h,
|
|
747
|
+
l
|
|
748
|
+
} = fromBig(lst[i], le);
|
|
749
|
+
[Ah[i], Al[i]] = [h, l];
|
|
647
750
|
}
|
|
751
|
+
return [Ah, Al];
|
|
648
752
|
}
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
return word << shift | word >>> 32 - shift >>> 0;
|
|
657
|
-
}
|
|
658
|
-
(() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
659
|
-
(() => typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
660
|
-
Array.from({
|
|
661
|
-
length: 256
|
|
662
|
-
}, (_, i) => i.toString(16).padStart(2, '0'));
|
|
663
|
-
function utf8ToBytes(str) {
|
|
664
|
-
if (typeof str !== 'string') throw new Error('string expected');
|
|
665
|
-
return new Uint8Array(new TextEncoder().encode(str));
|
|
666
|
-
}
|
|
667
|
-
function toBytes(data) {
|
|
668
|
-
if (typeof data === 'string') data = utf8ToBytes(data);
|
|
669
|
-
abytes(data);
|
|
670
|
-
return data;
|
|
753
|
+
const fromNumH = n => n / 2 ** 32 | 0;
|
|
754
|
+
const fromNumL = n => n >>> 0;
|
|
755
|
+
function setU64FromNum(view, byteOffset, n, isLE) {
|
|
756
|
+
const h = fromNumH(n);
|
|
757
|
+
const l = fromNumL(n);
|
|
758
|
+
view.setUint32(byteOffset, isLE ? l : h, isLE);
|
|
759
|
+
view.setUint32(byteOffset + 4, isLE ? h : l, isLE);
|
|
671
760
|
}
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
761
|
+
const shrSH = (h, _l, s) => h >>> s;
|
|
762
|
+
const shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
763
|
+
const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
764
|
+
const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
765
|
+
const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
766
|
+
const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
767
|
+
function add(Ah, Al, Bh, Bl) {
|
|
768
|
+
const l = (Al >>> 0) + (Bl >>> 0);
|
|
769
|
+
return {
|
|
770
|
+
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
771
|
+
l: l | 0
|
|
772
|
+
};
|
|
680
773
|
}
|
|
774
|
+
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
775
|
+
const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
776
|
+
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
777
|
+
const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
778
|
+
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
779
|
+
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
681
780
|
|
|
682
|
-
function setBigUint64(view, byteOffset, value, isLE) {
|
|
683
|
-
if (typeof view.setBigUint64 === 'function') return view.setBigUint64(byteOffset, value, isLE);
|
|
684
|
-
const _32n = BigInt(32);
|
|
685
|
-
const _u32_max = BigInt(0xffffffff);
|
|
686
|
-
const wh = Number(value >> _32n & _u32_max);
|
|
687
|
-
const wl = Number(value & _u32_max);
|
|
688
|
-
const h = isLE ? 4 : 0;
|
|
689
|
-
const l = isLE ? 0 : 4;
|
|
690
|
-
view.setUint32(byteOffset + h, wh, isLE);
|
|
691
|
-
view.setUint32(byteOffset + l, wl, isLE);
|
|
692
|
-
}
|
|
693
781
|
function Chi(a, b, c) {
|
|
694
782
|
return a & b ^ ~a & c;
|
|
695
783
|
}
|
|
696
784
|
function Maj(a, b, c) {
|
|
697
785
|
return a & b ^ a & c ^ b & c;
|
|
698
786
|
}
|
|
699
|
-
class HashMD
|
|
787
|
+
class HashMD {
|
|
788
|
+
blockLen;
|
|
789
|
+
outputLen;
|
|
790
|
+
canXOF = false;
|
|
791
|
+
padOffset;
|
|
792
|
+
isLE;
|
|
793
|
+
buffer;
|
|
794
|
+
view;
|
|
795
|
+
finished = false;
|
|
796
|
+
length = 0;
|
|
797
|
+
pos = 0;
|
|
798
|
+
destroyed = false;
|
|
700
799
|
constructor(blockLen, outputLen, padOffset, isLE) {
|
|
701
|
-
super();
|
|
702
|
-
this.finished = false;
|
|
703
|
-
this.length = 0;
|
|
704
|
-
this.pos = 0;
|
|
705
|
-
this.destroyed = false;
|
|
706
800
|
this.blockLen = blockLen;
|
|
707
801
|
this.outputLen = outputLen;
|
|
708
802
|
this.padOffset = padOffset;
|
|
709
803
|
this.isLE = isLE;
|
|
710
804
|
this.buffer = new Uint8Array(blockLen);
|
|
711
|
-
this.view = createView(this.buffer);
|
|
805
|
+
this.view = createView$1(this.buffer);
|
|
712
806
|
}
|
|
713
807
|
update(data) {
|
|
714
|
-
aexists(this);
|
|
715
|
-
|
|
716
|
-
abytes(data);
|
|
808
|
+
aexists$1(this);
|
|
809
|
+
abytes$1(data);
|
|
717
810
|
const {
|
|
718
811
|
view,
|
|
719
812
|
buffer,
|
|
720
813
|
blockLen
|
|
721
814
|
} = this;
|
|
722
815
|
const len = data.length;
|
|
816
|
+
let processed = false;
|
|
723
817
|
for (let pos = 0; pos < len;) {
|
|
724
818
|
const take = Math.min(blockLen - this.pos, len - pos);
|
|
725
819
|
if (take === blockLen) {
|
|
726
|
-
const dataView = createView(data);
|
|
820
|
+
const dataView = createView$1(data);
|
|
727
821
|
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
|
822
|
+
processed = true;
|
|
728
823
|
continue;
|
|
729
824
|
}
|
|
730
|
-
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
825
|
+
buffer.set(pos === 0 && take === len ? data : data.subarray(pos, pos + take), this.pos);
|
|
731
826
|
this.pos += take;
|
|
732
827
|
pos += take;
|
|
733
828
|
if (this.pos === blockLen) {
|
|
734
829
|
this.process(view, 0);
|
|
735
830
|
this.pos = 0;
|
|
831
|
+
processed = true;
|
|
736
832
|
}
|
|
737
833
|
}
|
|
738
834
|
this.length += data.length;
|
|
739
|
-
this.roundClean();
|
|
835
|
+
if (processed) this.roundClean();
|
|
740
836
|
return this;
|
|
741
837
|
}
|
|
742
838
|
digestInto(out) {
|
|
743
|
-
aexists(this);
|
|
744
|
-
aoutput(out, this);
|
|
839
|
+
aexists$1(this);
|
|
840
|
+
aoutput$1(out, this);
|
|
745
841
|
this.finished = true;
|
|
746
842
|
const {
|
|
747
843
|
buffer,
|
|
@@ -753,20 +849,19 @@ class HashMD extends Hash {
|
|
|
753
849
|
pos
|
|
754
850
|
} = this;
|
|
755
851
|
buffer[pos++] = 0b10000000;
|
|
756
|
-
|
|
852
|
+
buffer.fill(0, pos);
|
|
757
853
|
if (this.padOffset > blockLen - pos) {
|
|
758
854
|
this.process(view, 0);
|
|
759
|
-
|
|
855
|
+
buffer.fill(0);
|
|
760
856
|
}
|
|
761
|
-
|
|
762
|
-
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
|
857
|
+
setU64FromNum(view, blockLen - 8, this.length * 8, isLE);
|
|
763
858
|
this.process(view, 0);
|
|
764
|
-
|
|
859
|
+
this.roundClean();
|
|
860
|
+
const oview = out === buffer ? view : createView$1(out);
|
|
765
861
|
const len = this.outputLen;
|
|
766
|
-
if (len % 4) throw new Error('_sha2: outputLen should be aligned to 32bit');
|
|
767
862
|
const outLen = len / 4;
|
|
768
863
|
const state = this.get();
|
|
769
|
-
if (outLen > state.length) throw new Error('
|
|
864
|
+
if (len % 4 || outLen > state.length) throw new Error('invalid outputLen');
|
|
770
865
|
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
|
771
866
|
}
|
|
772
867
|
digest() {
|
|
@@ -779,11 +874,8 @@ class HashMD extends Hash {
|
|
|
779
874
|
this.destroy();
|
|
780
875
|
return res;
|
|
781
876
|
}
|
|
782
|
-
|
|
783
|
-
to || (to = new this.constructor());
|
|
784
|
-
to.set(...this.get());
|
|
877
|
+
_cloneIntoMeta(to) {
|
|
785
878
|
const {
|
|
786
|
-
blockLen,
|
|
787
879
|
buffer,
|
|
788
880
|
length,
|
|
789
881
|
finished,
|
|
@@ -794,7 +886,7 @@ class HashMD extends Hash {
|
|
|
794
886
|
to.finished = finished;
|
|
795
887
|
to.length = length;
|
|
796
888
|
to.pos = pos;
|
|
797
|
-
if (
|
|
889
|
+
if (pos) to.buffer.set(buffer);
|
|
798
890
|
return to;
|
|
799
891
|
}
|
|
800
892
|
clone() {
|
|
@@ -808,14 +900,14 @@ const SHA512_IV = Uint32Array.from([0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa7
|
|
|
808
900
|
|
|
809
901
|
const SHA1_IV = Uint32Array.from([0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0]);
|
|
810
902
|
const SHA1_W = new Uint32Array(80);
|
|
811
|
-
class
|
|
903
|
+
class _SHA1 extends HashMD {
|
|
904
|
+
A = SHA1_IV[0] | 0;
|
|
905
|
+
B = SHA1_IV[1] | 0;
|
|
906
|
+
C = SHA1_IV[2] | 0;
|
|
907
|
+
D = SHA1_IV[3] | 0;
|
|
908
|
+
E = SHA1_IV[4] | 0;
|
|
812
909
|
constructor() {
|
|
813
910
|
super(64, 20, 8, false);
|
|
814
|
-
this.A = SHA1_IV[0] | 0;
|
|
815
|
-
this.B = SHA1_IV[1] | 0;
|
|
816
|
-
this.C = SHA1_IV[2] | 0;
|
|
817
|
-
this.D = SHA1_IV[3] | 0;
|
|
818
|
-
this.E = SHA1_IV[4] | 0;
|
|
819
911
|
}
|
|
820
912
|
get() {
|
|
821
913
|
const {
|
|
@@ -834,6 +926,10 @@ class SHA1 extends HashMD {
|
|
|
834
926
|
this.D = D | 0;
|
|
835
927
|
this.E = E | 0;
|
|
836
928
|
}
|
|
929
|
+
_cloneInto(to) {
|
|
930
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
931
|
+
return this._cloneIntoMeta(to);
|
|
932
|
+
}
|
|
837
933
|
process(view, offset) {
|
|
838
934
|
for (let i = 0; i < 16; i++, offset += 4) SHA1_W[i] = view.getUint32(offset, false);
|
|
839
935
|
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);
|
|
@@ -874,27 +970,34 @@ class SHA1 extends HashMD {
|
|
|
874
970
|
this.set(A, B, C, D, E);
|
|
875
971
|
}
|
|
876
972
|
roundClean() {
|
|
877
|
-
clean(SHA1_W);
|
|
973
|
+
clean$1(SHA1_W);
|
|
878
974
|
}
|
|
879
975
|
destroy() {
|
|
976
|
+
this.destroyed = true;
|
|
880
977
|
this.set(0, 0, 0, 0, 0);
|
|
881
|
-
clean(this.buffer);
|
|
978
|
+
clean$1(this.buffer);
|
|
882
979
|
}
|
|
883
980
|
}
|
|
884
|
-
createHasher(() => new
|
|
981
|
+
createHasher(() => new _SHA1());
|
|
885
982
|
const p32 = Math.pow(2, 32);
|
|
886
983
|
const K = Array.from({
|
|
887
984
|
length: 64
|
|
888
985
|
}, (_, i) => Math.floor(p32 * Math.abs(Math.sin(i + 1))));
|
|
889
986
|
const MD5_IV = SHA1_IV.slice(0, 4);
|
|
890
987
|
const MD5_W = new Uint32Array(16);
|
|
891
|
-
|
|
988
|
+
const MD5_SHIFTS = (() => {
|
|
989
|
+
const S = [[7, 12, 17, 22], [5, 9, 14, 20], [4, 11, 16, 23], [6, 10, 15, 21]];
|
|
990
|
+
return Uint8Array.from({
|
|
991
|
+
length: 64
|
|
992
|
+
}, (_, i) => S[Math.floor(i / 16)][i % 4]);
|
|
993
|
+
})();
|
|
994
|
+
class _MD5 extends HashMD {
|
|
995
|
+
A = MD5_IV[0] | 0;
|
|
996
|
+
B = MD5_IV[1] | 0;
|
|
997
|
+
C = MD5_IV[2] | 0;
|
|
998
|
+
D = MD5_IV[3] | 0;
|
|
892
999
|
constructor() {
|
|
893
1000
|
super(64, 16, 8, true);
|
|
894
|
-
this.A = MD5_IV[0] | 0;
|
|
895
|
-
this.B = MD5_IV[1] | 0;
|
|
896
|
-
this.C = MD5_IV[2] | 0;
|
|
897
|
-
this.D = MD5_IV[3] | 0;
|
|
898
1001
|
}
|
|
899
1002
|
get() {
|
|
900
1003
|
const {
|
|
@@ -911,6 +1014,10 @@ class MD5 extends HashMD {
|
|
|
911
1014
|
this.C = C | 0;
|
|
912
1015
|
this.D = D | 0;
|
|
913
1016
|
}
|
|
1017
|
+
_cloneInto(to) {
|
|
1018
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1019
|
+
return this._cloneIntoMeta(to);
|
|
1020
|
+
}
|
|
914
1021
|
process(view, offset) {
|
|
915
1022
|
for (let i = 0; i < 16; i++, offset += 4) MD5_W[i] = view.getUint32(offset, true);
|
|
916
1023
|
let {
|
|
@@ -920,29 +1027,25 @@ class MD5 extends HashMD {
|
|
|
920
1027
|
D
|
|
921
1028
|
} = this;
|
|
922
1029
|
for (let i = 0; i < 64; i++) {
|
|
923
|
-
let F, g
|
|
1030
|
+
let F, g;
|
|
924
1031
|
if (i < 16) {
|
|
925
1032
|
F = Chi(B, C, D);
|
|
926
1033
|
g = i;
|
|
927
|
-
s = [7, 12, 17, 22];
|
|
928
1034
|
} else if (i < 32) {
|
|
929
1035
|
F = Chi(D, B, C);
|
|
930
1036
|
g = (5 * i + 1) % 16;
|
|
931
|
-
s = [5, 9, 14, 20];
|
|
932
1037
|
} else if (i < 48) {
|
|
933
1038
|
F = B ^ C ^ D;
|
|
934
1039
|
g = (3 * i + 5) % 16;
|
|
935
|
-
s = [4, 11, 16, 23];
|
|
936
1040
|
} else {
|
|
937
1041
|
F = C ^ (B | ~D);
|
|
938
1042
|
g = 7 * i % 16;
|
|
939
|
-
s = [6, 10, 15, 21];
|
|
940
1043
|
}
|
|
941
1044
|
F = F + A + K[i] + MD5_W[g];
|
|
942
1045
|
A = D;
|
|
943
1046
|
D = C;
|
|
944
1047
|
C = B;
|
|
945
|
-
B = B + rotl(F,
|
|
1048
|
+
B = B + rotl(F, MD5_SHIFTS[i]);
|
|
946
1049
|
}
|
|
947
1050
|
A = A + this.A | 0;
|
|
948
1051
|
B = B + this.B | 0;
|
|
@@ -951,14 +1054,15 @@ class MD5 extends HashMD {
|
|
|
951
1054
|
this.set(A, B, C, D);
|
|
952
1055
|
}
|
|
953
1056
|
roundClean() {
|
|
954
|
-
clean(MD5_W);
|
|
1057
|
+
clean$1(MD5_W);
|
|
955
1058
|
}
|
|
956
1059
|
destroy() {
|
|
1060
|
+
this.destroyed = true;
|
|
957
1061
|
this.set(0, 0, 0, 0);
|
|
958
|
-
clean(this.buffer);
|
|
1062
|
+
clean$1(this.buffer);
|
|
959
1063
|
}
|
|
960
1064
|
}
|
|
961
|
-
const md5 = createHasher(() => new
|
|
1065
|
+
const md5 = createHasher(() => new _MD5());
|
|
962
1066
|
const Rho160 = Uint8Array.from([7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8]);
|
|
963
1067
|
const Id160 = (() => Uint8Array.from(new Array(16).fill(0).map((_, i) => i)))();
|
|
964
1068
|
const Pi160 = (() => Id160.map(i => (9 * i + 5) % 16))();
|
|
@@ -984,14 +1088,14 @@ function ripemd_f(group, x, y, z) {
|
|
|
984
1088
|
return x ^ (y | ~z);
|
|
985
1089
|
}
|
|
986
1090
|
const BUF_160 = new Uint32Array(16);
|
|
987
|
-
class
|
|
1091
|
+
class _RIPEMD160 extends HashMD {
|
|
1092
|
+
h0 = 0x67452301 | 0;
|
|
1093
|
+
h1 = 0xefcdab89 | 0;
|
|
1094
|
+
h2 = 0x98badcfe | 0;
|
|
1095
|
+
h3 = 0x10325476 | 0;
|
|
1096
|
+
h4 = 0xc3d2e1f0 | 0;
|
|
988
1097
|
constructor() {
|
|
989
1098
|
super(64, 20, 8, true);
|
|
990
|
-
this.h0 = 0x67452301 | 0;
|
|
991
|
-
this.h1 = 0xefcdab89 | 0;
|
|
992
|
-
this.h2 = 0x98badcfe | 0;
|
|
993
|
-
this.h3 = 0x10325476 | 0;
|
|
994
|
-
this.h4 = 0xc3d2e1f0 | 0;
|
|
995
1099
|
}
|
|
996
1100
|
get() {
|
|
997
1101
|
const {
|
|
@@ -1010,6 +1114,10 @@ class RIPEMD160 extends HashMD {
|
|
|
1010
1114
|
this.h3 = h3 | 0;
|
|
1011
1115
|
this.h4 = h4 | 0;
|
|
1012
1116
|
}
|
|
1117
|
+
_cloneInto(to) {
|
|
1118
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1119
|
+
return this._cloneIntoMeta(to);
|
|
1120
|
+
}
|
|
1013
1121
|
process(view, offset) {
|
|
1014
1122
|
for (let i = 0; i < 16; i++, offset += 4) BUF_160[i] = view.getUint32(offset, true);
|
|
1015
1123
|
let al = this.h0 | 0,
|
|
@@ -1042,15 +1150,15 @@ class RIPEMD160 extends HashMD {
|
|
|
1042
1150
|
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);
|
|
1043
1151
|
}
|
|
1044
1152
|
roundClean() {
|
|
1045
|
-
clean(BUF_160);
|
|
1153
|
+
clean$1(BUF_160);
|
|
1046
1154
|
}
|
|
1047
1155
|
destroy() {
|
|
1048
1156
|
this.destroyed = true;
|
|
1049
|
-
clean(this.buffer);
|
|
1157
|
+
clean$1(this.buffer);
|
|
1050
1158
|
this.set(0, 0, 0, 0, 0);
|
|
1051
1159
|
}
|
|
1052
1160
|
}
|
|
1053
|
-
createHasher(() => new
|
|
1161
|
+
createHasher(() => new _RIPEMD160());
|
|
1054
1162
|
|
|
1055
1163
|
function md5Hash(data) {
|
|
1056
1164
|
return md5.create().update(data).digest();
|
|
@@ -1059,64 +1167,27 @@ function md5Hex(data) {
|
|
|
1059
1167
|
return bytesToHex(md5Hash(data));
|
|
1060
1168
|
}
|
|
1061
1169
|
|
|
1062
|
-
const U32_MASK64 = BigInt(2 ** 32 - 1);
|
|
1063
|
-
const _32n = BigInt(32);
|
|
1064
|
-
function fromBig(n, le = false) {
|
|
1065
|
-
if (le) return {
|
|
1066
|
-
h: Number(n & U32_MASK64),
|
|
1067
|
-
l: Number(n >> _32n & U32_MASK64)
|
|
1068
|
-
};
|
|
1069
|
-
return {
|
|
1070
|
-
h: Number(n >> _32n & U32_MASK64) | 0,
|
|
1071
|
-
l: Number(n & U32_MASK64) | 0
|
|
1072
|
-
};
|
|
1073
|
-
}
|
|
1074
|
-
function split(lst, le = false) {
|
|
1075
|
-
const len = lst.length;
|
|
1076
|
-
let Ah = new Uint32Array(len);
|
|
1077
|
-
let Al = new Uint32Array(len);
|
|
1078
|
-
for (let i = 0; i < len; i++) {
|
|
1079
|
-
const {
|
|
1080
|
-
h,
|
|
1081
|
-
l
|
|
1082
|
-
} = fromBig(lst[i], le);
|
|
1083
|
-
[Ah[i], Al[i]] = [h, l];
|
|
1084
|
-
}
|
|
1085
|
-
return [Ah, Al];
|
|
1086
|
-
}
|
|
1087
|
-
const shrSH = (h, _l, s) => h >>> s;
|
|
1088
|
-
const shrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1089
|
-
const rotrSH = (h, l, s) => h >>> s | l << 32 - s;
|
|
1090
|
-
const rotrSL = (h, l, s) => h << 32 - s | l >>> s;
|
|
1091
|
-
const rotrBH = (h, l, s) => h << 64 - s | l >>> s - 32;
|
|
1092
|
-
const rotrBL = (h, l, s) => h >>> s - 32 | l << 64 - s;
|
|
1093
|
-
function add(Ah, Al, Bh, Bl) {
|
|
1094
|
-
const l = (Al >>> 0) + (Bl >>> 0);
|
|
1095
|
-
return {
|
|
1096
|
-
h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
|
|
1097
|
-
l: l | 0
|
|
1098
|
-
};
|
|
1099
|
-
}
|
|
1100
|
-
const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
|
1101
|
-
const add3H = (low, Ah, Bh, Ch) => Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
|
|
1102
|
-
const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
|
1103
|
-
const add4H = (low, Ah, Bh, Ch, Dh) => Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
|
|
1104
|
-
const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
|
1105
|
-
const add5H = (low, Ah, Bh, Ch, Dh, Eh) => Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
|
|
1106
|
-
|
|
1107
1170
|
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]);
|
|
1108
1171
|
const SHA256_W = new Uint32Array(64);
|
|
1109
|
-
class
|
|
1110
|
-
|
|
1172
|
+
class SHA2_32B extends HashMD {
|
|
1173
|
+
A = 0;
|
|
1174
|
+
B = 0;
|
|
1175
|
+
C = 0;
|
|
1176
|
+
D = 0;
|
|
1177
|
+
E = 0;
|
|
1178
|
+
F = 0;
|
|
1179
|
+
G = 0;
|
|
1180
|
+
H = 0;
|
|
1181
|
+
constructor(outputLen, IV) {
|
|
1111
1182
|
super(64, outputLen, 8, false);
|
|
1112
|
-
this.A =
|
|
1113
|
-
this.B =
|
|
1114
|
-
this.C =
|
|
1115
|
-
this.D =
|
|
1116
|
-
this.E =
|
|
1117
|
-
this.F =
|
|
1118
|
-
this.G =
|
|
1119
|
-
this.H =
|
|
1183
|
+
this.A = IV[0] | 0;
|
|
1184
|
+
this.B = IV[1] | 0;
|
|
1185
|
+
this.C = IV[2] | 0;
|
|
1186
|
+
this.D = IV[3] | 0;
|
|
1187
|
+
this.E = IV[4] | 0;
|
|
1188
|
+
this.F = IV[5] | 0;
|
|
1189
|
+
this.G = IV[6] | 0;
|
|
1190
|
+
this.H = IV[7] | 0;
|
|
1120
1191
|
}
|
|
1121
1192
|
get() {
|
|
1122
1193
|
const {
|
|
@@ -1141,6 +1212,10 @@ class SHA256 extends HashMD {
|
|
|
1141
1212
|
this.G = G | 0;
|
|
1142
1213
|
this.H = H | 0;
|
|
1143
1214
|
}
|
|
1215
|
+
_cloneInto(to) {
|
|
1216
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1217
|
+
return this._cloneIntoMeta(to);
|
|
1218
|
+
}
|
|
1144
1219
|
process(view, offset) {
|
|
1145
1220
|
for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
|
|
1146
1221
|
for (let i = 16; i < 64; i++) {
|
|
@@ -1185,24 +1260,22 @@ class SHA256 extends HashMD {
|
|
|
1185
1260
|
this.set(A, B, C, D, E, F, G, H);
|
|
1186
1261
|
}
|
|
1187
1262
|
roundClean() {
|
|
1188
|
-
clean(SHA256_W);
|
|
1263
|
+
clean$1(SHA256_W);
|
|
1189
1264
|
}
|
|
1190
1265
|
destroy() {
|
|
1266
|
+
this.destroyed = true;
|
|
1191
1267
|
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
|
1192
|
-
clean(this.buffer);
|
|
1268
|
+
clean$1(this.buffer);
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
class _SHA256 extends SHA2_32B {
|
|
1272
|
+
constructor() {
|
|
1273
|
+
super(32, SHA256_IV);
|
|
1193
1274
|
}
|
|
1194
1275
|
}
|
|
1195
|
-
class
|
|
1276
|
+
class _SHA224 extends SHA2_32B {
|
|
1196
1277
|
constructor() {
|
|
1197
|
-
super(28);
|
|
1198
|
-
this.A = SHA224_IV[0] | 0;
|
|
1199
|
-
this.B = SHA224_IV[1] | 0;
|
|
1200
|
-
this.C = SHA224_IV[2] | 0;
|
|
1201
|
-
this.D = SHA224_IV[3] | 0;
|
|
1202
|
-
this.E = SHA224_IV[4] | 0;
|
|
1203
|
-
this.F = SHA224_IV[5] | 0;
|
|
1204
|
-
this.G = SHA224_IV[6] | 0;
|
|
1205
|
-
this.H = SHA224_IV[7] | 0;
|
|
1278
|
+
super(28, SHA224_IV);
|
|
1206
1279
|
}
|
|
1207
1280
|
}
|
|
1208
1281
|
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))))();
|
|
@@ -1210,25 +1283,41 @@ const SHA512_Kh = (() => K512[0])();
|
|
|
1210
1283
|
const SHA512_Kl = (() => K512[1])();
|
|
1211
1284
|
const SHA512_W_H = new Uint32Array(80);
|
|
1212
1285
|
const SHA512_W_L = new Uint32Array(80);
|
|
1213
|
-
class
|
|
1214
|
-
|
|
1286
|
+
class SHA2_64B extends HashMD {
|
|
1287
|
+
Ah = 0;
|
|
1288
|
+
Al = 0;
|
|
1289
|
+
Bh = 0;
|
|
1290
|
+
Bl = 0;
|
|
1291
|
+
Ch = 0;
|
|
1292
|
+
Cl = 0;
|
|
1293
|
+
Dh = 0;
|
|
1294
|
+
Dl = 0;
|
|
1295
|
+
Eh = 0;
|
|
1296
|
+
El = 0;
|
|
1297
|
+
Fh = 0;
|
|
1298
|
+
Fl = 0;
|
|
1299
|
+
Gh = 0;
|
|
1300
|
+
Gl = 0;
|
|
1301
|
+
Hh = 0;
|
|
1302
|
+
Hl = 0;
|
|
1303
|
+
constructor(outputLen, IV) {
|
|
1215
1304
|
super(128, outputLen, 16, false);
|
|
1216
|
-
this.Ah =
|
|
1217
|
-
this.Al =
|
|
1218
|
-
this.Bh =
|
|
1219
|
-
this.Bl =
|
|
1220
|
-
this.Ch =
|
|
1221
|
-
this.Cl =
|
|
1222
|
-
this.Dh =
|
|
1223
|
-
this.Dl =
|
|
1224
|
-
this.Eh =
|
|
1225
|
-
this.El =
|
|
1226
|
-
this.Fh =
|
|
1227
|
-
this.Fl =
|
|
1228
|
-
this.Gh =
|
|
1229
|
-
this.Gl =
|
|
1230
|
-
this.Hh =
|
|
1231
|
-
this.Hl =
|
|
1305
|
+
this.Ah = IV[0] | 0;
|
|
1306
|
+
this.Al = IV[1] | 0;
|
|
1307
|
+
this.Bh = IV[2] | 0;
|
|
1308
|
+
this.Bl = IV[3] | 0;
|
|
1309
|
+
this.Ch = IV[4] | 0;
|
|
1310
|
+
this.Cl = IV[5] | 0;
|
|
1311
|
+
this.Dh = IV[6] | 0;
|
|
1312
|
+
this.Dl = IV[7] | 0;
|
|
1313
|
+
this.Eh = IV[8] | 0;
|
|
1314
|
+
this.El = IV[9] | 0;
|
|
1315
|
+
this.Fh = IV[10] | 0;
|
|
1316
|
+
this.Fl = IV[11] | 0;
|
|
1317
|
+
this.Gh = IV[12] | 0;
|
|
1318
|
+
this.Gl = IV[13] | 0;
|
|
1319
|
+
this.Hh = IV[14] | 0;
|
|
1320
|
+
this.Hl = IV[15] | 0;
|
|
1232
1321
|
}
|
|
1233
1322
|
get() {
|
|
1234
1323
|
const {
|
|
@@ -1269,6 +1358,10 @@ class SHA512 extends HashMD {
|
|
|
1269
1358
|
this.Hh = Hh | 0;
|
|
1270
1359
|
this.Hl = Hl | 0;
|
|
1271
1360
|
}
|
|
1361
|
+
_cloneInto(to) {
|
|
1362
|
+
(to ||= new this.constructor()).set(...this.get());
|
|
1363
|
+
return this._cloneIntoMeta(to);
|
|
1364
|
+
}
|
|
1272
1365
|
process(view, offset) {
|
|
1273
1366
|
for (let i = 0; i < 16; i++, offset += 4) {
|
|
1274
1367
|
SHA512_W_H[i] = view.getUint32(offset);
|
|
@@ -1372,89 +1465,1569 @@ class SHA512 extends HashMD {
|
|
|
1372
1465
|
} = add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
|
1373
1466
|
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
|
1374
1467
|
}
|
|
1375
|
-
roundClean() {
|
|
1376
|
-
clean(SHA512_W_H, SHA512_W_L);
|
|
1468
|
+
roundClean() {
|
|
1469
|
+
clean$1(SHA512_W_H, SHA512_W_L);
|
|
1470
|
+
}
|
|
1471
|
+
destroy() {
|
|
1472
|
+
this.destroyed = true;
|
|
1473
|
+
clean$1(this.buffer);
|
|
1474
|
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
class _SHA512 extends SHA2_64B {
|
|
1478
|
+
constructor() {
|
|
1479
|
+
super(64, SHA512_IV);
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
class _SHA384 extends SHA2_64B {
|
|
1483
|
+
constructor() {
|
|
1484
|
+
super(48, SHA384_IV);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
const T224_IV = Uint32Array.from([0x8c3d37c8, 0x19544da2, 0x73e19966, 0x89dcd4d6, 0x1dfab7ae, 0x32ff9c82, 0x679dd514, 0x582f9fcf, 0x0f6d2b69, 0x7bd44da8, 0x77e36f73, 0x04c48942, 0x3f9d85a8, 0x6a1d36c8, 0x1112e6ad, 0x91d692a1]);
|
|
1488
|
+
const T256_IV = Uint32Array.from([0x22312194, 0xfc2bf72c, 0x9f555fa3, 0xc84c64c2, 0x2393b86b, 0x6f53b151, 0x96387719, 0x5940eabd, 0x96283ee2, 0xa88effe3, 0xbe5e1e25, 0x53863992, 0x2b0199fc, 0x2c85b8aa, 0x0eb72ddc, 0x81c52ca2]);
|
|
1489
|
+
class _SHA512_224 extends SHA2_64B {
|
|
1490
|
+
constructor() {
|
|
1491
|
+
super(28, T224_IV);
|
|
1492
|
+
}
|
|
1493
|
+
}
|
|
1494
|
+
class _SHA512_256 extends SHA2_64B {
|
|
1495
|
+
constructor() {
|
|
1496
|
+
super(32, T256_IV);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
const sha256 = createHasher(() => new _SHA256(), oidNist(0x01));
|
|
1500
|
+
createHasher(() => new _SHA224(), oidNist(0x04));
|
|
1501
|
+
createHasher(() => new _SHA512(), oidNist(0x03));
|
|
1502
|
+
createHasher(() => new _SHA384(), oidNist(0x02));
|
|
1503
|
+
createHasher(() => new _SHA512_256(), oidNist(0x06));
|
|
1504
|
+
createHasher(() => new _SHA512_224(), oidNist(0x05));
|
|
1505
|
+
|
|
1506
|
+
function sha256Hash(data) {
|
|
1507
|
+
return sha256(data);
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
function isBytes(a) {
|
|
1511
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array' && 'BYTES_PER_ELEMENT' in a && a.BYTES_PER_ELEMENT === 1;
|
|
1512
|
+
}
|
|
1513
|
+
const atitle = title => title ? `"${title}" ` : '';
|
|
1514
|
+
function abool(value, title = '') {
|
|
1515
|
+
if (typeof value !== 'boolean') throw new TypeError(atitle(title) + 'expected boolean, got type=' + typeof value);
|
|
1516
|
+
return value;
|
|
1517
|
+
}
|
|
1518
|
+
function anumber(n, title = '') {
|
|
1519
|
+
if (typeof n !== 'number') throw new TypeError(atitle(title) + 'expected number, got ' + typeof n);
|
|
1520
|
+
if (!Number.isSafeInteger(n) || n < 0) throw new RangeError(atitle(title) + 'expected integer >= 0, got ' + n);
|
|
1521
|
+
return n;
|
|
1522
|
+
}
|
|
1523
|
+
function abytes(value, length, title = '') {
|
|
1524
|
+
if (isBytes(value) && (length === undefined || value.length === length)) return value;
|
|
1525
|
+
if (length !== undefined) anumber(length, 'length');
|
|
1526
|
+
const bytes = isBytes(value);
|
|
1527
|
+
const ofLen = length !== undefined ? ` of length ${length}` : '';
|
|
1528
|
+
const got = bytes ? `length=${value.length}` : `type=${typeof value}`;
|
|
1529
|
+
const message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
1530
|
+
if (!bytes) throw new TypeError(message);
|
|
1531
|
+
throw new RangeError(message);
|
|
1532
|
+
}
|
|
1533
|
+
function aexists(instance, checkFinished = true) {
|
|
1534
|
+
if (instance.destroyed) throw new Error('hash was destroyed');
|
|
1535
|
+
if (checkFinished && instance.finished) throw new Error('digest() was already called');
|
|
1536
|
+
}
|
|
1537
|
+
function aoutput(out, instance) {
|
|
1538
|
+
abytes(out, undefined, 'output');
|
|
1539
|
+
const min = instance.outputLen;
|
|
1540
|
+
if (!(out.length >= min)) {
|
|
1541
|
+
throw new RangeError('"output" expected length >= ' + min);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
function aoutput32(out, instance) {
|
|
1545
|
+
aoutput(out, instance);
|
|
1546
|
+
if (!isAligned32(out)) throw new Error('invalid output, must be aligned');
|
|
1547
|
+
}
|
|
1548
|
+
function u8(arr) {
|
|
1549
|
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1550
|
+
}
|
|
1551
|
+
function u32(arr) {
|
|
1552
|
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
|
1553
|
+
}
|
|
1554
|
+
function clean(...arrays) {
|
|
1555
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
1556
|
+
arrays[i].fill(0);
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
function createView(arr) {
|
|
1560
|
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
1561
|
+
}
|
|
1562
|
+
const isLE = (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
|
1563
|
+
function byteSwap(word) {
|
|
1564
|
+
return word << 24 & 0xff000000 | word << 8 & 0xff0000 | word >>> 8 & 0xff00 | word >>> 24 & 0xff;
|
|
1565
|
+
}
|
|
1566
|
+
const swap8IfBE = isLE ? n => n : n => byteSwap(n) >>> 0;
|
|
1567
|
+
function byteSwap32(arr) {
|
|
1568
|
+
for (let i = 0; i < arr.length; i++) {
|
|
1569
|
+
arr[i] = byteSwap(arr[i]);
|
|
1570
|
+
}
|
|
1571
|
+
return arr;
|
|
1572
|
+
}
|
|
1573
|
+
const swap32IfBE = isLE ? u => u : byteSwap32;
|
|
1574
|
+
(() => typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
|
1575
|
+
Array.from({
|
|
1576
|
+
length: 256
|
|
1577
|
+
}, (_, i) => i.toString(16).padStart(2, '0'));
|
|
1578
|
+
BigInt(0);
|
|
1579
|
+
function overlapBytes(a, b) {
|
|
1580
|
+
if (!a.byteLength || !b.byteLength) return false;
|
|
1581
|
+
return a.buffer === b.buffer && a.byteOffset < b.byteOffset + b.byteLength && b.byteOffset < a.byteOffset + a.byteLength;
|
|
1582
|
+
}
|
|
1583
|
+
function complexOverlapBytes(input, output) {
|
|
1584
|
+
if (overlapBytes(input, output) && input.byteOffset < output.byteOffset) throw new Error('complex overlap of input and output is not supported');
|
|
1585
|
+
}
|
|
1586
|
+
function concatBytes(...arrays) {
|
|
1587
|
+
let sum = 0;
|
|
1588
|
+
for (let i = 0; i < arrays.length; i++) {
|
|
1589
|
+
const a = arrays[i];
|
|
1590
|
+
abytes(a);
|
|
1591
|
+
sum += a.length;
|
|
1592
|
+
}
|
|
1593
|
+
const res = new Uint8Array(sum);
|
|
1594
|
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
|
1595
|
+
const a = arrays[i];
|
|
1596
|
+
res.set(a, pad);
|
|
1597
|
+
pad += a.length;
|
|
1598
|
+
}
|
|
1599
|
+
return res;
|
|
1600
|
+
}
|
|
1601
|
+
function equalBytes(a, b) {
|
|
1602
|
+
a = abytes(a);
|
|
1603
|
+
b = abytes(b);
|
|
1604
|
+
if (a.length !== b.length) return false;
|
|
1605
|
+
let diff = 0;
|
|
1606
|
+
for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
|
|
1607
|
+
return diff === 0;
|
|
1608
|
+
}
|
|
1609
|
+
function wrapMacConstructor(keyLen, macCons, fromMsg) {
|
|
1610
|
+
const mac = macCons;
|
|
1611
|
+
const getArgs = fromMsg || (() => []);
|
|
1612
|
+
const macC = (msg, key) => mac(key, ...getArgs(msg)).update(msg).digest();
|
|
1613
|
+
const tmp = mac(new Uint8Array(keyLen), ...getArgs(new Uint8Array(0)));
|
|
1614
|
+
macC.outputLen = tmp.outputLen;
|
|
1615
|
+
macC.blockLen = tmp.blockLen;
|
|
1616
|
+
macC.create = (key, ...args) => mac(key, ...args);
|
|
1617
|
+
return macC;
|
|
1618
|
+
}
|
|
1619
|
+
const wrapCipher = (params, constructor) => {
|
|
1620
|
+
function wrappedCipher(key, ...args) {
|
|
1621
|
+
abytes(key, undefined, 'key');
|
|
1622
|
+
if (params.nonceLength !== undefined) {
|
|
1623
|
+
const nonce = args[0];
|
|
1624
|
+
abytes(nonce, params.varSizeNonce ? undefined : params.nonceLength, 'nonce');
|
|
1625
|
+
}
|
|
1626
|
+
const tagl = params.tagLength;
|
|
1627
|
+
const aadStart = params.nonceLength !== undefined ? 1 : 0;
|
|
1628
|
+
if (!params.withAAD) {
|
|
1629
|
+
for (let i = aadStart; i < args.length; i++) if (isBytes(args[i])) throw new Error('AAD not supported');
|
|
1630
|
+
}
|
|
1631
|
+
if (params.withAAD && args[aadStart] !== undefined) abytes(args[aadStart], undefined, 'AAD');
|
|
1632
|
+
const cipher = constructor(key, ...args);
|
|
1633
|
+
const checkOutput = (fnLength, output) => {
|
|
1634
|
+
if (output !== undefined) {
|
|
1635
|
+
if (fnLength !== 2) throw new Error('cipher output not supported');
|
|
1636
|
+
abytes(output, undefined, 'output');
|
|
1637
|
+
}
|
|
1638
|
+
};
|
|
1639
|
+
let called = false;
|
|
1640
|
+
const wrCipher = {
|
|
1641
|
+
encrypt(data, output) {
|
|
1642
|
+
if (called) throw new Error('cannot encrypt() twice with same key + nonce');
|
|
1643
|
+
called = true;
|
|
1644
|
+
abytes(data, undefined, 'data');
|
|
1645
|
+
checkOutput(cipher.encrypt.length, output);
|
|
1646
|
+
return cipher.encrypt(data, output);
|
|
1647
|
+
},
|
|
1648
|
+
decrypt(data, output) {
|
|
1649
|
+
abytes(data, undefined, 'data');
|
|
1650
|
+
if (tagl && data.length < tagl) throw new Error('"ciphertext" expected length >= tagLength=' + tagl);
|
|
1651
|
+
checkOutput(cipher.decrypt.length, output);
|
|
1652
|
+
return cipher.decrypt(data, output);
|
|
1653
|
+
}
|
|
1654
|
+
};
|
|
1655
|
+
return wrCipher;
|
|
1656
|
+
}
|
|
1657
|
+
Object.assign(wrappedCipher, params);
|
|
1658
|
+
return wrappedCipher;
|
|
1659
|
+
};
|
|
1660
|
+
function getOutput(expectedLength, out, onlyAligned = true) {
|
|
1661
|
+
if (out === undefined) return new Uint8Array(expectedLength);
|
|
1662
|
+
abytes(out, expectedLength, 'output');
|
|
1663
|
+
if (onlyAligned && !isAligned32(out)) throw new Error('invalid output, must be aligned');
|
|
1664
|
+
return out;
|
|
1665
|
+
}
|
|
1666
|
+
function u64Lengths(dataLength, aadLength, isLE) {
|
|
1667
|
+
anumber(dataLength);
|
|
1668
|
+
anumber(aadLength);
|
|
1669
|
+
abool(isLE);
|
|
1670
|
+
const num = new Uint8Array(16);
|
|
1671
|
+
const view = createView(num);
|
|
1672
|
+
view.setBigUint64(0, BigInt(aadLength), isLE);
|
|
1673
|
+
view.setBigUint64(8, BigInt(dataLength), isLE);
|
|
1674
|
+
return num;
|
|
1675
|
+
}
|
|
1676
|
+
function isAligned32(bytes) {
|
|
1677
|
+
return bytes.byteOffset % 4 === 0;
|
|
1678
|
+
}
|
|
1679
|
+
function copyBytes(bytes) {
|
|
1680
|
+
return Uint8Array.from(abytes(bytes));
|
|
1681
|
+
}
|
|
1682
|
+
|
|
1683
|
+
const BLOCK_SIZE$1 = 16;
|
|
1684
|
+
const ZEROS16 = new Uint8Array(16);
|
|
1685
|
+
const ZEROS32 = u32(ZEROS16);
|
|
1686
|
+
const POLY$1 = 0xe1;
|
|
1687
|
+
const mul2$1 = (s0, s1, s2, s3) => {
|
|
1688
|
+
const hiBit = s3 & 1;
|
|
1689
|
+
return {
|
|
1690
|
+
s3: s2 << 31 | s3 >>> 1,
|
|
1691
|
+
s2: s1 << 31 | s2 >>> 1,
|
|
1692
|
+
s1: s0 << 31 | s1 >>> 1,
|
|
1693
|
+
s0: s0 >>> 1 ^ POLY$1 << 24 & -(hiBit & 1)
|
|
1694
|
+
};
|
|
1695
|
+
};
|
|
1696
|
+
const swapLE = n => (n >>> 0 & 0xff) << 24 | (n >>> 8 & 0xff) << 16 | (n >>> 16 & 0xff) << 8 | n >>> 24 & 0xff | 0;
|
|
1697
|
+
const swap8IfLE = n => swap8IfBE(swapLE(n));
|
|
1698
|
+
function _toGHASHKey(k) {
|
|
1699
|
+
k.reverse();
|
|
1700
|
+
const hiBit = k[15] & 1;
|
|
1701
|
+
let carry = 0;
|
|
1702
|
+
for (let i = 0; i < k.length; i++) {
|
|
1703
|
+
const t = k[i];
|
|
1704
|
+
k[i] = t >>> 1 | carry;
|
|
1705
|
+
carry = (t & 1) << 7;
|
|
1706
|
+
}
|
|
1707
|
+
k[0] ^= -hiBit & 0xe1;
|
|
1708
|
+
return k;
|
|
1709
|
+
}
|
|
1710
|
+
const estimateWindow = bytes => {
|
|
1711
|
+
if (bytes > 64 * 1024) return 8;
|
|
1712
|
+
if (bytes > 1024) return 4;
|
|
1713
|
+
return 2;
|
|
1714
|
+
};
|
|
1715
|
+
class GHASH {
|
|
1716
|
+
blockLen = BLOCK_SIZE$1;
|
|
1717
|
+
outputLen = BLOCK_SIZE$1;
|
|
1718
|
+
s0 = 0;
|
|
1719
|
+
s1 = 0;
|
|
1720
|
+
s2 = 0;
|
|
1721
|
+
s3 = 0;
|
|
1722
|
+
finished = false;
|
|
1723
|
+
destroyed = false;
|
|
1724
|
+
t;
|
|
1725
|
+
W;
|
|
1726
|
+
windowSize;
|
|
1727
|
+
constructor(key, expectedLength) {
|
|
1728
|
+
abytes(key, 16, 'key');
|
|
1729
|
+
key = copyBytes(key);
|
|
1730
|
+
const kView = createView(key);
|
|
1731
|
+
let k0 = kView.getUint32(0, false);
|
|
1732
|
+
let k1 = kView.getUint32(4, false);
|
|
1733
|
+
let k2 = kView.getUint32(8, false);
|
|
1734
|
+
let k3 = kView.getUint32(12, false);
|
|
1735
|
+
const doubles = [];
|
|
1736
|
+
for (let i = 0; i < 128; i++) {
|
|
1737
|
+
doubles.push({
|
|
1738
|
+
s0: swapLE(k0),
|
|
1739
|
+
s1: swapLE(k1),
|
|
1740
|
+
s2: swapLE(k2),
|
|
1741
|
+
s3: swapLE(k3)
|
|
1742
|
+
});
|
|
1743
|
+
({
|
|
1744
|
+
s0: k0,
|
|
1745
|
+
s1: k1,
|
|
1746
|
+
s2: k2,
|
|
1747
|
+
s3: k3
|
|
1748
|
+
} = mul2$1(k0, k1, k2, k3));
|
|
1749
|
+
}
|
|
1750
|
+
const W = estimateWindow(expectedLength || 1024);
|
|
1751
|
+
if (![1, 2, 4, 8].includes(W)) throw new Error('ghash: invalid window size, expected 2, 4 or 8');
|
|
1752
|
+
this.W = W;
|
|
1753
|
+
const bits = 128;
|
|
1754
|
+
const windows = bits / W;
|
|
1755
|
+
const windowSize = this.windowSize = 2 ** W;
|
|
1756
|
+
const items = [];
|
|
1757
|
+
for (let w = 0; w < windows; w++) {
|
|
1758
|
+
for (let byte = 0; byte < windowSize; byte++) {
|
|
1759
|
+
let s0 = 0,
|
|
1760
|
+
s1 = 0,
|
|
1761
|
+
s2 = 0,
|
|
1762
|
+
s3 = 0;
|
|
1763
|
+
for (let j = 0; j < W; j++) {
|
|
1764
|
+
const bit = byte >>> W - j - 1 & 1;
|
|
1765
|
+
if (!bit) continue;
|
|
1766
|
+
const {
|
|
1767
|
+
s0: d0,
|
|
1768
|
+
s1: d1,
|
|
1769
|
+
s2: d2,
|
|
1770
|
+
s3: d3
|
|
1771
|
+
} = doubles[W * w + j];
|
|
1772
|
+
s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
|
|
1773
|
+
}
|
|
1774
|
+
items.push({
|
|
1775
|
+
s0,
|
|
1776
|
+
s1,
|
|
1777
|
+
s2,
|
|
1778
|
+
s3
|
|
1779
|
+
});
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
this.t = items;
|
|
1783
|
+
}
|
|
1784
|
+
_updateBlock(s0, s1, s2, s3) {
|
|
1785
|
+
s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
|
|
1786
|
+
const {
|
|
1787
|
+
W,
|
|
1788
|
+
t,
|
|
1789
|
+
windowSize
|
|
1790
|
+
} = this;
|
|
1791
|
+
let o0 = 0,
|
|
1792
|
+
o1 = 0,
|
|
1793
|
+
o2 = 0,
|
|
1794
|
+
o3 = 0;
|
|
1795
|
+
const mask = (1 << W) - 1;
|
|
1796
|
+
let w = 0;
|
|
1797
|
+
for (const num of [s0, s1, s2, s3]) {
|
|
1798
|
+
for (let bytePos = 0; bytePos < 4; bytePos++) {
|
|
1799
|
+
const byte = num >>> 8 * bytePos & 0xff;
|
|
1800
|
+
for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
|
|
1801
|
+
const bit = byte >>> W * bitPos & mask;
|
|
1802
|
+
const {
|
|
1803
|
+
s0: e0,
|
|
1804
|
+
s1: e1,
|
|
1805
|
+
s2: e2,
|
|
1806
|
+
s3: e3
|
|
1807
|
+
} = t[w * windowSize + bit];
|
|
1808
|
+
o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
|
|
1809
|
+
w += 1;
|
|
1810
|
+
}
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
this.s0 = o0;
|
|
1814
|
+
this.s1 = o1;
|
|
1815
|
+
this.s2 = o2;
|
|
1816
|
+
this.s3 = o3;
|
|
1817
|
+
}
|
|
1818
|
+
update(data) {
|
|
1819
|
+
aexists(this);
|
|
1820
|
+
abytes(data);
|
|
1821
|
+
data = copyBytes(data);
|
|
1822
|
+
const b32 = u32(data);
|
|
1823
|
+
const blocks = Math.floor(data.length / BLOCK_SIZE$1);
|
|
1824
|
+
const left = data.length % BLOCK_SIZE$1;
|
|
1825
|
+
for (let i = 0; i < blocks; i++) {
|
|
1826
|
+
this._updateBlock(swap8IfBE(b32[i * 4 + 0]), swap8IfBE(b32[i * 4 + 1]), swap8IfBE(b32[i * 4 + 2]), swap8IfBE(b32[i * 4 + 3]));
|
|
1827
|
+
}
|
|
1828
|
+
if (left) {
|
|
1829
|
+
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
|
|
1830
|
+
this._updateBlock(swap8IfBE(ZEROS32[0]), swap8IfBE(ZEROS32[1]), swap8IfBE(ZEROS32[2]), swap8IfBE(ZEROS32[3]));
|
|
1831
|
+
clean(ZEROS32);
|
|
1832
|
+
}
|
|
1833
|
+
return this;
|
|
1834
|
+
}
|
|
1835
|
+
destroy() {
|
|
1836
|
+
this.destroyed = true;
|
|
1837
|
+
const {
|
|
1838
|
+
t
|
|
1839
|
+
} = this;
|
|
1840
|
+
for (const elm of t) {
|
|
1841
|
+
elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
digestInto(out) {
|
|
1845
|
+
aexists(this);
|
|
1846
|
+
aoutput32(out, this);
|
|
1847
|
+
this.finished = true;
|
|
1848
|
+
const {
|
|
1849
|
+
s0,
|
|
1850
|
+
s1,
|
|
1851
|
+
s2,
|
|
1852
|
+
s3
|
|
1853
|
+
} = this;
|
|
1854
|
+
const o32 = u32(out);
|
|
1855
|
+
o32[0] = s0;
|
|
1856
|
+
o32[1] = s1;
|
|
1857
|
+
o32[2] = s2;
|
|
1858
|
+
o32[3] = s3;
|
|
1859
|
+
if (!isLE) swap32IfBE(o32.subarray(0, BLOCK_SIZE$1 / 4));
|
|
1860
|
+
}
|
|
1861
|
+
digest() {
|
|
1862
|
+
const res = new Uint8Array(BLOCK_SIZE$1);
|
|
1863
|
+
this.digestInto(res);
|
|
1864
|
+
this.destroy();
|
|
1865
|
+
return res;
|
|
1866
|
+
}
|
|
1867
|
+
}
|
|
1868
|
+
class Polyval extends GHASH {
|
|
1869
|
+
constructor(key, expectedLength) {
|
|
1870
|
+
abytes(key);
|
|
1871
|
+
const ghKey = _toGHASHKey(copyBytes(key));
|
|
1872
|
+
super(ghKey, expectedLength);
|
|
1873
|
+
clean(ghKey);
|
|
1874
|
+
}
|
|
1875
|
+
update(data) {
|
|
1876
|
+
aexists(this);
|
|
1877
|
+
abytes(data);
|
|
1878
|
+
data = copyBytes(data);
|
|
1879
|
+
const b32 = u32(data);
|
|
1880
|
+
const left = data.length % BLOCK_SIZE$1;
|
|
1881
|
+
const blocks = Math.floor(data.length / BLOCK_SIZE$1);
|
|
1882
|
+
for (let i = 0; i < blocks; i++) {
|
|
1883
|
+
this._updateBlock(swap8IfLE(b32[i * 4 + 3]), swap8IfLE(b32[i * 4 + 2]), swap8IfLE(b32[i * 4 + 1]), swap8IfLE(b32[i * 4 + 0]));
|
|
1884
|
+
}
|
|
1885
|
+
if (left) {
|
|
1886
|
+
ZEROS16.set(data.subarray(blocks * BLOCK_SIZE$1));
|
|
1887
|
+
this._updateBlock(swap8IfLE(ZEROS32[3]), swap8IfLE(ZEROS32[2]), swap8IfLE(ZEROS32[1]), swap8IfLE(ZEROS32[0]));
|
|
1888
|
+
clean(ZEROS32);
|
|
1889
|
+
}
|
|
1890
|
+
return this;
|
|
1891
|
+
}
|
|
1892
|
+
digestInto(out) {
|
|
1893
|
+
aexists(this);
|
|
1894
|
+
aoutput32(out, this);
|
|
1895
|
+
this.finished = true;
|
|
1896
|
+
const view = out.subarray(0, this.outputLen);
|
|
1897
|
+
const {
|
|
1898
|
+
s0,
|
|
1899
|
+
s1,
|
|
1900
|
+
s2,
|
|
1901
|
+
s3
|
|
1902
|
+
} = this;
|
|
1903
|
+
const o32 = u32(view);
|
|
1904
|
+
o32[0] = s0;
|
|
1905
|
+
o32[1] = s1;
|
|
1906
|
+
o32[2] = s2;
|
|
1907
|
+
o32[3] = s3;
|
|
1908
|
+
swap32IfBE(o32);
|
|
1909
|
+
view.reverse();
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
const ghash = wrapMacConstructor(16, (key, expectedLength) => new GHASH(key, expectedLength), msg => [msg.length]);
|
|
1913
|
+
const polyval = wrapMacConstructor(16, (key, expectedLength) => new Polyval(key, expectedLength), msg => [msg.length]);
|
|
1914
|
+
|
|
1915
|
+
const BLOCK_SIZE = 16;
|
|
1916
|
+
const BLOCK_SIZE32 = 4;
|
|
1917
|
+
const EMPTY_BLOCK = new Uint8Array(BLOCK_SIZE);
|
|
1918
|
+
const ONE_BLOCK = Uint8Array.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]);
|
|
1919
|
+
const POLY = 0x11b;
|
|
1920
|
+
function validateKeyLength(key) {
|
|
1921
|
+
if (![16, 24, 32].includes(key.length)) throw new Error('"aes key" expected Uint8Array of length 16/24/32, got length=' + key.length);
|
|
1922
|
+
}
|
|
1923
|
+
function mul2(n) {
|
|
1924
|
+
return n << 1 ^ POLY & -(n >> 7);
|
|
1925
|
+
}
|
|
1926
|
+
function mul(a, b) {
|
|
1927
|
+
let res = 0;
|
|
1928
|
+
for (; b > 0; b >>= 1) {
|
|
1929
|
+
res ^= a & -(b & 1);
|
|
1930
|
+
a = mul2(a);
|
|
1931
|
+
}
|
|
1932
|
+
return res;
|
|
1933
|
+
}
|
|
1934
|
+
const sbox = (() => {
|
|
1935
|
+
const t = new Uint8Array(256);
|
|
1936
|
+
for (let i = 0, x = 1; i < 256; i++, x ^= mul2(x)) t[i] = x;
|
|
1937
|
+
const box = new Uint8Array(256);
|
|
1938
|
+
box[0] = 0x63;
|
|
1939
|
+
for (let i = 0; i < 255; i++) {
|
|
1940
|
+
let x = t[255 - i];
|
|
1941
|
+
x |= x << 8;
|
|
1942
|
+
box[t[i]] = (x ^ x >> 4 ^ x >> 5 ^ x >> 6 ^ x >> 7 ^ 0x63) & 0xff;
|
|
1943
|
+
}
|
|
1944
|
+
clean(t);
|
|
1945
|
+
return box;
|
|
1946
|
+
})();
|
|
1947
|
+
const invSbox = sbox.map((_, j) => sbox.indexOf(j));
|
|
1948
|
+
const rotr32_8 = n => n << 24 | n >>> 8;
|
|
1949
|
+
const rotl32_8 = n => n << 8 | n >>> 24;
|
|
1950
|
+
function genTtable(sbox, fn) {
|
|
1951
|
+
if (sbox.length !== 256) throw new Error('wrong sbox length');
|
|
1952
|
+
const T0 = new Uint32Array(256).map((_, j) => fn(sbox[j]));
|
|
1953
|
+
const T1 = T0.map(rotl32_8);
|
|
1954
|
+
const T2 = T1.map(rotl32_8);
|
|
1955
|
+
const T3 = T2.map(rotl32_8);
|
|
1956
|
+
const T01 = new Uint32Array(256 * 256);
|
|
1957
|
+
const T23 = new Uint32Array(256 * 256);
|
|
1958
|
+
const sbox2 = new Uint16Array(256 * 256);
|
|
1959
|
+
for (let i = 0; i < 256; i++) {
|
|
1960
|
+
for (let j = 0; j < 256; j++) {
|
|
1961
|
+
const idx = i * 256 + j;
|
|
1962
|
+
T01[idx] = T0[i] ^ T1[j];
|
|
1963
|
+
T23[idx] = T2[i] ^ T3[j];
|
|
1964
|
+
sbox2[idx] = sbox[i] << 8 | sbox[j];
|
|
1965
|
+
}
|
|
1966
|
+
}
|
|
1967
|
+
return {
|
|
1968
|
+
sbox,
|
|
1969
|
+
sbox2,
|
|
1970
|
+
T0,
|
|
1971
|
+
T1,
|
|
1972
|
+
T2,
|
|
1973
|
+
T3,
|
|
1974
|
+
T01,
|
|
1975
|
+
T23
|
|
1976
|
+
};
|
|
1977
|
+
}
|
|
1978
|
+
const tableEncoding = genTtable(sbox, s => mul(s, 3) << 24 | s << 16 | s << 8 | mul(s, 2));
|
|
1979
|
+
const tableDecoding = genTtable(invSbox, s => mul(s, 11) << 24 | mul(s, 13) << 16 | mul(s, 9) << 8 | mul(s, 14));
|
|
1980
|
+
const xPowers = (() => {
|
|
1981
|
+
const p = new Uint8Array(16);
|
|
1982
|
+
for (let i = 0, x = 1; i < 16; i++, x = mul2(x)) p[i] = x;
|
|
1983
|
+
return p;
|
|
1984
|
+
})();
|
|
1985
|
+
function expandKeyLE(key) {
|
|
1986
|
+
abytes(key);
|
|
1987
|
+
const len = key.length;
|
|
1988
|
+
validateKeyLength(key);
|
|
1989
|
+
const {
|
|
1990
|
+
sbox2
|
|
1991
|
+
} = tableEncoding;
|
|
1992
|
+
const toClean = [];
|
|
1993
|
+
if (!isLE || !isAligned32(key)) toClean.push(key = copyBytes(key));
|
|
1994
|
+
const k32 = swap32IfBE(u32(key));
|
|
1995
|
+
const Nk = k32.length;
|
|
1996
|
+
const subByte = n => applySbox(sbox2, n, n, n, n);
|
|
1997
|
+
const xk = new Uint32Array(len + 28);
|
|
1998
|
+
xk.set(k32);
|
|
1999
|
+
for (let i = Nk; i < xk.length; i++) {
|
|
2000
|
+
let t = xk[i - 1];
|
|
2001
|
+
if (i % Nk === 0) t = subByte(rotr32_8(t)) ^ xPowers[i / Nk - 1];else if (Nk > 6 && i % Nk === 4) t = subByte(t);
|
|
2002
|
+
xk[i] = xk[i - Nk] ^ t;
|
|
2003
|
+
}
|
|
2004
|
+
clean(...toClean);
|
|
2005
|
+
return xk;
|
|
2006
|
+
}
|
|
2007
|
+
function expandKeyDecLE(key) {
|
|
2008
|
+
const encKey = expandKeyLE(key);
|
|
2009
|
+
const xk = encKey.slice();
|
|
2010
|
+
const Nk = encKey.length;
|
|
2011
|
+
const {
|
|
2012
|
+
sbox2
|
|
2013
|
+
} = tableEncoding;
|
|
2014
|
+
const {
|
|
2015
|
+
T0,
|
|
2016
|
+
T1,
|
|
2017
|
+
T2,
|
|
2018
|
+
T3
|
|
2019
|
+
} = tableDecoding;
|
|
2020
|
+
for (let i = 0; i < Nk; i += 4) {
|
|
2021
|
+
for (let j = 0; j < 4; j++) xk[i + j] = encKey[Nk - i - 4 + j];
|
|
2022
|
+
}
|
|
2023
|
+
clean(encKey);
|
|
2024
|
+
for (let i = 4; i < Nk - 4; i++) {
|
|
2025
|
+
const x = xk[i];
|
|
2026
|
+
const w = applySbox(sbox2, x, x, x, x);
|
|
2027
|
+
xk[i] = T0[w & 0xff] ^ T1[w >>> 8 & 0xff] ^ T2[w >>> 16 & 0xff] ^ T3[w >>> 24];
|
|
2028
|
+
}
|
|
2029
|
+
return xk;
|
|
2030
|
+
}
|
|
2031
|
+
function apply0123(T01, T23, s0, s1, s2, s3) {
|
|
2032
|
+
return T01[s0 << 8 & 0xff00 | s1 >>> 8 & 0xff] ^ T23[s2 >>> 8 & 0xff00 | s3 >>> 24 & 0xff];
|
|
2033
|
+
}
|
|
2034
|
+
function applySbox(sbox2, s0, s1, s2, s3) {
|
|
2035
|
+
return sbox2[s0 & 0xff | s1 & 0xff00] | sbox2[s2 >>> 16 & 0xff | s3 >>> 16 & 0xff00] << 16;
|
|
2036
|
+
}
|
|
2037
|
+
function encrypt(xk, s0, s1, s2, s3) {
|
|
2038
|
+
const {
|
|
2039
|
+
sbox2,
|
|
2040
|
+
T01,
|
|
2041
|
+
T23
|
|
2042
|
+
} = tableEncoding;
|
|
2043
|
+
let k = 0;
|
|
2044
|
+
s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
|
|
2045
|
+
const rounds = xk.length / 4 - 2;
|
|
2046
|
+
for (let i = 0; i < rounds; i++) {
|
|
2047
|
+
const t0 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
|
|
2048
|
+
const t1 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
|
|
2049
|
+
const t2 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
|
|
2050
|
+
const t3 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
|
|
2051
|
+
s0 = t0, s1 = t1, s2 = t2, s3 = t3;
|
|
2052
|
+
}
|
|
2053
|
+
const t0 = xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3);
|
|
2054
|
+
const t1 = xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0);
|
|
2055
|
+
const t2 = xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1);
|
|
2056
|
+
const t3 = xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2);
|
|
2057
|
+
return {
|
|
2058
|
+
s0: t0,
|
|
2059
|
+
s1: t1,
|
|
2060
|
+
s2: t2,
|
|
2061
|
+
s3: t3
|
|
2062
|
+
};
|
|
2063
|
+
}
|
|
2064
|
+
function decrypt(xk, s0, s1, s2, s3) {
|
|
2065
|
+
const {
|
|
2066
|
+
sbox2,
|
|
2067
|
+
T01,
|
|
2068
|
+
T23
|
|
2069
|
+
} = tableDecoding;
|
|
2070
|
+
let k = 0;
|
|
2071
|
+
s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
|
|
2072
|
+
const rounds = xk.length / 4 - 2;
|
|
2073
|
+
for (let i = 0; i < rounds; i++) {
|
|
2074
|
+
const t0 = xk[k++] ^ apply0123(T01, T23, s0, s3, s2, s1);
|
|
2075
|
+
const t1 = xk[k++] ^ apply0123(T01, T23, s1, s0, s3, s2);
|
|
2076
|
+
const t2 = xk[k++] ^ apply0123(T01, T23, s2, s1, s0, s3);
|
|
2077
|
+
const t3 = xk[k++] ^ apply0123(T01, T23, s3, s2, s1, s0);
|
|
2078
|
+
s0 = t0, s1 = t1, s2 = t2, s3 = t3;
|
|
2079
|
+
}
|
|
2080
|
+
const t0 = xk[k++] ^ applySbox(sbox2, s0, s3, s2, s1);
|
|
2081
|
+
const t1 = xk[k++] ^ applySbox(sbox2, s1, s0, s3, s2);
|
|
2082
|
+
const t2 = xk[k++] ^ applySbox(sbox2, s2, s1, s0, s3);
|
|
2083
|
+
const t3 = xk[k++] ^ applySbox(sbox2, s3, s2, s1, s0);
|
|
2084
|
+
return {
|
|
2085
|
+
s0: t0,
|
|
2086
|
+
s1: t1,
|
|
2087
|
+
s2: t2,
|
|
2088
|
+
s3: t3
|
|
2089
|
+
};
|
|
2090
|
+
}
|
|
2091
|
+
function ctrCounter(xk, nonce, src, dst) {
|
|
2092
|
+
abytes(nonce, BLOCK_SIZE, 'nonce');
|
|
2093
|
+
abytes(src);
|
|
2094
|
+
const srcLen = src.length;
|
|
2095
|
+
dst = getOutput(srcLen, dst);
|
|
2096
|
+
complexOverlapBytes(src, dst);
|
|
2097
|
+
const ctr = nonce;
|
|
2098
|
+
const c32 = u32(ctr);
|
|
2099
|
+
const src32 = u32(src);
|
|
2100
|
+
const dst32 = u32(dst);
|
|
2101
|
+
for (let i = 0; i + 4 <= src32.length; i += 4) {
|
|
2102
|
+
const {
|
|
2103
|
+
s0,
|
|
2104
|
+
s1,
|
|
2105
|
+
s2,
|
|
2106
|
+
s3
|
|
2107
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2108
|
+
dst32[i + 0] = src32[i + 0] ^ swap8IfBE(s0);
|
|
2109
|
+
dst32[i + 1] = src32[i + 1] ^ swap8IfBE(s1);
|
|
2110
|
+
dst32[i + 2] = src32[i + 2] ^ swap8IfBE(s2);
|
|
2111
|
+
dst32[i + 3] = src32[i + 3] ^ swap8IfBE(s3);
|
|
2112
|
+
for (let j = BLOCK_SIZE - 1, carry = 1; j >= 0; j--) {
|
|
2113
|
+
carry = carry + ctr[j] | 0;
|
|
2114
|
+
ctr[j] = carry & 0xff;
|
|
2115
|
+
carry >>>= 8;
|
|
2116
|
+
}
|
|
2117
|
+
}
|
|
2118
|
+
const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
|
|
2119
|
+
if (start < srcLen) {
|
|
2120
|
+
const {
|
|
2121
|
+
s0,
|
|
2122
|
+
s1,
|
|
2123
|
+
s2,
|
|
2124
|
+
s3
|
|
2125
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2126
|
+
const b32 = new Uint32Array([s0, s1, s2, s3]);
|
|
2127
|
+
swap32IfBE(b32);
|
|
2128
|
+
const buf = u8(b32);
|
|
2129
|
+
for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
|
|
2130
|
+
clean(b32);
|
|
2131
|
+
}
|
|
2132
|
+
return dst;
|
|
2133
|
+
}
|
|
2134
|
+
function ctr32(xk, isLE, nonce, src, dst) {
|
|
2135
|
+
abytes(nonce, BLOCK_SIZE, 'nonce');
|
|
2136
|
+
abytes(src);
|
|
2137
|
+
dst = getOutput(src.length, dst);
|
|
2138
|
+
const ctr = nonce;
|
|
2139
|
+
const c32 = u32(ctr);
|
|
2140
|
+
const view = createView(ctr);
|
|
2141
|
+
const src32 = u32(src);
|
|
2142
|
+
const dst32 = u32(dst);
|
|
2143
|
+
const ctrPos = isLE ? 0 : 12;
|
|
2144
|
+
const srcLen = src.length;
|
|
2145
|
+
let ctrNum = view.getUint32(ctrPos, isLE);
|
|
2146
|
+
for (let i = 0; i + 4 <= src32.length; i += 4) {
|
|
2147
|
+
const {
|
|
2148
|
+
s0,
|
|
2149
|
+
s1,
|
|
2150
|
+
s2,
|
|
2151
|
+
s3
|
|
2152
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2153
|
+
dst32[i + 0] = src32[i + 0] ^ swap8IfBE(s0);
|
|
2154
|
+
dst32[i + 1] = src32[i + 1] ^ swap8IfBE(s1);
|
|
2155
|
+
dst32[i + 2] = src32[i + 2] ^ swap8IfBE(s2);
|
|
2156
|
+
dst32[i + 3] = src32[i + 3] ^ swap8IfBE(s3);
|
|
2157
|
+
ctrNum = ctrNum + 1 >>> 0;
|
|
2158
|
+
view.setUint32(ctrPos, ctrNum, isLE);
|
|
2159
|
+
}
|
|
2160
|
+
const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
|
|
2161
|
+
if (start < srcLen) {
|
|
2162
|
+
const {
|
|
2163
|
+
s0,
|
|
2164
|
+
s1,
|
|
2165
|
+
s2,
|
|
2166
|
+
s3
|
|
2167
|
+
} = encrypt(xk, swap8IfBE(c32[0]), swap8IfBE(c32[1]), swap8IfBE(c32[2]), swap8IfBE(c32[3]));
|
|
2168
|
+
const b32 = new Uint32Array([s0, s1, s2, s3]);
|
|
2169
|
+
swap32IfBE(b32);
|
|
2170
|
+
const buf = u8(b32);
|
|
2171
|
+
for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
|
|
2172
|
+
clean(b32);
|
|
2173
|
+
}
|
|
2174
|
+
return dst;
|
|
2175
|
+
}
|
|
2176
|
+
const ctr = wrapCipher({
|
|
2177
|
+
blockSize: 16,
|
|
2178
|
+
nonceLength: 16
|
|
2179
|
+
}, function aesctr(key, nonce) {
|
|
2180
|
+
function processCtr(buf, dst) {
|
|
2181
|
+
abytes(buf);
|
|
2182
|
+
if (dst !== undefined) {
|
|
2183
|
+
abytes(dst);
|
|
2184
|
+
if (!isAligned32(dst)) throw new Error('unaligned destination');
|
|
2185
|
+
}
|
|
2186
|
+
const xk = expandKeyLE(key);
|
|
2187
|
+
const n = copyBytes(nonce);
|
|
2188
|
+
const toClean = [xk, n];
|
|
2189
|
+
if (!isAligned32(buf)) toClean.push(buf = copyBytes(buf));
|
|
2190
|
+
const out = ctrCounter(xk, n, buf, dst);
|
|
2191
|
+
clean(...toClean);
|
|
2192
|
+
return out;
|
|
2193
|
+
}
|
|
2194
|
+
return {
|
|
2195
|
+
encrypt: (plaintext, dst) => processCtr(plaintext, dst),
|
|
2196
|
+
decrypt: (ciphertext, dst) => processCtr(ciphertext, dst)
|
|
2197
|
+
};
|
|
2198
|
+
});
|
|
2199
|
+
function validateBlockDecrypt(data, dst) {
|
|
2200
|
+
abytes(data);
|
|
2201
|
+
if (data.length % BLOCK_SIZE !== 0) {
|
|
2202
|
+
throw new Error('ciphertext must be multiple of ' + BLOCK_SIZE);
|
|
2203
|
+
}
|
|
2204
|
+
if (dst !== undefined) {
|
|
2205
|
+
getOutput(data.length, dst);
|
|
2206
|
+
complexOverlapBytes(data, dst);
|
|
2207
|
+
}
|
|
2208
|
+
}
|
|
2209
|
+
function validateBlockEncrypt(plaintext, pkcs5, dst) {
|
|
2210
|
+
abytes(plaintext);
|
|
2211
|
+
let outLen = plaintext.length;
|
|
2212
|
+
const remaining = outLen % BLOCK_SIZE;
|
|
2213
|
+
if (!pkcs5 && remaining !== 0) throw new Error('plaintext must be multiple of ' + BLOCK_SIZE);
|
|
2214
|
+
if (pkcs5) {
|
|
2215
|
+
let left = BLOCK_SIZE - remaining;
|
|
2216
|
+
if (!left) left = BLOCK_SIZE;
|
|
2217
|
+
outLen = outLen + left;
|
|
2218
|
+
}
|
|
2219
|
+
if (dst !== undefined) {
|
|
2220
|
+
getOutput(outLen, dst);
|
|
2221
|
+
complexOverlapBytes(plaintext, dst);
|
|
2222
|
+
}
|
|
2223
|
+
return outLen;
|
|
2224
|
+
}
|
|
2225
|
+
function prepareBlockEncrypt(plaintext, outLen, dst) {
|
|
2226
|
+
if (dst === undefined) dst = new Uint8Array(outLen);
|
|
2227
|
+
if (!isLE || !isAligned32(plaintext)) plaintext = copyBytes(plaintext);
|
|
2228
|
+
const o = u32(dst);
|
|
2229
|
+
return {
|
|
2230
|
+
b: plaintext,
|
|
2231
|
+
o,
|
|
2232
|
+
out: dst
|
|
2233
|
+
};
|
|
2234
|
+
}
|
|
2235
|
+
function validatePKCS(data, pkcs5) {
|
|
2236
|
+
if (!pkcs5) return data;
|
|
2237
|
+
const len = data.length;
|
|
2238
|
+
if (len === 0) throw new Error('pkcs7: empty ciphertext not allowed');
|
|
2239
|
+
const lastByte = data[len - 1];
|
|
2240
|
+
let valid = 1;
|
|
2241
|
+
valid &= lastByte - 1 >>> 31 ^ 1;
|
|
2242
|
+
valid &= 16 - lastByte >>> 31 ^ 1;
|
|
2243
|
+
for (let i = 0; i < 16; i++) {
|
|
2244
|
+
const shouldCheck = i - lastByte >>> 31;
|
|
2245
|
+
const eq = (data[len - 1 - i] ^ lastByte) === 0 ? 1 : 0;
|
|
2246
|
+
valid &= eq | shouldCheck ^ 1;
|
|
2247
|
+
}
|
|
2248
|
+
if (!valid) throw new Error('aes: bad decrypt');
|
|
2249
|
+
return data.subarray(0, len - lastByte);
|
|
2250
|
+
}
|
|
2251
|
+
function padPCKS(left) {
|
|
2252
|
+
const tmp = new Uint8Array(16);
|
|
2253
|
+
const tmp32 = u32(tmp);
|
|
2254
|
+
tmp.set(left);
|
|
2255
|
+
const paddingByte = BLOCK_SIZE - left.length;
|
|
2256
|
+
for (let i = BLOCK_SIZE - paddingByte; i < BLOCK_SIZE; i++) tmp[i] = paddingByte;
|
|
2257
|
+
return tmp32;
|
|
2258
|
+
}
|
|
2259
|
+
const ecb = wrapCipher({
|
|
2260
|
+
blockSize: 16
|
|
2261
|
+
}, function aesecb(key, opts = {}) {
|
|
2262
|
+
const pkcs5 = !opts.disablePadding;
|
|
2263
|
+
return {
|
|
2264
|
+
encrypt(plaintext, dst) {
|
|
2265
|
+
const outLen = validateBlockEncrypt(plaintext, pkcs5, dst);
|
|
2266
|
+
const xk = expandKeyLE(key);
|
|
2267
|
+
const {
|
|
2268
|
+
b: input,
|
|
2269
|
+
o,
|
|
2270
|
+
out: _out
|
|
2271
|
+
} = prepareBlockEncrypt(plaintext, outLen, dst);
|
|
2272
|
+
const b = u32(input);
|
|
2273
|
+
swap32IfBE(b);
|
|
2274
|
+
let i = 0;
|
|
2275
|
+
for (; i + 4 <= b.length;) {
|
|
2276
|
+
const {
|
|
2277
|
+
s0,
|
|
2278
|
+
s1,
|
|
2279
|
+
s2,
|
|
2280
|
+
s3
|
|
2281
|
+
} = encrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
|
|
2282
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2283
|
+
}
|
|
2284
|
+
if (pkcs5) {
|
|
2285
|
+
const tmp32 = padPCKS(plaintext.subarray(i * 4));
|
|
2286
|
+
swap32IfBE(tmp32);
|
|
2287
|
+
const {
|
|
2288
|
+
s0,
|
|
2289
|
+
s1,
|
|
2290
|
+
s2,
|
|
2291
|
+
s3
|
|
2292
|
+
} = encrypt(xk, tmp32[0], tmp32[1], tmp32[2], tmp32[3]);
|
|
2293
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2294
|
+
clean(tmp32);
|
|
2295
|
+
}
|
|
2296
|
+
swap32IfBE(o);
|
|
2297
|
+
clean(xk);
|
|
2298
|
+
if (input !== plaintext) clean(input);
|
|
2299
|
+
return _out;
|
|
2300
|
+
},
|
|
2301
|
+
decrypt(ciphertext, dst) {
|
|
2302
|
+
validateBlockDecrypt(ciphertext, dst);
|
|
2303
|
+
const xk = expandKeyDecLE(key);
|
|
2304
|
+
if (dst === undefined) dst = new Uint8Array(ciphertext.length);
|
|
2305
|
+
const toClean = [xk];
|
|
2306
|
+
if (!isLE || !isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2307
|
+
const b = u32(ciphertext);
|
|
2308
|
+
const o = u32(dst);
|
|
2309
|
+
swap32IfBE(b);
|
|
2310
|
+
for (let i = 0; i + 4 <= b.length;) {
|
|
2311
|
+
const {
|
|
2312
|
+
s0,
|
|
2313
|
+
s1,
|
|
2314
|
+
s2,
|
|
2315
|
+
s3
|
|
2316
|
+
} = decrypt(xk, b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
|
|
2317
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2318
|
+
}
|
|
2319
|
+
swap32IfBE(o);
|
|
2320
|
+
clean(...toClean);
|
|
2321
|
+
return validatePKCS(dst, pkcs5);
|
|
2322
|
+
}
|
|
2323
|
+
};
|
|
2324
|
+
});
|
|
2325
|
+
const cbc = wrapCipher({
|
|
2326
|
+
blockSize: 16,
|
|
2327
|
+
nonceLength: 16
|
|
2328
|
+
}, function aescbc(key, iv, opts = {}) {
|
|
2329
|
+
const pkcs5 = !opts.disablePadding;
|
|
2330
|
+
return {
|
|
2331
|
+
encrypt(plaintext, dst) {
|
|
2332
|
+
const outLen = validateBlockEncrypt(plaintext, pkcs5, dst);
|
|
2333
|
+
const xk = expandKeyLE(key);
|
|
2334
|
+
const {
|
|
2335
|
+
b: input,
|
|
2336
|
+
o,
|
|
2337
|
+
out: _out
|
|
2338
|
+
} = prepareBlockEncrypt(plaintext, outLen, dst);
|
|
2339
|
+
const b = u32(input);
|
|
2340
|
+
swap32IfBE(b);
|
|
2341
|
+
let _iv = iv;
|
|
2342
|
+
const toClean = [xk];
|
|
2343
|
+
if (!isLE || !isAligned32(_iv)) toClean.push(_iv = copyBytes(_iv));
|
|
2344
|
+
const n32 = u32(_iv);
|
|
2345
|
+
swap32IfBE(n32);
|
|
2346
|
+
let s0 = n32[0],
|
|
2347
|
+
s1 = n32[1],
|
|
2348
|
+
s2 = n32[2],
|
|
2349
|
+
s3 = n32[3];
|
|
2350
|
+
let i = 0;
|
|
2351
|
+
for (; i + 4 <= b.length;) {
|
|
2352
|
+
s0 ^= b[i + 0], s1 ^= b[i + 1], s2 ^= b[i + 2], s3 ^= b[i + 3];
|
|
2353
|
+
({
|
|
2354
|
+
s0,
|
|
2355
|
+
s1,
|
|
2356
|
+
s2,
|
|
2357
|
+
s3
|
|
2358
|
+
} = encrypt(xk, s0, s1, s2, s3));
|
|
2359
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2360
|
+
}
|
|
2361
|
+
if (pkcs5) {
|
|
2362
|
+
const tmp32 = padPCKS(plaintext.subarray(i * 4));
|
|
2363
|
+
swap32IfBE(tmp32);
|
|
2364
|
+
s0 ^= tmp32[0], s1 ^= tmp32[1], s2 ^= tmp32[2], s3 ^= tmp32[3];
|
|
2365
|
+
({
|
|
2366
|
+
s0,
|
|
2367
|
+
s1,
|
|
2368
|
+
s2,
|
|
2369
|
+
s3
|
|
2370
|
+
} = encrypt(xk, s0, s1, s2, s3));
|
|
2371
|
+
o[i++] = s0, o[i++] = s1, o[i++] = s2, o[i++] = s3;
|
|
2372
|
+
clean(tmp32);
|
|
2373
|
+
}
|
|
2374
|
+
swap32IfBE(o);
|
|
2375
|
+
clean(...toClean);
|
|
2376
|
+
if (input !== plaintext) clean(input);
|
|
2377
|
+
return _out;
|
|
2378
|
+
},
|
|
2379
|
+
decrypt(ciphertext, dst) {
|
|
2380
|
+
validateBlockDecrypt(ciphertext, dst);
|
|
2381
|
+
const xk = expandKeyDecLE(key);
|
|
2382
|
+
if (dst === undefined) dst = new Uint8Array(ciphertext.length);
|
|
2383
|
+
let _iv = iv;
|
|
2384
|
+
const toClean = [xk];
|
|
2385
|
+
if (!isLE || !isAligned32(_iv)) toClean.push(_iv = copyBytes(_iv));
|
|
2386
|
+
const n32 = u32(_iv);
|
|
2387
|
+
swap32IfBE(n32);
|
|
2388
|
+
if (!isLE || !isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2389
|
+
const b = u32(ciphertext);
|
|
2390
|
+
const o = u32(dst);
|
|
2391
|
+
swap32IfBE(b);
|
|
2392
|
+
let s0 = n32[0],
|
|
2393
|
+
s1 = n32[1],
|
|
2394
|
+
s2 = n32[2],
|
|
2395
|
+
s3 = n32[3];
|
|
2396
|
+
for (let i = 0; i + 4 <= b.length;) {
|
|
2397
|
+
const ps0 = s0,
|
|
2398
|
+
ps1 = s1,
|
|
2399
|
+
ps2 = s2,
|
|
2400
|
+
ps3 = s3;
|
|
2401
|
+
s0 = b[i + 0], s1 = b[i + 1], s2 = b[i + 2], s3 = b[i + 3];
|
|
2402
|
+
const {
|
|
2403
|
+
s0: o0,
|
|
2404
|
+
s1: o1,
|
|
2405
|
+
s2: o2,
|
|
2406
|
+
s3: o3
|
|
2407
|
+
} = decrypt(xk, s0, s1, s2, s3);
|
|
2408
|
+
o[i++] = o0 ^ ps0, o[i++] = o1 ^ ps1, o[i++] = o2 ^ ps2, o[i++] = o3 ^ ps3;
|
|
2409
|
+
}
|
|
2410
|
+
swap32IfBE(o);
|
|
2411
|
+
clean(...toClean);
|
|
2412
|
+
return validatePKCS(dst, pkcs5);
|
|
2413
|
+
}
|
|
2414
|
+
};
|
|
2415
|
+
});
|
|
2416
|
+
wrapCipher({
|
|
2417
|
+
blockSize: 16,
|
|
2418
|
+
nonceLength: 16
|
|
2419
|
+
}, function aescfb(key, iv) {
|
|
2420
|
+
function processCfb(src, isEncrypt, dst) {
|
|
2421
|
+
abytes(src);
|
|
2422
|
+
const srcLen = src.length;
|
|
2423
|
+
dst = getOutput(srcLen, dst);
|
|
2424
|
+
if (overlapBytes(src, dst)) throw new Error('overlapping src and dst not supported');
|
|
2425
|
+
const xk = expandKeyLE(key);
|
|
2426
|
+
let _iv = iv;
|
|
2427
|
+
const toClean = [xk];
|
|
2428
|
+
if (!isLE || !isAligned32(_iv)) toClean.push(_iv = copyBytes(_iv));
|
|
2429
|
+
if (!isLE || !isAligned32(src)) toClean.push(src = copyBytes(src));
|
|
2430
|
+
const src32 = u32(src);
|
|
2431
|
+
const dst32 = u32(dst);
|
|
2432
|
+
const next32 = isEncrypt ? dst32 : src32;
|
|
2433
|
+
const n32 = u32(_iv);
|
|
2434
|
+
swap32IfBE(src32);
|
|
2435
|
+
swap32IfBE(n32);
|
|
2436
|
+
let s0 = n32[0],
|
|
2437
|
+
s1 = n32[1],
|
|
2438
|
+
s2 = n32[2],
|
|
2439
|
+
s3 = n32[3];
|
|
2440
|
+
for (let i = 0; i + 4 <= src32.length;) {
|
|
2441
|
+
const {
|
|
2442
|
+
s0: e0,
|
|
2443
|
+
s1: e1,
|
|
2444
|
+
s2: e2,
|
|
2445
|
+
s3: e3
|
|
2446
|
+
} = encrypt(xk, s0, s1, s2, s3);
|
|
2447
|
+
dst32[i + 0] = src32[i + 0] ^ e0;
|
|
2448
|
+
dst32[i + 1] = src32[i + 1] ^ e1;
|
|
2449
|
+
dst32[i + 2] = src32[i + 2] ^ e2;
|
|
2450
|
+
dst32[i + 3] = src32[i + 3] ^ e3;
|
|
2451
|
+
s0 = next32[i++], s1 = next32[i++], s2 = next32[i++], s3 = next32[i++];
|
|
2452
|
+
}
|
|
2453
|
+
const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
|
|
2454
|
+
if (start < srcLen) {
|
|
2455
|
+
if (!isLE) swap32IfBE(src32.subarray(start / 4));
|
|
2456
|
+
({
|
|
2457
|
+
s0,
|
|
2458
|
+
s1,
|
|
2459
|
+
s2,
|
|
2460
|
+
s3
|
|
2461
|
+
} = encrypt(xk, s0, s1, s2, s3));
|
|
2462
|
+
const tmp = new Uint32Array([s0, s1, s2, s3]);
|
|
2463
|
+
swap32IfBE(tmp);
|
|
2464
|
+
const buf = u8(tmp);
|
|
2465
|
+
for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
|
|
2466
|
+
clean(buf);
|
|
2467
|
+
}
|
|
2468
|
+
if (!isLE) swap32IfBE(dst32.subarray(0, start / 4));
|
|
2469
|
+
clean(...toClean);
|
|
2470
|
+
return dst;
|
|
2471
|
+
}
|
|
2472
|
+
return {
|
|
2473
|
+
encrypt: (plaintext, dst) => processCfb(plaintext, true, dst),
|
|
2474
|
+
decrypt: (ciphertext, dst) => processCfb(ciphertext, false, dst)
|
|
2475
|
+
};
|
|
2476
|
+
});
|
|
2477
|
+
function computeTag(fn, isLE, key, data, AAD) {
|
|
2478
|
+
const aadLength = AAD ? AAD.length : 0;
|
|
2479
|
+
const h = fn.create(key, data.length + aadLength);
|
|
2480
|
+
if (AAD) h.update(AAD);
|
|
2481
|
+
const num = u64Lengths(8 * data.length, 8 * aadLength, isLE);
|
|
2482
|
+
h.update(data);
|
|
2483
|
+
h.update(num);
|
|
2484
|
+
const res = h.digest();
|
|
2485
|
+
clean(num);
|
|
2486
|
+
return res;
|
|
2487
|
+
}
|
|
2488
|
+
wrapCipher({
|
|
2489
|
+
blockSize: 16,
|
|
2490
|
+
nonceLength: 12,
|
|
2491
|
+
tagLength: 16,
|
|
2492
|
+
withAAD: true,
|
|
2493
|
+
varSizeNonce: true
|
|
2494
|
+
}, function aesgcm(key, nonce, AAD) {
|
|
2495
|
+
if (nonce.length < 8) throw new Error('aes/gcm: invalid nonce length');
|
|
2496
|
+
const tagLength = 16;
|
|
2497
|
+
function _computeTag(authKey, tagMask, data) {
|
|
2498
|
+
const tag = computeTag(ghash, false, authKey, data, AAD);
|
|
2499
|
+
for (let i = 0; i < tagMask.length; i++) tag[i] ^= tagMask[i];
|
|
2500
|
+
return tag;
|
|
2501
|
+
}
|
|
2502
|
+
function deriveKeys() {
|
|
2503
|
+
const xk = expandKeyLE(key);
|
|
2504
|
+
const authKey = EMPTY_BLOCK.slice();
|
|
2505
|
+
const counter = EMPTY_BLOCK.slice();
|
|
2506
|
+
ctr32(xk, false, counter, counter, authKey);
|
|
2507
|
+
if (nonce.length === 12) {
|
|
2508
|
+
counter.set(nonce);
|
|
2509
|
+
} else {
|
|
2510
|
+
const nonceLen = EMPTY_BLOCK.slice();
|
|
2511
|
+
const view = createView(nonceLen);
|
|
2512
|
+
view.setBigUint64(8, BigInt(nonce.length * 8), false);
|
|
2513
|
+
const g = ghash.create(authKey).update(nonce).update(nonceLen);
|
|
2514
|
+
g.digestInto(counter);
|
|
2515
|
+
g.destroy();
|
|
2516
|
+
}
|
|
2517
|
+
const tagMask = ctr32(xk, false, counter, EMPTY_BLOCK);
|
|
2518
|
+
return {
|
|
2519
|
+
xk,
|
|
2520
|
+
authKey,
|
|
2521
|
+
counter,
|
|
2522
|
+
tagMask
|
|
2523
|
+
};
|
|
2524
|
+
}
|
|
2525
|
+
return {
|
|
2526
|
+
encrypt(plaintext) {
|
|
2527
|
+
const {
|
|
2528
|
+
xk,
|
|
2529
|
+
authKey,
|
|
2530
|
+
counter,
|
|
2531
|
+
tagMask
|
|
2532
|
+
} = deriveKeys();
|
|
2533
|
+
const out = new Uint8Array(plaintext.length + tagLength);
|
|
2534
|
+
const toClean = [xk, authKey, counter, tagMask];
|
|
2535
|
+
if (!isAligned32(plaintext)) toClean.push(plaintext = copyBytes(plaintext));
|
|
2536
|
+
ctr32(xk, false, counter, plaintext, out.subarray(0, plaintext.length));
|
|
2537
|
+
const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
|
|
2538
|
+
toClean.push(tag);
|
|
2539
|
+
out.set(tag, plaintext.length);
|
|
2540
|
+
clean(...toClean);
|
|
2541
|
+
return out;
|
|
2542
|
+
},
|
|
2543
|
+
decrypt(ciphertext) {
|
|
2544
|
+
const {
|
|
2545
|
+
xk,
|
|
2546
|
+
authKey,
|
|
2547
|
+
counter,
|
|
2548
|
+
tagMask
|
|
2549
|
+
} = deriveKeys();
|
|
2550
|
+
const toClean = [xk, authKey, tagMask, counter];
|
|
2551
|
+
if (!isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2552
|
+
const data = ciphertext.subarray(0, -tagLength);
|
|
2553
|
+
const passedTag = ciphertext.subarray(-tagLength);
|
|
2554
|
+
const tag = _computeTag(authKey, tagMask, data);
|
|
2555
|
+
toClean.push(tag);
|
|
2556
|
+
if (!equalBytes(tag, passedTag)) {
|
|
2557
|
+
clean(...toClean);
|
|
2558
|
+
throw new Error('aes-gcm: invalid tag');
|
|
2559
|
+
}
|
|
2560
|
+
const out = ctr32(xk, false, counter, data);
|
|
2561
|
+
clean(...toClean);
|
|
2562
|
+
return out;
|
|
2563
|
+
}
|
|
2564
|
+
};
|
|
2565
|
+
});
|
|
2566
|
+
const limit = (name, min, max) => value => {
|
|
2567
|
+
if (!Number.isSafeInteger(value) || min > value || value > max) {
|
|
2568
|
+
const minmax = '[' + min + '..' + max + ']';
|
|
2569
|
+
throw new Error('' + name + ': expected value in range ' + minmax + ', got ' + value);
|
|
2570
|
+
}
|
|
2571
|
+
};
|
|
2572
|
+
wrapCipher({
|
|
2573
|
+
blockSize: 16,
|
|
2574
|
+
nonceLength: 12,
|
|
2575
|
+
tagLength: 16,
|
|
2576
|
+
withAAD: true,
|
|
2577
|
+
varSizeNonce: true
|
|
2578
|
+
}, function aessiv(key, nonce, AAD) {
|
|
2579
|
+
const tagLength = 16;
|
|
2580
|
+
const AAD_LIMIT = limit('AAD', 0, 2 ** 36);
|
|
2581
|
+
const PLAIN_LIMIT = limit('plaintext', 0, 2 ** 36);
|
|
2582
|
+
const NONCE_LIMIT = limit('nonce', 12, 12);
|
|
2583
|
+
const CIPHER_LIMIT = limit('ciphertext', 16, 2 ** 36 + 16);
|
|
2584
|
+
abytes(key);
|
|
2585
|
+
validateKeyLength(key);
|
|
2586
|
+
NONCE_LIMIT(nonce.length);
|
|
2587
|
+
if (AAD !== undefined) AAD_LIMIT(AAD.length);
|
|
2588
|
+
function deriveKeys() {
|
|
2589
|
+
const xk = expandKeyLE(key);
|
|
2590
|
+
const encKey = new Uint8Array(key.length);
|
|
2591
|
+
const authKey = new Uint8Array(16);
|
|
2592
|
+
const toClean = [xk, encKey];
|
|
2593
|
+
let _nonce = nonce;
|
|
2594
|
+
if (!isLE || !isAligned32(_nonce)) toClean.push(_nonce = copyBytes(_nonce));
|
|
2595
|
+
const n32 = u32(_nonce);
|
|
2596
|
+
swap32IfBE(n32);
|
|
2597
|
+
let s0 = 0,
|
|
2598
|
+
s1 = n32[0],
|
|
2599
|
+
s2 = n32[1],
|
|
2600
|
+
s3 = n32[2];
|
|
2601
|
+
let counter = 0;
|
|
2602
|
+
for (const derivedKey of [authKey, encKey].map(u32)) {
|
|
2603
|
+
const d32 = u32(derivedKey);
|
|
2604
|
+
for (let i = 0; i < d32.length; i += 2) {
|
|
2605
|
+
const {
|
|
2606
|
+
s0: o0,
|
|
2607
|
+
s1: o1
|
|
2608
|
+
} = encrypt(xk, s0, s1, s2, s3);
|
|
2609
|
+
d32[i + 0] = o0;
|
|
2610
|
+
d32[i + 1] = o1;
|
|
2611
|
+
s0 = ++counter;
|
|
2612
|
+
}
|
|
2613
|
+
swap32IfBE(d32);
|
|
2614
|
+
}
|
|
2615
|
+
const res = {
|
|
2616
|
+
authKey,
|
|
2617
|
+
encKey: expandKeyLE(encKey)
|
|
2618
|
+
};
|
|
2619
|
+
clean(...toClean);
|
|
2620
|
+
return res;
|
|
2621
|
+
}
|
|
2622
|
+
function _computeTag(encKey, authKey, data) {
|
|
2623
|
+
const tag = computeTag(polyval, true, authKey, data, AAD);
|
|
2624
|
+
for (let i = 0; i < 12; i++) tag[i] ^= nonce[i];
|
|
2625
|
+
tag[15] &= 0x7f;
|
|
2626
|
+
const t32 = u32(tag);
|
|
2627
|
+
swap32IfBE(t32);
|
|
2628
|
+
let s0 = t32[0],
|
|
2629
|
+
s1 = t32[1],
|
|
2630
|
+
s2 = t32[2],
|
|
2631
|
+
s3 = t32[3];
|
|
2632
|
+
({
|
|
2633
|
+
s0,
|
|
2634
|
+
s1,
|
|
2635
|
+
s2,
|
|
2636
|
+
s3
|
|
2637
|
+
} = encrypt(encKey, s0, s1, s2, s3));
|
|
2638
|
+
t32[0] = s0, t32[1] = s1, t32[2] = s2, t32[3] = s3;
|
|
2639
|
+
swap32IfBE(t32);
|
|
2640
|
+
return tag;
|
|
2641
|
+
}
|
|
2642
|
+
function processSiv(encKey, tag, input) {
|
|
2643
|
+
let block = copyBytes(tag);
|
|
2644
|
+
block[15] |= 0x80;
|
|
2645
|
+
const res = ctr32(encKey, true, block, input);
|
|
2646
|
+
clean(block);
|
|
2647
|
+
return res;
|
|
2648
|
+
}
|
|
2649
|
+
return {
|
|
2650
|
+
encrypt(plaintext) {
|
|
2651
|
+
PLAIN_LIMIT(plaintext.length);
|
|
2652
|
+
const {
|
|
2653
|
+
encKey,
|
|
2654
|
+
authKey
|
|
2655
|
+
} = deriveKeys();
|
|
2656
|
+
const tag = _computeTag(encKey, authKey, plaintext);
|
|
2657
|
+
const toClean = [encKey, authKey, tag];
|
|
2658
|
+
if (!isAligned32(plaintext)) toClean.push(plaintext = copyBytes(plaintext));
|
|
2659
|
+
const out = new Uint8Array(plaintext.length + tagLength);
|
|
2660
|
+
out.set(tag, plaintext.length);
|
|
2661
|
+
out.set(processSiv(encKey, tag, plaintext));
|
|
2662
|
+
clean(...toClean);
|
|
2663
|
+
return out;
|
|
2664
|
+
},
|
|
2665
|
+
decrypt(ciphertext) {
|
|
2666
|
+
CIPHER_LIMIT(ciphertext.length);
|
|
2667
|
+
const tag = ciphertext.subarray(-tagLength);
|
|
2668
|
+
const {
|
|
2669
|
+
encKey,
|
|
2670
|
+
authKey
|
|
2671
|
+
} = deriveKeys();
|
|
2672
|
+
const toClean = [encKey, authKey];
|
|
2673
|
+
if (!isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
|
|
2674
|
+
const plaintext = processSiv(encKey, tag, ciphertext.subarray(0, -tagLength));
|
|
2675
|
+
const expectedTag = _computeTag(encKey, authKey, plaintext);
|
|
2676
|
+
toClean.push(expectedTag);
|
|
2677
|
+
if (!equalBytes(tag, expectedTag)) {
|
|
2678
|
+
clean(plaintext, ...toClean);
|
|
2679
|
+
throw new Error('invalid polyval tag');
|
|
2680
|
+
}
|
|
2681
|
+
clean(...toClean);
|
|
2682
|
+
return plaintext;
|
|
2683
|
+
}
|
|
2684
|
+
};
|
|
2685
|
+
});
|
|
2686
|
+
function isBytes32(a) {
|
|
2687
|
+
return a instanceof Uint32Array || ArrayBuffer.isView(a) && a.constructor.name === 'Uint32Array';
|
|
2688
|
+
}
|
|
2689
|
+
function encryptBlock(xk, block) {
|
|
2690
|
+
abytes(block, 16, 'block');
|
|
2691
|
+
if (!isBytes32(xk)) throw new Error('_encryptBlock accepts result of expandKeyLE');
|
|
2692
|
+
const b32 = u32(block);
|
|
2693
|
+
swap32IfBE(b32);
|
|
2694
|
+
let {
|
|
2695
|
+
s0,
|
|
2696
|
+
s1,
|
|
2697
|
+
s2,
|
|
2698
|
+
s3
|
|
2699
|
+
} = encrypt(xk, b32[0], b32[1], b32[2], b32[3]);
|
|
2700
|
+
b32[0] = s0, b32[1] = s1, b32[2] = s2, b32[3] = s3;
|
|
2701
|
+
swap32IfBE(b32);
|
|
2702
|
+
return block;
|
|
2703
|
+
}
|
|
2704
|
+
function decryptBlock(xk, block) {
|
|
2705
|
+
abytes(block, 16, 'block');
|
|
2706
|
+
if (!isBytes32(xk)) throw new Error('_decryptBlock accepts result of expandKeyLE');
|
|
2707
|
+
const b32 = u32(block);
|
|
2708
|
+
swap32IfBE(b32);
|
|
2709
|
+
let {
|
|
2710
|
+
s0,
|
|
2711
|
+
s1,
|
|
2712
|
+
s2,
|
|
2713
|
+
s3
|
|
2714
|
+
} = decrypt(xk, b32[0], b32[1], b32[2], b32[3]);
|
|
2715
|
+
b32[0] = s0, b32[1] = s1, b32[2] = s2, b32[3] = s3;
|
|
2716
|
+
swap32IfBE(b32);
|
|
2717
|
+
return block;
|
|
2718
|
+
}
|
|
2719
|
+
const AESW = {
|
|
2720
|
+
encrypt(kek, out) {
|
|
2721
|
+
if (out.length >= 2 ** 32) throw new Error('plaintext should be less than 4gb');
|
|
2722
|
+
const xk = expandKeyLE(kek);
|
|
2723
|
+
if (out.length === 16) encryptBlock(xk, out);else {
|
|
2724
|
+
const o32 = u32(out);
|
|
2725
|
+
swap32IfBE(o32);
|
|
2726
|
+
let a0 = o32[0],
|
|
2727
|
+
a1 = o32[1];
|
|
2728
|
+
for (let j = 0, ctr = 1; j < 6; j++) {
|
|
2729
|
+
for (let pos = 2; pos < o32.length; pos += 2, ctr++) {
|
|
2730
|
+
const {
|
|
2731
|
+
s0,
|
|
2732
|
+
s1,
|
|
2733
|
+
s2,
|
|
2734
|
+
s3
|
|
2735
|
+
} = encrypt(xk, a0, a1, o32[pos], o32[pos + 1]);
|
|
2736
|
+
a0 = s0, a1 = s1 ^ byteSwap(ctr), o32[pos] = s2, o32[pos + 1] = s3;
|
|
2737
|
+
}
|
|
2738
|
+
}
|
|
2739
|
+
o32[0] = a0, o32[1] = a1;
|
|
2740
|
+
swap32IfBE(o32);
|
|
2741
|
+
}
|
|
2742
|
+
xk.fill(0);
|
|
2743
|
+
},
|
|
2744
|
+
decrypt(kek, out) {
|
|
2745
|
+
if (out.length - 8 >= 2 ** 32) throw new Error('ciphertext should be less than 4gb');
|
|
2746
|
+
const xk = expandKeyDecLE(kek);
|
|
2747
|
+
const chunks = out.length / 8 - 1;
|
|
2748
|
+
if (chunks === 1) decryptBlock(xk, out);else {
|
|
2749
|
+
const o32 = u32(out);
|
|
2750
|
+
swap32IfBE(o32);
|
|
2751
|
+
let a0 = o32[0],
|
|
2752
|
+
a1 = o32[1];
|
|
2753
|
+
for (let j = 0, ctr = chunks * 6; j < 6; j++) {
|
|
2754
|
+
for (let pos = chunks * 2; pos >= 1; pos -= 2, ctr--) {
|
|
2755
|
+
a1 ^= byteSwap(ctr);
|
|
2756
|
+
const {
|
|
2757
|
+
s0,
|
|
2758
|
+
s1,
|
|
2759
|
+
s2,
|
|
2760
|
+
s3
|
|
2761
|
+
} = decrypt(xk, a0, a1, o32[pos], o32[pos + 1]);
|
|
2762
|
+
a0 = s0, a1 = s1, o32[pos] = s2, o32[pos + 1] = s3;
|
|
2763
|
+
}
|
|
2764
|
+
}
|
|
2765
|
+
o32[0] = a0, o32[1] = a1;
|
|
2766
|
+
swap32IfBE(o32);
|
|
2767
|
+
}
|
|
2768
|
+
xk.fill(0);
|
|
2769
|
+
}
|
|
2770
|
+
};
|
|
2771
|
+
const AESKW_IV = new Uint8Array(8).fill(0xa6);
|
|
2772
|
+
wrapCipher({
|
|
2773
|
+
blockSize: 8
|
|
2774
|
+
}, kek => ({
|
|
2775
|
+
encrypt(plaintext) {
|
|
2776
|
+
if (!plaintext.length || plaintext.length % 8 !== 0) throw new Error('invalid plaintext length');
|
|
2777
|
+
if (plaintext.length === 8) throw new Error('8-byte keys not allowed, use AESKWP');
|
|
2778
|
+
const out = concatBytes(AESKW_IV, plaintext);
|
|
2779
|
+
AESW.encrypt(kek, out);
|
|
2780
|
+
return out;
|
|
2781
|
+
},
|
|
2782
|
+
decrypt(ciphertext) {
|
|
2783
|
+
if (ciphertext.length % 8 !== 0 || ciphertext.length < 3 * 8) throw new Error('invalid ciphertext length');
|
|
2784
|
+
const out = copyBytes(ciphertext);
|
|
2785
|
+
AESW.decrypt(kek, out);
|
|
2786
|
+
if (!equalBytes(out.subarray(0, 8), AESKW_IV)) throw new Error('integrity check failed');
|
|
2787
|
+
out.subarray(0, 8).fill(0);
|
|
2788
|
+
return out.subarray(8);
|
|
2789
|
+
}
|
|
2790
|
+
}));
|
|
2791
|
+
const AESKWP_IV = 0xa65959a6;
|
|
2792
|
+
wrapCipher({
|
|
2793
|
+
blockSize: 8
|
|
2794
|
+
}, kek => ({
|
|
2795
|
+
encrypt(plaintext) {
|
|
2796
|
+
if (!plaintext.length) throw new Error('invalid plaintext length');
|
|
2797
|
+
const padded = Math.ceil(plaintext.length / 8) * 8;
|
|
2798
|
+
const out = new Uint8Array(8 + padded);
|
|
2799
|
+
out.set(plaintext, 8);
|
|
2800
|
+
const out32 = u32(out);
|
|
2801
|
+
out32[0] = swap8IfBE(AESKWP_IV);
|
|
2802
|
+
out32[1] = swap8IfBE(byteSwap(plaintext.length));
|
|
2803
|
+
AESW.encrypt(kek, out);
|
|
2804
|
+
return out;
|
|
2805
|
+
},
|
|
2806
|
+
decrypt(ciphertext) {
|
|
2807
|
+
if (ciphertext.length < 16) throw new Error('invalid ciphertext length');
|
|
2808
|
+
const out = copyBytes(ciphertext);
|
|
2809
|
+
const o32 = u32(out);
|
|
2810
|
+
AESW.decrypt(kek, out);
|
|
2811
|
+
const len = byteSwap(swap8IfBE(o32[1])) >>> 0;
|
|
2812
|
+
const padded = Math.ceil(len / 8) * 8;
|
|
2813
|
+
if (swap8IfBE(o32[0]) !== AESKWP_IV || out.length - 8 !== padded) throw new Error('integrity check failed');
|
|
2814
|
+
for (let i = len; i < padded; i++) if (out[8 + i] !== 0) throw new Error('integrity check failed');
|
|
2815
|
+
out.subarray(0, 8).fill(0);
|
|
2816
|
+
return out.subarray(8, 8 + len);
|
|
2817
|
+
}
|
|
2818
|
+
}));
|
|
2819
|
+
function dbl(block) {
|
|
2820
|
+
let carry = 0;
|
|
2821
|
+
for (let i = BLOCK_SIZE - 1; i >= 0; i--) {
|
|
2822
|
+
const newCarry = (block[i] & 0x80) >>> 7;
|
|
2823
|
+
block[i] = block[i] << 1 | carry;
|
|
2824
|
+
carry = newCarry;
|
|
2825
|
+
}
|
|
2826
|
+
block[BLOCK_SIZE - 1] ^= 0x87 & -carry;
|
|
2827
|
+
return block;
|
|
2828
|
+
}
|
|
2829
|
+
function xorBlock(a, b) {
|
|
2830
|
+
if (a.length !== b.length) throw new Error('xorBlock: blocks must have same length');
|
|
2831
|
+
for (let i = 0; i < a.length; i++) {
|
|
2832
|
+
a[i] = a[i] ^ b[i];
|
|
1377
2833
|
}
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
2834
|
+
return a;
|
|
2835
|
+
}
|
|
2836
|
+
function xorend(a, b) {
|
|
2837
|
+
if (b.length > a.length) {
|
|
2838
|
+
throw new Error('xorend: expected len(B) <= len(A)');
|
|
2839
|
+
}
|
|
2840
|
+
const offset = a.length - b.length;
|
|
2841
|
+
for (let i = 0; i < b.length; i++) {
|
|
2842
|
+
a[offset + i] = a[offset + i] ^ b[i];
|
|
1381
2843
|
}
|
|
2844
|
+
return a;
|
|
1382
2845
|
}
|
|
1383
|
-
class
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
this.
|
|
1399
|
-
this.
|
|
1400
|
-
this.
|
|
1401
|
-
this.
|
|
2846
|
+
class _CMAC {
|
|
2847
|
+
blockLen = BLOCK_SIZE;
|
|
2848
|
+
outputLen = BLOCK_SIZE;
|
|
2849
|
+
buffer;
|
|
2850
|
+
pos;
|
|
2851
|
+
finished;
|
|
2852
|
+
destroyed;
|
|
2853
|
+
k1;
|
|
2854
|
+
k2;
|
|
2855
|
+
x;
|
|
2856
|
+
x32;
|
|
2857
|
+
xk;
|
|
2858
|
+
constructor(key) {
|
|
2859
|
+
abytes(key);
|
|
2860
|
+
validateKeyLength(key);
|
|
2861
|
+
this.xk = expandKeyLE(key);
|
|
2862
|
+
this.buffer = new Uint8Array(BLOCK_SIZE);
|
|
2863
|
+
this.pos = 0;
|
|
2864
|
+
this.finished = false;
|
|
2865
|
+
this.destroyed = false;
|
|
2866
|
+
this.x = new Uint8Array(BLOCK_SIZE);
|
|
2867
|
+
this.x32 = u32(this.x);
|
|
2868
|
+
const L = new Uint8Array(BLOCK_SIZE);
|
|
2869
|
+
encryptBlock(this.xk, L);
|
|
2870
|
+
this.k1 = dbl(L);
|
|
2871
|
+
this.k2 = dbl(new Uint8Array(this.k1));
|
|
2872
|
+
}
|
|
2873
|
+
process(data, pos) {
|
|
2874
|
+
const {
|
|
2875
|
+
x,
|
|
2876
|
+
x32,
|
|
2877
|
+
xk
|
|
2878
|
+
} = this;
|
|
2879
|
+
for (let i = 0; i < BLOCK_SIZE; i++) x[i] ^= data[pos + i];
|
|
2880
|
+
swap32IfBE(x32);
|
|
2881
|
+
const {
|
|
2882
|
+
s0,
|
|
2883
|
+
s1,
|
|
2884
|
+
s2,
|
|
2885
|
+
s3
|
|
2886
|
+
} = encrypt(xk, x32[0], x32[1], x32[2], x32[3]);
|
|
2887
|
+
x32[0] = s0, x32[1] = s1, x32[2] = s2, x32[3] = s3;
|
|
2888
|
+
swap32IfBE(x32);
|
|
2889
|
+
}
|
|
2890
|
+
update(data) {
|
|
2891
|
+
aexists(this);
|
|
2892
|
+
abytes(data);
|
|
2893
|
+
let pos = 0;
|
|
2894
|
+
if (this.pos) {
|
|
2895
|
+
const take = Math.min(BLOCK_SIZE - this.pos, data.length);
|
|
2896
|
+
this.buffer.set(data.subarray(0, take), this.pos);
|
|
2897
|
+
this.pos += take;
|
|
2898
|
+
pos = take;
|
|
2899
|
+
if (this.pos === BLOCK_SIZE && pos < data.length) {
|
|
2900
|
+
this.process(this.buffer, 0);
|
|
2901
|
+
this.pos = 0;
|
|
2902
|
+
}
|
|
2903
|
+
}
|
|
2904
|
+
while (pos + BLOCK_SIZE < data.length) {
|
|
2905
|
+
this.process(data, pos);
|
|
2906
|
+
pos += BLOCK_SIZE;
|
|
2907
|
+
}
|
|
2908
|
+
if (pos < data.length) {
|
|
2909
|
+
this.buffer.set(data.subarray(pos), 0);
|
|
2910
|
+
this.pos = data.length - pos;
|
|
2911
|
+
}
|
|
2912
|
+
return this;
|
|
2913
|
+
}
|
|
2914
|
+
digestInto(out) {
|
|
2915
|
+
aexists(this);
|
|
2916
|
+
aoutput32(out, this);
|
|
2917
|
+
this.finished = true;
|
|
2918
|
+
const view = out.subarray(0, this.outputLen);
|
|
2919
|
+
let last = new Uint8Array(BLOCK_SIZE);
|
|
2920
|
+
if (this.pos === BLOCK_SIZE) {
|
|
2921
|
+
last.set(this.buffer);
|
|
2922
|
+
xorBlock(last, this.k1);
|
|
2923
|
+
} else {
|
|
2924
|
+
last.set(this.buffer.subarray(0, this.pos));
|
|
2925
|
+
last[this.pos] = 0x80;
|
|
2926
|
+
xorBlock(last, this.k2);
|
|
2927
|
+
}
|
|
2928
|
+
view.set(this.x);
|
|
2929
|
+
xorBlock(view, last);
|
|
2930
|
+
encryptBlock(this.xk, view);
|
|
2931
|
+
clean(last);
|
|
2932
|
+
}
|
|
2933
|
+
digest() {
|
|
2934
|
+
const {
|
|
2935
|
+
buffer,
|
|
2936
|
+
outputLen
|
|
2937
|
+
} = this;
|
|
2938
|
+
this.digestInto(buffer);
|
|
2939
|
+
const res = buffer.slice(0, outputLen);
|
|
2940
|
+
this.destroy();
|
|
2941
|
+
return res;
|
|
2942
|
+
}
|
|
2943
|
+
destroy() {
|
|
2944
|
+
const {
|
|
2945
|
+
buffer,
|
|
2946
|
+
destroyed,
|
|
2947
|
+
x,
|
|
2948
|
+
xk,
|
|
2949
|
+
k1,
|
|
2950
|
+
k2
|
|
2951
|
+
} = this;
|
|
2952
|
+
if (destroyed) return;
|
|
2953
|
+
this.destroyed = true;
|
|
2954
|
+
clean(buffer, x, xk, k1, k2);
|
|
1402
2955
|
}
|
|
1403
2956
|
}
|
|
1404
|
-
const
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
this.Ch = T256_IV[4] | 0;
|
|
1435
|
-
this.Cl = T256_IV[5] | 0;
|
|
1436
|
-
this.Dh = T256_IV[6] | 0;
|
|
1437
|
-
this.Dl = T256_IV[7] | 0;
|
|
1438
|
-
this.Eh = T256_IV[8] | 0;
|
|
1439
|
-
this.El = T256_IV[9] | 0;
|
|
1440
|
-
this.Fh = T256_IV[10] | 0;
|
|
1441
|
-
this.Fl = T256_IV[11] | 0;
|
|
1442
|
-
this.Gh = T256_IV[12] | 0;
|
|
1443
|
-
this.Gl = T256_IV[13] | 0;
|
|
1444
|
-
this.Hh = T256_IV[14] | 0;
|
|
1445
|
-
this.Hl = T256_IV[15] | 0;
|
|
1446
|
-
}
|
|
1447
|
-
}
|
|
1448
|
-
const sha256 = createHasher(() => new SHA256());
|
|
1449
|
-
createHasher(() => new SHA224());
|
|
1450
|
-
createHasher(() => new SHA512());
|
|
1451
|
-
createHasher(() => new SHA384());
|
|
1452
|
-
createHasher(() => new SHA512_256());
|
|
1453
|
-
createHasher(() => new SHA512_224());
|
|
1454
|
-
|
|
1455
|
-
function sha256Hash(data) {
|
|
1456
|
-
return sha256(data);
|
|
2957
|
+
const cmac = wrapMacConstructor(16, key => new _CMAC(key));
|
|
2958
|
+
function s2v(key, strings) {
|
|
2959
|
+
validateKeyLength(key);
|
|
2960
|
+
const len = strings.length;
|
|
2961
|
+
if (len > 127) {
|
|
2962
|
+
throw new Error('s2v: expected <= 127 inputs');
|
|
2963
|
+
}
|
|
2964
|
+
if (len === 0) return cmac(ONE_BLOCK, key);
|
|
2965
|
+
let d = cmac(EMPTY_BLOCK, key);
|
|
2966
|
+
for (let i = 0; i < len - 1; i++) {
|
|
2967
|
+
dbl(d);
|
|
2968
|
+
const cmacResult = cmac(strings[i], key);
|
|
2969
|
+
xorBlock(d, cmacResult);
|
|
2970
|
+
clean(cmacResult);
|
|
2971
|
+
}
|
|
2972
|
+
const s_n = strings[len - 1];
|
|
2973
|
+
abytes(s_n);
|
|
2974
|
+
let t;
|
|
2975
|
+
if (s_n.byteLength >= BLOCK_SIZE) {
|
|
2976
|
+
t = xorend(Uint8Array.from(s_n), d);
|
|
2977
|
+
} else {
|
|
2978
|
+
const paddedSn = new Uint8Array(BLOCK_SIZE);
|
|
2979
|
+
paddedSn.set(s_n);
|
|
2980
|
+
paddedSn[s_n.length] = 0x80;
|
|
2981
|
+
t = xorBlock(dbl(d), paddedSn);
|
|
2982
|
+
clean(paddedSn);
|
|
2983
|
+
}
|
|
2984
|
+
const result = cmac(t, key);
|
|
2985
|
+
clean(d, t);
|
|
2986
|
+
return result;
|
|
1457
2987
|
}
|
|
2988
|
+
wrapCipher({
|
|
2989
|
+
blockSize: 16,
|
|
2990
|
+
tagLength: 16,
|
|
2991
|
+
withAAD: true
|
|
2992
|
+
}, function aessiv(key, ...AAD) {
|
|
2993
|
+
const PLAIN_LIMIT = limit('plaintext', 0, 2 ** 132);
|
|
2994
|
+
const CIPHER_LIMIT = limit('ciphertext', 16, 2 ** 132 + 16);
|
|
2995
|
+
if (AAD.length > 126) {
|
|
2996
|
+
throw new Error('"AAD" expected <= 126 items');
|
|
2997
|
+
}
|
|
2998
|
+
AAD.forEach(aad => abytes(aad));
|
|
2999
|
+
abytes(key);
|
|
3000
|
+
if (![32, 48, 64].includes(key.length)) throw new Error('"aes key" expected Uint8Array of length 32/48/64, got length=' + key.length);
|
|
3001
|
+
const k1 = key.subarray(0, key.length / 2);
|
|
3002
|
+
const k2 = key.subarray(key.length / 2);
|
|
3003
|
+
return {
|
|
3004
|
+
encrypt(plaintext) {
|
|
3005
|
+
PLAIN_LIMIT(plaintext.length);
|
|
3006
|
+
const v = s2v(k1, [...AAD, plaintext]);
|
|
3007
|
+
const q = Uint8Array.from(v);
|
|
3008
|
+
q[8] &= 0x7f;
|
|
3009
|
+
q[12] &= 0x7f;
|
|
3010
|
+
const c = ctr(k2, q).encrypt(plaintext);
|
|
3011
|
+
return concatBytes(v, c);
|
|
3012
|
+
},
|
|
3013
|
+
decrypt(ciphertext) {
|
|
3014
|
+
CIPHER_LIMIT(ciphertext.length);
|
|
3015
|
+
const v = ciphertext.subarray(0, BLOCK_SIZE);
|
|
3016
|
+
const c = ciphertext.subarray(BLOCK_SIZE);
|
|
3017
|
+
const q = Uint8Array.from(v);
|
|
3018
|
+
q[8] &= 0x7f;
|
|
3019
|
+
q[12] &= 0x7f;
|
|
3020
|
+
const p = ctr(k2, q).decrypt(c);
|
|
3021
|
+
const t = s2v(k1, [...AAD, p]);
|
|
3022
|
+
if (equalBytes(t, v)) {
|
|
3023
|
+
return p;
|
|
3024
|
+
} else {
|
|
3025
|
+
clean(p);
|
|
3026
|
+
throw new Error('invalid siv tag');
|
|
3027
|
+
}
|
|
3028
|
+
}
|
|
3029
|
+
};
|
|
3030
|
+
});
|
|
1458
3031
|
|
|
1459
3032
|
function aesCbcEncrypt(data, key, iv, padding = true) {
|
|
1460
3033
|
return cbc(key, iv, {
|
|
@@ -1591,7 +3164,7 @@ class PDFSecurity {
|
|
|
1591
3164
|
}
|
|
1592
3165
|
infoStr += `${key}: ${info[key].valueOf()}\n`;
|
|
1593
3166
|
}
|
|
1594
|
-
return md5Hash(infoStr);
|
|
3167
|
+
return md5Hash(new TextEncoder().encode(infoStr));
|
|
1595
3168
|
}
|
|
1596
3169
|
static generateRandomWordArray(bytes) {
|
|
1597
3170
|
return randomBytes(bytes);
|
|
@@ -1726,7 +3299,7 @@ class PDFSecurity {
|
|
|
1726
3299
|
let digest;
|
|
1727
3300
|
if (this.version < 5) {
|
|
1728
3301
|
const suffix = new Uint8Array([obj & 0xff, obj >> 8 & 0xff, obj >> 16 & 0xff, gen & 0xff, gen >> 8 & 0xff]);
|
|
1729
|
-
digest = concatBytes(this.encryptionKey, suffix);
|
|
3302
|
+
digest = concatBytes$1(this.encryptionKey, suffix);
|
|
1730
3303
|
}
|
|
1731
3304
|
if (this.version === 1 || this.version === 2) {
|
|
1732
3305
|
let key = md5Hash(digest);
|
|
@@ -1737,14 +3310,14 @@ class PDFSecurity {
|
|
|
1737
3310
|
let key;
|
|
1738
3311
|
if (this.version === 4) {
|
|
1739
3312
|
const saltMarker = new Uint8Array([0x73, 0x41, 0x6c, 0x54]);
|
|
1740
|
-
key = md5Hash(concatBytes(digest, saltMarker));
|
|
3313
|
+
key = md5Hash(concatBytes$1(digest, saltMarker));
|
|
1741
3314
|
} else {
|
|
1742
3315
|
key = this.encryptionKey;
|
|
1743
3316
|
}
|
|
1744
3317
|
const iv = PDFSecurity.generateRandomWordArray(16);
|
|
1745
3318
|
return buffer => {
|
|
1746
3319
|
const encrypted = aesCbcEncrypt(buffer, key, iv, true);
|
|
1747
|
-
return concatBytes(iv, encrypted);
|
|
3320
|
+
return concatBytes$1(iv, encrypted);
|
|
1748
3321
|
};
|
|
1749
3322
|
}
|
|
1750
3323
|
end() {
|
|
@@ -1800,7 +3373,7 @@ function getUserPasswordR2(encryptionKey) {
|
|
|
1800
3373
|
}
|
|
1801
3374
|
function getUserPasswordR3R4(documentId, encryptionKey) {
|
|
1802
3375
|
const key = encryptionKey.slice();
|
|
1803
|
-
let cipher = md5Hash(concatBytes(processPasswordR2R3R4(), new Uint8Array(documentId)));
|
|
3376
|
+
let cipher = md5Hash(concatBytes$1(processPasswordR2R3R4(), new Uint8Array(documentId)));
|
|
1804
3377
|
for (let i = 0; i < 20; i++) {
|
|
1805
3378
|
const xorKey = new Uint8Array(key.length);
|
|
1806
3379
|
for (let j = 0; j < key.length; j++) {
|
|
@@ -1833,7 +3406,7 @@ function getOwnerPasswordR2R3R4(r, keyBits, paddedUserPassword, paddedOwnerPassw
|
|
|
1833
3406
|
}
|
|
1834
3407
|
function getEncryptionKeyR2R3R4(r, keyBits, documentId, paddedUserPassword, ownerPasswordEntry, permissions) {
|
|
1835
3408
|
const permBytes = new Uint8Array([permissions & 0xff, permissions >> 8 & 0xff, permissions >> 16 & 0xff, permissions >> 24 & 0xff]);
|
|
1836
|
-
let key = concatBytes(paddedUserPassword, ownerPasswordEntry, permBytes, new Uint8Array(documentId));
|
|
3409
|
+
let key = concatBytes$1(paddedUserPassword, ownerPasswordEntry, permBytes, new Uint8Array(documentId));
|
|
1837
3410
|
const round = r >= 3 ? 51 : 1;
|
|
1838
3411
|
const keyLen = keyBits / 8;
|
|
1839
3412
|
for (let i = 0; i < round; i++) {
|
|
@@ -1845,22 +3418,22 @@ function getEncryptionKeyR2R3R4(r, keyBits, documentId, paddedUserPassword, owne
|
|
|
1845
3418
|
function getUserPasswordR5(processedUserPassword, generateRandomWordArray) {
|
|
1846
3419
|
const validationSalt = generateRandomWordArray(8);
|
|
1847
3420
|
const keySalt = generateRandomWordArray(8);
|
|
1848
|
-
const hash = sha256Hash(concatBytes(processedUserPassword, validationSalt));
|
|
1849
|
-
return concatBytes(hash, validationSalt, keySalt);
|
|
3421
|
+
const hash = sha256Hash(concatBytes$1(processedUserPassword, validationSalt));
|
|
3422
|
+
return concatBytes$1(hash, validationSalt, keySalt);
|
|
1850
3423
|
}
|
|
1851
3424
|
function getUserEncryptionKeyR5(processedUserPassword, userKeySalt, encryptionKey) {
|
|
1852
|
-
const key = sha256Hash(concatBytes(processedUserPassword, userKeySalt));
|
|
3425
|
+
const key = sha256Hash(concatBytes$1(processedUserPassword, userKeySalt));
|
|
1853
3426
|
const iv = new Uint8Array(16);
|
|
1854
3427
|
return aesCbcEncrypt(encryptionKey, key, iv, false);
|
|
1855
3428
|
}
|
|
1856
3429
|
function getOwnerPasswordR5(processedOwnerPassword, userPasswordEntry, generateRandomWordArray) {
|
|
1857
3430
|
const validationSalt = generateRandomWordArray(8);
|
|
1858
3431
|
const keySalt = generateRandomWordArray(8);
|
|
1859
|
-
const hash = sha256Hash(concatBytes(processedOwnerPassword, validationSalt, userPasswordEntry));
|
|
1860
|
-
return concatBytes(hash, validationSalt, keySalt);
|
|
3432
|
+
const hash = sha256Hash(concatBytes$1(processedOwnerPassword, validationSalt, userPasswordEntry));
|
|
3433
|
+
return concatBytes$1(hash, validationSalt, keySalt);
|
|
1861
3434
|
}
|
|
1862
3435
|
function getOwnerEncryptionKeyR5(processedOwnerPassword, ownerKeySalt, userPasswordEntry, encryptionKey) {
|
|
1863
|
-
const key = sha256Hash(concatBytes(processedOwnerPassword, ownerKeySalt, userPasswordEntry));
|
|
3436
|
+
const key = sha256Hash(concatBytes$1(processedOwnerPassword, ownerKeySalt, userPasswordEntry));
|
|
1864
3437
|
const iv = new Uint8Array(16);
|
|
1865
3438
|
return aesCbcEncrypt(encryptionKey, key, iv, false);
|
|
1866
3439
|
}
|
|
@@ -1914,6 +3487,104 @@ function processPasswordR5(password = '') {
|
|
|
1914
3487
|
}
|
|
1915
3488
|
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];
|
|
1916
3489
|
|
|
3490
|
+
const OBJECTS_PER_STREAM = 200;
|
|
3491
|
+
const ENTRY_WIDTHS = [1, 4, 2];
|
|
3492
|
+
const ENTRY_SIZE = 7;
|
|
3493
|
+
const FREE = 0;
|
|
3494
|
+
const UNCOMPRESSED = 1;
|
|
3495
|
+
const COMPRESSED = 2;
|
|
3496
|
+
class PDFObjectStreams {
|
|
3497
|
+
constructor(document, options) {
|
|
3498
|
+
if (document.version < 1.5) {
|
|
3499
|
+
throw new Error("The objectStreams option requires a pdfVersion of '1.5' or higher");
|
|
3500
|
+
}
|
|
3501
|
+
if (/^PDF\/A-1/.test(options.subset)) {
|
|
3502
|
+
throw new Error('PDF/A-1 does not allow object streams');
|
|
3503
|
+
}
|
|
3504
|
+
if (options.userPassword || options.ownerPassword) {
|
|
3505
|
+
throw new Error('The objectStreams option cannot be used with encryption');
|
|
3506
|
+
}
|
|
3507
|
+
this.document = document;
|
|
3508
|
+
this.pending = [];
|
|
3509
|
+
this.locations = new Map();
|
|
3510
|
+
}
|
|
3511
|
+
add(ref) {
|
|
3512
|
+
this.pending.push({
|
|
3513
|
+
id: ref.id,
|
|
3514
|
+
body: PDFObject.convert(ref.data)
|
|
3515
|
+
});
|
|
3516
|
+
if (this.pending.length >= OBJECTS_PER_STREAM) {
|
|
3517
|
+
this.flush();
|
|
3518
|
+
}
|
|
3519
|
+
}
|
|
3520
|
+
flush() {
|
|
3521
|
+
if (!this.pending.length) {
|
|
3522
|
+
return;
|
|
3523
|
+
}
|
|
3524
|
+
const objects = this.pending;
|
|
3525
|
+
this.pending = [];
|
|
3526
|
+
let offset = 0;
|
|
3527
|
+
const pairs = [];
|
|
3528
|
+
for (const {
|
|
3529
|
+
id,
|
|
3530
|
+
body
|
|
3531
|
+
} of objects) {
|
|
3532
|
+
pairs.push(id, offset);
|
|
3533
|
+
offset += body.length + 1;
|
|
3534
|
+
}
|
|
3535
|
+
const header = pairs.join(' ') + '\n';
|
|
3536
|
+
const stream = this.document.ref({
|
|
3537
|
+
Type: 'ObjStm',
|
|
3538
|
+
N: objects.length,
|
|
3539
|
+
First: header.length
|
|
3540
|
+
});
|
|
3541
|
+
stream.end(fromBinaryString(header + objects.map(o => o.body).join('\n')));
|
|
3542
|
+
objects.forEach(({
|
|
3543
|
+
id
|
|
3544
|
+
}, index) => {
|
|
3545
|
+
this.locations.set(id, [stream.id, index]);
|
|
3546
|
+
});
|
|
3547
|
+
}
|
|
3548
|
+
finalize() {
|
|
3549
|
+
const doc = this.document;
|
|
3550
|
+
this.flush();
|
|
3551
|
+
const xref = doc.ref({
|
|
3552
|
+
Type: 'XRef',
|
|
3553
|
+
Root: doc._root,
|
|
3554
|
+
Info: doc._info,
|
|
3555
|
+
ID: [doc._id, doc._id]
|
|
3556
|
+
});
|
|
3557
|
+
const xrefOffset = doc._offset;
|
|
3558
|
+
if (xrefOffset > 0xffffffff) {
|
|
3559
|
+
throw new Error('The objectStreams option cannot write files over 4 GB');
|
|
3560
|
+
}
|
|
3561
|
+
doc._offsets[xref.id - 1] = xrefOffset;
|
|
3562
|
+
const size = doc._offsets.length + 1;
|
|
3563
|
+
const table = new DataView(new ArrayBuffer(size * ENTRY_SIZE));
|
|
3564
|
+
const put = (id, type, field2, field3) => {
|
|
3565
|
+
const start = id * ENTRY_SIZE;
|
|
3566
|
+
table.setUint8(start, type);
|
|
3567
|
+
table.setUint32(start + 1, field2);
|
|
3568
|
+
table.setUint16(start + 5, field3);
|
|
3569
|
+
};
|
|
3570
|
+
put(0, FREE, 0, 0xffff);
|
|
3571
|
+
for (let id = 1; id < size; id++) {
|
|
3572
|
+
const location = this.locations.get(id);
|
|
3573
|
+
if (location) {
|
|
3574
|
+
put(id, COMPRESSED, ...location);
|
|
3575
|
+
} else {
|
|
3576
|
+
put(id, UNCOMPRESSED, doc._offsets[id - 1], 0);
|
|
3577
|
+
}
|
|
3578
|
+
}
|
|
3579
|
+
xref.data.Size = size;
|
|
3580
|
+
xref.data.W = ENTRY_WIDTHS;
|
|
3581
|
+
xref.end(new Uint8Array(table.buffer));
|
|
3582
|
+
doc._write('startxref');
|
|
3583
|
+
doc._write(`${xrefOffset}`);
|
|
3584
|
+
doc._write('%%EOF');
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
|
|
1917
3588
|
const {
|
|
1918
3589
|
number: number$2
|
|
1919
3590
|
} = PDFObject;
|
|
@@ -1952,16 +3623,16 @@ let PDFGradient$1 = class PDFGradient {
|
|
|
1952
3623
|
}
|
|
1953
3624
|
embed(m) {
|
|
1954
3625
|
let fn;
|
|
1955
|
-
|
|
1956
|
-
if (stopsLength === 0) {
|
|
3626
|
+
if (this.stops.length === 0) {
|
|
1957
3627
|
return;
|
|
1958
3628
|
}
|
|
1959
3629
|
this.embedded = true;
|
|
1960
3630
|
this.matrix = m;
|
|
1961
|
-
const last = this.stops[
|
|
3631
|
+
const last = this.stops[this.stops.length - 1];
|
|
1962
3632
|
if (last[0] < 1) {
|
|
1963
3633
|
this.stops.push([1, last[1], last[2]]);
|
|
1964
3634
|
}
|
|
3635
|
+
const stopsLength = this.stops.length;
|
|
1965
3636
|
const bounds = [];
|
|
1966
3637
|
const encode = [];
|
|
1967
3638
|
const stops = [];
|
|
@@ -2745,18 +4416,15 @@ const runners = {
|
|
|
2745
4416
|
return cy += a[3];
|
|
2746
4417
|
},
|
|
2747
4418
|
Q(doc, a) {
|
|
4419
|
+
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]);
|
|
2748
4420
|
px = a[0];
|
|
2749
4421
|
py = a[1];
|
|
2750
4422
|
cx = a[2];
|
|
2751
4423
|
cy = a[3];
|
|
2752
|
-
return doc
|
|
4424
|
+
return doc;
|
|
2753
4425
|
},
|
|
2754
4426
|
q(doc, a) {
|
|
2755
|
-
|
|
2756
|
-
px = cx + a[0];
|
|
2757
|
-
py = cy + a[1];
|
|
2758
|
-
cx += a[2];
|
|
2759
|
-
return cy += a[3];
|
|
4427
|
+
return this.Q(doc, [a[0] + cx, a[1] + cy, a[2] + cx, a[3] + cy]);
|
|
2760
4428
|
},
|
|
2761
4429
|
T(doc, a) {
|
|
2762
4430
|
if (px === null) {
|
|
@@ -2766,23 +4434,10 @@ const runners = {
|
|
|
2766
4434
|
px = cx - (px - cx);
|
|
2767
4435
|
py = cy - (py - cy);
|
|
2768
4436
|
}
|
|
2769
|
-
|
|
2770
|
-
px = cx - (px - cx);
|
|
2771
|
-
py = cy - (py - cy);
|
|
2772
|
-
cx = a[0];
|
|
2773
|
-
return cy = a[1];
|
|
4437
|
+
return this.Q(doc, [px, py, a[0], a[1]]);
|
|
2774
4438
|
},
|
|
2775
4439
|
t(doc, a) {
|
|
2776
|
-
|
|
2777
|
-
px = cx;
|
|
2778
|
-
py = cy;
|
|
2779
|
-
} else {
|
|
2780
|
-
px = cx - (px - cx);
|
|
2781
|
-
py = cy - (py - cy);
|
|
2782
|
-
}
|
|
2783
|
-
doc.quadraticCurveTo(px, py, cx + a[0], cy + a[1]);
|
|
2784
|
-
cx += a[0];
|
|
2785
|
-
return cy += a[1];
|
|
4440
|
+
return this.T(doc, [cx + a[0], cy + a[1]]);
|
|
2786
4441
|
},
|
|
2787
4442
|
A(doc, a) {
|
|
2788
4443
|
solveArc(doc, cx, cy, a);
|
|
@@ -3283,8 +4938,12 @@ const WIN_ANSI_MAP = {
|
|
|
3283
4938
|
353: 154,
|
|
3284
4939
|
376: 159,
|
|
3285
4940
|
381: 142,
|
|
3286
|
-
382: 158
|
|
4941
|
+
382: 158,
|
|
4942
|
+
8208: 45,
|
|
4943
|
+
8209: 45,
|
|
4944
|
+
8239: 160
|
|
3287
4945
|
};
|
|
4946
|
+
const outputCode = charCode => WIN_ANSI_MAP[charCode] || charCode;
|
|
3288
4947
|
const characters = `\
|
|
3289
4948
|
.notdef .notdef .notdef .notdef
|
|
3290
4949
|
.notdef .notdef .notdef .notdef
|
|
@@ -3369,6 +5028,7 @@ class AFMFont {
|
|
|
3369
5028
|
this.lineGap = this.bbox[3] - this.bbox[1] - (this.ascender - this.descender);
|
|
3370
5029
|
const glyphNames = data.glyphNames.split(' ');
|
|
3371
5030
|
this.glyphWidths = Object.fromEntries(glyphNames.map((name, index) => [name, data.glyphWidths[index]]));
|
|
5031
|
+
this.builtInEncoding = data.glyphCodes ? Object.fromEntries(data.glyphCodes.map((code, index) => [code, glyphNames[index]])) : null;
|
|
3372
5032
|
this.kernPairs = {};
|
|
3373
5033
|
for (let index = 0; index < data.kernPairs.length; index += 2) {
|
|
3374
5034
|
const amount = data.kernPairs[index];
|
|
@@ -3383,23 +5043,31 @@ class AFMFont {
|
|
|
3383
5043
|
}
|
|
3384
5044
|
encodeText(text) {
|
|
3385
5045
|
const res = [];
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
5046
|
+
let i = 0;
|
|
5047
|
+
while (i < text.length) {
|
|
5048
|
+
const codePoint = text.codePointAt(i);
|
|
5049
|
+
const char = outputCode(codePoint);
|
|
5050
|
+
res.push(char.toString(16).padStart(2, '0'));
|
|
5051
|
+
i += codePoint > 0xffff ? 2 : 1;
|
|
3390
5052
|
}
|
|
3391
5053
|
return res;
|
|
3392
5054
|
}
|
|
3393
5055
|
glyphsForString(string) {
|
|
3394
5056
|
const glyphs = [];
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
5057
|
+
let i = 0;
|
|
5058
|
+
while (i < string.length) {
|
|
5059
|
+
const codePoint = string.codePointAt(i);
|
|
5060
|
+
glyphs.push(this.characterToGlyph(codePoint));
|
|
5061
|
+
i += codePoint > 0xffff ? 2 : 1;
|
|
3398
5062
|
}
|
|
3399
5063
|
return glyphs;
|
|
3400
5064
|
}
|
|
3401
5065
|
characterToGlyph(character) {
|
|
3402
|
-
|
|
5066
|
+
const code = outputCode(character);
|
|
5067
|
+
if (this.builtInEncoding) {
|
|
5068
|
+
return this.builtInEncoding[code] || '.notdef';
|
|
5069
|
+
}
|
|
5070
|
+
return characters[code] || '.notdef';
|
|
3403
5071
|
}
|
|
3404
5072
|
widthOfGlyph(glyph) {
|
|
3405
5073
|
return this.glyphWidths[glyph] || 0;
|
|
@@ -3445,13 +5113,18 @@ class PDFFont {
|
|
|
3445
5113
|
}
|
|
3446
5114
|
}
|
|
3447
5115
|
|
|
5116
|
+
const afmFonts = new WeakMap();
|
|
3448
5117
|
class StandardFont extends PDFFont {
|
|
3449
5118
|
constructor(document, fontData, id) {
|
|
3450
5119
|
super();
|
|
3451
5120
|
this.document = document;
|
|
3452
5121
|
this.name = fontData.name;
|
|
3453
5122
|
this.id = id;
|
|
3454
|
-
this.font =
|
|
5123
|
+
this.font = afmFonts.get(fontData);
|
|
5124
|
+
if (!this.font) {
|
|
5125
|
+
this.font = new AFMFont(fontData);
|
|
5126
|
+
afmFonts.set(fontData, this.font);
|
|
5127
|
+
}
|
|
3455
5128
|
({
|
|
3456
5129
|
ascender: this.ascender,
|
|
3457
5130
|
descender: this.descender,
|
|
@@ -3502,6 +5175,23 @@ class StandardFont extends PDFFont {
|
|
|
3502
5175
|
const toHex = function (num) {
|
|
3503
5176
|
return `0000${num.toString(16)}`.slice(-4);
|
|
3504
5177
|
};
|
|
5178
|
+
const WIN_ANSI_CODE_TO_UNICODE = Object.fromEntries(Object.entries(WIN_ANSI_MAP).map(([codePoint, code]) => [code, Number(codePoint)]));
|
|
5179
|
+
const UNDEFINED_WIN_ANSI_CODES = new Set([129, 141, 143, 144, 157]);
|
|
5180
|
+
const FIRST_WIN_ANSI_CHAR = 32;
|
|
5181
|
+
const LAST_WIN_ANSI_CHAR = 255;
|
|
5182
|
+
function unicodeForWinAnsiCode(code) {
|
|
5183
|
+
if (UNDEFINED_WIN_ANSI_CODES.has(code)) {
|
|
5184
|
+
return null;
|
|
5185
|
+
}
|
|
5186
|
+
return WIN_ANSI_CODE_TO_UNICODE[code] ?? code;
|
|
5187
|
+
}
|
|
5188
|
+
function completeSubsetOf(font) {
|
|
5189
|
+
const subset = font.createSubset();
|
|
5190
|
+
for (let gid = 0; gid < font.numGlyphs; gid++) {
|
|
5191
|
+
subset.includeGlyph(gid);
|
|
5192
|
+
}
|
|
5193
|
+
return subset;
|
|
5194
|
+
}
|
|
3505
5195
|
class EmbeddedFont extends PDFFont {
|
|
3506
5196
|
constructor(document, font, id) {
|
|
3507
5197
|
super();
|
|
@@ -3599,13 +5289,31 @@ class EmbeddedFont extends PDFFont {
|
|
|
3599
5289
|
const scale = size / 1000;
|
|
3600
5290
|
return width * scale;
|
|
3601
5291
|
}
|
|
3602
|
-
|
|
3603
|
-
|
|
5292
|
+
completeRef() {
|
|
5293
|
+
return this.completeDictionary != null ? this.completeDictionary : this.completeDictionary = this.document.ref();
|
|
5294
|
+
}
|
|
5295
|
+
finalize() {
|
|
5296
|
+
if (this.embedded) {
|
|
5297
|
+
return;
|
|
5298
|
+
}
|
|
5299
|
+
if (this.dictionary != null) {
|
|
5300
|
+
this.embed();
|
|
5301
|
+
}
|
|
5302
|
+
if (this.completeDictionary != null) {
|
|
5303
|
+
this.embedComplete();
|
|
5304
|
+
}
|
|
5305
|
+
this.embedded = true;
|
|
5306
|
+
}
|
|
5307
|
+
embedProgram(subset, complete) {
|
|
5308
|
+
const isCFF = subset.cff != null;
|
|
5309
|
+
const fontProgram = subset.encode();
|
|
3604
5310
|
const fontFile = this.document.ref();
|
|
3605
5311
|
if (isCFF) {
|
|
3606
5312
|
fontFile.data.Subtype = 'CIDFontType0C';
|
|
5313
|
+
} else if (complete) {
|
|
5314
|
+
fontFile.data.Length1 = fontProgram.length;
|
|
3607
5315
|
}
|
|
3608
|
-
fontFile.end(
|
|
5316
|
+
fontFile.end(fontProgram);
|
|
3609
5317
|
const familyClass = ((this.font['OS/2'] != null ? this.font['OS/2'].sFamilyClass : undefined) || 0) >> 8;
|
|
3610
5318
|
let flags = 0;
|
|
3611
5319
|
if (this.font.post.isFixedPitch) {
|
|
@@ -3621,8 +5329,9 @@ class EmbeddedFont extends PDFFont {
|
|
|
3621
5329
|
if (this.font.head.macStyle.italic) {
|
|
3622
5330
|
flags |= 1 << 6;
|
|
3623
5331
|
}
|
|
5332
|
+
const postscriptName = this.font.postscriptName?.replaceAll(' ', '_');
|
|
3624
5333
|
const tag = [1, 2, 3, 4, 5, 6].map(i => String.fromCharCode((this.id.charCodeAt(i) || 73) + 17)).join('');
|
|
3625
|
-
const name =
|
|
5334
|
+
const name = complete ? postscriptName : `${tag}+${postscriptName}`;
|
|
3626
5335
|
const {
|
|
3627
5336
|
bbox
|
|
3628
5337
|
} = this.font;
|
|
@@ -3643,7 +5352,7 @@ class EmbeddedFont extends PDFFont {
|
|
|
3643
5352
|
} else {
|
|
3644
5353
|
descriptor.data.FontFile2 = fontFile;
|
|
3645
5354
|
}
|
|
3646
|
-
if (this.document.subset && this.document.subset === 1) {
|
|
5355
|
+
if (!complete && this.document.subset && this.document.subset === 1) {
|
|
3647
5356
|
const maxCID = this.widths.length - 1;
|
|
3648
5357
|
const cidSetBuffer = new Uint8Array(Math.ceil((maxCID + 1) / 8));
|
|
3649
5358
|
for (let cid = 0; cid <= maxCID; cid++) {
|
|
@@ -3657,6 +5366,18 @@ class EmbeddedFont extends PDFFont {
|
|
|
3657
5366
|
descriptor.data.CIDSet = CIDSetRef;
|
|
3658
5367
|
}
|
|
3659
5368
|
descriptor.end();
|
|
5369
|
+
return {
|
|
5370
|
+
descriptor,
|
|
5371
|
+
name,
|
|
5372
|
+
isCFF
|
|
5373
|
+
};
|
|
5374
|
+
}
|
|
5375
|
+
embed() {
|
|
5376
|
+
const {
|
|
5377
|
+
descriptor,
|
|
5378
|
+
name,
|
|
5379
|
+
isCFF
|
|
5380
|
+
} = this.embedProgram(this.subset, false);
|
|
3660
5381
|
const descendantFontData = {
|
|
3661
5382
|
Type: 'Font',
|
|
3662
5383
|
Subtype: 'CIDFontType0',
|
|
@@ -3685,6 +5406,84 @@ class EmbeddedFont extends PDFFont {
|
|
|
3685
5406
|
};
|
|
3686
5407
|
return this.dictionary.end();
|
|
3687
5408
|
}
|
|
5409
|
+
embedComplete() {
|
|
5410
|
+
const {
|
|
5411
|
+
descriptor,
|
|
5412
|
+
name,
|
|
5413
|
+
isCFF
|
|
5414
|
+
} = this.embedProgram(completeSubsetOf(this.font), true);
|
|
5415
|
+
const widths = [];
|
|
5416
|
+
for (let gid = 0; gid < this.font.numGlyphs; gid++) {
|
|
5417
|
+
widths.push(this.font.getGlyph(gid).advanceWidth * this.scale);
|
|
5418
|
+
}
|
|
5419
|
+
const descendantFontData = {
|
|
5420
|
+
Type: 'Font',
|
|
5421
|
+
Subtype: isCFF ? 'CIDFontType0' : 'CIDFontType2',
|
|
5422
|
+
BaseFont: name,
|
|
5423
|
+
CIDSystemInfo: {
|
|
5424
|
+
Registry: new String('Adobe'),
|
|
5425
|
+
Ordering: new String('Identity'),
|
|
5426
|
+
Supplement: 0
|
|
5427
|
+
},
|
|
5428
|
+
FontDescriptor: descriptor,
|
|
5429
|
+
W: [0, widths]
|
|
5430
|
+
};
|
|
5431
|
+
if (!isCFF) {
|
|
5432
|
+
descendantFontData.CIDToGIDMap = 'Identity';
|
|
5433
|
+
}
|
|
5434
|
+
const descendantFont = this.document.ref(descendantFontData);
|
|
5435
|
+
descendantFont.end();
|
|
5436
|
+
this.completeDictionary.data = {
|
|
5437
|
+
Type: 'Font',
|
|
5438
|
+
Subtype: 'Type0',
|
|
5439
|
+
BaseFont: name,
|
|
5440
|
+
Encoding: this.winAnsiToGidCmap(),
|
|
5441
|
+
DescendantFonts: [descendantFont]
|
|
5442
|
+
};
|
|
5443
|
+
return this.completeDictionary.end();
|
|
5444
|
+
}
|
|
5445
|
+
winAnsiToGidCmap() {
|
|
5446
|
+
const cmap = this.document.ref();
|
|
5447
|
+
cmap.data.Type = 'CMap';
|
|
5448
|
+
const entries = [];
|
|
5449
|
+
for (let code = FIRST_WIN_ANSI_CHAR; code <= LAST_WIN_ANSI_CHAR; code++) {
|
|
5450
|
+
const codePoint = unicodeForWinAnsiCode(code);
|
|
5451
|
+
if (codePoint == null || !this.font.hasGlyphForCodePoint(codePoint)) {
|
|
5452
|
+
continue;
|
|
5453
|
+
}
|
|
5454
|
+
const gid = this.font.glyphForCodePoint(codePoint).id;
|
|
5455
|
+
entries.push(`<${code.toString(16).padStart(2, '0')}> ${gid}`);
|
|
5456
|
+
}
|
|
5457
|
+
const chunkSize = 100;
|
|
5458
|
+
const chunks = Math.ceil(entries.length / chunkSize);
|
|
5459
|
+
const ranges = [];
|
|
5460
|
+
for (let i = 0; i < chunks; i++) {
|
|
5461
|
+
const start = i * chunkSize;
|
|
5462
|
+
const end = Math.min((i + 1) * chunkSize, entries.length);
|
|
5463
|
+
ranges.push(`${end - start} begincidchar\n${entries.slice(start, end).join('\n')}\nendcidchar`);
|
|
5464
|
+
}
|
|
5465
|
+
cmap.end(`\
|
|
5466
|
+
/CIDInit /ProcSet findresource begin
|
|
5467
|
+
12 dict begin
|
|
5468
|
+
begincmap
|
|
5469
|
+
/CIDSystemInfo <<
|
|
5470
|
+
/Registry (Adobe)
|
|
5471
|
+
/Ordering (Identity)
|
|
5472
|
+
/Supplement 0
|
|
5473
|
+
>> def
|
|
5474
|
+
/CMapName /Adobe-Identity-WinAnsi def
|
|
5475
|
+
/CMapType 1 def
|
|
5476
|
+
1 begincodespacerange
|
|
5477
|
+
<20> <ff>
|
|
5478
|
+
endcodespacerange
|
|
5479
|
+
${ranges.join('\n')}
|
|
5480
|
+
endcmap
|
|
5481
|
+
CMapName currentdict /CMap defineresource pop
|
|
5482
|
+
end
|
|
5483
|
+
end\
|
|
5484
|
+
`);
|
|
5485
|
+
return cmap;
|
|
5486
|
+
}
|
|
3688
5487
|
toUnicodeCmap() {
|
|
3689
5488
|
const cmap = this.document.ref();
|
|
3690
5489
|
const entries = [];
|
|
@@ -3801,6 +5600,7 @@ const isEqualFont = (font1, font2) => {
|
|
|
3801
5600
|
var FontsMixin = {
|
|
3802
5601
|
initFonts(defaultFont = 'Helvetica', defaultFontFamily = null, defaultFontSize = 12) {
|
|
3803
5602
|
this._fontFamilies = {};
|
|
5603
|
+
this._fontsBySource = new WeakMap();
|
|
3804
5604
|
this._fontCount = 0;
|
|
3805
5605
|
this._fontSource = defaultFont;
|
|
3806
5606
|
this._fontFamily = defaultFontFamily;
|
|
@@ -3839,12 +5639,19 @@ var FontsMixin = {
|
|
|
3839
5639
|
this._font = font;
|
|
3840
5640
|
return this;
|
|
3841
5641
|
}
|
|
5642
|
+
const bySource = !family && src !== null && typeof src === 'object';
|
|
5643
|
+
if (bySource && (font = this._fontsBySource.get(src))) {
|
|
5644
|
+
this._font = font;
|
|
5645
|
+
return this;
|
|
5646
|
+
}
|
|
3842
5647
|
const id = `F${++this._fontCount}`;
|
|
3843
5648
|
this._font = PDFFontFactory.open(this, src, family, id);
|
|
3844
5649
|
if ((font = this._fontFamilies[this._font.name]) && isEqualFont(this._font, font)) {
|
|
3845
5650
|
this._font = font;
|
|
5651
|
+
if (bySource) this._fontsBySource.set(src, font);
|
|
3846
5652
|
return this;
|
|
3847
5653
|
}
|
|
5654
|
+
if (bySource) this._fontsBySource.set(src, this._font);
|
|
3848
5655
|
if (cacheKey) {
|
|
3849
5656
|
this._fontFamilies[cacheKey] = this._font;
|
|
3850
5657
|
}
|
|
@@ -3932,6 +5739,8 @@ var FontsMixin = {
|
|
|
3932
5739
|
|
|
3933
5740
|
const SOFT_HYPHEN = '\u00AD';
|
|
3934
5741
|
const HYPHEN = '-';
|
|
5742
|
+
const WHITESPACE_BREAK = /[\s\u00AD\u200B]/;
|
|
5743
|
+
const isWhitespaceBreak = (text, bk) => bk.required || bk.position >= text.length || WHITESPACE_BREAK.test(text[bk.position - 1]);
|
|
3935
5744
|
class LineWrapper extends EventEmitter {
|
|
3936
5745
|
constructor(document, options) {
|
|
3937
5746
|
super();
|
|
@@ -3949,6 +5758,7 @@ class LineWrapper extends EventEmitter {
|
|
|
3949
5758
|
this.startY = this.document.y;
|
|
3950
5759
|
this.column = 1;
|
|
3951
5760
|
this.ellipsis = options.ellipsis;
|
|
5761
|
+
this.breakAtWhitespaceOnly = options.lineBreak === 'whitespace';
|
|
3952
5762
|
this.continuedX = 0;
|
|
3953
5763
|
this.indentApplied = false;
|
|
3954
5764
|
this.features = options.features;
|
|
@@ -4017,6 +5827,9 @@ class LineWrapper extends EventEmitter {
|
|
|
4017
5827
|
let last = null;
|
|
4018
5828
|
const wordWidths = Object.create(null);
|
|
4019
5829
|
while (bk = breaker.nextBreak()) {
|
|
5830
|
+
if (this.breakAtWhitespaceOnly && !isWhitespaceBreak(text, bk)) {
|
|
5831
|
+
continue;
|
|
5832
|
+
}
|
|
4020
5833
|
var shouldContinue;
|
|
4021
5834
|
let word = text.slice((last != null ? last.position : undefined) || 0, bk.position);
|
|
4022
5835
|
let w = wordWidths[word] != null ? wordWidths[word] : wordWidths[word] = this.wordWidth(word);
|
|
@@ -4026,7 +5839,7 @@ class LineWrapper extends EventEmitter {
|
|
|
4026
5839
|
while (word.length) {
|
|
4027
5840
|
var l, mightGrow;
|
|
4028
5841
|
if (w > this.spaceLeft) {
|
|
4029
|
-
l = Math.ceil(this.spaceLeft / (w / word.length));
|
|
5842
|
+
l = Math.max(0, Math.ceil(this.spaceLeft / (w / word.length)));
|
|
4030
5843
|
w = this.wordWidth(word.slice(0, l));
|
|
4031
5844
|
mightGrow = w <= this.spaceLeft && l < word.length;
|
|
4032
5845
|
} else {
|
|
@@ -4083,6 +5896,9 @@ class LineWrapper extends EventEmitter {
|
|
|
4083
5896
|
if (options.ellipsis != null) {
|
|
4084
5897
|
this.ellipsis = options.ellipsis;
|
|
4085
5898
|
}
|
|
5899
|
+
if (options.lineBreak != null) {
|
|
5900
|
+
this.breakAtWhitespaceOnly = options.lineBreak === 'whitespace';
|
|
5901
|
+
}
|
|
4086
5902
|
const nextY = document.y + document.currentLineHeight(true);
|
|
4087
5903
|
if (document.y > this.maxY || nextY > this.maxY) {
|
|
4088
5904
|
this.nextSection();
|
|
@@ -4252,7 +6068,7 @@ var TextMixin = {
|
|
|
4252
6068
|
options = this._initOptions(x, y, options);
|
|
4253
6069
|
text = text == null ? '' : `${text}`;
|
|
4254
6070
|
if (options.wordSpacing) {
|
|
4255
|
-
text = text.replace(
|
|
6071
|
+
text = text.replace(/[^\S\n]{2,}/g, ' ');
|
|
4256
6072
|
}
|
|
4257
6073
|
const addStructure = () => {
|
|
4258
6074
|
if (options.structParent) {
|
|
@@ -4302,7 +6118,7 @@ var TextMixin = {
|
|
|
4302
6118
|
let contentWidth = 0;
|
|
4303
6119
|
string = String(string ?? '');
|
|
4304
6120
|
if (options.wordSpacing) {
|
|
4305
|
-
string = string.replace(
|
|
6121
|
+
string = string.replace(/[^\S\n]{2,}/g, ' ');
|
|
4306
6122
|
}
|
|
4307
6123
|
if (options.width) {
|
|
4308
6124
|
let wrapper = new LineWrapper(this, options);
|
|
@@ -4318,13 +6134,18 @@ var TextMixin = {
|
|
|
4318
6134
|
const spaceWidth = this.widthOfString(' ') + characterSpacing;
|
|
4319
6135
|
wordSpacing = Math.max(0, (options.lineWidth - textWidth) / Math.max(1, words.length - 1) - spaceWidth);
|
|
4320
6136
|
}
|
|
4321
|
-
contentWidth = Math.max(contentWidth,
|
|
6137
|
+
contentWidth = Math.max(contentWidth, this.widthOfString(text, options) + wordSpacing * (options.wordCount - 1));
|
|
4322
6138
|
}
|
|
4323
6139
|
});
|
|
4324
6140
|
wrapper.wrap(string, options);
|
|
4325
6141
|
} else {
|
|
4326
6142
|
for (let line of string.split('\n')) {
|
|
4327
|
-
|
|
6143
|
+
let lineWidth = this.widthOfString(line, options);
|
|
6144
|
+
if (options.wordSpacing) {
|
|
6145
|
+
const trimmed = line.trim();
|
|
6146
|
+
const space = /\s/g;
|
|
6147
|
+
while (space.test(trimmed)) lineWidth += options.wordSpacing;
|
|
6148
|
+
}
|
|
4328
6149
|
this.y += lineHeight;
|
|
4329
6150
|
contentWidth = Math.max(contentWidth, lineWidth);
|
|
4330
6151
|
}
|
|
@@ -4391,6 +6212,7 @@ var TextMixin = {
|
|
|
4391
6212
|
options = this._initOptions(options);
|
|
4392
6213
|
options.height = Infinity;
|
|
4393
6214
|
const lineGap = options.lineGap || this._lineGap || 0;
|
|
6215
|
+
if (!options.continued && options.width) this._wrapper = null;
|
|
4394
6216
|
this._text(text, this.x, this.y, options, () => {
|
|
4395
6217
|
this.y += this.currentLineHeight(true) + lineGap;
|
|
4396
6218
|
});
|
|
@@ -4407,12 +6229,19 @@ var TextMixin = {
|
|
|
4407
6229
|
const r = options.bulletRadius || unit / 3;
|
|
4408
6230
|
const indent = options.textIndent || (listType === 'bullet' ? r * 5 : unit * 2);
|
|
4409
6231
|
const itemIndent = options.bulletIndent || (listType === 'bullet' ? r * 8 : unit * 2);
|
|
6232
|
+
const {
|
|
6233
|
+
lineWidth,
|
|
6234
|
+
textWidth,
|
|
6235
|
+
wordCount,
|
|
6236
|
+
...labelOptions
|
|
6237
|
+
} = options;
|
|
6238
|
+
labelOptions.align = 'left';
|
|
4410
6239
|
let level = 1;
|
|
4411
6240
|
const items = [];
|
|
4412
6241
|
const levels = [];
|
|
4413
6242
|
const numbers = [];
|
|
4414
6243
|
var flatten = function (list) {
|
|
4415
|
-
let n = 1;
|
|
6244
|
+
let n = level === 1 ? options.start ?? 1 : 1;
|
|
4416
6245
|
for (let i = 0; i < list.length; i++) {
|
|
4417
6246
|
const item = list[i];
|
|
4418
6247
|
if (Array.isArray(item)) {
|
|
@@ -4466,7 +6295,7 @@ var TextMixin = {
|
|
|
4466
6295
|
case 'numbered':
|
|
4467
6296
|
case 'lettered':
|
|
4468
6297
|
var text = formatListLabel(numbers[i - 1], listType);
|
|
4469
|
-
this._fragment(text, this.x - indent, this.y,
|
|
6298
|
+
this._fragment(text, this.x - indent, this.y, labelOptions);
|
|
4470
6299
|
break;
|
|
4471
6300
|
}
|
|
4472
6301
|
if (item && labelType && bodyType) {
|
|
@@ -4554,10 +6383,15 @@ var TextMixin = {
|
|
|
4554
6383
|
switch (align) {
|
|
4555
6384
|
case 'right':
|
|
4556
6385
|
textWidth = this.widthOfString(text.replace(/\s+$/, ''), options);
|
|
6386
|
+
if (wordSpacing) {
|
|
6387
|
+
const gaps = text.trim().split(/\s+/).length - 1;
|
|
6388
|
+
textWidth += wordSpacing * gaps * horizontalScaling / 100;
|
|
6389
|
+
}
|
|
4557
6390
|
x += options.lineWidth - textWidth;
|
|
4558
6391
|
break;
|
|
4559
6392
|
case 'center':
|
|
4560
|
-
|
|
6393
|
+
textWidth = options.textWidth - (characterSpacing + wordSpacing * options.wordCount) * horizontalScaling / 100;
|
|
6394
|
+
x += options.lineWidth / 2 - textWidth / 2;
|
|
4561
6395
|
break;
|
|
4562
6396
|
case 'justify':
|
|
4563
6397
|
words = text.trim().split(/\s+/);
|
|
@@ -4599,7 +6433,8 @@ var TextMixin = {
|
|
|
4599
6433
|
}
|
|
4600
6434
|
dy = dy / 1000 * this._fontSize;
|
|
4601
6435
|
}
|
|
4602
|
-
const
|
|
6436
|
+
const lineTop = y + dy - this._font.ascender / 1000 * this._fontSize;
|
|
6437
|
+
const measuredWidth = this.widthOfString(text, options) - characterSpacing * (text.length - 1);
|
|
4603
6438
|
const measuredWordCount = options.wordCount ?? (text.trim() ? text.trim().split(/\s+/).length : 0);
|
|
4604
6439
|
const renderedWidth = measuredWidth + wordSpacing * (measuredWordCount - 1) + characterSpacing * (text.length - 1);
|
|
4605
6440
|
if (options.link != null) {
|
|
@@ -4607,10 +6442,10 @@ var TextMixin = {
|
|
|
4607
6442
|
if (this._currentStructureElement && this._currentStructureElement.dictionary.data.S === 'Link') {
|
|
4608
6443
|
linkOptions.structParent = this._currentStructureElement;
|
|
4609
6444
|
}
|
|
4610
|
-
this.link(x,
|
|
6445
|
+
this.link(x, lineTop, renderedWidth, this.currentLineHeight(), options.link, linkOptions);
|
|
4611
6446
|
}
|
|
4612
6447
|
if (options.goTo != null) {
|
|
4613
|
-
this.goTo(x,
|
|
6448
|
+
this.goTo(x, lineTop, renderedWidth, this.currentLineHeight(), options.goTo);
|
|
4614
6449
|
}
|
|
4615
6450
|
if (options.destination != null) {
|
|
4616
6451
|
this.addNamedDestination(options.destination, 'XYZ', x, y, null);
|
|
@@ -4620,9 +6455,9 @@ var TextMixin = {
|
|
|
4620
6455
|
if (!options.stroke) {
|
|
4621
6456
|
this.strokeColor(...(this._fillColor || []));
|
|
4622
6457
|
}
|
|
4623
|
-
const lineWidth = this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10);
|
|
6458
|
+
const lineWidth = options.decorationThickness ?? (this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10));
|
|
4624
6459
|
this.lineWidth(lineWidth);
|
|
4625
|
-
let lineY =
|
|
6460
|
+
let lineY = lineTop + this.currentLineHeight() - lineWidth + (options.underlineOffset ?? 0);
|
|
4626
6461
|
this.moveTo(x, lineY);
|
|
4627
6462
|
this.lineTo(x + renderedWidth, lineY);
|
|
4628
6463
|
this.stroke();
|
|
@@ -4633,9 +6468,9 @@ var TextMixin = {
|
|
|
4633
6468
|
if (!options.stroke) {
|
|
4634
6469
|
this.strokeColor(...(this._fillColor || []));
|
|
4635
6470
|
}
|
|
4636
|
-
const lineWidth = this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10);
|
|
6471
|
+
const lineWidth = options.decorationThickness ?? (this._fontSize < 10 ? 0.5 : Math.floor(this._fontSize / 10));
|
|
4637
6472
|
this.lineWidth(lineWidth);
|
|
4638
|
-
let lineY =
|
|
6473
|
+
let lineY = lineTop + this.currentLineHeight() / 2;
|
|
4639
6474
|
this.moveTo(x, lineY);
|
|
4640
6475
|
this.lineTo(x + renderedWidth, lineY);
|
|
4641
6476
|
this.stroke();
|
|
@@ -4896,12 +6731,11 @@ class PNGImage {
|
|
|
4896
6731
|
}
|
|
4897
6732
|
obj.data['Mask'] = mask;
|
|
4898
6733
|
} else if (image.transparency.indexed) {
|
|
4899
|
-
|
|
6734
|
+
this.loadIndexedAlphaChannel();
|
|
4900
6735
|
} else if (hasAlphaChannel) {
|
|
4901
|
-
|
|
4902
|
-
}
|
|
4903
|
-
|
|
4904
|
-
return this.decodeData();
|
|
6736
|
+
this.splitAlphaChannel();
|
|
6737
|
+
} else if (isInterlaced && true) {
|
|
6738
|
+
this.decodeData();
|
|
4905
6739
|
}
|
|
4906
6740
|
this.finalize();
|
|
4907
6741
|
}
|
|
@@ -4922,52 +6756,46 @@ class PNGImage {
|
|
|
4922
6756
|
}
|
|
4923
6757
|
this.obj.end(this.imgData);
|
|
4924
6758
|
this.image = null;
|
|
4925
|
-
|
|
6759
|
+
this.imgData = null;
|
|
4926
6760
|
}
|
|
4927
6761
|
splitAlphaChannel() {
|
|
4928
|
-
|
|
4929
|
-
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
4935
|
-
|
|
4936
|
-
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
i += skipByteCount;
|
|
4941
|
-
}
|
|
4942
|
-
alphaChannel[a++] = pixels[i++];
|
|
6762
|
+
const pixels = this.image.decodePixelsSync();
|
|
6763
|
+
let a, p;
|
|
6764
|
+
const colorCount = this.image.colors;
|
|
6765
|
+
const pixelCount = this.width * this.height;
|
|
6766
|
+
const imgData = new Uint8Array(pixelCount * colorCount);
|
|
6767
|
+
const alphaChannel = new Uint8Array(pixelCount);
|
|
6768
|
+
let i = p = a = 0;
|
|
6769
|
+
const len = pixels.length;
|
|
6770
|
+
const skipByteCount = this.image.bits === 16 ? 1 : 0;
|
|
6771
|
+
while (i < len) {
|
|
6772
|
+
for (let colorIndex = 0; colorIndex < colorCount; colorIndex++) {
|
|
6773
|
+
imgData[p++] = pixels[i++];
|
|
4943
6774
|
i += skipByteCount;
|
|
4944
6775
|
}
|
|
4945
|
-
|
|
4946
|
-
|
|
4947
|
-
|
|
4948
|
-
|
|
6776
|
+
alphaChannel[a++] = pixels[i++];
|
|
6777
|
+
i += skipByteCount;
|
|
6778
|
+
}
|
|
6779
|
+
this.imgData = zlib.deflateSync(imgData);
|
|
6780
|
+
this.alphaChannel = zlib.deflateSync(alphaChannel);
|
|
4949
6781
|
}
|
|
4950
6782
|
loadIndexedAlphaChannel() {
|
|
4951
6783
|
const transparency = this.image.transparency.indexed;
|
|
4952
6784
|
const isInterlaced = this.image.interlaceMethod === 1;
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
4956
|
-
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
|
|
4962
|
-
|
|
4963
|
-
return this.finalize();
|
|
4964
|
-
});
|
|
6785
|
+
const pixels = this.image.decodePixelsSync();
|
|
6786
|
+
const alphaChannel = new Uint8Array(this.width * this.height);
|
|
6787
|
+
let i = 0;
|
|
6788
|
+
for (let j = 0, end = pixels.length; j < end; j++) {
|
|
6789
|
+
alphaChannel[i++] = transparency[pixels[j]];
|
|
6790
|
+
}
|
|
6791
|
+
if (isInterlaced) {
|
|
6792
|
+
this.imgData = zlib.deflateSync(pixels);
|
|
6793
|
+
}
|
|
6794
|
+
this.alphaChannel = zlib.deflateSync(alphaChannel);
|
|
4965
6795
|
}
|
|
4966
6796
|
decodeData() {
|
|
4967
|
-
this.image.
|
|
4968
|
-
|
|
4969
|
-
this.finalize();
|
|
4970
|
-
});
|
|
6797
|
+
const pixels = this.image.decodePixelsSync();
|
|
6798
|
+
this.imgData = zlib.deflateSync(pixels);
|
|
4971
6799
|
}
|
|
4972
6800
|
}
|
|
4973
6801
|
|
|
@@ -5050,11 +6878,14 @@ var ImagesMixin = {
|
|
|
5050
6878
|
} else if (options.scale) {
|
|
5051
6879
|
w = width * options.scale;
|
|
5052
6880
|
h = height * options.scale;
|
|
5053
|
-
} else if (options.fit) {
|
|
5054
|
-
[bw, bh] = options.fit;
|
|
6881
|
+
} else if (options.fit || options.scaleDown) {
|
|
6882
|
+
[bw, bh] = options.fit || options.scaleDown;
|
|
5055
6883
|
bp = bw / bh;
|
|
5056
6884
|
ip = width / height;
|
|
5057
|
-
if (
|
|
6885
|
+
if (!options.fit && width <= bw && height <= bh) {
|
|
6886
|
+
w = width;
|
|
6887
|
+
h = height;
|
|
6888
|
+
} else if (ip > bp) {
|
|
5058
6889
|
w = bw;
|
|
5059
6890
|
h = bw / ip;
|
|
5060
6891
|
} else {
|
|
@@ -5073,7 +6904,7 @@ var ImagesMixin = {
|
|
|
5073
6904
|
h = bw / ip;
|
|
5074
6905
|
}
|
|
5075
6906
|
}
|
|
5076
|
-
if (options.fit || options.cover) {
|
|
6907
|
+
if (options.fit || options.scaleDown || options.cover) {
|
|
5077
6908
|
if (options.align === 'center') {
|
|
5078
6909
|
x = x + bw / 2 - w / 2;
|
|
5079
6910
|
} else if (options.align === 'right') {
|
|
@@ -5165,6 +6996,12 @@ var ImagesMixin = {
|
|
|
5165
6996
|
if (options.opacity != null) {
|
|
5166
6997
|
this._doOpacity(options.opacity, null);
|
|
5167
6998
|
}
|
|
6999
|
+
if (options.flipH) {
|
|
7000
|
+
this.transform(-1, 0, 0, 1, 2 * x + w, 0);
|
|
7001
|
+
}
|
|
7002
|
+
if (options.flipV) {
|
|
7003
|
+
this.transform(1, 0, 0, -1, 0, 2 * y + h);
|
|
7004
|
+
}
|
|
5168
7005
|
if (rotateAngle) {
|
|
5169
7006
|
this.rotate(rotateAngle, {
|
|
5170
7007
|
origin: [originX, originY]
|
|
@@ -5191,8 +7028,9 @@ var ImagesMixin = {
|
|
|
5191
7028
|
};
|
|
5192
7029
|
|
|
5193
7030
|
class PDFAnnotationReference {
|
|
5194
|
-
constructor(annotationRef) {
|
|
7031
|
+
constructor(annotationRef, pageRef = annotationRef.document.page.dictionary) {
|
|
5195
7032
|
this.annotationRef = annotationRef;
|
|
7033
|
+
this.pageRef = pageRef;
|
|
5196
7034
|
}
|
|
5197
7035
|
}
|
|
5198
7036
|
|
|
@@ -5220,7 +7058,7 @@ var AnnotationsMixin = {
|
|
|
5220
7058
|
const ref = this.ref(annotations);
|
|
5221
7059
|
this.page.annotations.push(ref);
|
|
5222
7060
|
if (typeof structParent?.add === 'function') {
|
|
5223
|
-
const annotRef = new PDFAnnotationReference(ref);
|
|
7061
|
+
const annotRef = new PDFAnnotationReference(ref, this.page.dictionary);
|
|
5224
7062
|
structParent.add(annotRef);
|
|
5225
7063
|
}
|
|
5226
7064
|
ref.end();
|
|
@@ -5362,16 +7200,15 @@ var AnnotationsMixin = {
|
|
|
5362
7200
|
}
|
|
5363
7201
|
return this.annotate(x, y, w, h, annotationOptions);
|
|
5364
7202
|
},
|
|
5365
|
-
_convertRect(
|
|
5366
|
-
let y2 = y1;
|
|
5367
|
-
y1 += h;
|
|
5368
|
-
let x2 = x1 + w;
|
|
7203
|
+
_convertRect(x, y, w, h) {
|
|
5369
7204
|
const [m0, m1, m2, m3, m4, m5] = this._ctm;
|
|
5370
|
-
|
|
5371
|
-
|
|
5372
|
-
|
|
5373
|
-
|
|
5374
|
-
|
|
7205
|
+
const ox = m0 * x + m2 * y + m4;
|
|
7206
|
+
const oy = m1 * x + m3 * y + m5;
|
|
7207
|
+
const wx = m0 * w;
|
|
7208
|
+
const wy = m1 * w;
|
|
7209
|
+
const hx = m2 * h;
|
|
7210
|
+
const hy = m3 * h;
|
|
7211
|
+
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)];
|
|
5375
7212
|
}
|
|
5376
7213
|
};
|
|
5377
7214
|
|
|
@@ -5654,11 +7491,10 @@ class PDFStructureElement {
|
|
|
5654
7491
|
});
|
|
5655
7492
|
}
|
|
5656
7493
|
if (child instanceof PDFAnnotationReference) {
|
|
5657
|
-
const pageRef = this.document.page.dictionary;
|
|
5658
7494
|
const objr = {
|
|
5659
7495
|
Type: 'OBJR',
|
|
5660
7496
|
Obj: child.annotationRef,
|
|
5661
|
-
Pg: pageRef
|
|
7497
|
+
Pg: child.pageRef
|
|
5662
7498
|
};
|
|
5663
7499
|
this.dictionary.data.K.push(objr);
|
|
5664
7500
|
}
|
|
@@ -5859,7 +7695,8 @@ const FIELD_JUSTIFY = {
|
|
|
5859
7695
|
};
|
|
5860
7696
|
const VALUE_MAP = {
|
|
5861
7697
|
value: 'V',
|
|
5862
|
-
defaultValue: 'DV'
|
|
7698
|
+
defaultValue: 'DV',
|
|
7699
|
+
alternateName: 'TU'
|
|
5863
7700
|
};
|
|
5864
7701
|
const FORMAT_SPECIAL = {
|
|
5865
7702
|
zip: '0',
|
|
@@ -5910,6 +7747,11 @@ function mapTypeAndFlags(type, userOptions, pdfObject) {
|
|
|
5910
7747
|
pdfObject.Ff = flags;
|
|
5911
7748
|
}
|
|
5912
7749
|
}
|
|
7750
|
+
function mapVisibility(options, pdfObject) {
|
|
7751
|
+
if (options.hidden) {
|
|
7752
|
+
pdfObject.F = 6;
|
|
7753
|
+
}
|
|
7754
|
+
}
|
|
5913
7755
|
function mapJustify(userOptions, pdfObject) {
|
|
5914
7756
|
const result = FIELD_JUSTIFY[userOptions.align];
|
|
5915
7757
|
if (typeof result === 'number' && result !== 0) {
|
|
@@ -5941,6 +7783,28 @@ function mapStrings(options, pdfObject) {
|
|
|
5941
7783
|
pdfObject.MK.CA = new String(options.label);
|
|
5942
7784
|
}
|
|
5943
7785
|
}
|
|
7786
|
+
const ANNOTATION_ACTIONS = {
|
|
7787
|
+
onClick: 'U',
|
|
7788
|
+
onMouseDown: 'D',
|
|
7789
|
+
onMouseEnter: 'E',
|
|
7790
|
+
onMouseExit: 'X',
|
|
7791
|
+
onFocus: 'Fo',
|
|
7792
|
+
onBlur: 'Bl'
|
|
7793
|
+
};
|
|
7794
|
+
function mapActions(options, pdfObject) {
|
|
7795
|
+
for (const [option, key] of Object.entries(ANNOTATION_ACTIONS)) {
|
|
7796
|
+
const action = options[option];
|
|
7797
|
+
if (!action) {
|
|
7798
|
+
continue;
|
|
7799
|
+
}
|
|
7800
|
+
const js = typeof action === 'function' ? `(${action}).call(this);` : action;
|
|
7801
|
+
pdfObject.AA = pdfObject.AA ?? {};
|
|
7802
|
+
pdfObject.AA[key] = {
|
|
7803
|
+
S: 'JavaScript',
|
|
7804
|
+
JS: new String(js)
|
|
7805
|
+
};
|
|
7806
|
+
}
|
|
7807
|
+
}
|
|
5944
7808
|
function mapFormat(options, pdfObject) {
|
|
5945
7809
|
const f = options.format;
|
|
5946
7810
|
if (f?.type) {
|
|
@@ -5980,16 +7844,20 @@ function mapFormat(options, pdfObject) {
|
|
|
5980
7844
|
};
|
|
5981
7845
|
}
|
|
5982
7846
|
}
|
|
7847
|
+
function formFontRef(doc, font) {
|
|
7848
|
+
return doc._acroform.embedFonts && typeof font.completeRef === 'function' ? font.completeRef() : font.ref();
|
|
7849
|
+
}
|
|
5983
7850
|
var AcroFormMixin = {
|
|
5984
|
-
initForm() {
|
|
7851
|
+
initForm(options = {}) {
|
|
5985
7852
|
if (!this._font) {
|
|
5986
7853
|
throw new Error('Must set a font before calling initForm method');
|
|
5987
7854
|
}
|
|
5988
7855
|
this._acroform = {
|
|
5989
7856
|
fonts: {},
|
|
5990
|
-
defaultFont: this._font.name
|
|
7857
|
+
defaultFont: this._font.name,
|
|
7858
|
+
embedFonts: options.embedFonts === true
|
|
5991
7859
|
};
|
|
5992
|
-
this._acroform.fonts[this._font.id] = this._font
|
|
7860
|
+
this._acroform.fonts[this._font.id] = formFontRef(this, this._font);
|
|
5993
7861
|
let data = {
|
|
5994
7862
|
Fields: [],
|
|
5995
7863
|
NeedAppearances: true,
|
|
@@ -5998,7 +7866,7 @@ var AcroFormMixin = {
|
|
|
5998
7866
|
Font: {}
|
|
5999
7867
|
}
|
|
6000
7868
|
};
|
|
6001
|
-
data.DR.Font[this._font.id] = this._font
|
|
7869
|
+
data.DR.Font[this._font.id] = formFontRef(this, this._font);
|
|
6002
7870
|
const AcroForm = this.ref(data);
|
|
6003
7871
|
this._root.data.AcroForm = AcroForm;
|
|
6004
7872
|
return this;
|
|
@@ -6040,7 +7908,10 @@ var AcroFormMixin = {
|
|
|
6040
7908
|
if (fieldDict.F === undefined) {
|
|
6041
7909
|
fieldDict.F = 4;
|
|
6042
7910
|
}
|
|
6043
|
-
this.annotate(x, y, w, h,
|
|
7911
|
+
this.annotate(x, y, w, h, {
|
|
7912
|
+
...fieldDict,
|
|
7913
|
+
structParent: options.structParent
|
|
7914
|
+
});
|
|
6044
7915
|
let annotRef = this.page.annotations[this.page.annotations.length - 1];
|
|
6045
7916
|
return this._addToParent(annotRef);
|
|
6046
7917
|
},
|
|
@@ -6080,11 +7951,13 @@ var AcroFormMixin = {
|
|
|
6080
7951
|
}
|
|
6081
7952
|
const pdfObject = {};
|
|
6082
7953
|
mapTypeAndFlags(type, options, pdfObject);
|
|
7954
|
+
mapVisibility(options, pdfObject);
|
|
6083
7955
|
mapJustify(options, pdfObject);
|
|
6084
7956
|
this._mapFont(options, pdfObject);
|
|
6085
7957
|
mapStrings(options, pdfObject);
|
|
6086
7958
|
this._mapColors(options, pdfObject);
|
|
6087
7959
|
mapFormat(options, pdfObject);
|
|
7960
|
+
mapActions(options, pdfObject);
|
|
6088
7961
|
pdfObject.T = new String(name);
|
|
6089
7962
|
if (options.parent) {
|
|
6090
7963
|
pdfObject.Parent = options.parent;
|
|
@@ -6111,14 +7984,17 @@ var AcroFormMixin = {
|
|
|
6111
7984
|
_font
|
|
6112
7985
|
} = this;
|
|
6113
7986
|
if (_acroform.fonts[_font.id] == null) {
|
|
6114
|
-
_acroform.fonts[_font.id] = _font
|
|
7987
|
+
_acroform.fonts[_font.id] = formFontRef(this, _font);
|
|
6115
7988
|
}
|
|
6116
|
-
|
|
7989
|
+
const isDefaultFont = _acroform.defaultFont === _font.name;
|
|
7990
|
+
if (!isDefaultFont) {
|
|
6117
7991
|
pdfObject.DR = {
|
|
6118
7992
|
Font: {}
|
|
6119
7993
|
};
|
|
7994
|
+
pdfObject.DR.Font[_font.id] = formFontRef(this, _font);
|
|
7995
|
+
}
|
|
7996
|
+
if (!isDefaultFont || options.fontSize != null) {
|
|
6120
7997
|
const fontSize = options.fontSize || 0;
|
|
6121
|
-
pdfObject.DR.Font[_font.id] = _font.ref();
|
|
6122
7998
|
pdfObject.DA = new String(`/${_font.id} ${fontSize} Tf 0 g`);
|
|
6123
7999
|
}
|
|
6124
8000
|
}
|
|
@@ -6218,8 +8094,63 @@ function isSameDate(a, b) {
|
|
|
6218
8094
|
return a.getTime() === b.getTime();
|
|
6219
8095
|
}
|
|
6220
8096
|
|
|
6221
|
-
const
|
|
8097
|
+
const STYLES = {
|
|
8098
|
+
decimal: 'D',
|
|
8099
|
+
upperRoman: 'R',
|
|
8100
|
+
lowerRoman: 'r',
|
|
8101
|
+
upperAlpha: 'A',
|
|
8102
|
+
lowerAlpha: 'a'
|
|
8103
|
+
};
|
|
8104
|
+
var PageLabelsMixin = {
|
|
8105
|
+
addPageLabel(pageIndex, options = {}) {
|
|
8106
|
+
if (!Number.isInteger(pageIndex) || pageIndex < 0) {
|
|
8107
|
+
throw new Error(`addPageLabel(${pageIndex}) invalid, pageIndex must be a non-negative integer`);
|
|
8108
|
+
}
|
|
8109
|
+
const {
|
|
8110
|
+
style,
|
|
8111
|
+
prefix,
|
|
8112
|
+
start
|
|
8113
|
+
} = options;
|
|
8114
|
+
const label = {};
|
|
8115
|
+
if (style != null) {
|
|
8116
|
+
if (!Object.hasOwn(STYLES, style)) {
|
|
8117
|
+
throw new Error(`addPageLabel style ${style} invalid, must be one of ${Object.keys(STYLES).join(', ')}`);
|
|
8118
|
+
}
|
|
8119
|
+
label.S = STYLES[style];
|
|
8120
|
+
}
|
|
8121
|
+
if (prefix != null) {
|
|
8122
|
+
label.P = new String(prefix);
|
|
8123
|
+
}
|
|
8124
|
+
if (start != null) {
|
|
8125
|
+
if (!Number.isInteger(start) || start < 1) {
|
|
8126
|
+
throw new Error(`addPageLabel start ${start} invalid, must be a positive integer`);
|
|
8127
|
+
}
|
|
8128
|
+
label.St = start;
|
|
8129
|
+
}
|
|
8130
|
+
if (!this._root.data.PageLabels) {
|
|
8131
|
+
this._root.data.PageLabels = new PDFNumberTree({
|
|
8132
|
+
limits: false
|
|
8133
|
+
});
|
|
8134
|
+
this._root.data.PageLabels.add(0, {
|
|
8135
|
+
S: STYLES.decimal
|
|
8136
|
+
});
|
|
8137
|
+
}
|
|
8138
|
+
this._root.data.PageLabels.add(pageIndex, label);
|
|
8139
|
+
return this;
|
|
8140
|
+
}
|
|
8141
|
+
};
|
|
8142
|
+
|
|
8143
|
+
let iccProfilePath;
|
|
6222
8144
|
let iccProfile;
|
|
8145
|
+
const getIccProfilePath = () => {
|
|
8146
|
+
if (iccProfilePath === undefined) {
|
|
8147
|
+
if (import.meta.url == null) {
|
|
8148
|
+
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.');
|
|
8149
|
+
}
|
|
8150
|
+
iccProfilePath = new URL('./data/sRGB_IEC61966_2_1.icc', import.meta.url).href;
|
|
8151
|
+
}
|
|
8152
|
+
return iccProfilePath;
|
|
8153
|
+
};
|
|
6223
8154
|
var PDFA = {
|
|
6224
8155
|
initPDFA(pSubset) {
|
|
6225
8156
|
if (pSubset.charAt(pSubset.length - 3) === '-') {
|
|
@@ -6236,7 +8167,7 @@ var PDFA = {
|
|
|
6236
8167
|
},
|
|
6237
8168
|
_addColorOutputIntent() {
|
|
6238
8169
|
if (!iccProfile) {
|
|
6239
|
-
iccProfile = fs.readFileSync(
|
|
8170
|
+
iccProfile = fs.readFileSync(getIccProfilePath());
|
|
6240
8171
|
}
|
|
6241
8172
|
const colorProfileRef = this.ref({
|
|
6242
8173
|
Length: iccProfile.length,
|
|
@@ -6617,15 +8548,25 @@ function measure(row, rowIndex) {
|
|
|
6617
8548
|
}
|
|
6618
8549
|
this._rowHeights[rowIndex] = rowHeight;
|
|
6619
8550
|
let newPage = false;
|
|
8551
|
+
let repeatHeaders = false;
|
|
6620
8552
|
if (rowHeight > this.document.page.contentHeight) {
|
|
6621
8553
|
console.warn(new Error(`Row ${rowIndex} requested more than the safe page height, row has been clamped`).stack.slice(7));
|
|
6622
8554
|
this._rowHeights[rowIndex] = this.document.page.maxY() - this._rowYPos[rowIndex];
|
|
6623
8555
|
} else if (this._rowYPos[rowIndex] + rowHeight >= this.document.page.maxY()) {
|
|
6624
|
-
|
|
8556
|
+
let top = this.document.page.margins.top;
|
|
8557
|
+
if (rowIndex >= this._headerRows) {
|
|
8558
|
+
const headerHeight = this._rowHeights.slice(0, this._headerRows).reduce((acc, height) => acc + height, 0);
|
|
8559
|
+
if (this._headerRows > 0 && rowHeight + headerHeight <= this.document.page.contentHeight) {
|
|
8560
|
+
top += headerHeight;
|
|
8561
|
+
repeatHeaders = true;
|
|
8562
|
+
}
|
|
8563
|
+
}
|
|
8564
|
+
this._rowYPos[rowIndex] = top;
|
|
6625
8565
|
newPage = true;
|
|
6626
8566
|
}
|
|
6627
8567
|
return {
|
|
6628
8568
|
newPage,
|
|
8569
|
+
repeatHeaders,
|
|
6629
8570
|
toRender: toRender.map(cell => measureCell.call(this, cell, rowHeight))
|
|
6630
8571
|
};
|
|
6631
8572
|
}
|
|
@@ -6827,6 +8768,22 @@ function renderRow(row, rowIndex) {
|
|
|
6827
8768
|
}
|
|
6828
8769
|
return this._rowYPos[rowIndex] + this._rowHeights[rowIndex];
|
|
6829
8770
|
}
|
|
8771
|
+
function renderHeaderRows(cells) {
|
|
8772
|
+
const doc = this.document;
|
|
8773
|
+
if (this._tableStruct) doc.markContent('Artifact', {
|
|
8774
|
+
type: 'Pagination'
|
|
8775
|
+
});
|
|
8776
|
+
cells.forEach(cell => {
|
|
8777
|
+
let y = doc.page.margins.top;
|
|
8778
|
+
for (let i = 0; i < cell.rowIndex; i++) y += this._rowHeights[i];
|
|
8779
|
+
renderCell.call(this, {
|
|
8780
|
+
...cell,
|
|
8781
|
+
y,
|
|
8782
|
+
textY: y + cell.padding.top
|
|
8783
|
+
});
|
|
8784
|
+
});
|
|
8785
|
+
if (this._tableStruct) doc.endMarkedContent();
|
|
8786
|
+
}
|
|
6830
8787
|
function renderCell(cell, rowStruct) {
|
|
6831
8788
|
const cellRenderer = () => {
|
|
6832
8789
|
if (cell.backgroundColor != null) {
|
|
@@ -6915,6 +8872,8 @@ class PDFTable {
|
|
|
6915
8872
|
this.opts = Object.freeze(opts);
|
|
6916
8873
|
normalizeTable.call(this);
|
|
6917
8874
|
accommodateTable.call(this);
|
|
8875
|
+
this._headerRows = opts.headerRows ?? 0;
|
|
8876
|
+
this._headerCells = [];
|
|
6918
8877
|
this._currRowIndex = 0;
|
|
6919
8878
|
this._ended = false;
|
|
6920
8879
|
if (opts.data) {
|
|
@@ -6931,9 +8890,14 @@ class PDFTable {
|
|
|
6931
8890
|
if (this._currRowIndex === 0) ensure.call(this, row);
|
|
6932
8891
|
const {
|
|
6933
8892
|
newPage,
|
|
8893
|
+
repeatHeaders,
|
|
6934
8894
|
toRender
|
|
6935
8895
|
} = measure.call(this, row, this._currRowIndex);
|
|
6936
8896
|
if (newPage) this.document.continueOnNewPage();
|
|
8897
|
+
if (repeatHeaders) renderHeaderRows.call(this, this._headerCells);
|
|
8898
|
+
if (this._currRowIndex < this._headerRows) {
|
|
8899
|
+
this._headerCells.push(...toRender);
|
|
8900
|
+
}
|
|
6937
8901
|
const yPos = renderRow.call(this, toRender, this._currRowIndex);
|
|
6938
8902
|
this.document.x = this._position.x;
|
|
6939
8903
|
this.document.y = yPos;
|
|
@@ -6989,6 +8953,9 @@ class PDFMetadata {
|
|
|
6989
8953
|
}
|
|
6990
8954
|
}
|
|
6991
8955
|
|
|
8956
|
+
function escapeXML(value) {
|
|
8957
|
+
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, '
');
|
|
8958
|
+
}
|
|
6992
8959
|
var MetadataMixin = {
|
|
6993
8960
|
initMetadata() {
|
|
6994
8961
|
this.metadata = new PDFMetadata();
|
|
@@ -7000,7 +8967,7 @@ var MetadataMixin = {
|
|
|
7000
8967
|
this.appendXML(`
|
|
7001
8968
|
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/">
|
|
7002
8969
|
<xmp:CreateDate>${this.info.CreationDate.toISOString().split('.')[0] + 'Z'}</xmp:CreateDate>
|
|
7003
|
-
<xmp:CreatorTool>${this.info.Creator}</xmp:CreatorTool>
|
|
8970
|
+
<xmp:CreatorTool>${escapeXML(this.info.Creator)}</xmp:CreatorTool>
|
|
7004
8971
|
</rdf:Description>
|
|
7005
8972
|
`);
|
|
7006
8973
|
if (this.info.Title || this.info.Author || this.info.Subject) {
|
|
@@ -7011,7 +8978,7 @@ var MetadataMixin = {
|
|
|
7011
8978
|
this.appendXML(`
|
|
7012
8979
|
<dc:title>
|
|
7013
8980
|
<rdf:Alt>
|
|
7014
|
-
<rdf:li xml:lang="x-default">${this.info.Title}</rdf:li>
|
|
8981
|
+
<rdf:li xml:lang="x-default">${escapeXML(this.info.Title)}</rdf:li>
|
|
7015
8982
|
</rdf:Alt>
|
|
7016
8983
|
</dc:title>
|
|
7017
8984
|
`);
|
|
@@ -7020,7 +8987,7 @@ var MetadataMixin = {
|
|
|
7020
8987
|
this.appendXML(`
|
|
7021
8988
|
<dc:creator>
|
|
7022
8989
|
<rdf:Seq>
|
|
7023
|
-
<rdf:li>${this.info.Author}</rdf:li>
|
|
8990
|
+
<rdf:li>${escapeXML(this.info.Author)}</rdf:li>
|
|
7024
8991
|
</rdf:Seq>
|
|
7025
8992
|
</dc:creator>
|
|
7026
8993
|
`);
|
|
@@ -7029,7 +8996,7 @@ var MetadataMixin = {
|
|
|
7029
8996
|
this.appendXML(`
|
|
7030
8997
|
<dc:description>
|
|
7031
8998
|
<rdf:Alt>
|
|
7032
|
-
<rdf:li xml:lang="x-default">${this.info.Subject}</rdf:li>
|
|
8999
|
+
<rdf:li xml:lang="x-default">${escapeXML(this.info.Subject)}</rdf:li>
|
|
7033
9000
|
</rdf:Alt>
|
|
7034
9001
|
</dc:description>
|
|
7035
9002
|
`);
|
|
@@ -7040,10 +9007,10 @@ var MetadataMixin = {
|
|
|
7040
9007
|
}
|
|
7041
9008
|
this.appendXML(`
|
|
7042
9009
|
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/">
|
|
7043
|
-
<pdf:Producer>${this.info.Producer}</pdf:Producer>`, false);
|
|
9010
|
+
<pdf:Producer>${escapeXML(this.info.Producer)}</pdf:Producer>`, false);
|
|
7044
9011
|
if (this.info.Keywords) {
|
|
7045
9012
|
this.appendXML(`
|
|
7046
|
-
<pdf:Keywords>${this.info.Keywords}</pdf:Keywords>`, false);
|
|
9013
|
+
<pdf:Keywords>${escapeXML(this.info.Keywords)}</pdf:Keywords>`, false);
|
|
7047
9014
|
}
|
|
7048
9015
|
this.appendXML(`
|
|
7049
9016
|
</rdf:Description>
|
|
@@ -7089,6 +9056,9 @@ class PDFDocument extends Readable {
|
|
|
7089
9056
|
break;
|
|
7090
9057
|
}
|
|
7091
9058
|
this.compress = this.options.compress != null ? this.options.compress : true;
|
|
9059
|
+
if (options.objectStreams) {
|
|
9060
|
+
this._objectStreams = new PDFObjectStreams(this, options);
|
|
9061
|
+
}
|
|
7092
9062
|
this._pageBuffer = [];
|
|
7093
9063
|
this._pageBufferStart = 0;
|
|
7094
9064
|
this._offsets = [];
|
|
@@ -7253,8 +9223,8 @@ class PDFDocument extends Readable {
|
|
|
7253
9223
|
_refEnd(ref) {
|
|
7254
9224
|
this._offsets[ref.id - 1] = ref.offset;
|
|
7255
9225
|
if (--this._waiting === 0 && this._ended) {
|
|
7256
|
-
this._finalize();
|
|
7257
9226
|
this._ended = false;
|
|
9227
|
+
this._finalize();
|
|
7258
9228
|
}
|
|
7259
9229
|
}
|
|
7260
9230
|
end() {
|
|
@@ -7297,6 +9267,11 @@ class PDFDocument extends Readable {
|
|
|
7297
9267
|
}
|
|
7298
9268
|
}
|
|
7299
9269
|
_finalize() {
|
|
9270
|
+
if (this._objectStreams) {
|
|
9271
|
+
this._objectStreams.finalize();
|
|
9272
|
+
this.push(null);
|
|
9273
|
+
return;
|
|
9274
|
+
}
|
|
7300
9275
|
const xRefOffset = this._offset;
|
|
7301
9276
|
this._write('xref');
|
|
7302
9277
|
this._write(`0 ${this._offsets.length + 1}`);
|
|
@@ -7339,12 +9314,13 @@ mixin(OutlineMixin);
|
|
|
7339
9314
|
mixin(MarkingsMixin);
|
|
7340
9315
|
mixin(AcroFormMixin);
|
|
7341
9316
|
mixin(AttachmentsMixin);
|
|
9317
|
+
mixin(PageLabelsMixin);
|
|
7342
9318
|
mixin(SubsetMixin);
|
|
7343
9319
|
mixin(TableMixin);
|
|
7344
9320
|
|
|
7345
9321
|
var iccProfileBase64 = "AAAL0AAAAAACAAAAbW50clJHQiBYWVogB98AAgAPAAAAAAAAYWNzcAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAAAAAAAAAAPbWAAEAAAAA0y0AAAAAPQ6y3q6Tl76bZybOjApDzgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQZGVzYwAAAUQAAABjYlhZWgAAAagAAAAUYlRSQwAAAbwAAAgMZ1RSQwAAAbwAAAgMclRSQwAAAbwAAAgMZG1kZAAACcgAAACIZ1hZWgAAClAAAAAUbHVtaQAACmQAAAAUbWVhcwAACngAAAAkYmtwdAAACpwAAAAUclhZWgAACrAAAAAUdGVjaAAACsQAAAAMdnVlZAAACtAAAACHd3RwdAAAC1gAAAAUY3BydAAAC2wAAAA3Y2hhZAAAC6QAAAAsZGVzYwAAAAAAAAAJc1JHQjIwMTQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAAAkoAAAD4QAALbPY3VydgAAAAAAAAQAAAAABQAKAA8AFAAZAB4AIwAoAC0AMgA3ADsAQABFAEoATwBUAFkAXgBjAGgAbQByAHcAfACBAIYAiwCQAJUAmgCfAKQAqQCuALIAtwC8AMEAxgDLANAA1QDbAOAA5QDrAPAA9gD7AQEBBwENARMBGQEfASUBKwEyATgBPgFFAUwBUgFZAWABZwFuAXUBfAGDAYsBkgGaAaEBqQGxAbkBwQHJAdEB2QHhAekB8gH6AgMCDAIUAh0CJgIvAjgCQQJLAlQCXQJnAnECegKEAo4CmAKiAqwCtgLBAssC1QLgAusC9QMAAwsDFgMhAy0DOANDA08DWgNmA3IDfgOKA5YDogOuA7oDxwPTA+AD7AP5BAYEEwQgBC0EOwRIBFUEYwRxBH4EjASaBKgEtgTEBNME4QTwBP4FDQUcBSsFOgVJBVgFZwV3BYYFlgWmBbUFxQXVBeUF9gYGBhYGJwY3BkgGWQZqBnsGjAadBq8GwAbRBuMG9QcHBxkHKwc9B08HYQd0B4YHmQesB78H0gflB/gICwgfCDIIRghaCG4IggiWCKoIvgjSCOcI+wkQCSUJOglPCWQJeQmPCaQJugnPCeUJ+woRCicKPQpUCmoKgQqYCq4KxQrcCvMLCwsiCzkLUQtpC4ALmAuwC8gL4Qv5DBIMKgxDDFwMdQyODKcMwAzZDPMNDQ0mDUANWg10DY4NqQ3DDd4N+A4TDi4OSQ5kDn8Omw62DtIO7g8JDyUPQQ9eD3oPlg+zD88P7BAJECYQQxBhEH4QmxC5ENcQ9RETETERTxFtEYwRqhHJEegSBxImEkUSZBKEEqMSwxLjEwMTIxNDE2MTgxOkE8UT5RQGFCcUSRRqFIsUrRTOFPAVEhU0FVYVeBWbFb0V4BYDFiYWSRZsFo8WshbWFvoXHRdBF2UXiReuF9IX9xgbGEAYZRiKGK8Y1Rj6GSAZRRlrGZEZtxndGgQaKhpRGncanhrFGuwbFBs7G2MbihuyG9ocAhwqHFIcexyjHMwc9R0eHUcdcB2ZHcMd7B4WHkAeah6UHr4e6R8THz4faR+UH78f6iAVIEEgbCCYIMQg8CEcIUghdSGhIc4h+yInIlUigiKvIt0jCiM4I2YjlCPCI/AkHyRNJHwkqyTaJQklOCVoJZclxyX3JicmVyaHJrcm6CcYJ0kneierJ9woDSg/KHEooijUKQYpOClrKZ0p0CoCKjUqaCqbKs8rAis2K2krnSvRLAUsOSxuLKIs1y0MLUEtdi2rLeEuFi5MLoIuty7uLyQvWi+RL8cv/jA1MGwwpDDbMRIxSjGCMbox8jIqMmMymzLUMw0zRjN/M7gz8TQrNGU0njTYNRM1TTWHNcI1/TY3NnI2rjbpNyQ3YDecN9c4FDhQOIw4yDkFOUI5fzm8Ofk6Njp0OrI67zstO2s7qjvoPCc8ZTykPOM9Ij1hPaE94D4gPmA+oD7gPyE/YT+iP+JAI0BkQKZA50EpQWpBrEHuQjBCckK1QvdDOkN9Q8BEA0RHRIpEzkUSRVVFmkXeRiJGZ0arRvBHNUd7R8BIBUhLSJFI10kdSWNJqUnwSjdKfUrESwxLU0uaS+JMKkxyTLpNAk1KTZNN3E4lTm5Ot08AT0lPk0/dUCdQcVC7UQZRUFGbUeZSMVJ8UsdTE1NfU6pT9lRCVI9U21UoVXVVwlYPVlxWqVb3V0RXklfgWC9YfVjLWRpZaVm4WgdaVlqmWvVbRVuVW+VcNVyGXNZdJ114XcleGl5sXr1fD19hX7NgBWBXYKpg/GFPYaJh9WJJYpxi8GNDY5dj62RAZJRk6WU9ZZJl52Y9ZpJm6Gc9Z5Nn6Wg/aJZo7GlDaZpp8WpIap9q92tPa6dr/2xXbK9tCG1gbbluEm5rbsRvHm94b9FwK3CGcOBxOnGVcfByS3KmcwFzXXO4dBR0cHTMdSh1hXXhdj52m3b4d1Z3s3gReG54zHkqeYl553pGeqV7BHtje8J8IXyBfOF9QX2hfgF+Yn7CfyN/hH/lgEeAqIEKgWuBzYIwgpKC9INXg7qEHYSAhOOFR4Wrhg6GcobXhzuHn4gEiGmIzokziZmJ/opkisqLMIuWi/yMY4zKjTGNmI3/jmaOzo82j56QBpBukNaRP5GokhGSepLjk02TtpQglIqU9JVflcmWNJaflwqXdZfgmEyYuJkkmZCZ/JpomtWbQpuvnByciZz3nWSd0p5Anq6fHZ+Ln/qgaaDYoUehtqImopajBqN2o+akVqTHpTilqaYapoum/adup+CoUqjEqTepqaocqo+rAqt1q+msXKzQrUStuK4trqGvFq+LsACwdbDqsWCx1rJLssKzOLOutCW0nLUTtYq2AbZ5tvC3aLfguFm40blKucK6O7q1uy67p7whvJu9Fb2Pvgq+hL7/v3q/9cBwwOzBZ8Hjwl/C28NYw9TEUcTOxUvFyMZGxsPHQce/yD3IvMk6ybnKOMq3yzbLtsw1zLXNNc21zjbOts83z7jQOdC60TzRvtI/0sHTRNPG1EnUy9VO1dHWVdbY11zX4Nhk2OjZbNnx2nba+9uA3AXcit0Q3ZbeHN6i3ynfr+A24L3hROHM4lPi2+Nj4+vkc+T85YTmDeaW5x/nqegy6LzpRunQ6lvq5etw6/vshu0R7ZzuKO6070DvzPBY8OXxcvH/8ozzGfOn9DT0wvVQ9d72bfb794r4Gfio+Tj5x/pX+uf7d/wH/Jj9Kf26/kv+3P9t//9kZXNjAAAAAAAAAC5JRUMgNjE5NjYtMi0xIERlZmF1bHQgUkdCIENvbG91ciBTcGFjZSAtIHNSR0IAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAAAAAUAAAAAAAAG1lYXMAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlhZWiAAAAAAAAAAngAAAKQAAACHWFlaIAAAAAAAAG+iAAA49QAAA5BzaWcgAAAAAENSVCBkZXNjAAAAAAAAAC1SZWZlcmVuY2UgVmlld2luZyBDb25kaXRpb24gaW4gSUVDIDYxOTY2LTItMQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWFlaIAAAAAAAAPbWAAEAAAAA0y10ZXh0AAAAAENvcHlyaWdodCBJbnRlcm5hdGlvbmFsIENvbG9yIENvbnNvcnRpdW0sIDIwMTUAAHNmMzIAAAAAAAEMRAAABd////MmAAAHlAAA/Y////uh///9ogAAA9sAAMB1";
|
|
7346
9322
|
|
|
7347
|
-
registerFile(
|
|
9323
|
+
registerFile(getIccProfilePath(), fromBase64(iccProfileBase64));
|
|
7348
9324
|
|
|
7349
|
-
export { LineWrapper, PDFDocument, PDFDocument as default, registerFile, registerStdFonts };
|
|
9325
|
+
export { LineWrapper, PAGE_SIZES, PDFDocument, PDFDocument as default, registerFile, registerStdFonts };
|
|
7350
9326
|
//# sourceMappingURL=pdfkit.browser.mjs.map
|