mol_crypto_lib 0.1.1384 → 0.1.1386
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 +91 -81
- package/node.d.ts.map +1 -1
- package/node.deps.json +1 -1
- package/node.js +431 -369
- package/node.js.map +1 -1
- package/node.meta.tree +4 -1
- package/node.mjs +431 -369
- package/node.test.js +1383 -1302
- package/node.test.js.map +1 -1
- package/package.json +20 -18
- package/web.d.ts +50 -40
- package/web.d.ts.map +1 -1
- package/web.deps.json +1 -1
- package/web.js +229 -167
- package/web.js.map +1 -1
- package/web.meta.tree +4 -1
- package/web.mjs +229 -167
- package/web.test.js +71 -52
- package/web.test.js.map +1 -1
package/web.js
CHANGED
@@ -32,97 +32,10 @@ $.$$ = $
|
|
32
32
|
"use strict";
|
33
33
|
var $;
|
34
34
|
(function ($) {
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
;
|
39
|
-
"use strict";
|
40
|
-
|
41
|
-
;
|
42
|
-
"use strict";
|
43
|
-
var $node = $node || {};
|
44
|
-
|
45
|
-
;
|
46
|
-
"use strict";
|
47
|
-
var $;
|
48
|
-
(function ($) {
|
49
|
-
const TextEncoder = globalThis.TextEncoder ?? $node.util.TextEncoder;
|
50
|
-
const encoder = new TextEncoder();
|
51
|
-
function $mol_charset_encode(value) {
|
52
|
-
return encoder.encode(value);
|
53
|
-
}
|
54
|
-
$.$mol_charset_encode = $mol_charset_encode;
|
55
|
-
})($ || ($ = {}));
|
56
|
-
|
57
|
-
;
|
58
|
-
"use strict";
|
59
|
-
var $;
|
60
|
-
(function ($) {
|
61
|
-
const algorithm = {
|
62
|
-
name: 'AES-CBC',
|
63
|
-
length: 128,
|
64
|
-
tagLength: 32,
|
65
|
-
};
|
66
|
-
class $mol_crypto_secret extends Object {
|
67
|
-
native;
|
68
|
-
static size = 16;
|
69
|
-
constructor(native) {
|
70
|
-
super();
|
71
|
-
this.native = native;
|
72
|
-
}
|
73
|
-
static async generate() {
|
74
|
-
return new this(await $mol_crypto_native.subtle.generateKey(algorithm, true, ['encrypt', 'decrypt']));
|
75
|
-
}
|
76
|
-
static async from(serial) {
|
77
|
-
return new this(await $mol_crypto_native.subtle.importKey('raw', serial, algorithm, true, ['encrypt', 'decrypt']));
|
78
|
-
}
|
79
|
-
static async pass(pass, salt) {
|
80
|
-
return new this(await $mol_crypto_native.subtle.deriveKey({
|
81
|
-
name: "PBKDF2",
|
82
|
-
salt,
|
83
|
-
iterations: 10_000,
|
84
|
-
hash: "SHA-256",
|
85
|
-
}, await $mol_crypto_native.subtle.importKey("raw", $mol_charset_encode(pass), "PBKDF2", false, ["deriveKey"]), algorithm, true, ['encrypt', 'decrypt']));
|
86
|
-
}
|
87
|
-
static async derive(private_serial, public_serial) {
|
88
|
-
const ecdh = { name: "ECDH", namedCurve: "P-256" };
|
89
|
-
const jwk = { crv: 'P-256', ext: true, kty: 'EC' };
|
90
|
-
const private_key = await $mol_crypto_native.subtle.importKey('jwk', {
|
91
|
-
...jwk,
|
92
|
-
key_ops: ['deriveKey'],
|
93
|
-
x: private_serial.slice(0, 43),
|
94
|
-
y: private_serial.slice(43, 86),
|
95
|
-
d: private_serial.slice(86, 129),
|
96
|
-
}, ecdh, true, ['deriveKey']);
|
97
|
-
const public_key = await $mol_crypto_native.subtle.importKey('jwk', {
|
98
|
-
...jwk,
|
99
|
-
key_ops: [],
|
100
|
-
x: public_serial.slice(0, 43),
|
101
|
-
y: public_serial.slice(43, 86),
|
102
|
-
}, ecdh, true, []);
|
103
|
-
const secret = await $mol_crypto_native.subtle.deriveKey({
|
104
|
-
name: "ECDH",
|
105
|
-
public: public_key,
|
106
|
-
}, private_key, algorithm, true, ["encrypt", "decrypt"]);
|
107
|
-
return new this(secret);
|
108
|
-
}
|
109
|
-
async serial() {
|
110
|
-
return new Uint8Array(await $mol_crypto_native.subtle.exportKey('raw', this.native));
|
111
|
-
}
|
112
|
-
async encrypt(open, salt) {
|
113
|
-
return new Uint8Array(await $mol_crypto_native.subtle.encrypt({
|
114
|
-
...algorithm,
|
115
|
-
iv: salt,
|
116
|
-
}, this.native, open));
|
117
|
-
}
|
118
|
-
async decrypt(closed, salt) {
|
119
|
-
return new Uint8Array(await $mol_crypto_native.subtle.decrypt({
|
120
|
-
...algorithm,
|
121
|
-
iv: salt,
|
122
|
-
}, this.native, closed));
|
123
|
-
}
|
35
|
+
function $mol_base64_encode(src) {
|
36
|
+
throw new Error('Not implemented');
|
124
37
|
}
|
125
|
-
$.$
|
38
|
+
$.$mol_base64_encode = $mol_base64_encode;
|
126
39
|
})($ || ($ = {}));
|
127
40
|
|
128
41
|
;
|
@@ -145,63 +58,6 @@ var $;
|
|
145
58
|
$.$mol_dom = $mol_dom_context;
|
146
59
|
})($ || ($ = {}));
|
147
60
|
|
148
|
-
;
|
149
|
-
"use strict";
|
150
|
-
var $;
|
151
|
-
(function ($) {
|
152
|
-
async function $mol_crypto_secret_id() {
|
153
|
-
const signed = this.$mol_dom_context.localStorage.getItem('$mol_crypto_secret');
|
154
|
-
if (signed === '')
|
155
|
-
return await this.$mol_crypto_secret_id_get();
|
156
|
-
const id = await this.$mol_crypto_secret_id_new();
|
157
|
-
this.$mol_dom_context.localStorage.setItem('$mol_crypto_secret', '');
|
158
|
-
return id;
|
159
|
-
}
|
160
|
-
$.$mol_crypto_secret_id = $mol_crypto_secret_id;
|
161
|
-
async function $mol_crypto_secret_id_new() {
|
162
|
-
const cred = await this.$mol_dom_context.navigator.credentials.create({
|
163
|
-
publicKey: {
|
164
|
-
rp: {
|
165
|
-
name: "$mol_crypto_id",
|
166
|
-
},
|
167
|
-
user: {
|
168
|
-
id: new Uint8Array([0]),
|
169
|
-
name: "",
|
170
|
-
displayName: ""
|
171
|
-
},
|
172
|
-
pubKeyCredParams: [
|
173
|
-
{ type: "public-key", alg: -7 },
|
174
|
-
{ type: "public-key", alg: -257 },
|
175
|
-
],
|
176
|
-
challenge: new Uint8Array().buffer,
|
177
|
-
},
|
178
|
-
});
|
179
|
-
return $mol_crypto_secret.from(cred.rawId);
|
180
|
-
}
|
181
|
-
$.$mol_crypto_secret_id_new = $mol_crypto_secret_id_new;
|
182
|
-
async function $mol_crypto_secret_id_get() {
|
183
|
-
const cred = await this.$mol_dom_context.navigator.credentials.get({
|
184
|
-
mediation: 'silent',
|
185
|
-
publicKey: {
|
186
|
-
userVerification: 'discouraged',
|
187
|
-
challenge: new Uint8Array().buffer,
|
188
|
-
},
|
189
|
-
});
|
190
|
-
return $mol_crypto_secret.from(cred.rawId);
|
191
|
-
}
|
192
|
-
$.$mol_crypto_secret_id_get = $mol_crypto_secret_id_get;
|
193
|
-
})($ || ($ = {}));
|
194
|
-
|
195
|
-
;
|
196
|
-
"use strict";
|
197
|
-
var $;
|
198
|
-
(function ($) {
|
199
|
-
function $mol_base64_encode(src) {
|
200
|
-
throw new Error('Not implemented');
|
201
|
-
}
|
202
|
-
$.$mol_base64_encode = $mol_base64_encode;
|
203
|
-
})($ || ($ = {}));
|
204
|
-
|
205
61
|
;
|
206
62
|
"use strict";
|
207
63
|
var $;
|
@@ -417,6 +273,24 @@ var $;
|
|
417
273
|
$.$mol_buffer = $mol_buffer;
|
418
274
|
})($ || ($ = {}));
|
419
275
|
|
276
|
+
;
|
277
|
+
"use strict";
|
278
|
+
var $;
|
279
|
+
(function ($) {
|
280
|
+
$.$mol_crypto_native = crypto;
|
281
|
+
})($ || ($ = {}));
|
282
|
+
|
283
|
+
;
|
284
|
+
"use strict";
|
285
|
+
var $;
|
286
|
+
(function ($) {
|
287
|
+
function $mol_crypto_salt() {
|
288
|
+
return $mol_crypto_native.getRandomValues(new Uint8Array(16));
|
289
|
+
}
|
290
|
+
$.$mol_crypto_salt = $mol_crypto_salt;
|
291
|
+
$.$mol_crypto_salt_once = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]);
|
292
|
+
})($ || ($ = {}));
|
293
|
+
|
420
294
|
;
|
421
295
|
"use strict";
|
422
296
|
var $;
|
@@ -654,11 +528,175 @@ var $;
|
|
654
528
|
"use strict";
|
655
529
|
var $;
|
656
530
|
(function ($) {
|
657
|
-
|
531
|
+
class $mol_crypto_sacred extends $mol_buffer {
|
532
|
+
static size = 16;
|
533
|
+
static make() {
|
534
|
+
return this.from($mol_crypto_salt());
|
535
|
+
}
|
536
|
+
static from(serial) {
|
537
|
+
if (typeof serial === 'string') {
|
538
|
+
serial = new Uint8Array([
|
539
|
+
...$mol_base64_url_decode(serial),
|
540
|
+
]);
|
541
|
+
}
|
542
|
+
if (!(serial instanceof Uint8Array)) {
|
543
|
+
serial = new Uint8Array(serial.buffer, serial.byteOffset, serial.byteLength);
|
544
|
+
}
|
545
|
+
;
|
546
|
+
serial[0] = 0;
|
547
|
+
const sacred = super.from(serial);
|
548
|
+
return sacred;
|
549
|
+
}
|
550
|
+
static async from_native(native) {
|
551
|
+
const buf = await $mol_crypto_native.subtle.exportKey('raw', native);
|
552
|
+
const sacred = this.from(new Uint8Array(buf));
|
553
|
+
sacred._native = native;
|
554
|
+
return sacred;
|
555
|
+
}
|
556
|
+
constructor(buffer, byteOffset, byteLength) {
|
557
|
+
super(buffer, byteOffset, byteLength);
|
558
|
+
if (this.getUint8(0) !== 0)
|
559
|
+
$mol_fail(new Error('Buffer should starts with 0 byte'));
|
560
|
+
}
|
561
|
+
toString() {
|
562
|
+
return $mol_base64_url_encode(this.asArray());
|
563
|
+
}
|
564
|
+
_native;
|
565
|
+
async native() {
|
566
|
+
return this._native ?? (this._native = await $mol_crypto_native.subtle.importKey('raw', this, {
|
567
|
+
name: 'AES-CBC',
|
568
|
+
length: 128,
|
569
|
+
}, true, ['encrypt', 'decrypt']));
|
570
|
+
}
|
571
|
+
async encrypt(open, salt) {
|
572
|
+
return new Uint8Array(await $mol_crypto_native.subtle.encrypt({
|
573
|
+
name: 'AES-CBC',
|
574
|
+
length: 128,
|
575
|
+
tagLength: 32,
|
576
|
+
iv: salt,
|
577
|
+
}, await this.native(), open));
|
578
|
+
}
|
579
|
+
async decrypt(closed, salt) {
|
580
|
+
return new Uint8Array(await $mol_crypto_native.subtle.decrypt({
|
581
|
+
name: 'AES-CBC',
|
582
|
+
length: 128,
|
583
|
+
tagLength: 32,
|
584
|
+
iv: salt,
|
585
|
+
}, await this.native(), closed));
|
586
|
+
}
|
587
|
+
async close(sacred, salt) {
|
588
|
+
const buf = new Uint8Array(this.buffer, this.byteOffset + 1, this.byteLength - 1);
|
589
|
+
return sacred.encrypt(buf, salt);
|
590
|
+
}
|
591
|
+
}
|
592
|
+
__decorate([
|
593
|
+
$mol_memo.method
|
594
|
+
], $mol_crypto_sacred.prototype, "toString", null);
|
595
|
+
$.$mol_crypto_sacred = $mol_crypto_sacred;
|
596
|
+
})($ || ($ = {}));
|
597
|
+
|
598
|
+
;
|
599
|
+
"use strict";
|
600
|
+
var $;
|
601
|
+
(function ($) {
|
602
|
+
async function $mol_crypto_sacred_id() {
|
603
|
+
const inited = this.$mol_dom.localStorage.getItem('$mol_crypto_sacred_id');
|
604
|
+
if (inited === 'true')
|
605
|
+
return await this.$mol_crypto_sacred_id_get();
|
606
|
+
const id = await this.$mol_crypto_sacred_id_new();
|
607
|
+
this.$mol_dom.localStorage.setItem('$mol_crypto_sacred_id', 'true');
|
608
|
+
return id;
|
609
|
+
}
|
610
|
+
$.$mol_crypto_sacred_id = $mol_crypto_sacred_id;
|
611
|
+
async function $mol_crypto_sacred_id_new() {
|
612
|
+
const cred = await this.$mol_dom.navigator.credentials.create({
|
613
|
+
publicKey: {
|
614
|
+
rp: {
|
615
|
+
name: "$mol_crypto_sacred_id",
|
616
|
+
},
|
617
|
+
authenticatorSelection: {
|
618
|
+
userVerification: 'discouraged',
|
619
|
+
residentKey: 'discouraged',
|
620
|
+
},
|
621
|
+
user: {
|
622
|
+
id: new Uint8Array([0]),
|
623
|
+
name: "",
|
624
|
+
displayName: ""
|
625
|
+
},
|
626
|
+
pubKeyCredParams: [],
|
627
|
+
challenge: new Uint8Array().buffer,
|
628
|
+
},
|
629
|
+
});
|
630
|
+
console.log(cred);
|
631
|
+
const key = new Uint8Array(cred.rawId, 0, 16);
|
632
|
+
return $mol_crypto_sacred.from(key);
|
633
|
+
}
|
634
|
+
$.$mol_crypto_sacred_id_new = $mol_crypto_sacred_id_new;
|
635
|
+
async function $mol_crypto_sacred_id_get() {
|
636
|
+
const cred = await this.$mol_dom.navigator.credentials.get({
|
637
|
+
mediation: 'silent',
|
638
|
+
publicKey: {
|
639
|
+
userVerification: 'discouraged',
|
640
|
+
challenge: new Uint8Array().buffer,
|
641
|
+
},
|
642
|
+
});
|
643
|
+
const key = new Uint8Array(cred.rawId, 0, 16);
|
644
|
+
console.log(key);
|
645
|
+
return $mol_crypto_sacred.from(key);
|
646
|
+
}
|
647
|
+
$.$mol_crypto_sacred_id_get = $mol_crypto_sacred_id_get;
|
648
|
+
})($ || ($ = {}));
|
649
|
+
|
650
|
+
;
|
651
|
+
"use strict";
|
652
|
+
|
653
|
+
;
|
654
|
+
"use strict";
|
655
|
+
var $node = $node || {};
|
656
|
+
|
657
|
+
;
|
658
|
+
"use strict";
|
659
|
+
var $;
|
660
|
+
(function ($) {
|
661
|
+
const TextEncoder = globalThis.TextEncoder ?? $node.util.TextEncoder;
|
662
|
+
const encoder = new TextEncoder();
|
663
|
+
function $mol_charset_encode(value) {
|
664
|
+
return encoder.encode(value);
|
665
|
+
}
|
666
|
+
$.$mol_charset_encode = $mol_charset_encode;
|
667
|
+
})($ || ($ = {}));
|
668
|
+
|
669
|
+
;
|
670
|
+
"use strict";
|
671
|
+
var $;
|
672
|
+
(function ($) {
|
673
|
+
async function $mol_crypto_sacred_pass(pass, salt) {
|
674
|
+
const raw = await $mol_crypto_native.subtle.importKey("raw", $mol_charset_encode(pass), "PBKDF2", false, ["deriveKey"]);
|
675
|
+
const hard = await $mol_crypto_native.subtle.deriveKey({
|
676
|
+
name: "PBKDF2",
|
677
|
+
salt,
|
678
|
+
iterations: 10_000,
|
679
|
+
hash: "SHA-256",
|
680
|
+
}, raw, {
|
681
|
+
name: 'AES-CBC',
|
682
|
+
length: 128,
|
683
|
+
}, Boolean('extractable'), ['encrypt', 'decrypt']);
|
684
|
+
return $mol_crypto_sacred.from_native(hard);
|
685
|
+
}
|
686
|
+
$.$mol_crypto_sacred_pass = $mol_crypto_sacred_pass;
|
687
|
+
})($ || ($ = {}));
|
688
|
+
|
689
|
+
;
|
690
|
+
"use strict";
|
691
|
+
var $;
|
692
|
+
(function ($) {
|
693
|
+
const ecdsa = {
|
658
694
|
name: 'ECDSA',
|
659
695
|
hash: 'SHA-1',
|
660
696
|
namedCurve: "P-256",
|
661
697
|
};
|
698
|
+
const ecdh = { name: "ECDH", namedCurve: "P-256" };
|
699
|
+
const jwk = { crv: 'P-256', ext: true, kty: 'EC' };
|
662
700
|
class $mol_crypto_key extends $mol_buffer {
|
663
701
|
static from(serial) {
|
664
702
|
if (typeof serial === 'string') {
|
@@ -670,9 +708,6 @@ var $;
|
|
670
708
|
}
|
671
709
|
return super.from(serial);
|
672
710
|
}
|
673
|
-
asArray() {
|
674
|
-
return new Uint8Array(this.buffer, this.byteOffset, this.byteLength);
|
675
|
-
}
|
676
711
|
toString() {
|
677
712
|
const arr = this.asArray();
|
678
713
|
return $mol_base64_url_encode(arr.subarray(0, 32))
|
@@ -696,22 +731,34 @@ var $;
|
|
696
731
|
kty: "EC",
|
697
732
|
x: str.slice(0, 43),
|
698
733
|
y: str.slice(43, 86),
|
699
|
-
},
|
734
|
+
}, ecdsa, Boolean('extractable'), ['verify']);
|
735
|
+
}
|
736
|
+
async native_derive() {
|
737
|
+
const serial = this.toString();
|
738
|
+
return await $mol_crypto_native.subtle.importKey('jwk', {
|
739
|
+
...jwk,
|
740
|
+
key_ops: [],
|
741
|
+
x: serial.slice(0, 43),
|
742
|
+
y: serial.slice(43, 86),
|
743
|
+
}, ecdh, true, []);
|
700
744
|
}
|
701
745
|
async verify(data, sign) {
|
702
|
-
return await $mol_crypto_native.subtle.verify(
|
746
|
+
return await $mol_crypto_native.subtle.verify(ecdsa, await this.native(), sign, data);
|
703
747
|
}
|
704
748
|
}
|
705
749
|
__decorate([
|
706
750
|
$mol_memo.method
|
707
751
|
], $mol_crypto_key_public.prototype, "native", null);
|
752
|
+
__decorate([
|
753
|
+
$mol_memo.method
|
754
|
+
], $mol_crypto_key_public.prototype, "native_derive", null);
|
708
755
|
$.$mol_crypto_key_public = $mol_crypto_key_public;
|
709
756
|
class $mol_crypto_key_private extends $mol_crypto_key {
|
710
757
|
static size_str = 129;
|
711
758
|
static size_bin = 96;
|
712
759
|
static size_sign = 64;
|
713
760
|
static async generate() {
|
714
|
-
const pair = await $mol_crypto_native.subtle.generateKey(
|
761
|
+
const pair = await $mol_crypto_native.subtle.generateKey(ecdsa, Boolean('extractable'), ['sign', 'verify']);
|
715
762
|
const { x, y, d } = await $mol_crypto_native.subtle.exportKey('jwk', pair.privateKey);
|
716
763
|
return this.from(x + y + d);
|
717
764
|
}
|
@@ -725,24 +772,50 @@ var $;
|
|
725
772
|
x: str.slice(0, 43),
|
726
773
|
y: str.slice(43, 86),
|
727
774
|
d: str.slice(86, 129),
|
728
|
-
},
|
775
|
+
}, ecdsa, Boolean('extractable'), ['sign']);
|
776
|
+
}
|
777
|
+
async native_derive() {
|
778
|
+
const serial = this.toString();
|
779
|
+
return $mol_crypto_native.subtle.importKey('jwk', {
|
780
|
+
...jwk,
|
781
|
+
key_ops: ['deriveKey', 'deriveBits'],
|
782
|
+
x: serial.slice(0, 43),
|
783
|
+
y: serial.slice(43, 86),
|
784
|
+
d: serial.slice(86, 129),
|
785
|
+
}, ecdh, Boolean('extractable'), ['deriveKey', 'deriveBits']);
|
729
786
|
}
|
730
787
|
public() {
|
731
788
|
return new $mol_crypto_key_public(this.buffer, this.byteOffset, this.byteOffset + 64);
|
732
789
|
}
|
733
790
|
async sign(data) {
|
734
|
-
return new Uint8Array(await $mol_crypto_native.subtle.sign(
|
791
|
+
return new Uint8Array(await $mol_crypto_native.subtle.sign(ecdsa, await this.native(), data));
|
735
792
|
}
|
736
793
|
}
|
737
794
|
__decorate([
|
738
795
|
$mol_memo.method
|
739
796
|
], $mol_crypto_key_private.prototype, "native", null);
|
797
|
+
__decorate([
|
798
|
+
$mol_memo.method
|
799
|
+
], $mol_crypto_key_private.prototype, "native_derive", null);
|
740
800
|
__decorate([
|
741
801
|
$mol_memo.method
|
742
802
|
], $mol_crypto_key_private.prototype, "public", null);
|
743
803
|
$.$mol_crypto_key_private = $mol_crypto_key_private;
|
744
804
|
})($ || ($ = {}));
|
745
805
|
|
806
|
+
;
|
807
|
+
"use strict";
|
808
|
+
var $;
|
809
|
+
(function ($) {
|
810
|
+
async function $mol_crypto_sacred_shared(priv, pub) {
|
811
|
+
return $mol_crypto_sacred.from(new Uint8Array(await $mol_crypto_native.subtle.deriveBits({
|
812
|
+
name: "ECDH",
|
813
|
+
public: await pub.native_derive(),
|
814
|
+
}, await priv.native_derive(), $mol_crypto_sacred.size)));
|
815
|
+
}
|
816
|
+
$.$mol_crypto_sacred_shared = $mol_crypto_sacred_shared;
|
817
|
+
})($ || ($ = {}));
|
818
|
+
|
746
819
|
;
|
747
820
|
"use strict";
|
748
821
|
var $;
|
@@ -845,16 +918,5 @@ var $;
|
|
845
918
|
$.$mol_crypto_hash = $mol_crypto_hash;
|
846
919
|
})($ || ($ = {}));
|
847
920
|
|
848
|
-
;
|
849
|
-
"use strict";
|
850
|
-
var $;
|
851
|
-
(function ($) {
|
852
|
-
function $mol_crypto_salt() {
|
853
|
-
return $mol_crypto_native.getRandomValues(new Uint8Array(16));
|
854
|
-
}
|
855
|
-
$.$mol_crypto_salt = $mol_crypto_salt;
|
856
|
-
$.$mol_crypto_salt_once = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]);
|
857
|
-
})($ || ($ = {}));
|
858
|
-
|
859
921
|
|
860
922
|
//# sourceMappingURL=web.js.map
|