mol_crypto2_lib 0.0.37 → 0.0.39
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/node.d.ts +235 -1
- package/node.d.ts.map +1 -1
- package/node.deps.json +1 -1
- package/node.js +246 -9
- package/node.js.map +1 -1
- package/node.mjs +246 -9
- package/node.test.js +317 -15
- package/node.test.js.map +1 -1
- package/package.json +1 -1
- package/web.d.ts +65 -0
- package/web.d.ts.map +1 -1
- package/web.deps.json +1 -1
- package/web.js +68 -1
- package/web.js.map +1 -1
- package/web.mjs +68 -1
- package/web.test.js +76 -6
- package/web.test.js.map +1 -1
package/web.js
CHANGED
|
@@ -198,6 +198,7 @@ var $;
|
|
|
198
198
|
this.setUint16(offset + 4, (value / 2 ** 32) | 0, LE);
|
|
199
199
|
}
|
|
200
200
|
}
|
|
201
|
+
/** 1-byte signed integer channel for offset. */
|
|
201
202
|
int8(offset, next) {
|
|
202
203
|
if (next === undefined)
|
|
203
204
|
return this.getInt8(offset);
|
|
@@ -205,6 +206,7 @@ var $;
|
|
|
205
206
|
return this.setInt8(offset, next), next;
|
|
206
207
|
$mol_fail(new Error(`Wrong int8 value ${next}`));
|
|
207
208
|
}
|
|
209
|
+
/** 1-byte unsigned integer channel for offset. */
|
|
208
210
|
uint8(offset, next) {
|
|
209
211
|
if (next === undefined)
|
|
210
212
|
return this.getUint8(offset);
|
|
@@ -212,6 +214,7 @@ var $;
|
|
|
212
214
|
return this.setUint8(offset, next), next;
|
|
213
215
|
$mol_fail(new Error(`Wrong uint8 value ${next}`));
|
|
214
216
|
}
|
|
217
|
+
/** 2-byte signed integer little-endian channel for offset. */
|
|
215
218
|
int16(offset, next) {
|
|
216
219
|
if (next === undefined)
|
|
217
220
|
return this.getInt16(offset, true);
|
|
@@ -219,6 +222,7 @@ var $;
|
|
|
219
222
|
return this.setInt16(offset, next, true), next;
|
|
220
223
|
$mol_fail(new Error(`Wrong int16 value ${next}`));
|
|
221
224
|
}
|
|
225
|
+
/** 2-byte unsigned integer little-endian channel for offset. */
|
|
222
226
|
uint16(offset, next) {
|
|
223
227
|
if (next === undefined)
|
|
224
228
|
return this.getUint16(offset, true);
|
|
@@ -226,6 +230,7 @@ var $;
|
|
|
226
230
|
return this.setUint16(offset, next, true), next;
|
|
227
231
|
$mol_fail(new Error(`Wrong uint16 value ${next}`));
|
|
228
232
|
}
|
|
233
|
+
/** 4-byte signed integer little-endian channel for offset. */
|
|
229
234
|
int32(offset, next) {
|
|
230
235
|
if (next === undefined)
|
|
231
236
|
return this.getInt32(offset, true);
|
|
@@ -233,6 +238,7 @@ var $;
|
|
|
233
238
|
return this.setInt32(offset, next, true), next;
|
|
234
239
|
$mol_fail(new Error(`Wrong int32 value ${next}`));
|
|
235
240
|
}
|
|
241
|
+
/** 4-byte unsigned integer little-endian channel for offset. */
|
|
236
242
|
uint32(offset, next) {
|
|
237
243
|
if (next === undefined)
|
|
238
244
|
return this.getUint32(offset, true);
|
|
@@ -240,6 +246,7 @@ var $;
|
|
|
240
246
|
return this.setUint32(offset, next, true), next;
|
|
241
247
|
$mol_fail(new Error(`Wrong uint32 value ${next}`));
|
|
242
248
|
}
|
|
249
|
+
/** 8-byte signed integer little-endian channel for offset. */
|
|
243
250
|
int64(offset, next) {
|
|
244
251
|
if (next === undefined)
|
|
245
252
|
return this.getBigInt64(offset, true);
|
|
@@ -247,6 +254,7 @@ var $;
|
|
|
247
254
|
return this.setBigInt64(offset, next, true), next;
|
|
248
255
|
$mol_fail(new Error(`Wrong int64 value ${next}`));
|
|
249
256
|
}
|
|
257
|
+
/** 6-byte unsigned integer little-endian channel for offset. */
|
|
250
258
|
uint48(offset, next) {
|
|
251
259
|
if (next === undefined)
|
|
252
260
|
return this.getUint48(offset, true);
|
|
@@ -254,6 +262,7 @@ var $;
|
|
|
254
262
|
return this.setUint48(offset, next, true), next;
|
|
255
263
|
$mol_fail(new Error(`Wrong uint48 value ${next}`));
|
|
256
264
|
}
|
|
265
|
+
/** 8-byte unsigned integer little-endian channel for offset. */
|
|
257
266
|
uint64(offset, next) {
|
|
258
267
|
if (next === undefined)
|
|
259
268
|
return this.getBigUint64(offset, true);
|
|
@@ -261,24 +270,29 @@ var $;
|
|
|
261
270
|
return this.setBigUint64(offset, next, true), next;
|
|
262
271
|
$mol_fail(new Error(`Wrong uint64 value ${next}`));
|
|
263
272
|
}
|
|
273
|
+
/** 2-byte float little-endian channel for offset. */
|
|
264
274
|
float16(offset, next) {
|
|
265
275
|
if (next !== undefined)
|
|
266
276
|
this.setFloat16(offset, next, true);
|
|
267
277
|
return this.getFloat16(offset, true);
|
|
268
278
|
}
|
|
279
|
+
/** 4-byte float little-endian channel for offset. */
|
|
269
280
|
float32(offset, next) {
|
|
270
281
|
if (next !== undefined)
|
|
271
282
|
this.setFloat32(offset, next, true);
|
|
272
283
|
return this.getFloat32(offset, true);
|
|
273
284
|
}
|
|
285
|
+
/** 8-byte float little-endian channel for offset. */
|
|
274
286
|
float64(offset, next) {
|
|
275
287
|
if (next !== undefined)
|
|
276
288
|
this.setFloat64(offset, next, true);
|
|
277
289
|
return this.getFloat64(offset, true);
|
|
278
290
|
}
|
|
291
|
+
/** A Uint8Array view for the same buffer. */
|
|
279
292
|
asArray() {
|
|
280
293
|
return new Uint8Array(this.buffer, this.byteOffset, this.byteLength);
|
|
281
294
|
}
|
|
295
|
+
/** base64ae string from buffer. */
|
|
282
296
|
toString() {
|
|
283
297
|
return $mol_base64_ae_encode(this.asArray());
|
|
284
298
|
}
|
|
@@ -336,6 +350,11 @@ var $;
|
|
|
336
350
|
var $;
|
|
337
351
|
(function ($) {
|
|
338
352
|
const instances = new WeakSet();
|
|
353
|
+
/**
|
|
354
|
+
* Proxy that delegates all to lazy returned target.
|
|
355
|
+
*
|
|
356
|
+
* $mol_delegate( Array.prototype , ()=> fetch_array() )
|
|
357
|
+
*/
|
|
339
358
|
function $mol_delegate(proto, target) {
|
|
340
359
|
const proxy = new Proxy(proto, {
|
|
341
360
|
get: (_, field) => {
|
|
@@ -429,7 +448,7 @@ var $;
|
|
|
429
448
|
var $;
|
|
430
449
|
(function ($) {
|
|
431
450
|
function $mol_fail_hidden(error) {
|
|
432
|
-
throw error;
|
|
451
|
+
throw error; /// Use 'Never Pause Here' breakpoint in DevTools or simply blackbox this script
|
|
433
452
|
}
|
|
434
453
|
$.$mol_fail_hidden = $mol_fail_hidden;
|
|
435
454
|
})($ || ($ = {}));
|
|
@@ -489,6 +508,9 @@ var $;
|
|
|
489
508
|
[Symbol.dispose]() {
|
|
490
509
|
this.destructor();
|
|
491
510
|
}
|
|
511
|
+
//[ Symbol.toPrimitive ]( hint: string ) {
|
|
512
|
+
// return hint === 'number' ? this.valueOf() : this.toString()
|
|
513
|
+
//}
|
|
492
514
|
toString() {
|
|
493
515
|
return this[Symbol.toStringTag] || this.constructor.name + '<>';
|
|
494
516
|
}
|
|
@@ -559,9 +581,11 @@ var $;
|
|
|
559
581
|
"use strict";
|
|
560
582
|
var $;
|
|
561
583
|
(function ($) {
|
|
584
|
+
/** Base class for crypto keys. */
|
|
562
585
|
class $mol_crypto2_key extends $mol_buffer {
|
|
563
586
|
static size_str = 43;
|
|
564
587
|
static size_bin = 32;
|
|
588
|
+
/** Kakes key from different params. */
|
|
565
589
|
static from(serial) {
|
|
566
590
|
if (typeof serial === 'string') {
|
|
567
591
|
serial = new Uint8Array(serial.match(/.{43}/g)
|
|
@@ -573,6 +597,7 @@ var $;
|
|
|
573
597
|
}
|
|
574
598
|
return super.from(serial);
|
|
575
599
|
}
|
|
600
|
+
/** Array view of public part. */
|
|
576
601
|
asArray() {
|
|
577
602
|
const size = this.constructor.size_bin;
|
|
578
603
|
if (this.byteLength < size) {
|
|
@@ -583,6 +608,7 @@ var $;
|
|
|
583
608
|
}
|
|
584
609
|
return new Uint8Array(this.buffer, this.byteOffset, size);
|
|
585
610
|
}
|
|
611
|
+
/** String representation of public part. */
|
|
586
612
|
toString() {
|
|
587
613
|
return $mol_base64_url_encode(this.asArray());
|
|
588
614
|
}
|
|
@@ -604,6 +630,7 @@ var $;
|
|
|
604
630
|
"use strict";
|
|
605
631
|
var $;
|
|
606
632
|
(function ($) {
|
|
633
|
+
/** Derived debuggable error with stack */
|
|
607
634
|
function $mol_crypto_restack(error) {
|
|
608
635
|
error = new Error(error instanceof Error ? error.message : String(error), { cause: error });
|
|
609
636
|
$mol_fail_hidden(error);
|
|
@@ -615,7 +642,9 @@ var $;
|
|
|
615
642
|
"use strict";
|
|
616
643
|
var $;
|
|
617
644
|
(function ($) {
|
|
645
|
+
/** Ed25519 public key for sign verifying. */
|
|
618
646
|
class $mol_crypto2_auditor extends $mol_crypto2_key {
|
|
647
|
+
/** Native WebAPI public key. */
|
|
619
648
|
async native() {
|
|
620
649
|
return $mol_crypto_native.subtle.importKey('jwk', {
|
|
621
650
|
crv: "Ed25519",
|
|
@@ -625,6 +654,7 @@ var $;
|
|
|
625
654
|
x: this.toString(),
|
|
626
655
|
}, "Ed25519", Boolean('extractable'), ['verify']).catch($mol_crypto_restack);
|
|
627
656
|
}
|
|
657
|
+
/** Verifies signature of data. */
|
|
628
658
|
async verify(data, sign) {
|
|
629
659
|
return await $mol_crypto_native.subtle.verify("Ed25519", await this.native(), sign, data).catch($mol_crypto_restack);
|
|
630
660
|
}
|
|
@@ -639,13 +669,16 @@ var $;
|
|
|
639
669
|
"use strict";
|
|
640
670
|
var $;
|
|
641
671
|
(function ($) {
|
|
672
|
+
/** Ed25519 private key for data signing. */
|
|
642
673
|
class $mol_crypto2_signer extends $mol_crypto2_auditor {
|
|
643
674
|
static size_sign = 64;
|
|
675
|
+
/** Generates new Signer. */
|
|
644
676
|
static async generate() {
|
|
645
677
|
const pair = await $mol_crypto_native.subtle.generateKey("Ed25519", Boolean('extractable'), ['sign', 'verify']).catch($mol_crypto_restack);
|
|
646
678
|
const { x, d } = await $mol_crypto_native.subtle.exportKey('jwk', pair.privateKey).catch($mol_crypto_restack);
|
|
647
679
|
return this.from(x + d);
|
|
648
680
|
}
|
|
681
|
+
/** Native WebAPI private key. */
|
|
649
682
|
async nativePrivate() {
|
|
650
683
|
return await $mol_crypto_native.subtle.importKey('jwk', {
|
|
651
684
|
crv: "Ed25519",
|
|
@@ -656,15 +689,19 @@ var $;
|
|
|
656
689
|
d: this.toStringPrivate(),
|
|
657
690
|
}, "Ed25519", Boolean('extractable'), ['sign']).catch($mol_crypto_restack);
|
|
658
691
|
}
|
|
692
|
+
/** Array view of private part. */
|
|
659
693
|
asArrayPrivate() {
|
|
660
694
|
return new Uint8Array(this.buffer, this.byteOffset + 32, 32);
|
|
661
695
|
}
|
|
696
|
+
/** String representation of private part. */
|
|
662
697
|
toStringPrivate() {
|
|
663
698
|
return $mol_base64_url_encode(this.asArrayPrivate());
|
|
664
699
|
}
|
|
700
|
+
/** Returns Auditor from this Signer. */
|
|
665
701
|
auditor() {
|
|
666
702
|
return $mol_crypto2_auditor.from(this.asArray());
|
|
667
703
|
}
|
|
704
|
+
/** Makes Signature for data. */
|
|
668
705
|
async sign(data) {
|
|
669
706
|
return new Uint8Array(await $mol_crypto_native.subtle.sign("Ed25519", await this.nativePrivate(), data).catch($mol_crypto_restack));
|
|
670
707
|
}
|
|
@@ -685,7 +722,9 @@ var $;
|
|
|
685
722
|
"use strict";
|
|
686
723
|
var $;
|
|
687
724
|
(function ($) {
|
|
725
|
+
/** x25519 public key for data encryption. */
|
|
688
726
|
class $mol_crypto2_socket extends $mol_crypto2_key {
|
|
727
|
+
/** Native WebAPI public key. */
|
|
689
728
|
async native() {
|
|
690
729
|
return await $mol_crypto_native.subtle.importKey('jwk', {
|
|
691
730
|
crv: 'X25519',
|
|
@@ -706,6 +745,7 @@ var $;
|
|
|
706
745
|
"use strict";
|
|
707
746
|
var $;
|
|
708
747
|
(function ($) {
|
|
748
|
+
/** 16 unique bytes. */
|
|
709
749
|
function $mol_crypto2_nonce() {
|
|
710
750
|
return $mol_crypto_native.getRandomValues(new Uint8Array(16));
|
|
711
751
|
}
|
|
@@ -716,6 +756,7 @@ var $;
|
|
|
716
756
|
"use strict";
|
|
717
757
|
var $;
|
|
718
758
|
(function ($) {
|
|
759
|
+
/** @deprecated Use $mol_crypto2_nonce */
|
|
719
760
|
$.$mol_crypto_salt = $mol_crypto2_nonce;
|
|
720
761
|
})($ || ($ = {}));
|
|
721
762
|
|
|
@@ -723,11 +764,15 @@ var $;
|
|
|
723
764
|
"use strict";
|
|
724
765
|
var $;
|
|
725
766
|
(function ($) {
|
|
767
|
+
/** Symmetric cipher with shortest payload. */
|
|
726
768
|
class $mol_crypto_sacred extends $mol_buffer {
|
|
769
|
+
/** Key size in bytes. */
|
|
727
770
|
static size = 16;
|
|
771
|
+
/** Makes new random secret. */
|
|
728
772
|
static make() {
|
|
729
773
|
return this.from($mol_crypto_salt());
|
|
730
774
|
}
|
|
775
|
+
/** Makes from string of buffer view. */
|
|
731
776
|
static from(serial) {
|
|
732
777
|
if (typeof serial === 'string') {
|
|
733
778
|
serial = new Uint8Array([
|
|
@@ -757,12 +802,14 @@ var $;
|
|
|
757
802
|
return $mol_base64_url_encode(this.asArray());
|
|
758
803
|
}
|
|
759
804
|
_native;
|
|
805
|
+
/** Native crypto secret */
|
|
760
806
|
async native() {
|
|
761
807
|
return this._native ?? (this._native = await $mol_crypto_native.subtle.importKey('raw', this, {
|
|
762
808
|
name: 'AES-CBC',
|
|
763
809
|
length: 128,
|
|
764
810
|
}, true, ['encrypt', 'decrypt']).catch($mol_crypto_restack));
|
|
765
811
|
}
|
|
812
|
+
/** Encrypt any binary message. 16n bytes */
|
|
766
813
|
async encrypt(open, salt) {
|
|
767
814
|
return new Uint8Array(await $mol_crypto_native.subtle.encrypt({
|
|
768
815
|
name: 'AES-CBC',
|
|
@@ -771,6 +818,7 @@ var $;
|
|
|
771
818
|
iv: salt,
|
|
772
819
|
}, await this.native(), open).catch($mol_crypto_restack));
|
|
773
820
|
}
|
|
821
|
+
/** Decrypt any binary message. */
|
|
774
822
|
async decrypt(closed, salt) {
|
|
775
823
|
return new Uint8Array(await $mol_crypto_native.subtle.decrypt({
|
|
776
824
|
name: 'AES-CBC',
|
|
@@ -779,12 +827,14 @@ var $;
|
|
|
779
827
|
iv: salt,
|
|
780
828
|
}, await this.native(), closed).catch($mol_crypto_restack));
|
|
781
829
|
}
|
|
830
|
+
/** Encrypts 0xFF prefixed buffer. 16 bytes */
|
|
782
831
|
async close(opened, salt) {
|
|
783
832
|
if (opened.getUint8(0) !== 0xFF)
|
|
784
833
|
throw new Error('Closable buffer should starts with 0xFF');
|
|
785
834
|
const trimed = new Uint8Array(opened.buffer, opened.byteOffset + 1, opened.byteLength - 1);
|
|
786
835
|
return this.encrypt(trimed, salt);
|
|
787
836
|
}
|
|
837
|
+
/** Decrypts 0xFF prefixed buffer. 16 bytes */
|
|
788
838
|
async open(closed, salt) {
|
|
789
839
|
const trimed = await this.decrypt(closed, salt);
|
|
790
840
|
if (trimed.byteLength !== closed.byteLength - 1)
|
|
@@ -805,13 +855,16 @@ var $;
|
|
|
805
855
|
"use strict";
|
|
806
856
|
var $;
|
|
807
857
|
(function ($) {
|
|
858
|
+
/** x25519 private key for data encryption. */
|
|
808
859
|
class $mol_crypto2_cipher extends $mol_crypto2_socket {
|
|
809
860
|
static size_secret = 16;
|
|
861
|
+
/** Generates new Cipher. */
|
|
810
862
|
static async generate() {
|
|
811
863
|
const pair = await $mol_crypto_native.subtle.generateKey("X25519", Boolean('extractable'), ['deriveKey']).catch($mol_crypto_restack);
|
|
812
864
|
const { x, d } = await $mol_crypto_native.subtle.exportKey('jwk', pair.privateKey).catch($mol_crypto_restack);
|
|
813
865
|
return this.from(x + d);
|
|
814
866
|
}
|
|
867
|
+
/** Native WebAPI private key. */
|
|
815
868
|
async nativePrivate() {
|
|
816
869
|
return $mol_crypto_native.subtle.importKey('jwk', {
|
|
817
870
|
crv: 'X25519',
|
|
@@ -822,15 +875,19 @@ var $;
|
|
|
822
875
|
d: this.toStringPrivate(),
|
|
823
876
|
}, "X25519", Boolean('extractable'), ['deriveKey', 'deriveBits']).catch($mol_crypto_restack);
|
|
824
877
|
}
|
|
878
|
+
/** Array view of private part. */
|
|
825
879
|
asArrayPrivate() {
|
|
826
880
|
return new Uint8Array(this.buffer, this.byteOffset + 32, 32);
|
|
827
881
|
}
|
|
882
|
+
/** String representation of private part. */
|
|
828
883
|
toStringPrivate() {
|
|
829
884
|
return $mol_base64_url_encode(this.asArrayPrivate());
|
|
830
885
|
}
|
|
886
|
+
/** Returns Socket from this Chipher. */
|
|
831
887
|
socket() {
|
|
832
888
|
return $mol_crypto2_socket.from(this.asArray());
|
|
833
889
|
}
|
|
890
|
+
/** Makes shared secret for combination of Chiper and Soacket. */
|
|
834
891
|
async secret(pub) {
|
|
835
892
|
return $mol_crypto_sacred.from(new Uint8Array(await $mol_crypto_native.subtle.deriveBits({
|
|
836
893
|
name: "X25519",
|
|
@@ -854,12 +911,15 @@ var $;
|
|
|
854
911
|
"use strict";
|
|
855
912
|
var $;
|
|
856
913
|
(function ($) {
|
|
914
|
+
/** Compose public key for verifying and encryption, based on Curve25519. */
|
|
857
915
|
class $mol_crypto2_public extends $mol_crypto2_key {
|
|
858
916
|
static size_str = 86;
|
|
859
917
|
static size_bin = 64;
|
|
918
|
+
/** Return Auditor part. */
|
|
860
919
|
auditor() {
|
|
861
920
|
return $mol_crypto2_auditor.from(this.asArray().subarray(0, 32));
|
|
862
921
|
}
|
|
922
|
+
/** Return Socket part. */
|
|
863
923
|
socket() {
|
|
864
924
|
return $mol_crypto2_socket.from(this.asArray().subarray(32, 64));
|
|
865
925
|
}
|
|
@@ -883,7 +943,9 @@ var $;
|
|
|
883
943
|
"use strict";
|
|
884
944
|
var $;
|
|
885
945
|
(function ($) {
|
|
946
|
+
/** Compose private key for signing and encryption, based on Curve25519. */
|
|
886
947
|
class $mol_crypto2_private extends $mol_crypto2_public {
|
|
948
|
+
/** Generates new private key. */
|
|
887
949
|
static async generate() {
|
|
888
950
|
const [signer, cipher] = await Promise.all([
|
|
889
951
|
$mol_crypto2_signer.generate(),
|
|
@@ -896,24 +958,29 @@ var $;
|
|
|
896
958
|
key.asArrayPrivate().set(cipher.asArrayPrivate(), 32);
|
|
897
959
|
return key;
|
|
898
960
|
}
|
|
961
|
+
/** Return Signer part. */
|
|
899
962
|
signer() {
|
|
900
963
|
const signer = $mol_crypto2_signer.from($mol_crypto2_auditor.size_bin + $mol_crypto2_signer.size_bin);
|
|
901
964
|
signer.asArray().set(this.asArray().subarray(0, 32));
|
|
902
965
|
signer.asArrayPrivate().set(this.asArrayPrivate().subarray(0, 32));
|
|
903
966
|
return signer;
|
|
904
967
|
}
|
|
968
|
+
/** Return Cipher part. */
|
|
905
969
|
cipher() {
|
|
906
970
|
const cipher = $mol_crypto2_cipher.from($mol_crypto2_socket.size_bin + $mol_crypto2_cipher.size_bin);
|
|
907
971
|
cipher.asArray().set(this.asArray().subarray(32, 64));
|
|
908
972
|
cipher.asArrayPrivate().set(this.asArrayPrivate().subarray(32, 64));
|
|
909
973
|
return cipher;
|
|
910
974
|
}
|
|
975
|
+
/** Return Public part. */
|
|
911
976
|
public() {
|
|
912
977
|
return $mol_crypto2_public.from(this.asArray());
|
|
913
978
|
}
|
|
979
|
+
/** Array view of private part. */
|
|
914
980
|
asArrayPrivate() {
|
|
915
981
|
return new Uint8Array(this.buffer, this.byteOffset + 64, 64);
|
|
916
982
|
}
|
|
983
|
+
/** String representation of private part. */
|
|
917
984
|
toStringPrivate() {
|
|
918
985
|
return this.signer().toStringPrivate() + this.cipher().toStringPrivate();
|
|
919
986
|
}
|