@etsoo/appscript 1.4.3 → 1.4.5

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.
@@ -246,7 +246,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
246
246
  * @param passphrase Secret passphrase
247
247
  * @returns Pure text
248
248
  */
249
- decrypt(messageEncrypted: string, passphrase?: string): Promise<string | undefined>;
249
+ decrypt(messageEncrypted: string, passphrase?: string): string | undefined;
250
250
  /**
251
251
  * Enhanced decrypt message
252
252
  * @param messageEncrypted Encrypted message
@@ -254,7 +254,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
254
254
  * @param durationSeconds Duration seconds, <= 12 will be considered as month
255
255
  * @returns Pure text
256
256
  */
257
- decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): Promise<string | undefined>;
257
+ decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): string | undefined;
258
258
  /**
259
259
  * Detect IP data, call only one time
260
260
  * @param callback Callback will be called when the IP is ready
@@ -268,7 +268,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
268
268
  * @param iterations Iterations, 1000 times, 1 - 99
269
269
  * @returns Result
270
270
  */
271
- encrypt(message: string, passphrase?: string, iterations?: number): Promise<string>;
271
+ encrypt(message: string, passphrase?: string, iterations?: number): string;
272
272
  /**
273
273
  * Enhanced encrypt message
274
274
  * @param message Message
@@ -276,7 +276,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
276
276
  * @param iterations Iterations, 1000 times, 1 - 99
277
277
  * @returns Result
278
278
  */
279
- encryptEnhanced(message: string, passphrase?: string, iterations?: number): Promise<string>;
279
+ encryptEnhanced(message: string, passphrase?: string, iterations?: number): string;
280
280
  /**
281
281
  * Enchance secret passphrase
282
282
  * @param passphrase Secret passphrase
@@ -430,14 +430,14 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
430
430
  * @param message Message
431
431
  * @param passphrase Secret passphrase
432
432
  */
433
- hash(message: string, passphrase?: string): Promise<string>;
433
+ hash(message: string, passphrase?: string): string;
434
434
  /**
435
435
  * Hash message Hex, SHA3 or HmacSHA512, 512 as Base64
436
436
  * https://cryptojs.gitbook.io/docs/
437
437
  * @param message Message
438
438
  * @param passphrase Secret passphrase
439
439
  */
440
- hashHex(message: string, passphrase?: string): Promise<string>;
440
+ hashHex(message: string, passphrase?: string): string;
441
441
  /**
442
442
  * Check use has the specific role permission or not
443
443
  * @param roles Roles to check
@@ -34,6 +34,8 @@ const EntityStatus_1 = require("../business/EntityStatus");
34
34
  const ActionResultError_1 = require("../result/ActionResultError");
35
35
  const IApp_1 = require("./IApp");
36
36
  const UserRole_1 = require("./UserRole");
37
+ let CJ;
38
+ Promise.resolve().then(() => __importStar(require('crypto-js'))).then((result) => (CJ = result));
37
39
  /**
38
40
  * Core application
39
41
  */
