mol_crypto_lib 0.1.1081 → 0.1.1083

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.mjs CHANGED
@@ -1655,7 +1655,10 @@ var $node = new Proxy({ require }, {
1655
1655
  }
1656
1656
  catch (error) {
1657
1657
  if (error.code === 'ERR_REQUIRE_ESM') {
1658
- return importSync(name);
1658
+ const module = cache.get(name);
1659
+ if (module)
1660
+ return module;
1661
+ throw import(name).then(module => cache.set(name, module));
1659
1662
  }
1660
1663
  $.$mol_fail_log(error);
1661
1664
  return null;
@@ -1666,8 +1669,7 @@ var $node = new Proxy({ require }, {
1666
1669
  return true;
1667
1670
  },
1668
1671
  });
1669
- const importAsync = async (uri) => import(uri);
1670
- const importSync = $.$mol_wire_sync(importAsync);
1672
+ const cache = new Map();
1671
1673
  require = (req => Object.assign(function require(name) {
1672
1674
  return $node[name];
1673
1675
  }, req))(require);
@@ -1758,12 +1760,16 @@ var $;
1758
1760
  return new this(await $mol_crypto_native.subtle.generateKey(algorithm, true, ['encrypt', 'decrypt']));
1759
1761
  }
1760
1762
  static async from(serial) {
1761
- if (typeof serial === 'string') {
1762
- serial = $mol_charset_encode(serial);
1763
- serial = await $mol_crypto_native.subtle.digest('SHA-256', serial);
1764
- }
1765
1763
  return new this(await $mol_crypto_native.subtle.importKey('raw', serial, algorithm, true, ['encrypt', 'decrypt']));
1766
1764
  }
1765
+ static async pass(pass, salt) {
1766
+ return new this(await $mol_crypto_native.subtle.deriveKey({
1767
+ name: "PBKDF2",
1768
+ salt,
1769
+ iterations: 10_000,
1770
+ hash: "SHA-256",
1771
+ }, await $mol_crypto_native.subtle.importKey("raw", $mol_charset_encode(pass), "PBKDF2", false, ["deriveKey"]), algorithm, true, ['encrypt', 'decrypt']));
1772
+ }
1767
1773
  static async derive(private_serial, public_serial) {
1768
1774
  const ecdh = { name: "ECDH", namedCurve: "P-256" };
1769
1775
  const jwk = { crv: 'P-256', ext: true, kty: 'EC' };
@@ -2287,6 +2293,7 @@ var $;
2287
2293
  return $mol_crypto_native.getRandomValues(new Uint8Array(16));
2288
2294
  }
2289
2295
  $.$mol_crypto_salt = $mol_crypto_salt;
2296
+ $.$mol_crypto_salt_once = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]);
2290
2297
  })($ || ($ = {}));
2291
2298
 
2292
2299
 
package/node.test.js CHANGED
@@ -1646,7 +1646,10 @@ var $node = new Proxy({ require }, {
1646
1646
  }
1647
1647
  catch (error) {
1648
1648
  if (error.code === 'ERR_REQUIRE_ESM') {
1649
- return importSync(name);
1649
+ const module = cache.get(name);
1650
+ if (module)
1651
+ return module;
1652
+ throw import(name).then(module => cache.set(name, module));
1650
1653
  }
1651
1654
  $.$mol_fail_log(error);
1652
1655
  return null;
@@ -1657,8 +1660,7 @@ var $node = new Proxy({ require }, {
1657
1660
  return true;
1658
1661
  },
1659
1662
  });
1660
- const importAsync = async (uri) => import(uri);
1661
- const importSync = $.$mol_wire_sync(importAsync);
1663
+ const cache = new Map();
1662
1664
  require = (req => Object.assign(function require(name) {
1663
1665
  return $node[name];
1664
1666
  }, req))(require);
@@ -1749,12 +1751,16 @@ var $;
1749
1751
  return new this(await $mol_crypto_native.subtle.generateKey(algorithm, true, ['encrypt', 'decrypt']));
1750
1752
  }
1751
1753
  static async from(serial) {
1752
- if (typeof serial === 'string') {
1753
- serial = $mol_charset_encode(serial);
1754
- serial = await $mol_crypto_native.subtle.digest('SHA-256', serial);
1755
- }
1756
1754
  return new this(await $mol_crypto_native.subtle.importKey('raw', serial, algorithm, true, ['encrypt', 'decrypt']));
1757
1755
  }
