@cloudpss/crypto 0.5.24 → 0.5.26

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.
Files changed (43) hide show
  1. package/benchmark.js +44 -0
  2. package/dist/encryption/browser.d.ts +3 -3
  3. package/dist/encryption/browser.js +1 -2
  4. package/dist/encryption/browser.js.map +1 -1
  5. package/dist/encryption/common.d.ts +45 -16
  6. package/dist/encryption/common.js +59 -9
  7. package/dist/encryption/common.js.map +1 -1
  8. package/dist/encryption/index.d.ts +4 -21
  9. package/dist/encryption/index.js +11 -63
  10. package/dist/encryption/index.js.map +1 -1
  11. package/dist/encryption/module.d.ts +22 -0
  12. package/dist/encryption/module.js +62 -0
  13. package/dist/encryption/module.js.map +1 -0
  14. package/dist/encryption/node.d.ts +3 -3
  15. package/dist/encryption/node.js +19 -15
  16. package/dist/encryption/node.js.map +1 -1
  17. package/dist/encryption/wasm.d.ts +5 -0
  18. package/dist/encryption/wasm.js +21 -0
  19. package/dist/encryption/wasm.js.map +1 -0
  20. package/dist/encryption/web.d.ts +3 -3
  21. package/dist/encryption/web.js +17 -15
  22. package/dist/encryption/web.js.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.js +1 -1
  25. package/dist/index.js.map +1 -1
  26. package/lib/wasm.d.ts +26 -0
  27. package/lib/wasm.js +149 -0
  28. package/package.json +11 -10
  29. package/src/encryption/browser.ts +4 -5
  30. package/src/encryption/common.ts +83 -16
  31. package/src/encryption/index.ts +12 -71
  32. package/src/encryption/module.ts +94 -0
  33. package/src/encryption/node.ts +24 -15
  34. package/src/encryption/wasm.ts +46 -0
  35. package/src/encryption/web.ts +24 -15
  36. package/src/index.ts +1 -1
  37. package/tests/encryption.js +151 -55
  38. package/tsconfig.json +2 -1
  39. package/wasm-build.js +30 -0
  40. package/dist/encryption/pure-js.d.ts +0 -5
  41. package/dist/encryption/pure-js.js +0 -54
  42. package/dist/encryption/pure-js.js.map +0 -1
  43. package/src/encryption/pure-js.ts +0 -62
@@ -1,62 +0,0 @@
1
- import { pbkdf2, createSHA256 } from 'hash-wasm';
2
- import AES from 'crypto-js/aes.js';
3
- import WordArray from 'crypto-js/lib-typedarrays.js';
4
- import type CryptoJS from 'crypto-js';
5
- import { AES_IV_SIZE, AES_KEY_SIZE, PBKDF2_SALT_SIZE, type EncryptionResult, PBKDF2_ITERATIONS } from './common.js';
6
-
7
- /** Convert word array to buffer data */
8
- function wordArrayToBuffer(wordArray: WordArray): Uint8Array {
9
- const { sigBytes, words } = wordArray;
10
- if (sigBytes < 0 || words.length * 4 < sigBytes) throw new Error('Invalid word array');
11
- const result = new Uint8Array(sigBytes);
12
- for (let i = 0; i < sigBytes; i++) {
13
- result[i] = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
14
- }
15
- return result;
16
- }
17
-
18
- /** Convert buffer data to word array */
19
- function bufferToWordArray(buffer: Uint8Array): WordArray {
20
- return WordArray.create(buffer);
21
- }
22
-
23
- /** Create aes params */
24
- async function aesKdfJs(passphrase: string, salt: Uint8Array): Promise<WordArray> {
25
- const result = await pbkdf2({
26
- password: passphrase,
27
- salt: salt,
28
- iterations: PBKDF2_ITERATIONS,
29
- hashLength: AES_KEY_SIZE,
30
- hashFunction: createSHA256(),
31
- outputType: 'binary',
32
- });
33
- return WordArray.create(result);
34
- }
35
-
36
- /** crypto-js encrypt */
37
- export async function encrypt(data: Uint8Array, passphrase: string): Promise<EncryptionResult> {
38
- const salt = wordArrayToBuffer(WordArray.random(PBKDF2_SALT_SIZE));
39
- const key = await aesKdfJs(passphrase, salt);
40
- const iv = WordArray.random(AES_IV_SIZE);
41
- const encrypted = AES.encrypt(bufferToWordArray(data), key, { iv });
42
- return {
43
- salt: salt,
44
- iv: wordArrayToBuffer(iv),
45
- data: wordArrayToBuffer(encrypted.ciphertext),
46
- };
47
- }
48
-
49
- /** crypto-js decrypt */
50
- export async function decrypt({ data, iv, salt }: EncryptionResult, passphrase: string): Promise<Uint8Array> {
51
- const key = await aesKdfJs(passphrase, salt);
52
- const decrypted = AES.decrypt(
53
- {
54
- ciphertext: bufferToWordArray(data),
55
- } as CryptoJS.lib.CipherParams,
56
- key,
57
- {
58
- iv: bufferToWordArray(iv),
59
- },
60
- );
61
- return wordArrayToBuffer(decrypted);
62
- }