@digitaldefiance/ecies-lib 1.1.23 → 1.1.24
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.
- package/README.md +4 -0
- package/package.json +13 -33
- package/src/constants.ts +474 -0
- package/src/email-string.ts +83 -0
- package/src/enumerations/ecies-encryption-type.ts +102 -0
- package/src/enumerations/ecies-error-type.ts +31 -0
- package/src/enumerations/ecies-string-key.ts +108 -0
- package/src/enumerations/guid-brand-type.ts +26 -0
- package/src/enumerations/guid-error-type.ts +6 -0
- package/{dist/enumerations/index.d.ts → src/enumerations/index.ts} +0 -1
- package/src/enumerations/invalid-email-type.ts +5 -0
- package/src/enumerations/length-encoding-type.ts +6 -0
- package/src/enumerations/length-error-type.ts +5 -0
- package/src/enumerations/member-error-type.ts +106 -0
- package/{dist/enumerations/member-type.d.ts → src/enumerations/member-type.ts} +7 -6
- package/src/enumerations/password-login-error-type.ts +4 -0
- package/src/enumerations/pbkdf2-error-type.ts +5 -0
- package/src/enumerations/pbkdf2-profile.ts +5 -0
- package/src/enumerations/secure-storage-error-type.ts +5 -0
- package/src/errors/disposed.ts +15 -0
- package/src/errors/ecies.ts +34 -0
- package/src/errors/guid.ts +34 -0
- package/{dist/errors/index.d.ts → src/errors/index.ts} +0 -1
- package/src/errors/invalid-email.ts +11 -0
- package/src/errors/length.ts +11 -0
- package/src/errors/member.ts +12 -0
- package/src/errors/pbkdf2.ts +12 -0
- package/src/errors/secure-storage.ts +13 -0
- package/src/errors/simple-ecies.ts +18 -0
- package/src/errors/simple-test-error.ts +6 -0
- package/src/guid.ts +800 -0
- package/src/i18n-setup.ts +1312 -0
- package/{dist/index.d.ts → src/index.ts} +0 -1
- package/src/interfaces/checksum-config.ts +4 -0
- package/src/interfaces/checksum-consts.ts +13 -0
- package/src/interfaces/constants.ts +48 -0
- package/src/interfaces/ecies-config.ts +8 -0
- package/src/interfaces/ecies-consts.ts +70 -0
- package/src/interfaces/ecies-file-service.ts +6 -0
- package/src/interfaces/guid.ts +53 -0
- package/{dist/interfaces/index.d.ts → src/interfaces/index.ts} +0 -1
- package/src/interfaces/library-error.ts +23 -0
- package/src/interfaces/member-operational.ts +54 -0
- package/{dist/interfaces/member-storage.d.ts → src/interfaces/member-storage.ts} +11 -10
- package/{dist/interfaces/member-with-mnemonic.d.ts → src/interfaces/member-with-mnemonic.ts} +3 -3
- package/src/interfaces/pbkdf2-config.ts +6 -0
- package/src/interfaces/pbkdf2-consts.ts +10 -0
- package/src/interfaces/pbkdf2-result.ts +5 -0
- package/src/member.ts +429 -0
- package/{dist/pbkdf2-profiles.d.ts → src/pbkdf2-profiles.ts} +2 -2
- package/src/phone-number.ts +18 -0
- package/src/regexes.ts +10 -0
- package/src/secure-buffer.ts +183 -0
- package/src/secure-string.ts +229 -0
- package/src/services/aes-gcm.ts +177 -0
- package/src/services/ecies/README.md +147 -0
- package/src/services/ecies/crypto-core.ts +180 -0
- package/src/services/ecies/example.ts +185 -0
- package/src/services/ecies/file.ts +167 -0
- package/{dist/services/ecies/index.d.ts → src/services/ecies/index.ts} +3 -1
- package/src/services/ecies/integration.ts +241 -0
- package/src/services/ecies/interfaces.ts +59 -0
- package/src/services/ecies/manual-test.ts +219 -0
- package/src/services/ecies/multi-recipient.ts +394 -0
- package/src/services/ecies/service.ts +317 -0
- package/src/services/ecies/signature.ts +93 -0
- package/src/services/ecies/single-recipient.ts +340 -0
- package/{dist/services/index.d.ts → src/services/index.ts} +0 -1
- package/src/services/password-login.ts +228 -0
- package/src/services/pbkdf2.ts +172 -0
- package/src/services/xor.ts +65 -0
- package/src/types/deep-partial.ts +11 -0
- package/{dist/types.d.ts → src/types.ts} +10 -4
- package/src/utils.ts +331 -0
- package/dist/constants.d.ts +0 -46
- package/dist/constants.d.ts.map +0 -1
- package/dist/constants.js +0 -358
- package/dist/constants.js.map +0 -1
- package/dist/email-string.d.ts +0 -42
- package/dist/email-string.d.ts.map +0 -1
- package/dist/email-string.js +0 -75
- package/dist/email-string.js.map +0 -1
- package/dist/enumerations/ecies-encryption-type.d.ts +0 -15
- package/dist/enumerations/ecies-encryption-type.d.ts.map +0 -1
- package/dist/enumerations/ecies-encryption-type.js +0 -71
- package/dist/enumerations/ecies-encryption-type.js.map +0 -1
- package/dist/enumerations/ecies-error-type.d.ts +0 -32
- package/dist/enumerations/ecies-error-type.d.ts.map +0 -1
- package/dist/enumerations/ecies-error-type.js +0 -36
- package/dist/enumerations/ecies-error-type.js.map +0 -1
- package/dist/enumerations/ecies-string-key.d.ts +0 -96
- package/dist/enumerations/ecies-string-key.d.ts.map +0 -1
- package/dist/enumerations/ecies-string-key.js +0 -105
- package/dist/enumerations/ecies-string-key.js.map +0 -1
- package/dist/enumerations/guid-brand-type.d.ts +0 -27
- package/dist/enumerations/guid-brand-type.d.ts.map +0 -1
- package/dist/enumerations/guid-brand-type.js +0 -31
- package/dist/enumerations/guid-brand-type.js.map +0 -1
- package/dist/enumerations/guid-error-type.d.ts +0 -7
- package/dist/enumerations/guid-error-type.d.ts.map +0 -1
- package/dist/enumerations/guid-error-type.js +0 -11
- package/dist/enumerations/guid-error-type.js.map +0 -1
- package/dist/enumerations/index.d.ts.map +0 -1
- package/dist/enumerations/index.js +0 -31
- package/dist/enumerations/index.js.map +0 -1
- package/dist/enumerations/invalid-email-type.d.ts +0 -6
- package/dist/enumerations/invalid-email-type.d.ts.map +0 -1
- package/dist/enumerations/invalid-email-type.js +0 -10
- package/dist/enumerations/invalid-email-type.js.map +0 -1
- package/dist/enumerations/length-encoding-type.d.ts +0 -7
- package/dist/enumerations/length-encoding-type.d.ts.map +0 -1
- package/dist/enumerations/length-encoding-type.js +0 -11
- package/dist/enumerations/length-encoding-type.js.map +0 -1
- package/dist/enumerations/length-error-type.d.ts +0 -6
- package/dist/enumerations/length-error-type.d.ts.map +0 -1
- package/dist/enumerations/length-error-type.js +0 -10
- package/dist/enumerations/length-error-type.js.map +0 -1
- package/dist/enumerations/member-error-type.d.ts +0 -87
- package/dist/enumerations/member-error-type.d.ts.map +0 -1
- package/dist/enumerations/member-error-type.js +0 -91
- package/dist/enumerations/member-error-type.js.map +0 -1
- package/dist/enumerations/member-type.d.ts.map +0 -1
- package/dist/enumerations/member-type.js +0 -19
- package/dist/enumerations/member-type.js.map +0 -1
- package/dist/enumerations/password-login-error-type.d.ts +0 -5
- package/dist/enumerations/password-login-error-type.d.ts.map +0 -1
- package/dist/enumerations/password-login-error-type.js +0 -9
- package/dist/enumerations/password-login-error-type.js.map +0 -1
- package/dist/enumerations/pbkdf2-error-type.d.ts +0 -6
- package/dist/enumerations/pbkdf2-error-type.d.ts.map +0 -1
- package/dist/enumerations/pbkdf2-error-type.js +0 -10
- package/dist/enumerations/pbkdf2-error-type.js.map +0 -1
- package/dist/enumerations/pbkdf2-profile.d.ts +0 -6
- package/dist/enumerations/pbkdf2-profile.d.ts.map +0 -1
- package/dist/enumerations/pbkdf2-profile.js +0 -10
- package/dist/enumerations/pbkdf2-profile.js.map +0 -1
- package/dist/enumerations/secure-storage-error-type.d.ts +0 -6
- package/dist/enumerations/secure-storage-error-type.d.ts.map +0 -1
- package/dist/enumerations/secure-storage-error-type.js +0 -10
- package/dist/enumerations/secure-storage-error-type.js.map +0 -1
- package/dist/errors/disposed.d.ts +0 -4
- package/dist/errors/disposed.d.ts.map +0 -1
- package/dist/errors/disposed.js +0 -20
- package/dist/errors/disposed.js.map +0 -1
- package/dist/errors/ecies.d.ts +0 -7
- package/dist/errors/ecies.d.ts.map +0 -1
- package/dist/errors/ecies.js +0 -15
- package/dist/errors/ecies.js.map +0 -1
- package/dist/errors/guid.d.ts +0 -15
- package/dist/errors/guid.d.ts.map +0 -1
- package/dist/errors/guid.js +0 -26
- package/dist/errors/guid.js.map +0 -1
- package/dist/errors/index.d.ts.map +0 -1
- package/dist/errors/index.js +0 -25
- package/dist/errors/index.js.map +0 -1
- package/dist/errors/invalid-email.d.ts +0 -7
- package/dist/errors/invalid-email.d.ts.map +0 -1
- package/dist/errors/invalid-email.js +0 -14
- package/dist/errors/invalid-email.js.map +0 -1
- package/dist/errors/length.d.ts +0 -7
- package/dist/errors/length.d.ts.map +0 -1
- package/dist/errors/length.js +0 -14
- package/dist/errors/length.js.map +0 -1
- package/dist/errors/member.d.ts +0 -7
- package/dist/errors/member.d.ts.map +0 -1
- package/dist/errors/member.js +0 -14
- package/dist/errors/member.js.map +0 -1
- package/dist/errors/pbkdf2.d.ts +0 -7
- package/dist/errors/pbkdf2.d.ts.map +0 -1
- package/dist/errors/pbkdf2.js +0 -14
- package/dist/errors/pbkdf2.js.map +0 -1
- package/dist/errors/secure-storage.d.ts +0 -7
- package/dist/errors/secure-storage.d.ts.map +0 -1
- package/dist/errors/secure-storage.js +0 -15
- package/dist/errors/secure-storage.js.map +0 -1
- package/dist/errors/simple-ecies.d.ts +0 -6
- package/dist/errors/simple-ecies.d.ts.map +0 -1
- package/dist/errors/simple-ecies.js +0 -15
- package/dist/errors/simple-ecies.js.map +0 -1
- package/dist/errors/simple-test-error.d.ts +0 -4
- package/dist/errors/simple-test-error.d.ts.map +0 -1
- package/dist/errors/simple-test-error.js +0 -11
- package/dist/errors/simple-test-error.js.map +0 -1
- package/dist/guid.d.ts +0 -153
- package/dist/guid.d.ts.map +0 -1
- package/dist/guid.js +0 -647
- package/dist/guid.js.map +0 -1
- package/dist/i18n-setup.d.ts +0 -24
- package/dist/i18n-setup.d.ts.map +0 -1
- package/dist/i18n-setup.js +0 -837
- package/dist/i18n-setup.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -32
- package/dist/index.js.map +0 -1
- package/dist/interfaces/checksum-config.d.ts +0 -5
- package/dist/interfaces/checksum-config.d.ts.map +0 -1
- package/dist/interfaces/checksum-config.js +0 -3
- package/dist/interfaces/checksum-config.js.map +0 -1
- package/dist/interfaces/checksum-consts.d.ts +0 -11
- package/dist/interfaces/checksum-consts.d.ts.map +0 -1
- package/dist/interfaces/checksum-consts.js +0 -3
- package/dist/interfaces/checksum-consts.js.map +0 -1
- package/dist/interfaces/constants.d.ts +0 -45
- package/dist/interfaces/constants.d.ts.map +0 -1
- package/dist/interfaces/constants.js +0 -3
- package/dist/interfaces/constants.js.map +0 -1
- package/dist/interfaces/ecies-config.d.ts +0 -9
- package/dist/interfaces/ecies-config.d.ts.map +0 -1
- package/dist/interfaces/ecies-config.js +0 -3
- package/dist/interfaces/ecies-config.js.map +0 -1
- package/dist/interfaces/ecies-consts.d.ts +0 -58
- package/dist/interfaces/ecies-consts.d.ts.map +0 -1
- package/dist/interfaces/ecies-consts.js +0 -3
- package/dist/interfaces/ecies-consts.js.map +0 -1
- package/dist/interfaces/ecies-file-service.d.ts +0 -7
- package/dist/interfaces/ecies-file-service.d.ts.map +0 -1
- package/dist/interfaces/ecies-file-service.js +0 -3
- package/dist/interfaces/ecies-file-service.js.map +0 -1
- package/dist/interfaces/guid.d.ts +0 -45
- package/dist/interfaces/guid.d.ts.map +0 -1
- package/dist/interfaces/guid.js +0 -3
- package/dist/interfaces/guid.js.map +0 -1
- package/dist/interfaces/index.d.ts.map +0 -1
- package/dist/interfaces/index.js +0 -30
- package/dist/interfaces/index.js.map +0 -1
- package/dist/interfaces/library-error.d.ts +0 -23
- package/dist/interfaces/library-error.d.ts.map +0 -1
- package/dist/interfaces/library-error.js +0 -3
- package/dist/interfaces/library-error.js.map +0 -1
- package/dist/interfaces/member-operational.d.ts +0 -40
- package/dist/interfaces/member-operational.d.ts.map +0 -1
- package/dist/interfaces/member-operational.js +0 -3
- package/dist/interfaces/member-operational.js.map +0 -1
- package/dist/interfaces/member-storage.d.ts.map +0 -1
- package/dist/interfaces/member-storage.js +0 -3
- package/dist/interfaces/member-storage.js.map +0 -1
- package/dist/interfaces/member-with-mnemonic.d.ts.map +0 -1
- package/dist/interfaces/member-with-mnemonic.js +0 -3
- package/dist/interfaces/member-with-mnemonic.js.map +0 -1
- package/dist/interfaces/pbkdf2-config.d.ts +0 -7
- package/dist/interfaces/pbkdf2-config.d.ts.map +0 -1
- package/dist/interfaces/pbkdf2-config.js +0 -3
- package/dist/interfaces/pbkdf2-config.js.map +0 -1
- package/dist/interfaces/pbkdf2-consts.d.ts +0 -9
- package/dist/interfaces/pbkdf2-consts.d.ts.map +0 -1
- package/dist/interfaces/pbkdf2-consts.js +0 -3
- package/dist/interfaces/pbkdf2-consts.js.map +0 -1
- package/dist/interfaces/pbkdf2-result.d.ts +0 -6
- package/dist/interfaces/pbkdf2-result.d.ts.map +0 -1
- package/dist/interfaces/pbkdf2-result.js +0 -3
- package/dist/interfaces/pbkdf2-result.js.map +0 -1
- package/dist/member.d.ts +0 -66
- package/dist/member.d.ts.map +0 -1
- package/dist/member.js +0 -271
- package/dist/member.js.map +0 -1
- package/dist/pbkdf2-profiles.d.ts.map +0 -1
- package/dist/pbkdf2-profiles.js +0 -3
- package/dist/pbkdf2-profiles.js.map +0 -1
- package/dist/phone-number.d.ts +0 -6
- package/dist/phone-number.d.ts.map +0 -1
- package/dist/phone-number.js +0 -22
- package/dist/phone-number.js.map +0 -1
- package/dist/regexes.d.ts +0 -7
- package/dist/regexes.d.ts.map +0 -1
- package/dist/regexes.js +0 -10
- package/dist/regexes.js.map +0 -1
- package/dist/secure-buffer.d.ts +0 -38
- package/dist/secure-buffer.d.ts.map +0 -1
- package/dist/secure-buffer.js +0 -168
- package/dist/secure-buffer.js.map +0 -1
- package/dist/secure-string.d.ts +0 -39
- package/dist/secure-string.d.ts.map +0 -1
- package/dist/secure-string.js +0 -195
- package/dist/secure-string.js.map +0 -1
- package/dist/services/aes-gcm.d.ts +0 -57
- package/dist/services/aes-gcm.d.ts.map +0 -1
- package/dist/services/aes-gcm.js +0 -111
- package/dist/services/aes-gcm.js.map +0 -1
- package/dist/services/ecies/crypto-core.d.ts +0 -51
- package/dist/services/ecies/crypto-core.d.ts.map +0 -1
- package/dist/services/ecies/crypto-core.js +0 -139
- package/dist/services/ecies/crypto-core.js.map +0 -1
- package/dist/services/ecies/example.d.ts +0 -25
- package/dist/services/ecies/example.d.ts.map +0 -1
- package/dist/services/ecies/example.js +0 -128
- package/dist/services/ecies/example.js.map +0 -1
- package/dist/services/ecies/file.d.ts +0 -18
- package/dist/services/ecies/file.d.ts.map +0 -1
- package/dist/services/ecies/file.js +0 -110
- package/dist/services/ecies/file.js.map +0 -1
- package/dist/services/ecies/index.d.ts.map +0 -1
- package/dist/services/ecies/index.js +0 -57
- package/dist/services/ecies/index.js.map +0 -1
- package/dist/services/ecies/integration.d.ts +0 -59
- package/dist/services/ecies/integration.d.ts.map +0 -1
- package/dist/services/ecies/integration.js +0 -172
- package/dist/services/ecies/integration.js.map +0 -1
- package/dist/services/ecies/interfaces.d.ts +0 -51
- package/dist/services/ecies/interfaces.d.ts.map +0 -1
- package/dist/services/ecies/interfaces.js +0 -6
- package/dist/services/ecies/interfaces.js.map +0 -1
- package/dist/services/ecies/manual-test.d.ts +0 -29
- package/dist/services/ecies/manual-test.d.ts.map +0 -1
- package/dist/services/ecies/manual-test.js +0 -171
- package/dist/services/ecies/manual-test.js.map +0 -1
- package/dist/services/ecies/multi-recipient.d.ts +0 -52
- package/dist/services/ecies/multi-recipient.d.ts.map +0 -1
- package/dist/services/ecies/multi-recipient.js +0 -243
- package/dist/services/ecies/multi-recipient.js.map +0 -1
- package/dist/services/ecies/service.d.ts +0 -104
- package/dist/services/ecies/service.d.ts.map +0 -1
- package/dist/services/ecies/service.js +0 -192
- package/dist/services/ecies/service.js.map +0 -1
- package/dist/services/ecies/signature.d.ts +0 -27
- package/dist/services/ecies/signature.d.ts.map +0 -1
- package/dist/services/ecies/signature.js +0 -76
- package/dist/services/ecies/signature.js.map +0 -1
- package/dist/services/ecies/single-recipient.d.ts +0 -46
- package/dist/services/ecies/single-recipient.d.ts.map +0 -1
- package/dist/services/ecies/single-recipient.js +0 -212
- package/dist/services/ecies/single-recipient.js.map +0 -1
- package/dist/services/index.d.ts.map +0 -1
- package/dist/services/index.js +0 -22
- package/dist/services/index.js.map +0 -1
- package/dist/services/password-login.d.ts +0 -49
- package/dist/services/password-login.d.ts.map +0 -1
- package/dist/services/password-login.js +0 -121
- package/dist/services/password-login.js.map +0 -1
- package/dist/services/pbkdf2.d.ts +0 -56
- package/dist/services/pbkdf2.d.ts.map +0 -1
- package/dist/services/pbkdf2.js +0 -114
- package/dist/services/pbkdf2.js.map +0 -1
- package/dist/services/xor.d.ts +0 -37
- package/dist/services/xor.d.ts.map +0 -1
- package/dist/services/xor.js +0 -67
- package/dist/services/xor.js.map +0 -1
- package/dist/types/deep-partial.d.ts +0 -4
- package/dist/types/deep-partial.d.ts.map +0 -1
- package/dist/types/deep-partial.js +0 -3
- package/dist/types/deep-partial.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -3
- package/dist/types.js.map +0 -1
- package/dist/utils.d.ts +0 -68
- package/dist/utils.d.ts.map +0 -1
- package/dist/utils.js +0 -288
- package/dist/utils.js.map +0 -1
package/src/guid.ts
ADDED
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
import * as uuid from 'uuid';
|
|
2
|
+
import { GuidBrandType } from './enumerations/guid-brand-type';
|
|
3
|
+
import { GuidErrorType } from './enumerations/guid-error-type';
|
|
4
|
+
import { GuidError } from './errors/guid';
|
|
5
|
+
import { getEciesI18nEngine } from './i18n-setup';
|
|
6
|
+
import { IGuidV4 } from './interfaces/guid';
|
|
7
|
+
import {
|
|
8
|
+
Base64Guid,
|
|
9
|
+
BigIntGuid,
|
|
10
|
+
FullHexGuid,
|
|
11
|
+
RawGuidUint8Array,
|
|
12
|
+
ShortHexGuid,
|
|
13
|
+
} from './types';
|
|
14
|
+
import { base64ToUint8Array, uint8ArrayToHex } from './utils';
|
|
15
|
+
|
|
16
|
+
// Re-export Base64Guid so it's available to importers of guid.ts
|
|
17
|
+
export type { Base64Guid };
|
|
18
|
+
|
|
19
|
+
// Define a type that can handle all GUID variants
|
|
20
|
+
export type GuidInput =
|
|
21
|
+
| string
|
|
22
|
+
| FullHexGuid
|
|
23
|
+
| ShortHexGuid
|
|
24
|
+
| Base64Guid
|
|
25
|
+
| BigIntGuid
|
|
26
|
+
| RawGuidUint8Array
|
|
27
|
+
| bigint
|
|
28
|
+
| Uint8Array;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* GuidV4 represents a GUID (Globally Unique Identifier) that is compliant with the RFC 4122 standard.
|
|
32
|
+
* GuidV4 instances can be created from a variety of input types, including:
|
|
33
|
+
* - FullHexGuid: A 36-character string representation of the GUID, including dashes
|
|
34
|
+
* - ShortHexGuid: A 32-character string representation of the GUID, excluding dashes
|
|
35
|
+
* - Base64Guid: A 24-character base64-encoded string representation of the GUID
|
|
36
|
+
* - BigIntGuid: A bigint representation of the GUID
|
|
37
|
+
* - RawGuidUint8Array: A 16-byte Uint8Array representation of the GUID
|
|
38
|
+
* GuidV4 instances can be converted to any of these representations using the appropriate method.
|
|
39
|
+
*/
|
|
40
|
+
export class GuidV4 implements IGuidV4 {
|
|
41
|
+
/**
|
|
42
|
+
* GUID is stored internally as a raw 16-byte Uint8Array.
|
|
43
|
+
*/
|
|
44
|
+
private readonly _value: RawGuidUint8Array;
|
|
45
|
+
constructor(value: GuidInput) {
|
|
46
|
+
// Check for UnknownLength issues first - this applies to any input type
|
|
47
|
+
let brandCheckPassed = false;
|
|
48
|
+
try {
|
|
49
|
+
GuidV4.whichBrand(value);
|
|
50
|
+
brandCheckPassed = true;
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (
|
|
53
|
+
error instanceof GuidError &&
|
|
54
|
+
(error.type === GuidErrorType.UnknownLength ||
|
|
55
|
+
error.type === GuidErrorType.UnknownBrand)
|
|
56
|
+
) {
|
|
57
|
+
// For Uint8Array with wrong length, treat as Invalid rather than UnknownLength
|
|
58
|
+
if (
|
|
59
|
+
value instanceof Uint8Array &&
|
|
60
|
+
error.type === GuidErrorType.UnknownLength
|
|
61
|
+
) {
|
|
62
|
+
throw new GuidError(
|
|
63
|
+
GuidErrorType.Invalid,
|
|
64
|
+
getEciesI18nEngine() as any,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
throw error; // Let UnknownLength/UnknownBrand bubble up for other types
|
|
68
|
+
}
|
|
69
|
+
// For other errors from whichBrand, continue with validation
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Only do validation if brand check passed
|
|
73
|
+
if (brandCheckPassed) {
|
|
74
|
+
try {
|
|
75
|
+
if (!GuidV4.isValid(value as any)) {
|
|
76
|
+
throw new GuidError(
|
|
77
|
+
GuidErrorType.Invalid,
|
|
78
|
+
getEciesI18nEngine() as any,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (error instanceof GuidError) {
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
throw new GuidError(
|
|
86
|
+
GuidErrorType.Invalid,
|
|
87
|
+
getEciesI18nEngine() as any,
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this._value = GuidV4.toRawGuidUint8Array(value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public static isValid(
|
|
96
|
+
value:
|
|
97
|
+
| string
|
|
98
|
+
| FullHexGuid
|
|
99
|
+
| ShortHexGuid
|
|
100
|
+
| Base64Guid
|
|
101
|
+
| BigIntGuid
|
|
102
|
+
| RawGuidUint8Array,
|
|
103
|
+
) {
|
|
104
|
+
try {
|
|
105
|
+
if (value === null || value === undefined) {
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const strValue = String(value);
|
|
109
|
+
if (!strValue && value !== 0n) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
const expectedBrand = GuidV4.whichBrand(value);
|
|
113
|
+
const verifiedBrand = GuidV4.verifyGuid(expectedBrand, value);
|
|
114
|
+
if (!verifiedBrand) {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
const uintArray = GuidV4.toRawGuidUint8Array(value);
|
|
118
|
+
// Skip UUID validation for boundary values that are mathematically valid but not RFC 4122 compliant
|
|
119
|
+
const fullHex = GuidV4.toFullHexGuid(uintArray);
|
|
120
|
+
const isAllZeros = fullHex === '00000000-0000-0000-0000-000000000000';
|
|
121
|
+
const isAllFs = fullHex === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
|
|
122
|
+
if (!isAllZeros && !isAllFs && !uuid.validate(fullHex)) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
return true;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public static validateUuid(value: string): boolean {
|
|
132
|
+
return uuid.validate(value);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
public serialize(): string {
|
|
136
|
+
return this.asBase64Guid;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
public static hydrate(value: string): GuidV4 {
|
|
140
|
+
return new GuidV4(value as Base64Guid);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private static readonly LengthMap: Record<GuidBrandType, number> = {
|
|
144
|
+
[GuidBrandType.Unknown]: -1,
|
|
145
|
+
[GuidBrandType.FullHexGuid]: 36,
|
|
146
|
+
[GuidBrandType.ShortHexGuid]: 32,
|
|
147
|
+
[GuidBrandType.Base64Guid]: 24,
|
|
148
|
+
[GuidBrandType.RawGuidUint8Array]: 16,
|
|
149
|
+
[GuidBrandType.BigIntGuid]: -1, // Variable length
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
private static readonly ReverseLengthMap: Record<number, GuidBrandType> = {
|
|
153
|
+
0: GuidBrandType.Unknown,
|
|
154
|
+
36: GuidBrandType.FullHexGuid,
|
|
155
|
+
32: GuidBrandType.ShortHexGuid,
|
|
156
|
+
24: GuidBrandType.Base64Guid,
|
|
157
|
+
16: GuidBrandType.RawGuidUint8Array,
|
|
158
|
+
// BigIntGuid is variable length, so it is not included in the reverse map
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
private static readonly VerifyFunctions: Record<
|
|
162
|
+
GuidBrandType,
|
|
163
|
+
(guid: GuidInput, validate?: boolean) => boolean
|
|
164
|
+
> = {
|
|
165
|
+
[GuidBrandType.Unknown]: () => false,
|
|
166
|
+
[GuidBrandType.FullHexGuid]: (guid: GuidInput) => this.isFullHexGuid(guid),
|
|
167
|
+
[GuidBrandType.ShortHexGuid]: (guid: GuidInput) =>
|
|
168
|
+
this.isShortHexGuid(guid),
|
|
169
|
+
[GuidBrandType.Base64Guid]: (guid: GuidInput) => this.isBase64Guid(guid),
|
|
170
|
+
[GuidBrandType.BigIntGuid]: (guid: GuidInput) => this.isBigIntGuid(guid),
|
|
171
|
+
[GuidBrandType.RawGuidUint8Array]: (guid: GuidInput) =>
|
|
172
|
+
this.isRawGuidUint8Array(guid),
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns the GUID as a raw Uint8Array.
|
|
177
|
+
*/
|
|
178
|
+
public get asRawGuidUint8Array(): RawGuidUint8Array {
|
|
179
|
+
return this._value;
|
|
180
|
+
}
|
|
181
|
+
public static new<
|
|
182
|
+
TStringKey extends string,
|
|
183
|
+
TLanguage extends string,
|
|
184
|
+
>(): GuidV4 {
|
|
185
|
+
try {
|
|
186
|
+
const uuidStr = uuid.v4();
|
|
187
|
+
if (!uuidStr) {
|
|
188
|
+
throw new GuidError(
|
|
189
|
+
GuidErrorType.Invalid,
|
|
190
|
+
getEciesI18nEngine() as any,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
return new GuidV4(uuidStr as FullHexGuid);
|
|
194
|
+
} catch (error) {
|
|
195
|
+
// If there's an error creating the GUID, throw a more specific error
|
|
196
|
+
if (error instanceof GuidError) {
|
|
197
|
+
throw error;
|
|
198
|
+
}
|
|
199
|
+
throw new GuidError(
|
|
200
|
+
GuidErrorType.Invalid,
|
|
201
|
+
getEciesI18nEngine() as any,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Returns the GUID as a full hex string.
|
|
207
|
+
*/
|
|
208
|
+
public get asFullHexGuid(): FullHexGuid {
|
|
209
|
+
return GuidV4.toFullHexGuid(this._value) as FullHexGuid;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Returns the GUID as a raw Uint8Array.
|
|
213
|
+
*/
|
|
214
|
+
public get asUint8Array(): Uint8Array {
|
|
215
|
+
return this._value as Uint8Array;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Returns the GUID as a short hex string.
|
|
219
|
+
*/
|
|
220
|
+
public get asShortHexGuid(): ShortHexGuid {
|
|
221
|
+
return GuidV4.toShortHexGuid(this.asFullHexGuid) as ShortHexGuid;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Returns the GUID as a base64 string.
|
|
225
|
+
*/
|
|
226
|
+
public toString(): Base64Guid {
|
|
227
|
+
return this.asBase64Guid as Base64Guid;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Returns the GUID as a JSON string.
|
|
231
|
+
* @returns The GUID as a JSON string.
|
|
232
|
+
*/
|
|
233
|
+
public toJson(): string {
|
|
234
|
+
return JSON.stringify(this.asBase64Guid);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Returns the GUID as a bigint.
|
|
238
|
+
*/
|
|
239
|
+
public get asBigIntGuid(): BigIntGuid {
|
|
240
|
+
const hex = uint8ArrayToHex(this._value);
|
|
241
|
+
return BigInt('0x' + hex) as BigIntGuid;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Returns the GUID as a base64 string.
|
|
245
|
+
*/
|
|
246
|
+
public get asBase64Guid(): Base64Guid {
|
|
247
|
+
return btoa(String.fromCharCode(...this._value)) as Base64Guid;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Verifies if a given GUID is valid for the given brand.
|
|
252
|
+
* @param guidBrand The brand of the GUID to verify.
|
|
253
|
+
* @param guid The GUID to verify.
|
|
254
|
+
* @returns True if the GUID is valid for the given brand, false otherwise.
|
|
255
|
+
*/
|
|
256
|
+
public static verifyGuid(guidBrand: GuidBrandType, guid: GuidInput): boolean {
|
|
257
|
+
if (guid === null || guid === undefined) {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
try {
|
|
261
|
+
const verifyFunc = GuidV4.VerifyFunctions[guidBrand];
|
|
262
|
+
return verifyFunc(guid);
|
|
263
|
+
} catch {
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Returns the length of the GUID for the given brand.
|
|
270
|
+
* @param guidBrand The brand of the GUID to get the length for.
|
|
271
|
+
* @returns The length of the GUID for the given brand.
|
|
272
|
+
*/
|
|
273
|
+
public static guidBrandToLength<
|
|
274
|
+
TStringKey extends string,
|
|
275
|
+
TLanguage extends string,
|
|
276
|
+
>(guidBrand: GuidBrandType): number {
|
|
277
|
+
const length = GuidV4.LengthMap[guidBrand];
|
|
278
|
+
if (length <= 0) {
|
|
279
|
+
throw new GuidError(
|
|
280
|
+
GuidErrorType.UnknownBrand,
|
|
281
|
+
getEciesI18nEngine() as any,
|
|
282
|
+
guidBrand,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return length as number;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Returns the brand of the GUID for the given length.
|
|
290
|
+
* @param length The length of the GUID to get the brand for.
|
|
291
|
+
* @param isUint8Array Whether the GUID is a Uint8Array.
|
|
292
|
+
* @returns The brand of the GUID for the given length.
|
|
293
|
+
*/
|
|
294
|
+
public static lengthToGuidBrand<
|
|
295
|
+
TStringKey extends string,
|
|
296
|
+
TLanguage extends string,
|
|
297
|
+
>(length: number, isUint8Array: boolean): GuidBrandType {
|
|
298
|
+
if (length <= 0) {
|
|
299
|
+
throw new GuidError(
|
|
300
|
+
GuidErrorType.UnknownLength,
|
|
301
|
+
getEciesI18nEngine() as any,
|
|
302
|
+
undefined,
|
|
303
|
+
length,
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
const keys = Object.keys(GuidV4.ReverseLengthMap);
|
|
307
|
+
const values = Object.values(GuidV4.ReverseLengthMap);
|
|
308
|
+
for (let i = 0; i < keys.length; i++) {
|
|
309
|
+
const len = parseInt(keys[i]);
|
|
310
|
+
const brand = values[i];
|
|
311
|
+
if (len === length) {
|
|
312
|
+
if (isUint8Array && !brand.endsWith('Uint8Array')) {
|
|
313
|
+
continue;
|
|
314
|
+
} else if (!isUint8Array && brand.endsWith('Uint8Array')) {
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
return brand;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
throw new GuidError(
|
|
321
|
+
GuidErrorType.UnknownLength,
|
|
322
|
+
getEciesI18nEngine() as any,
|
|
323
|
+
undefined,
|
|
324
|
+
length,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* Verifies if a given GUID is a valid full hex GUID.
|
|
330
|
+
* @param fullHexGuidValue The full hex GUID to verify.
|
|
331
|
+
* @returns True if the GUID is a valid full hex GUID, false otherwise.
|
|
332
|
+
*/
|
|
333
|
+
public static isFullHexGuid(fullHexGuidValue: GuidInput): boolean {
|
|
334
|
+
try {
|
|
335
|
+
if (fullHexGuidValue === null || fullHexGuidValue === undefined) {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
const expectedLength = GuidV4.guidBrandToLength(
|
|
339
|
+
GuidBrandType.FullHexGuid,
|
|
340
|
+
);
|
|
341
|
+
const strValue = String(fullHexGuidValue);
|
|
342
|
+
|
|
343
|
+
if (strValue.length !== expectedLength) {
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Allow boundary values (all zeros and all fs)
|
|
348
|
+
const isAllZeros = strValue === '00000000-0000-0000-0000-000000000000';
|
|
349
|
+
const isAllFs = strValue === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
|
|
350
|
+
return isAllZeros || isAllFs || GuidV4.validateUuid(strValue);
|
|
351
|
+
} catch {
|
|
352
|
+
return false;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Verifies if a given GUID is a valid short hex GUID.
|
|
358
|
+
* @param shortHexGuidValue The short hex GUID to verify.
|
|
359
|
+
* @returns True if the GUID is a valid short hex GUID, false otherwise.
|
|
360
|
+
*/
|
|
361
|
+
public static isShortHexGuid(shortHexGuidValue: GuidInput): boolean {
|
|
362
|
+
try {
|
|
363
|
+
if (shortHexGuidValue === null || shortHexGuidValue === undefined) {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
const expectedLength = GuidV4.guidBrandToLength(
|
|
367
|
+
GuidBrandType.ShortHexGuid,
|
|
368
|
+
);
|
|
369
|
+
const strValue = String(shortHexGuidValue);
|
|
370
|
+
|
|
371
|
+
if (strValue.length !== expectedLength) {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
try {
|
|
376
|
+
const fullHexGuid = GuidV4.toFullHexGuid(strValue);
|
|
377
|
+
// Allow boundary values (all zeros and all fs)
|
|
378
|
+
const isAllZeros =
|
|
379
|
+
fullHexGuid === '00000000-0000-0000-0000-000000000000';
|
|
380
|
+
const isAllFs = fullHexGuid === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
|
|
381
|
+
return isAllZeros || isAllFs || uuid.validate(fullHexGuid);
|
|
382
|
+
} catch {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
} catch {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Verifies if a given GUID is a valid base64 GUID.
|
|
392
|
+
* @param value The base64 GUID to verify.
|
|
393
|
+
* @returns True if the GUID is a valid base64 GUID, false otherwise.
|
|
394
|
+
*/
|
|
395
|
+
public static isBase64Guid(value: GuidInput): boolean {
|
|
396
|
+
try {
|
|
397
|
+
if (value === null || value === undefined) {
|
|
398
|
+
return false;
|
|
399
|
+
}
|
|
400
|
+
let valueLength: number;
|
|
401
|
+
if (typeof value === 'bigint') {
|
|
402
|
+
valueLength = value.toString(16).length;
|
|
403
|
+
} else if (value instanceof Uint8Array) {
|
|
404
|
+
valueLength = value.length;
|
|
405
|
+
} else {
|
|
406
|
+
valueLength = String(value).length;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
const result =
|
|
410
|
+
valueLength === GuidV4.guidBrandToLength(GuidBrandType.Base64Guid);
|
|
411
|
+
|
|
412
|
+
if (result) {
|
|
413
|
+
try {
|
|
414
|
+
const fromBase64: RawGuidUint8Array =
|
|
415
|
+
GuidV4.toRawGuidUint8Array(value);
|
|
416
|
+
const hex = uint8ArrayToHex(fromBase64);
|
|
417
|
+
const fullHexGuid = GuidV4.toFullHexGuid(hex);
|
|
418
|
+
// Allow boundary values (all zeros and all fs)
|
|
419
|
+
const isAllZeros =
|
|
420
|
+
fullHexGuid === '00000000-0000-0000-0000-000000000000';
|
|
421
|
+
const isAllFs =
|
|
422
|
+
fullHexGuid === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
|
|
423
|
+
return isAllZeros || isAllFs || uuid.validate(fullHexGuid);
|
|
424
|
+
} catch {
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return result;
|
|
429
|
+
} catch {
|
|
430
|
+
return false;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/**
|
|
435
|
+
* Verifies if a given GUID is a valid raw GUID buffer.
|
|
436
|
+
* @param value The raw GUID buffer to verify.
|
|
437
|
+
* @returns True if the GUID is a valid raw GUID buffer, false otherwise.
|
|
438
|
+
*/
|
|
439
|
+
public static isRawGuidUint8Array(value: GuidInput): boolean {
|
|
440
|
+
try {
|
|
441
|
+
if (value === null || value === undefined) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
const expectedLength = GuidV4.guidBrandToLength(
|
|
445
|
+
GuidBrandType.RawGuidUint8Array,
|
|
446
|
+
);
|
|
447
|
+
let valueLength: number;
|
|
448
|
+
if (typeof value === 'bigint') {
|
|
449
|
+
valueLength = value.toString(16).length;
|
|
450
|
+
} else if (value instanceof Uint8Array) {
|
|
451
|
+
valueLength = value.length;
|
|
452
|
+
} else {
|
|
453
|
+
valueLength = String(value).length;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (valueLength !== expectedLength) {
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
try {
|
|
461
|
+
if (!(value instanceof Uint8Array)) {
|
|
462
|
+
return false;
|
|
463
|
+
}
|
|
464
|
+
const hex = uint8ArrayToHex(value);
|
|
465
|
+
const fullHexGuid = GuidV4.toFullHexGuid(hex);
|
|
466
|
+
// Allow boundary values (all zeros and all fs)
|
|
467
|
+
const isAllZeros =
|
|
468
|
+
fullHexGuid === '00000000-0000-0000-0000-000000000000';
|
|
469
|
+
const isAllFs = fullHexGuid === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
|
|
470
|
+
return isAllZeros || isAllFs || GuidV4.validateUuid(fullHexGuid);
|
|
471
|
+
} catch {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
} catch {
|
|
475
|
+
return false;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Verifies if a given GUID is a valid bigint GUID.
|
|
481
|
+
* @param value The bigint GUID to verify.
|
|
482
|
+
* @returns True if the GUID is a valid bigint GUID, false otherwise.
|
|
483
|
+
*/
|
|
484
|
+
public static isBigIntGuid(value: GuidInput): boolean {
|
|
485
|
+
try {
|
|
486
|
+
if (value === null || value === undefined) {
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
if (typeof value !== 'bigint') {
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
if (value < 0n) {
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
const bigIntString = value.toString(16);
|
|
496
|
+
if (bigIntString.length > 32) {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
try {
|
|
501
|
+
const fromBigInt = GuidV4.toFullHexFromBigInt(value);
|
|
502
|
+
// Allow boundary values (all zeros and all fs)
|
|
503
|
+
const isAllZeros =
|
|
504
|
+
fromBigInt === '00000000-0000-0000-0000-000000000000';
|
|
505
|
+
const isAllFs = fromBigInt === 'ffffffff-ffff-ffff-ffff-ffffffffffff';
|
|
506
|
+
return isAllZeros || isAllFs || uuid.validate(fromBigInt);
|
|
507
|
+
} catch {
|
|
508
|
+
return false;
|
|
509
|
+
}
|
|
510
|
+
} catch {
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Determines the brand of a given GUID value.
|
|
517
|
+
* @param value The GUID value to determine the brand of.
|
|
518
|
+
* @returns The brand of the GUID value.
|
|
519
|
+
*/
|
|
520
|
+
public static whichBrand(value: GuidInput): GuidBrandType {
|
|
521
|
+
if (value === null || value === undefined) {
|
|
522
|
+
throw new GuidError(
|
|
523
|
+
GuidErrorType.Invalid,
|
|
524
|
+
getEciesI18nEngine() as any,
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
if (typeof value === 'bigint') {
|
|
528
|
+
return GuidBrandType.BigIntGuid;
|
|
529
|
+
}
|
|
530
|
+
const isUint8Array = value instanceof Uint8Array;
|
|
531
|
+
let expectedLength: number;
|
|
532
|
+
|
|
533
|
+
if (typeof value === 'bigint') {
|
|
534
|
+
// For bigint, we'll use the string representation length
|
|
535
|
+
const bigintValue = value as bigint;
|
|
536
|
+
expectedLength = bigintValue.toString(16).length;
|
|
537
|
+
} else if (isUint8Array) {
|
|
538
|
+
expectedLength = (value as Uint8Array).length;
|
|
539
|
+
} else {
|
|
540
|
+
expectedLength = String(value).length;
|
|
541
|
+
}
|
|
542
|
+
return GuidV4.lengthToGuidBrand(expectedLength, isUint8Array);
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
/**
|
|
546
|
+
* Converts a given short hex GUID to a full hex GUID.
|
|
547
|
+
* @param shortGuid The short hex GUID to convert.
|
|
548
|
+
* @returns The short hex GUID as a full hex GUID.
|
|
549
|
+
*/
|
|
550
|
+
private static shortGuidToFullGuid(shortGuid: string): FullHexGuid {
|
|
551
|
+
// insert dashes
|
|
552
|
+
const str = shortGuid.replace(
|
|
553
|
+
/(.{8})(.{4})(.{4})(.{4})(.{12})/,
|
|
554
|
+
'$1-$2-$3-$4-$5',
|
|
555
|
+
);
|
|
556
|
+
return str as FullHexGuid;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Converts a given GUID value to a full hex GUID.
|
|
561
|
+
* @param guid The GUID value to convert.
|
|
562
|
+
* @param dateCreated The date created for Mongoose IDs
|
|
563
|
+
* @returns The GUID value as a full hex GUID.
|
|
564
|
+
*/
|
|
565
|
+
public static toFullHexGuid<
|
|
566
|
+
TStringKey extends string,
|
|
567
|
+
TLanguage extends string,
|
|
568
|
+
>(
|
|
569
|
+
guid:
|
|
570
|
+
| RawGuidUint8Array
|
|
571
|
+
| BigIntGuid
|
|
572
|
+
| Base64Guid
|
|
573
|
+
| ShortHexGuid
|
|
574
|
+
| FullHexGuid
|
|
575
|
+
| string,
|
|
576
|
+
): FullHexGuid {
|
|
577
|
+
if (!guid) {
|
|
578
|
+
throw new GuidError(
|
|
579
|
+
GuidErrorType.Invalid,
|
|
580
|
+
getEciesI18nEngine() as any,
|
|
581
|
+
);
|
|
582
|
+
} else if (typeof guid === 'bigint') {
|
|
583
|
+
return GuidV4.toFullHexFromBigInt(guid);
|
|
584
|
+
} else if (
|
|
585
|
+
guid instanceof Uint8Array &&
|
|
586
|
+
guid.length === GuidV4.guidBrandToLength(GuidBrandType.RawGuidUint8Array)
|
|
587
|
+
) {
|
|
588
|
+
const shortHex = uint8ArrayToHex(guid) as ShortHexGuid;
|
|
589
|
+
return GuidV4.shortGuidToFullGuid(shortHex);
|
|
590
|
+
} else if (guid instanceof Uint8Array) {
|
|
591
|
+
throw new GuidError(
|
|
592
|
+
GuidErrorType.Invalid,
|
|
593
|
+
getEciesI18nEngine() as any,
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
// all remaining cases are string types
|
|
597
|
+
const strValue = String(guid);
|
|
598
|
+
if (
|
|
599
|
+
strValue.length === GuidV4.guidBrandToLength(GuidBrandType.ShortHexGuid)
|
|
600
|
+
) {
|
|
601
|
+
// short hex guid
|
|
602
|
+
return GuidV4.shortGuidToFullGuid(strValue);
|
|
603
|
+
} else if (
|
|
604
|
+
strValue.length === GuidV4.guidBrandToLength(GuidBrandType.FullHexGuid)
|
|
605
|
+
) {
|
|
606
|
+
// already a full hex guid
|
|
607
|
+
return strValue as FullHexGuid;
|
|
608
|
+
} else if (
|
|
609
|
+
strValue.length === GuidV4.guidBrandToLength(GuidBrandType.Base64Guid)
|
|
610
|
+
) {
|
|
611
|
+
// base64 guid
|
|
612
|
+
const shortGuid = uint8ArrayToHex(base64ToUint8Array(strValue));
|
|
613
|
+
return GuidV4.shortGuidToFullGuid(shortGuid);
|
|
614
|
+
} else {
|
|
615
|
+
throw new GuidError(
|
|
616
|
+
GuidErrorType.Invalid,
|
|
617
|
+
getEciesI18nEngine() as any,
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
public static toShortHexGuid<
|
|
623
|
+
TStringKey extends string,
|
|
624
|
+
TLanguage extends string,
|
|
625
|
+
>(
|
|
626
|
+
guid:
|
|
627
|
+
| RawGuidUint8Array
|
|
628
|
+
| BigIntGuid
|
|
629
|
+
| Base64Guid
|
|
630
|
+
| ShortHexGuid
|
|
631
|
+
| FullHexGuid
|
|
632
|
+
| string,
|
|
633
|
+
): ShortHexGuid {
|
|
634
|
+
if (!guid) {
|
|
635
|
+
throw new GuidError(
|
|
636
|
+
GuidErrorType.Invalid,
|
|
637
|
+
getEciesI18nEngine() as any,
|
|
638
|
+
);
|
|
639
|
+
} else if (typeof guid === 'bigint') {
|
|
640
|
+
const fullHex = GuidV4.toFullHexFromBigInt(guid);
|
|
641
|
+
return fullHex.replace(/-/g, '') as ShortHexGuid;
|
|
642
|
+
} else if (
|
|
643
|
+
guid instanceof Uint8Array &&
|
|
644
|
+
guid.length === GuidV4.guidBrandToLength(GuidBrandType.RawGuidUint8Array)
|
|
645
|
+
) {
|
|
646
|
+
return uint8ArrayToHex(guid) as ShortHexGuid;
|
|
647
|
+
} else if (guid instanceof Uint8Array) {
|
|
648
|
+
throw new GuidError(
|
|
649
|
+
GuidErrorType.Invalid,
|
|
650
|
+
getEciesI18nEngine() as any,
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
// all remaining cases are string types
|
|
654
|
+
const strValue = String(guid);
|
|
655
|
+
|
|
656
|
+
if (
|
|
657
|
+
strValue.length === GuidV4.guidBrandToLength(GuidBrandType.ShortHexGuid)
|
|
658
|
+
) {
|
|
659
|
+
// already a short hex guid
|
|
660
|
+
return strValue as ShortHexGuid;
|
|
661
|
+
} else if (
|
|
662
|
+
strValue.length === GuidV4.guidBrandToLength(GuidBrandType.FullHexGuid)
|
|
663
|
+
) {
|
|
664
|
+
// full hex guid
|
|
665
|
+
return strValue.replace(/-/g, '') as ShortHexGuid;
|
|
666
|
+
} else if (
|
|
667
|
+
strValue.length === GuidV4.guidBrandToLength(GuidBrandType.Base64Guid)
|
|
668
|
+
) {
|
|
669
|
+
// base64 guid
|
|
670
|
+
return uint8ArrayToHex(base64ToUint8Array(strValue)) as ShortHexGuid;
|
|
671
|
+
} else {
|
|
672
|
+
throw new GuidError(
|
|
673
|
+
GuidErrorType.Invalid,
|
|
674
|
+
getEciesI18nEngine() as any,
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Converts a given bigint to a full hex GUID.
|
|
681
|
+
* @param bigInt The bigint to convert.
|
|
682
|
+
* @returns The bigint as a full hex GUID.
|
|
683
|
+
*/
|
|
684
|
+
public static toFullHexFromBigInt<
|
|
685
|
+
TStringKey extends string,
|
|
686
|
+
TLanguage extends string,
|
|
687
|
+
>(bigInt: bigint): FullHexGuid {
|
|
688
|
+
if (bigInt < 0n) {
|
|
689
|
+
throw new GuidError(
|
|
690
|
+
GuidErrorType.Invalid,
|
|
691
|
+
getEciesI18nEngine() as any,
|
|
692
|
+
);
|
|
693
|
+
}
|
|
694
|
+
const uuidBigInt = bigInt.toString(16).padStart(32, '0');
|
|
695
|
+
if (uuidBigInt.length !== 32) {
|
|
696
|
+
throw new GuidError(
|
|
697
|
+
GuidErrorType.Invalid,
|
|
698
|
+
getEciesI18nEngine() as any,
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
const rebuiltUuid =
|
|
702
|
+
uuidBigInt.slice(0, 8) +
|
|
703
|
+
'-' +
|
|
704
|
+
uuidBigInt.slice(8, 12) +
|
|
705
|
+
'-' +
|
|
706
|
+
uuidBigInt.slice(12, 16) +
|
|
707
|
+
'-' +
|
|
708
|
+
uuidBigInt.slice(16, 20) +
|
|
709
|
+
'-' +
|
|
710
|
+
uuidBigInt.slice(20);
|
|
711
|
+
return rebuiltUuid as FullHexGuid;
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Converts a given GUID value to a raw GUID buffer.
|
|
716
|
+
* @param value The GUID value to convert.
|
|
717
|
+
* @param dateCreated The date the GUID was created for Mongoose GUIDs
|
|
718
|
+
* @returns The GUID value as a raw GUID buffer.
|
|
719
|
+
*/
|
|
720
|
+
public static toRawGuidUint8Array<
|
|
721
|
+
TStringKey extends string,
|
|
722
|
+
TLanguage extends string,
|
|
723
|
+
>(value: GuidInput): RawGuidUint8Array {
|
|
724
|
+
const expectedBrand = GuidV4.whichBrand(value);
|
|
725
|
+
let rawGuidUint8ArrayResult: RawGuidUint8Array = new Uint8Array(
|
|
726
|
+
0,
|
|
727
|
+
) as RawGuidUint8Array;
|
|
728
|
+
switch (expectedBrand) {
|
|
729
|
+
case GuidBrandType.FullHexGuid:
|
|
730
|
+
const shortHex1 = GuidV4.toShortHexGuid(value as ShortHexGuid);
|
|
731
|
+
rawGuidUint8ArrayResult = new Uint8Array(
|
|
732
|
+
shortHex1.match(/.{2}/g)!.map((byte: string) => parseInt(byte, 16)),
|
|
733
|
+
) as RawGuidUint8Array;
|
|
734
|
+
break;
|
|
735
|
+
case GuidBrandType.ShortHexGuid:
|
|
736
|
+
const shortHex2 = GuidV4.toShortHexGuid(value as ShortHexGuid);
|
|
737
|
+
rawGuidUint8ArrayResult = new Uint8Array(
|
|
738
|
+
shortHex2.match(/.{2}/g)!.map((byte: string) => parseInt(byte, 16)),
|
|
739
|
+
) as RawGuidUint8Array;
|
|
740
|
+
break;
|
|
741
|
+
case GuidBrandType.Base64Guid:
|
|
742
|
+
// Ensure value is a string before using it with atob
|
|
743
|
+
if (typeof value === 'string' || value instanceof Uint8Array) {
|
|
744
|
+
const binaryString = atob(value.toString());
|
|
745
|
+
rawGuidUint8ArrayResult = new Uint8Array(
|
|
746
|
+
Array.from(binaryString).map((c) => c.charCodeAt(0)),
|
|
747
|
+
) as RawGuidUint8Array;
|
|
748
|
+
} else {
|
|
749
|
+
throw new GuidError(
|
|
750
|
+
GuidErrorType.Invalid,
|
|
751
|
+
getEciesI18nEngine() as any,
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
break;
|
|
755
|
+
case GuidBrandType.RawGuidUint8Array:
|
|
756
|
+
rawGuidUint8ArrayResult = value as RawGuidUint8Array;
|
|
757
|
+
break;
|
|
758
|
+
case GuidBrandType.BigIntGuid:
|
|
759
|
+
const shortHex3 = GuidV4.toShortHexGuid(
|
|
760
|
+
GuidV4.toFullHexFromBigInt(value as bigint),
|
|
761
|
+
);
|
|
762
|
+
rawGuidUint8ArrayResult = new Uint8Array(
|
|
763
|
+
shortHex3.match(/.{2}/g)!.map((byte: string) => parseInt(byte, 16)),
|
|
764
|
+
) as RawGuidUint8Array;
|
|
765
|
+
break;
|
|
766
|
+
default:
|
|
767
|
+
throw new GuidError(
|
|
768
|
+
GuidErrorType.UnknownBrand,
|
|
769
|
+
getEciesI18nEngine() as any,
|
|
770
|
+
);
|
|
771
|
+
}
|
|
772
|
+
if (
|
|
773
|
+
rawGuidUint8ArrayResult.length !==
|
|
774
|
+
GuidV4.guidBrandToLength(GuidBrandType.RawGuidUint8Array)
|
|
775
|
+
) {
|
|
776
|
+
throw new GuidError(
|
|
777
|
+
GuidErrorType.UnknownLength,
|
|
778
|
+
getEciesI18nEngine() as any,
|
|
779
|
+
undefined,
|
|
780
|
+
rawGuidUint8ArrayResult.length,
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
return rawGuidUint8ArrayResult;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Compare two GuidV4 instances
|
|
788
|
+
* @param other - The other GuidV4 instance to compare
|
|
789
|
+
* @returns True if the two GuidV4 instances are equal, false otherwise
|
|
790
|
+
*/
|
|
791
|
+
public equals(other: IGuidV4): boolean {
|
|
792
|
+
const a = this.asRawGuidUint8Array;
|
|
793
|
+
const b = other.asRawGuidUint8Array;
|
|
794
|
+
if (a.length !== b.length) return false;
|
|
795
|
+
for (let i = 0; i < a.length; i++) {
|
|
796
|
+
if (a[i] !== b[i]) return false;
|
|
797
|
+
}
|
|
798
|
+
return true;
|
|
799
|
+
}
|
|
800
|
+
}
|