1756
+ static async pass(pass, salt) {
1757
+ return new this(await $mol_crypto_native.subtle.deriveKey({
1758
+ name: "PBKDF2",
1759
+ salt,
1760
+ iterations: 10_000,
1761
+ hash: "SHA-256",
1762
+ }, await $mol_crypto_native.subtle.importKey("raw", $mol_charset_encode(pass), "PBKDF2", false, ["deriveKey"]), algorithm, true, ['encrypt', 'decrypt']));
1763
+ }
1758
1764
  static async derive(private_serial, public_serial) {
1759
1765
  const ecdh = { name: "ECDH", namedCurve: "P-256" };
1760
1766
  const jwk = { crv: 'P-256', ext: true, kty: 'EC' };
@@ -2278,6 +2284,7 @@ var $;
2278
2284
  return $mol_crypto_native.getRandomValues(new Uint8Array(16));
2279
2285
  }
2280
2286
  $.$mol_crypto_salt = $mol_crypto_salt;
2287
+ $.$mol_crypto_salt_once = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]);
2281
2288
  })($ || ($ = {}));
2282
2289
 
2283
2290
  ;
@@ -4035,21 +4042,21 @@ var $;
4035
4042
  (function ($) {
4036
4043
  $mol_test({
4037
4044
  async 'sizes'() {
4038
- const cipher = await $mol_crypto_secret.generate();
4039
- const key = await cipher.serial();
4045
+ const secret = await $mol_crypto_secret.generate();
4046
+ const key = await secret.serial();
4040
4047
  $mol_assert_equal(key.byteLength, $mol_crypto_secret.size);
4041
4048
  const data = new Uint8Array([1, 2, 3]);
4042
4049
  const salt = $mol_crypto_salt();
4043
- const closed = await cipher.encrypt(data, salt);
4050
+ const closed = await secret.encrypt(data, salt);
4044
4051
  $mol_assert_equal(closed.byteLength, 16);
4045
4052
  },
4046
4053
  async 'decrypt self encrypted with auto generated key'() {
4047
- const cipher = await $mol_crypto_secret.generate();
4054
+ const secret = await $mol_crypto_secret.generate();
4048
4055
  const data = new Uint8Array([1, 2, 3]);
4049
4056
  const salt = $mol_crypto_salt();
4050
- const closed = await cipher.encrypt(data, salt);
4051
- const opened = await cipher.decrypt(closed, salt);
4052
- $mol_assert_like(data, new Uint8Array(opened));
4057
+ const closed = await secret.encrypt(data, salt);
4058
+ const opened = await secret.decrypt(closed, salt);
4059
+ $mol_assert_equal(data, opened);
4053
4060
  },
4054
4061
  async 'decrypt encrypted with exported auto generated key'() {
4055
4062
  const data = new Uint8Array([1, 2, 3]);
@@ -4058,14 +4065,23 @@ var $;
4058
4065
  const closed = await Alice.encrypt(data, salt);
4059
4066
  const Bob = await $mol_crypto_secret.from(await Alice.serial());
4060
4067
  const opened = await Bob.decrypt(closed, salt);
4061
- $mol_assert_like(data, new Uint8Array(opened));
4068
+ $mol_assert_equal(data, opened);
4062
4069
  },
4063
4070
  async 'derivation from public & private keys'() {
4064
4071
  const A = await $mol_crypto_key_private.generate();
4065
4072
  const B = await $mol_crypto_key_private.generate();
4066
4073
  const AK = await $mol_crypto_secret.derive(A.toString(), B.public().toString());
4067
4074
  const BK = await $mol_crypto_secret.derive(B.toString(), A.public().toString());
4068
- $mol_assert_like(new Uint8Array(await AK.serial()), new Uint8Array(await BK.serial()));
4075
+ $mol_assert_equal(await AK.serial(), await BK.serial());
4076
+ },
4077
+ async 'derivation from passwod'() {
4078
+ const data = new Uint8Array([1, 2, 3]);
4079
+ const salt1 = $mol_crypto_salt();
4080
+ const secret = await $mol_crypto_secret.pass('hello', salt1);
4081
+ const salt2 = $mol_crypto_salt();
4082
+ const closed = await secret.encrypt(data, salt2);
4083
+ const opened = await secret.decrypt(closed, salt2);
4084
+ $mol_assert_equal(data, opened);
4069
4085
  },
4070
4086
  });
4071
4087
  })($ || ($ = {}));