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/web.mjs CHANGED
@@ -32,97 +32,10 @@ $.$$ = $
32
32
  "use strict";
33
33
  var $;
34
34
  (function ($) {
35
- $.$mol_crypto_native = crypto;
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
- $.$mol_crypto_secret = $mol_crypto_secret;
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
- const algorithm = {
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
- }, algorithm, true, ['verify']);
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(algorithm, await this.native(), sign, data);
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(algorithm, true, ['sign', 'verify']);
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
- }, algorithm, true, ['sign']);
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(algorithm, await this.native(), data));
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,17 +918,6 @@ 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
  export default $
861
923
  //# sourceMappingURL=web.js.map
package/web.test.js CHANGED
@@ -1199,19 +1199,6 @@ var $;
1199
1199
  });
1200
1200
  })($ || ($ = {}));
1201
1201
 
1202
- ;
1203
- "use strict";
1204
- var $;
1205
- (function ($) {
1206
- $mol_test({
1207
- 'encode utf8 string'() {
1208
- const str = 'Hello, ΧΨΩЫ';
1209
- const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
1210
- $mol_assert_like($mol_charset_encode(str), encoded);
1211
- },
1212
- });
1213
- })($ || ($ = {}));
1214
-
1215
1202
  ;
1216
1203
  "use strict";
1217
1204
  var $;
@@ -1451,6 +1438,73 @@ var $;
1451
1438
  });
1452
1439
  })($ || ($ = {}));
1453
1440
 
1441
+ ;
1442
+ "use strict";
1443
+ var $;
1444
+ (function ($) {
1445
+ $mol_test({
1446
+ async 'Sizes'() {
1447
+ const secret = $mol_crypto_sacred.make();
1448
+ const key = secret.asArray();
1449
+ $mol_assert_equal(key.byteLength, $mol_crypto_sacred.size);
1450
+ const data = new Uint8Array([1, 2, 3]);
1451
+ const salt = $mol_crypto_salt();
1452
+ const closed = await secret.encrypt(data, salt);
1453
+ $mol_assert_equal(closed.byteLength, $mol_crypto_sacred.size);
1454
+ const self_closed = await secret.close(secret, salt);
1455
+ $mol_assert_equal(self_closed.byteLength, $mol_crypto_sacred.size);
1456
+ },
1457
+ async 'Decrypt self encrypted'() {
1458
+ const secret = $mol_crypto_sacred.make();
1459
+ const data = new Uint8Array([1, 2, 3]);
1460
+ const salt = $mol_crypto_salt();
1461
+ const closed = await secret.encrypt(data, salt);
1462
+ const opened = await secret.decrypt(closed, salt);
1463
+ $mol_assert_equal(data, opened);
1464
+ },
1465
+ async 'Decrypt encrypted with exported key'() {
1466
+ const data = new Uint8Array([1, 2, 3]);
1467
+ const salt = $mol_crypto_salt();
1468
+ const Alice = $mol_crypto_sacred.make();
1469
+ const closed = await Alice.encrypt(data, salt);
1470
+ const Bob = $mol_crypto_sacred.from(Alice.asArray());
1471
+ const opened = await Bob.decrypt(closed, salt);
1472
+ $mol_assert_equal(data, opened);
1473
+ },
1474
+ });
1475
+ })($ || ($ = {}));
1476
+
1477
+ ;
1478
+ "use strict";
1479
+ var $;
1480
+ (function ($) {
1481
+ $mol_test({
1482
+ 'encode utf8 string'() {
1483
+ const str = 'Hello, ΧΨΩЫ';
1484
+ const encoded = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 206, 167, 206, 168, 206, 169, 208, 171]);
1485
+ $mol_assert_like($mol_charset_encode(str), encoded);
1486
+ },
1487
+ });
1488
+ })($ || ($ = {}));
1489
+
1490
+ ;
1491
+ "use strict";
1492
+ var $;
1493
+ (function ($) {
1494
+ $mol_test({
1495
+ async 'Derivation from password'() {
1496
+ const data = new Uint8Array([1, 2, 3]);
1497
+ const salt1 = $mol_crypto_salt();
1498
+ const secret1 = await $mol_crypto_sacred_pass('hello', salt1);
1499
+ const secret2 = await $mol_crypto_sacred_pass('hello', salt1);
1500
+ const salt2 = $mol_crypto_salt();
1501
+ const closed = await secret1.encrypt(data, salt2);
1502
+ const opened = await secret2.decrypt(closed, salt2);
1503
+ $mol_assert_equal(data, opened);
1504
+ },
1505
+ });
1506
+ })($ || ($ = {}));
1507
+
1454
1508
  ;
1455
1509
  "use strict";
1456
1510
  var $;
@@ -1501,47 +1555,12 @@ var $;
1501
1555
  var $;
1502
1556
  (function ($) {
1503
1557
  $mol_test({
1504
- async 'sizes'() {
1505
- const secret = await $mol_crypto_secret.generate();
1506
- const key = await secret.serial();
1507
- $mol_assert_equal(key.byteLength, $mol_crypto_secret.size);
1508
- const data = new Uint8Array([1, 2, 3]);
1509
- const salt = $mol_crypto_salt();
1510
- const closed = await secret.encrypt(data, salt);
1511
- $mol_assert_equal(closed.byteLength, 16);
1512
- },
1513
- async 'decrypt self encrypted with auto generated key'() {
1514
- const secret = await $mol_crypto_secret.generate();
1515
- const data = new Uint8Array([1, 2, 3]);
1516
- const salt = $mol_crypto_salt();
1517
- const closed = await secret.encrypt(data, salt);
1518
- const opened = await secret.decrypt(closed, salt);
1519
- $mol_assert_equal(data, opened);
1520
- },
1521
- async 'decrypt encrypted with exported auto generated key'() {
1522
- const data = new Uint8Array([1, 2, 3]);
1523
- const salt = $mol_crypto_salt();
1524
- const Alice = await $mol_crypto_secret.generate();
1525
- const closed = await Alice.encrypt(data, salt);
1526
- const Bob = await $mol_crypto_secret.from(await Alice.serial());
1527
- const opened = await Bob.decrypt(closed, salt);
1528
- $mol_assert_equal(data, opened);
1529
- },
1530
- async 'derivation from public & private keys'() {
1558
+ async 'Shared secret from public & private keys'() {
1531
1559
  const A = await $mol_crypto_key_private.generate();
1532
1560
  const B = await $mol_crypto_key_private.generate();
1533
- const AK = await $mol_crypto_secret.derive(A.toString(), B.public().toString());
1534
- const BK = await $mol_crypto_secret.derive(B.toString(), A.public().toString());
1535
- $mol_assert_equal(await AK.serial(), await BK.serial());
1536
- },
1537
- async 'derivation from passwod'() {
1538
- const data = new Uint8Array([1, 2, 3]);
1539
- const salt1 = $mol_crypto_salt();
1540
- const secret = await $mol_crypto_secret.pass('hello', salt1);
1541
- const salt2 = $mol_crypto_salt();
1542
- const closed = await secret.encrypt(data, salt2);
1543
- const opened = await secret.decrypt(closed, salt2);
1544
- $mol_assert_equal(data, opened);
1561
+ const SA = await $mol_crypto_sacred_shared(A, B.public());
1562
+ const SB = await $mol_crypto_sacred_shared(B, A.public());
1563
+ $mol_assert_equal(SA.asArray(), SB.asArray());
1545
1564
  },
1546
1565
  });
1547
1566
  })($ || ($ = {}));