@digitaldefiance/ecies-lib 1.0.0

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 (267) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +237 -0
  3. package/dist/backup-code-string.d.ts +26 -0
  4. package/dist/backup-code-string.d.ts.map +1 -0
  5. package/dist/backup-code-string.js +69 -0
  6. package/dist/backup-code-string.js.map +1 -0
  7. package/dist/constants.d.ts +22 -0
  8. package/dist/constants.d.ts.map +1 -0
  9. package/dist/constants.js +177 -0
  10. package/dist/constants.js.map +1 -0
  11. package/dist/email-string.d.ts +42 -0
  12. package/dist/email-string.d.ts.map +1 -0
  13. package/dist/email-string.js +67 -0
  14. package/dist/email-string.js.map +1 -0
  15. package/dist/enumerations/ecies-encryption-type.d.ts +15 -0
  16. package/dist/enumerations/ecies-encryption-type.d.ts.map +1 -0
  17. package/dist/enumerations/ecies-encryption-type.js +63 -0
  18. package/dist/enumerations/ecies-encryption-type.js.map +1 -0
  19. package/dist/enumerations/ecies-error-type.d.ts +32 -0
  20. package/dist/enumerations/ecies-error-type.d.ts.map +1 -0
  21. package/dist/enumerations/ecies-error-type.js +33 -0
  22. package/dist/enumerations/ecies-error-type.js.map +1 -0
  23. package/dist/enumerations/guid-brand-type.d.ts +27 -0
  24. package/dist/enumerations/guid-brand-type.d.ts.map +1 -0
  25. package/dist/enumerations/guid-brand-type.js +28 -0
  26. package/dist/enumerations/guid-brand-type.js.map +1 -0
  27. package/dist/enumerations/guid-error-type.d.ts +7 -0
  28. package/dist/enumerations/guid-error-type.d.ts.map +1 -0
  29. package/dist/enumerations/guid-error-type.js +8 -0
  30. package/dist/enumerations/guid-error-type.js.map +1 -0
  31. package/dist/enumerations/index.d.ts +13 -0
  32. package/dist/enumerations/index.d.ts.map +1 -0
  33. package/dist/enumerations/index.js +13 -0
  34. package/dist/enumerations/index.js.map +1 -0
  35. package/dist/enumerations/invalid-email-type.d.ts +6 -0
  36. package/dist/enumerations/invalid-email-type.d.ts.map +1 -0
  37. package/dist/enumerations/invalid-email-type.js +7 -0
  38. package/dist/enumerations/invalid-email-type.js.map +1 -0
  39. package/dist/enumerations/length-encoding-type.d.ts +7 -0
  40. package/dist/enumerations/length-encoding-type.d.ts.map +1 -0
  41. package/dist/enumerations/length-encoding-type.js +8 -0
  42. package/dist/enumerations/length-encoding-type.js.map +1 -0
  43. package/dist/enumerations/length-error-type.d.ts +6 -0
  44. package/dist/enumerations/length-error-type.d.ts.map +1 -0
  45. package/dist/enumerations/length-error-type.js +7 -0
  46. package/dist/enumerations/length-error-type.js.map +1 -0
  47. package/dist/enumerations/member-error-type.d.ts +87 -0
  48. package/dist/enumerations/member-error-type.d.ts.map +1 -0
  49. package/dist/enumerations/member-error-type.js +88 -0
  50. package/dist/enumerations/member-error-type.js.map +1 -0
  51. package/dist/enumerations/member-type.d.ts +16 -0
  52. package/dist/enumerations/member-type.d.ts.map +1 -0
  53. package/dist/enumerations/member-type.js +17 -0
  54. package/dist/enumerations/member-type.js.map +1 -0
  55. package/dist/enumerations/pbkdf2-error-type.d.ts +5 -0
  56. package/dist/enumerations/pbkdf2-error-type.d.ts.map +1 -0
  57. package/dist/enumerations/pbkdf2-error-type.js +6 -0
  58. package/dist/enumerations/pbkdf2-error-type.js.map +1 -0
  59. package/dist/enumerations/pbkdf2-profile.d.ts +5 -0
  60. package/dist/enumerations/pbkdf2-profile.d.ts.map +1 -0
  61. package/dist/enumerations/pbkdf2-profile.js +6 -0
  62. package/dist/enumerations/pbkdf2-profile.js.map +1 -0
  63. package/dist/enumerations/secure-storage-error-type.d.ts +6 -0
  64. package/dist/enumerations/secure-storage-error-type.d.ts.map +1 -0
  65. package/dist/enumerations/secure-storage-error-type.js +7 -0
  66. package/dist/enumerations/secure-storage-error-type.js.map +1 -0
  67. package/dist/errors/disposed.d.ts +4 -0
  68. package/dist/errors/disposed.d.ts.map +1 -0
  69. package/dist/errors/disposed.js +8 -0
  70. package/dist/errors/disposed.js.map +1 -0
  71. package/dist/errors/ecies.d.ts +6 -0
  72. package/dist/errors/ecies.d.ts.map +1 -0
  73. package/dist/errors/ecies.js +8 -0
  74. package/dist/errors/ecies.js.map +1 -0
  75. package/dist/errors/guid.d.ts +14 -0
  76. package/dist/errors/guid.d.ts.map +1 -0
  77. package/dist/errors/guid.js +19 -0
  78. package/dist/errors/guid.js.map +1 -0
  79. package/dist/errors/index.d.ts +9 -0
  80. package/dist/errors/index.d.ts.map +1 -0
  81. package/dist/errors/index.js +9 -0
  82. package/dist/errors/index.js.map +1 -0
  83. package/dist/errors/invalid-backup-code.d.ts +4 -0
  84. package/dist/errors/invalid-backup-code.d.ts.map +1 -0
  85. package/dist/errors/invalid-backup-code.js +7 -0
  86. package/dist/errors/invalid-backup-code.js.map +1 -0
  87. package/dist/errors/invalid-email.d.ts +6 -0
  88. package/dist/errors/invalid-email.d.ts.map +1 -0
  89. package/dist/errors/invalid-email.js +9 -0
  90. package/dist/errors/invalid-email.js.map +1 -0
  91. package/dist/errors/length.d.ts +6 -0
  92. package/dist/errors/length.d.ts.map +1 -0
  93. package/dist/errors/length.js +9 -0
  94. package/dist/errors/length.js.map +1 -0
  95. package/dist/errors/member.d.ts +6 -0
  96. package/dist/errors/member.d.ts.map +1 -0
  97. package/dist/errors/member.js +8 -0
  98. package/dist/errors/member.js.map +1 -0
  99. package/dist/errors/pbkdf2.d.ts +6 -0
  100. package/dist/errors/pbkdf2.d.ts.map +1 -0
  101. package/dist/errors/pbkdf2.js +8 -0
  102. package/dist/errors/pbkdf2.js.map +1 -0
  103. package/dist/errors/secure-storage.d.ts +6 -0
  104. package/dist/errors/secure-storage.d.ts.map +1 -0
  105. package/dist/errors/secure-storage.js +8 -0
  106. package/dist/errors/secure-storage.js.map +1 -0
  107. package/dist/errors/typed.d.ts +5 -0
  108. package/dist/errors/typed.d.ts.map +1 -0
  109. package/dist/errors/typed.js +11 -0
  110. package/dist/errors/typed.js.map +1 -0
  111. package/dist/guid.d.ts +153 -0
  112. package/dist/guid.d.ts.map +1 -0
  113. package/dist/guid.js +604 -0
  114. package/dist/guid.js.map +1 -0
  115. package/dist/index.d.ts +13 -0
  116. package/dist/index.d.ts.map +1 -0
  117. package/dist/index.js +13 -0
  118. package/dist/index.js.map +1 -0
  119. package/dist/interfaces/backup-code-consts.d.ts +16 -0
  120. package/dist/interfaces/backup-code-consts.d.ts.map +1 -0
  121. package/dist/interfaces/backup-code-consts.js +2 -0
  122. package/dist/interfaces/backup-code-consts.js.map +1 -0
  123. package/dist/interfaces/backup-code.d.ts +7 -0
  124. package/dist/interfaces/backup-code.d.ts.map +1 -0
  125. package/dist/interfaces/backup-code.js +2 -0
  126. package/dist/interfaces/backup-code.js.map +1 -0
  127. package/dist/interfaces/constants.d.ts +45 -0
  128. package/dist/interfaces/constants.d.ts.map +1 -0
  129. package/dist/interfaces/constants.js +2 -0
  130. package/dist/interfaces/constants.js.map +1 -0
  131. package/dist/interfaces/ecies-config.d.ts +9 -0
  132. package/dist/interfaces/ecies-config.d.ts.map +1 -0
  133. package/dist/interfaces/ecies-config.js +2 -0
  134. package/dist/interfaces/ecies-config.js.map +1 -0
  135. package/dist/interfaces/ecies-consts.d.ts +58 -0
  136. package/dist/interfaces/ecies-consts.d.ts.map +1 -0
  137. package/dist/interfaces/ecies-consts.js +2 -0
  138. package/dist/interfaces/ecies-consts.js.map +1 -0
  139. package/dist/interfaces/ecies-file-service.d.ts +7 -0
  140. package/dist/interfaces/ecies-file-service.d.ts.map +1 -0
  141. package/dist/interfaces/ecies-file-service.js +2 -0
  142. package/dist/interfaces/ecies-file-service.js.map +1 -0
  143. package/dist/interfaces/guid.d.ts +45 -0
  144. package/dist/interfaces/guid.d.ts.map +1 -0
  145. package/dist/interfaces/guid.js +2 -0
  146. package/dist/interfaces/guid.js.map +1 -0
  147. package/dist/interfaces/index.d.ts +14 -0
  148. package/dist/interfaces/index.d.ts.map +1 -0
  149. package/dist/interfaces/index.js +14 -0
  150. package/dist/interfaces/index.js.map +1 -0
  151. package/dist/interfaces/member-operational.d.ts +39 -0
  152. package/dist/interfaces/member-operational.d.ts.map +1 -0
  153. package/dist/interfaces/member-operational.js +2 -0
  154. package/dist/interfaces/member-operational.js.map +1 -0
  155. package/dist/interfaces/member-storage.d.ts +21 -0
  156. package/dist/interfaces/member-storage.d.ts.map +1 -0
  157. package/dist/interfaces/member-storage.js +2 -0
  158. package/dist/interfaces/member-storage.js.map +1 -0
  159. package/dist/interfaces/member-with-mnemonic.d.ts +7 -0
  160. package/dist/interfaces/member-with-mnemonic.d.ts.map +1 -0
  161. package/dist/interfaces/member-with-mnemonic.js +2 -0
  162. package/dist/interfaces/member-with-mnemonic.js.map +1 -0
  163. package/dist/interfaces/pbkdf2-config.d.ts +7 -0
  164. package/dist/interfaces/pbkdf2-config.d.ts.map +1 -0
  165. package/dist/interfaces/pbkdf2-config.js +2 -0
  166. package/dist/interfaces/pbkdf2-config.js.map +1 -0
  167. package/dist/interfaces/pbkdf2-consts.d.ts +9 -0
  168. package/dist/interfaces/pbkdf2-consts.d.ts.map +1 -0
  169. package/dist/interfaces/pbkdf2-consts.js +2 -0
  170. package/dist/interfaces/pbkdf2-consts.js.map +1 -0
  171. package/dist/interfaces/pbkdf2-result.d.ts +6 -0
  172. package/dist/interfaces/pbkdf2-result.d.ts.map +1 -0
  173. package/dist/interfaces/pbkdf2-result.js +2 -0
  174. package/dist/interfaces/pbkdf2-result.js.map +1 -0
  175. package/dist/member.d.ts +68 -0
  176. package/dist/member.d.ts.map +1 -0
  177. package/dist/member.js +256 -0
  178. package/dist/member.js.map +1 -0
  179. package/dist/pbkdf2-profiles.d.ts +6 -0
  180. package/dist/pbkdf2-profiles.d.ts.map +1 -0
  181. package/dist/pbkdf2-profiles.js +2 -0
  182. package/dist/pbkdf2-profiles.js.map +1 -0
  183. package/dist/secure-buffer.d.ts +38 -0
  184. package/dist/secure-buffer.d.ts.map +1 -0
  185. package/dist/secure-buffer.js +161 -0
  186. package/dist/secure-buffer.js.map +1 -0
  187. package/dist/secure-string.d.ts +39 -0
  188. package/dist/secure-string.d.ts.map +1 -0
  189. package/dist/secure-string.js +188 -0
  190. package/dist/secure-string.js.map +1 -0
  191. package/dist/services/aes-gcm.d.ts +56 -0
  192. package/dist/services/aes-gcm.d.ts.map +1 -0
  193. package/dist/services/aes-gcm.js +101 -0
  194. package/dist/services/aes-gcm.js.map +1 -0
  195. package/dist/services/ecies/crypto-core.d.ts +53 -0
  196. package/dist/services/ecies/crypto-core.d.ts.map +1 -0
  197. package/dist/services/ecies/crypto-core.js +158 -0
  198. package/dist/services/ecies/crypto-core.js.map +1 -0
  199. package/dist/services/ecies/example.d.ts +25 -0
  200. package/dist/services/ecies/example.d.ts.map +1 -0
  201. package/dist/services/ecies/example.js +121 -0
  202. package/dist/services/ecies/example.js.map +1 -0
  203. package/dist/services/ecies/file.d.ts +18 -0
  204. package/dist/services/ecies/file.d.ts.map +1 -0
  205. package/dist/services/ecies/file.js +106 -0
  206. package/dist/services/ecies/file.js.map +1 -0
  207. package/dist/services/ecies/index.d.ts +38 -0
  208. package/dist/services/ecies/index.d.ts.map +1 -0
  209. package/dist/services/ecies/index.js +39 -0
  210. package/dist/services/ecies/index.js.map +1 -0
  211. package/dist/services/ecies/integration.d.ts +59 -0
  212. package/dist/services/ecies/integration.d.ts.map +1 -0
  213. package/dist/services/ecies/integration.js +163 -0
  214. package/dist/services/ecies/integration.js.map +1 -0
  215. package/dist/services/ecies/interfaces.d.ts +51 -0
  216. package/dist/services/ecies/interfaces.d.ts.map +1 -0
  217. package/dist/services/ecies/interfaces.js +5 -0
  218. package/dist/services/ecies/interfaces.js.map +1 -0
  219. package/dist/services/ecies/manual-test.d.ts +29 -0
  220. package/dist/services/ecies/manual-test.d.ts.map +1 -0
  221. package/dist/services/ecies/manual-test.js +163 -0
  222. package/dist/services/ecies/manual-test.js.map +1 -0
  223. package/dist/services/ecies/multi-recipient.d.ts +49 -0
  224. package/dist/services/ecies/multi-recipient.d.ts.map +1 -0
  225. package/dist/services/ecies/multi-recipient.js +222 -0
  226. package/dist/services/ecies/multi-recipient.js.map +1 -0
  227. package/dist/services/ecies/service.d.ts +100 -0
  228. package/dist/services/ecies/service.d.ts.map +1 -0
  229. package/dist/services/ecies/service.js +186 -0
  230. package/dist/services/ecies/service.js.map +1 -0
  231. package/dist/services/ecies/signature.d.ts +27 -0
  232. package/dist/services/ecies/signature.d.ts.map +1 -0
  233. package/dist/services/ecies/signature.js +72 -0
  234. package/dist/services/ecies/signature.js.map +1 -0
  235. package/dist/services/ecies/single-recipient.d.ts +43 -0
  236. package/dist/services/ecies/single-recipient.d.ts.map +1 -0
  237. package/dist/services/ecies/single-recipient.js +196 -0
  238. package/dist/services/ecies/single-recipient.js.map +1 -0
  239. package/dist/services/index.d.ts +5 -0
  240. package/dist/services/index.d.ts.map +1 -0
  241. package/dist/services/index.js +5 -0
  242. package/dist/services/index.js.map +1 -0
  243. package/dist/services/password-login.d.ts +41 -0
  244. package/dist/services/password-login.d.ts.map +1 -0
  245. package/dist/services/password-login.js +96 -0
  246. package/dist/services/password-login.js.map +1 -0
  247. package/dist/services/pbkdf2.d.ts +49 -0
  248. package/dist/services/pbkdf2.d.ts.map +1 -0
  249. package/dist/services/pbkdf2.js +97 -0
  250. package/dist/services/pbkdf2.js.map +1 -0
  251. package/dist/services/xor.d.ts +37 -0
  252. package/dist/services/xor.d.ts.map +1 -0
  253. package/dist/services/xor.js +63 -0
  254. package/dist/services/xor.js.map +1 -0
  255. package/dist/types.d.ts +29 -0
  256. package/dist/types.d.ts.map +1 -0
  257. package/dist/types.js +2 -0
  258. package/dist/types.js.map +1 -0
  259. package/dist/utils.d.ts +62 -0
  260. package/dist/utils.d.ts.map +1 -0
  261. package/dist/utils.js +244 -0
  262. package/dist/utils.js.map +1 -0
  263. package/dist/xor.d.ts +37 -0
  264. package/dist/xor.d.ts.map +1 -0
  265. package/dist/xor.js +63 -0
  266. package/dist/xor.js.map +1 -0
  267. package/package.json +60 -0
