@aidc-toolkit/utility 0.9.14-beta → 0.9.16-beta
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/dist/character-set.d.ts +301 -0
- package/dist/character-set.d.ts.map +1 -0
- package/dist/character-set.js +559 -0
- package/dist/character-set.js.map +1 -0
- package/dist/index.d.ts +24 -909
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -0
- package/dist/locale/en/locale-strings.d.ts +33 -0
- package/dist/locale/en/locale-strings.d.ts.map +1 -0
- package/dist/locale/en/locale-strings.js +33 -0
- package/dist/locale/en/locale-strings.js.map +1 -0
- package/dist/locale/fr/locale-strings.d.ts +33 -0
- package/dist/locale/fr/locale-strings.d.ts.map +1 -0
- package/dist/locale/fr/locale-strings.js +33 -0
- package/dist/locale/fr/locale-strings.js.map +1 -0
- package/dist/locale/i18n.d.ts +27 -0
- package/dist/locale/i18n.d.ts.map +1 -0
- package/dist/locale/i18n.js +35 -0
- package/dist/locale/i18n.js.map +1 -0
- package/dist/record.d.ts +41 -0
- package/dist/record.d.ts.map +1 -0
- package/dist/record.js +55 -0
- package/dist/record.js.map +1 -0
- package/dist/reg-exp.d.ts +43 -0
- package/dist/reg-exp.d.ts.map +1 -0
- package/dist/reg-exp.js +55 -0
- package/dist/reg-exp.js.map +1 -0
- package/dist/sequence.d.ts +68 -0
- package/dist/sequence.d.ts.map +1 -0
- package/dist/sequence.js +96 -0
- package/dist/sequence.js.map +1 -0
- package/dist/string.d.ts +22 -0
- package/dist/string.d.ts.map +1 -0
- package/dist/string.js +2 -0
- package/dist/string.js.map +1 -0
- package/dist/transformer.d.ts +377 -0
- package/dist/transformer.d.ts.map +1 -0
- package/dist/transformer.js +483 -0
- package/dist/transformer.js.map +1 -0
- package/package.json +8 -8
- package/dist/index.cjs +0 -17
- package/dist/index.d.cts +0 -909
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { i18nextUtility } from "./locale/i18n.js";
|
|
2
|
+
import { Sequence } from "./sequence.js";
|
|
3
|
+
/**
|
|
4
|
+
* Transform an input iterable to an output iterable that applies a transformer callback to each value in the input.
|
|
5
|
+
*
|
|
6
|
+
* @param values
|
|
7
|
+
* Input values iterable.
|
|
8
|
+
*
|
|
9
|
+
* @param transformerCallback
|
|
10
|
+
* Callback to transform input value to output value.
|
|
11
|
+
*
|
|
12
|
+
* @returns
|
|
13
|
+
* Output values iterable.
|
|
14
|
+
*/
|
|
15
|
+
export function transformIterable(values, transformerCallback) {
|
|
16
|
+
return {
|
|
17
|
+
/**
|
|
18
|
+
* Iterable implementation.
|
|
19
|
+
*
|
|
20
|
+
* @yields
|
|
21
|
+
* Next output value.
|
|
22
|
+
*/
|
|
23
|
+
*[Symbol.iterator]() {
|
|
24
|
+
let index = 0;
|
|
25
|
+
for (const value of values) {
|
|
26
|
+
yield transformerCallback(value, index++);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Transformer that transforms values in a numeric domain to values in a range equal to the domain or to another range
|
|
33
|
+
* defined by a callback function. In other words, the domain determines valid input values and, without a callback, the
|
|
34
|
+
* range of valid output values.
|
|
35
|
+
*
|
|
36
|
+
* The concept is similar to {@link https://en.wikipedia.org/wiki/Format-preserving_encryption | format-preserving
|
|
37
|
+
* encryption}, where input values within a specified domain (e.g., {@link
|
|
38
|
+
* https://en.wikipedia.org/wiki/Payment_card_number | payment card numbers} ranging from 8-19 digits) are transformed
|
|
39
|
+
* into values in the same domain, typically for storage in a database where the data type and length are already fixed
|
|
40
|
+
* and exfiltration of the data can have significant repercussions.
|
|
41
|
+
*
|
|
42
|
+
* Two subclasses are supported directly by this class: {@link IdentityTransformer} (which operates based on a domain
|
|
43
|
+
* only) and {@link EncryptionTransformer} (which operates based on a domain and a tweak). If an application is expected
|
|
44
|
+
* to make repeated use of a transformer with the same domain and (optional) tweak and can't manage the transformer
|
|
45
|
+
* object, an in-memory cache is available via the {@link get} method. Properties in {@link IdentityTransformer} and
|
|
46
|
+
* {@link EncryptionTransformer} are read-only once constructed, so there is no issue with their shared use.
|
|
47
|
+
*/
|
|
48
|
+
export class Transformer {
|
|
49
|
+
/**
|
|
50
|
+
* Transformers cache, mapping a domain to another map, which maps an optional tweak to a transformer.
|
|
51
|
+
*/
|
|
52
|
+
static TRANSFORMER_MAPS_MAP = new Map();
|
|
53
|
+
/**
|
|
54
|
+
* Domain.
|
|
55
|
+
*/
|
|
56
|
+
_domain;
|
|
57
|
+
/**
|
|
58
|
+
* Constructor.
|
|
59
|
+
*
|
|
60
|
+
* @param domain
|
|
61
|
+
* Domain.
|
|
62
|
+
*/
|
|
63
|
+
constructor(domain) {
|
|
64
|
+
this._domain = BigInt(domain);
|
|
65
|
+
if (this._domain <= 0n) {
|
|
66
|
+
throw new RangeError(i18nextUtility.t("Transformer.domainMustBeGreaterThanZero", {
|
|
67
|
+
domain
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get a transformer, constructing it if necessary. The type returned is {@link IdentityTransformer} if tweak is
|
|
73
|
+
* undefined, {@link EncryptionTransformer} if tweak is defined. Note that although an {@link EncryptionTransformer}
|
|
74
|
+
* with a zero tweak operates as an {@link IdentityTransformer}, {@link EncryptionTransformer} is still the type
|
|
75
|
+
* returned if a zero tweak is explicitly specified.
|
|
76
|
+
*
|
|
77
|
+
* @param domain
|
|
78
|
+
* Domain.
|
|
79
|
+
*
|
|
80
|
+
* @param tweak
|
|
81
|
+
* Tweak.
|
|
82
|
+
*
|
|
83
|
+
* @returns
|
|
84
|
+
* {@link IdentityTransformer} if tweak is undefined, {@link EncryptionTransformer} if tweak is defined.
|
|
85
|
+
*/
|
|
86
|
+
static get(domain, tweak) {
|
|
87
|
+
const domainN = BigInt(domain);
|
|
88
|
+
let transformersMap = Transformer.TRANSFORMER_MAPS_MAP.get(domainN);
|
|
89
|
+
if (transformersMap === undefined) {
|
|
90
|
+
transformersMap = new Map();
|
|
91
|
+
Transformer.TRANSFORMER_MAPS_MAP.set(domainN, transformersMap);
|
|
92
|
+
}
|
|
93
|
+
const tweakN = tweak === undefined ? undefined : BigInt(tweak);
|
|
94
|
+
let transformer = transformersMap.get(tweakN);
|
|
95
|
+
if (transformer === undefined) {
|
|
96
|
+
transformer = tweakN === undefined ? new IdentityTransformer(domainN) : new EncryptionTransformer(domainN, tweakN);
|
|
97
|
+
transformersMap.set(tweakN, transformer);
|
|
98
|
+
}
|
|
99
|
+
return transformer;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get the domain.
|
|
103
|
+
*/
|
|
104
|
+
get domain() {
|
|
105
|
+
return this._domain;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Validate that a value is within the domain.
|
|
109
|
+
*
|
|
110
|
+
* @param value
|
|
111
|
+
* Value.
|
|
112
|
+
*/
|
|
113
|
+
validate(value) {
|
|
114
|
+
if (value < 0n) {
|
|
115
|
+
throw new RangeError(i18nextUtility.t("Transformer.valueMustBeGreaterThanOrEqualToZero", {
|
|
116
|
+
value
|
|
117
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
if (value >= this.domain) {
|
|
120
|
+
throw new RangeError(i18nextUtility.t("Transformer.valueMustBeLessThan", {
|
|
121
|
+
value,
|
|
122
|
+
domain: this.domain
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Validate that a value is within the domain and do the work of transforming it forward.
|
|
128
|
+
*
|
|
129
|
+
* @param value
|
|
130
|
+
* Value.
|
|
131
|
+
*
|
|
132
|
+
* @returns
|
|
133
|
+
* Transformed value.
|
|
134
|
+
*/
|
|
135
|
+
validateDoForward(value) {
|
|
136
|
+
const valueN = BigInt(value);
|
|
137
|
+
this.validate(valueN);
|
|
138
|
+
return this.doForward(valueN);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Validate that a value is within the domain, do the work of transforming it forward, and apply a callback.
|
|
142
|
+
*
|
|
143
|
+
* @param transformerCallback
|
|
144
|
+
* Called after each value is transformed to convert it to its final value.
|
|
145
|
+
*
|
|
146
|
+
* @param value
|
|
147
|
+
* Value.
|
|
148
|
+
*
|
|
149
|
+
* @param index
|
|
150
|
+
* Index in sequence (0 for single transformation).
|
|
151
|
+
*
|
|
152
|
+
* @returns
|
|
153
|
+
* Transformed value.
|
|
154
|
+
*/
|
|
155
|
+
validateDoForwardCallback(transformerCallback, value, index) {
|
|
156
|
+
return transformerCallback(this.validateDoForward(value), index);
|
|
157
|
+
}
|
|
158
|
+
;
|
|
159
|
+
// eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
|
|
160
|
+
forward(valueOrValues, transformerCallback) {
|
|
161
|
+
// TODO Refactor type when https://github.com/microsoft/TypeScript/pull/56941 released.
|
|
162
|
+
let result;
|
|
163
|
+
if (typeof valueOrValues !== "object") {
|
|
164
|
+
result = transformerCallback === undefined ? this.validateDoForward(valueOrValues) : this.validateDoForwardCallback(transformerCallback, valueOrValues, 0);
|
|
165
|
+
}
|
|
166
|
+
else if (valueOrValues instanceof Sequence) {
|
|
167
|
+
if (valueOrValues.minimumValue < 0n) {
|
|
168
|
+
throw new RangeError(i18nextUtility.t("Transformer.minimumValueMustBeGreaterThanOrEqualToZero", {
|
|
169
|
+
minimumValue: valueOrValues.minimumValue
|
|
170
|
+
}));
|
|
171
|
+
}
|
|
172
|
+
if (valueOrValues.maximumValue >= this.domain) {
|
|
173
|
+
throw new RangeError(i18nextUtility.t("Transformer.maximumValueMustBeLessThan", {
|
|
174
|
+
maximumValue: valueOrValues.maximumValue,
|
|
175
|
+
domain: this.domain
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
result = transformerCallback === undefined ? transformIterable(valueOrValues, value => this.doForward(value)) : transformIterable(valueOrValues, (value, index) => transformerCallback(this.doForward(value), index));
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
result = transformerCallback === undefined ? transformIterable(valueOrValues, value => this.validateDoForward(value)) : transformIterable(valueOrValues, (value, index) => this.validateDoForwardCallback(transformerCallback, value, index));
|
|
182
|
+
}
|
|
183
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion -- Type determination is handled above.
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Transform a value in reverse.
|
|
188
|
+
*
|
|
189
|
+
* @param transformedValue
|
|
190
|
+
* Transformed value.
|
|
191
|
+
*
|
|
192
|
+
* @returns
|
|
193
|
+
* Value.
|
|
194
|
+
*/
|
|
195
|
+
reverse(transformedValue) {
|
|
196
|
+
const transformedValueN = BigInt(transformedValue);
|
|
197
|
+
this.validate(transformedValueN);
|
|
198
|
+
return this.doReverse(transformedValueN);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Identity transformer. Values are transformed to themselves.
|
|
203
|
+
*/
|
|
204
|
+
export class IdentityTransformer extends Transformer {
|
|
205
|
+
/**
|
|
206
|
+
* @inheritDoc
|
|
207
|
+
*/
|
|
208
|
+
doForward(value) {
|
|
209
|
+
return value;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* @inheritDoc
|
|
213
|
+
*/
|
|
214
|
+
doReverse(transformedValue) {
|
|
215
|
+
return transformedValue;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Encryption transformer. Values are transformed using repeated shuffle and xor operations, similar to those found in
|
|
220
|
+
* many cryptography algorithms, particularly AES. While sufficient for obfuscation of numeric sequences (e.g., serial
|
|
221
|
+
* number generation, below), if true format-preserving encryption is required, a more robust algorithm such as
|
|
222
|
+
* {@link https://doi.org/10.6028/NIST.SP.800-38Gr1-draft | FF1} is recommended. Furthermore, no work has been done to
|
|
223
|
+
* mitigate {@link https://timing.attacks.cr.yp.to/index.html | timing attacks} for key detection.
|
|
224
|
+
*
|
|
225
|
+
* The purpose of the encryption transformer is to generate pseudo-random values in a deterministic manner to obscure
|
|
226
|
+
* the sequence of values generated over time. A typical example is for serial number generation, where knowledge of the
|
|
227
|
+
* sequence can infer production volumes (e.g., serial number 1000 implies that at least 1,000 units have been
|
|
228
|
+
* manufactured) or can be used in counterfeiting (e.g., a counterfeiter can generate serial numbers 1001, 1002, ...
|
|
229
|
+
* with reasonable confidence that they would be valid if queried).
|
|
230
|
+
*
|
|
231
|
+
* The domain and the tweak together determine the encryption key, which in turn determines the number of rounds of
|
|
232
|
+
* shuffle and xor operations. The minimum number of rounds is 4, except where the domain is less than or equal to 256,
|
|
233
|
+
* which results in single-byte operations. To ensure that the operations are effective for single-byte domains, the
|
|
234
|
+
* number of rounds is 1 and only the xor operation is applied (shuffling a single byte is an identity operation).
|
|
235
|
+
*
|
|
236
|
+
* Another exception is when there is a tweak value of 0; this results in identity operations where the output value is
|
|
237
|
+
* identical to the input value, as no shuffle or xor takes place.
|
|
238
|
+
*/
|
|
239
|
+
export class EncryptionTransformer extends Transformer {
|
|
240
|
+
/**
|
|
241
|
+
* Individual bits, pre-calculated for performance.
|
|
242
|
+
*/
|
|
243
|
+
static BITS = new Uint8Array([
|
|
244
|
+
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
|
|
245
|
+
]);
|
|
246
|
+
/**
|
|
247
|
+
* Inverse individual bits, pre-calculated for performance.
|
|
248
|
+
*/
|
|
249
|
+
static INVERSE_BITS = new Uint8Array([
|
|
250
|
+
0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF, 0x7F
|
|
251
|
+
]);
|
|
252
|
+
/**
|
|
253
|
+
* Number of bytes covered by the domain.
|
|
254
|
+
*/
|
|
255
|
+
_domainBytes;
|
|
256
|
+
/**
|
|
257
|
+
* Xor bytes array generated from the domain and tweak.
|
|
258
|
+
*/
|
|
259
|
+
_xorBytes;
|
|
260
|
+
/**
|
|
261
|
+
* Bits array generated from the domain and tweak.
|
|
262
|
+
*/
|
|
263
|
+
_bits;
|
|
264
|
+
/**
|
|
265
|
+
* Inverse bits array generated from the domain and tweak.
|
|
266
|
+
*/
|
|
267
|
+
_inverseBits;
|
|
268
|
+
/**
|
|
269
|
+
* Number of rounds (length of arrays) generated from the domain and tweak.
|
|
270
|
+
*/
|
|
271
|
+
_rounds;
|
|
272
|
+
/**
|
|
273
|
+
* Constructor.
|
|
274
|
+
*
|
|
275
|
+
* @param domain
|
|
276
|
+
* Domain.
|
|
277
|
+
*
|
|
278
|
+
* @param tweak
|
|
279
|
+
* Tweak.
|
|
280
|
+
*/
|
|
281
|
+
constructor(domain, tweak) {
|
|
282
|
+
super(domain);
|
|
283
|
+
if (tweak < 0n) {
|
|
284
|
+
throw new RangeError(i18nextUtility.t("Transformer.tweakMustBeGreaterThanOrEqualToZero", {
|
|
285
|
+
tweak
|
|
286
|
+
}));
|
|
287
|
+
}
|
|
288
|
+
let domainBytes = 0;
|
|
289
|
+
// The number of bytes in the domain determines the size of the shuffle and xor operations.
|
|
290
|
+
for (let reducedDomainMinusOne = this.domain - 1n; reducedDomainMinusOne !== 0n; reducedDomainMinusOne >>= 8n) {
|
|
291
|
+
domainBytes++;
|
|
292
|
+
}
|
|
293
|
+
this._domainBytes = domainBytes;
|
|
294
|
+
const xorBytes = new Array();
|
|
295
|
+
const bits = new Array();
|
|
296
|
+
const inverseBits = new Array();
|
|
297
|
+
// Key is the product of domain, tweak, and an 8-digit prime to force at least four rounds.
|
|
298
|
+
for (let reducedKey = this.domain * BigInt(tweak) * 603868999n; reducedKey !== 0n; reducedKey >>= 8n) {
|
|
299
|
+
// Extract the least significant byte.
|
|
300
|
+
xorBytes.unshift(Number(BigInt.asUintN(8, reducedKey)));
|
|
301
|
+
// Bit number is the reduced key mod 8.
|
|
302
|
+
const bitNumber = Number(BigInt.asUintN(3, reducedKey));
|
|
303
|
+
// Bits are applied in reverse order so that they don't correlate directly with the key bytes at the same index.
|
|
304
|
+
bits.push(EncryptionTransformer.BITS[bitNumber]);
|
|
305
|
+
inverseBits.push(EncryptionTransformer.INVERSE_BITS[bitNumber]);
|
|
306
|
+
}
|
|
307
|
+
// Domains occupying a single byte will not shuffle and will map all values to themselves for very small domains.
|
|
308
|
+
if (domainBytes === 1) {
|
|
309
|
+
// Determine the lowest possible mask that will cover all values in the domain.
|
|
310
|
+
const domainMask = EncryptionTransformer.BITS.filter(bit => bit < domain).reduce((accumulator, bit) => accumulator | bit, 0);
|
|
311
|
+
// Reduce all xor bytes to a single byte and strip higher bits.
|
|
312
|
+
this._xorBytes = new Uint8Array([xorBytes.reduce((accumulator, xorByte) => accumulator ^ xorByte, 0) & domainMask]);
|
|
313
|
+
// Bits and inverse bits are irrelevant as there will be no shuffling; choose first bit arbitrarily.
|
|
314
|
+
this._bits = new Uint8Array([EncryptionTransformer.BITS[0]]);
|
|
315
|
+
this._inverseBits = new Uint8Array([EncryptionTransformer.INVERSE_BITS[0]]);
|
|
316
|
+
// Everything will be done in one round.
|
|
317
|
+
this._rounds = 1;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
this._xorBytes = new Uint8Array(xorBytes);
|
|
321
|
+
this._bits = new Uint8Array(bits);
|
|
322
|
+
this._inverseBits = new Uint8Array(inverseBits);
|
|
323
|
+
this._rounds = xorBytes.length;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Convert a value to a byte array big enough to handle the entire domain.
|
|
328
|
+
*
|
|
329
|
+
* @param value
|
|
330
|
+
* Value.
|
|
331
|
+
*
|
|
332
|
+
* @returns
|
|
333
|
+
* Big-endian byte array equivalent to the value.
|
|
334
|
+
*/
|
|
335
|
+
valueToBytes(value) {
|
|
336
|
+
const bytes = new Uint8Array(this._domainBytes);
|
|
337
|
+
// Build byte array in reverse order to get as big-endian.
|
|
338
|
+
for (let index = this._domainBytes - 1, reducedValue = value; index >= 0 && reducedValue !== 0n; index--, reducedValue >>= 8n) {
|
|
339
|
+
bytes[index] = Number(BigInt.asUintN(8, reducedValue));
|
|
340
|
+
}
|
|
341
|
+
return bytes;
|
|
342
|
+
}
|
|
343
|
+
/**
|
|
344
|
+
* Convert a byte array to a value.
|
|
345
|
+
*
|
|
346
|
+
* @param bytes
|
|
347
|
+
* Big-endian byte array equivalent to the value.
|
|
348
|
+
*
|
|
349
|
+
* @returns
|
|
350
|
+
* Value.
|
|
351
|
+
*/
|
|
352
|
+
static bytesToValue(bytes) {
|
|
353
|
+
return bytes.reduce((accumulator, byte) => accumulator << 8n | BigInt(byte), 0n);
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Shuffle a byte array.
|
|
357
|
+
*
|
|
358
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
359
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
360
|
+
*
|
|
361
|
+
* The shuffle operation starts by testing the bit at `bits[round]` for each `byte` in `bytes`. The indexes for all
|
|
362
|
+
* bytes with that bit set are put into one array (`shuffleIndexes1`) and the rest are put into another
|
|
363
|
+
* (`shuffleIndexes0`). The two arrays are concatenated and used to shuffle the input array, using their values
|
|
364
|
+
* (`shuffleIndex`) and the indexes of those values (`index`) in the concatenated array.
|
|
365
|
+
*
|
|
366
|
+
* Forward shuffling moves the entry at `shuffleIndex` to the `index` position.
|
|
367
|
+
*
|
|
368
|
+
* Reverse shuffling moves the entry at `index` to the `shuffleIndex` position.
|
|
369
|
+
*
|
|
370
|
+
* As each byte is moved, the bit at `bits[round]` is preserved in its original position. This ensures that the
|
|
371
|
+
* process is reversible.
|
|
372
|
+
*
|
|
373
|
+
* @param bytes
|
|
374
|
+
* Byte array.
|
|
375
|
+
*
|
|
376
|
+
* @param round
|
|
377
|
+
* Round number.
|
|
378
|
+
*
|
|
379
|
+
* @param forward
|
|
380
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
381
|
+
*
|
|
382
|
+
* @returns
|
|
383
|
+
* Shuffled byte array.
|
|
384
|
+
*/
|
|
385
|
+
shuffle(bytes, round, forward) {
|
|
386
|
+
const bytesLength = bytes.length;
|
|
387
|
+
const determinants = new Uint8Array(bytesLength);
|
|
388
|
+
const shuffleIndexes1 = new Array();
|
|
389
|
+
const shuffleIndexes0 = new Array();
|
|
390
|
+
const bit = this._bits[round];
|
|
391
|
+
bytes.forEach((byte, index) => {
|
|
392
|
+
const determinant = byte & bit;
|
|
393
|
+
determinants[index] = determinant;
|
|
394
|
+
// Place byte in array chosen by bit state.
|
|
395
|
+
(determinant !== 0 ? shuffleIndexes1 : shuffleIndexes0).push(index);
|
|
396
|
+
});
|
|
397
|
+
const inverseBit = this._inverseBits[round];
|
|
398
|
+
const shuffleBytes = new Uint8Array(bytesLength);
|
|
399
|
+
// Concatenate shuffle indexes arrays and complete shuffle.
|
|
400
|
+
[...shuffleIndexes1, ...shuffleIndexes0].forEach((shuffleIndex, index) => {
|
|
401
|
+
if (forward) {
|
|
402
|
+
shuffleBytes[index] = (bytes[shuffleIndex] & inverseBit) | determinants[index];
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
shuffleBytes[shuffleIndex] = (bytes[index] & inverseBit) | determinants[shuffleIndex];
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
return shuffleBytes;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Xor a byte array.
|
|
412
|
+
*
|
|
413
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
414
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
415
|
+
*
|
|
416
|
+
* Forward:
|
|
417
|
+
* - `bytes'[0] = bytes[0] ^ xorBytes[round]`
|
|
418
|
+
* - `bytes'[1] = bytes[1] ^ bytes'[0]`
|
|
419
|
+
* - `bytes'[2] = bytes[2] ^ bytes'[1]`
|
|
420
|
+
* - `...`
|
|
421
|
+
* - `bytes'[domainBytes - 1] = bytes[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
422
|
+
*
|
|
423
|
+
* Reverse:
|
|
424
|
+
* - `bytes[0] = bytes'[0] ^ xorBytes[round]`
|
|
425
|
+
* - `bytes[1] = bytes'[1] ^ bytes'[0]`
|
|
426
|
+
* - `bytes[2] = bytes'[2] ^ bytes'[1]`
|
|
427
|
+
* - `...`
|
|
428
|
+
* - `bytes[domainBytes - 1] = bytes'[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
429
|
+
*
|
|
430
|
+
* @param bytes
|
|
431
|
+
* Byte array.
|
|
432
|
+
*
|
|
433
|
+
* @param round
|
|
434
|
+
* Round number.
|
|
435
|
+
*
|
|
436
|
+
* @param forward
|
|
437
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
438
|
+
*
|
|
439
|
+
* @returns
|
|
440
|
+
* Xored byte array.
|
|
441
|
+
*/
|
|
442
|
+
xor(bytes, round, forward) {
|
|
443
|
+
let cumulativeXorByte = this._xorBytes[round];
|
|
444
|
+
return bytes.map((byte) => {
|
|
445
|
+
const xorByte = byte ^ cumulativeXorByte;
|
|
446
|
+
cumulativeXorByte = forward ? xorByte : byte;
|
|
447
|
+
return xorByte;
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* @inheritDoc
|
|
452
|
+
*/
|
|
453
|
+
doForward(value) {
|
|
454
|
+
let bytes = this.valueToBytes(value);
|
|
455
|
+
let transformedValue;
|
|
456
|
+
// Loop repeats until transformed value is within domain.
|
|
457
|
+
do {
|
|
458
|
+
// Forward operation is shuffle then xor for the number of rounds.
|
|
459
|
+
for (let round = 0; round < this._rounds; round++) {
|
|
460
|
+
bytes = this.xor(this.shuffle(bytes, round, true), round, true);
|
|
461
|
+
}
|
|
462
|
+
transformedValue = EncryptionTransformer.bytesToValue(bytes);
|
|
463
|
+
} while (transformedValue >= this.domain);
|
|
464
|
+
return transformedValue;
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* @inheritDoc
|
|
468
|
+
*/
|
|
469
|
+
doReverse(transformedValue) {
|
|
470
|
+
let bytes = this.valueToBytes(transformedValue);
|
|
471
|
+
let value;
|
|
472
|
+
// Loop repeats until value is within domain.
|
|
473
|
+
do {
|
|
474
|
+
// Reverse operation is xor then shuffle for the number of rounds in reverse.
|
|
475
|
+
for (let round = this._rounds - 1; round >= 0; round--) {
|
|
476
|
+
bytes = this.shuffle(this.xor(bytes, round, false), round, false);
|
|
477
|
+
}
|
|
478
|
+
value = EncryptionTransformer.bytesToValue(bytes);
|
|
479
|
+
} while (value >= this.domain);
|
|
480
|
+
return value;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
//# sourceMappingURL=transformer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transformer.js","sourceRoot":"","sources":["../src/transformer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAqDzC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iBAAiB,CAA+C,MAAwB,EAAE,mBAAyD;IAC/J,OAAO;QACH;;;;;WAKG;QACH,CAAE,CAAC,MAAM,CAAC,QAAQ,CAAC;YACf,IAAI,KAAK,GAAG,CAAC,CAAC;YAEd,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;gBACzB,MAAM,mBAAmB,CAAC,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;YAC9C,CAAC;QACL,CAAC;KACJ,CAAC;AACN,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,OAAgB,WAAW;IAC7B;;OAEG;IACK,MAAM,CAAU,oBAAoB,GAAG,IAAI,GAAG,EAAgD,CAAC;IAEvG;;OAEG;IACc,OAAO,CAAS;IAEjC;;;;;OAKG;IACH,YAAY,MAAuB;QAC/B,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;QAE9B,IAAI,IAAI,CAAC,OAAO,IAAI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ACO,SAAS,CAAC,gBAAwB;QACxC,IAAI,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,gBAAgB,CAAC,CAAC;QAChD,IAAI,KAAa,CAAC;QAElB,6CAA6C;QAC7C,GAAG,CAAC;YACA,6EAA6E;YAC7E,KAAK,IAAI,KAAK,GAAG,IAAI,CAAC,OAAO,GAAG,CAAC,EAAE,KAAK,IAAI,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC;gBACrD,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;YACtE,CAAC;YAED,KAAK,GAAG,qBAAqB,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QACtD,CAAC,QAAQ,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE;QAE/B,OAAO,KAAK,CAAC;IACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidc-toolkit/utility",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.16-beta",
|
|
4
4
|
"description": "Foundational utilities for AIDC Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
|
-
"homepage": "https://
|
|
7
|
+
"homepage": "https://aidc-toolkit.com/",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "git+https://github.com/aidc-toolkit/utility.git"
|
|
@@ -21,19 +21,19 @@
|
|
|
21
21
|
"scripts": {
|
|
22
22
|
"update-aidc-toolkit": "npm update @aidc-toolkit/dev @aidc-toolkit/core",
|
|
23
23
|
"lint": "eslint",
|
|
24
|
-
"build:core": "
|
|
25
|
-
"build:dev": "npm run build:core
|
|
26
|
-
"build:release": "npm run build:core --
|
|
24
|
+
"build:core": "rimraf dist && tsc --project",
|
|
25
|
+
"build:dev": "npm run build:core -- node_modules/@aidc-toolkit/dev/tsconfig-build-dev.json",
|
|
26
|
+
"build:release": "npm run build:core -- node_modules/@aidc-toolkit/dev/tsconfig-build.json",
|
|
27
27
|
"build:doc": "npm run build:dev",
|
|
28
28
|
"publish-dev": "publish-dev",
|
|
29
29
|
"test": "vitest run"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@aidc-toolkit/dev": "^0.9.
|
|
33
|
-
"vitest": "^3.0.
|
|
32
|
+
"@aidc-toolkit/dev": "^0.9.16-beta",
|
|
33
|
+
"vitest": "^3.0.7"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@aidc-toolkit/core": "^0.9.
|
|
36
|
+
"@aidc-toolkit/core": "^0.9.16-beta",
|
|
37
37
|
"i18next": "^24.2.2"
|
|
38
38
|
}
|
|
39
39
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";var R=Object.create;var y=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var U=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty;var F=(i,e)=>{for(var t in e)y(i,t,{get:e[t],enumerable:!0})},A=(i,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of q(e))!G.call(i,n)&&n!==t&&y(i,n,{get:()=>e[n],enumerable:!(r=C(e,n))||r.enumerable});return i};var Z=(i,e,t)=>(t=i!=null?R(U(i)):{},A(e||!i||!i.__esModule?y(t,"default",{value:i,enumerable:!0}):t,i)),D=i=>A(y({},"__esModule",{value:!0}),i);var j={};F(j,{ALPHABETIC_CREATOR:()=>H,ALPHANUMERIC_CREATOR:()=>X,CharacterSetCreator:()=>f,CharacterSetValidator:()=>M,EncryptionTransformer:()=>N,Exclusion:()=>L,HEXADECIMAL_CREATOR:()=>k,IdentityTransformer:()=>S,NUMERIC_CREATOR:()=>z,RecordValidator:()=>w,RegExpValidator:()=>x,Sequence:()=>T,Transformer:()=>g,i18nUtilityInit:()=>P,i18nextUtility:()=>u,transformIterable:()=>b,utilityNS:()=>V,utilityResources:()=>O});module.exports=D(j);var E=require("@aidc-toolkit/core"),B=Z(require("i18next"),1);var _={Transformer:{domainMustBeGreaterThanZero:"Domain {{domain}} must be greater than 0",tweakMustBeGreaterThanOrEqualToZero:"Tweak {{tweak}} must be greater than or equal to 0",valueMustBeGreaterThanOrEqualToZero:"Value {{value}} must be greater than or equal to 0",valueMustBeLessThan:"Value {{value}} must be less than {{domain}}",minimumValueMustBeGreaterThanOrEqualToZero:"Minimum value {{minimumValue}} must be greater than or equal to 0",maximumValueMustBeLessThan:"Maximum value {{maximumValue}} must be less than {{domain}}"},RegExpValidator:{stringDoesNotMatchPattern:"String {{s}} does not match pattern"},CharacterSetValidator:{firstZeroFirstCharacter:"Character set must support zero as first character",allNumericAllNumericCharacters:"Character set must support all numeric characters in sequence",stringMustNotBeAllNumeric:"String must not be all numeric",lengthMustBeGreaterThanOrEqualTo:"Length {{length}} must be greater than or equal to {{minimumLength}}",lengthMustBeLessThanOrEqualTo:"Length {{length}} must be less than or equal to {{maximumLength}}",lengthMustBeEqualTo:"Length {{length}} must be equal to {{exactLength}}",lengthOfComponentMustBeGreaterThanOrEqualTo:"Length {{length}} of {{component}} must be greater than or equal to {{minimumLength}}",lengthOfComponentMustBeLessThanOrEqualTo:"Length {{length}} of {{component}} must be less than or equal to {{maximumLength}}",lengthOfComponentMustBeEqualTo:"Length {{length}} of {{component}} must be equal to {{exactLength}}",invalidCharacterAtPosition:"Invalid character '{{c}}' at position {{position}}",invalidCharacterAtPositionOfComponent:"Invalid character '{{c}}' at position {{position}} of {{component}}",exclusionNotSupported:"Exclusion value of {{exclusion}} is not supported",invalidTweakWithAllNumericExclusion:"Tweak must not be used with all-numeric exclusion",endSequenceValueMustBeLessThanOrEqualTo:"End sequence value (start sequence value + count - 1) must be less than {{domain}}"},RecordValidator:{typeNameKeyNotFound:'{{typeName}} "{{key}}" not found'}};var v={Transformer:{domainMustBeGreaterThanZero:"Le domaine {{domain}} doit \xEAtre sup\xE9rieur \xE0 0",tweakMustBeGreaterThanOrEqualToZero:"Le r\xE9glage {{tweak}} doit \xEAtre sup\xE9rieur ou \xE9gal \xE0 0",valueMustBeGreaterThanOrEqualToZero:"La valeur {{value}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",valueMustBeLessThan:"La valeur {{value}} doit \xEAtre inf\xE9rieure \xE0 {{domain}}",minimumValueMustBeGreaterThanOrEqualToZero:"La valeur minimale {{minimumValue}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",maximumValueMustBeLessThan:"La valeur maximale {{maximumValue}} doit \xEAtre inf\xE9rieure \xE0 {{domain}}"},RegExpValidator:{stringDoesNotMatchPattern:"La cha\xEEne {{s}} ne correspond pas au mod\xE8le"},CharacterSetValidator:{firstZeroFirstCharacter:"Le jeu de caract\xE8res doit prendre en charge z\xE9ro comme premier caract\xE8re",allNumericAllNumericCharacters:"Le jeu de caract\xE8res doit prendre en charge tous les caract\xE8res num\xE9riques en s\xE9quence",stringMustNotBeAllNumeric:"La cha\xEEne ne doit pas \xEAtre enti\xE8rement num\xE9rique",lengthMustBeGreaterThanOrEqualTo:"La longueur {{length}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength}}",lengthMustBeLessThanOrEqualTo:"La longueur {{length}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength}}",lengthMustBeEqualTo:"La longueur {{length}} doit \xEAtre \xE9gale \xE0 {{exactLength}}",lengthOfComponentMustBeGreaterThanOrEqualTo:"La longueur {{length}} de {{component}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength}}",lengthOfComponentMustBeLessThanOrEqualTo:"La longueur {{length}} de {{component}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength}}",lengthOfComponentMustBeEqualTo:"La longueur {{length}} de {{component}} doit \xEAtre \xE9gale \xE0 {{exactLength}}",invalidCharacterAtPosition:"Caract\xE8re non valide '{{c}}' \xE0 la position {{position}}",invalidCharacterAtPositionOfComponent:"Caract\xE8re non valide '{{c}}' \xE0 la position {{position}} de {{component}}",exclusionNotSupported:"La valeur d'exclusion de {{exclusion}} n'est pas prise en charge",invalidTweakWithAllNumericExclusion:"Le r\xE9glage ne doit pas \xEAtre utilis\xE9 avec une exclusion enti\xE8rement num\xE9rique",endSequenceValueMustBeLessThanOrEqualTo:"La valeur de la s\xE9quence de fin (valeur de la s\xE9quence de d\xE9but + nombre - 1) doit \xEAtre inf\xE9rieure \xE0 {{domaine}}"},RecordValidator:{typeNameKeyNotFound:'{{typeName}} "{{key}}" introuvable'}};var V="aidct_utility";(0,E.i18nAssertValidResources)(_,"fr",v);var O={en:{aidct_utility:_},fr:{aidct_utility:v}},u=B.default.createInstance();async function P(i,e=!1){await(0,E.i18nCoreInit)(u,i,e,V,O)}var T=class{_startValue;_endValue;_count;_nextDelta;_minimumValue;_maximumValue;constructor(e,t){this._startValue=BigInt(e),this._endValue=this._startValue+BigInt(t),this._count=t,t>=0?(this._nextDelta=1n,this._minimumValue=this._startValue,this._maximumValue=this._endValue-1n):(this._nextDelta=-1n,this._minimumValue=this._endValue+1n,this._maximumValue=this._startValue)}get startValue(){return this._startValue}get endValue(){return this._endValue}get count(){return this._count}get minimumValue(){return this._minimumValue}get maximumValue(){return this._maximumValue}*[Symbol.iterator](){for(let e=this._startValue;e!==this._endValue;e+=this._nextDelta)yield e}};function b(i,e){return{*[Symbol.iterator](){let t=0;for(let r of i)yield e(r,t++)}}}var g=class i{static TRANSFORMER_MAPS_MAP=new Map;_domain;constructor(e){if(this._domain=BigInt(e),this._domain<=0n)throw new RangeError(u.t("Transformer.domainMustBeGreaterThanZero",{domain:e}))}static get(e,t){let r=BigInt(e),n=i.TRANSFORMER_MAPS_MAP.get(r);n===void 0&&(n=new Map,i.TRANSFORMER_MAPS_MAP.set(r,n));let a=t===void 0?void 0:BigInt(t),o=n.get(a);return o===void 0&&(o=a===void 0?new S(r):new N(r,a),n.set(a,o)),o}get domain(){return this._domain}validate(e){if(e<0n)throw new RangeError(u.t("Transformer.valueMustBeGreaterThanOrEqualToZero",{value:e}));if(e>=this.domain)throw new RangeError(u.t("Transformer.valueMustBeLessThan",{value:e,domain:this.domain}))}validateDoForward(e){let t=BigInt(e);return this.validate(t),this.doForward(t)}validateDoForwardCallback(e,t,r){return e(this.validateDoForward(t),r)}forward(e,t){let r;if(typeof e!="object")r=t===void 0?this.validateDoForward(e):this.validateDoForwardCallback(t,e,0);else if(e instanceof T){if(e.minimumValue<0n)throw new RangeError(u.t("Transformer.minimumValueMustBeGreaterThanOrEqualToZero",{minimumValue:e.minimumValue}));if(e.maximumValue>=this.domain)throw new RangeError(u.t("Transformer.maximumValueMustBeLessThan",{maximumValue:e.maximumValue,domain:this.domain}));r=t===void 0?b(e,n=>this.doForward(n)):b(e,(n,a)=>t(this.doForward(n),a))}else r=t===void 0?b(e,n=>this.validateDoForward(n)):b(e,(n,a)=>this.validateDoForwardCallback(t,n,a));return r}reverse(e){let t=BigInt(e);return this.validate(t),this.doReverse(t)}},S=class extends g{doForward(e){return e}doReverse(e){return e}},N=class i extends g{static BITS=new Uint8Array([1,2,4,8,16,32,64,128]);static INVERSE_BITS=new Uint8Array([254,253,251,247,239,223,191,127]);_domainBytes;_xorBytes;_bits;_inverseBits;_rounds;constructor(e,t){if(super(e),t<0n)throw new RangeError(u.t("Transformer.tweakMustBeGreaterThanOrEqualToZero",{tweak:t}));let r=0;for(let s=this.domain-1n;s!==0n;s>>=8n)r++;this._domainBytes=r;let n=new Array,a=new Array,o=new Array;for(let s=this.domain*BigInt(t)*603868999n;s!==0n;s>>=8n){n.unshift(Number(BigInt.asUintN(8,s)));let l=Number(BigInt.asUintN(3,s));a.push(i.BITS[l]),o.push(i.INVERSE_BITS[l])}if(r===1){let s=i.BITS.filter(l=>l<e).reduce((l,h)=>l|h,0);this._xorBytes=new Uint8Array([n.reduce((l,h)=>l^h,0)&s]),this._bits=new Uint8Array([i.BITS[0]]),this._inverseBits=new Uint8Array([i.INVERSE_BITS[0]]),this._rounds=1}else this._xorBytes=new Uint8Array(n),this._bits=new Uint8Array(a),this._inverseBits=new Uint8Array(o),this._rounds=n.length}valueToBytes(e){let t=new Uint8Array(this._domainBytes);for(let r=this._domainBytes-1,n=e;r>=0&&n!==0n;r--,n>>=8n)t[r]=Number(BigInt.asUintN(8,n));return t}static bytesToValue(e){return e.reduce((t,r)=>t<<8n|BigInt(r),0n)}shuffle(e,t,r){let n=e.length,a=new Uint8Array(n),o=new Array,s=new Array,l=this._bits[t];e.forEach((m,c)=>{let p=m&l;a[c]=p,(p!==0?o:s).push(c)});let h=this._inverseBits[t],d=new Uint8Array(n);return[...o,...s].forEach((m,c)=>{r?d[c]=e[m]&h|a[c]:d[m]=e[c]&h|a[m]}),d}xor(e,t,r){let n=this._xorBytes[t];return e.map(a=>{let o=a^n;return n=r?o:a,o})}doForward(e){let t=this.valueToBytes(e),r;do{for(let n=0;n<this._rounds;n++)t=this.xor(this.shuffle(t,n,!0),n,!0);r=i.bytesToValue(t)}while(r>=this.domain);return r}doReverse(e){let t=this.valueToBytes(e),r;do{for(let n=this._rounds-1;n>=0;n--)t=this.shuffle(this.xor(t,n,!1),n,!1);r=i.bytesToValue(t)}while(r>=this.domain);return r}};var x=class{_regExp;constructor(e){this._regExp=e}get regExp(){return this._regExp}createErrorMessage(e){return u.t("RegExpValidator.stringDoesNotMatchPattern",{s:e})}validate(e){if(!this._regExp.test(e))throw new RangeError(this.createErrorMessage(e))}};var w=class{_typeName;_record;constructor(e,t){this._typeName=e,this._record=t}get typeName(){return this._typeName}get record(){return this._record}validate(e){if(!(e in this.record))throw new RangeError(u.t("RecordValidator.typeNameKeyNotFound",{typeName:this.typeName,key:e}))}};var L=(r=>(r[r.None=0]="None",r[r.FirstZero=1]="FirstZero",r[r.AllNumeric=2]="AllNumeric",r))(L||{}),M=class i{static NOT_ALL_NUMERIC_VALIDATOR=new class extends x{createErrorMessage(e){return u.t("CharacterSetValidator.stringMustNotBeAllNumeric")}}(/\D/);_characterSet;_characterSetMap;_exclusionSupport;constructor(e,...t){this._characterSet=e;let r=new Map;e.forEach((n,a)=>{r.set(n,a)}),this._characterSetMap=r,this._exclusionSupport=t}get characterSet(){return this._characterSet}get characterSetSize(){return this._characterSet.length}get exclusionSupport(){return this._exclusionSupport}character(e){return this._characterSet[e]}characterIndex(e){return this._characterSetMap.get(e)}characterIndexes(e){return Array.from(e).map(t=>this._characterSetMap.get(t))}static componentToString(e){return typeof e=="function"?e():e}validateExclusion(e){if(e!==0&&!this._exclusionSupport.includes(e))throw new RangeError(u.t("CharacterSetValidator.exclusionNotSupported",{exclusion:e}))}validate(e,t){let r=e.length,n=t?.minimumLength,a=t?.maximumLength;if(n!==void 0&&r<n){let s;throw a!==void 0&&a===n?s=u.t(t?.component===void 0?"CharacterSetValidator.lengthMustBeEqualTo":"CharacterSetValidator.lengthOfComponentMustBeEqualTo",{component:i.componentToString(t?.component),length:r,exactLength:n}):s=u.t(t?.component===void 0?"CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo":"CharacterSetValidator.lengthOfComponentMustBeGreaterThanOrEqualTo",{component:i.componentToString(t?.component),length:r,minimumLength:n}),new RangeError(s)}if(a!==void 0&&r>a)throw new RangeError(u.t(t?.component===void 0?"CharacterSetValidator.lengthMustBeLessThanOrEqualTo":"CharacterSetValidator.lengthOfComponentMustBeLessThanOrEqualTo",{component:i.componentToString(t?.component),length:r,maximumLength:a}));let o=this.characterIndexes(e).findIndex(s=>s===void 0);if(o!==-1)throw new RangeError(u.t(t?.component===void 0?"CharacterSetValidator.invalidCharacterAtPosition":"CharacterSetValidator.invalidCharacterAtPositionOfComponent",{component:i.componentToString(t?.component),c:e.charAt(o),position:o+(t?.positionOffset??0)+1}));if(t?.exclusion!==void 0)switch(this.validateExclusion(t.exclusion),t.exclusion){case 0:break;case 1:if(e.startsWith("0"))throw new RangeError(u.t(t.component===void 0?"CharacterSetValidator.invalidCharacterAtPosition":"CharacterSetValidator.invalidCharacterAtPositionOfComponent",{component:i.componentToString(t.component),c:"0",position:(t.positionOffset??0)+1}));break;case 2:i.NOT_ALL_NUMERIC_VALIDATOR.validate(e);break}}},f=class i extends M{static MAXIMUM_STRING_LENGTH=40;static _powersOf10=i.createPowersOf(10);static createPowersOf(e){let t=new Array(this.MAXIMUM_STRING_LENGTH+1),r=BigInt(e);for(let n=0,a=1n;n<=this.MAXIMUM_STRING_LENGTH;n++,a*=r)t[n]=a;return t}static powerOf10(e){return this._powersOf10[e]}_characterSetSizeN;_characterSetSizeMinusOneN;_exclusionDomains;_allZerosValues;constructor(e,...t){super(e,...t),this._characterSetSizeN=BigInt(this.characterSetSize),this._characterSetSizeMinusOneN=BigInt(this.characterSetSize-1);let r=[],n=i.createPowersOf(this.characterSetSize);if(r[0]=n,t.includes(1)){if(e[0]!=="0")throw new RangeError(u.t("CharacterSetValidator.firstZeroFirstCharacter"));let o=new Array(i.MAXIMUM_STRING_LENGTH+1);o[0]=0n;for(let s=1;s<=i.MAXIMUM_STRING_LENGTH;s++)o[s]=this._characterSetSizeMinusOneN*n[s-1];r[1]=o}if(t.includes(2)){let s=function(c){let p=c[0];for(let I of c){if(I===void 0||I!==p)throw new RangeError(u.t("CharacterSetValidator.allNumericAllNumericCharacters"));p=I+1}};var a=s;let o=new Array(i.MAXIMUM_STRING_LENGTH+1),l=this.characterIndexes("0123456789");s(l);let h=BigInt(l[0]),d=new Array(i.MAXIMUM_STRING_LENGTH+1),m=0n;for(let c=0;c<=i.MAXIMUM_STRING_LENGTH;c++)o[c]=n[c]-i.powerOf10(c),d[c]=m,m=m*this._characterSetSizeN+h;this._allZerosValues=d,r[2]=o}else this._allZerosValues=[];this._exclusionDomains=r}powerOfSize(e){return this._exclusionDomains[0][e]}allNumericShift(e,t,r){let n;if(t===0){if(!e&&r<10n)throw new RangeError(u.t("CharacterSetValidator.stringMustNotBeAllNumeric"));n=10n}else{let a=this.powerOfSize(t),o=i.powerOf10(t),s=e?a-o:a,l=r/s;l>=10n?n=i.powerOf10(t+1):n=l*o+this.allNumericShift(e,t-1,r-l*s)}return n}validateLength(e){if(e<0)throw new RangeError(u.t("CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo",{length:e,minimumLength:0}));if(e>i.MAXIMUM_STRING_LENGTH)throw new RangeError(u.t("CharacterSetValidator.lengthMustBeLessThanOrEqualTo",{length:e,maximumLength:i.MAXIMUM_STRING_LENGTH}))}create(e,t,r=0,n,a){this.validateLength(e),this.validateExclusion(r);let o=r===2?this._allZerosValues[e]:0n;return g.get(this._exclusionDomains[r][e],n).forward(t,(l,h)=>{let d="";if(e!==0){let m=l;r===2&&m>=o&&(m=m+this.allNumericShift(!0,e,m-o));for(let c=e-1;c>0;c--){let p=m/this._characterSetSizeN;d=this.character(Number(m-p*this._characterSetSizeN))+d,m=p}d=this.character(r===1?Number(m%this._characterSetSizeMinusOneN)+1:Number(m%this._characterSetSizeN))+d}return a===void 0?d:a(d,h)})}valueFor(e,t=0,r){let n=e.length;this.validateLength(n),this.validateExclusion(t);let a=BigInt(this.characterSetSize),o=this.characterIndexes(e).reduce((s,l,h)=>{if(l===void 0)throw new RangeError(u.t("CharacterSetValidator.invalidCharacterAtPosition",{c:e.charAt(h),position:h+1}));let d;if(h===0&&t===1){if(l===0)throw new RangeError(u.t("CharacterSetValidator.invalidCharacterAtPosition",{c:"0",position:1}));d=BigInt(l-1)}else d=s*a+BigInt(l);return d},0n);if(t===2){let s=this._allZerosValues[n];o>=s&&(o-=this.allNumericShift(!1,n,o-s))}return g.get(this._exclusionDomains[t][n],r).reverse(o)}},z=new f(["0","1","2","3","4","5","6","7","8","9"],1),k=new f(["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"],1,2),H=new f(["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]),X=new f(["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],1,2);0&&(module.exports={ALPHABETIC_CREATOR,ALPHANUMERIC_CREATOR,CharacterSetCreator,CharacterSetValidator,EncryptionTransformer,Exclusion,HEXADECIMAL_CREATOR,IdentityTransformer,NUMERIC_CREATOR,RecordValidator,RegExpValidator,Sequence,Transformer,i18nUtilityInit,i18nextUtility,transformIterable,utilityNS,utilityResources});
|
|
2
|
-
/*!
|
|
3
|
-
* Copyright © 2024-2025 Dolphin Data Development Ltd. and AIDC Toolkit
|
|
4
|
-
* contributors
|
|
5
|
-
*
|
|
6
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
-
* you may not use this file except in compliance with the License.
|
|
8
|
-
* You may obtain a copy of the License at
|
|
9
|
-
*
|
|
10
|
-
* https://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
-
*
|
|
12
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
-
* See the License for the specific language governing permissions and
|
|
16
|
-
* limitations under the License.
|
|
17
|
-
*/
|