@digitaldefiance/ecies-lib 4.4.2 → 4.4.4

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 (589) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +9 -6
  3. package/src/builders/ecies-builder.ts +39 -0
  4. package/src/builders/{index.js → index.ts} +1 -1
  5. package/src/builders/member-builder.ts +155 -0
  6. package/src/constants.ts +609 -0
  7. package/src/core/errors/crypto-error.ts +78 -0
  8. package/src/core/{index.js → index.ts} +1 -1
  9. package/src/core/types/result.ts +19 -0
  10. package/src/email-string.ts +82 -0
  11. package/src/enumerations/disposed-error-type.ts +11 -0
  12. package/src/enumerations/ecies-cipher-suite.ts +4 -0
  13. package/src/enumerations/ecies-encryption-type.ts +41 -0
  14. package/src/enumerations/ecies-error-type.ts +43 -0
  15. package/src/enumerations/ecies-string-key.ts +205 -0
  16. package/src/enumerations/ecies-version.ts +3 -0
  17. package/src/enumerations/guid-brand-type.ts +26 -0
  18. package/src/enumerations/guid-error-type.ts +6 -0
  19. package/src/enumerations/id-provider-error-type.ts +50 -0
  20. package/src/enumerations/{index.js → index.ts} +0 -1
  21. package/src/enumerations/invalid-email-type.ts +5 -0
  22. package/src/enumerations/length-encoding-type.ts +6 -0
  23. package/src/enumerations/length-error-type.ts +5 -0
  24. package/src/enumerations/member-error-type.ts +106 -0
  25. package/src/enumerations/{member-type.d.ts → member-type.ts} +7 -6
  26. package/src/enumerations/password-login-error-type.ts +4 -0
  27. package/src/enumerations/pbkdf2-error-type.ts +5 -0
  28. package/src/enumerations/pbkdf2-profile.ts +5 -0
  29. package/src/enumerations/secure-storage-error-type.ts +5 -0
  30. package/src/errors/disposed.ts +36 -0
  31. package/src/errors/ecies.ts +153 -0
  32. package/src/errors/guid.ts +130 -0
  33. package/src/errors/id-provider.ts +40 -0
  34. package/src/errors/{index.d.ts → index.ts} +0 -1
  35. package/src/errors/invalid-email.ts +23 -0
  36. package/src/errors/length.ts +19 -0
  37. package/src/errors/member.ts +20 -0
  38. package/src/errors/pbkdf2.ts +20 -0
  39. package/src/errors/secure-storage.ts +17 -0
  40. package/src/errors/simple-ecies.ts +21 -0
  41. package/src/errors/simple-test-error.ts +6 -0
  42. package/src/i18n-setup.ts +130 -0
  43. package/src/{index.js → index.ts} +64 -7
  44. package/src/interfaces/checksum-config.ts +4 -0
  45. package/src/interfaces/checksum-consts.ts +13 -0
  46. package/src/interfaces/configuration-provenance.ts +54 -0
  47. package/src/interfaces/constants.ts +75 -0
  48. package/src/interfaces/ecies-config.ts +8 -0
  49. package/src/interfaces/ecies-consts.ts +74 -0
  50. package/src/interfaces/ecies-file-service.ts +6 -0
  51. package/src/interfaces/encrypted-chunk.ts +64 -0
  52. package/src/interfaces/encryption-state.ts +19 -0
  53. package/src/interfaces/frontend-member-operational.ts +77 -0
  54. package/src/interfaces/guid.ts +86 -0
  55. package/src/interfaces/id-provider.ts +152 -0
  56. package/src/interfaces/{index.d.ts → index.ts} +0 -1
  57. package/src/interfaces/invariant.ts +60 -0
  58. package/src/interfaces/library-error.ts +23 -0
  59. package/src/interfaces/{member-storage.d.ts → member-storage.ts} +11 -10
  60. package/src/interfaces/{member-with-mnemonic.d.ts → member-with-mnemonic.ts} +3 -3
  61. package/src/interfaces/member.ts +84 -0
  62. package/src/interfaces/multi-recipient-chunk.ts +61 -0
  63. package/src/interfaces/pbkdf2-config.ts +6 -0
  64. package/src/interfaces/pbkdf2-consts.ts +10 -0
  65. package/src/interfaces/pbkdf2-result.ts +5 -0
  66. package/src/interfaces/stream-config.ts +17 -0
  67. package/src/interfaces/stream-header.ts +34 -0
  68. package/src/interfaces/stream-progress.ts +31 -0
  69. package/src/lib/configuration-provenance-utils.ts +26 -0
  70. package/src/lib/crypto-container.ts +64 -0
  71. package/src/lib/guid.ts +1097 -0
  72. package/src/lib/id-providers/custom-provider.ts +109 -0
  73. package/src/lib/id-providers/guidv4-provider.ts +141 -0
  74. package/src/lib/id-providers/{index.d.ts → index.ts} +6 -5
  75. package/src/lib/id-providers/objectid-provider.ts +125 -0
  76. package/src/lib/id-providers/uuid-provider.ts +133 -0
  77. package/src/lib/{index.js → index.ts} +2 -2
  78. package/src/lib/invariant-validator.ts +133 -0
  79. package/src/lib/invariants/encryption-algorithm-consistency.ts +73 -0
  80. package/src/lib/invariants/{index.js → index.ts} +0 -1
  81. package/src/lib/invariants/pbkdf2-profiles-validity.ts +78 -0
  82. package/src/lib/invariants/recipient-id-consistency.ts +46 -0
  83. package/src/lib/multi-recipient-chunk-utils.ts +63 -0
  84. package/src/member.ts +495 -0
  85. package/src/{pbkdf2-profiles.d.ts → pbkdf2-profiles.ts} +2 -2
  86. package/src/phone-number.ts +18 -0
  87. package/src/regexes.ts +10 -0
  88. package/src/secure-buffer.ts +226 -0
  89. package/src/secure-string.ts +244 -0
  90. package/src/services/aes-gcm.ts +220 -0
  91. package/src/services/chunk-processor.ts +188 -0
  92. package/src/services/ecies/README.md +147 -0
  93. package/src/services/ecies/crypto-core.ts +292 -0
  94. package/src/services/ecies/example.ts +185 -0
  95. package/src/services/ecies/file.ts +167 -0
  96. package/src/services/ecies/{index.js → index.ts} +2 -1
  97. package/src/services/ecies/integration.ts +241 -0
  98. package/src/services/ecies/interfaces.ts +62 -0
  99. package/src/services/ecies/manual-test.ts +219 -0
  100. package/src/services/ecies/multi-recipient.ts +545 -0
  101. package/src/services/ecies/service.ts +370 -0
  102. package/src/services/ecies/signature.ts +93 -0
  103. package/src/services/ecies/single-recipient.ts +476 -0
  104. package/src/services/encryption-stream.ts +435 -0
  105. package/src/services/{index.js → index.ts} +0 -1
  106. package/src/services/multi-recipient-processor.ts +377 -0
  107. package/src/services/password-login.ts +226 -0
  108. package/src/services/pbkdf2.ts +169 -0
  109. package/src/services/progress-tracker.ts +128 -0
  110. package/src/services/resumable-encryption.ts +135 -0
  111. package/src/services/xor.ts +65 -0
  112. package/src/test-mocks/index.ts +1 -0
  113. package/src/test-mocks/mock-frontend-member.ts +276 -0
  114. package/src/{testing.js → testing.ts} +0 -1
  115. package/src/translations/{de.js → de.ts} +112 -56
  116. package/src/translations/{en-US.js → en-US.ts} +116 -58
  117. package/src/translations/{es.js → es.ts} +134 -67
  118. package/src/translations/{fr.js → fr.ts} +129 -64
  119. package/src/translations/{ja.js → ja.ts} +111 -55
  120. package/src/translations/{uk.js → uk.ts} +132 -67
  121. package/src/translations/{zh-cn.js → zh-cn.ts} +60 -29
  122. package/src/types/deep-partial.ts +11 -0
  123. package/src/{types.d.ts → types.ts} +15 -7
  124. package/src/utils/encryption-type-utils.ts +76 -0
  125. package/src/utils.ts +329 -0
  126. package/src/builders/ecies-builder.d.ts +0 -18
  127. package/src/builders/ecies-builder.d.ts.map +0 -1
  128. package/src/builders/ecies-builder.js +0 -30
  129. package/src/builders/ecies-builder.js.map +0 -1
  130. package/src/builders/index.d.ts +0 -6
  131. package/src/builders/index.d.ts.map +0 -1
  132. package/src/builders/index.js.map +0 -1
  133. package/src/builders/member-builder.d.ts +0 -51
  134. package/src/builders/member-builder.d.ts.map +0 -1
  135. package/src/builders/member-builder.js +0 -97
  136. package/src/builders/member-builder.js.map +0 -1
  137. package/src/constants.d.ts +0 -60
  138. package/src/constants.d.ts.map +0 -1
  139. package/src/constants.js +0 -446
  140. package/src/constants.js.map +0 -1
  141. package/src/core/errors/crypto-error.d.ts +0 -34
  142. package/src/core/errors/crypto-error.d.ts.map +0 -1
  143. package/src/core/errors/crypto-error.js +0 -56
  144. package/src/core/errors/crypto-error.js.map +0 -1
  145. package/src/core/index.d.ts +0 -6
  146. package/src/core/index.d.ts.map +0 -1
  147. package/src/core/index.js.map +0 -1
  148. package/src/core/types/result.d.ts +0 -16
  149. package/src/core/types/result.d.ts.map +0 -1
  150. package/src/core/types/result.js +0 -12
  151. package/src/core/types/result.js.map +0 -1
  152. package/src/email-string.d.ts +0 -42
  153. package/src/email-string.d.ts.map +0 -1
  154. package/src/email-string.js +0 -67
  155. package/src/email-string.js.map +0 -1
  156. package/src/enumerations/disposed-error-type.d.ts +0 -12
  157. package/src/enumerations/disposed-error-type.d.ts.map +0 -1
  158. package/src/enumerations/disposed-error-type.js +0 -13
  159. package/src/enumerations/disposed-error-type.js.map +0 -1
  160. package/src/enumerations/ecies-cipher-suite.d.ts +0 -4
  161. package/src/enumerations/ecies-cipher-suite.d.ts.map +0 -1
  162. package/src/enumerations/ecies-cipher-suite.js +0 -6
  163. package/src/enumerations/ecies-cipher-suite.js.map +0 -1
  164. package/src/enumerations/ecies-encryption-type.d.ts +0 -11
  165. package/src/enumerations/ecies-encryption-type.d.ts.map +0 -1
  166. package/src/enumerations/ecies-encryption-type.js +0 -27
  167. package/src/enumerations/ecies-encryption-type.js.map +0 -1
  168. package/src/enumerations/ecies-error-type.d.ts +0 -44
  169. package/src/enumerations/ecies-error-type.d.ts.map +0 -1
  170. package/src/enumerations/ecies-error-type.js +0 -45
  171. package/src/enumerations/ecies-error-type.js.map +0 -1
  172. package/src/enumerations/ecies-string-key.d.ts +0 -192
  173. package/src/enumerations/ecies-string-key.d.ts.map +0 -1
  174. package/src/enumerations/ecies-string-key.js +0 -199
  175. package/src/enumerations/ecies-string-key.js.map +0 -1
  176. package/src/enumerations/ecies-version.d.ts +0 -4
  177. package/src/enumerations/ecies-version.d.ts.map +0 -1
  178. package/src/enumerations/ecies-version.js +0 -5
  179. package/src/enumerations/ecies-version.js.map +0 -1
  180. package/src/enumerations/guid-brand-type.d.ts +0 -27
  181. package/src/enumerations/guid-brand-type.d.ts.map +0 -1
  182. package/src/enumerations/guid-brand-type.js +0 -28
  183. package/src/enumerations/guid-brand-type.js.map +0 -1
  184. package/src/enumerations/guid-error-type.d.ts +0 -7
  185. package/src/enumerations/guid-error-type.d.ts.map +0 -1
  186. package/src/enumerations/guid-error-type.js +0 -8
  187. package/src/enumerations/guid-error-type.js.map +0 -1
  188. package/src/enumerations/id-provider-error-type.d.ts +0 -43
  189. package/src/enumerations/id-provider-error-type.d.ts.map +0 -1
  190. package/src/enumerations/id-provider-error-type.js +0 -44
  191. package/src/enumerations/id-provider-error-type.js.map +0 -1
  192. package/src/enumerations/index.d.ts +0 -18
  193. package/src/enumerations/index.d.ts.map +0 -1
  194. package/src/enumerations/index.js.map +0 -1
  195. package/src/enumerations/invalid-email-type.d.ts +0 -6
  196. package/src/enumerations/invalid-email-type.d.ts.map +0 -1
  197. package/src/enumerations/invalid-email-type.js +0 -7
  198. package/src/enumerations/invalid-email-type.js.map +0 -1
  199. package/src/enumerations/length-encoding-type.d.ts +0 -7
  200. package/src/enumerations/length-encoding-type.d.ts.map +0 -1
  201. package/src/enumerations/length-encoding-type.js +0 -8
  202. package/src/enumerations/length-encoding-type.js.map +0 -1
  203. package/src/enumerations/length-error-type.d.ts +0 -6
  204. package/src/enumerations/length-error-type.d.ts.map +0 -1
  205. package/src/enumerations/length-error-type.js +0 -7
  206. package/src/enumerations/length-error-type.js.map +0 -1
  207. package/src/enumerations/member-error-type.d.ts +0 -87
  208. package/src/enumerations/member-error-type.d.ts.map +0 -1
  209. package/src/enumerations/member-error-type.js +0 -88
  210. package/src/enumerations/member-error-type.js.map +0 -1
  211. package/src/enumerations/member-type.d.ts.map +0 -1
  212. package/src/enumerations/member-type.js +0 -16
  213. package/src/enumerations/member-type.js.map +0 -1
  214. package/src/enumerations/password-login-error-type.d.ts +0 -5
  215. package/src/enumerations/password-login-error-type.d.ts.map +0 -1
  216. package/src/enumerations/password-login-error-type.js +0 -6
  217. package/src/enumerations/password-login-error-type.js.map +0 -1
  218. package/src/enumerations/pbkdf2-error-type.d.ts +0 -6
  219. package/src/enumerations/pbkdf2-error-type.d.ts.map +0 -1
  220. package/src/enumerations/pbkdf2-error-type.js +0 -7
  221. package/src/enumerations/pbkdf2-error-type.js.map +0 -1
  222. package/src/enumerations/pbkdf2-profile.d.ts +0 -6
  223. package/src/enumerations/pbkdf2-profile.d.ts.map +0 -1
  224. package/src/enumerations/pbkdf2-profile.js +0 -7
  225. package/src/enumerations/pbkdf2-profile.js.map +0 -1
  226. package/src/enumerations/secure-storage-error-type.d.ts +0 -6
  227. package/src/enumerations/secure-storage-error-type.d.ts.map +0 -1
  228. package/src/enumerations/secure-storage-error-type.js +0 -7
  229. package/src/enumerations/secure-storage-error-type.js.map +0 -1
  230. package/src/errors/disposed.d.ts +0 -22
  231. package/src/errors/disposed.d.ts.map +0 -1
  232. package/src/errors/disposed.js +0 -28
  233. package/src/errors/disposed.js.map +0 -1
  234. package/src/errors/ecies.d.ts +0 -52
  235. package/src/errors/ecies.d.ts.map +0 -1
  236. package/src/errors/ecies.js +0 -78
  237. package/src/errors/ecies.js.map +0 -1
  238. package/src/errors/guid.d.ts +0 -49
  239. package/src/errors/guid.d.ts.map +0 -1
  240. package/src/errors/guid.js +0 -96
  241. package/src/errors/guid.js.map +0 -1
  242. package/src/errors/id-provider.d.ts +0 -23
  243. package/src/errors/id-provider.d.ts.map +0 -1
  244. package/src/errors/id-provider.js +0 -29
  245. package/src/errors/id-provider.js.map +0 -1
  246. package/src/errors/index.d.ts.map +0 -1
  247. package/src/errors/index.js +0 -10
  248. package/src/errors/index.js.map +0 -1
  249. package/src/errors/invalid-email.d.ts +0 -8
  250. package/src/errors/invalid-email.d.ts.map +0 -1
  251. package/src/errors/invalid-email.js +0 -15
  252. package/src/errors/invalid-email.js.map +0 -1
  253. package/src/errors/length.d.ts +0 -7
  254. package/src/errors/length.d.ts.map +0 -1
  255. package/src/errors/length.js +0 -11
  256. package/src/errors/length.js.map +0 -1
  257. package/src/errors/member.d.ts +0 -7
  258. package/src/errors/member.d.ts.map +0 -1
  259. package/src/errors/member.js +0 -11
  260. package/src/errors/member.js.map +0 -1
  261. package/src/errors/pbkdf2.d.ts +0 -7
  262. package/src/errors/pbkdf2.d.ts.map +0 -1
  263. package/src/errors/pbkdf2.js +0 -11
  264. package/src/errors/pbkdf2.js.map +0 -1
  265. package/src/errors/secure-storage.d.ts +0 -7
  266. package/src/errors/secure-storage.d.ts.map +0 -1
  267. package/src/errors/secure-storage.js +0 -12
  268. package/src/errors/secure-storage.js.map +0 -1
  269. package/src/errors/simple-ecies.d.ts +0 -6
  270. package/src/errors/simple-ecies.d.ts.map +0 -1
  271. package/src/errors/simple-ecies.js +0 -12
  272. package/src/errors/simple-ecies.js.map +0 -1
  273. package/src/errors/simple-test-error.d.ts +0 -4
  274. package/src/errors/simple-test-error.d.ts.map +0 -1
  275. package/src/errors/simple-test-error.js +0 -7
  276. package/src/errors/simple-test-error.js.map +0 -1
  277. package/src/i18n-setup.d.ts +0 -32
  278. package/src/i18n-setup.d.ts.map +0 -1
  279. package/src/i18n-setup.js +0 -101
  280. package/src/i18n-setup.js.map +0 -1
  281. package/src/index.d.ts +0 -80
  282. package/src/index.d.ts.map +0 -1
  283. package/src/index.js.map +0 -1
  284. package/src/interfaces/checksum-config.d.ts +0 -5
  285. package/src/interfaces/checksum-config.d.ts.map +0 -1
  286. package/src/interfaces/checksum-config.js +0 -2
  287. package/src/interfaces/checksum-config.js.map +0 -1
  288. package/src/interfaces/checksum-consts.d.ts +0 -11
  289. package/src/interfaces/checksum-consts.d.ts.map +0 -1
  290. package/src/interfaces/checksum-consts.js +0 -2
  291. package/src/interfaces/checksum-consts.js.map +0 -1
  292. package/src/interfaces/configuration-provenance.d.ts +0 -43
  293. package/src/interfaces/configuration-provenance.d.ts.map +0 -1
  294. package/src/interfaces/configuration-provenance.js +0 -3
  295. package/src/interfaces/configuration-provenance.js.map +0 -1
  296. package/src/interfaces/constants.d.ts +0 -70
  297. package/src/interfaces/constants.d.ts.map +0 -1
  298. package/src/interfaces/constants.js +0 -2
  299. package/src/interfaces/constants.js.map +0 -1
  300. package/src/interfaces/ecies-config.d.ts +0 -9
  301. package/src/interfaces/ecies-config.d.ts.map +0 -1
  302. package/src/interfaces/ecies-config.js +0 -2
  303. package/src/interfaces/ecies-config.js.map +0 -1
  304. package/src/interfaces/ecies-consts.d.ts +0 -61
  305. package/src/interfaces/ecies-consts.d.ts.map +0 -1
  306. package/src/interfaces/ecies-consts.js +0 -2
  307. package/src/interfaces/ecies-consts.js.map +0 -1
  308. package/src/interfaces/ecies-file-service.d.ts +0 -7
  309. package/src/interfaces/ecies-file-service.d.ts.map +0 -1
  310. package/src/interfaces/ecies-file-service.js +0 -2
  311. package/src/interfaces/ecies-file-service.js.map +0 -1
  312. package/src/interfaces/encrypted-chunk.d.ts +0 -55
  313. package/src/interfaces/encrypted-chunk.d.ts.map +0 -1
  314. package/src/interfaces/encrypted-chunk.js +0 -12
  315. package/src/interfaces/encrypted-chunk.js.map +0 -1
  316. package/src/interfaces/encryption-state.d.ts +0 -18
  317. package/src/interfaces/encryption-state.d.ts.map +0 -1
  318. package/src/interfaces/encryption-state.js +0 -2
  319. package/src/interfaces/encryption-state.js.map +0 -1
  320. package/src/interfaces/frontend-member-operational.d.ts +0 -51
  321. package/src/interfaces/frontend-member-operational.d.ts.map +0 -1
  322. package/src/interfaces/frontend-member-operational.js +0 -2
  323. package/src/interfaces/frontend-member-operational.js.map +0 -1
  324. package/src/interfaces/guid.d.ts +0 -78
  325. package/src/interfaces/guid.d.ts.map +0 -1
  326. package/src/interfaces/guid.js +0 -2
  327. package/src/interfaces/guid.js.map +0 -1
  328. package/src/interfaces/id-provider.d.ts +0 -107
  329. package/src/interfaces/id-provider.d.ts.map +0 -1
  330. package/src/interfaces/id-provider.js +0 -52
  331. package/src/interfaces/id-provider.js.map +0 -1
  332. package/src/interfaces/index.d.ts.map +0 -1
  333. package/src/interfaces/index.js +0 -13
  334. package/src/interfaces/index.js.map +0 -1
  335. package/src/interfaces/invariant.d.ts +0 -46
  336. package/src/interfaces/invariant.d.ts.map +0 -1
  337. package/src/interfaces/invariant.js +0 -18
  338. package/src/interfaces/invariant.js.map +0 -1
  339. package/src/interfaces/library-error.d.ts +0 -23
  340. package/src/interfaces/library-error.d.ts.map +0 -1
  341. package/src/interfaces/library-error.js +0 -2
  342. package/src/interfaces/library-error.js.map +0 -1
  343. package/src/interfaces/member-storage.d.ts.map +0 -1
  344. package/src/interfaces/member-storage.js +0 -2
  345. package/src/interfaces/member-storage.js.map +0 -1
  346. package/src/interfaces/member-with-mnemonic.d.ts.map +0 -1
  347. package/src/interfaces/member-with-mnemonic.js +0 -2
  348. package/src/interfaces/member-with-mnemonic.js.map +0 -1
  349. package/src/interfaces/member.d.ts +0 -55
  350. package/src/interfaces/member.d.ts.map +0 -1
  351. package/src/interfaces/member.js +0 -2
  352. package/src/interfaces/member.js.map +0 -1
  353. package/src/interfaces/multi-recipient-chunk.d.ts +0 -54
  354. package/src/interfaces/multi-recipient-chunk.d.ts.map +0 -1
  355. package/src/interfaces/multi-recipient-chunk.js +0 -11
  356. package/src/interfaces/multi-recipient-chunk.js.map +0 -1
  357. package/src/interfaces/pbkdf2-config.d.ts +0 -7
  358. package/src/interfaces/pbkdf2-config.d.ts.map +0 -1
  359. package/src/interfaces/pbkdf2-config.js +0 -2
  360. package/src/interfaces/pbkdf2-config.js.map +0 -1
  361. package/src/interfaces/pbkdf2-consts.d.ts +0 -9
  362. package/src/interfaces/pbkdf2-consts.d.ts.map +0 -1
  363. package/src/interfaces/pbkdf2-consts.js +0 -2
  364. package/src/interfaces/pbkdf2-consts.js.map +0 -1
  365. package/src/interfaces/pbkdf2-result.d.ts +0 -6
  366. package/src/interfaces/pbkdf2-result.d.ts.map +0 -1
  367. package/src/interfaces/pbkdf2-result.js +0 -2
  368. package/src/interfaces/pbkdf2-result.js.map +0 -1
  369. package/src/interfaces/stream-config.d.ts +0 -14
  370. package/src/interfaces/stream-config.d.ts.map +0 -1
  371. package/src/interfaces/stream-config.js +0 -8
  372. package/src/interfaces/stream-config.js.map +0 -1
  373. package/src/interfaces/stream-header.d.ts +0 -29
  374. package/src/interfaces/stream-header.d.ts.map +0 -1
  375. package/src/interfaces/stream-header.js +0 -9
  376. package/src/interfaces/stream-header.js.map +0 -1
  377. package/src/interfaces/stream-progress.d.ts +0 -33
  378. package/src/interfaces/stream-progress.d.ts.map +0 -1
  379. package/src/interfaces/stream-progress.js +0 -2
  380. package/src/interfaces/stream-progress.js.map +0 -1
  381. package/src/lib/configuration-provenance-utils.d.ts +0 -11
  382. package/src/lib/configuration-provenance-utils.d.ts.map +0 -1
  383. package/src/lib/configuration-provenance-utils.js +0 -23
  384. package/src/lib/configuration-provenance-utils.js.map +0 -1
  385. package/src/lib/crypto-container.d.ts +0 -25
  386. package/src/lib/crypto-container.d.ts.map +0 -1
  387. package/src/lib/crypto-container.js +0 -46
  388. package/src/lib/crypto-container.js.map +0 -1
  389. package/src/lib/guid.d.ts +0 -344
  390. package/src/lib/guid.d.ts.map +0 -1
  391. package/src/lib/guid.js +0 -914
  392. package/src/lib/guid.js.map +0 -1
  393. package/src/lib/id-providers/custom-provider.d.ts +0 -46
  394. package/src/lib/id-providers/custom-provider.d.ts.map +0 -1
  395. package/src/lib/id-providers/custom-provider.js +0 -85
  396. package/src/lib/id-providers/custom-provider.js.map +0 -1
  397. package/src/lib/id-providers/guidv4-provider.d.ts +0 -56
  398. package/src/lib/id-providers/guidv4-provider.d.ts.map +0 -1
  399. package/src/lib/id-providers/guidv4-provider.js +0 -122
  400. package/src/lib/id-providers/guidv4-provider.js.map +0 -1
  401. package/src/lib/id-providers/index.d.ts.map +0 -1
  402. package/src/lib/id-providers/index.js +0 -29
  403. package/src/lib/id-providers/index.js.map +0 -1
  404. package/src/lib/id-providers/objectid-provider.d.ts +0 -43
  405. package/src/lib/id-providers/objectid-provider.d.ts.map +0 -1
  406. package/src/lib/id-providers/objectid-provider.js +0 -104
  407. package/src/lib/id-providers/objectid-provider.js.map +0 -1
  408. package/src/lib/id-providers/uuid-provider.d.ts +0 -52
  409. package/src/lib/id-providers/uuid-provider.d.ts.map +0 -1
  410. package/src/lib/id-providers/uuid-provider.js +0 -110
  411. package/src/lib/id-providers/uuid-provider.js.map +0 -1
  412. package/src/lib/index.d.ts +0 -6
  413. package/src/lib/index.d.ts.map +0 -1
  414. package/src/lib/index.js.map +0 -1
  415. package/src/lib/invariant-validator.d.ts +0 -59
  416. package/src/lib/invariant-validator.d.ts.map +0 -1
  417. package/src/lib/invariant-validator.js +0 -97
  418. package/src/lib/invariant-validator.js.map +0 -1
  419. package/src/lib/invariants/encryption-algorithm-consistency.d.ts +0 -17
  420. package/src/lib/invariants/encryption-algorithm-consistency.d.ts.map +0 -1
  421. package/src/lib/invariants/encryption-algorithm-consistency.js +0 -49
  422. package/src/lib/invariants/encryption-algorithm-consistency.js.map +0 -1
  423. package/src/lib/invariants/index.d.ts +0 -4
  424. package/src/lib/invariants/index.d.ts.map +0 -1
  425. package/src/lib/invariants/index.js.map +0 -1
  426. package/src/lib/invariants/pbkdf2-profiles-validity.d.ts +0 -16
  427. package/src/lib/invariants/pbkdf2-profiles-validity.d.ts.map +0 -1
  428. package/src/lib/invariants/pbkdf2-profiles-validity.js +0 -58
  429. package/src/lib/invariants/pbkdf2-profiles-validity.js.map +0 -1
  430. package/src/lib/invariants/recipient-id-consistency.d.ts +0 -18
  431. package/src/lib/invariants/recipient-id-consistency.d.ts.map +0 -1
  432. package/src/lib/invariants/recipient-id-consistency.js +0 -31
  433. package/src/lib/invariants/recipient-id-consistency.js.map +0 -1
  434. package/src/lib/multi-recipient-chunk-utils.d.ts +0 -38
  435. package/src/lib/multi-recipient-chunk-utils.d.ts.map +0 -1
  436. package/src/lib/multi-recipient-chunk-utils.js +0 -41
  437. package/src/lib/multi-recipient-chunk-utils.js.map +0 -1
  438. package/src/member.d.ts +0 -92
  439. package/src/member.d.ts.map +0 -1
  440. package/src/member.js +0 -322
  441. package/src/member.js.map +0 -1
  442. package/src/pbkdf2-profiles.d.ts.map +0 -1
  443. package/src/pbkdf2-profiles.js +0 -2
  444. package/src/pbkdf2-profiles.js.map +0 -1
  445. package/src/phone-number.d.ts +0 -6
  446. package/src/phone-number.d.ts.map +0 -1
  447. package/src/phone-number.js +0 -18
  448. package/src/phone-number.js.map +0 -1
  449. package/src/regexes.d.ts +0 -7
  450. package/src/regexes.d.ts.map +0 -1
  451. package/src/regexes.js +0 -7
  452. package/src/regexes.js.map +0 -1
  453. package/src/secure-buffer.d.ts +0 -61
  454. package/src/secure-buffer.d.ts.map +0 -1
  455. package/src/secure-buffer.js +0 -201
  456. package/src/secure-buffer.js.map +0 -1
  457. package/src/secure-string.d.ts +0 -46
  458. package/src/secure-string.d.ts.map +0 -1
  459. package/src/secure-string.js +0 -206
  460. package/src/secure-string.js.map +0 -1
  461. package/src/services/aes-gcm.d.ts +0 -57
  462. package/src/services/aes-gcm.d.ts.map +0 -1
  463. package/src/services/aes-gcm.js +0 -142
  464. package/src/services/aes-gcm.js.map +0 -1
  465. package/src/services/chunk-processor.d.ts +0 -31
  466. package/src/services/chunk-processor.d.ts.map +0 -1
  467. package/src/services/chunk-processor.js +0 -145
  468. package/src/services/chunk-processor.js.map +0 -1
  469. package/src/services/ecies/crypto-core.d.ts +0 -72
  470. package/src/services/ecies/crypto-core.d.ts.map +0 -1
  471. package/src/services/ecies/crypto-core.js +0 -205
  472. package/src/services/ecies/crypto-core.js.map +0 -1
  473. package/src/services/ecies/example.d.ts +0 -25
  474. package/src/services/ecies/example.d.ts.map +0 -1
  475. package/src/services/ecies/example.js +0 -121
  476. package/src/services/ecies/example.js.map +0 -1
  477. package/src/services/ecies/file.d.ts +0 -18
  478. package/src/services/ecies/file.d.ts.map +0 -1
  479. package/src/services/ecies/file.js +0 -106
  480. package/src/services/ecies/file.js.map +0 -1
  481. package/src/services/ecies/index.d.ts +0 -38
  482. package/src/services/ecies/index.d.ts.map +0 -1
  483. package/src/services/ecies/index.js.map +0 -1
  484. package/src/services/ecies/integration.d.ts +0 -59
  485. package/src/services/ecies/integration.d.ts.map +0 -1
  486. package/src/services/ecies/integration.js +0 -167
  487. package/src/services/ecies/integration.js.map +0 -1
  488. package/src/services/ecies/interfaces.d.ts +0 -54
  489. package/src/services/ecies/interfaces.d.ts.map +0 -1
  490. package/src/services/ecies/interfaces.js +0 -5
  491. package/src/services/ecies/interfaces.js.map +0 -1
  492. package/src/services/ecies/manual-test.d.ts +0 -29
  493. package/src/services/ecies/manual-test.d.ts.map +0 -1
  494. package/src/services/ecies/manual-test.js +0 -163
  495. package/src/services/ecies/manual-test.js.map +0 -1
  496. package/src/services/ecies/multi-recipient.d.ts +0 -56
  497. package/src/services/ecies/multi-recipient.d.ts.map +0 -1
  498. package/src/services/ecies/multi-recipient.js +0 -344
  499. package/src/services/ecies/multi-recipient.js.map +0 -1
  500. package/src/services/ecies/service.d.ts +0 -120
  501. package/src/services/ecies/service.d.ts.map +0 -1
  502. package/src/services/ecies/service.js +0 -210
  503. package/src/services/ecies/service.js.map +0 -1
  504. package/src/services/ecies/signature.d.ts +0 -27
  505. package/src/services/ecies/signature.d.ts.map +0 -1
  506. package/src/services/ecies/signature.js +0 -72
  507. package/src/services/ecies/signature.js.map +0 -1
  508. package/src/services/ecies/single-recipient.d.ts +0 -46
  509. package/src/services/ecies/single-recipient.d.ts.map +0 -1
  510. package/src/services/ecies/single-recipient.js +0 -322
  511. package/src/services/ecies/single-recipient.js.map +0 -1
  512. package/src/services/encryption-stream.d.ts +0 -71
  513. package/src/services/encryption-stream.d.ts.map +0 -1
  514. package/src/services/encryption-stream.js +0 -295
  515. package/src/services/encryption-stream.js.map +0 -1
  516. package/src/services/index.d.ts +0 -11
  517. package/src/services/index.d.ts.map +0 -1
  518. package/src/services/index.js.map +0 -1
  519. package/src/services/multi-recipient-processor.d.ts +0 -35
  520. package/src/services/multi-recipient-processor.d.ts.map +0 -1
  521. package/src/services/multi-recipient-processor.js +0 -289
  522. package/src/services/multi-recipient-processor.js.map +0 -1
  523. package/src/services/password-login.d.ts +0 -47
  524. package/src/services/password-login.d.ts.map +0 -1
  525. package/src/services/password-login.js +0 -115
  526. package/src/services/password-login.js.map +0 -1
  527. package/src/services/pbkdf2.d.ts +0 -54
  528. package/src/services/pbkdf2.d.ts.map +0 -1
  529. package/src/services/pbkdf2.js +0 -108
  530. package/src/services/pbkdf2.js.map +0 -1
  531. package/src/services/progress-tracker.d.ts +0 -23
  532. package/src/services/progress-tracker.d.ts.map +0 -1
  533. package/src/services/progress-tracker.js +0 -103
  534. package/src/services/progress-tracker.js.map +0 -1
  535. package/src/services/resumable-encryption.d.ts +0 -19
  536. package/src/services/resumable-encryption.d.ts.map +0 -1
  537. package/src/services/resumable-encryption.js +0 -105
  538. package/src/services/resumable-encryption.js.map +0 -1
  539. package/src/services/xor.d.ts +0 -37
  540. package/src/services/xor.d.ts.map +0 -1
  541. package/src/services/xor.js +0 -63
  542. package/src/services/xor.js.map +0 -1
  543. package/src/test-mocks/index.d.ts +0 -2
  544. package/src/test-mocks/index.d.ts.map +0 -1
  545. package/src/test-mocks/index.js +0 -2
  546. package/src/test-mocks/index.js.map +0 -1
  547. package/src/test-mocks/mock-frontend-member.d.ts +0 -85
  548. package/src/test-mocks/mock-frontend-member.d.ts.map +0 -1
  549. package/src/test-mocks/mock-frontend-member.js +0 -190
  550. package/src/test-mocks/mock-frontend-member.js.map +0 -1
  551. package/src/testing.d.ts +0 -2
  552. package/src/testing.d.ts.map +0 -1
  553. package/src/testing.js.map +0 -1
  554. package/src/translations/de.d.ts +0 -4
  555. package/src/translations/de.d.ts.map +0 -1
  556. package/src/translations/de.js.map +0 -1
  557. package/src/translations/en-US.d.ts +0 -4
  558. package/src/translations/en-US.d.ts.map +0 -1
  559. package/src/translations/en-US.js.map +0 -1
  560. package/src/translations/es.d.ts +0 -4
  561. package/src/translations/es.d.ts.map +0 -1
  562. package/src/translations/es.js.map +0 -1
  563. package/src/translations/fr.d.ts +0 -4
  564. package/src/translations/fr.d.ts.map +0 -1
  565. package/src/translations/fr.js.map +0 -1
  566. package/src/translations/ja.d.ts +0 -4
  567. package/src/translations/ja.d.ts.map +0 -1
  568. package/src/translations/ja.js.map +0 -1
  569. package/src/translations/uk.d.ts +0 -4
  570. package/src/translations/uk.d.ts.map +0 -1
  571. package/src/translations/uk.js.map +0 -1
  572. package/src/translations/zh-cn.d.ts +0 -4
  573. package/src/translations/zh-cn.d.ts.map +0 -1
  574. package/src/translations/zh-cn.js.map +0 -1
  575. package/src/types/deep-partial.d.ts +0 -4
  576. package/src/types/deep-partial.d.ts.map +0 -1
  577. package/src/types/deep-partial.js +0 -2
  578. package/src/types/deep-partial.js.map +0 -1
  579. package/src/types.d.ts.map +0 -1
  580. package/src/types.js +0 -2
  581. package/src/types.js.map +0 -1
  582. package/src/utils/encryption-type-utils.d.ts +0 -29
  583. package/src/utils/encryption-type-utils.d.ts.map +0 -1
  584. package/src/utils/encryption-type-utils.js +0 -61
  585. package/src/utils/encryption-type-utils.js.map +0 -1
  586. package/src/utils.d.ts +0 -68
  587. package/src/utils.d.ts.map +0 -1
  588. package/src/utils.js +0 -273
  589. package/src/utils.js.map +0 -1
