mol_crypto2_lib 0.0.121 → 0.0.122
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 +49 -35
- package/node.d.ts +11 -22
- package/node.d.ts.map +1 -1
- package/node.deps.json +1 -1
- package/node.js +31 -74
- package/node.js.map +1 -1
- package/node.meta.tree +1 -0
- package/node.mjs +31 -74
- package/node.test.js +50 -92
- package/node.test.js.map +1 -1
- package/package.json +2 -3
- package/web.d.ts +11 -22
- package/web.d.ts.map +1 -1
- package/web.deps.json +1 -1
- package/web.js +31 -74
- package/web.js.map +1 -1
- package/web.meta.tree +1 -0
- package/web.mjs +31 -74
- package/web.test.js +19 -18
- package/web.test.js.map +1 -1
package/node.meta.tree
CHANGED
package/node.mjs
CHANGED
|
@@ -2823,99 +2823,56 @@ var $;
|
|
|
2823
2823
|
"use strict";
|
|
2824
2824
|
var $;
|
|
2825
2825
|
(function ($) {
|
|
2826
|
-
/**
|
|
2827
|
-
|
|
2828
|
-
})($ || ($ = {}));
|
|
2829
|
-
|
|
2830
|
-
;
|
|
2831
|
-
"use strict";
|
|
2832
|
-
var $;
|
|
2833
|
-
(function ($) {
|
|
2834
|
-
/** Symmetric cipher with shortest payload. */
|
|
2835
|
-
class $mol_crypto_sacred extends $mol_buffer {
|
|
2836
|
-
/** Key size in bytes. */
|
|
2826
|
+
/** Symmetric AES-CTR secret. */
|
|
2827
|
+
class $mol_crypto2_secret extends $mol_buffer {
|
|
2837
2828
|
static size = 16;
|
|
2838
2829
|
/** Makes new random secret. */
|
|
2839
2830
|
static make() {
|
|
2840
|
-
return this.from($
|
|
2831
|
+
return this.from($mol_crypto2_nonce());
|
|
2841
2832
|
}
|
|
2842
|
-
/** Makes from string
|
|
2833
|
+
/** Makes from string or buffer view. */
|
|
2843
2834
|
static from(serial) {
|
|
2844
|
-
if (typeof serial === 'string')
|
|
2845
|
-
serial =
|
|
2846
|
-
|
|
2847
|
-
|
|
2835
|
+
if (typeof serial === 'string')
|
|
2836
|
+
serial = $mol_base64_url_decode(serial);
|
|
2837
|
+
if (serial.byteLength !== $mol_crypto2_secret.size) {
|
|
2838
|
+
return $mol_fail(new RangeError('Wrong key size', { cause: { size: serial.byteLength } }));
|
|
2848
2839
|
}
|
|
2849
2840
|
if (!(serial instanceof Uint8Array)) {
|
|
2850
2841
|
serial = new Uint8Array(serial.buffer, serial.byteOffset, serial.byteLength);
|
|
2851
2842
|
}
|
|
2852
|
-
;
|
|
2853
|
-
serial[0] = 0xFF;
|
|
2854
|
-
const sacred = super.from(serial);
|
|
2855
|
-
return sacred;
|
|
2843
|
+
return super.from(serial);
|
|
2856
2844
|
}
|
|
2857
2845
|
static async from_native(native) {
|
|
2858
|
-
const
|
|
2859
|
-
const
|
|
2860
|
-
|
|
2861
|
-
return
|
|
2862
|
-
}
|
|
2863
|
-
constructor(buffer, byteOffset, byteLength) {
|
|
2864
|
-
super(buffer, byteOffset, byteLength);
|
|
2865
|
-
if (this.getUint8(0) !== 0xFF)
|
|
2866
|
-
$mol_fail(new Error('Buffer should starts with 0xFF byte'));
|
|
2846
|
+
const buffer = await $mol_crypto_native.subtle.exportKey('raw', native).catch($mol_crypto_restack);
|
|
2847
|
+
const secret = this.from(new Uint8Array(buffer));
|
|
2848
|
+
secret._native = native;
|
|
2849
|
+
return secret;
|
|
2867
2850
|
}
|
|
2868
2851
|
toString() {
|
|
2869
2852
|
return $mol_base64_url_encode(this.asArray());
|
|
2870
2853
|
}
|
|
2871
2854
|
_native;
|
|
2872
|
-
/** Native crypto secret */
|
|
2855
|
+
/** Native crypto secret. */
|
|
2873
2856
|
async native() {
|
|
2874
|
-
return this._native ?? (this._native = await $mol_crypto_native.subtle.importKey('raw',
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
|
|
2879
|
-
|
|
2880
|
-
|
|
2881
|
-
|
|
2882
|
-
|
|
2883
|
-
|
|
2884
|
-
|
|
2885
|
-
|
|
2886
|
-
}, await this.native(),
|
|
2887
|
-
}
|
|
2888
|
-
/** Decrypt any binary message. */
|
|
2889
|
-
async decrypt(closed, salt) {
|
|
2890
|
-
return new Uint8Array(await $mol_crypto_native.subtle.decrypt({
|
|
2891
|
-
name: 'AES-CBC',
|
|
2892
|
-
length: 128,
|
|
2893
|
-
tagLength: 32,
|
|
2894
|
-
iv: salt,
|
|
2895
|
-
}, await this.native(), closed).catch($mol_crypto_restack));
|
|
2896
|
-
}
|
|
2897
|
-
/** Encrypts 0xFF prefixed buffer. 16 bytes */
|
|
2898
|
-
async close(opened, salt) {
|
|
2899
|
-
if (opened.getUint8(0) !== 0xFF)
|
|
2900
|
-
throw new Error('Closable buffer should starts with 0xFF');
|
|
2901
|
-
const trimed = new Uint8Array(opened.buffer, opened.byteOffset + 1, opened.byteLength - 1);
|
|
2902
|
-
return this.encrypt(trimed, salt);
|
|
2903
|
-
}
|
|
2904
|
-
/** Decrypts 0xFF prefixed buffer. 16 bytes */
|
|
2905
|
-
async open(closed, salt) {
|
|
2906
|
-
const trimed = await this.decrypt(closed, salt);
|
|
2907
|
-
if (trimed.byteLength !== closed.byteLength - 1)
|
|
2908
|
-
throw new Error('Length of opened buffer should be ' + (closed.byteLength - 1));
|
|
2909
|
-
const opened = new Uint8Array(closed.byteLength);
|
|
2910
|
-
opened[0] = 0xFF;
|
|
2911
|
-
opened.set(trimed, 1);
|
|
2912
|
-
return opened;
|
|
2857
|
+
return this._native ?? (this._native = await $mol_crypto_native.subtle.importKey('raw', // format
|
|
2858
|
+
this, // buffer source
|
|
2859
|
+
'AES-CTR', // algorithm
|
|
2860
|
+
true, // extractable
|
|
2861
|
+
['encrypt', 'decrypt']).catch($mol_crypto_restack));
|
|
2862
|
+
}
|
|
2863
|
+
/** Encrypts data without changing its length. */
|
|
2864
|
+
async encrypt(open, nonce) {
|
|
2865
|
+
return new Uint8Array(await $mol_crypto_native.subtle.encrypt({ name: 'AES-CTR', counter: nonce, length: 64 }, await this.native(), open).catch($mol_crypto_restack));
|
|
2866
|
+
}
|
|
2867
|
+
/** Decrypts data without changing its length. */
|
|
2868
|
+
async decrypt(closed, nonce) {
|
|
2869
|
+
return new Uint8Array(await $mol_crypto_native.subtle.decrypt({ name: 'AES-CTR', counter: nonce, length: 64 }, await this.native(), closed).catch($mol_crypto_restack));
|
|
2913
2870
|
}
|
|
2914
2871
|
}
|
|
2915
2872
|
__decorate([
|
|
2916
2873
|
$mol_memo.method
|
|
2917
|
-
], $
|
|
2918
|
-
$.$
|
|
2874
|
+
], $mol_crypto2_secret.prototype, "toString", null);
|
|
2875
|
+
$.$mol_crypto2_secret = $mol_crypto2_secret;
|
|
2919
2876
|
})($ || ($ = {}));
|
|
2920
2877
|
|
|
2921
2878
|
;
|
|
@@ -2956,10 +2913,10 @@ var $;
|
|
|
2956
2913
|
}
|
|
2957
2914
|
/** Makes shared secret for combination of Chiper and Socket. */
|
|
2958
2915
|
async secret(pub) {
|
|
2959
|
-
return $
|
|
2916
|
+
return $mol_crypto2_secret.from(new Uint8Array(await $mol_crypto_native.subtle.deriveBits({
|
|
2960
2917
|
name: "X25519",
|
|
2961
2918
|
public: await pub.native(),
|
|
2962
|
-
}, await this.nativePrivate(), $
|
|
2919
|
+
}, await this.nativePrivate(), $mol_crypto2_secret.size * 8).catch($mol_crypto_restack)));
|
|
2963
2920
|
}
|
|
2964
2921
|
}
|
|
2965
2922
|
__decorate([
|
package/node.test.js
CHANGED
|
@@ -2814,99 +2814,56 @@ var $;
|
|
|
2814
2814
|
"use strict";
|
|
2815
2815
|
var $;
|
|
2816
2816
|
(function ($) {
|
|
2817
|
-
/**
|
|
2818
|
-
|
|
2819
|
-
})($ || ($ = {}));
|
|
2820
|
-
|
|
2821
|
-
;
|
|
2822
|
-
"use strict";
|
|
2823
|
-
var $;
|
|
2824
|
-
(function ($) {
|
|
2825
|
-
/** Symmetric cipher with shortest payload. */
|
|
2826
|
-
class $mol_crypto_sacred extends $mol_buffer {
|
|
2827
|
-
/** Key size in bytes. */
|
|
2817
|
+
/** Symmetric AES-CTR secret. */
|
|
2818
|
+
class $mol_crypto2_secret extends $mol_buffer {
|
|
2828
2819
|
static size = 16;
|
|
2829
2820
|
/** Makes new random secret. */
|
|
2830
2821
|
static make() {
|
|
2831
|
-
return this.from($
|
|
2822
|
+
return this.from($mol_crypto2_nonce());
|
|
2832
2823
|
}
|
|
2833
|
-
/** Makes from string
|
|
2824
|
+
/** Makes from string or buffer view. */
|
|
2834
2825
|
static from(serial) {
|
|
2835
|
-
if (typeof serial === 'string')
|
|
2836
|
-
serial =
|
|
2837
|
-
|
|
2838
|
-
|
|
2826
|
+
if (typeof serial === 'string')
|
|
2827
|
+
serial = $mol_base64_url_decode(serial);
|
|
2828
|
+
if (serial.byteLength !== $mol_crypto2_secret.size) {
|
|
2829
|
+
return $mol_fail(new RangeError('Wrong key size', { cause: { size: serial.byteLength } }));
|
|
2839
2830
|
}
|
|
2840
2831
|
if (!(serial instanceof Uint8Array)) {
|
|
2841
2832
|
serial = new Uint8Array(serial.buffer, serial.byteOffset, serial.byteLength);
|
|
2842
2833
|
}
|
|
2843
|
-
;
|
|
2844
|
-
serial[0] = 0xFF;
|
|
2845
|
-
const sacred = super.from(serial);
|
|
2846
|
-
return sacred;
|
|
2834
|
+
return super.from(serial);
|
|
2847
2835
|
}
|
|
2848
2836
|
static async from_native(native) {
|
|
2849
|
-
const
|
|
2850
|
-
const
|
|
2851
|
-
|
|
2852
|
-
return
|
|
2853
|
-
}
|
|
2854
|
-
constructor(buffer, byteOffset, byteLength) {
|
|
2855
|
-
super(buffer, byteOffset, byteLength);
|
|
2856
|
-
if (this.getUint8(0) !== 0xFF)
|
|
2857
|
-
$mol_fail(new Error('Buffer should starts with 0xFF byte'));
|
|
2837
|
+
const buffer = await $mol_crypto_native.subtle.exportKey('raw', native).catch($mol_crypto_restack);
|
|
2838
|
+
const secret = this.from(new Uint8Array(buffer));
|
|
2839
|
+
secret._native = native;
|
|
2840
|
+
return secret;
|
|
2858
2841
|
}
|
|
2859
2842
|
toString() {
|
|
2860
2843
|
return $mol_base64_url_encode(this.asArray());
|
|
2861
2844
|
}
|
|
2862
2845
|
_native;
|
|
2863
|
-
/** Native crypto secret */
|
|
2846
|
+
/** Native crypto secret. */
|
|
2864
2847
|
async native() {
|
|
2865
|
-
return this._native ?? (this._native = await $mol_crypto_native.subtle.importKey('raw',
|
|
2866
|
-
|
|
2867
|
-
|
|
2868
|
-
|
|
2869
|
-
|
|
2870
|
-
|
|
2871
|
-
|
|
2872
|
-
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
}, await this.native(),
|
|
2878
|
-
}
|
|
2879
|
-
/** Decrypt any binary message. */
|
|
2880
|
-
async decrypt(closed, salt) {
|
|
2881
|
-
return new Uint8Array(await $mol_crypto_native.subtle.decrypt({
|
|
2882
|
-
name: 'AES-CBC',
|
|
2883
|
-
length: 128,
|
|
2884
|
-
tagLength: 32,
|
|
2885
|
-
iv: salt,
|
|
2886
|
-
}, await this.native(), closed).catch($mol_crypto_restack));
|
|
2887
|
-
}
|
|
2888
|
-
/** Encrypts 0xFF prefixed buffer. 16 bytes */
|
|
2889
|
-
async close(opened, salt) {
|
|
2890
|
-
if (opened.getUint8(0) !== 0xFF)
|
|
2891
|
-
throw new Error('Closable buffer should starts with 0xFF');
|
|
2892
|
-
const trimed = new Uint8Array(opened.buffer, opened.byteOffset + 1, opened.byteLength - 1);
|
|
2893
|
-
return this.encrypt(trimed, salt);
|
|
2894
|
-
}
|
|
2895
|
-
/** Decrypts 0xFF prefixed buffer. 16 bytes */
|
|
2896
|
-
async open(closed, salt) {
|
|
2897
|
-
const trimed = await this.decrypt(closed, salt);
|
|
2898
|
-
if (trimed.byteLength !== closed.byteLength - 1)
|
|
2899
|
-
throw new Error('Length of opened buffer should be ' + (closed.byteLength - 1));
|
|
2900
|
-
const opened = new Uint8Array(closed.byteLength);
|
|
2901
|
-
opened[0] = 0xFF;
|
|
2902
|
-
opened.set(trimed, 1);
|
|
2903
|
-
return opened;
|
|
2848
|
+
return this._native ?? (this._native = await $mol_crypto_native.subtle.importKey('raw', // format
|
|
2849
|
+
this, // buffer source
|
|
2850
|
+
'AES-CTR', // algorithm
|
|
2851
|
+
true, // extractable
|
|
2852
|
+
['encrypt', 'decrypt']).catch($mol_crypto_restack));
|
|
2853
|
+
}
|
|
2854
|
+
/** Encrypts data without changing its length. */
|
|
2855
|
+
async encrypt(open, nonce) {
|
|
2856
|
+
return new Uint8Array(await $mol_crypto_native.subtle.encrypt({ name: 'AES-CTR', counter: nonce, length: 64 }, await this.native(), open).catch($mol_crypto_restack));
|
|
2857
|
+
}
|
|
2858
|
+
/** Decrypts data without changing its length. */
|
|
2859
|
+
async decrypt(closed, nonce) {
|
|
2860
|
+
return new Uint8Array(await $mol_crypto_native.subtle.decrypt({ name: 'AES-CTR', counter: nonce, length: 64 }, await this.native(), closed).catch($mol_crypto_restack));
|
|
2904
2861
|
}
|
|
2905
2862
|
}
|
|
2906
2863
|
__decorate([
|
|
2907
2864
|
$mol_memo.method
|
|
2908
|
-
], $
|
|
2909
|
-
$.$
|
|
2865
|
+
], $mol_crypto2_secret.prototype, "toString", null);
|
|
2866
|
+
$.$mol_crypto2_secret = $mol_crypto2_secret;
|
|
2910
2867
|
})($ || ($ = {}));
|
|
2911
2868
|
|
|
2912
2869
|
;
|
|
@@ -2947,10 +2904,10 @@ var $;
|
|
|
2947
2904
|
}
|
|
2948
2905
|
/** Makes shared secret for combination of Chiper and Socket. */
|
|
2949
2906
|
async secret(pub) {
|
|
2950
|
-
return $
|
|
2907
|
+
return $mol_crypto2_secret.from(new Uint8Array(await $mol_crypto_native.subtle.deriveBits({
|
|
2951
2908
|
name: "X25519",
|
|
2952
2909
|
public: await pub.native(),
|
|
2953
|
-
}, await this.nativePrivate(), $
|
|
2910
|
+
}, await this.nativePrivate(), $mol_crypto2_secret.size * 8).catch($mol_crypto_restack)));
|
|
2954
2911
|
}
|
|
2955
2912
|
}
|
|
2956
2913
|
__decorate([
|
|
@@ -5409,31 +5366,32 @@ var $;
|
|
|
5409
5366
|
(function ($) {
|
|
5410
5367
|
$mol_test({
|
|
5411
5368
|
async 'Sizes'() {
|
|
5412
|
-
const secret = $
|
|
5413
|
-
|
|
5414
|
-
$mol_assert_equal(key.byteLength, $mol_crypto_sacred.size);
|
|
5369
|
+
const secret = $mol_crypto2_secret.make();
|
|
5370
|
+
$mol_assert_equal(secret.asArray().byteLength, $mol_crypto2_secret.size);
|
|
5415
5371
|
const data = new Uint8Array([1, 2, 3]);
|
|
5416
|
-
const
|
|
5417
|
-
const closed = await secret.encrypt(data,
|
|
5418
|
-
$mol_assert_equal(closed.byteLength,
|
|
5419
|
-
|
|
5420
|
-
|
|
5372
|
+
const nonce = $mol_crypto2_nonce();
|
|
5373
|
+
const closed = await secret.encrypt(data, nonce);
|
|
5374
|
+
$mol_assert_equal(closed.byteLength, data.byteLength);
|
|
5375
|
+
},
|
|
5376
|
+
'Wrong key size'() {
|
|
5377
|
+
$mol_assert_fail(() => $mol_crypto2_secret.from(new Uint8Array(8)), 'Wrong key size');
|
|
5421
5378
|
},
|
|
5422
5379
|
async 'Decrypt self encrypted'() {
|
|
5423
|
-
const secret = $
|
|
5380
|
+
const secret = $mol_crypto2_secret.make();
|
|
5381
|
+
const nonce = $mol_crypto2_nonce();
|
|
5424
5382
|
const data = new Uint8Array([1, 2, 3]);
|
|
5425
|
-
const
|
|
5426
|
-
|
|
5427
|
-
const opened = await secret.decrypt(closed,
|
|
5428
|
-
$mol_assert_equal(
|
|
5383
|
+
const closed = await secret.encrypt(data, nonce);
|
|
5384
|
+
$mol_assert_equal(closed.byteLength, 3);
|
|
5385
|
+
const opened = await secret.decrypt(closed, nonce);
|
|
5386
|
+
$mol_assert_equal(opened, data);
|
|
5429
5387
|
},
|
|
5430
5388
|
async 'Decrypt encrypted with exported key'() {
|
|
5431
5389
|
const data = new Uint8Array([1, 2, 3]);
|
|
5432
|
-
const
|
|
5433
|
-
const Alice = $
|
|
5434
|
-
const closed = await Alice.encrypt(data,
|
|
5435
|
-
const Bob = $
|
|
5436
|
-
const opened = await Bob.decrypt(closed,
|
|
5390
|
+
const nonce = $mol_crypto2_nonce();
|
|
5391
|
+
const Alice = $mol_crypto2_secret.make();
|
|
5392
|
+
const closed = await Alice.encrypt(data, nonce);
|
|
5393
|
+
const Bob = $mol_crypto2_secret.from(Alice.asArray());
|
|
5394
|
+
const opened = await Bob.decrypt(closed, nonce);
|
|
5437
5395
|
$mol_assert_equal(data, opened);
|
|
5438
5396
|
},
|
|
5439
5397
|
});
|