@digitaldefiance/node-ecies-lib 1.1.21 → 1.1.22

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 (211) hide show
  1. package/README.md +4 -0
  2. package/package.json +14 -35
  3. package/src/constants.ts +182 -0
  4. package/src/enumerations/index.ts +1 -0
  5. package/src/enumerations/pbkdf2-profile.ts +8 -0
  6. package/src/i18n/ecies-i18n-factory.ts +435 -0
  7. package/{dist/i18n/index.d.ts → src/i18n/index.ts} +0 -1
  8. package/{dist/index.d.ts → src/index.ts} +0 -1
  9. package/src/interfaces/authenticated-cipher.ts +9 -0
  10. package/src/interfaces/authenticated-decipher.ts +8 -0
  11. package/src/interfaces/checksum-config.ts +4 -0
  12. package/src/interfaces/checksum-consts.ts +13 -0
  13. package/src/interfaces/constants.ts +43 -0
  14. package/src/interfaces/ecies-consts.ts +99 -0
  15. package/src/interfaces/encryption-consts.ts +10 -0
  16. package/{dist/interfaces/index.d.ts → src/interfaces/index.ts} +0 -1
  17. package/src/interfaces/keypair-buffer-with-un-encrypted-private-key.ts +7 -0
  18. package/src/interfaces/keyring-consts.ts +5 -0
  19. package/src/interfaces/member-operational.ts +52 -0
  20. package/{dist/interfaces/member-with-mnemonic.d.ts → src/interfaces/member-with-mnemonic.ts} +3 -3
  21. package/{dist/interfaces/multi-encrypted-message.d.ts → src/interfaces/multi-encrypted-message.ts} +5 -5
  22. package/src/interfaces/multi-encrypted-parsed-header.ts +24 -0
  23. package/{dist/interfaces/pbkdf-profiles.d.ts → src/interfaces/pbkdf-profiles.ts} +2 -2
  24. package/src/interfaces/pbkdf2-result.ts +5 -0
  25. package/src/interfaces/signing-key-private-key-info.ts +12 -0
  26. package/{dist/interfaces/simple-keypair-buffer.d.ts → src/interfaces/simple-keypair-buffer.ts} +3 -3
  27. package/{dist/interfaces/simple-keypair.d.ts → src/interfaces/simple-keypair.ts} +3 -3
  28. package/src/interfaces/simple-public-key-only-buffer.ts +3 -0
  29. package/src/interfaces/simple-public-key-only.ts +3 -0
  30. package/src/interfaces/single-encrypted-parsed-header.ts +35 -0
  31. package/{dist/interfaces/wallet-seed.d.ts → src/interfaces/wallet-seed.ts} +3 -3
  32. package/src/interfaces/wrapped-key-consts.ts +6 -0
  33. package/src/member.ts +463 -0
  34. package/src/services/aes-gcm.ts +160 -0
  35. package/src/services/ecies/crypto-core.ts +213 -0
  36. package/src/services/ecies/file.ts +174 -0
  37. package/{dist/services/ecies/index.d.ts → src/services/ecies/index.ts} +0 -1
  38. package/src/services/ecies/multi-recipient.ts +583 -0
  39. package/src/services/ecies/service.ts +351 -0
  40. package/src/services/ecies/signature.ts +91 -0
  41. package/src/services/ecies/single-recipient.ts +676 -0
  42. package/src/services/ecies/utilities.ts +111 -0
  43. package/src/services/index.ts +3 -0
  44. package/src/services/pbkdf2.ts +307 -0
  45. package/{dist/types.d.ts → src/types.ts} +26 -9
  46. package/src/utils.ts +104 -0
  47. package/dist/constants.d.ts +0 -32
  48. package/dist/constants.d.ts.map +0 -1
  49. package/dist/constants.js +0 -137
  50. package/dist/constants.js.map +0 -1
  51. package/dist/enumerations/index.d.ts +0 -2
  52. package/dist/enumerations/index.d.ts.map +0 -1
  53. package/dist/enumerations/index.js +0 -18
  54. package/dist/enumerations/index.js.map +0 -1
  55. package/dist/enumerations/pbkdf2-profile.d.ts +0 -9
  56. package/dist/enumerations/pbkdf2-profile.d.ts.map +0 -1
  57. package/dist/enumerations/pbkdf2-profile.js +0 -13
  58. package/dist/enumerations/pbkdf2-profile.js.map +0 -1
  59. package/dist/i18n/ecies-i18n-factory.d.ts +0 -54
  60. package/dist/i18n/ecies-i18n-factory.d.ts.map +0 -1
  61. package/dist/i18n/ecies-i18n-factory.js +0 -333
  62. package/dist/i18n/ecies-i18n-factory.js.map +0 -1
  63. package/dist/i18n/index.d.ts.map +0 -1
  64. package/dist/i18n/index.js +0 -18
  65. package/dist/i18n/index.js.map +0 -1
  66. package/dist/index.d.ts.map +0 -1
  67. package/dist/index.js +0 -24
  68. package/dist/index.js.map +0 -1
  69. package/dist/interfaces/authenticated-cipher.d.ts +0 -10
  70. package/dist/interfaces/authenticated-cipher.d.ts.map +0 -1
  71. package/dist/interfaces/authenticated-cipher.js +0 -3
  72. package/dist/interfaces/authenticated-cipher.js.map +0 -1
  73. package/dist/interfaces/authenticated-decipher.d.ts +0 -9
  74. package/dist/interfaces/authenticated-decipher.d.ts.map +0 -1
  75. package/dist/interfaces/authenticated-decipher.js +0 -3
  76. package/dist/interfaces/authenticated-decipher.js.map +0 -1
  77. package/dist/interfaces/checksum-config.d.ts +0 -5
  78. package/dist/interfaces/checksum-config.d.ts.map +0 -1
  79. package/dist/interfaces/checksum-config.js +0 -3
  80. package/dist/interfaces/checksum-config.js.map +0 -1
  81. package/dist/interfaces/checksum-consts.d.ts +0 -11
  82. package/dist/interfaces/checksum-consts.d.ts.map +0 -1
  83. package/dist/interfaces/checksum-consts.js +0 -3
  84. package/dist/interfaces/checksum-consts.js.map +0 -1
  85. package/dist/interfaces/constants.d.ts +0 -43
  86. package/dist/interfaces/constants.d.ts.map +0 -1
  87. package/dist/interfaces/constants.js +0 -3
  88. package/dist/interfaces/constants.js.map +0 -1
  89. package/dist/interfaces/ecies-consts.d.ts +0 -88
  90. package/dist/interfaces/ecies-consts.d.ts.map +0 -1
  91. package/dist/interfaces/ecies-consts.js +0 -3
  92. package/dist/interfaces/ecies-consts.js.map +0 -1
  93. package/dist/interfaces/encryption-consts.d.ts +0 -11
  94. package/dist/interfaces/encryption-consts.d.ts.map +0 -1
  95. package/dist/interfaces/encryption-consts.js +0 -3
  96. package/dist/interfaces/encryption-consts.js.map +0 -1
  97. package/dist/interfaces/index.d.ts.map +0 -1
  98. package/dist/interfaces/index.js +0 -34
  99. package/dist/interfaces/index.js.map +0 -1
  100. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.d.ts +0 -6
  101. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.d.ts.map +0 -1
  102. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.js +0 -3
  103. package/dist/interfaces/keypair-buffer-with-un-encrypted-private-key.js.map +0 -1
  104. package/dist/interfaces/keyring-consts.d.ts +0 -6
  105. package/dist/interfaces/keyring-consts.d.ts.map +0 -1
  106. package/dist/interfaces/keyring-consts.js +0 -3
  107. package/dist/interfaces/keyring-consts.js.map +0 -1
  108. package/dist/interfaces/member-operational.d.ts +0 -36
  109. package/dist/interfaces/member-operational.d.ts.map +0 -1
  110. package/dist/interfaces/member-operational.js +0 -3
  111. package/dist/interfaces/member-operational.js.map +0 -1
  112. package/dist/interfaces/member-with-mnemonic.d.ts.map +0 -1
  113. package/dist/interfaces/member-with-mnemonic.js +0 -3
  114. package/dist/interfaces/member-with-mnemonic.js.map +0 -1
  115. package/dist/interfaces/multi-encrypted-message.d.ts.map +0 -1
  116. package/dist/interfaces/multi-encrypted-message.js +0 -3
  117. package/dist/interfaces/multi-encrypted-message.js.map +0 -1
  118. package/dist/interfaces/multi-encrypted-parsed-header.d.ts +0 -24
  119. package/dist/interfaces/multi-encrypted-parsed-header.d.ts.map +0 -1
  120. package/dist/interfaces/multi-encrypted-parsed-header.js +0 -3
  121. package/dist/interfaces/multi-encrypted-parsed-header.js.map +0 -1
  122. package/dist/interfaces/pbkdf-profiles.d.ts.map +0 -1
  123. package/dist/interfaces/pbkdf-profiles.js +0 -3
  124. package/dist/interfaces/pbkdf-profiles.js.map +0 -1
  125. package/dist/interfaces/pbkdf2-result.d.ts +0 -6
  126. package/dist/interfaces/pbkdf2-result.d.ts.map +0 -1
  127. package/dist/interfaces/pbkdf2-result.js +0 -3
  128. package/dist/interfaces/pbkdf2-result.js.map +0 -1
  129. package/dist/interfaces/signing-key-private-key-info.d.ts +0 -11
  130. package/dist/interfaces/signing-key-private-key-info.d.ts.map +0 -1
  131. package/dist/interfaces/signing-key-private-key-info.js +0 -3
  132. package/dist/interfaces/signing-key-private-key-info.js.map +0 -1
  133. package/dist/interfaces/simple-keypair-buffer.d.ts.map +0 -1
  134. package/dist/interfaces/simple-keypair-buffer.js +0 -3
  135. package/dist/interfaces/simple-keypair-buffer.js.map +0 -1
  136. package/dist/interfaces/simple-keypair.d.ts.map +0 -1
  137. package/dist/interfaces/simple-keypair.js +0 -3
  138. package/dist/interfaces/simple-keypair.js.map +0 -1
  139. package/dist/interfaces/simple-public-key-only-buffer.d.ts +0 -4
  140. package/dist/interfaces/simple-public-key-only-buffer.d.ts.map +0 -1
  141. package/dist/interfaces/simple-public-key-only-buffer.js +0 -3
  142. package/dist/interfaces/simple-public-key-only-buffer.js.map +0 -1
  143. package/dist/interfaces/simple-public-key-only.d.ts +0 -4
  144. package/dist/interfaces/simple-public-key-only.d.ts.map +0 -1
  145. package/dist/interfaces/simple-public-key-only.js +0 -3
  146. package/dist/interfaces/simple-public-key-only.js.map +0 -1
  147. package/dist/interfaces/single-encrypted-parsed-header.d.ts +0 -35
  148. package/dist/interfaces/single-encrypted-parsed-header.d.ts.map +0 -1
  149. package/dist/interfaces/single-encrypted-parsed-header.js +0 -3
  150. package/dist/interfaces/single-encrypted-parsed-header.js.map +0 -1
  151. package/dist/interfaces/wallet-seed.d.ts.map +0 -1
  152. package/dist/interfaces/wallet-seed.js +0 -3
  153. package/dist/interfaces/wallet-seed.js.map +0 -1
  154. package/dist/interfaces/wrapped-key-consts.d.ts +0 -7
  155. package/dist/interfaces/wrapped-key-consts.d.ts.map +0 -1
  156. package/dist/interfaces/wrapped-key-consts.js +0 -3
  157. package/dist/interfaces/wrapped-key-consts.js.map +0 -1
  158. package/dist/member.d.ts +0 -74
  159. package/dist/member.d.ts.map +0 -1
  160. package/dist/member.js +0 -273
  161. package/dist/member.js.map +0 -1
  162. package/dist/services/aes-gcm.d.ts +0 -66
  163. package/dist/services/aes-gcm.d.ts.map +0 -1
  164. package/dist/services/aes-gcm.js +0 -115
  165. package/dist/services/aes-gcm.js.map +0 -1
  166. package/dist/services/ecies/crypto-core.d.ts +0 -83
  167. package/dist/services/ecies/crypto-core.d.ts.map +0 -1
  168. package/dist/services/ecies/crypto-core.js +0 -166
  169. package/dist/services/ecies/crypto-core.js.map +0 -1
  170. package/dist/services/ecies/file.d.ts +0 -30
  171. package/dist/services/ecies/file.d.ts.map +0 -1
  172. package/dist/services/ecies/file.js +0 -144
  173. package/dist/services/ecies/file.js.map +0 -1
  174. package/dist/services/ecies/index.d.ts.map +0 -1
  175. package/dist/services/ecies/index.js +0 -24
  176. package/dist/services/ecies/index.js.map +0 -1
  177. package/dist/services/ecies/multi-recipient.d.ts +0 -82
  178. package/dist/services/ecies/multi-recipient.d.ts.map +0 -1
  179. package/dist/services/ecies/multi-recipient.js +0 -360
  180. package/dist/services/ecies/multi-recipient.js.map +0 -1
  181. package/dist/services/ecies/service.d.ts +0 -70
  182. package/dist/services/ecies/service.d.ts.map +0 -1
  183. package/dist/services/ecies/service.js +0 -167
  184. package/dist/services/ecies/service.js.map +0 -1
  185. package/dist/services/ecies/signature.d.ts +0 -38
  186. package/dist/services/ecies/signature.d.ts.map +0 -1
  187. package/dist/services/ecies/signature.js +0 -69
  188. package/dist/services/ecies/signature.js.map +0 -1
  189. package/dist/services/ecies/single-recipient.d.ts +0 -85
  190. package/dist/services/ecies/single-recipient.d.ts.map +0 -1
  191. package/dist/services/ecies/single-recipient.js +0 -399
  192. package/dist/services/ecies/single-recipient.js.map +0 -1
  193. package/dist/services/ecies/utilities.d.ts +0 -22
  194. package/dist/services/ecies/utilities.d.ts.map +0 -1
  195. package/dist/services/ecies/utilities.js +0 -75
  196. package/dist/services/ecies/utilities.js.map +0 -1
  197. package/dist/services/index.d.ts +0 -4
  198. package/dist/services/index.d.ts.map +0 -1
  199. package/dist/services/index.js +0 -20
  200. package/dist/services/index.js.map +0 -1
  201. package/dist/services/pbkdf2.d.ts +0 -106
  202. package/dist/services/pbkdf2.d.ts.map +0 -1
  203. package/dist/services/pbkdf2.js +0 -195
  204. package/dist/services/pbkdf2.js.map +0 -1
  205. package/dist/types.d.ts.map +0 -1
  206. package/dist/types.js +0 -3
  207. package/dist/types.js.map +0 -1
  208. package/dist/utils.d.ts +0 -11
  209. package/dist/utils.d.ts.map +0 -1
  210. package/dist/utils.js +0 -82
  211. package/dist/utils.js.map +0 -1