@@ -0,0 +1,1097 @@
1
+ import { Constants } from '../constants';
2
+ import * as uuid from 'uuid';
3
+ import { GuidBrandType } from '../enumerations/guid-brand-type';
4
+ import { GuidErrorType } from '../enumerations/guid-error-type';
5
+ import { GuidError } from '../errors/guid';
6
+ import { IGuidV4 } from '../interfaces/guid';
7
+ import {
8
+ Base64Guid,
9
+ BigIntGuid,
10
+ FullHexGuid,
11
+ RawGuidBuffer,
12
+ ShortHexGuid,
13
+ } from '../types';
14
+
15
+ // Re-export Base64Guid so it's available to importers of guid.ts
16
+ export type { Base64Guid };
17
+
18
+ // Define a type that can handle all GUID variants
19
+ export type GuidInput =
20
+ | string
21
+ | FullHexGuid
22
+ | ShortHexGuid
23
+ | Base64Guid
24
+ | BigIntGuid
25
+ | RawGuidBuffer
26
+ | bigint
27
+ | Buffer;
28
+
29
+ /**
30
+ * GuidV4 represents a GUID (Globally Unique Identifier) that is compliant with the RFC 4122 standard.
31
+ * GuidV4 instances can be created from a variety of input types, including:
32
+ * - FullHexGuid: A 36-character string representation of the GUID, including dashes
33
+ * - ShortHexGuid: A 32-character string representation of the GUID, excluding dashes
34
+ * - Base64Guid: A 24-character base64-encoded string representation of the GUID
35
+ * - BigIntGuid: A bigint representation of the GUID
36
+ * - RawGuidBuffer: A 16-byte Buffer representation of the GUID
37
+ * GuidV4 instances can be converted to any of these representations using the appropriate method.
38
+ */
39
+ export class GuidV4 implements IGuidV4 {
40
+ /**
41
+ * GUID is stored internally as a raw 16-byte Buffer.
42
+ */
43
+ private readonly _value: RawGuidBuffer;
44
+
45
+ /**
46
+ * Boundary value constants for special GUID validation
47
+ */
48
+ private static readonly BOUNDARY_VALUES = {
49
+ ALL_ZEROS_FULL: '00000000-0000-0000-0000-000000000000' as const,
50
+ ALL_ZEROS_SHORT: '00000000000000000000000000000000' as const,
51
+ ALL_FS_FULL: 'ffffffff-ffff-ffff-ffff-ffffffffffff' as const,
52
+ ALL_FS_SHORT: 'ffffffffffffffffffffffffffffffff' as const,
53
+ } as const;
54
+
55
+ /**
56
+ * Maximum valid bigint value for a 128-bit GUID
57
+ */
58
+ private static readonly MAX_BIGINT_VALUE = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF');
59
+
60
+ /**
61
+ * Cached full hex representation for performance
62
+ */
63
+ private _cachedFullHex?: FullHexGuid;
64
+
65
+ /**
66
+ * Cached short hex representation for performance
67
+ */
68
+ private _cachedShortHex?: ShortHexGuid;
69
+
70
+ /**
71
+ * Cached base64 representation for performance
72
+ */
73
+ private _cachedBase64?: Base64Guid;
74
+
75
+ /**
76
+ * Regex for validating hex strings (case insensitive)
77
+ */
78
+ private static readonly HEX_PATTERN = /^[0-9a-f]+$/i;
79
+
80
+ /**
81
+ * Regex for validating full hex GUID format
82
+ */
83
+ private static readonly FULL_HEX_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
84
+
85
+ /**
86
+ * Cached empty/nil GUID constant (all zeros)
87
+ */
88
+ private static _empty?: GuidV4;
89
+
90
+ /**
91
+ * Empty/nil GUID constant (all zeros)
92
+ */
93
+ public static get Empty(): GuidV4 {
94
+ if (!GuidV4._empty) {
95
+ GuidV4._empty = Object.freeze(
96
+ new GuidV4('00000000-0000-0000-0000-000000000000' as FullHexGuid)
97
+ ) as GuidV4;
98
+ }
99
+ return GuidV4._empty;
100
+ }
101
+
102
+ constructor(
103
+ value: GuidInput,
104
+ ) {
105
+ const buffer = GuidV4.validateAndConvert(value);
106
+ // Note: We cannot freeze a Buffer as it's an ArrayBuffer view
107
+ // Instead, we ensure the buffer is never directly modified after construction
108
+ this._value = buffer;
109
+
110
+ // Initialize cache properties so they exist before sealing
111
+ this._cachedFullHex = undefined;
112
+ this._cachedShortHex = undefined;
113
+ this._cachedBase64 = undefined;
114
+
115
+ // Seal the instance to prevent property addition/deletion
116
+ // Cache properties can still be set once since they were initialized
117
+ Object.seal(this);
118
+ }
119
+
120
+ /**
121
+ * Validates input and converts to raw buffer with comprehensive error handling.
122
+ * This centralizes all validation logic for better maintainability.
123
+ * @param value The input value to validate and convert
124
+ * @returns The validated raw GUID buffer
125
+ * @throws {GuidError} If validation fails
126
+ */
127
+ private static validateAndConvert(value: GuidInput): RawGuidBuffer {
128
+ try {
129
+ // Null/undefined check
130
+ if (value === null || value === undefined) {
131
+ throw new GuidError(GuidErrorType.InvalidGuid);
132
+ }
133
+
134
+ // Empty string check (but allow 0n bigint)
135
+ const strValue = String(value);
136
+ if (!strValue && value !== 0n) {
137
+ throw new GuidError(GuidErrorType.InvalidGuid);
138
+ }
139
+
140
+ // Validate bigint is non-negative
141
+ if (typeof value === 'bigint' && value < 0n) {
142
+ throw new GuidError(GuidErrorType.InvalidGuid);
143
+ }
144
+
145
+ // Validate hex strings contain only valid hex characters
146
+ if (typeof value === 'string') {
147
+ const isFullHex = value.length === 36 && value.includes('-');
148
+ const isShortHex = value.length === 32 && !value.includes('-');
149
+
150
+ if (isFullHex && !GuidV4.FULL_HEX_PATTERN.test(value)) {
151
+ const buffer = Buffer.from(value);
152
+ throw new GuidError(
153
+ GuidErrorType.InvalidGuidWithDetails,
154
+ GuidBrandType.FullHexGuid,
155
+ value.length,
156
+ buffer,
157
+ );
158
+ } else if (isShortHex && !GuidV4.HEX_PATTERN.test(value)) {
159
+ const buffer = Buffer.from(value);
160
+ throw new GuidError(
161
+ GuidErrorType.InvalidGuidWithDetails,
162
+ GuidBrandType.ShortHexGuid,
163
+ value.length,
164
+ buffer,
165
+ );
166
+ }
167
+ }
168
+
169
+ // Determine and verify the brand/type
170
+ const expectedBrand = GuidV4.whichBrand(value);
171
+ const verifiedBrand = GuidV4.verifyGuid(expectedBrand, value);
172
+
173
+ if (!verifiedBrand) {
174
+ const valueBuffer = Buffer.isBuffer(value)
175
+ ? value
176
+ : Buffer.from(strValue);
177
+ throw new GuidError(
178
+ GuidErrorType.InvalidGuidWithDetails,
179
+ expectedBrand,
180
+ undefined,
181
+ valueBuffer,
182
+ );
183
+ }
184
+
185
+ // Convert to raw buffer
186
+ const buffer = GuidV4.toRawGuidBuffer(value);
187
+
188
+ // Validate against UUID standard (skip for boundary values)
189
+ const fullHex = GuidV4.toFullHexGuid(buffer.toString('hex'));
190
+ const isBoundary = GuidV4.isBoundaryValue(fullHex);
191
+
192
+ if (!isBoundary && !uuid.validate(fullHex)) {
193
+ throw new GuidError(
194
+ GuidErrorType.InvalidGuid,
195
+ expectedBrand,
196
+ undefined,
197
+ buffer,
198
+ );
199
+ }
200
+
201
+ return buffer;
202
+ } catch (error) {
203
+ // Re-throw GuidError as-is
204
+ if (error instanceof GuidError) {
205
+ throw error;
206
+ }
207
+
208
+ // Wrap other errors with context
209
+ if (typeof value === 'bigint') {
210
+ throw new GuidError(GuidErrorType.InvalidGuid);
211
+ }
212
+
213
+ const length = value instanceof Buffer ? value.length : String(value).length;
214
+ throw new GuidError(
215
+ GuidErrorType.InvalidGuidUnknownLength,
216
+ undefined,
217
+ length,
218
+ );
219
+ }
220
+ }
221
+
222
+ public static validateUuid(value: string): boolean {
223
+ return uuid.validate(value);
224
+ }
225
+
226
+ public serialize(): string {
227
+ return this.asBase64Guid;
228
+ }
229
+
230
+ public static hydrate(value: string): GuidV4 {
231
+ return new GuidV4(value as Base64Guid);
232
+ }
233
+
234
+ private static readonly LengthMap: Record<GuidBrandType, number> = {
235
+ [GuidBrandType.Unknown]: -1,
236
+ [GuidBrandType.FullHexGuid]: 36,
237
+ [GuidBrandType.ShortHexGuid]: 32,
238
+ [GuidBrandType.Base64Guid]: 24,
239
+ [GuidBrandType.RawGuidBuffer]: 16,
240
+ [GuidBrandType.BigIntGuid]: -1, // Variable length
241
+ };
242
+
243
+ private static readonly ReverseLengthMap: Record<number, GuidBrandType> = {
244
+ 0: GuidBrandType.Unknown,
245
+ 36: GuidBrandType.FullHexGuid,
246
+ 32: GuidBrandType.ShortHexGuid,
247
+ 24: GuidBrandType.Base64Guid,
248
+ 16: GuidBrandType.RawGuidBuffer,
249
+ // BigIntGuid is variable length, so it is not included in the reverse map
250
+ };
251
+
252
+ private static readonly VerifyFunctions: Record<
253
+ GuidBrandType,
254
+ (guid: GuidInput, validate?: boolean) => boolean
255
+ > = {
256
+ [GuidBrandType.Unknown]: () => false,
257
+ [GuidBrandType.FullHexGuid]: (guid: GuidInput) => this.isFullHexGuid(guid),
258
+ [GuidBrandType.ShortHexGuid]: (guid: GuidInput) =>
259
+ this.isShortHexGuid(guid),
260
+ [GuidBrandType.Base64Guid]: (guid: GuidInput) => this.isBase64Guid(guid),
261
+ [GuidBrandType.BigIntGuid]: (guid: GuidInput) => this.isBigIntGuid(guid),
262
+ [GuidBrandType.RawGuidBuffer]: (guid: GuidInput) =>
263
+ this.isRawGuidBuffer(guid),
264
+ };
265
+
266
+ /**
267
+ * Returns the GUID as a raw Buffer.
268
+ * NOTE: Returns a defensive copy to prevent external mutation.
269
+ * Use asRawGuidBufferUnsafe() if you need the internal buffer and guarantee no mutation.
270
+ */
271
+ public get asRawGuidBuffer(): RawGuidBuffer {
272
+ return Buffer.from(this._value) as RawGuidBuffer;
273
+ }
274
+
275
+ /**
276
+ * Returns the internal raw Buffer without copying.
277
+ * ⚠️ WARNING: Do NOT mutate the returned buffer! This is for performance-critical paths only.
278
+ * Mutating this buffer will corrupt the GUID instance.
279
+ * @internal
280
+ */
281
+ public get asRawGuidBufferUnsafe(): RawGuidBuffer {
282
+ return this._value;
283
+ }
284
+
285
+ /**
286
+ * Generates a new random v4 GUID.
287
+ * @returns A new GuidV4 instance with a randomly generated value
288
+ */
289
+ public static generate(): GuidV4 {
290
+ try {
291
+ const uuidStr = uuid.v4();
292
+ if (!uuidStr) {
293
+ throw new GuidError(GuidErrorType.InvalidGuid);
294
+ }
295
+ return new GuidV4(uuidStr as FullHexGuid);
296
+ } catch (error) {
297
+ if (error instanceof GuidError) {
298
+ throw error;
299
+ }
300
+ throw new GuidError(GuidErrorType.InvalidGuid);
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Alias for generate() for backward compatibility.
306
+ * @deprecated Use generate() instead for clearer intent
307
+ */
308
+ public static new(): GuidV4 {
309
+ return GuidV4.generate();
310
+ }
311
+
312
+ /**
313
+ * Parses a GUID from any valid format, throwing on invalid input.
314
+ * This is the primary parsing method for when you expect valid input.
315
+ * @param value The value to parse
316
+ * @returns A new GuidV4 instance
317
+ * @throws {GuidError} If the value is not a valid GUID
318
+ */
319
+ public static parse(value: GuidInput): GuidV4 {
320
+ return new GuidV4(value);
321
+ }
322
+
323
+ /**
324
+ * Attempts to parse a GUID, returning null on failure instead of throwing.
325
+ * Use this when you're uncertain if the input is valid.
326
+ * @param value The value to parse
327
+ * @returns A new GuidV4 instance or null if parsing fails
328
+ */
329
+ public static tryParse(value: GuidInput): GuidV4 | null {
330
+ try {
331
+ return new GuidV4(value);
332
+ } catch {
333
+ return null;
334
+ }
335
+ }
336
+
337
+ /**
338
+ * Validates whether a value is a valid GUID without creating an instance.
339
+ * More efficient than tryParse when you only need validation.
340
+ * @param value The value to validate
341
+ * @returns True if valid, false otherwise
342
+ */
343
+ public static isValid(value: unknown): boolean {
344
+ if (!value) return false;
345
+ try {
346
+ const guid = new GuidV4(value as GuidInput);
347
+ return guid.isValidV4();
348
+ } catch {
349
+ return false;
350
+ }
351
+ }
352
+
353
+ /**
354
+ * Factory method to create a GUID from a full hex string.
355
+ * @param fullHex The full hex string (with dashes)
356
+ * @returns A new GuidV4 instance
357
+ */
358
+ public static fromFullHex(fullHex: string): GuidV4 {
359
+ return new GuidV4(fullHex as FullHexGuid);
360
+ }
361
+
362
+ /**
363
+ * Factory method to create a GUID from a short hex string.
364
+ * @param shortHex The short hex string (without dashes)
365
+ * @returns A new GuidV4 instance
366
+ */
367
+ public static fromShortHex(shortHex: string): GuidV4 {
368
+ return new GuidV4(shortHex as ShortHexGuid);
369
+ }
370
+
371
+ /**
372
+ * Factory method to create a GUID from a base64 string.
373
+ * @param base64 The base64 encoded string
374
+ * @returns A new GuidV4 instance
375
+ */
376
+ public static fromBase64(base64: string): GuidV4 {
377
+ return new GuidV4(base64 as Base64Guid);
378
+ }
379
+
380
+ /**
381
+ * Factory method to create a GUID from a bigint.
382
+ * @param bigint The bigint value
383
+ * @returns A new GuidV4 instance
384
+ */
385
+ public static fromBigInt(bigint: bigint): GuidV4 {
386
+ return new GuidV4(bigint as BigIntGuid);
387
+ }
388
+
389
+ /**
390
+ * Factory method to create a GUID from a raw buffer.
391
+ * @param buffer The raw 16-byte buffer
392
+ * @returns A new GuidV4 instance
393
+ */
394
+ public static fromBuffer(buffer: Buffer): GuidV4 {
395
+ return new GuidV4(buffer as RawGuidBuffer);
396
+ }
397
+
398
+ /**
399
+ * Creates a namespace-based v3 GUID (MD5 hash).
400
+ * Use this for deterministic GUIDs based on a namespace and name.
401
+ * @param namespace The namespace GUID (e.g., uuid.v3.DNS)
402
+ * @param name The name to hash within the namespace
403
+ * @returns A new GuidV4 instance containing the v3 GUID
404
+ * @example
405
+ * const guid = GuidV4.v3('example.com', uuid.v3.DNS);
406
+ */
407
+ public static v3(name: string, namespace: string | Buffer): GuidV4 {
408
+ try {
409
+ const namespaceStr = Buffer.isBuffer(namespace)
410
+ ? GuidV4.toFullHexGuid(namespace.toString('hex'))
411
+ : namespace;
412
+ const v3Guid = uuid.v3(name, namespaceStr);
413
+ return new GuidV4(v3Guid as FullHexGuid);
414
+ } catch (error) {
415
+ if (error instanceof GuidError) {
416
+ throw error;
417
+ }
418
+ throw new GuidError(GuidErrorType.InvalidGuid);
419
+ }
420
+ }
421
+
422
+ /**
423
+ * Creates a namespace-based v5 GUID (SHA-1 hash).
424
+ * Use this for deterministic GUIDs based on a namespace and name.
425
+ * Preferred over v3 as SHA-1 is stronger than MD5.
426
+ * @param namespace The namespace GUID (e.g., uuid.v5.DNS)
427
+ * @param name The name to hash within the namespace
428
+ * @returns A new GuidV4 instance containing the v5 GUID
429
+ * @example
430
+ * const guid = GuidV4.v5('example.com', uuid.v5.DNS);
431
+ */
432
+ public static v5(name: string, namespace: string | Buffer): GuidV4 {
433
+ try {
434
+ const namespaceStr = Buffer.isBuffer(namespace)
435
+ ? GuidV4.toFullHexGuid(namespace.toString('hex'))
436
+ : namespace;
437
+ const v5Guid = uuid.v5(name, namespaceStr);
438
+ return new GuidV4(v5Guid as FullHexGuid);
439
+ } catch (error) {
440
+ if (error instanceof GuidError) {
441
+ throw error;
442
+ }
443
+ throw new GuidError(GuidErrorType.InvalidGuid);
444
+ }
445
+ }
446
+
447
+ /**
448
+ * Common namespace constants for use with v3/v5 GUIDs.
449
+ * Re-exported from uuid library for convenience.
450
+ */
451
+ public static readonly Namespaces = {
452
+ DNS: uuid.v5.DNS,
453
+ URL: uuid.v5.URL,
454
+ } as const;
455
+ /**
456
+ * Returns the GUID as a full hex string.
457
+ * Result is cached for performance.
458
+ */
459
+ public get asFullHexGuid(): FullHexGuid {
460
+ if (!this._cachedFullHex) {
461
+ this._cachedFullHex = GuidV4.toFullHexGuid(this._value.toString('hex'));
462
+ }
463
+ return this._cachedFullHex;
464
+ }
465
+ /**
466
+ * Returns the GUID as a raw Uint8Array.
467
+ */
468
+ public get asUint8Array(): Uint8Array {
469
+ return this._value as Uint8Array;
470
+ }
471
+ /**
472
+ * Returns the GUID as a short hex string.
473
+ * Result is cached for performance.
474
+ */
475
+ public get asShortHexGuid(): ShortHexGuid {
476
+ if (!this._cachedShortHex) {
477
+ this._cachedShortHex = GuidV4.toShortHexGuid(this.asFullHexGuid);
478
+ }
479
+ return this._cachedShortHex;
480
+ }
481
+ /**
482
+ * Returns the GUID as a base64 string.
483
+ */
484
+ public toString(): Base64Guid {
485
+ return this.asBase64Guid as Base64Guid;
486
+ }
487
+ /**
488
+ * Returns the GUID as a JSON string.
489
+ * @returns The GUID as a JSON string.
490
+ */
491
+ public toJson(): string {
492
+ return JSON.stringify(this.asBase64Guid);
493
+ }
494
+ /**
495
+ * Returns the GUID as a bigint.
496
+ */
497
+ public get asBigIntGuid(): BigIntGuid {
498
+ return BigInt('0x' + this._value.toString('hex')) as BigIntGuid;
499
+ }
500
+ /**
501
+ * Returns the GUID as a base64 string.
502
+ * Result is cached for performance.
503
+ */
504
+ public get asBase64Guid(): Base64Guid {
505
+ if (!this._cachedBase64) {
506
+ this._cachedBase64 = this._value.toString('base64') as Base64Guid;
507
+ }
508
+ return this._cachedBase64;
509
+ }
510
+
511
+ /**
512
+ * Checks if a GUID value is a boundary value (all zeros or all Fs).
513
+ * @param value The GUID value to check.
514
+ * @returns True if the value is a boundary value.
515
+ */
516
+ private static isBoundaryValue(value: string): boolean {
517
+ return (
518
+ value === GuidV4.BOUNDARY_VALUES.ALL_ZEROS_FULL ||
519
+ value === GuidV4.BOUNDARY_VALUES.ALL_ZEROS_SHORT ||
520
+ value === GuidV4.BOUNDARY_VALUES.ALL_FS_FULL ||
521
+ value === GuidV4.BOUNDARY_VALUES.ALL_FS_SHORT
522
+ );
523
+ }
524
+
525
+ /**
526
+ * Verifies if a given GUID is valid for the given brand.
527
+ * @param guidBrand The brand of the GUID to verify.
528
+ * @param guid The GUID to verify.
529
+ * @returns True if the GUID is valid for the given brand, false otherwise.
530
+ */
531
+ public static verifyGuid(guidBrand: GuidBrandType, guid: GuidInput): boolean {
532
+ if (guid === null || guid === undefined) {
533
+ return false;
534
+ }
535
+ try {
536
+ const verifyFunc = GuidV4.VerifyFunctions[guidBrand];
537
+ return verifyFunc(guid);
538
+ } catch {
539
+ return false;
540
+ }
541
+ }
542
+
543
+ /**
544
+ * Returns the length of the GUID for the given brand.
545
+ * @param guidBrand The brand of the GUID to get the length for.
546
+ * @returns The length of the GUID for the given brand.
547
+ */
548
+ public static guidBrandToLength(guidBrand: GuidBrandType): number {
549
+ const length = GuidV4.LengthMap[guidBrand];
550
+ if (length <= 0) {
551
+ throw new GuidError(GuidErrorType.InvalidGuidUnknownBrand, guidBrand);
552
+ }
553
+ return length as number;
554
+ }
555
+
556
+ /**
557
+ * Returns the brand of the GUID for the given length.
558
+ * @param length The length of the GUID to get the brand for.
559
+ * @param isBuffer Whether the GUID is a Buffer.
560
+ * @returns The brand of the GUID for the given length.
561
+ */
562
+ public static lengthToGuidBrand(
563
+ length: number,
564
+ isBuffer: boolean,
565
+ ): GuidBrandType {
566
+ if (length <= 0) {
567
+ throw new GuidError(GuidErrorType.InvalidGuidUnknownLength, undefined, length);
568
+ }
569
+
570
+ const brand = GuidV4.ReverseLengthMap[length];
571
+
572
+ if (!brand || brand === GuidBrandType.Unknown) {
573
+ throw new GuidError(GuidErrorType.InvalidGuidUnknownLength, undefined, length);
574
+ }
575
+
576
+ // Validate buffer vs string type consistency
577
+ const isBrandBuffer = brand === GuidBrandType.RawGuidBuffer;
578
+ if (isBuffer !== isBrandBuffer) {
579
+ throw new GuidError(GuidErrorType.InvalidGuidUnknownLength, brand, length);
580
+ }
581
+
582
+ return brand;
583
+ }
584
+
585
+ /**
586
+ * Verifies if a given GUID is a valid full hex GUID.
587
+ * @param fullHexGuidValue The full hex GUID to verify.
588
+ * @returns True if the GUID is a valid full hex GUID, false otherwise.
589
+ */
590
+ public static isFullHexGuid(fullHexGuidValue: GuidInput): boolean {
591
+ try {
592
+ if (fullHexGuidValue === null || fullHexGuidValue === undefined) {
593
+ return false;
594
+ }
595
+ const expectedLength = GuidV4.guidBrandToLength(
596
+ GuidBrandType.FullHexGuid,
597
+ );
598
+ const strValue = String(fullHexGuidValue);
599
+
600
+ if (strValue.length !== expectedLength) {
601
+ return false;
602
+ }
603
+
604
+ // Boundary values are always valid
605
+ if (GuidV4.isBoundaryValue(strValue)) {
606
+ return true;
607
+ }
608
+
609
+ return GuidV4.validateUuid(strValue);
610
+ } catch {
611
+ return false;
612
+ }
613
+ }
614
+
615
+ /**
616
+ * Verifies if a given GUID is a valid short hex GUID.
617
+ * @param shortHexGuidValue The short hex GUID to verify.
618
+ * @returns True if the GUID is a valid short hex GUID, false otherwise.
619
+ */
620
+ public static isShortHexGuid(shortHexGuidValue: GuidInput): boolean {
621
+ try {
622
+ if (shortHexGuidValue === null || shortHexGuidValue === undefined) {
623
+ return false;
624
+ }
625
+ const expectedLength = GuidV4.guidBrandToLength(
626
+ GuidBrandType.ShortHexGuid,
627
+ );
628
+ const strValue = String(shortHexGuidValue);
629
+
630
+ if (strValue.length !== expectedLength) {
631
+ return false;
632
+ }
633
+
634
+ try {
635
+ const fullHexGuid = GuidV4.toFullHexGuid(strValue);
636
+ // Boundary values are always valid
637
+ if (GuidV4.isBoundaryValue(fullHexGuid)) {
638
+ return true;
639
+ }
640
+ return uuid.validate(fullHexGuid);
641
+ } catch {
642
+ return false;
643
+ }
644
+ } catch {
645
+ return false;
646
+ }
647
+ }
648
+
649
+ /**
650
+ * Verifies if a given GUID is a valid base64 GUID.
651
+ * @param value The base64 GUID to verify.
652
+ * @returns True if the GUID is a valid base64 GUID, false otherwise.
653
+ */
654
+ public static isBase64Guid(value: GuidInput): boolean {
655
+ try {
656
+ if (value === null || value === undefined) {
657
+ return false;
658
+ }
659
+ let valueLength: number;
660
+ if (typeof value === 'bigint') {
661
+ valueLength = value.toString(16).length;
662
+ } else if (value instanceof Buffer) {
663
+ valueLength = value.length;
664
+ } else {
665
+ valueLength = String(value).length;
666
+ }
667
+
668
+ const result =
669
+ valueLength === GuidV4.guidBrandToLength(GuidBrandType.Base64Guid);
670
+
671
+ if (result) {
672
+ try {
673
+ const fromBase64: Buffer = GuidV4.toRawGuidBuffer(value);
674
+ const fullHexGuid = GuidV4.toFullHexGuid(fromBase64.toString('hex'));
675
+ // Boundary values are always valid
676
+ if (GuidV4.isBoundaryValue(fullHexGuid)) {
677
+ return true;
678
+ }
679
+ return uuid.validate(fullHexGuid);
680
+ } catch {
681
+ return false;
682
+ }
683
+ }
684
+ return result;
685
+ } catch {
686
+ return false;
687
+ }
688
+ }
689
+
690
+ /**
691
+ * Verifies if a given GUID is a valid raw GUID buffer.
692
+ * @param value The raw GUID buffer to verify.
693
+ * @returns True if the GUID is a valid raw GUID buffer, false otherwise.
694
+ */
695
+ public static isRawGuidBuffer(value: GuidInput): boolean {
696
+ try {
697
+ if (value === null || value === undefined) {
698
+ return false;
699
+ }
700
+ const expectedLength = GuidV4.guidBrandToLength(
701
+ GuidBrandType.RawGuidBuffer,
702
+ );
703
+ let valueLength: number;
704
+ if (typeof value === 'bigint') {
705
+ valueLength = value.toString(16).length;
706
+ } else if (value instanceof Buffer) {
707
+ valueLength = value.length;
708
+ } else {
709
+ valueLength = String(value).length;
710
+ }
711
+
712
+ if (valueLength !== expectedLength) {
713
+ return false;
714
+ }
715
+
716
+ try {
717
+ if (!(value instanceof Buffer)) {
718
+ return false;
719
+ }
720
+ const fullHexGuid = GuidV4.toFullHexGuid(value.toString('hex'));
721
+ // Boundary values are always valid
722
+ if (GuidV4.isBoundaryValue(fullHexGuid)) {
723
+ return true;
724
+ }
725
+ return GuidV4.validateUuid(fullHexGuid);
726
+ } catch {
727
+ return false;
728
+ }
729
+ } catch {
730
+ return false;
731
+ }
732
+ }
733
+
734
+ /**
735
+ * Verifies if a given GUID is a valid bigint GUID.
736
+ * @param value The bigint GUID to verify.
737
+ * @returns True if the GUID is a valid bigint GUID, false otherwise.
738
+ */
739
+ public static isBigIntGuid(value: GuidInput): boolean {
740
+ try {
741
+ if (value === null || value === undefined) {
742
+ return false;
743
+ }
744
+ if (typeof value !== 'bigint') {
745
+ return false;
746
+ }
747
+ if (value < 0n || value > GuidV4.MAX_BIGINT_VALUE) {
748
+ return false;
749
+ }
750
+
751
+ try {
752
+ const fromBigInt = GuidV4.toFullHexFromBigInt(value);
753
+ // Boundary values are always valid
754
+ if (GuidV4.isBoundaryValue(fromBigInt)) {
755
+ return true;
756
+ }
757
+ return uuid.validate(fromBigInt);
758
+ } catch {
759
+ return false;
760
+ }
761
+ } catch {
762
+ return false;
763
+ }
764
+ }
765
+
766
+ /**
767
+ * Determines the brand of a given GUID value.
768
+ * @param value The GUID value to determine the brand of.
769
+ * @returns The brand of the GUID value.
770
+ */
771
+ public static whichBrand(value: GuidInput): GuidBrandType {
772
+ if (value === null || value === undefined) {
773
+ throw new GuidError(GuidErrorType.InvalidGuid);
774
+ }
775
+
776
+ if (typeof value === 'bigint') {
777
+ return GuidBrandType.BigIntGuid;
778
+ }
779
+
780
+ const isBuffer = value instanceof Buffer;
781
+ const expectedLength = isBuffer ? value.length : String(value).length;
782
+
783
+ return GuidV4.lengthToGuidBrand(expectedLength, isBuffer);
784
+ }
785
+
786
+ /**
787
+ * Converts a given short hex GUID to a full hex GUID.
788
+ * @param shortGuid The short hex GUID to convert.
789
+ * @returns The short hex GUID as a full hex GUID.
790
+ */
791
+ private static shortGuidToFullGuid(shortGuid: string): FullHexGuid {
792
+ // insert dashes
793
+ const str = shortGuid.replace(
794
+ /(.{8})(.{4})(.{4})(.{4})(.{12})/,
795
+ '$1-$2-$3-$4-$5',
796
+ );
797
+ return str as FullHexGuid;
798
+ }
799
+
800
+ /**
801
+ * Converts a given GUID value to a full hex GUID.
802
+ * @param guid The GUID value to convert.
803
+ * @returns The GUID value as a full hex GUID.
804
+ */
805
+ public static toFullHexGuid(
806
+ guid:
807
+ | RawGuidBuffer
808
+ | BigIntGuid
809
+ | Base64Guid
810
+ | ShortHexGuid
811
+ | FullHexGuid
812
+ | string,
813
+ ): FullHexGuid {
814
+ if (guid === null || guid === undefined) {
815
+ throw new GuidError(GuidErrorType.InvalidGuid);
816
+ }
817
+
818
+ if (typeof guid === 'bigint') {
819
+ return GuidV4.toFullHexFromBigInt(guid);
820
+ } else if (
821
+ Buffer.isBuffer(guid) &&
822
+ guid.length === GuidV4.guidBrandToLength(GuidBrandType.RawGuidBuffer)
823
+ ) {
824
+ const shortHex = guid.toString('hex') as ShortHexGuid;
825
+ return GuidV4.shortGuidToFullGuid(shortHex);
826
+ } else if (Buffer.isBuffer(guid)) {
827
+ throw new GuidError(GuidErrorType.InvalidGuid);
828
+ }
829
+ // all remaining cases are string types
830
+ const strValue = String(guid);
831
+ if (
832
+ strValue.length === GuidV4.guidBrandToLength(GuidBrandType.ShortHexGuid)
833
+ ) {
834
+ // short hex guid
835
+ return GuidV4.shortGuidToFullGuid(strValue);
836
+ } else if (
837
+ strValue.length === GuidV4.guidBrandToLength(GuidBrandType.FullHexGuid)
838
+ ) {
839
+ // already a full hex guid
840
+ return strValue as FullHexGuid;
841
+ } else if (
842
+ strValue.length === GuidV4.guidBrandToLength(GuidBrandType.Base64Guid)
843
+ ) {
844
+ // base64 guid
845
+ const shortGuid = Buffer.from(strValue, 'base64').toString('hex');
846
+ return GuidV4.shortGuidToFullGuid(shortGuid);
847
+ } else {
848
+ throw new GuidError(GuidErrorType.InvalidGuid);
849
+ }
850
+ }
851
+
852
+ public static toShortHexGuid(
853
+ guid:
854
+ | RawGuidBuffer
855
+ | BigIntGuid
856
+ | Base64Guid
857
+ | ShortHexGuid
858
+ | FullHexGuid
859
+ | string,
860
+ ): ShortHexGuid {
861
+ if (guid === null || guid === undefined) {
862
+ throw new GuidError(GuidErrorType.InvalidGuid);
863
+ }
864
+
865
+ if (typeof guid === 'bigint') {
866
+ const fullHex = GuidV4.toFullHexFromBigInt(guid);
867
+ return fullHex.replace(/-/g, '') as ShortHexGuid;
868
+ } else if (
869
+ Buffer.isBuffer(guid) &&
870
+ guid.length === GuidV4.guidBrandToLength(GuidBrandType.RawGuidBuffer)
871
+ ) {
872
+ return guid.toString('hex') as ShortHexGuid;
873
+ } else if (Buffer.isBuffer(guid)) {
874
+ throw new GuidError(GuidErrorType.InvalidGuid);
875
+ }
876
+ // all remaining cases are string types
877
+ const strValue = String(guid);
878
+
879
+ if (
880
+ strValue.length === GuidV4.guidBrandToLength(GuidBrandType.ShortHexGuid)
881
+ ) {
882
+ // already a short hex guid
883
+ return strValue as ShortHexGuid;
884
+ } else if (
885
+ strValue.length === GuidV4.guidBrandToLength(GuidBrandType.FullHexGuid)
886
+ ) {
887
+ // full hex guid
888
+ return strValue.replace(/-/g, '') as ShortHexGuid;
889
+ } else if (
890
+ strValue.length === GuidV4.guidBrandToLength(GuidBrandType.Base64Guid)
891
+ ) {
892
+ // base64 guid
893
+ return Buffer.from(strValue, 'base64').toString('hex') as ShortHexGuid;
894
+ } else {
895
+ throw new GuidError(GuidErrorType.InvalidGuid);
896
+ }
897
+ }
898
+
899
+ /**
900
+ * Converts a given bigint to a full hex GUID.
901
+ * @param bigInt The bigint to convert.
902
+ * @returns The bigint as a full hex GUID.
903
+ */
904
+ public static toFullHexFromBigInt(bigInt: bigint): FullHexGuid {
905
+ if (bigInt < 0n || bigInt > GuidV4.MAX_BIGINT_VALUE) {
906
+ throw new GuidError(GuidErrorType.InvalidGuid);
907
+ }
908
+ const uuidBigInt = bigInt.toString(16).padStart(32, '0');
909
+ // After padding, should always be exactly 32 characters
910
+ if (uuidBigInt.length !== 32) {
911
+ throw new GuidError(GuidErrorType.InvalidGuid);
912
+ }
913
+ const rebuiltUuid =
914
+ uuidBigInt.slice(0, 8) +
915
+ '-' +
916
+ uuidBigInt.slice(8, 12) +
917
+ '-' +
918
+ uuidBigInt.slice(12, 16) +
919
+ '-' +
920
+ uuidBigInt.slice(16, 20) +
921
+ '-' +
922
+ uuidBigInt.slice(20);
923
+ return rebuiltUuid as FullHexGuid;
924
+ }
925
+
926
+ /**
927
+ * Converts a given GUID value to a raw GUID buffer.
928
+ * @param value The GUID value to convert.
929
+ * @returns The GUID value as a raw GUID buffer.
930
+ */
931
+ public static toRawGuidBuffer(value: GuidInput): RawGuidBuffer {
932
+ const expectedBrand = GuidV4.whichBrand(value);
933
+ let rawGuidBufferResult: RawGuidBuffer = Buffer.alloc(0) as RawGuidBuffer;
934
+ switch (expectedBrand) {
935
+ case GuidBrandType.FullHexGuid:
936
+ rawGuidBufferResult = Buffer.from(
937
+ GuidV4.toShortHexGuid(value as FullHexGuid),
938
+ 'hex',
939
+ ) as RawGuidBuffer;
940
+ break;
941
+ case GuidBrandType.ShortHexGuid:
942
+ rawGuidBufferResult = Buffer.from(
943
+ GuidV4.toShortHexGuid(value as ShortHexGuid),
944
+ 'hex',
945
+ ) as RawGuidBuffer;
946
+ break;
947
+ case GuidBrandType.Base64Guid:
948
+ // Ensure value is a string before using it with Buffer.from
949
+ if (typeof value === 'string' || value instanceof Buffer) {
950
+ rawGuidBufferResult = Buffer.from(
951
+ value.toString(),
952
+ 'base64',
953
+ ) as RawGuidBuffer;
954
+ } else {
955
+ throw new GuidError(GuidErrorType.InvalidGuid);
956
+ }
957
+ break;
958
+ case GuidBrandType.RawGuidBuffer:
959
+ rawGuidBufferResult = value as RawGuidBuffer;
960
+ break;
961
+ case GuidBrandType.BigIntGuid:
962
+ rawGuidBufferResult = Buffer.from(
963
+ GuidV4.toShortHexGuid(GuidV4.toFullHexFromBigInt(value as bigint)),
964
+ 'hex',
965
+ ) as RawGuidBuffer;
966
+ break;
967
+ default:
968
+ throw new GuidError(GuidErrorType.InvalidGuidUnknownBrand);
969
+ }
970
+ if (
971
+ rawGuidBufferResult.length !==
972
+ GuidV4.guidBrandToLength(GuidBrandType.RawGuidBuffer)
973
+ ) {
974
+ throw new GuidError(
975
+ GuidErrorType.InvalidGuidUnknownLength,
976
+ undefined,
977
+ rawGuidBufferResult.length,
978
+ );
979
+ }
980
+ return rawGuidBufferResult;
981
+ }
982
+
983
+ /**
984
+ * Compare two GuidV4 instances for equality.
985
+ * @param other - The other GuidV4 instance to compare (can be null/undefined)
986
+ * @param constantTime - Use constant-time comparison to prevent timing attacks (default: false)
987
+ * @returns True if the two GuidV4 instances are equal, false otherwise
988
+ */
989
+ public equals(other: IGuidV4 | null | undefined, constantTime = false): boolean {
990
+ if (!other) {
991
+ return false;
992
+ }
993
+
994
+ if (constantTime) {
995
+ // Constant-time comparison to prevent timing attacks
996
+ // Always compare all 16 bytes regardless of early mismatches
997
+ const a = this.asRawGuidBufferUnsafe;
998
+ const b = other.asRawGuidBuffer;
999
+ let result = 0;
1000
+ for (let i = 0; i < 16; i++) {
1001
+ result |= a[i] ^ b[i];
1002
+ }
1003
+ return result === 0;
1004
+ }
1005
+
1006
+ return Buffer.compare(this.asRawGuidBufferUnsafe, other.asRawGuidBuffer) === 0;
1007
+ }
1008
+
1009
+ /**
1010
+ * Checks if this GUID is empty (all zeros).
1011
+ * @returns True if the GUID is all zeros, false otherwise
1012
+ */
1013
+ public isEmpty(): boolean {
1014
+ // Check if all bytes are zero
1015
+ for (let i = 0; i < this._value.length; i++) {
1016
+ if (this._value[i] !== 0) {
1017
+ return false;
1018
+ }
1019
+ }
1020
+ return true;
1021
+ }
1022
+
1023
+ /**
1024
+ * Static helper to check if a GUID is null, undefined, or empty.
1025
+ * @param guid The GUID to check
1026
+ * @returns True if the GUID is null, undefined, or empty
1027
+ */
1028
+ public static isNilOrEmpty(guid: IGuidV4 | null | undefined): boolean {
1029
+ return !guid || (guid instanceof GuidV4 && guid.isEmpty());
1030
+ }
1031
+
1032
+ /**
1033
+ * Creates a new GuidV4 instance with the same value as this one.
1034
+ * @returns A new GuidV4 instance with identical value
1035
+ */
1036
+ public clone(): GuidV4 {
1037
+ return new GuidV4(Buffer.from(this._value) as RawGuidBuffer);
1038
+ }
1039
+
1040
+ /**
1041
+ * Returns the hash code for this GUID based on its buffer content.
1042
+ * Useful for using GUIDs as Map/Set keys.
1043
+ * @returns A numeric hash code
1044
+ */
1045
+ public hashCode(): number {
1046
+ let hash = 0;
1047
+ for (let i = 0; i < this._value.length; i++) {
1048
+ hash = ((hash << 5) - hash) + this._value[i];
1049
+ hash = hash & hash; // Convert to 32-bit integer
1050
+ }
1051
+ return hash;
1052
+ }
1053
+
1054
+ /**
1055
+ * Extracts the RFC 4122 version from the GUID.
1056
+ * Returns undefined for boundary values or invalid GUIDs.
1057
+ * @returns The version number (1-5) or undefined
1058
+ */
1059
+ public getVersion(): number | undefined {
1060
+ // Skip boundary values
1061
+ if (GuidV4.isBoundaryValue(this.asFullHexGuid)) {
1062
+ return undefined;
1063
+ }
1064
+
1065
+ // Version is in bits 48-51 (byte 6, high nibble)
1066
+ const versionByte = this._value[6];
1067
+ const version = (versionByte >> 4) & 0x0F;
1068
+
1069
+ // Valid RFC 4122 versions are 1-5
1070
+ return version >= 1 && version <= 5 ? version : undefined;
1071
+ }
1072
+
1073
+ /**
1074
+ * Validates that this GUID is a proper v4 GUID according to RFC 4122.
1075
+ * Boundary values (all zeros/all Fs) return true as they're mathematically valid.
1076
+ * @returns True if valid v4 GUID or boundary value, false otherwise
1077
+ */
1078
+ public isValidV4(): boolean {
1079
+ // Boundary values are considered valid
1080
+ if (GuidV4.isBoundaryValue(this.asFullHexGuid)) {
1081
+ return true;
1082
+ }
1083
+
1084
+ const version = this.getVersion();
1085
+ return version === 4;
1086
+ }
1087
+
1088
+ /**
1089
+ * Compares two GUIDs for ordering.
1090
+ * Useful for sorting GUID arrays.
1091
+ * @param other The other GUID to compare to
1092
+ * @returns -1 if this < other, 0 if equal, 1 if this > other
1093
+ */
1094
+ public compareTo(other: IGuidV4): number {
1095
+ return Buffer.compare(this.asRawGuidBufferUnsafe, other.asRawGuidBuffer);
1096
+ }
1097
+ }