@@ -0,0 +1,96 @@
1
+ import { Wallet } from '@ethereumjs/wallet';
2
+ import { EciesEncryptionTypeEnum } from '../enumerations/ecies-encryption-type';
3
+ import { Pbkdf2ProfileEnum } from '../enumerations/pbkdf2-profile';
4
+ import { SecureString } from '../secure-string';
5
+ import { hexToUint8Array, uint8ArrayToHex } from '../utils';
6
+ import { AESGCMService } from './aes-gcm';
7
+ import { Pbkdf2Service } from './pbkdf2';
8
+ export class PasswordLoginService {
9
+ eciesService;
10
+ static privateKeyStorageKey = 'encryptedPrivateKey';
11
+ static saltStorageKey = 'passwordLoginSalt';
12
+ static encryptedMnemonicStorageKey = 'encryptedMnemonic';
13
+ constructor(eciesService) {
14
+ this.eciesService = eciesService;
15
+ }
16
+ async createPasswordLoginBundle(mnemonic, password) {
17
+ const { wallet } = this.eciesService.walletAndSeedFromMnemonic(mnemonic);
18
+ const derivedKey = await Pbkdf2Service.deriveKeyFromPasswordWithProfileAsync(password.valueAsUint8Array, Pbkdf2ProfileEnum.BROWSER_PASSWORD);
19
+ // Encrypt private key with derived key
20
+ const privateKeyBytes = wallet.getPrivateKey();
21
+ const { encrypted, iv, tag } = await AESGCMService.encrypt(privateKeyBytes, derivedKey.hash, true);
22
+ const encryptedPrivateKey = AESGCMService.combineIvTagAndEncryptedData(iv, encrypted, tag);
23
+ // now use the public key to encrypt the mnemonic and store it
24
+ const encryptedMnemonic = await this.eciesService.encrypt(EciesEncryptionTypeEnum.Simple, [{ publicKey: wallet.getPublicKey() }], mnemonic.valueAsUint8Array);
25
+ return {
26
+ salt: derivedKey.salt,
27
+ encryptedPrivateKey: encryptedPrivateKey,
28
+ encryptedMnemonic: encryptedMnemonic,
29
+ wallet,
30
+ };
31
+ }
32
+ /**
33
+ * Set up password login by deriving a key from the password and using it to encrypt
34
+ * @param mnemonic The user's mnemonic
35
+ * @param password The user's password
36
+ */
37
+ async setupPasswordLoginLocalStorageBundle(mnemonic, password) {
38
+ const { salt, encryptedPrivateKey, encryptedMnemonic, wallet } = await this.createPasswordLoginBundle(mnemonic, password);
39
+ // store the salt and encrypted private key in local storage
40
+ localStorage.setItem(PasswordLoginService.saltStorageKey, uint8ArrayToHex(salt));
41
+ localStorage.setItem(PasswordLoginService.privateKeyStorageKey, uint8ArrayToHex(encryptedPrivateKey));
42
+ localStorage.setItem(PasswordLoginService.encryptedMnemonicStorageKey, uint8ArrayToHex(encryptedMnemonic));
43
+ return wallet;
44
+ }
45
+ async getWalletAndMnemonicFromEncryptedPasswordBundle(salt, encryptedPrivateKey, encryptedMnemonic, password) {
46
+ if (!salt || !encryptedPrivateKey || !encryptedMnemonic) {
47
+ throw new Error('Password login not set up');
48
+ }
49
+ const derivedKey = await Pbkdf2Service.deriveKeyFromPasswordWithProfileAsync(password.valueAsUint8Array, Pbkdf2ProfileEnum.BROWSER_PASSWORD, salt);
50
+ // Decrypt private key with derived key
51
+ const { iv, encryptedDataWithTag } = AESGCMService.splitEncryptedData(encryptedPrivateKey, true);
52
+ const privateKeyBytes = await AESGCMService.decrypt(iv, encryptedDataWithTag, derivedKey.hash, true);
53
+ const wallet = Wallet.fromPrivateKey(privateKeyBytes);
54
+ // now decrypt the mnemonic
55
+ const decryptedMnemonic = await this.eciesService.decryptSimpleOrSingleWithHeader(true, wallet.getPrivateKey(), encryptedMnemonic);
56
+ return { wallet, mnemonic: new SecureString(decryptedMnemonic) };
57
+ }
58
+ /**
59
+ * Recover wallet and mnemonic from password
60
+ * @param password The user's password
61
+ * @returns The user's wallet and mnemonic
62
+ */
63
+ async getWalletAndMnemonicFromLocalStorageBundle(password) {
64
+ const saltHex = localStorage.getItem(PasswordLoginService.saltStorageKey);
65
+ const encryptedPrivateKeyHex = localStorage.getItem(PasswordLoginService.privateKeyStorageKey);
66
+ const encryptedMnemonicHex = localStorage.getItem(PasswordLoginService.encryptedMnemonicStorageKey);
67
+ if (!saltHex ||
68
+ !encryptedPrivateKeyHex ||
69
+ !encryptedMnemonicHex ||
70
+ saltHex === '' ||
71
+ encryptedPrivateKeyHex === '' ||
72
+ encryptedMnemonicHex === '') {
73
+ throw new Error('Password login not set up');
74
+ }
75
+ const salt = hexToUint8Array(saltHex);
76
+ const encryptedPrivateKey = hexToUint8Array(encryptedPrivateKeyHex);
77
+ const encryptedMnemonic = hexToUint8Array(encryptedMnemonicHex);
78
+ return await this.getWalletAndMnemonicFromEncryptedPasswordBundle(salt, encryptedPrivateKey, encryptedMnemonic, password);
79
+ }
80
+ /**
81
+ *
82
+ * @returns True if password login is set up (i.e. salt and encrypted private key are in local storage)
83
+ */
84
+ static isPasswordLoginSetup() {
85
+ const saltHex = localStorage.getItem(PasswordLoginService.saltStorageKey);
86
+ const encryptedPrivateKeyHex = localStorage.getItem(PasswordLoginService.privateKeyStorageKey);
87
+ const encryptedMnemonicHex = localStorage.getItem(PasswordLoginService.encryptedMnemonicStorageKey);
88
+ return !!(saltHex &&
89
+ encryptedPrivateKeyHex &&
90
+ encryptedMnemonicHex &&
91
+ saltHex !== '' &&
92
+ encryptedPrivateKeyHex !== '' &&
93
+ encryptedMnemonicHex !== '');
94
+ }
95
+ }
96
+ //# sourceMappingURL=password-login.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"password-login.js","sourceRoot":"","sources":["../../src/services/password-login.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,MAAM,uCAAuC,CAAC;AAChF,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACnE,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,MAAM,OAAO,oBAAoB;IACd,YAAY,CAAe;IACrC,MAAM,CAAU,oBAAoB,GAAG,qBAAqB,CAAC;IAC7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AAsB;YACtB,oBAAoB;YACpB,OAAO,KAAK,EAAE;YACd,sBAAsB,KAAK,EAAE;YAC7B,oBAAoB,KAAK,EAAE,CAC5B,CAAC;IACJ,CAAC"}
@@ -0,0 +1,49 @@
1
+ import { PBKDF2_PROFILES } from '../constants';
2
+ import { Pbkdf2ProfileEnum } from '../enumerations/pbkdf2-profile';
3
+ import { IPbkdf2Config } from '../interfaces/pbkdf2-config';
4
+ import { IPbkdf2Result } from '../interfaces/pbkdf2-result';
5
+ /**
6
+ * Service for handling PBKDF2 (Password-Based Key Derivation Function 2) operations.
7
+ * This service provides functionality for:
8
+ * - Generating secure key derivation configurations
9
+ * - Deriving cryptographic keys from passwords
10
+ * - Managing salt and iteration parameters
11
+ * - Both synchronous and asynchronous key derivation
12
+ */
13
+ export declare abstract class Pbkdf2Service {
14
+ /**
15
+ * Get a predefined configuration profile for common use cases
16
+ * @param profile The name of the profile to use
17
+ * @returns Configuration object for the specified profile
18
+ */
19
+ static getProfileConfig(profile: keyof typeof PBKDF2_PROFILES): IPbkdf2Config;
20
+ /**
21
+ * Generate an options object for pbkdf2
22
+ * @param iterations Optional number of iterations (defaults to Pbkdf2IterationsPerSecond)
23
+ * @param saltBytes Optional salt size in bytes (defaults to PBKDF2.SALT_BYTES)
24
+ * @param hashBytes Optional hash size in bytes (defaults to ECIES.SYMMETRIC.KEY_SIZE)
25
+ * @param algorithm Optional hash algorithm (defaults to PBKDF2.ALGORITHM)
26
+ * @returns Configuration object for PBKDF2
27
+ */
28
+ static getConfig(iterations?: number, saltBytes?: number, hashBytes?: number, algorithm?: string): IPbkdf2Config;
29
+ /**
30
+ * Given a password, use pbkdf2 to generate an appropriately sized key for AES encryption
31
+ * @param password The password to derive a key from
32
+ * @param salt Optional salt (will be randomly generated if not provided)
33
+ * @param iterations Optional number of iterations
34
+ * @param saltBytes Optional salt size in bytes
35
+ * @param keySize Optional key size in bytes
36
+ * @param algorithm Optional hash algorithm
37
+ * @returns Object containing the derived key, salt, and iteration count
38
+ */
39
+ static deriveKeyFromPasswordAsync(password: Uint8Array, salt?: Uint8Array, iterations?: number, saltBytes?: number, keySize?: number, algorithm?: string): Promise<IPbkdf2Result>;
40
+ /**
41
+ * Derive a key using a predefined configuration profile
42
+ * @param password The password to derive a key from
43
+ * @param profile The configuration profile to use
44
+ * @param salt Optional salt (will be randomly generated if not provided)
45
+ * @returns Object containing the derived key, salt, and iteration count
46
+ */
47
+ static deriveKeyFromPasswordWithProfileAsync(password: Uint8Array, profile: Pbkdf2ProfileEnum, salt?: Uint8Array): Promise<IPbkdf2Result>;
48
+ }
49
+ //# sourceMappingURL=pbkdf2.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbkdf2.d.ts","sourceRoot":"","sources":["../../src/services/pbkdf2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiB,eAAe,EAAE,MAAM,cAAc,CAAC;AAE9D,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AAEnE,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D;;;;;;;GAOG;AACH,8BAAsB,aAAa;IACjC;;;;OAIG;WACW,gBAAgB,CAC5B,OAAO,EAAE,MAAM,OAAO,eAAe,GACpC,aAAa;IAUhB;;;;;;;OAOG;WACW,SAAS,CACrB,UAAU,CAAC,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,MAAM,EAClB,SAAS,CAAC,EAAE,MAAM,EAClB,SAAS,CAAC,EAAE,MAAM,GACjB,aAAa;IAkBhB;;;;;;;;;OASG;WACiB,0BAA0B,CAC5C,QAAQ,EAAE,UAAU,EACpB,IAAI,CAAC,EAAE,UAAU,EACjB,UAAU,CAAC,EAAE,MAAM,EACnB,SAAS,CAAC,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,MAAM,EAChB,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,aAAa,CAAC;IA8CzB;;;;;;OAMG;WACiB,qCAAqC,CACvD,QAAQ,EAAE,UAAU,EACpB,OAAO,EAAE,iBAAiB,EAC1B,IAAI,CAAC,EAAE,UAAU,GAChB,OAAO,CAAC,aAAa,CAAC;CAW1B"}
@@ -0,0 +1,97 @@
1
+ import { ECIES, PBKDF2, PBKDF2_PROFILES } from '../constants';
2
+ import { Pbkdf2ErrorType } from '../enumerations/pbkdf2-error-type';
3
+ import { Pbkdf2Error } from '../errors/pbkdf2';
4
+ /**
5
+ * Service for handling PBKDF2 (Password-Based Key Derivation Function 2) operations.
6
+ * This service provides functionality for:
7
+ * - Generating secure key derivation configurations
8
+ * - Deriving cryptographic keys from passwords
9
+ * - Managing salt and iteration parameters
10
+ * - Both synchronous and asynchronous key derivation
11
+ */
12
+ export class Pbkdf2Service {
13
+ /**
14
+ * Get a predefined configuration profile for common use cases
15
+ * @param profile The name of the profile to use
16
+ * @returns Configuration object for the specified profile
17
+ */
18
+ static getProfileConfig(profile) {
19
+ const profileConfig = PBKDF2_PROFILES[profile];
20
+ return {
21
+ hashBytes: profileConfig.hashBytes,
22
+ saltBytes: profileConfig.saltBytes,
23
+ iterations: profileConfig.iterations,
24
+ algorithm: profileConfig.algorithm,
25
+ };
26
+ }
27
+ /**
28
+ * Generate an options object for pbkdf2
29
+ * @param iterations Optional number of iterations (defaults to Pbkdf2IterationsPerSecond)
30
+ * @param saltBytes Optional salt size in bytes (defaults to PBKDF2.SALT_BYTES)
31
+ * @param hashBytes Optional hash size in bytes (defaults to ECIES.SYMMETRIC.KEY_SIZE)
32
+ * @param algorithm Optional hash algorithm (defaults to PBKDF2.ALGORITHM)
33
+ * @returns Configuration object for PBKDF2
34
+ */
35
+ static getConfig(iterations, saltBytes, hashBytes, algorithm) {
36
+ // larger numbers mean better security, less
37
+ return {
38
+ // size of the generated hash
39
+ hashBytes: hashBytes ?? ECIES.SYMMETRIC.KEY_SIZE,
40
+ // larger salt means hashed passwords are more resistant to rainbow table, but
41
+ // you get diminishing returns pretty fast
42
+ saltBytes: saltBytes ?? PBKDF2.SALT_BYTES,
43
+ // more iterations means an attacker has to take longer to brute force an
44
+ // individual password, so larger is better. however, larger also means longer
45
+ // to hash the password. tune so that hashing the password takes about a
46
+ // second
47
+ iterations: iterations ?? PBKDF2.ITERATIONS_PER_SECOND,
48
+ // hash algorithm
49
+ algorithm: algorithm ?? PBKDF2.ALGORITHM,
50
+ };
51
+ }
52
+ /**
53
+ * Given a password, use pbkdf2 to generate an appropriately sized key for AES encryption
54
+ * @param password The password to derive a key from
55
+ * @param salt Optional salt (will be randomly generated if not provided)
56
+ * @param iterations Optional number of iterations
57
+ * @param saltBytes Optional salt size in bytes
58
+ * @param keySize Optional key size in bytes
59
+ * @param algorithm Optional hash algorithm
60
+ * @returns Object containing the derived key, salt, and iteration count
61
+ */
62
+ static async deriveKeyFromPasswordAsync(password, salt, iterations, saltBytes, keySize, algorithm) {
63
+ const config = Pbkdf2Service.getConfig(iterations, saltBytes, keySize, algorithm);
64
+ const saltBytes_ = salt ?? crypto.getRandomValues(new Uint8Array(config.saltBytes));
65
+ if (saltBytes_.length !== config.saltBytes) {
66
+ throw new Pbkdf2Error(Pbkdf2ErrorType.InvalidSaltLength);
67
+ }
68
+ const keyMaterial = await crypto.subtle.importKey('raw', new Uint8Array(password), 'PBKDF2', false, ['deriveBits']);
69
+ const keyArray = await crypto.subtle.deriveBits({
70
+ name: 'PBKDF2',
71
+ salt: new Uint8Array(saltBytes_),
72
+ iterations: config.iterations,
73
+ hash: config.algorithm,
74
+ }, keyMaterial, config.hashBytes * 8);
75
+ const keyBytes = new Uint8Array(keyArray);
76
+ if (keyBytes.length !== config.hashBytes) {
77
+ throw new Pbkdf2Error(Pbkdf2ErrorType.InvalidHashLength);
78
+ }
79
+ return {
80
+ salt: saltBytes_,
81
+ hash: keyBytes,
82
+ iterations: config.iterations,
83
+ };
84
+ }
85
+ /**
86
+ * Derive a key using a predefined configuration profile
87
+ * @param password The password to derive a key from
88
+ * @param profile The configuration profile to use
89
+ * @param salt Optional salt (will be randomly generated if not provided)
90
+ * @returns Object containing the derived key, salt, and iteration count
91
+ */
92
+ static async deriveKeyFromPasswordWithProfileAsync(password, profile, salt) {
93
+ const config = Pbkdf2Service.getProfileConfig(profile);
94
+ return await Pbkdf2Service.deriveKeyFromPasswordAsync(password, salt, config.iterations, config.saltBytes, config.hashBytes, config.algorithm);
95
+ }
96
+ }
97
+ //# sourceMappingURL=pbkdf2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pbkdf2.js","sourceRoot":"","sources":["../../src/services/pbkdf2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAI/C;;;;;;;GAOG;AACH,MAAM,OAAgB,aAAa;IACjC;;;;OAIG;IACI,MAAM,CAAC,gBAAgB,CAC5B,OAAqC;QAErC,MAAM,aAAa,GAAG,eAAe,CAAC,OAAO,CAAC,CAAC;QAC/C,OAAO;YACL,SAAS,EAAE,aAAa,CAAC,SAAS;YAClC,SAAS,EAAE,aAAa,CAAC,SAAS;YAClC,UAAU,EAAE,aAAa,CAAC,UAAU;YACpC,SAAS,EAAE,aAAa,CAAC,SAAS;SACnC,CAAC;IACJ,CAAC;IAED;;;;;;;OAOG;IACI,MAAM,CAAC,SAAS,CACrB,UAAmB,EACnB,SAAkB,EAClB,SAAkB,EAClB,SAAkB;QAElB,4CAA4C;QAC5C,OAAO;YACL,6BAA6B;YAC7B,SAAS,EAAE,SAAS,IAAI,KAAK,CAAC,SAAS,CAAC,QAAQ;YAChD,8EAA8E;YAC9E,0CAA0C;YAC1C,SAAS,EAAE,SAAS,IAAI,MAAM,CAAC,UAAU;YACzC,yEAAyE;YACzE,8EAA8E;YAC9E,wEAAwE;YACxE,SAAS;YACT,U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AAC,iBAAiB,CAAC,CAAC;QAC3D,CAAC;QAED,OAAO;YACL,IAAI,EAAE,UAAU;YAChB,IAAI,EAAE,QAAQ;YACd,UAAU,EAAE,MAAM,CAAC,UAAU;SAC9B,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,qCAAqC,CACvD,QAAoB,EACpB,OAA0B,EAC1B,IAAiB;QAEjB,MAAM,MAAM,GAAG,aAAa,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;QACvD,OAAO,MAAM,aAAa,CAAC,0BAA0B,CACnD,QAAQ,EACR,IAAI,EACJ,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,SAAS,EAChB,MAAM,CAAC,SAAS,EAChB,MAAM,CAAC,SAAS,CACjB,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * A simple and fast XOR cipher for obfuscating data in memory for browser environments.
3
+ * This is not a substitute for strong cryptography but provides a lightweight way
4
+ * to prevent sensitive data from being stored in plaintext in memory.
5
+ */
6
+ export declare class XorService {
7
+ /**
8
+ * Encrypts or decrypts data using a simple XOR cipher.
9
+ * The key is XORed with the data. The same function is used for
10
+ * both encryption and decryption.
11
+ *
12
+ * @param data The data to process.
13
+ * @param key The key to use for the XOR operation.
14
+ * @returns A new Uint8Array containing the result of the XOR operation.
15
+ */
16
+ static xor(data: Uint8Array, key: Uint8Array): Uint8Array;
17
+ /**
18
+ * Generates a random key of a specified length.
19
+ * @param length The length of the key in bytes.
20
+ * @returns A Uint8Array containing the random key.
21
+ * @throws Error if crypto API is not available.
22
+ */
23
+ static generateKey(length: number): Uint8Array;
24
+ /**
25
+ * Helper method to convert a string to a Uint8Array.
26
+ * @param str The string to convert.
27
+ * @returns A Uint8Array representation of the string.
28
+ */
29
+ static stringToBytes(str: string): Uint8Array;
30
+ /**
31
+ * Helper method to convert a Uint8Array to a string.
32
+ * @param bytes The Uint8Array to convert.
33
+ * @returns A string representation of the Uint8Array.
34
+ */
35
+ static bytesToString(bytes: Uint8Array): string;
36
+ }
37
+ //# sourceMappingURL=xor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xor.d.ts","sourceRoot":"","sources":["../../src/services/xor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,qBAAa,UAAU;IACrB;;;;;;;;OAQG;WACW,GAAG,CAAC,IAAI,EAAE,UAAU,EAAE,GAAG,EAAE,UAAU,GAAG,UAAU;IAQhE;;;;;OAKG;WACW,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU;IAgBrD;;;;OAIG;WACW,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU;IAKpD;;;;OAIG;WACW,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM;CAIvD"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * A simple and fast XOR cipher for obfuscating data in memory for browser environments.
3
+ * This is not a substitute for strong cryptography but provides a lightweight way
4
+ * to prevent sensitive data from being stored in plaintext in memory.
5
+ */
6
+ export class XorService {
7
+ /**
8
+ * Encrypts or decrypts data using a simple XOR cipher.
9
+ * The key is XORed with the data. The same function is used for
10
+ * both encryption and decryption.
11
+ *
12
+ * @param data The data to process.
13
+ * @param key The key to use for the XOR operation.
14
+ * @returns A new Uint8Array containing the result of the XOR operation.
15
+ */
16
+ static xor(data, key) {
17
+ const result = new Uint8Array(data.length);
18
+ for (let i = 0; i < data.length; i++) {
19
+ result[i] = data[i] ^ key[i % key.length];
20
+ }
21
+ return result;
22
+ }
23
+ /**
24
+ * Generates a random key of a specified length.
25
+ * @param length The length of the key in bytes.
26
+ * @returns A Uint8Array containing the random key.
27
+ * @throws Error if crypto API is not available.
28
+ */
29
+ static generateKey(length) {
30
+ const randomBytes = new Uint8Array(length);
31
+ // Check if running in a browser with crypto support
32
+ if (typeof window !== 'undefined' && window.crypto) {
33
+ window.crypto.getRandomValues(randomBytes);
34
+ }
35
+ else if (typeof crypto !== 'undefined') {
36
+ // For environments where crypto is global (some newer JS runtimes)
37
+ crypto.getRandomValues(randomBytes);
38
+ }
39
+ else {
40
+ throw new Error('Crypto API not available in this environment');
41
+ }
42
+ return randomBytes;
43
+ }
44
+ /**
45
+ * Helper method to convert a string to a Uint8Array.
46
+ * @param str The string to convert.
47
+ * @returns A Uint8Array representation of the string.
48
+ */
49
+ static stringToBytes(str) {
50
+ const encoder = new TextEncoder();
51
+ return encoder.encode(str);
52
+ }
53
+ /**
54
+ * Helper method to convert a Uint8Array to a string.
55
+ * @param bytes The Uint8Array to convert.
56
+ * @returns A string representation of the Uint8Array.
57
+ */
58
+ static bytesToString(bytes) {
59
+ const decoder = new TextDecoder();
60
+ return decoder.decode(bytes);
61
+ }
62
+ }
63
+ //# sourceMappingURL=xor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xor.js","sourceRoot":"","sources":["../../src/services/xor.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,OAAO,UAAU;IACrB;;;;;;;;OAQG;IACI,MAAM,CAAC,GAAG,CAAC,IAAgB,EAAE,GAAe;QACjD,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC3C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,MAAM,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC;QAC5C,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,WAAW,CAAC,MAAc;QACtC,MAAM,WAAW,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;QAE3C,oDAAoD;QACpD,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YACnD,MAAM,CAAC,MAAM,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC;QAC7C,CAAC;aAAM,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;YACzC,mEAAmE;YACnE,MAAM,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC;QACtC,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAClE,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,GAAW;QACrC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QAClC,OAAO,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,aAAa,CAAC,KAAiB;QAC3C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;QAClC,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;CACF"}
@@ -0,0 +1,29 @@
1
+ import { Brand } from 'ts-brand';
2
+ import { GuidBrandType } from './enumerations';
3
+ /**
4
+ * GUID stored as a BigInt
5
+ */
6
+ export type BigIntGuid = Brand<bigint, 'GuidV4', GuidBrandType.BigIntGuid>;
7
+ /**
8
+ * GUID stored as a hex string with dashes
9
+ */
10
+ export type FullHexGuid = Brand<string, 'GuidV4', GuidBrandType.FullHexGuid>;
11
+ /**
12
+ * GUID stored as a hex string without dashes
13
+ */
14
+ export type ShortHexGuid = Brand<string, 'GuidV4', GuidBrandType.ShortHexGuid>;
15
+ /**
16
+ * GUID stored as a base64 string
17
+ */
18
+ export type Base64Guid = Brand<string, 'GuidV4', GuidBrandType.Base64Guid>;
19
+ /**
20
+ * GUID stored as a raw buffer
21
+ */
22
+ export type RawGuidUint8Array = Uint8Array & Brand<Uint8Array, 'GuidV4', GuidBrandType.RawGuidUint8Array>;
23
+ export type BinaryGuid = Uint8Array;
24
+ export type HexGuid = string;
25
+ export type ShortGuid = string;
26
+ export type SignatureUint8Array = Uint8Array & Brand<Uint8Array, 'SignatureArray'>;
27
+ export type ChecksumUint8Array = Uint8Array & Brand<Uint8Array, 'Sha3Checksum', 'ChecksumArray'>;
28
+ export type SignatureString = string & Brand<string, 'SignatureString'>;
29
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;AAC3E;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;AAC7E;;GAEG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;AAC/E;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,EAAE,QAAQ,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;AAC3E;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,UAAU,GACxC,KAAK,CAAC,UAAU,EAAE,QAAQ,EAAE,aAAa,CAAC,iBAAiB,CAAC,CAAC;AAG/D,MAAM,MAAM,UAAU,GAAG,UAAU,CAAC;AACpC,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAC7B,MAAM,MAAM,SAAS,GAAG,MAAM,CAAC;AAE/B,MAAM,MAAM,mBAAmB,GAAG,UAAU,GAC1C,KAAK,CAAC,UAAU,EAAE,gBAAgB,CAAC,CAAC;AACtC,MAAM,MAAM,kBAAkB,GAAG,UAAU,GACzC,KAAK,CAAC,UAAU,EAAE,cAAc,EAAE,eAAe,CAAC,CAAC;AACrD,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,KAAK,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
@@ -0,0 +1,62 @@
1
+ import { LengthEncodingType } from './enumerations/length-encoding-type';
2
+ /**
3
+ * Encodes the length of the data in the buffer
4
+ * @param buffer The buffer to encode
5
+ * @returns The encoded buffer
6
+ */
7
+ export declare function lengthEncodeData(buffer: Buffer): Buffer;
8
+ export declare function decodeLengthEncodedData(buffer: Buffer): {
9
+ data: Buffer;
10
+ totalLength: number;
11
+ };
12
+ export declare function uint8ArrayToBase64(uint8Array: Uint8Array): string;
13
+ export declare function base64ToUint8Array(base64String: string): Uint8Array;
14
+ export declare function uint8ArrayToHex(uint8Array: Uint8Array): string;
15
+ export declare function hexToUint8Array(hexString: string): Uint8Array;
16
+ /**
17
+ * Utility functions for browser ECIES implementation
18
+ */
19
+ /**
20
+ * CRC16-CCITT implementation for data integrity checking
21
+ * Uses CRC16-CCITT-FALSE variant (init 0xFFFF)
22
+ */
23
+ export declare function crc16(data: Uint8Array): Uint8Array;
24
+ /**
25
+ * Convert string to Uint8Array (UTF-8 encoding)
26
+ */
27
+ export declare function stringToUint8Array(str: string): Uint8Array;
28
+ /**
29
+ * Convert Uint8Array to string (UTF-8 decoding)
30
+ */
31
+ export declare function uint8ArrayToString(array: Uint8Array): string;
32
+ /**
33
+ * Secure random bytes generation
34
+ */
35
+ export declare function randomBytes(length: number): Uint8Array;
36
+ /**
37
+ * Compare two Uint8Arrays for equality
38
+ */
39
+ export declare function arraysEqual(a: Uint8Array, b: Uint8Array): boolean;
40
+ /**
41
+ * Concatenate multiple Uint8Arrays
42
+ */
43
+ export declare function concatUint8Arrays(...arrays: Uint8Array[]): Uint8Array;
44
+ /**
45
+ * Get the length encoding type for a given length
46
+ * @param length The length to evaluate
47
+ * @returns The corresponding LengthEncodingType
48
+ */
49
+ export declare function getLengthEncodingTypeForLength(length: number | BigInt): LengthEncodingType;
50
+ /**
51
+ * Get the length encoding type for a given value
52
+ * @param value The value to evaluate
53
+ * @returns The corresponding LengthEncodingType
54
+ */
55
+ export declare function getLengthEncodingTypeFromValue(value: number): LengthEncodingType;
56
+ /**
57
+ * Get the length in bytes for a given LengthEncodingType
58
+ * @param type The LengthEncodingType to evaluate
59
+ * @returns The length in bytes
60
+ */
61
+ export declare function getLengthForLengthType(type: LengthEncodingType): number;
62
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAIzE;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAuBvD;AAED,wBAAgB,uBAAuB,CAAC,MAAM,EAAE,MAAM,GAAG;IACvD,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;CACrB,CAyDA;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAMjE;AAED,wBAAgB,kBAAkB,CAAC,YAAY,EAAE,MAAM,GAAG,UAAU,CAQnE;AAED,wBAAgB,eAAe,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAI9D;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,CAO7D;AAED;;GAEG;AAEH;;;GAGG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,UAAU,CAoBlD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAE1D;AA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