@@ -0,0 +1,111 @@
1
+ import {
2
+ EciesEncryptionType,
3
+ ECIESError,
4
+ ECIESErrorTypeEnum,
5
+ IECIESConfig,
6
+ } from '@digitaldefiance/ecies-lib';
7
+ import { createEciesTranslationEngine } from '../../i18n/ecies-i18n-factory';
8
+ import { ECIESService } from './service';
9
+ import { getNodeRuntimeConfiguration } from '../../constants';
10
+
11
+ /**
12
+ * Utility functions for ECIES operations
13
+ */
14
+ export class EciesUtilities {
15
+ /**
16
+ * Computes the encrypted length from the data length.
17
+ * @param dataLength - The length of the data.
18
+ * @param encryptionMode - The encryption mode (simple, single, multiple).
19
+ * @param recipientCount - The number of recipients for multiple encryption mode.
20
+ * @returns The encrypted length details.
21
+ */
22
+ public computeEncryptedLengthFromDataLength(
23
+ dataLength: number,
24
+ encryptionMode: EciesEncryptionType,
25
+ recipientCount?: number,
26
+ ): number {
27
+ if (dataLength < 0) {
28
+ throw new ECIESError(
29
+ ECIESErrorTypeEnum.InvalidDataLength,
30
+ createEciesTranslationEngine(),
31
+ );
32
+ }
33
+ const runtimeDefaults = getNodeRuntimeConfiguration();
34
+ const eciesDefaults = runtimeDefaults.ECIES;
35
+ const config: IECIESConfig = {
36
+ curveName: eciesDefaults.CURVE_NAME,
37
+ primaryKeyDerivationPath: eciesDefaults.PRIMARY_KEY_DERIVATION_PATH,
38
+ mnemonicStrength: eciesDefaults.MNEMONIC_STRENGTH,
39
+ symmetricAlgorithm: eciesDefaults.SYMMETRIC_ALGORITHM_CONFIGURATION,
40
+ symmetricKeyBits: eciesDefaults.SYMMETRIC.KEY_BITS,
41
+ symmetricKeyMode: eciesDefaults.SYMMETRIC.MODE,
42
+ };
43
+ const engine = createEciesTranslationEngine();
44
+ const eciesService: ECIESService = new ECIESService(
45
+ engine,
46
+ config,
47
+ eciesDefaults,
48
+ );
49
+ switch (encryptionMode) {
50
+ case 'simple':
51
+ // type (1) + public key (65) + IV (16) + auth tag (16) = 98
52
+ return dataLength + eciesDefaults.SIMPLE.FIXED_OVERHEAD_SIZE;
53
+ case 'single':
54
+ // type (1) + public key (65) + IV (16) + auth tag (16) + data length (4) + crc16 (2) = 104
55
+ return dataLength + eciesDefaults.SINGLE.FIXED_OVERHEAD_SIZE;
56
+ case 'multiple':
57
+ return (
58
+ dataLength +
59
+ eciesService.calculateECIESMultipleRecipientOverhead(
60
+ recipientCount ?? 1,
61
+ true,
62
+ )
63
+ );
64
+ default:
65
+ throw new ECIESError(
66
+ ECIESErrorTypeEnum.InvalidEncryptionType,
67
+ createEciesTranslationEngine(),
68
+ );
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Computes the decrypted length from the encrypted data length.
74
+ * @param encryptedDataLength - The length of the encrypted data.
75
+ * @param padding - Optional padding value.
76
+ * @returns The decrypted length.
77
+ */
78
+ public computeDecryptedLengthFromEncryptedDataLength(
79
+ encryptedDataLength: number,
80
+ padding?: number,
81
+ ): number {
82
+ if (encryptedDataLength < 0) {
83
+ throw new ECIESError(
84
+ ECIESErrorTypeEnum.InvalidEncryptedDataLength,
85
+ createEciesTranslationEngine(),
86
+ );
87
+ }
88
+
89
+ const { ECIES: eciesDefaults } = getNodeRuntimeConfiguration();
90
+ const overhead = eciesDefaults.SINGLE.FIXED_OVERHEAD_SIZE;
91
+ const actualPadding = padding !== undefined ? padding : 0;
92
+
93
+ const decryptedLength = encryptedDataLength - overhead - actualPadding;
94
+ if (decryptedLength < 0) {
95
+ throw new ECIESError(
96
+ ECIESErrorTypeEnum.InvalidEncryptedDataLength,
97
+ createEciesTranslationEngine(),
98
+ undefined,
99
+ undefined,
100
+ {
101
+ encryptedDataLength: String(encryptedDataLength),
102
+ overhead: String(overhead),
103
+ padding: String(actualPadding),
104
+ computedLength: String(decryptedLength),
105
+ },
106
+ );
107
+ }
108
+
109
+ return decryptedLength;
110
+ }
111
+ }
@@ -0,0 +1,3 @@
1
+ export * from './aes-gcm';
2
+ export * from './ecies';
3
+ export * from './pbkdf2';
@@ -0,0 +1,307 @@
1
+ import { PluginI18nEngine, CoreLanguageCode } from '@digitaldefiance/i18n-lib';
2
+ import { EciesStringKey, EciesComponentId } from '@digitaldefiance/ecies-lib';
3
+ import {
4
+ IPbkdf2Config,
5
+ IPBkdf2Consts,
6
+ Pbkdf2ErrorType,
7
+ } from '@digitaldefiance/ecies-lib';
8
+ import { pbkdf2 as pbkdf2Async, pbkdf2Sync, randomBytes } from 'crypto';
9
+ import { promisify } from 'util';
10
+ import { IConstants } from '../interfaces/constants';
11
+ import { Pbkdf2ProfileEnum } from '../enumerations/pbkdf2-profile';
12
+ import { getEciesPluginI18nEngine, getNodeEciesTranslation, NodeEciesStringKey, NodeEciesComponentId } from '../i18n/ecies-i18n-factory';
13
+ import { IPbkdf2Result } from '../interfaces/pbkdf2-result';
14
+ import { IECIESConsts } from '../interfaces/ecies-consts';
15
+ import { Constants, getNodeRuntimeConfiguration } from '../constants';
16
+
17
+ /**
18
+ * Custom PBKDF2 error class that works with the plugin i18n system
19
+ */
20
+ export class NodePbkdf2Error extends Error {
21
+ constructor(message: string, public readonly type: Pbkdf2ErrorType) {
22
+ super(message);
23
+ this.name = 'NodePbkdf2Error';
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Service for handling PBKDF2 (Password-Based Key Derivation Function 2) operations.
29
+ * This service provides functionality for:
30
+ * - Generating secure key derivation configurations
31
+ * - Deriving cryptographic keys from passwords
32
+ * - Managing salt and iteration parameters
33
+ * - Both synchronous and asynchronous key derivation
34
+ */
35
+ export class Pbkdf2Service<TLanguage extends CoreLanguageCode = CoreLanguageCode> {
36
+ protected readonly engine: PluginI18nEngine<TLanguage>;
37
+ protected readonly profiles: Record<string, IPbkdf2Config>;
38
+ protected readonly eciesConsts: IECIESConsts;
39
+ protected readonly pbkdf2Consts: IPBkdf2Consts;
40
+
41
+ constructor(
42
+ engine: PluginI18nEngine<TLanguage>,
43
+ profiles?: Record<string, IPbkdf2Config>,
44
+ eciesParams: IECIESConsts = Constants.ECIES,
45
+ pbkdf2Params: IPBkdf2Consts = Constants.PBKDF2,
46
+ ) {
47
+ this.engine = engine;
48
+ this.profiles = profiles ? { ...profiles } : {};
49
+ const runtimeDefaults = getNodeRuntimeConfiguration();
50
+ this.eciesConsts = eciesParams ?? runtimeDefaults.ECIES;
51
+ this.pbkdf2Consts =
52
+ pbkdf2Params ?? runtimeDefaults.PBKDF2;
53
+ }
54
+
55
+ /**
56
+ * Register a new PBKDF2 profile
57
+ * @param profileName The name of the profile
58
+ * @param config The configuration for the profile
59
+ */
60
+ public registerProfile(profileName: string, config: IPbkdf2Config): void {
61
+ this.profiles[profileName] = { ...config };
62
+ }
63
+
64
+ /**
65
+ * Get all registered profile names
66
+ * @returns Array of profile names
67
+ */
68
+ public getRegisteredProfiles(): string[] {
69
+ return Object.keys(this.profiles);
70
+ }
71
+
72
+ /**
73
+ * Check if a profile is registered
74
+ * @param profileName The name of the profile to check
75
+ * @returns True if the profile exists
76
+ */
77
+ public hasProfile(profileName: string): boolean {
78
+ return profileName in this.profiles;
79
+ }
80
+
81
+ /**
82
+ * Create a Pbkdf2Service instance from IConstants (for backward compatibility)
83
+ * @param constants The constants object
84
+ * @returns A new Pbkdf2Service instance
85
+ */
86
+ public static fromConstants(constants: IConstants): Pbkdf2Service {
87
+ const engine = getEciesPluginI18nEngine();
88
+ const runtimeDefaults = getNodeRuntimeConfiguration();
89
+ return new Pbkdf2Service(
90
+ engine,
91
+ constants.PBKDF2_PROFILES,
92
+ runtimeDefaults.ECIES,
93
+ constants.PBKDF2,
94
+ );
95
+ }
96
+ /**
97
+ * Get a predefined configuration profile for common use cases
98
+ * @param profile The name of the profile to use
99
+ * @returns Configuration object for the specified profile
100
+ */
101
+ public getProfileConfig(
102
+ profile: string,
103
+ ): IPbkdf2Config {
104
+ const profileConfig = this.profiles[profile];
105
+ if (!profileConfig) {
106
+ throw new NodePbkdf2Error(
107
+ getNodeEciesTranslation(NodeEciesStringKey.Error_Pbkdf2_InvalidSaltLength),
108
+ Pbkdf2ErrorType.InvalidProfile,
109
+ );
110
+ }
111
+ return {
112
+ hashBytes: profileConfig.hashBytes,
113
+ saltBytes: profileConfig.saltBytes,
114
+ iterations: profileConfig.iterations,
115
+ algorithm: profileConfig.algorithm,
116
+ };
117
+ }
118
+
119
+ /**
120
+ * Generate an options object for pbkdf2
121
+ * @param iterations Optional number of iterations (defaults to Pbkdf2IterationsPerSecond)
122
+ * @param saltBytes Optional salt size in bytes (defaults to PBKDF2.SALT_BYTES)
123
+ * @param hashBytes Optional hash size in bytes (defaults to ECIES.SYMMETRIC.KEY_SIZE)
124
+ * @param algorithm Optional hash algorithm (defaults to PBKDF2.ALGORITHM)
125
+ * @returns Configuration object for PBKDF2
126
+ */
127
+ public getConfig(
128
+ iterations?: number,
129
+ saltBytes?: number,
130
+ hashBytes?: number,
131
+ algorithm?: string,
132
+ ): IPbkdf2Config {
133
+ // larger numbers mean better security, less
134
+ return {
135
+ // size of the generated hash
136
+ hashBytes: hashBytes ?? this.eciesConsts.SYMMETRIC.KEY_SIZE,
137
+ // larger salt means hashed passwords are more resistant to rainbow table, but
138
+ // you get diminishing returns pretty fast
139
+ saltBytes: saltBytes ?? this.pbkdf2Consts.SALT_BYTES,
140
+ // more iterations means an attacker has to take longer to brute force an
141
+ // individual password, so larger is better. however, larger also means longer
142
+ // to hash the password. tune so that hashing the password takes about a
143
+ // second
144
+ iterations: iterations ?? this.pbkdf2Consts.ITERATIONS_PER_SECOND,
145
+ // hash algorithm
146
+ algorithm: algorithm ?? this.pbkdf2Consts.ALGORITHM,
147
+ };
148
+ }
149
+
150
+ /**
151
+ * Given a password, use pbkdf2 to generate an appropriately sized key for AES encryption
152
+ * @param password The password to derive a key from
153
+ * @param salt Optional salt (will be randomly generated if not provided)
154
+ * @param iterations Optional number of iterations
155
+ * @param saltBytes Optional salt size in bytes
156
+ * @param keySize Optional key size in bytes
157
+ * @param algorithm Optional hash algorithm
158
+ * @returns Object containing the derived key, salt, and iteration count
159
+ */
160
+ public deriveKeyFromPassword(
161
+ password: Buffer,
162
+ salt?: Buffer,
163
+ iterations?: number,
164
+ saltBytes?: number,
165
+ keySize?: number,
166
+ algorithm?: string,
167
+ ): IPbkdf2Result {
168
+ const config = this.getConfig(
169
+ iterations,
170
+ saltBytes,
171
+ keySize,
172
+ algorithm,
173
+ );
174
+ const saltBytes_ = salt ?? randomBytes(config.saltBytes);
175
+
176
+ if (saltBytes_.length !== config.saltBytes) {
177
+ throw new NodePbkdf2Error(
178
+ getNodeEciesTranslation(NodeEciesStringKey.Error_Pbkdf2_InvalidSaltLength),
179
+ Pbkdf2ErrorType.InvalidSaltLength,
180
+ );
181
+ }
182
+
183
+ const hashBytes = pbkdf2Sync(
184
+ password,
185
+ saltBytes_,
186
+ config.iterations,
187
+ config.hashBytes,
188
+ config.algorithm,
189
+ );
190
+
191
+ if (hashBytes.length !== config.hashBytes) {
192
+ throw new NodePbkdf2Error(
193
+ getNodeEciesTranslation(NodeEciesStringKey.Error_Pbkdf2_InvalidHashLength),
194
+ Pbkdf2ErrorType.InvalidHashLength,
195
+ );
196
+ }
197
+
198
+ return {
199
+ salt: saltBytes_,
200
+ hash: hashBytes,
201
+ iterations: config.iterations,
202
+ };
203
+ }
204
+
205
+ /**
206
+ * Async version of deriveKeyFromPassword that uses libuv threadpool via crypto.pbkdf2
207
+ * to avoid blocking the event loop during password verification.
208
+ * @param password The password to derive a key from
209
+ * @param salt Optional salt (will be randomly generated if not provided)
210
+ * @param iterations Optional number of iterations
211
+ * @param saltBytes Optional salt size in bytes
212
+ * @param keySize Optional key size in bytes
213
+ * @param algorithm Optional hash algorithm
214
+ * @returns Promise resolving to object containing the derived key, salt, and iteration count
215
+ */
216
+ public async deriveKeyFromPasswordAsync(
217
+ password: Buffer,
218
+ salt?: Buffer,
219
+ iterations?: number,
220
+ saltBytes?: number,
221
+ keySize?: number,
222
+ algorithm?: string,
223
+ ): Promise<IPbkdf2Result> {
224
+ const config = this.getConfig(
225
+ iterations,
226
+ saltBytes,
227
+ keySize,
228
+ algorithm,
229
+ );
230
+ const saltBytes_ = salt ?? randomBytes(config.saltBytes);
231
+
232
+ if (saltBytes_.length !== config.saltBytes) {
233
+ throw new NodePbkdf2Error(
234
+ getNodeEciesTranslation(NodeEciesStringKey.Error_Pbkdf2_InvalidSaltLength),
235
+ Pbkdf2ErrorType.InvalidSaltLength,
236
+ );
237
+ }
238
+
239
+ const pbkdf2 = promisify(pbkdf2Async);
240
+ const hashBytes = (await pbkdf2(
241
+ password,
242
+ saltBytes_,
243
+ config.iterations,
244
+ config.hashBytes,
245
+ config.algorithm,
246
+ )) as Buffer;
247
+
248
+ if (hashBytes.length !== config.hashBytes) {
249
+ throw new NodePbkdf2Error(
250
+ getNodeEciesTranslation(NodeEciesStringKey.Error_Pbkdf2_InvalidHashLength),
251
+ Pbkdf2ErrorType.InvalidHashLength,
252
+ );
253
+ }
254
+
255
+ return {
256
+ salt: saltBytes_,
257
+ hash: hashBytes,
258
+ iterations: config.iterations,
259
+ };
260
+ }
261
+
262
+ /**
263
+ * Derive a key using a predefined configuration profile
264
+ * @param password The password to derive a key from
265
+ * @param profile The configuration profile to use
266
+ * @param salt Optional salt (will be randomly generated if not provided)
267
+ * @returns Object containing the derived key, salt, and iteration count
268
+ */
269
+ public deriveKeyFromPasswordWithProfile(
270
+ password: Buffer,
271
+ profile: Pbkdf2ProfileEnum,
272
+ salt?: Buffer,
273
+ ): IPbkdf2Result {
274
+ const config = this.getProfileConfig(profile);
275
+ return this.deriveKeyFromPassword(
276
+ password,
277
+ salt,
278
+ config.iterations,
279
+ config.saltBytes,
280
+ config.hashBytes,
281
+ config.algorithm,
282
+ );
283
+ }
284
+
285
+ /**
286
+ * Async version of deriveKeyFromPasswordWithProfile
287
+ * @param password The password to derive a key from
288
+ * @param profile The configuration profile to use
289
+ * @param salt Optional salt (will be randomly generated if not provided)
290
+ * @returns Promise resolving to object containing the derived key, salt, and iteration count
291
+ */
292
+ public async deriveKeyFromPasswordWithProfileAsync(
293
+ password: Buffer,
294
+ profile: Pbkdf2ProfileEnum,
295
+ salt?: Buffer,
296
+ ): Promise<IPbkdf2Result> {
297
+ const config = this.getProfileConfig(profile);
298
+ return this.deriveKeyFromPasswordAsync(
299
+ password,
300
+ salt,
301
+ config.iterations,
302
+ config.saltBytes,
303
+ config.hashBytes,
304
+ config.algorithm,
305
+ );
306
+ }
307
+ }
@@ -5,21 +5,38 @@ import { ISigningKeyPrivateKeyInfo } from './interfaces/signing-key-private-key-
5
5
  import { ISimpleKeyPairBuffer } from './interfaces/simple-keypair-buffer';
6
6
  import { ISimplePublicKeyOnly } from './interfaces/simple-public-key-only';
7
7
  import { ISimplePublicKeyOnlyBuffer } from './interfaces/simple-public-key-only-buffer';
8
- export type KeyPairBufferWithUnEncryptedPrivateKey = Brand<IKeyPairBufferWithUnEncryptedPrivateKey, 'KeyPairBufferWithUnEncryptedPrivateKey'>;
9
- export type SigningKeyPrivateKeyInfo = Brand<ISigningKeyPrivateKeyInfo, 'SigningKeyPrivateKeyInfo'>;
8
+
9
+ export type KeyPairBufferWithUnEncryptedPrivateKey = Brand<
10
+ IKeyPairBufferWithUnEncryptedPrivateKey,
11
+ 'KeyPairBufferWithUnEncryptedPrivateKey'
12
+ >;
13
+ export type SigningKeyPrivateKeyInfo = Brand<
14
+ ISigningKeyPrivateKeyInfo,
15
+ 'SigningKeyPrivateKeyInfo'
16
+ >;
10
17
  export type SimpleKeyPair = Brand<SimplePublicKeyOnly, 'SimpleKeyPair'>;
11
- export type SimplePublicKeyOnly = Brand<ISimplePublicKeyOnly, 'SimplePublicKeyOnly'>;
12
- export type SimpleKeyPairBuffer = Brand<ISimpleKeyPairBuffer, 'SimpleKeyPairBuffer'>;
13
- export type SimplePublicKeyOnlyBuffer = Brand<ISimplePublicKeyOnlyBuffer, 'SimplePublicKeyOnlyBuffer'>;
18
+ export type SimplePublicKeyOnly = Brand<
19
+ ISimplePublicKeyOnly,
20
+ 'SimplePublicKeyOnly'
21
+ >;
22
+ export type SimpleKeyPairBuffer = Brand<
23
+ ISimpleKeyPairBuffer,
24
+ 'SimpleKeyPairBuffer'
25
+ >;
26
+ export type SimplePublicKeyOnlyBuffer = Brand<
27
+ ISimplePublicKeyOnlyBuffer,
28
+ 'SimplePublicKeyOnlyBuffer'
29
+ >;
14
30
  export type SignatureString = Brand<HexString, 'SignatureString'>;
15
31
  export type SignatureBuffer = Buffer & Brand<Buffer, 'SignatureBuffer'>;
16
- export type ChecksumBuffer = Buffer & Brand<Buffer, 'Sha3Checksum', 'ChecksumBuffer'>;
32
+ export type ChecksumBuffer = Buffer &
33
+ Brand<Buffer, 'Sha3Checksum', 'ChecksumBuffer'>;
17
34
  export type ChecksumString = Brand<HexString, 'Sha3Checksum', 'ChecksumString'>;
35
+
18
36
  /**
19
37
  * Extended Buffer type for data
20
38
  */
21
39
  export type DataBuffer = Buffer & {
22
- toBuffer(): Buffer;
23
- toHex(): string;
40
+ toBuffer(): Buffer;
41
+ toHex(): string;
24
42
  };
25
- //# sourceMappingURL=types.d.ts.map
package/src/utils.ts ADDED
@@ -0,0 +1,104 @@
1
+ import {
2
+ getLengthEncodingTypeForLength,
3
+ getLengthEncodingTypeFromValue,
4
+ getLengthForLengthType,
5
+ LengthEncodingType,
6
+ } from '@digitaldefiance/ecies-lib';
7
+ import {
8
+ getEciesPluginI18nEngine,
9
+ getNodeEciesTranslation,
10
+ NodeEciesComponentId,
11
+ NodeEciesStringKey,
12
+ } from './i18n/ecies-i18n-factory';
13
+
14
+ /**
15
+ * Custom error class for length encoding errors
16
+ */
17
+ class LengthEncodingError extends Error {
18
+ constructor(message: string) {
19
+ super(message);
20
+ this.name = 'LengthEncodingError';
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Encodes the length of the data in the buffer
26
+ * @param buffer The buffer to encode
27
+ * @returns The encoded buffer
28
+ */
29
+ export function lengthEncodeData(buffer: Buffer): Buffer {
30
+ const lengthType: LengthEncodingType = getLengthEncodingTypeForLength(
31
+ buffer.length,
32
+ );
33
+ const lengthTypeSize: number = getLengthForLengthType(lengthType);
34
+ const result: Buffer = Buffer.alloc(1 + lengthTypeSize + buffer.length);
35
+ result.writeUInt8(lengthType, 0);
36
+ switch (lengthType) {
37
+ case LengthEncodingType.UInt8:
38
+ result.writeUInt8(buffer.length, 1);
39
+ break;
40
+ case LengthEncodingType.UInt16:
41
+ result.writeUInt16BE(buffer.length, 1);
42
+ break;
43
+ case LengthEncodingType.UInt32:
44
+ result.writeUInt32BE(buffer.length, 1);
45
+ break;
46
+ case LengthEncodingType.UInt64:
47
+ result.writeBigUInt64BE(BigInt(buffer.length), 1);
48
+ break;
49
+ }
50
+ buffer.copy(result, 1 + lengthTypeSize);
51
+ return result;
52
+ }
53
+
54
+ export function decodeLengthEncodedData(buffer: Buffer): {
55
+ data: Buffer;
56
+ totalLength: number;
57
+ } {
58
+ const pluginEngine = getEciesPluginI18nEngine();
59
+ if (buffer.length < 1) {
60
+ throw new RangeError(pluginEngine.translate(NodeEciesComponentId, NodeEciesStringKey.Error_BufferIsTooShort));
61
+ }
62
+ const lengthType: LengthEncodingType = getLengthEncodingTypeFromValue(
63
+ buffer.readUint8(0),
64
+ );
65
+ const lengthTypeSize: number = getLengthForLengthType(lengthType);
66
+
67
+ if (buffer.length < 1 + lengthTypeSize) {
68
+ throw new RangeError(pluginEngine.translate(NodeEciesComponentId, NodeEciesStringKey.Error_BufferIsTooShortToReadFullLengthValue));
69
+ }
70
+
71
+ let length: number | BigInt;
72
+ switch (lengthType) {
73
+ case LengthEncodingType.UInt8:
74
+ length = buffer.readUint8(1);
75
+ break;
76
+ case LengthEncodingType.UInt16:
77
+ length = buffer.readUint16BE(1);
78
+ break;
79
+ case LengthEncodingType.UInt32:
80
+ length = buffer.readUint32BE(1);
81
+ break;
82
+ case LengthEncodingType.UInt64:
83
+ length = buffer.readBigUInt64BE(1);
84
+ if (Number(length) > Number.MAX_SAFE_INTEGER) {
85
+ throw new RangeError(pluginEngine.translate(NodeEciesComponentId, NodeEciesStringKey.Error_LengthExceedsMaximumSafeInteger));
86
+ }
87
+ break;
88
+ default:
89
+ throw new LengthEncodingError(
90
+ getNodeEciesTranslation(
91
+ NodeEciesStringKey.Error_LengthError_LengthIsInvalidType,
92
+ ),
93
+ );
94
+ }
95
+
96
+ const totalLength = 1 + lengthTypeSize + Number(length);
97
+ if (totalLength > buffer.length) {
98
+ throw new RangeError(pluginEngine.translate(NodeEciesComponentId, NodeEciesStringKey.Error_BufferIsTooShortForDeclaredDataLength));
99
+ }
100
+ return {
101
+ data: buffer.subarray(1 + lengthTypeSize, totalLength),
102
+ totalLength,
103
+ };
104
+ }
@@ -1,32 +0,0 @@
1
- import { IPBkdf2Consts, getRuntimeConfiguration, registerRuntimeConfiguration } from '@digitaldefiance/ecies-lib';
2
- import { CipherGCMTypes } from 'crypto';
3
- import { IChecksumConsts } from './interfaces/checksum-consts';
4
- import { IConstants } from './interfaces/constants';
5
- import { IEncryptionConsts } from './interfaces/encryption-consts';
6
- import { IKeyringConsts } from './interfaces/keyring-consts';
7
- import { PbkdfProfiles } from './interfaces/pbkdf-profiles';
8
- import { IWrappedKeyConsts } from './interfaces/wrapped-key-consts';
9
- /**
10
- * Constants for checksum operations
11
- * These values are critical for data integrity and MUST NOT be changed
12
- * in an already established system as it will break all existing checksums.
13
- */
14
- export declare const NODE_RUNTIME_CONFIGURATION_KEY: unique symbol;
15
- type NodeRuntimeConfiguration = ReturnType<typeof getRuntimeConfiguration>;
16
- type NodeRuntimeOverrides = Parameters<typeof registerRuntimeConfiguration>[1];
17
- export declare const NODE_DEFAULTS_OVERRIDES: NodeRuntimeOverrides;
18
- export declare function getNodeRuntimeConfiguration(): NodeRuntimeConfiguration;
19
- export declare function registerNodeRuntimeConfiguration(configOrOverrides?: NodeRuntimeOverrides | NodeRuntimeConfiguration, options?: Parameters<typeof registerRuntimeConfiguration>[2]): NodeRuntimeConfiguration;
20
- export declare const CHECKSUM: IChecksumConsts;
21
- export declare const KEYRING: IKeyringConsts;
22
- export declare const PBKDF2: IPBkdf2Consts;
23
- export declare const PBKDF2_PROFILES: PbkdfProfiles;
24
- export declare const WRAPPED_KEY: IWrappedKeyConsts;
25
- export declare const KEYRING_ALGORITHM_CONFIGURATION: CipherGCMTypes;
26
- /**
27
- * Constants for encrypted data
28
- */
29
- export declare const ENCRYPTION: IEncryptionConsts;
30
- export declare const Constants: IConstants;
31
- export {};
32
- //# sourceMappingURL=constants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EACb,uBAAuB,EACvB,4BAA4B,EAC7B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAExC,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AAIpE;;;;GAIG;AACH,eAAO,MAAM,8BAA8B,eAE1C,CAAC;AAEF,KAAK,wBAAwB,GAAG,UAAU,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAC3E,KAAK,oBAAoB,GAAG,UAAU,CACpC,OAAO,4BAA4B,CACpC,CAAC,CAAC,CAAC,CAAC;AAEL,eAAO,MAAM,uBAAuB,EAAE,oBAIpC,CAAC;AAOH,wBAAgB,2BAA2B,IAAI,wBAAwB,CAEtE;AAED,wBAAgB,gCAAgC,CAC9C,iBAAiB,CAAC,EAAE,oBAAoB,GAAG,wBAAwB,EACnE,OAAO,CAAC,EAAE,UAAU,CAAC,OAAO,4BAA4B,CAAC,CAAC,CAAC,CAAC,GAC3D,wBAAwB,CAO1B;AAED,eAAO,MAAM,QAAQ,EAAE,eAA0C,CAAC;AAElE,eAAO,MAAM,OAAO,EAAE,cAIX,CAAC;AAEZ,eAAO,MAAM,MAAM,EAAE,aAAsC,CAAC;AAE5D,eAAO,MAAM,eAAe,EAAE,aAwCnB,CAAC;AAEZ,eAAO,MAAM,WAAW,EAAE,iBAKf,CAAC;AAEZ,eAAO,MAAM,+BAA+B,EACoB,cAAc,CAAC;AAE/E;;GAEG;AACH,eAAO,MAAM,UAAU,EAAE,iBAGd,CAAC;AAGZ,eAAO,MAAM,SAAS,EAAE,UAoCb,CAAC"}