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