@@ -408,7 +410,7 @@ class CoreApp {
408
410
  this.storage.setPersistedData(this.fields.devices, devices);
409
411
  // Current passphrase
410
412
  this.passphrase = passphrase;
411
- this.storage.setData(this.fields.devicePassphrase, this.encrypt(passphrase, this.name));
413
+ this.storage.setData(this.fields.devicePassphrase, await this.encrypt(passphrase, this.name));
412
414
  // Previous passphrase
413
415
  if (data.previousPassphrase) {
414
416
  const prev = await this.decrypt(data.previousPassphrase, timestamp.toString());
@@ -474,7 +476,7 @@ class CoreApp {
474
476
  // Overwrite the current value
475
477
  if (refreshToken !== '') {
476
478
  if (refreshToken != null)
477
- this.encrypt(refreshToken).then((result) => this.storage.setData(this.fields.headerToken, result));
479
+ refreshToken = this.encrypt(refreshToken);
478
480
  this.storage.setData(this.fields.headerToken, refreshToken);
479
481
  }
480
482
  // Reset tryLogin state
@@ -595,12 +597,12 @@ class CoreApp {
595
597
  * @param passphrase Secret passphrase
596
598
  * @returns Pure text
597
599
  */
598
- async decrypt(messageEncrypted, passphrase) {
600
+ decrypt(messageEncrypted, passphrase) {
599
601
  // Iterations
600
602
  const iterations = parseInt(messageEncrypted.substring(0, 2), 10);
601
603
  if (isNaN(iterations))
602
604
  return undefined;
603
- const { PBKDF2, algo, enc, AES, pad, mode } = await Promise.resolve().then(() => __importStar(require('crypto-js')));
605
+ const { PBKDF2, algo, enc, AES, pad, mode } = CJ;
604
606
  try {
605
607
  const salt = enc.Hex.parse(messageEncrypted.substring(2, 34));
606
608
  const iv = enc.Hex.parse(messageEncrypted.substring(34, 66));
@@ -628,7 +630,7 @@ class CoreApp {
628
630
  * @param durationSeconds Duration seconds, <= 12 will be considered as month
629
631
  * @returns Pure text
630
632
  */
631
- async decryptEnhanced(messageEncrypted, passphrase, durationSeconds) {
633
+ decryptEnhanced(messageEncrypted, passphrase, durationSeconds) {
632
634
  // Timestamp splitter
633
635
  const pos = messageEncrypted.indexOf('!');
634
636
  // Miliseconds chars are longer than 8
@@ -649,7 +651,7 @@ class CoreApp {
649
651
  }
650
652
  const message = messageEncrypted.substring(pos + 1);
651
653
  passphrase = this.encryptionEnhance(passphrase !== null && passphrase !== void 0 ? passphrase : this.passphrase, timestamp);
652
- return await this.decrypt(message, passphrase);
654
+ return this.decrypt(message, passphrase);
653
655
  }
654
656
  catch (e) {
655
657
  console.log('decryptEnhanced', e);
@@ -696,10 +698,10 @@ class CoreApp {
696
698
  * @param iterations Iterations, 1000 times, 1 - 99
697
699
  * @returns Result
698
700
  */
699
- async encrypt(message, passphrase, iterations) {
701
+ encrypt(message, passphrase, iterations) {
700
702
  // Default 1 * 1000
701
703
  iterations !== null && iterations !== void 0 ? iterations : (iterations = 1);
702
- const { lib, PBKDF2, algo, enc, AES, pad, mode } = await Promise.resolve().then(() => __importStar(require('crypto-js')));
704
+ const { lib, PBKDF2, algo, enc, AES, pad, mode } = CJ;
703
705
  const bits = 16; // 128 / 8
704
706
  const salt = lib.WordArray.random(bits);
705
707
  const key = PBKDF2(passphrase !== null && passphrase !== void 0 ? passphrase : this.passphrase, salt, {
@@ -725,11 +727,11 @@ class CoreApp {
725
727
  * @param iterations Iterations, 1000 times, 1 - 99
726
728
  * @returns Result
727
729
  */
728
- async encryptEnhanced(message, passphrase, iterations) {
730
+ encryptEnhanced(message, passphrase, iterations) {
729
731
  // Timestamp
730
732
  const timestamp = shared_1.Utils.numberToChars(new Date().getTime());
731
733
  passphrase = this.encryptionEnhance(passphrase !== null && passphrase !== void 0 ? passphrase : this.passphrase, timestamp);
732
- const result = await this.encrypt(message, passphrase, iterations);
734
+ const result = this.encrypt(message, passphrase, iterations);
733
735
  return timestamp + '!' + result;
734
736
  }
735
737
  /**
@@ -1069,8 +1071,8 @@ class CoreApp {
1069
1071
  * @param message Message
1070
1072
  * @param passphrase Secret passphrase
1071
1073
  */
1072
- async hash(message, passphrase) {
1073
- const { SHA3, enc, HmacSHA512 } = await Promise.resolve().then(() => __importStar(require('crypto-js')));
1074
+ hash(message, passphrase) {
1075
+ const { SHA3, enc, HmacSHA512 } = CJ;
1074
1076
  if (passphrase == null)
1075
1077
  return SHA3(message, { outputLength: 512 }).toString(enc.Base64);
1076
1078
  else
@@ -1082,8 +1084,8 @@ class CoreApp {
1082
1084
  * @param message Message
1083
1085
  * @param passphrase Secret passphrase
1084
1086
  */
1085
- async hashHex(message, passphrase) {
1086
- const { SHA3, enc, HmacSHA512 } = await Promise.resolve().then(() => __importStar(require('crypto-js')));
1087
+ hashHex(message, passphrase) {
1088
+ const { SHA3, enc, HmacSHA512 } = CJ;
1087
1089
  if (passphrase == null)
1088
1090
  return SHA3(message, { outputLength: 512 }).toString(enc.Hex);
1089
1091
  else
@@ -1258,7 +1260,7 @@ class CoreApp {
1258
1260
  this.authorize(user.token, refreshToken);
1259
1261
  }
1260
1262
  else {
1261
- this.encrypt(refreshToken).then((result) => (this.cachedRefreshToken = result));
1263
+ this.cachedRefreshToken = this.encrypt(refreshToken);
1262
1264
  this.authorize(user.token, undefined);
1263
1265
  }
1264
1266
  }
@@ -188,7 +188,7 @@ export interface IApp {
188
188
  * @param passphrase Secret passphrase
189
189
  * @returns Pure text
190
190
  */
191
- decrypt(messageEncrypted: string, passphrase?: string): Promise<string | undefined>;
191
+ decrypt(messageEncrypted: string, passphrase?: string): string | undefined;
192
192
  /**
193
193
  * Enhanced decrypt message
194
194
  * @param messageEncrypted Encrypted message
@@ -196,7 +196,7 @@ export interface IApp {
196
196
  * @param durationSeconds Duration seconds, <= 12 will be considered as month
197
197
  * @returns Pure text
198
198
  */
199
- decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): Promise<string | undefined>;
199
+ decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): string | undefined;
200
200
  /**
201
201
  * Detect IP data, call only one time
202
202
  * @param callback Callback will be called when the IP is ready
@@ -209,7 +209,7 @@ export interface IApp {
209
209
  * @param iterations Iterations, 1000 times, 1 - 99
210
210
  * @returns Result
211
211
  */
212
- encrypt(message: string, passphrase?: string, iterations?: number): Promise<string>;
212
+ encrypt(message: string, passphrase?: string, iterations?: number): string;
213
213
  /**
214
214
  * Enhanced encrypt message
215
215
  * @param message Message
@@ -217,7 +217,7 @@ export interface IApp {
217
217
  * @param iterations Iterations, 1000 times, 1 - 99
218
218
  * @returns Result
219
219
  */
220
- encryptEnhanced(message: string, passphrase?: string, iterations?: number): Promise<string>;
220
+ encryptEnhanced(message: string, passphrase?: string, iterations?: number): string;
221
221
  /**
222
222
  * Format date to string
223
223
  * @param input Input date
@@ -366,14 +366,14 @@ export interface IApp {
366
366
  * @param message Message
367
367
  * @param passphrase Secret passphrase
368
368
  */
369
- hash(message: string, passphrase?: string): Promise<string>;
369
+ hash(message: string, passphrase?: string): string;
370
370
  /**
371
371
  * Hash message Hex, SHA3 or HmacSHA512, 512 as Base64
372
372
  * https://cryptojs.gitbook.io/docs/
373
373
  * @param message Message
374
374
  * @param passphrase Secret passphrase
375
375
  */
376
- hashHex(message: string, passphrase?: string): Promise<string>;
376
+ hashHex(message: string, passphrase?: string): string;
377
377
  /**
378
378
  * Check use has the specific role permission or not
379
379
  * @param roles Roles to check
@@ -246,7 +246,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
246
246
  * @param passphrase Secret passphrase
247
247
  * @returns Pure text
248
248
  */
249
- decrypt(messageEncrypted: string, passphrase?: string): Promise<string | undefined>;
249
+ decrypt(messageEncrypted: string, passphrase?: string): string | undefined;
250
250
  /**
251
251
  * Enhanced decrypt message
252
252
  * @param messageEncrypted Encrypted message
@@ -254,7 +254,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
254
254
  * @param durationSeconds Duration seconds, <= 12 will be considered as month
255
255
  * @returns Pure text
256
256
  */
257
- decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): Promise<string | undefined>;
257
+ decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): string | undefined;
258
258
  /**
259
259
  * Detect IP data, call only one time
260
260
  * @param callback Callback will be called when the IP is ready
@@ -268,7 +268,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
268
268
  * @param iterations Iterations, 1000 times, 1 - 99
269
269
  * @returns Result
270
270
  */
271
- encrypt(message: string, passphrase?: string, iterations?: number): Promise<string>;
271
+ encrypt(message: string, passphrase?: string, iterations?: number): string;
272
272
  /**
273
273
  * Enhanced encrypt message
274
274
  * @param message Message
@@ -276,7 +276,7 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
276
276
  * @param iterations Iterations, 1000 times, 1 - 99
277
277
  * @returns Result
278
278
  */
279
- encryptEnhanced(message: string, passphrase?: string, iterations?: number): Promise<string>;
279
+ encryptEnhanced(message: string, passphrase?: string, iterations?: number): string;
280
280
  /**
281
281
  * Enchance secret passphrase
282
282
  * @param passphrase Secret passphrase
@@ -430,14 +430,14 @@ export declare abstract class CoreApp<U extends IUser, S extends IAppSettings, N
430
430
  * @param message Message
431
431
  * @param passphrase Secret passphrase
432
432
  */
433
- hash(message: string, passphrase?: string): Promise<string>;
433
+ hash(message: string, passphrase?: string): string;
434
434
  /**
435
435
  * Hash message Hex, SHA3 or HmacSHA512, 512 as Base64
436
436
  * https://cryptojs.gitbook.io/docs/
437
437
  * @param message Message
438
438
  * @param passphrase Secret passphrase
439
439
  */
440
- hashHex(message: string, passphrase?: string): Promise<string>;
440
+ hashHex(message: string, passphrase?: string): string;
441
441
  /**
442
442
  * Check use has the specific role permission or not
443
443
  * @param roles Roles to check
@@ -8,6 +8,8 @@ import { EntityStatus } from '../business/EntityStatus';
8
8
  import { ActionResultError } from '../result/ActionResultError';
9
9
  import { appFields } from './IApp';
10
10
  import { UserRole } from './UserRole';
11
+ let CJ;
12
+ import('crypto-js').then((result) => (CJ = result));
11
13
  /**
12
14
  * Core application
13
15
  */
@@ -382,7 +384,7 @@ export class CoreApp {
382
384
  this.storage.setPersistedData(this.fields.devices, devices);
383
385
  // Current passphrase
384
386
  this.passphrase = passphrase;
385
- this.storage.setData(this.fields.devicePassphrase, this.encrypt(passphrase, this.name));
387
+ this.storage.setData(this.fields.devicePassphrase, await this.encrypt(passphrase, this.name));
386
388
  // Previous passphrase
387
389
  if (data.previousPassphrase) {
388
390
  const prev = await this.decrypt(data.previousPassphrase, timestamp.toString());
@@ -448,7 +450,7 @@ export class CoreApp {
448
450
  // Overwrite the current value
449
451
  if (refreshToken !== '') {
450
452
  if (refreshToken != null)
451
- this.encrypt(refreshToken).then((result) => this.storage.setData(this.fields.headerToken, result));
453
+ refreshToken = this.encrypt(refreshToken);
452
454
  this.storage.setData(this.fields.headerToken, refreshToken);
453
455
  }
454
456
  // Reset tryLogin state
@@ -569,12 +571,12 @@ export class CoreApp {
569
571
  * @param passphrase Secret passphrase
570
572
  * @returns Pure text
571
573
  */
572
- async decrypt(messageEncrypted, passphrase) {
574
+ decrypt(messageEncrypted, passphrase) {
573
575
  // Iterations
574
576
  const iterations = parseInt(messageEncrypted.substring(0, 2), 10);
575
577
  if (isNaN(iterations))
576
578
  return undefined;
577
- const { PBKDF2, algo, enc, AES, pad, mode } = await import('crypto-js');
579
+ const { PBKDF2, algo, enc, AES, pad, mode } = CJ;
578
580
  try {
579
581
  const salt = enc.Hex.parse(messageEncrypted.substring(2, 34));
580
582
  const iv = enc.Hex.parse(messageEncrypted.substring(34, 66));
@@ -602,7 +604,7 @@ export class CoreApp {
602
604
  * @param durationSeconds Duration seconds, <= 12 will be considered as month
603
605
  * @returns Pure text
604
606
  */
605
- async decryptEnhanced(messageEncrypted, passphrase, durationSeconds) {
607
+ decryptEnhanced(messageEncrypted, passphrase, durationSeconds) {
606
608
  // Timestamp splitter
607
609
  const pos = messageEncrypted.indexOf('!');
608
610
  // Miliseconds chars are longer than 8
@@ -623,7 +625,7 @@ export class CoreApp {
623
625
  }
624
626
  const message = messageEncrypted.substring(pos + 1);
625
627
  passphrase = this.encryptionEnhance(passphrase !== null && passphrase !== void 0 ? passphrase : this.passphrase, timestamp);
626
- return await this.decrypt(message, passphrase);
628
+ return this.decrypt(message, passphrase);
627
629
  }
628
630
  catch (e) {
629
631
  console.log('decryptEnhanced', e);
@@ -670,10 +672,10 @@ export class CoreApp {
670
672
  * @param iterations Iterations, 1000 times, 1 - 99
671
673
  * @returns Result
672
674
  */
673
- async encrypt(message, passphrase, iterations) {
675
+ encrypt(message, passphrase, iterations) {
674
676
  // Default 1 * 1000
675
677
  iterations !== null && iterations !== void 0 ? iterations : (iterations = 1);
676
- const { lib, PBKDF2, algo, enc, AES, pad, mode } = await import('crypto-js');
678
+ const { lib, PBKDF2, algo, enc, AES, pad, mode } = CJ;
677
679
  const bits = 16; // 128 / 8
678
680
  const salt = lib.WordArray.random(bits);
679
681
  const key = PBKDF2(passphrase !== null && passphrase !== void 0 ? passphrase : this.passphrase, salt, {
@@ -699,11 +701,11 @@ export class CoreApp {
699
701
  * @param iterations Iterations, 1000 times, 1 - 99
700
702
  * @returns Result
701
703
  */
702
- async encryptEnhanced(message, passphrase, iterations) {
704
+ encryptEnhanced(message, passphrase, iterations) {
703
705
  // Timestamp
704
706
  const timestamp = Utils.numberToChars(new Date().getTime());
705
707
  passphrase = this.encryptionEnhance(passphrase !== null && passphrase !== void 0 ? passphrase : this.passphrase, timestamp);
706
- const result = await this.encrypt(message, passphrase, iterations);
708
+ const result = this.encrypt(message, passphrase, iterations);
707
709
  return timestamp + '!' + result;
708
710
  }
709
711
  /**
@@ -1043,8 +1045,8 @@ export class CoreApp {
1043
1045
  * @param message Message
1044
1046
  * @param passphrase Secret passphrase
1045
1047
  */
1046
- async hash(message, passphrase) {
1047
- const { SHA3, enc, HmacSHA512 } = await import('crypto-js');
1048
+ hash(message, passphrase) {
1049
+ const { SHA3, enc, HmacSHA512 } = CJ;
1048
1050
  if (passphrase == null)
1049
1051
  return SHA3(message, { outputLength: 512 }).toString(enc.Base64);
1050
1052
  else
@@ -1056,8 +1058,8 @@ export class CoreApp {
1056
1058
  * @param message Message
1057
1059
  * @param passphrase Secret passphrase
1058
1060
  */
1059
- async hashHex(message, passphrase) {
1060
- const { SHA3, enc, HmacSHA512 } = await import('crypto-js');
1061
+ hashHex(message, passphrase) {
1062
+ const { SHA3, enc, HmacSHA512 } = CJ;
1061
1063
  if (passphrase == null)
1062
1064
  return SHA3(message, { outputLength: 512 }).toString(enc.Hex);
1063
1065
  else
@@ -1232,7 +1234,7 @@ export class CoreApp {
1232
1234
  this.authorize(user.token, refreshToken);
1233
1235
  }
1234
1236
  else {
1235
- this.encrypt(refreshToken).then((result) => (this.cachedRefreshToken = result));
1237
+ this.cachedRefreshToken = this.encrypt(refreshToken);
1236
1238
  this.authorize(user.token, undefined);
1237
1239
  }
1238
1240
  }
@@ -188,7 +188,7 @@ export interface IApp {
188
188
  * @param passphrase Secret passphrase
189
189
  * @returns Pure text
190
190
  */
191
- decrypt(messageEncrypted: string, passphrase?: string): Promise<string | undefined>;
191
+ decrypt(messageEncrypted: string, passphrase?: string): string | undefined;
192
192
  /**
193
193
  * Enhanced decrypt message
194
194
  * @param messageEncrypted Encrypted message
@@ -196,7 +196,7 @@ export interface IApp {
196
196
  * @param durationSeconds Duration seconds, <= 12 will be considered as month
197
197
  * @returns Pure text
198
198
  */
199
- decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): Promise<string | undefined>;
199
+ decryptEnhanced(messageEncrypted: string, passphrase?: string, durationSeconds?: number): string | undefined;
200
200
  /**
201
201
  * Detect IP data, call only one time
202
202
  * @param callback Callback will be called when the IP is ready
@@ -209,7 +209,7 @@ export interface IApp {
209
209
  * @param iterations Iterations, 1000 times, 1 - 99
210
210
  * @returns Result
211
211
  */
212
- encrypt(message: string, passphrase?: string, iterations?: number): Promise<string>;
212
+ encrypt(message: string, passphrase?: string, iterations?: number): string;
213
213
  /**
214
214
  * Enhanced encrypt message
215
215
  * @param message Message
@@ -217,7 +217,7 @@ export interface IApp {
217
217
  * @param iterations Iterations, 1000 times, 1 - 99
218
218
  * @returns Result
219
219
  */
220
- encryptEnhanced(message: string, passphrase?: string, iterations?: number): Promise<string>;
220
+ encryptEnhanced(message: string, passphrase?: string, iterations?: number): string;
221
221
  /**
222
222
  * Format date to string
223
223
  * @param input Input date
@@ -366,14 +366,14 @@ export interface IApp {
366
366
  * @param message Message
367
367
  * @param passphrase Secret passphrase
368
368
  */
369
- hash(message: string, passphrase?: string): Promise<string>;
369
+ hash(message: string, passphrase?: string): string;
370
370
  /**
371
371
  * Hash message Hex, SHA3 or HmacSHA512, 512 as Base64
372
372
  * https://cryptojs.gitbook.io/docs/
373
373
  * @param message Message
374
374
  * @param passphrase Secret passphrase
375
375
  */
376
- hashHex(message: string, passphrase?: string): Promise<string>;
376
+ hashHex(message: string, passphrase?: string): string;
377
377
  /**
378
378
  * Check use has the specific role permission or not
379
379
  * @param roles Roles to check
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/appscript",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "Applications shared TypeScript framework",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -38,6 +38,11 @@ import {
38
38
  RefreshTokenResult
39
39
  } from './IApp';
40
40
  import { UserRole } from './UserRole';
41
+ import type CryptoJS from 'crypto-js';
42
+
43
+ type CJType = typeof CryptoJS;
44
+ let CJ: CJType;
45
+ import('crypto-js').then((result) => (CJ = result));
41
46
 
42
47
  /**
43
48
  * Core application interface
@@ -593,7 +598,7 @@ export abstract class CoreApp<
593
598
  this.passphrase = passphrase;
594
599
  this.storage.setData(
595
600
  this.fields.devicePassphrase,
596
- this.encrypt(passphrase, this.name)
601
+ await this.encrypt(passphrase, this.name)
597
602
  );
598
603
 
599
604
  // Previous passphrase
@@ -681,10 +686,7 @@ export abstract class CoreApp<
681
686
 
682
687
  // Overwrite the current value
683
688
  if (refreshToken !== '') {
684
- if (refreshToken != null)
685
- this.encrypt(refreshToken).then((result) =>
686
- this.storage.setData(this.fields.headerToken, result)
687
- );
689
+ if (refreshToken != null) refreshToken = this.encrypt(refreshToken);
688
690
  this.storage.setData(this.fields.headerToken, refreshToken);
689
691
  }
690
692
 
@@ -826,12 +828,12 @@ export abstract class CoreApp<
826
828
  * @param passphrase Secret passphrase
827
829
  * @returns Pure text
828
830
  */
829
- async decrypt(messageEncrypted: string, passphrase?: string) {
831
+ decrypt(messageEncrypted: string, passphrase?: string) {
830
832
  // Iterations
831
833
  const iterations = parseInt(messageEncrypted.substring(0, 2), 10);
832
834
  if (isNaN(iterations)) return undefined;
833
835
 
834
- const { PBKDF2, algo, enc, AES, pad, mode } = await import('crypto-js');
836
+ const { PBKDF2, algo, enc, AES, pad, mode } = CJ;
835
837
 
836
838
  try {
837
839
  const salt = enc.Hex.parse(messageEncrypted.substring(2, 34));
@@ -862,7 +864,7 @@ export abstract class CoreApp<
862
864
  * @param durationSeconds Duration seconds, <= 12 will be considered as month
863
865
  * @returns Pure text
864
866
  */
865
- async decryptEnhanced(
867
+ decryptEnhanced(
866
868
  messageEncrypted: string,
867
869
  passphrase?: string,
868
870
  durationSeconds?: number
@@ -895,7 +897,7 @@ export abstract class CoreApp<
895
897
  timestamp
896
898
  );
897
899
 
898
- return await this.decrypt(message, passphrase);
900
+ return this.decrypt(message, passphrase);
899
901
  } catch (e) {
900
902
  console.log('decryptEnhanced', e);
901
903
  return undefined;
@@ -949,13 +951,11 @@ export abstract class CoreApp<
949
951
  * @param iterations Iterations, 1000 times, 1 - 99
950
952
  * @returns Result
951
953
  */
952
- async encrypt(message: string, passphrase?: string, iterations?: number) {
954
+ encrypt(message: string, passphrase?: string, iterations?: number) {
953
955
  // Default 1 * 1000
954
956
  iterations ??= 1;
955
957
 
956
- const { lib, PBKDF2, algo, enc, AES, pad, mode } = await import(
957
- 'crypto-js'
958
- );
958
+ const { lib, PBKDF2, algo, enc, AES, pad, mode } = CJ;
959
959
 
960
960
  const bits = 16; // 128 / 8
961
961
  const salt = lib.WordArray.random(bits);
@@ -985,11 +985,7 @@ export abstract class CoreApp<
985
985
  * @param iterations Iterations, 1000 times, 1 - 99
986
986
  * @returns Result
987
987
  */
988
- async encryptEnhanced(
989
- message: string,
990
- passphrase?: string,
991
- iterations?: number
992
- ) {
988
+ encryptEnhanced(message: string, passphrase?: string, iterations?: number) {
993
989
  // Timestamp
994
990
  const timestamp = Utils.numberToChars(new Date().getTime());
995
991
 
@@ -998,7 +994,7 @@ export abstract class CoreApp<
998
994
  timestamp
999
995
  );
1000
996
 
1001
- const result = await this.encrypt(message, passphrase, iterations);
997
+ const result = this.encrypt(message, passphrase, iterations);
1002
998
 
1003
999
  return timestamp + '!' + result;
1004
1000
  }
@@ -1399,8 +1395,8 @@ export abstract class CoreApp<
1399
1395
  * @param message Message
1400
1396
  * @param passphrase Secret passphrase
1401
1397
  */
1402
- async hash(message: string, passphrase?: string) {
1403
- const { SHA3, enc, HmacSHA512 } = await import('crypto-js');
1398
+ hash(message: string, passphrase?: string) {
1399
+ const { SHA3, enc, HmacSHA512 } = CJ;
1404
1400
  if (passphrase == null)
1405
1401
  return SHA3(message, { outputLength: 512 }).toString(enc.Base64);
1406
1402
  else return HmacSHA512(message, passphrase).toString(enc.Base64);
@@ -1412,8 +1408,8 @@ export abstract class CoreApp<
1412
1408
  * @param message Message
1413
1409
  * @param passphrase Secret passphrase
1414
1410
  */
1415
- async hashHex(message: string, passphrase?: string) {
1416
- const { SHA3, enc, HmacSHA512 } = await import('crypto-js');
1411
+ hashHex(message: string, passphrase?: string) {
1412
+ const { SHA3, enc, HmacSHA512 } = CJ;
1417
1413
  if (passphrase == null)
1418
1414
  return SHA3(message, { outputLength: 512 }).toString(enc.Hex);
1419
1415
  else return HmacSHA512(message, passphrase).toString(enc.Hex);
@@ -1610,9 +1606,7 @@ export abstract class CoreApp<
1610
1606
  if (keep) {
1611
1607
  this.authorize(user.token, refreshToken);
1612
1608
  } else {
1613
- this.encrypt(refreshToken).then(
1614
- (result) => (this.cachedRefreshToken = result)
1615
- );
1609
+ this.cachedRefreshToken = this.encrypt(refreshToken);
1616
1610
  this.authorize(user.token, undefined);
1617
1611
  }
1618
1612
  }
package/src/app/IApp.ts CHANGED
@@ -251,10 +251,7 @@ export interface IApp {
251
251
  * @param passphrase Secret passphrase
252
252
  * @returns Pure text
253
253
  */
254
- decrypt(
255
- messageEncrypted: string,
256
- passphrase?: string
257
- ): Promise<string | undefined>;
254
+ decrypt(messageEncrypted: string, passphrase?: string): string | undefined;
258
255
 
259
256
  /**
260
257
  * Enhanced decrypt message
@@ -267,7 +264,7 @@ export interface IApp {
267
264
  messageEncrypted: string,
268
265
  passphrase?: string,
269
266
  durationSeconds?: number
270
- ): Promise<string | undefined>;
267
+ ): string | undefined;
271
268
 
272
269
  /**
273
270
  * Detect IP data, call only one time
@@ -282,11 +279,7 @@ export interface IApp {
282
279
  * @param iterations Iterations, 1000 times, 1 - 99
283
280
  * @returns Result
284
281
  */
285
- encrypt(
286
- message: string,
287
- passphrase?: string,
288
- iterations?: number
289
- ): Promise<string>;
282
+ encrypt(message: string, passphrase?: string, iterations?: number): string;
290
283
 
291
284
  /**
292
285
  * Enhanced encrypt message
@@ -299,7 +292,7 @@ export interface IApp {
299
292
  message: string,
300
293
  passphrase?: string,
301
294
  iterations?: number
302
- ): Promise<string>;
295
+ ): string;
303
296
 
304
297
  /**
305
298
  * Format date to string
@@ -504,7 +497,7 @@ export interface IApp {
504
497
  * @param message Message
505
498
  * @param passphrase Secret passphrase
506
499
  */
507
- hash(message: string, passphrase?: string): Promise<string>;
500
+ hash(message: string, passphrase?: string): string;
508
501
 
509
502
  /**
510
503
  * Hash message Hex, SHA3 or HmacSHA512, 512 as Base64
@@ -512,7 +505,7 @@ export interface IApp {
512
505
  * @param message Message
513
506
  * @param passphrase Secret passphrase
514
507
  */
515
- hashHex(message: string, passphrase?: string): Promise<string>;
508
+ hashHex(message: string, passphrase?: string): string;
516
509
 
517
510
  /**
518
511
  * Check use has the specific role permission or not