@cloudpss/crypto 0.5.37 → 0.5.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.
@@ -1,5 +1,5 @@
1
1
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
2
- const module = () => {
2
+ const module = async () => {
3
3
  if (typeof crypto == 'object' &&
4
4
  typeof crypto.subtle == 'object' &&
5
5
  typeof crypto.subtle.importKey == 'function' &&
@@ -1 +1 @@
1
- {"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/encryption/browser.ts"],"names":[],"mappings":"AAEA,4EAA4E;AAC5E,MAAM,MAAM,GAAG,GAAG,EAAE;IAChB,IACI,OAAO,MAAM,IAAI,QAAQ;QACzB,OAAO,MAAM,CAAC,MAAM,IAAI,QAAQ;QAChC,OAAO,MAAM,CAAC,MAAM,CAAC,SAAS,IAAI,UAAU;QAC5C,OAAO,MAAM,CAAC,MAAM,CAAC,SAAS,IAAI,UAAU;QAC5C,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,UAAU;QAC1C,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,UAAU,EAC5C,CAAC;QACC,OAAO,MAAM,CAAC,UAAU,CAAC,CAAC;IAC9B,CAAC;SAAM,CAAC;QACJ,OAAO,MAAM,CAAC,WAAW,CAAC,CAAC;IAC/B,CAAC;AACL,CAAC,CAAC;AAEF,sBAAsB;AACtB,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,IAAe,EAAE,UAAkB;IAC7D,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,MAAM,EAAE,CAAC;IACnC,OAAO,MAAM,OAAO,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC;AAED,sBAAsB;AACtB,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,IAAmB,EAAE,UAAkB;IACjE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,MAAM,EAAE,CAAC;IACnC,OAAO,MAAM,OAAO,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC"}
1
+ {"version":3,"file":"browser.js","sourceRoot":"","sources":["../../src/encryption/browser.ts"],"names":[],"mappings":"AAEA,4EAA4E;AAC5E,MAAM,MAAM,GAAG,KAAK,IAAI,EAAE;IACtB,IACI,OAAO,MAAM,IAAI,QAAQ;QACzB,OAAO,MAAM,CAAC,MAAM,IAAI,QAAQ;QAChC,OAAO,MAAM,CAAC,MAAM,CAAC,SAAS,IAAI,UAAU;QAC5C,OAAO,MAAM,CAAC,MAAM,CAAC,SAAS,IAAI,UAAU;QAC5C,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,UAAU;QAC1C,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,IAAI,UAAU,EAC5C,CAAC;QACC,OAAO,MAAM,CAAC,UAAU,CAAC,CAAC;IAC9B,CAAC;SAAM,CAAC;QACJ,OAAO,MAAM,CAAC,WAAW,CAAC,CAAC;IAC/B,CAAC;AACL,CAAC,CAAC;AAEF,sBAAsB;AACtB,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,IAAe,EAAE,UAAkB;IAC7D,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,MAAM,EAAE,CAAC;IACnC,OAAO,MAAM,OAAO,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC;AAED,sBAAsB;AACtB,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,IAAmB,EAAE,UAAkB;IACjE,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,MAAM,EAAE,CAAC;IACnC,OAAO,MAAM,OAAO,CAAC,IAAI,EAAE,UAAU,CAAC,CAAC;AAC3C,CAAC"}
@@ -3,7 +3,7 @@ import { PBKDF2_ITERATIONS, NONCE_SIZE, AES_TAG_SIZE, AES_KEY_SIZE, } from './co
3
3
  import { promisify } from 'node:util';
4
4
  import { toUint8Array } from '../utils.js';
5
5
  const pbkdf2 = promisify(_pbkdf2);
6
- const aesKdf = (passphrase, salt) => {
6
+ const aesKdf = async (passphrase, salt) => {
7
7
  return pbkdf2(passphrase, salt, PBKDF2_ITERATIONS, AES_KEY_SIZE, 'sha256');
8
8
  };
9
9
  /** nodejs encrypt */
@@ -1 +1 @@
1
- {"version":3,"file":"node.js","sourceRoot":"","sources":["../../src/encryption/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/F,OAAO,EACH,iBAAiB,EACjB,UAAU,EACV,YAAY,EACZ,YAAY,GAGf,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,MAAM,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;AAClC,MAAM,MAAM,GAAG,CAAC,UAAkB,EAAE,IAAgB,EAAmB,EAAE;IACrE,OAAO,MAAM,CAAC,UAAU,EAAE,IAAI,EAAE,iBAAiB,EAAE,YAAY,EAAE,QAAQ,CAAC,CAAC;AAC/E,CAAC,CAAC;AAEF,qBAAqB;AACrB,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,EAAE,IAAI,EAAE,GAAG,EAAa,EAAE,UAAkB;IACtE,MAAM,KAAK,GAAG,WAAW,CAAC,UAAU,CAAC,CAAC;IACtC,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAC5C,MAAM,MAAM,GAAG,cAAc,CAAC,aAAa,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,aAAa,EAAE,YAAY,EAAE,CAAC,CAAC;IAC1F,IAAI,GAAG,CAAC,UAAU;QAAE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACvC,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,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AG,MAAM,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;IAC5C,MAAM,QAAQ,GAAG,gBAAgB,CAAC,aAAa,EAAE,GAAG,EAAE,KAAK,EAAE,EAAE,aAAa,EAAE,YAAY,EAAE,CAAC,CAAC;IAC9F,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC;IAC/D,IAAI,GAAG,CAAC,UAAU;QAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACzC,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,IAAI,CAAC,MAAM,GAAG,YAAY,CAAC,CAAC,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IACnH,OAAO,EAAE,IAAI,EAAE,YAAY,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,CAAC;AAClD,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudpss/crypto",
3
- "version": "0.5.37",
3
+ "version": "0.5.39",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -17,7 +17,7 @@
17
17
  "#lib-wasm": "./lib/wasm.js"
18
18
  },
19
19
  "devDependencies": {
20
- "esbuild": "^0.23.0",
20
+ "esbuild": "^0.23.1",
21
21
  "esbuild-plugin-wasm": "^1.1.0"
22
22
  },
23
23
  "scripts": {
@@ -1,7 +1,7 @@
1
1
  import type { EncryptedData, PlainData } from './common.js';
2
2
 
3
3
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
4
- const module = () => {
4
+ const module = async () => {
5
5
  if (
6
6
  typeof crypto == 'object' &&
7
7
  typeof crypto.subtle == 'object' &&
@@ -11,7 +11,7 @@ import { promisify } from 'node:util';
11
11
  import { toUint8Array } from '../utils.js';
12
12
 
13
13
  const pbkdf2 = promisify(_pbkdf2);
14
- const aesKdf = (passphrase: string, salt: Uint8Array): Promise<Buffer> => {
14
+ const aesKdf = async (passphrase: string, salt: Uint8Array): Promise<Buffer> => {
15
15
  return pbkdf2(passphrase, salt, PBKDF2_ITERATIONS, AES_KEY_SIZE, 'sha256');
16
16
  };
17
17
 
@@ -48,20 +48,20 @@ describe('Encryption root export', () => {
48
48
 
49
49
  it('encrypt check', async () => {
50
50
  // @ts-expect-error bad type
51
- await expect(() => encrypt({}, 'xx')).rejects.toThrow('Invalid data');
51
+ await expect(async () => encrypt({}, 'xx')).rejects.toThrow('Invalid data');
52
52
  // @ts-expect-error bad type
53
53
  // eslint-disable-next-line unicorn/new-for-builtins
54
- await expect(() => encrypt(Buffer.alloc(0), new String(1))).rejects.toThrow('Invalid passphrase');
55
- await expect(() => encrypt(Buffer.alloc(0), '')).rejects.toThrow('Invalid passphrase');
54
+ await expect(async () => encrypt(Buffer.alloc(0), new String(1))).rejects.toThrow('Invalid passphrase');
55
+ await expect(async () => encrypt(Buffer.alloc(0), '')).rejects.toThrow('Invalid passphrase');
56
56
  });
57
57
  it('decrypt check', async () => {
58
58
  // @ts-expect-error bad type
59
- await expect(() => decrypt({}, 'xx')).rejects.toThrow('Invalid data');
59
+ await expect(async () => decrypt({}, 'xx')).rejects.toThrow('Invalid data');
60
60
  // @ts-expect-error bad type
61
61
  // eslint-disable-next-line unicorn/new-for-builtins
62
- await expect(() => decrypt(Buffer.alloc(0), new String(1))).rejects.toThrow('Invalid passphrase');
63
- await expect(() => decrypt(Buffer.alloc(0), '')).rejects.toThrow('Invalid passphrase');
64
- await expect(() => decrypt(Buffer.alloc(100), 'xx')).rejects.toThrow('Invalid encrypted data');
62
+ await expect(async () => decrypt(Buffer.alloc(0), new String(1))).rejects.toThrow('Invalid passphrase');
63
+ await expect(async () => decrypt(Buffer.alloc(0), '')).rejects.toThrow('Invalid passphrase');
64
+ await expect(async () => decrypt(Buffer.alloc(100), 'xx')).rejects.toThrow('Invalid encrypted data');
65
65
  });
66
66
 
67
67
  describe('aad', () => {
@@ -108,7 +108,6 @@ function checkModule(module) {
108
108
  });
109
109
  });
110
110
 
111
- // eslint-disable-next-line @typescript-eslint/unbound-method
112
111
  const { encrypt, decrypt, encryptAad } = module;
113
112
 
114
113
  it.each(data)('encrypt/decrypt $type[$length] ($tag)', async ({ raw }) => {
package/benchmark.js DELETED
@@ -1,44 +0,0 @@
1
- /* eslint-disable no-console */
2
- import prettyBytes from 'pretty-bytes';
3
- import files from '../../benchmark-files/index.js';
4
- import { createModule } from './dist/encryption/module.js';
5
- import * as wasm from './dist/encryption/wasm.js';
6
- import * as web from './dist/encryption/web.js';
7
- import * as node from './dist/encryption/node.js';
8
-
9
- const t = (/** @type {number} */ time) =>
10
- Number.isFinite(time) ? (time.toFixed(2) + 'ms').padStart(10) : ' --------';
11
-
12
- const pb = (/** @type {number} */ size) => prettyBytes(size, { binary: true });
13
-
14
- /** 生成测试 */
15
- function createTest(/** @type {string} */ name, { encrypt, decrypt }, /** @type {boolean} */ decompressOnly = false) {
16
- /** 测试函数 */
17
- async function fn(/** @type {Buffer} */ data) {
18
- let start = performance.now();
19
- const encrypted = await encrypt(data, 'pass').catch((e) => console.error(e.message));
20
- const encryptTime = performance.now() - start;
21
- start = performance.now();
22
- const decrypted = await decrypt(encrypted, 'pass').catch((e) => console.error(e.message));
23
- const decryptTime = performance.now() - start;
24
- console.assert(data.equals(decrypted), name, `unmatched`);
25
- return { encrypted, encryptTime, decryptTime };
26
- }
27
- Object.defineProperty(fn, 'name', { value: name });
28
- return fn;
29
- }
30
- const tests = [
31
- createTest('web', createModule(web)),
32
- createTest('node', createModule(node)),
33
- createTest('wasm', createModule(wasm)),
34
- ];
35
- for await (const { file, data } of files()) {
36
- console.log(`File: ${file} \tRaw: ${pb(data.length)}`);
37
- for (const test of tests) {
38
- const result = await test(data);
39
- const diff = `+${result.encrypted.length - data.length}`;
40
- console.log(
41
- ` ${test.name.padEnd(25)} ${diff.padStart(6)} ${t(result.encryptTime)} ${t(result.decryptTime)}`,
42
- );
43
- }
44
- }
package/wasm-build.js DELETED
@@ -1,29 +0,0 @@
1
- import path from 'node:path';
2
- import fs from 'node:fs/promises';
3
- import { once } from 'node:events';
4
- import { spawn } from 'node:child_process';
5
- import esbuild from 'esbuild';
6
- import { wasmLoader } from 'esbuild-plugin-wasm';
7
-
8
- const wasmPack = spawn('wasm-pack', ['build', '--target', 'bundler', '--release'], {
9
- stdio: 'inherit',
10
- cwd: new URL('./wasm', import.meta.url),
11
- });
12
-
13
- await once(wasmPack, 'exit');
14
-
15
- await esbuild.build({
16
- entryPoints: [path.resolve(import.meta.dirname, './wasm/pkg/wasm.js')],
17
- outdir: path.resolve(import.meta.dirname, './lib'),
18
- charset: 'utf8',
19
- target: 'es2022',
20
- format: 'esm',
21
- bundle: true,
22
- minify: false,
23
- plugins: [wasmLoader({ mode: 'embedded' })],
24
- });
25
-
26
- await fs.copyFile(
27
- path.resolve(import.meta.dirname, './wasm/pkg/wasm.d.ts'),
28
- path.resolve(import.meta.dirname, './lib/wasm.d.ts'),
29
- );