@crossdyne/security 0.2.0-beta.1 → 0.3.0-beta.1

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/index.js CHANGED
@@ -1,82 +1,150 @@
1
+ // src/configurations/security-constants.ts
2
+ var SecurityConstants = {
3
+ AesGcmNonceSize: 12,
4
+ AesGcmTagSize: 16,
5
+ AesGcmTagSizeMin: 12,
6
+ AesGcmTagSizeMax: 16,
7
+ KeySizeBytes: 32,
8
+ // 256 bits
9
+ Pbkdf2IterationsDefault: 6e5,
10
+ Pbkdf2IterationsMinimum: 1e5
11
+ };
12
+
13
+ // src/configurations/supported-hash-algorithms.ts
14
+ var SupportedHashAlgorithms = ["SHA-256", "SHA-384", "SHA-512"];
15
+
1
16
  // src/utils/security.utils.ts
2
17
  var SecurityUtils = class {
3
- /*
4
- Base64 helpers
5
- */
18
+ /** Encodes Uint8Array to standard Base64. */
6
19
  static toBase64(bytes) {
7
20
  let binary = "";
8
- for (let i = 0; i < bytes.byteLength; i++)
21
+ for (let i = 0; i < bytes.length; i++)
9
22
  binary += String.fromCharCode(bytes[i]);
10
- return window.btoa(binary);
23
+ return btoa(binary);
11
24
  }
25
+ /** Decodes URL-safe or standard Base64 to Uint8Array. */
12
26
  static fromBase64(base64) {
13
- const cleaned = base64.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
14
- const binary = window.atob(cleaned);
27
+ let cleaned = base64.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
28
+ const mod = cleaned.length % 4;
29
+ if (mod !== 0) {
30
+ cleaned += "=".repeat(4 - mod);
31
+ }
32
+ const binary = atob(cleaned);
15
33
  const bytes = new Uint8Array(binary.length);
16
34
  for (let i = 0; i < binary.length; i++)
17
35
  bytes[i] = binary.charCodeAt(i);
18
36
  return bytes;
19
37
  }
38
+ /** Converts big-endian bytes to bigint. */
39
+ static bytesToBigInt(bytes) {
40
+ if (bytes.length === 0)
41
+ return 0n;
42
+ return BigInt("0x" + Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join(""));
43
+ }
44
+ /** Converts bigint to fixed-length big-endian bytes (pads/truncates). */
20
45
  static bigIntToFixedBytes(bn, length) {
21
46
  let hex = bn.toString(16);
22
47
  if (hex.length % 2 !== 0)
23
48
  hex = "0" + hex;
24
49
  if (hex.length > length * 2)
25
- throw new Error(`\u0417\u043D\u0430\u0447\u0435\u043D\u0438\u0435 \u0441\u043B\u0438\u0448\u043A\u043E\u043C \u0432\u0435\u043B\u0438\u043A\u043E \u0434\u043B\u044F ${length} \u0431\u0430\u0439\u0442`);
26
- hex = hex.padStart(length * 2, "0");
27
- return this.fromHex(hex);
50
+ hex = hex.slice(hex.length - length * 2);
51
+ else
52
+ hex = hex.padStart(length * 2, "0");
53
+ return new Uint8Array(hex.match(/.{1,2}/g)?.map((b) => parseInt(b, 16)) || []);
28
54
  }
29
- static fromHex(hex) {
30
- const matches = hex.match(/.{1,2}/g);
31
- return new Uint8Array(matches ? matches.map((byte) => parseInt(byte, 16)) : []);
55
+ /** Constant-time comparison of two Uint8Arrays. */
56
+ static fixedTimeEquals(a, b) {
57
+ if (a.length !== b.length)
58
+ return false;
59
+ let diff = 0;
60
+ for (let i = 0; i < a.length; i++)
61
+ diff |= a[i] ^ b[i];
62
+ return diff === 0;
32
63
  }
33
- /*
34
- Math
35
- */
64
+ /** Modular exponentiation (base^exp mod mod) using binary exponentiation. */
36
65
  static expMod(base, exp, mod) {
37
66
  let res = BigInt(1);
38
67
  base = base % mod;
39
- while (exp > 0) {
40
- if (exp % BigInt(2) === BigInt(1)) res = res * base % mod;
68
+ while (exp > 0n) {
69
+ if (exp % 2n === 1n)
70
+ res = res * base % mod;
41
71
  base = base * base % mod;
42
- exp = exp / BigInt(2);
72
+ exp = exp / 2n;
43
73
  }
44
74
  return res;
45
75
  }
46
- /*
47
- SRP helper
48
- */
49
- static async hashAsPerSrp6a(items) {
50
- const buffers = items.map(({ value, length }) => this.bigIntToFixedBytes(value, length));
51
- const totalLen = buffers.reduce((sum, buf) => sum + buf.length, 0);
52
- const combined = new Uint8Array(totalLen);
53
- let offset = 0;
54
- for (const buf of buffers) {
55
- combined.set(buf, offset);
56
- offset += buf.length;
57
- }
58
- const hash = await crypto.subtle.digest("SHA-256", combined);
59
- return this.bytesToBigInt(new Uint8Array(hash));
76
+ };
77
+
78
+ // src/crypto/aes-gcm-options.ts
79
+ var AesGcmOptions = class _AesGcmOptions {
80
+ constructor() {
81
+ this._nonceSize = SecurityConstants.AesGcmNonceSize;
82
+ this._tagSize = SecurityConstants.AesGcmTagSize;
83
+ }
84
+ /** Nonce size in bytes (must be 12). */
85
+ get nonceSize() {
86
+ return this._nonceSize;
87
+ }
88
+ set nonceSize(v) {
89
+ if (v !== SecurityConstants.AesGcmNonceSize) throw new RangeError(`AES-GCM requires ${SecurityConstants.AesGcmNonceSize}-byte nonce`);
90
+ this._nonceSize = v;
91
+ }
92
+ /** Tag size in bytes (12–16, default 16). */
93
+ get tagSize() {
94
+ return this._tagSize;
95
+ }
96
+ set tagSize(v) {
97
+ if (v < SecurityConstants.AesGcmTagSizeMin || v > SecurityConstants.AesGcmTagSizeMax) throw new RangeError(`Tag size must be between ${SecurityConstants.AesGcmTagSizeMin} and ${SecurityConstants.AesGcmTagSizeMax}`);
98
+ this._tagSize = v;
99
+ }
100
+ /** Validates that {@link tagSize} is in the allowed range. */
101
+ validate() {
102
+ if (this.tagSize < SecurityConstants.AesGcmTagSizeMin || this.tagSize > SecurityConstants.AesGcmTagSizeMax) throw new Error(`Invalid Tag Size: ${this.tagSize}`);
103
+ }
104
+ /** Default preset: nonce=12, tag=16, no AAD. */
105
+ static get default() {
106
+ return new _AesGcmOptions();
107
+ }
108
+ /** Fluent setter for {@link tagSize}. */
109
+ withTagSize(s) {
110
+ this.tagSize = s;
111
+ return this;
112
+ }
113
+ /**
114
+ * Fluent setter for {@link associatedData}.
115
+ * Accepts a byte array or a UTF-8 string (encoded internally).
116
+ */
117
+ withAssociatedData(aad) {
118
+ this.associatedData = typeof aad === "string" ? new TextEncoder().encode(aad) : aad;
119
+ return this;
120
+ }
121
+ /** Validates and returns this instance. */
122
+ build() {
123
+ this.validate();
124
+ return this;
60
125
  }
61
126
  };
62
- SecurityUtils.MODULUS_SIZE = 384;
63
- SecurityUtils.NONCE_SIZE = 12;
64
- SecurityUtils.TAG_SIZE = 16;
65
- SecurityUtils.KEY_SIZE = 32;
66
- SecurityUtils.N = BigInt("0xAC6BDB41324A9A9BF166DE5E1F403D434A6E1B3B94A7E62AC1211858E002C75AD4455C9D19C0A3180296917A376205164043E20144FF485719D181A99EB574671AC58054457ED444A67032EA17D03AD43464D2397449CA593630A670D90D95A78E846A3C8AF80862098D80F33C42ED7059E75225E0A52718E2379369F65B79680A6560B080092EE71986066735A96A7D42E7597116742B02D3A154471B6A23D84E0D642C790D597A2BB7F5A48F734898BDD138C69493E723491959C1B4BD40C91C1C7924F88D046467A006507E781220A80C55A927906A7C6C9C227E674686DD5D1B855D28F0D604E24586C608630B9A34C4808381A54F0D9080A5F90B60187F");
67
- SecurityUtils.g = BigInt(2);
68
- SecurityUtils.k = BigInt("0xD55AE1AEC9F9115621E93E16E5DE4517DF8450D0957024D1256AA32B71E4E412");
69
- /*
70
- BigInt helpers
71
- */
72
- SecurityUtils.bytesToBigInt = (bytes) => BigInt("0x" + Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join(""));
73
127
 
74
128
  // src/crypto/crypto.service.ts
75
129
  var CryptoService = class {
76
130
  constructor() {
131
+ /**
132
+ * Generates cryptographically secure random bytes.
133
+ * @param length - Number of bytes (default 32).
134
+ * @returns Uint8Array of random bytes.
135
+ */
77
136
  this.generateRandomBytes = (length = 32) => crypto.getRandomValues(new Uint8Array(length));
78
137
  }
79
- async encryptData(dataModel, key) {
138
+ /**
139
+ * Encrypts a serializable object to a Base64 string.
140
+ * @param dataModel - Object or Uint8Array to encrypt.
141
+ * @param key - AES-256 key (32 bytes).
142
+ * @param options - AES-GCM configuration; uses default if omitted.
143
+ * @returns Base64-encoded ciphertext with prepended nonce.
144
+ */
145
+ async encryptData(dataModel, key, options) {
146
+ const opts = options ?? AesGcmOptions.default;
147
+ opts.validate();
80
148
  const encoder = new TextEncoder();
81
149
  let jsonString;
82
150
  if (dataModel instanceof Uint8Array) {
@@ -85,7 +153,7 @@ var CryptoService = class {
85
153
  jsonString = JSON.stringify(dataModel);
86
154
  }
87
155
  const plainBytes = encoder.encode(jsonString);
88
- const nonce = crypto.getRandomValues(new Uint8Array(SecurityUtils.NONCE_SIZE));
156
+ const nonce = crypto.getRandomValues(new Uint8Array(opts.nonceSize));
89
157
  const cryptoKey = await crypto.subtle.importKey(
90
158
  "raw",
91
159
  key,
@@ -93,28 +161,43 @@ var CryptoService = class {
93
161
  false,
94
162
  ["encrypt"]
95
163
  );
164
+ let associatedData = new Uint8Array(0);
165
+ if (opts.associatedData != null) {
166
+ associatedData = opts.associatedData;
167
+ }
96
168
  const encryptedContent = await crypto.subtle.encrypt(
97
169
  {
98
170
  name: "AES-GCM",
99
171
  iv: nonce,
100
- tagLength: SecurityUtils.TAG_SIZE * 8
172
+ tagLength: opts.tagSize * 8,
173
+ additionalData: associatedData
101
174
  },
102
175
  cryptoKey,
103
176
  plainBytes
104
177
  );
105
- const result = new Uint8Array(SecurityUtils.NONCE_SIZE + encryptedContent.byteLength);
178
+ const result = new Uint8Array(opts.nonceSize + encryptedContent.byteLength);
106
179
  result.set(nonce, 0);
107
- result.set(new Uint8Array(encryptedContent), SecurityUtils.NONCE_SIZE);
180
+ result.set(new Uint8Array(encryptedContent), opts.nonceSize);
108
181
  return SecurityUtils.toBase64(result);
109
182
  }
110
- async decryptedData(encryptedBase64, key) {
183
+ /**
184
+ * Decrypts a Base64-encoded ciphertext back to the original object.
185
+ * @param encryptedBase64 - The encrypted data.
186
+ * @param key - AES-256 key (32 bytes).
187
+ * @param options - AES-GCM configuration; uses default if omitted.
188
+ * @returns Deserialized object, or null if input is empty.
189
+ * @throws If authentication tag mismatch or corrupted data.
190
+ */
191
+ async decryptData(encryptedBase64, key, options) {
111
192
  if (!encryptedBase64)
112
193
  return null;
194
+ const opts = options ?? AesGcmOptions.default;
195
+ opts.validate();
113
196
  const encryptedBytes = SecurityUtils.fromBase64(encryptedBase64);
114
- if (encryptedBytes.length < SecurityUtils.NONCE_SIZE + SecurityUtils.TAG_SIZE)
115
- throw new Error("\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u044B\u0439 \u0444\u043E\u0440\u043C\u0430\u0442 \u0437\u0430\u0448\u0438\u0444\u0440\u043E\u0432\u0430\u043D\u043D\u044B\u0445 \u0434\u0430\u043D\u043D\u044B\u0445");
116
- const nonce = encryptedBytes.slice(0, SecurityUtils.NONCE_SIZE);
117
- const ciphertextWithTag = encryptedBytes.slice(SecurityUtils.NONCE_SIZE);
197
+ if (encryptedBytes.length < opts.nonceSize + opts.tagSize)
198
+ throw new Error(`Invalid format: minimum expected ${opts.nonceSize + opts.tagSize} byte.`);
199
+ const nonce = encryptedBytes.slice(0, opts.nonceSize);
200
+ const ciphertextWithTag = encryptedBytes.slice(opts.nonceSize);
118
201
  const cryptoKey = await crypto.subtle.importKey(
119
202
  "raw",
120
203
  key,
@@ -123,11 +206,16 @@ var CryptoService = class {
123
206
  ["decrypt"]
124
207
  );
125
208
  try {
209
+ let associatedData = new Uint8Array(0);
210
+ if (opts.associatedData != null) {
211
+ associatedData = opts.associatedData;
212
+ }
126
213
  const decryptedBuffer = await crypto.subtle.decrypt(
127
214
  {
128
215
  name: "AES-GCM",
129
216
  iv: nonce,
130
- tagLength: SecurityUtils.TAG_SIZE * 8
217
+ tagLength: opts.tagSize * 8,
218
+ additionalData: associatedData
131
219
  },
132
220
  cryptoKey,
133
221
  ciphertextWithTag
@@ -136,109 +224,502 @@ var CryptoService = class {
136
224
  const jsonString = decoder.decode(decryptedBuffer);
137
225
  return JSON.parse(jsonString);
138
226
  } catch (e) {
139
- console.error("\u041E\u0448\u0438\u0431\u043A\u0430 \u0434\u0435\u0448\u0438\u0444\u0440\u043E\u0432\u0430\u043D\u0438\u044F:", e);
140
- return null;
227
+ console.error("Decryption error:", e);
228
+ throw new Error("Decryption failed: authentication tag mismatch or corrupted data.");
141
229
  }
142
230
  }
143
231
  };
144
232
 
145
- // src/crypto/key-derivation.service.ts
146
- var KeyDerivationService = class {
233
+ // src/crypto/kdf-options.ts
234
+ var KdfOptions = class _KdfOptions {
147
235
  constructor() {
148
- this.ITERATIONS = 6e5;
149
- this.KEY_SIZE = 32;
236
+ this._pbkdf2Iterations = SecurityConstants.Pbkdf2IterationsDefault;
237
+ this._hashAlgorithm = "SHA-256";
238
+ }
239
+ /** PBKDF2 iteration count (minimum 100_000). */
240
+ get pbkdf2Iterations() {
241
+ return this._pbkdf2Iterations;
242
+ }
243
+ set pbkdf2Iterations(v) {
244
+ if (v < SecurityConstants.Pbkdf2IterationsMinimum)
245
+ throw new RangeError(`PBKDF2 iterations must be \u2265 ${SecurityConstants.Pbkdf2IterationsMinimum}`);
246
+ this._pbkdf2Iterations = v;
247
+ }
248
+ /** Hash algorithm used by PBKDF2. Must be one of {@link SupportedHashAlgorithms}. */
249
+ get hashAlgorithm() {
250
+ return this._hashAlgorithm;
251
+ }
252
+ set hashAlgorithm(v) {
253
+ if (!SupportedHashAlgorithms.includes(v))
254
+ throw new RangeError(`Unsupported hash: ${v}`);
255
+ this._hashAlgorithm = v;
256
+ }
257
+ /** Validates iterations and hash algorithm. */
258
+ validate() {
259
+ if (this.pbkdf2Iterations < SecurityConstants.Pbkdf2IterationsMinimum)
260
+ throw new Error(`Pbkdf2Iterations (${this.pbkdf2Iterations}) below minimum`);
261
+ if (!SupportedHashAlgorithms.includes(this.hashAlgorithm))
262
+ throw new Error(`Invalid hash algorithm: ${this.hashAlgorithm}`);
150
263
  }
151
- async deriveKeysFromPassword(login, password, salt) {
264
+ /** Default preset: SHA-256, 600_000 iterations. */
265
+ static get default() {
266
+ return new _KdfOptions();
267
+ }
268
+ /** Fluent setter for {@link pbkdf2Iterations}. */
269
+ withPbkdf2Iterations(i) {
270
+ this.pbkdf2Iterations = i;
271
+ return this;
272
+ }
273
+ /** Fluent setter for {@link hashAlgorithm}. */
274
+ withHashAlgorithm(h) {
275
+ this.hashAlgorithm = h;
276
+ return this;
277
+ }
278
+ /** Validates and returns this instance. */
279
+ build() {
280
+ this.validate();
281
+ return this;
282
+ }
283
+ };
284
+
285
+ // src/crypto/key-derivation.service.ts
286
+ var KeyDerivationService = class {
287
+ /**
288
+ * Derives KEK and Base64 AuthHash. Identity is normalized (trimmed, lowercase).
289
+ * @param identity - User identity (email, username).
290
+ * @param password - User password.
291
+ * @param salt - Random salt.
292
+ * @param options - KDF configuration; uses default if omitted.
293
+ * @returns Object with `kek` (Uint8Array) and `authHash` (Base64 string).
294
+ */
295
+ async deriveKeysFromPassword(identity, password, salt, options) {
296
+ const opts = options ?? KdfOptions.default;
297
+ opts.validate();
152
298
  const safeSalt = new Uint8Array(salt);
153
299
  const encoder = new TextEncoder();
154
- const normalizedLogin = login.trim().toLocaleLowerCase();
300
+ const normalizedLogin = identity.trim().toLowerCase();
155
301
  const combinedPassword = `${normalizedLogin}:${password}`;
156
302
  const passwordBytes = encoder.encode(combinedPassword);
157
303
  const baseKey = await crypto.subtle.importKey("raw", passwordBytes, "PBKDF2", false, ["deriveBits", "deriveKey"]);
158
304
  const masterKeyBits = await crypto.subtle.deriveBits({
159
305
  name: "PBKDF2",
160
306
  salt: safeSalt,
161
- iterations: this.ITERATIONS,
162
- hash: "SHA-256"
163
- }, baseKey, this.KEY_SIZE * 8);
307
+ iterations: opts.pbkdf2Iterations,
308
+ hash: opts.hashAlgorithm
309
+ }, baseKey, SecurityConstants.KeySizeBytes * 8);
164
310
  const masterKey = await crypto.subtle.importKey("raw", masterKeyBits, "HKDF", false, ["deriveBits"]);
165
311
  const kek = await crypto.subtle.deriveBits(
166
- { name: "HKDF", hash: "SHA-256", salt: new Uint8Array(0), info: encoder.encode("AES-GCM-KEK-v1") },
312
+ { name: "HKDF", hash: opts.hashAlgorithm, salt: new Uint8Array(0), info: encoder.encode("AES-GCM-KEK-v1") },
167
313
  masterKey,
168
- this.KEY_SIZE * 8
314
+ SecurityConstants.KeySizeBytes * 8
169
315
  );
170
316
  const authBytes = await crypto.subtle.deriveBits(
171
- { name: "HKDF", hash: "SHA-256", salt: new Uint8Array(0), info: encoder.encode("SERVER-AUTH-HASH-v1") },
317
+ { name: "HKDF", hash: opts.hashAlgorithm, salt: new Uint8Array(0), info: encoder.encode("SERVER-AUTH-HASH-v1") },
172
318
  masterKey,
173
- this.KEY_SIZE * 8
319
+ SecurityConstants.KeySizeBytes * 8
174
320
  );
175
321
  return {
176
322
  kek: new Uint8Array(kek),
177
323
  authHash: SecurityUtils.toBase64(new Uint8Array(authBytes))
178
324
  };
179
325
  }
326
+ /**
327
+ * Derives an SRP-compatible authentication hash (output size = hash output length).
328
+ * @param identity - User identity.
329
+ * @param password - User password.
330
+ * @param salt - Random salt.
331
+ * @param srpHashAlgorithm - SRP hash algorithm (SHA-256/384/512).
332
+ * @param options - KDF configuration; uses default if omitted.
333
+ * @returns Raw hash bytes for use as SRP verifier input (x).
334
+ */
335
+ async deriveAuthHashForSrp(identity, password, salt, srpHashAlgorithm, options) {
336
+ const opts = options ?? KdfOptions.default;
337
+ opts.validate();
338
+ const srpHashSize = srpHashAlgorithm === "SHA-256" ? 32 : srpHashAlgorithm === "SHA-384" ? 48 : 64;
339
+ const normalizedLogin = identity.trim().toLowerCase();
340
+ const combinedPassword = `${normalizedLogin}:${password}`;
341
+ const passwordBytes = new TextEncoder().encode(combinedPassword);
342
+ const baseKey = await crypto.subtle.importKey(
343
+ "raw",
344
+ passwordBytes,
345
+ "PBKDF2",
346
+ false,
347
+ ["deriveBits"]
348
+ );
349
+ const masterKeyBits = await crypto.subtle.deriveBits({
350
+ name: "PBKDF2",
351
+ salt,
352
+ iterations: opts.pbkdf2Iterations,
353
+ hash: srpHashAlgorithm
354
+ }, baseKey, SecurityConstants.KeySizeBytes * 8);
355
+ const masterKey = await crypto.subtle.importKey(
356
+ "raw",
357
+ masterKeyBits,
358
+ "HKDF",
359
+ false,
360
+ ["deriveBits"]
361
+ );
362
+ const info = new TextEncoder().encode("SRP-AUTH-HASH-v1");
363
+ const authBytes = await crypto.subtle.deriveBits(
364
+ {
365
+ name: "HKDF",
366
+ hash: srpHashAlgorithm,
367
+ salt: new Uint8Array(0),
368
+ info
369
+ },
370
+ masterKey,
371
+ srpHashSize * 8
372
+ );
373
+ return new Uint8Array(authBytes);
374
+ }
375
+ };
376
+
377
+ // src/crypto/crypto-version.ts
378
+ var CryptoVersion = /* @__PURE__ */ ((CryptoVersion2) => {
379
+ CryptoVersion2[CryptoVersion2["V1"] = 1] = "V1";
380
+ return CryptoVersion2;
381
+ })(CryptoVersion || {});
382
+
383
+ // src/crypto/crypto-profile.ts
384
+ var CryptoProfile = class {
385
+ /**
386
+ * Creates a new CryptoProfile.
387
+ * @param params - Object containing version, kdfOptions, aesGcmOptions.
388
+ */
389
+ constructor(params) {
390
+ this.version = params.version;
391
+ this.kdfOptions = params.kdfOptions;
392
+ this.aesGcmOptions = params.aesGcmOptions;
393
+ }
394
+ };
395
+
396
+ // src/crypto/crypto-profile-registry.ts
397
+ var CryptoProfileRegistry = class {
398
+ static getProfile(version) {
399
+ switch (version) {
400
+ case 1 /* V1 */:
401
+ return new CryptoProfile({
402
+ version: 1 /* V1 */,
403
+ kdfOptions: KdfOptions.default,
404
+ aesGcmOptions: AesGcmOptions.default
405
+ });
406
+ default:
407
+ throw new Error(`Unsupported crypto version: ${version}`);
408
+ }
409
+ }
410
+ /** Latest supported profile (currently V1). */
411
+ static get latest() {
412
+ return this.getProfile(1 /* V1 */);
413
+ }
414
+ };
415
+
416
+ // src/utils/srp-encoding.ts
417
+ var SrpEncoding = class {
418
+ /** Serializes a bigint to modulus-sized big-endian bytes. */
419
+ static toModulusBytes(ctx, value) {
420
+ return SecurityUtils.bigIntToFixedBytes(value, ctx.modulusSize);
421
+ }
422
+ /** Serializes a bigint to hash-sized big-endian bytes. */
423
+ static toHashBytes(ctx, value) {
424
+ return SecurityUtils.bigIntToFixedBytes(value, ctx.hashSize);
425
+ }
426
+ /** Hashes modulus-sized values (e.g., u = H(A, B)). */
427
+ static async hashModuli(ctx, ...values) {
428
+ const buffers = values.map((v) => this.toModulusBytes(ctx, v));
429
+ return this.hash(ctx.hashAlgorithmName, ...buffers);
430
+ }
431
+ /** Computes M1 = H(A || B || sessionKeyK). */
432
+ static async computeM1(ctx, A, B, sessionKeyK) {
433
+ return this.hash(ctx.hashAlgorithmName, this.toModulusBytes(ctx, A), this.toModulusBytes(ctx, B), sessionKeyK);
434
+ }
435
+ /** Computes M2 = H(A || M1 || sessionKeyK). */
436
+ static async computeM2(ctx, A, M1, sessionKeyK) {
437
+ return this.hash(ctx.hashAlgorithmName, this.toModulusBytes(ctx, A), this.toHashBytes(ctx, M1), sessionKeyK);
438
+ }
439
+ /** Computes session key K = H(S). */
440
+ static async computeSessionKey(ctx, S) {
441
+ const sBytes = this.toModulusBytes(ctx, S);
442
+ const hashBuffer = await crypto.subtle.digest(
443
+ ctx.hashAlgorithmName,
444
+ sBytes
445
+ );
446
+ return new Uint8Array(hashBuffer);
447
+ }
448
+ /** Concatenates byte arrays and returns the hash as bigint. */
449
+ static async hash(algo, ...buffers) {
450
+ const totalLen = buffers.reduce((sum, b) => sum + b.length, 0);
451
+ const combined = new Uint8Array(totalLen);
452
+ let offset = 0;
453
+ for (const buf of buffers) {
454
+ combined.set(buf, offset);
455
+ offset += buf.length;
456
+ }
457
+ const hashBuffer = await crypto.subtle.digest(algo, combined);
458
+ return SecurityUtils.bytesToBigInt(new Uint8Array(hashBuffer));
459
+ }
180
460
  };
181
461
 
182
- // src/srp/srp.service.ts
183
- var SrpService = class {
462
+ // src/srp/srp-client-service.ts
463
+ var SrpClientService = class {
184
464
  constructor() {
185
465
  this.keyDerivation = new KeyDerivationService();
186
466
  }
187
- async generateSrpVerifier(authHash) {
467
+ /**
468
+ * Computes SRP verifier v = g^x mod N from the authentication hash.
469
+ * @param authHash - Auth hash (Base64).
470
+ * @param ctx - SRP context (N, g, hash algorithm, etc.).
471
+ * @returns Verifier as Base64 string.
472
+ */
473
+ async generateSrpVerifier(authHash, ctx) {
188
474
  const x = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(authHash));
189
- const v = SecurityUtils.expMod(SecurityUtils.g, x, SecurityUtils.N);
190
- return SecurityUtils.toBase64(SecurityUtils.bigIntToFixedBytes(v, SecurityUtils.MODULUS_SIZE));
475
+ const v = SecurityUtils.expMod(ctx.g, x, ctx.N);
476
+ return SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, v));
191
477
  }
192
- async generateSrpProof(login, password, saltBase64, B_base64) {
478
+ /**
479
+ * Generates client proof (A, M1, session key S) from server challenge.
480
+ * @param login - User login.
481
+ * @param password - Plaintext password.
482
+ * @param saltBase64 - Server salt (URL-safe Base64).
483
+ * @param B_base64 - Server public ephemeral B (URL-safe Base64).
484
+ * @param ctx - SRP context.
485
+ * @returns Object with A, M1, S as Base64 strings.
486
+ */
487
+ async generateSrpProof(login, password, saltBase64, B_base64, ctx) {
193
488
  const salt = SecurityUtils.fromBase64(saltBase64);
194
- const { authHash } = await this.keyDerivation.deriveKeysFromPassword(login, password, salt);
195
- const x = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(authHash));
489
+ const authHash = await this.keyDerivation.deriveAuthHashForSrp(login, password, salt, ctx.hashAlgorithmName);
490
+ const x = SecurityUtils.bytesToBigInt(authHash);
196
491
  const aBytes = crypto.getRandomValues(new Uint8Array(32));
197
492
  const a = SecurityUtils.bytesToBigInt(aBytes);
198
- const A = SecurityUtils.expMod(SecurityUtils.g, a, SecurityUtils.N);
199
- if (A % SecurityUtils.N === 0n)
200
- throw new Error("\u041A\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043A\u0430\u044F \u043E\u0448\u0438\u0431\u043A\u0430: A % N === 0");
493
+ const A = SecurityUtils.expMod(ctx.g, a, ctx.N);
494
+ if (A % ctx.N === 0n)
495
+ throw new Error("Critical error: A % N === 0");
201
496
  const B = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(B_base64));
202
- if (B % SecurityUtils.N === 0n)
203
- throw new Error("\u041A\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043A\u0430\u044F \u043E\u0448\u0438\u0431\u043A\u0430: B % N === 0");
204
- const u = await SecurityUtils.hashAsPerSrp6a([
205
- { value: A, length: SecurityUtils.MODULUS_SIZE },
206
- { value: B, length: SecurityUtils.MODULUS_SIZE }
207
- ]);
497
+ if (B % ctx.N === 0n)
498
+ throw new Error("Critical error: B % N === 0");
499
+ const u = await SrpEncoding.hashModuli(ctx, A, B);
208
500
  if (u === 0n)
209
501
  throw new Error("\u041D\u0435\u0434\u043E\u043F\u0443\u0441\u0442\u0438\u043C\u043E\u0435 \u0437\u043D\u0430\u0447\u0435\u043D\u0438\u0435 u");
210
- const gX = SecurityUtils.expMod(SecurityUtils.g, x, SecurityUtils.N);
211
- const term = SecurityUtils.k * gX % SecurityUtils.N;
212
- const base = (B - term + SecurityUtils.N) % SecurityUtils.N;
502
+ const gX = SecurityUtils.expMod(ctx.g, x, ctx.N);
503
+ const term = ctx.k * gX % ctx.N;
504
+ const base = (B - term + ctx.N) % ctx.N;
213
505
  const exponent = a + u * x;
214
- const S = SecurityUtils.expMod(base, exponent, SecurityUtils.N);
506
+ const S = SecurityUtils.expMod(base, exponent, ctx.N);
215
507
  if (S === 0n)
216
- throw new Error("\u041A\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043A\u0430\u044F \u043E\u0448\u0438\u0431\u043A\u0430: S === 0");
217
- const M1 = await SecurityUtils.hashAsPerSrp6a([
218
- { value: A, length: SecurityUtils.MODULUS_SIZE },
219
- { value: B, length: SecurityUtils.MODULUS_SIZE },
220
- { value: S, length: SecurityUtils.MODULUS_SIZE }
221
- ]);
508
+ throw new Error("Critical error: S === 0");
509
+ const sessionKeyK = await SrpEncoding.computeSessionKey(ctx, S);
510
+ const M1 = await SrpEncoding.computeM1(ctx, A, B, sessionKeyK);
222
511
  return {
223
- A: SecurityUtils.toBase64(SecurityUtils.bigIntToFixedBytes(A, SecurityUtils.MODULUS_SIZE)),
224
- M1: SecurityUtils.toBase64(SecurityUtils.bigIntToFixedBytes(M1, 32)),
225
- S: SecurityUtils.toBase64(SecurityUtils.bigIntToFixedBytes(S, SecurityUtils.MODULUS_SIZE))
512
+ A: SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, A)),
513
+ M1: SecurityUtils.toBase64(SrpEncoding.toHashBytes(ctx, M1)),
514
+ S: SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, S))
226
515
  };
227
516
  }
228
- async verifyServerM2(A_b64, M1_b64, S_b64, serverM2_b64) {
517
+ /**
518
+ * Validates the server proof M2 to authenticate the server.
519
+ * @param A_b64 - Client public A (Base64).
520
+ * @param M1_b64 - Client proof M1 (Base64).
521
+ * @param S_b64 - Session key S (Base64).
522
+ * @param serverM2_b64 - Server proof M2 (Base64).
523
+ * @param ctx - SRP context.
524
+ * @returns True if the server proof is valid.
525
+ */
526
+ async verifyServerM2(A_b64, M1_b64, S_b64, serverM2_b64, ctx) {
229
527
  const A = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(A_b64));
230
528
  const M1 = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(M1_b64));
231
529
  const S = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(S_b64));
232
- const computedM2 = await SecurityUtils.hashAsPerSrp6a([
233
- { value: A, length: SecurityUtils.MODULUS_SIZE },
234
- { value: M1, length: 32 },
235
- { value: S, length: SecurityUtils.MODULUS_SIZE }
236
- ]);
237
- const computedM2_b64 = SecurityUtils.toBase64(SecurityUtils.bigIntToFixedBytes(computedM2, 32));
238
- return serverM2_b64 === computedM2_b64;
530
+ const sessionKeyK = await SrpEncoding.computeSessionKey(ctx, S);
531
+ const computedM2 = await SrpEncoding.computeM2(ctx, A, M1, sessionKeyK);
532
+ const computedM2Bytes = SrpEncoding.toHashBytes(ctx, computedM2);
533
+ const serverM2Bytes = SecurityUtils.fromBase64(serverM2_b64);
534
+ return SecurityUtils.fixedTimeEquals(computedM2Bytes, serverM2Bytes);
535
+ }
536
+ };
537
+
538
+ // src/srp/srp-server-service.ts
539
+ var SrpServerService = class {
540
+ /**
541
+ * Generates server challenge B and session state from verifier.
542
+ * @param login - User login.
543
+ * @param verifierBytes - Stored verifier v as byte array.
544
+ * @param ctx - SRP context (hash, N, g, etc.).
545
+ * @returns Session state with private b, verifier, and public B.
546
+ */
547
+ async getSrpChallenge(login, verifierBytes, ctx) {
548
+ const v = SecurityUtils.bytesToBigInt(verifierBytes);
549
+ const bBytes = crypto.getRandomValues(new Uint8Array(32));
550
+ const b = SecurityUtils.bytesToBigInt(bBytes);
551
+ const gB = SecurityUtils.expMod(ctx.g, b, ctx.N);
552
+ const B = (ctx.k * v + gB) % ctx.N;
553
+ return {
554
+ login,
555
+ privateKeyB: SecurityUtils.toBase64(bBytes),
556
+ verifier: SecurityUtils.toBase64(verifierBytes),
557
+ publicKeyB: SecurityUtils.toBase64(SrpEncoding.toModulusBytes(ctx, B))
558
+ };
559
+ }
560
+ /**
561
+ * Verifies client M1 proof and returns server M2 proof.
562
+ * @param sessionState - Server session state.
563
+ * @param a - Client public A (Base64).
564
+ * @param m1 - Client proof M1 (Base64).
565
+ * @param ctx - SRP context.
566
+ * @returns Server proof M2 as Base64 string.
567
+ * @throws If verification fails or input is invalid.
568
+ */
569
+ async verifySrpProof(sessionState, a, m1, ctx) {
570
+ const A = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(a));
571
+ const M1_client = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(m1));
572
+ const b = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(sessionState.privateKeyB));
573
+ const v = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(sessionState.verifier));
574
+ const B = SecurityUtils.bytesToBigInt(SecurityUtils.fromBase64(sessionState.publicKeyB));
575
+ if (v <= 0n)
576
+ throw new Error("The verifier is corrupted");
577
+ if (A % ctx.N === 0n)
578
+ throw new Error("Incorrect value of A");
579
+ if (A <= 0n || A >= ctx.N)
580
+ throw new Error("Invalid A (out of range)");
581
+ const u = await SrpEncoding.hashModuli(ctx, A, B);
582
+ if (u === 0n)
583
+ throw new Error("Error in calculating the parameter u");
584
+ const vU = SecurityUtils.expMod(v, u, ctx.N);
585
+ const S = SecurityUtils.expMod(A * vU % ctx.N, b, ctx.N);
586
+ const sessionKeyK = await SrpEncoding.computeSessionKey(ctx, S);
587
+ const M1_server = await SrpEncoding.computeM1(ctx, A, B, sessionKeyK);
588
+ const m1ServerBytes = SrpEncoding.toHashBytes(ctx, M1_server);
589
+ const m1ClientBytes = SrpEncoding.toHashBytes(ctx, M1_client);
590
+ if (!SecurityUtils.fixedTimeEquals(m1ServerBytes, m1ClientBytes))
591
+ throw new Error("Invalid password");
592
+ const M2_server = await SrpEncoding.computeM2(ctx, A, M1_client, sessionKeyK);
593
+ return SecurityUtils.toBase64(SrpEncoding.toHashBytes(ctx, M2_server));
594
+ }
595
+ };
596
+
597
+ // src/srp/srp-group.ts
598
+ var SrpGroup = /* @__PURE__ */ ((SrpGroup2) => {
599
+ SrpGroup2[SrpGroup2["Rfc5054_1024"] = 1] = "Rfc5054_1024";
600
+ SrpGroup2[SrpGroup2["Rfc5054_1536"] = 2] = "Rfc5054_1536";
601
+ SrpGroup2[SrpGroup2["Rfc5054_2048"] = 3] = "Rfc5054_2048";
602
+ SrpGroup2[SrpGroup2["Rfc5054_3072"] = 4] = "Rfc5054_3072";
603
+ SrpGroup2[SrpGroup2["Rfc5054_4096"] = 5] = "Rfc5054_4096";
604
+ SrpGroup2[SrpGroup2["Rfc5054_6144"] = 6] = "Rfc5054_6144";
605
+ SrpGroup2[SrpGroup2["Rfc5054_8192"] = 7] = "Rfc5054_8192";
606
+ SrpGroup2[SrpGroup2["Custom"] = 99] = "Custom";
607
+ return SrpGroup2;
608
+ })(SrpGroup || {});
609
+
610
+ // src/srp/srp-group-params.ts
611
+ var SrpGroupParams = class {
612
+ /**
613
+ * Returns the prime modulus N for the given SRP group.
614
+ * @param group - SRP group identifier.
615
+ * @returns N as a bigint.
616
+ */
617
+ static getN(group) {
618
+ switch (group) {
619
+ case 1 /* Rfc5054_1024 */:
620
+ return BigInt("0xEEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9AFD5138FE8376435B9FC61D2FC0EB06E3");
621
+ case 2 /* Rfc5054_1536 */:
622
+ return BigInt("0x9DEF3CAFB939277AB1F12A8617A47BBBDBA51DF499AC4C80BEEEA9614B19CC4D5F4F5F556E27CBDE51C6A94BE4607A291558903BA0D0F84380B655BB9A22E8DCDF028A7CEC67F0D08134B1C8B97989149B609E0BE3BAB63D47548381DBC5B1FC764E3F4B53DD9DA1158BFD3E2B9C8CF56EDF019539349627DB2FD53D24B7C48665772E437D6C7F8CE442734AF7CCB7AE837C264AE3A9BEB87F8A2FE9B8B5292E5A021FFF5E91479E8CE7A28C2442C6F315180F93499A234DCF76E3FED135F9BB");
623
+ case 3 /* Rfc5054_2048 */:
624
+ return BigInt("0xAC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73");
625
+ case 4 /* Rfc5054_3072 */:
626
+ return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF");
627
+ case 5 /* Rfc5054_4096 */:
628
+ return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199FFFFFFFFFFFFFFFF");
629
+ case 6 /* Rfc5054_6144 */:
630
+ return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DCC4024FFFFFFFFFFFFFFFF");
631
+ case 7 /* Rfc5054_8192 */:
632
+ return BigInt("0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B2699C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C93402849236C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B332051512BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97FBEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AACC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58BB7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD922222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC8862F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A66D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC50846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E7160C980DD98EDD3DFFFFFFFFFFFFFFFFF");
633
+ default:
634
+ throw new Error("Unsupported group");
635
+ }
636
+ }
637
+ /**
638
+ * Returns the generator g for the given SRP group.
639
+ * @param group - SRP group identifier.
640
+ * @returns g as a bigint (2, 5, or 19).
641
+ */
642
+ static getG(group) {
643
+ switch (group) {
644
+ case 1 /* Rfc5054_1024 */:
645
+ return BigInt(2);
646
+ case 2 /* Rfc5054_1536 */:
647
+ return BigInt(2);
648
+ case 3 /* Rfc5054_2048 */:
649
+ return BigInt(2);
650
+ case 4 /* Rfc5054_3072 */:
651
+ return BigInt(5);
652
+ case 5 /* Rfc5054_4096 */:
653
+ return BigInt(5);
654
+ case 6 /* Rfc5054_6144 */:
655
+ return BigInt(5);
656
+ case 7 /* Rfc5054_8192 */:
657
+ return BigInt(19);
658
+ default:
659
+ throw new Error("Unsupported group");
660
+ }
661
+ }
662
+ };
663
+
664
+ // src/srp/srp-options.ts
665
+ var SrpOptions = class {
666
+ constructor() {
667
+ /** Diffie-Hellman group. Default Rfc5054_3072 (g=5). */
668
+ this.group = 4 /* Rfc5054_3072 */;
669
+ /** Hash algorithm for SRP computations. Default SHA-256. */
670
+ this.hashAlgorithmName = "SHA-256";
671
+ /** Salt size in bytes. Default 32. */
672
+ this.saltSize = 32;
673
+ }
674
+ /** Prime modulus N for the selected group. */
675
+ get N() {
676
+ return SrpGroupParams.getN(this.group);
677
+ }
678
+ /** Generator g for the selected group (2, 5, or 19). */
679
+ get g() {
680
+ return SrpGroupParams.getG(this.group);
681
+ }
682
+ /** Byte length of N (ceil(bitLength / 8)). */
683
+ get modulusSize() {
684
+ return Math.floor((this.N.toString(2).length + 7) / 8);
685
+ }
686
+ /**
687
+ * Computes the multiplier parameter k = H(PAD(N) || PAD(g)) (RFC 5054, 2.5.3).
688
+ * @returns k as a bigint.
689
+ */
690
+ async computeK() {
691
+ const nBytes = SecurityUtils.bigIntToFixedBytes(this.N, this.modulusSize);
692
+ const gBytes = SecurityUtils.bigIntToFixedBytes(this.g, this.modulusSize);
693
+ const combined = new Uint8Array(nBytes.length + gBytes.length);
694
+ combined.set(nBytes, 0);
695
+ combined.set(gBytes, nBytes.length);
696
+ const hash = await crypto.subtle.digest(this.hashAlgorithmName, combined);
697
+ return SecurityUtils.bytesToBigInt(new Uint8Array(hash));
698
+ }
699
+ };
700
+
701
+ // src/srp/srp-context-factory.ts
702
+ var SrpContextFactory = class {
703
+ /**
704
+ * Creates an SRP context with the specified group.
705
+ * @param group - SRP group (default Rfc5054_3072).
706
+ * @returns A ready-to-use SrpContext.
707
+ */
708
+ static async create(group = 4 /* Rfc5054_3072 */) {
709
+ const opts = new SrpOptions();
710
+ opts.group = group;
711
+ opts.hashAlgorithmName = group === 5 /* Rfc5054_4096 */ || group === 6 /* Rfc5054_6144 */ || group === 7 /* Rfc5054_8192 */ ? "SHA-384" : "SHA-256";
712
+ return {
713
+ N: opts.N,
714
+ g: opts.g,
715
+ k: await opts.computeK(),
716
+ modulusSize: opts.modulusSize,
717
+ hashAlgorithmName: opts.hashAlgorithmName,
718
+ hashSize: opts.hashAlgorithmName === "SHA-256" ? 32 : opts.hashAlgorithmName === "SHA-384" ? 48 : 64
719
+ };
239
720
  }
240
721
  };
241
722
 
242
- export { CryptoService, KeyDerivationService, SecurityUtils, SrpService };
723
+ export { AesGcmOptions, CryptoProfile, CryptoProfileRegistry, CryptoService, CryptoVersion, KdfOptions, KeyDerivationService, SecurityConstants, SecurityUtils, SrpClientService, SrpContextFactory, SrpEncoding, SrpGroup, SrpGroupParams, SrpOptions, SrpServerService, SupportedHashAlgorithms };
243
724
  //# sourceMappingURL=index.js.map
244
725
  //# sourceMappingURL=index.js.map