@jscrypto/classic 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +21 -3
  2. package/dist/ciphers/aes.js +1 -1
  3. package/dist/ciphers/aes.js.map +1 -1
  4. package/dist/ciphers/rc4.js +2 -2
  5. package/dist/ciphers/rc4.js.map +1 -1
  6. package/dist/ciphers/triple-des.js +1 -1
  7. package/dist/ciphers/triple-des.js.map +1 -1
  8. package/dist/index.cjs +330 -49
  9. package/dist/index.cjs.map +3 -3
  10. package/dist/index.mjs +312 -27
  11. package/dist/index.mjs.map +3 -3
  12. package/dist/jscrypto-classic.iife.js +415 -84
  13. package/dist/jscrypto-classic.iife.js.map +3 -3
  14. package/dist/jscrypto-classic.iife.min.js +2 -2
  15. package/dist/jscrypto-classic.iife.min.js.map +3 -3
  16. package/dist/jscrypto-classic.umd.js +415 -84
  17. package/dist/jscrypto-classic.umd.js.map +3 -3
  18. package/dist/jscrypto-classic.umd.min.js +2 -2
  19. package/dist/jscrypto-classic.umd.min.js.map +3 -3
  20. package/dist/kdfs/evpkdf.js +3 -2
  21. package/dist/kdfs/evpkdf.js.map +1 -1
  22. package/dist/kdfs/pbkdf2.js +1 -1
  23. package/dist/kdfs/pbkdf2.js.map +1 -1
  24. package/dist/modes/cbc.js +4 -4
  25. package/dist/modes/cbc.js.map +1 -1
  26. package/dist/modes/cfb.js +2 -2
  27. package/dist/modes/cfb.js.map +1 -1
  28. package/dist/modes/ctr.js +2 -2
  29. package/dist/modes/ctr.js.map +1 -1
  30. package/dist/modes/ecb.js +4 -4
  31. package/dist/modes/ecb.js.map +1 -1
  32. package/dist/modes/gcm.d.ts +1 -1
  33. package/dist/modes/gcm.d.ts.map +1 -1
  34. package/dist/modes/gcm.js +289 -4
  35. package/dist/modes/gcm.js.map +1 -1
  36. package/dist/modes/ofb.js +2 -2
  37. package/dist/modes/ofb.js.map +1 -1
  38. package/dist/paddings/iso10126.js +2 -1
  39. package/dist/paddings/iso10126.js.map +1 -1
  40. package/package.json +11 -2
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @jscrypto/classic v0.1.0
2
+ * @jscrypto/classic v0.2.0
3
3
  * Copyright 2026 Chen, Yi-Cyuan
4
4
  * Released under the MIT license
5
5
  */
@@ -7,16 +7,45 @@
7
7
  var jscryptoClassic = (() => {
8
8
  var __create = Object.create;
9
9
  var __defProp = Object.defineProperty;
10
+ var __defProps = Object.defineProperties;
10
11
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
11
13
  var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
12
15
  var __getProtoOf = Object.getPrototypeOf;
13
16
  var __hasOwnProp = Object.prototype.hasOwnProperty;
17
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
18
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
19
+ var __spreadValues = (a, b) => {
20
+ for (var prop in b || (b = {}))
21
+ if (__hasOwnProp.call(b, prop))
22
+ __defNormalProp(a, prop, b[prop]);
23
+ if (__getOwnPropSymbols)
24
+ for (var prop of __getOwnPropSymbols(b)) {
25
+ if (__propIsEnum.call(b, prop))
26
+ __defNormalProp(a, prop, b[prop]);
27
+ }
28
+ return a;
29
+ };
30
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
14
31
  var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
15
32
  get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
16
33
  }) : x)(function(x) {
17
34
  if (typeof require !== "undefined") return require.apply(this, arguments);
18
35
  throw Error('Dynamic require of "' + x + '" is not supported');
19
36
  });
37
+ var __objRest = (source, exclude) => {
38
+ var target = {};
39
+ for (var prop in source)
40
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
41
+ target[prop] = source[prop];
42
+ if (source != null && __getOwnPropSymbols)
43
+ for (var prop of __getOwnPropSymbols(source)) {
44
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
45
+ target[prop] = source[prop];
46
+ }
47
+ return target;
48
+ };
20
49
  var __commonJS = (cb, mod) => function __require2() {
21
50
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
22
51
  };
@@ -6701,7 +6730,7 @@ var jscryptoClassic = (() => {
6701
6730
  }
6702
6731
  };
6703
6732
  function createAesCipher(key) {
6704
- if (![16, 24, 32].includes(key.length)) {
6733
+ if (key.length !== 16 && key.length !== 24 && key.length !== 32) {
6705
6734
  throw new Error("AES key must be 128, 192, or 256 bits.");
6706
6735
  }
6707
6736
  return createCryptoJsBlockCipher({
@@ -6778,10 +6807,10 @@ var jscryptoClassic = (() => {
6778
6807
  assertNotFinalized(finalized);
6779
6808
  return processInput(input);
6780
6809
  },
6781
- finalize(input = new Uint8Array()) {
6810
+ finalize(input = new Uint8Array(0)) {
6782
6811
  assertNotFinalized(finalized);
6783
6812
  finalized = true;
6784
- return input.length === 0 ? new Uint8Array() : processInput(input);
6813
+ return input.length === 0 ? new Uint8Array(0) : processInput(input);
6785
6814
  }
6786
6815
  };
6787
6816
  }
@@ -6846,7 +6875,7 @@ var jscryptoClassic = (() => {
6846
6875
  }
6847
6876
  };
6848
6877
  function createTripleDesCipher(key) {
6849
- if (![16, 24].includes(key.length)) {
6878
+ if (key.length !== 16 && key.length !== 24) {
6850
6879
  throw new Error("Triple DES key must be 128 or 192 bits.");
6851
6880
  }
6852
6881
  return createCryptoJsBlockCipher({
@@ -6906,6 +6935,11 @@ var jscryptoClassic = (() => {
6906
6935
  }
6907
6936
  return output;
6908
6937
  }
6938
+ function assertBytes(value, name) {
6939
+ if (!(value instanceof Uint8Array)) {
6940
+ throw new TypeError(`${name} must be a Uint8Array.`);
6941
+ }
6942
+ }
6909
6943
 
6910
6944
  // packages/core/src/passphrase.ts
6911
6945
  function createPassphraseCipher(registry2, options) {
@@ -6925,8 +6959,9 @@ var jscryptoClassic = (() => {
6925
6959
  };
6926
6960
  }
6927
6961
  function createPassphraseEncryptor(registry2, options) {
6962
+ var _a;
6928
6963
  const format = resolveFormat(registry2, options);
6929
- const salt = options.salt ? options.salt.slice() : randomBytes(options.saltSize ?? 8);
6964
+ const salt = options.salt ? options.salt.slice() : randomBytes((_a = options.saltSize) != null ? _a : 8);
6930
6965
  const { key, iv } = deriveKeyIv(registry2, options, salt);
6931
6966
  const encryptor = registry2.createCipher(toTransformOptions(options, key, iv)).createEncryptor();
6932
6967
  if (!format) {
@@ -6938,16 +6973,16 @@ var jscryptoClassic = (() => {
6938
6973
  let emittedHeader = false;
6939
6974
  const emitHeader = () => {
6940
6975
  if (emittedHeader) {
6941
- return new Uint8Array();
6976
+ return new Uint8Array(0);
6942
6977
  }
6943
6978
  emittedHeader = true;
6944
- return format.stringify({ ciphertext: new Uint8Array(), salt });
6979
+ return format.stringify({ ciphertext: new Uint8Array(0), salt });
6945
6980
  };
6946
6981
  return {
6947
6982
  process(input) {
6948
6983
  return concatBytes(emitHeader(), encryptor.process(input));
6949
6984
  },
6950
- finalize(input = new Uint8Array()) {
6985
+ finalize(input = new Uint8Array(0)) {
6951
6986
  return concatBytes(emitHeader(), encryptor.finalize(input));
6952
6987
  }
6953
6988
  };
@@ -6955,43 +6990,44 @@ var jscryptoClassic = (() => {
6955
6990
  function createPassphraseDecryptor(registry2, options) {
6956
6991
  const format = resolveFormat(registry2, options);
6957
6992
  if (!format) {
6958
- const { key, iv } = deriveKeyIv(registry2, options, new Uint8Array());
6993
+ const { key, iv } = deriveKeyIv(registry2, options, new Uint8Array(0));
6959
6994
  return registry2.createCipher(toTransformOptions(options, key, iv)).createDecryptor();
6960
6995
  }
6961
6996
  if (!isStreamingOpenSslFormat(format)) {
6962
6997
  return createBufferedFormatDecryptor(registry2, options, format);
6963
6998
  }
6964
- let header = new Uint8Array();
6999
+ let header = new Uint8Array(0);
6965
7000
  let decryptor;
6966
7001
  const initDecryptor = (input) => {
7002
+ var _a;
6967
7003
  if (decryptor) {
6968
7004
  return input;
6969
7005
  }
6970
7006
  header = new Uint8Array(concatBytes(header, input));
6971
7007
  if (header.length < 16) {
6972
- return new Uint8Array();
7008
+ return new Uint8Array(0);
6973
7009
  }
6974
7010
  const parsed = format.parse(header.slice(0, 16));
6975
7011
  const hasSalt = parsed.salt !== void 0;
6976
- const salt = parsed.salt ?? new Uint8Array();
7012
+ const salt = (_a = parsed.salt) != null ? _a : new Uint8Array(0);
6977
7013
  const ciphertext = hasSalt ? concatBytes(parsed.ciphertext, header.slice(16)) : header;
6978
7014
  const { key, iv } = deriveKeyIv(registry2, options, salt);
6979
7015
  decryptor = registry2.createCipher(toTransformOptions(options, key, iv)).createDecryptor();
6980
- header = new Uint8Array();
7016
+ header = new Uint8Array(0);
6981
7017
  return ciphertext;
6982
7018
  };
6983
7019
  return {
6984
7020
  process(input) {
6985
7021
  const ciphertext = initDecryptor(input);
6986
- return decryptor ? decryptor.process(ciphertext) : new Uint8Array();
7022
+ return decryptor ? decryptor.process(ciphertext) : new Uint8Array(0);
6987
7023
  },
6988
- finalize(input = new Uint8Array()) {
7024
+ finalize(input = new Uint8Array(0)) {
6989
7025
  const ciphertext = initDecryptor(input);
6990
7026
  if (!decryptor) {
6991
- const { key, iv } = deriveKeyIv(registry2, options, new Uint8Array());
7027
+ const { key, iv } = deriveKeyIv(registry2, options, new Uint8Array(0));
6992
7028
  decryptor = registry2.createCipher(toTransformOptions(options, key, iv)).createDecryptor();
6993
7029
  const buffered = header;
6994
- header = new Uint8Array();
7030
+ header = new Uint8Array(0);
6995
7031
  return decryptor.finalize(buffered);
6996
7032
  }
6997
7033
  return decryptor.finalize(ciphertext);
@@ -7006,9 +7042,9 @@ var jscryptoClassic = (() => {
7006
7042
  if (output.length !== 0) {
7007
7043
  chunks.push(output);
7008
7044
  }
7009
- return new Uint8Array();
7045
+ return new Uint8Array(0);
7010
7046
  },
7011
- finalize(input = new Uint8Array()) {
7047
+ finalize(input = new Uint8Array(0)) {
7012
7048
  const output = encryptor.finalize(input);
7013
7049
  if (output.length !== 0) {
7014
7050
  chunks.push(output);
@@ -7024,14 +7060,15 @@ var jscryptoClassic = (() => {
7024
7060
  if (input.length !== 0) {
7025
7061
  chunks.push(input);
7026
7062
  }
7027
- return new Uint8Array();
7063
+ return new Uint8Array(0);
7028
7064
  },
7029
- finalize(input = new Uint8Array()) {
7065
+ finalize(input = new Uint8Array(0)) {
7066
+ var _a;
7030
7067
  if (input.length !== 0) {
7031
7068
  chunks.push(input);
7032
7069
  }
7033
7070
  const parsed = format.parse(concatBytes(...chunks));
7034
- const { key, iv } = deriveKeyIv(registry2, options, parsed.salt ?? new Uint8Array());
7071
+ const { key, iv } = deriveKeyIv(registry2, options, (_a = parsed.salt) != null ? _a : new Uint8Array(0));
7035
7072
  return registry2.createCipher(toTransformOptions(options, key, iv)).decrypt(parsed.ciphertext);
7036
7073
  }
7037
7074
  };
@@ -7048,33 +7085,37 @@ var jscryptoClassic = (() => {
7048
7085
  function deriveSync(registry2, options, salt, length) {
7049
7086
  const kdfOptions = normalizeNamedOptions(options.kdf);
7050
7087
  const kdf = registry2.get("kdf", kdfOptions.name);
7051
- const result = kdf.derive({
7052
- ...withoutName(kdfOptions),
7088
+ const result = kdf.derive(__spreadProps(__spreadValues({}, withoutName(kdfOptions)), {
7053
7089
  passphrase: options.passphrase,
7054
7090
  salt,
7055
7091
  length
7056
- });
7092
+ }));
7057
7093
  if (result instanceof Promise) {
7058
7094
  throw new Error(`KDF ${kdfOptions.name} is asynchronous; use an async passphrase cipher API.`);
7059
7095
  }
7060
7096
  return result;
7061
7097
  }
7062
7098
  function toTransformOptions(options, key, iv) {
7063
- const {
7099
+ const _a = options, {
7064
7100
  passphrase,
7065
7101
  kdf,
7066
7102
  format,
7067
7103
  salt,
7068
7104
  saltSize,
7069
7105
  keySize,
7070
- ivSize,
7071
- ...transformOptions
7072
- } = options;
7073
- return {
7074
- ...transformOptions,
7075
- key,
7076
- ...iv ? { iv } : {}
7077
- };
7106
+ ivSize
7107
+ } = _a, transformOptions = __objRest(_a, [
7108
+ "passphrase",
7109
+ "kdf",
7110
+ "format",
7111
+ "salt",
7112
+ "saltSize",
7113
+ "keySize",
7114
+ "ivSize"
7115
+ ]);
7116
+ return __spreadValues(__spreadProps(__spreadValues({}, transformOptions), {
7117
+ key
7118
+ }), iv ? { iv } : {});
7078
7119
  }
7079
7120
  function resolveKeySize(registry2, options) {
7080
7121
  if (options.keySize !== void 0) {
@@ -7108,17 +7149,18 @@ var jscryptoClassic = (() => {
7108
7149
  return typeof options === "string" ? { name: options } : options;
7109
7150
  }
7110
7151
  function withoutName(options) {
7111
- const { name, ...rest } = options;
7152
+ const _a = options, { name } = _a, rest = __objRest(_a, ["name"]);
7112
7153
  return rest;
7113
7154
  }
7114
7155
  function randomBytes(length) {
7156
+ var _a;
7115
7157
  assertNonNegativeInteger(length, "saltSize");
7116
7158
  const bytes = new Uint8Array(length);
7117
7159
  if (bytes.length === 0) {
7118
7160
  return bytes;
7119
7161
  }
7120
7162
  const crypto = globalThis;
7121
- crypto.crypto?.getRandomValues(bytes);
7163
+ (_a = crypto.crypto) == null ? void 0 : _a.getRandomValues(bytes);
7122
7164
  if (bytes.some((byte) => byte !== 0)) {
7123
7165
  return bytes;
7124
7166
  }
@@ -7151,27 +7193,29 @@ var jscryptoClassic = (() => {
7151
7193
  const blockCipher = cipher.create(options.key);
7152
7194
  const modeEncryptor = mode.createEncryptor({
7153
7195
  cipher: blockCipher,
7154
- iv: options.iv
7196
+ iv: options.iv,
7197
+ options
7155
7198
  });
7156
7199
  if (mode.requiresPadding === false) {
7157
7200
  return createStreamEncryptor(modeEncryptor);
7158
7201
  }
7202
+ const blockPadding = padding;
7159
7203
  let finalized = false;
7160
- let pending = new Uint8Array();
7204
+ let pending = new Uint8Array(0);
7161
7205
  return {
7162
7206
  process(input) {
7163
7207
  assertNotFinalized2(finalized);
7164
7208
  const data = concatBytes(pending, input);
7165
7209
  const processLength = data.length - data.length % blockCipher.blockSize;
7166
7210
  pending = data.slice(processLength);
7167
- return processLength === 0 ? new Uint8Array() : modeEncryptor.process(data.subarray(0, processLength));
7211
+ return processLength === 0 ? new Uint8Array(0) : modeEncryptor.process(data.subarray(0, processLength));
7168
7212
  },
7169
- finalize(input = new Uint8Array()) {
7213
+ finalize(input = new Uint8Array(0)) {
7170
7214
  assertNotFinalized2(finalized);
7171
- const processed = input.length === 0 ? new Uint8Array() : this.process(input);
7215
+ const processed = input.length === 0 ? new Uint8Array(0) : this.process(input);
7172
7216
  finalized = true;
7173
- const finalBlock = padding.pad(pending, blockCipher.blockSize);
7174
- pending = new Uint8Array();
7217
+ const finalBlock = blockPadding.pad(pending, blockCipher.blockSize);
7218
+ pending = new Uint8Array(0);
7175
7219
  return concatBytes(processed, modeEncryptor.finalize(finalBlock));
7176
7220
  }
7177
7221
  };
@@ -7188,14 +7232,16 @@ var jscryptoClassic = (() => {
7188
7232
  const blockCipher = cipher.create(options.key);
7189
7233
  const modeDecryptor = mode.createDecryptor({
7190
7234
  cipher: blockCipher,
7191
- iv: options.iv
7235
+ iv: options.iv,
7236
+ options
7192
7237
  });
7193
7238
  if (mode.requiresPadding === false) {
7194
7239
  return createStreamDecryptor(modeDecryptor);
7195
7240
  }
7241
+ const blockPadding = padding;
7196
7242
  let finalized = false;
7197
- let pending = new Uint8Array();
7198
- let plaintextPending = new Uint8Array();
7243
+ let pending = new Uint8Array(0);
7244
+ let plaintextPending = new Uint8Array(0);
7199
7245
  return {
7200
7246
  process(input) {
7201
7247
  assertNotFinalized2(finalized);
@@ -7203,23 +7249,23 @@ var jscryptoClassic = (() => {
7203
7249
  const processLength = data.length - data.length % blockCipher.blockSize;
7204
7250
  pending = data.slice(processLength);
7205
7251
  if (processLength === 0) {
7206
- return new Uint8Array();
7252
+ return new Uint8Array(0);
7207
7253
  }
7208
7254
  const decrypted = modeDecryptor.process(data.subarray(0, processLength));
7209
7255
  const output = plaintextPending;
7210
7256
  plaintextPending = decrypted;
7211
7257
  return output;
7212
7258
  },
7213
- finalize(input = new Uint8Array()) {
7259
+ finalize(input = new Uint8Array(0)) {
7214
7260
  assertNotFinalized2(finalized);
7215
- const processed = input.length === 0 ? new Uint8Array() : this.process(input);
7261
+ const processed = input.length === 0 ? new Uint8Array(0) : this.process(input);
7216
7262
  finalized = true;
7217
7263
  if (pending.length !== 0) {
7218
- padding.unpad(pending, blockCipher.blockSize);
7264
+ blockPadding.unpad(pending, blockCipher.blockSize);
7219
7265
  }
7220
- const finalPlaintext = padding.unpad(plaintextPending, blockCipher.blockSize);
7221
- pending = new Uint8Array();
7222
- plaintextPending = new Uint8Array();
7266
+ const finalPlaintext = blockPadding.unpad(plaintextPending, blockCipher.blockSize);
7267
+ pending = new Uint8Array(0);
7268
+ plaintextPending = new Uint8Array(0);
7223
7269
  return concatBytes(processed, finalPlaintext, modeDecryptor.finalize());
7224
7270
  }
7225
7271
  };
@@ -7231,11 +7277,11 @@ var jscryptoClassic = (() => {
7231
7277
  assertNotFinalized2(finalized);
7232
7278
  return modeEncryptor.process(input);
7233
7279
  },
7234
- finalize(input = new Uint8Array()) {
7280
+ finalize(input = new Uint8Array(0)) {
7235
7281
  assertNotFinalized2(finalized);
7236
7282
  finalized = true;
7237
7283
  return concatBytes(
7238
- input.length === 0 ? new Uint8Array() : modeEncryptor.process(input),
7284
+ input.length === 0 ? new Uint8Array(0) : modeEncryptor.process(input),
7239
7285
  modeEncryptor.finalize()
7240
7286
  );
7241
7287
  }
@@ -7248,11 +7294,11 @@ var jscryptoClassic = (() => {
7248
7294
  assertNotFinalized2(finalized);
7249
7295
  return modeDecryptor.process(input);
7250
7296
  },
7251
- finalize(input = new Uint8Array()) {
7297
+ finalize(input = new Uint8Array(0)) {
7252
7298
  assertNotFinalized2(finalized);
7253
7299
  finalized = true;
7254
7300
  return concatBytes(
7255
- input.length === 0 ? new Uint8Array() : modeDecryptor.process(input),
7301
+ input.length === 0 ? new Uint8Array(0) : modeDecryptor.process(input),
7256
7302
  modeDecryptor.finalize()
7257
7303
  );
7258
7304
  }
@@ -7262,13 +7308,18 @@ var jscryptoClassic = (() => {
7262
7308
  if (!options.mode) {
7263
7309
  throw new Error(`${options.cipher} block cipher requires a mode.`);
7264
7310
  }
7265
- if (!options.padding) {
7266
- throw new Error(`${options.cipher} block cipher requires padding.`);
7311
+ const mode = registry2.get("mode", options.mode);
7312
+ let padding;
7313
+ if (mode.requiresPadding !== false) {
7314
+ if (!options.padding) {
7315
+ throw new Error(`${options.cipher} block cipher requires padding.`);
7316
+ }
7317
+ padding = registry2.get("padding", options.padding);
7267
7318
  }
7268
7319
  return {
7269
7320
  cipher: registry2.get("cipher", options.cipher),
7270
- mode: registry2.get("mode", options.mode),
7271
- padding: registry2.get("padding", options.padding)
7321
+ mode,
7322
+ padding
7272
7323
  };
7273
7324
  }
7274
7325
  function assertNotFinalized2(finalized) {
@@ -7433,13 +7484,14 @@ var jscryptoClassic = (() => {
7433
7484
  }
7434
7485
  };
7435
7486
  function deriveEvpKdf(params) {
7487
+ var _a;
7436
7488
  assertPositiveInteger2(params.length, "EvpKDF length");
7437
7489
  if (params.iterations !== void 0) {
7438
7490
  assertPositiveInteger2(params.iterations, "EvpKDF iterations");
7439
7491
  }
7440
7492
  const options = {
7441
7493
  keySize: params.length / 4,
7442
- iterations: params.iterations ?? 1
7494
+ iterations: (_a = params.iterations) != null ? _a : 1
7443
7495
  };
7444
7496
  const hasher = getHasher(params.hash);
7445
7497
  if (hasher) {
@@ -7462,7 +7514,7 @@ var jscryptoClassic = (() => {
7462
7514
  return hasher;
7463
7515
  }
7464
7516
  function normalizeHashName(hash) {
7465
- return hash.replaceAll("-", "").toUpperCase();
7517
+ return hash.replace(/-/g, "").toUpperCase();
7466
7518
  }
7467
7519
  function assertPositiveInteger2(value, label) {
7468
7520
  if (!Number.isInteger(value) || value <= 0) {
@@ -7506,7 +7558,7 @@ var jscryptoClassic = (() => {
7506
7558
  return hasher;
7507
7559
  }
7508
7560
  function normalizeHashName2(hash) {
7509
- return hash.replaceAll("-", "").toUpperCase();
7561
+ return hash.replace(/-/g, "").toUpperCase();
7510
7562
  }
7511
7563
  function assertPositiveInteger3(value, label) {
7512
7564
  if (!Number.isInteger(value) || value <= 0) {
@@ -7542,8 +7594,8 @@ var jscryptoClassic = (() => {
7542
7594
  }
7543
7595
  return output;
7544
7596
  },
7545
- finalize(input = new Uint8Array()) {
7546
- return input.length === 0 ? new Uint8Array() : this.process(input);
7597
+ finalize(input = new Uint8Array(0)) {
7598
+ return input.length === 0 ? new Uint8Array(0) : this.process(input);
7547
7599
  }
7548
7600
  };
7549
7601
  }
@@ -7561,8 +7613,8 @@ var jscryptoClassic = (() => {
7561
7613
  }
7562
7614
  return output;
7563
7615
  },
7564
- finalize(input = new Uint8Array()) {
7565
- return input.length === 0 ? new Uint8Array() : this.process(input);
7616
+ finalize(input = new Uint8Array(0)) {
7617
+ return input.length === 0 ? new Uint8Array(0) : this.process(input);
7566
7618
  }
7567
7619
  };
7568
7620
  }
@@ -7615,8 +7667,8 @@ var jscryptoClassic = (() => {
7615
7667
  }
7616
7668
  return output;
7617
7669
  },
7618
- finalize(input = new Uint8Array()) {
7619
- return input.length === 0 ? new Uint8Array() : this.process(input);
7670
+ finalize(input = new Uint8Array(0)) {
7671
+ return input.length === 0 ? new Uint8Array(0) : this.process(input);
7620
7672
  }
7621
7673
  };
7622
7674
  }
@@ -7653,8 +7705,8 @@ var jscryptoClassic = (() => {
7653
7705
  }
7654
7706
  return output;
7655
7707
  },
7656
- finalize(input = new Uint8Array()) {
7657
- return input.length === 0 ? new Uint8Array() : this.process(input);
7708
+ finalize(input = new Uint8Array(0)) {
7709
+ return input.length === 0 ? new Uint8Array(0) : this.process(input);
7658
7710
  }
7659
7711
  };
7660
7712
  }
@@ -7689,8 +7741,8 @@ var jscryptoClassic = (() => {
7689
7741
  }
7690
7742
  return output;
7691
7743
  },
7692
- finalize(input = new Uint8Array()) {
7693
- return input.length === 0 ? new Uint8Array() : this.process(input);
7744
+ finalize(input = new Uint8Array(0)) {
7745
+ return input.length === 0 ? new Uint8Array(0) : this.process(input);
7694
7746
  }
7695
7747
  };
7696
7748
  }
@@ -7704,8 +7756,8 @@ var jscryptoClassic = (() => {
7704
7756
  }
7705
7757
  return output;
7706
7758
  },
7707
- finalize(input = new Uint8Array()) {
7708
- return input.length === 0 ? new Uint8Array() : this.process(input);
7759
+ finalize(input = new Uint8Array(0)) {
7760
+ return input.length === 0 ? new Uint8Array(0) : this.process(input);
7709
7761
  }
7710
7762
  };
7711
7763
  }
@@ -7720,13 +7772,291 @@ var jscryptoClassic = (() => {
7720
7772
  aead: true,
7721
7773
  requiredBlockSize: 16,
7722
7774
  requiresPadding: false,
7723
- createEncryptor() {
7724
- throw new Error("GCM mode is not implemented yet.");
7775
+ createEncryptor({ cipher, iv, options }) {
7776
+ return createGcmEncryptor(cipher, getNonce(iv, options), getAad(options), getTagLength(options));
7725
7777
  },
7726
- createDecryptor() {
7727
- throw new Error("GCM mode is not implemented yet.");
7778
+ createDecryptor({ cipher, iv, options }) {
7779
+ return createGcmDecryptor(cipher, getNonce(iv, options), getAad(options), getTagLength(options), getTag(options));
7728
7780
  }
7729
7781
  };
7782
+ var BLOCK_SIZE = 16;
7783
+ var DEFAULT_TAG_LENGTH = 16;
7784
+ var R_WORD = 3774873600;
7785
+ function createGcmEncryptor(cipher, nonce, aad, tagLength) {
7786
+ assertGcmCipher(cipher);
7787
+ const auth = createGhash(cipher.encryptBlock(new Uint8Array(BLOCK_SIZE)));
7788
+ auth.update(aad);
7789
+ auth.pad();
7790
+ const j0 = createInitialCounter(auth.h, nonce);
7791
+ const tagMask = cipher.encryptBlock(j0);
7792
+ const counter = j0.slice();
7793
+ incrementCounter2(counter);
7794
+ const xor = createCounterXor(cipher, counter);
7795
+ let ciphertextLength = 0;
7796
+ return {
7797
+ process(input) {
7798
+ const ciphertext = xor(input);
7799
+ auth.update(ciphertext);
7800
+ ciphertextLength += ciphertext.length;
7801
+ return ciphertext;
7802
+ },
7803
+ finalize(input = new Uint8Array(0)) {
7804
+ const ciphertext = input.length === 0 ? new Uint8Array(0) : this.process(input);
7805
+ const tag = createTag(auth, tagMask, aad.length, ciphertextLength, tagLength);
7806
+ return concatBytes(ciphertext, tag);
7807
+ }
7808
+ };
7809
+ }
7810
+ function createGcmDecryptor(cipher, nonce, aad, tagLength, detachedTag) {
7811
+ assertGcmCipher(cipher);
7812
+ const h = cipher.encryptBlock(new Uint8Array(BLOCK_SIZE));
7813
+ const j0 = createInitialCounter(h, nonce);
7814
+ const tagMask = cipher.encryptBlock(j0);
7815
+ let pending = new Uint8Array(0);
7816
+ return {
7817
+ process(input) {
7818
+ pending = concatBytes(pending, input);
7819
+ return new Uint8Array(0);
7820
+ },
7821
+ finalize(input = new Uint8Array(0)) {
7822
+ if (input.length !== 0) {
7823
+ this.process(input);
7824
+ }
7825
+ let ciphertext = pending;
7826
+ let tag = detachedTag;
7827
+ if (!tag) {
7828
+ if (pending.length < tagLength) {
7829
+ throw new Error("GCM ciphertext must include an authentication tag.");
7830
+ }
7831
+ ciphertext = new Uint8Array(pending.subarray(0, pending.length - tagLength));
7832
+ tag = new Uint8Array(pending.subarray(pending.length - tagLength));
7833
+ }
7834
+ const auth = createGhash(h);
7835
+ auth.update(aad);
7836
+ auth.pad();
7837
+ auth.update(ciphertext);
7838
+ const expectedTag = createTag(auth, tagMask, aad.length, ciphertext.length, tagLength);
7839
+ if (!equalBytes(expectedTag, tag)) {
7840
+ throw new Error("GCM authentication failed.");
7841
+ }
7842
+ const counter = j0.slice();
7843
+ incrementCounter2(counter);
7844
+ const plaintext = createCounterXor(cipher, counter)(ciphertext);
7845
+ pending = new Uint8Array(0);
7846
+ return plaintext;
7847
+ }
7848
+ };
7849
+ }
7850
+ function createGhash(h) {
7851
+ const hValue = blockToWords(h);
7852
+ let state = createZeroWords();
7853
+ let pending = new Uint8Array(0);
7854
+ function updateBlock(block) {
7855
+ state = multiplyGf128(xorWords(state, blockToWords(padBlock(block))), hValue);
7856
+ }
7857
+ return {
7858
+ h,
7859
+ update(input) {
7860
+ const data = concatBytes(pending, input);
7861
+ const processLength = data.length - data.length % BLOCK_SIZE;
7862
+ for (let offset = 0; offset < processLength; offset += BLOCK_SIZE) {
7863
+ updateBlock(data.subarray(offset, offset + BLOCK_SIZE));
7864
+ }
7865
+ pending = data.slice(processLength);
7866
+ },
7867
+ pad() {
7868
+ if (pending.length !== 0) {
7869
+ updateBlock(pending);
7870
+ pending = new Uint8Array(0);
7871
+ }
7872
+ },
7873
+ digest(aadLength, ciphertextLength) {
7874
+ this.pad();
7875
+ updateBlock(createLengthBlock(aadLength, ciphertextLength));
7876
+ return wordsToBlock(state);
7877
+ }
7878
+ };
7879
+ }
7880
+ function createInitialCounter(h, nonce) {
7881
+ if (nonce.length === 0) {
7882
+ throw new Error("GCM mode requires a nonce.");
7883
+ }
7884
+ if (nonce.length === 12) {
7885
+ const j0 = new Uint8Array(BLOCK_SIZE);
7886
+ j0.set(nonce);
7887
+ j0[15] = 1;
7888
+ return j0;
7889
+ }
7890
+ const auth = createGhash(h);
7891
+ auth.update(nonce);
7892
+ return auth.digest(0, nonce.length);
7893
+ }
7894
+ function createCounterXor(cipher, counter) {
7895
+ let keystream = new Uint8Array(0);
7896
+ let position = BLOCK_SIZE;
7897
+ return (input) => {
7898
+ const output = new Uint8Array(input.length);
7899
+ for (let i = 0; i < input.length; i++) {
7900
+ if (position === BLOCK_SIZE) {
7901
+ keystream = cipher.encryptBlock(counter);
7902
+ incrementCounter2(counter);
7903
+ position = 0;
7904
+ }
7905
+ output[i] = input[i] ^ keystream[position];
7906
+ position++;
7907
+ }
7908
+ return output;
7909
+ };
7910
+ }
7911
+ function createTag(auth, tagMask, aadLength, ciphertextLength, tagLength) {
7912
+ const tag = auth.digest(aadLength, ciphertextLength);
7913
+ for (let i = 0; i < tag.length; i++) {
7914
+ tag[i] ^= tagMask[i];
7915
+ }
7916
+ return tag.subarray(0, tagLength);
7917
+ }
7918
+ function createZeroWords() {
7919
+ return [0, 0, 0, 0];
7920
+ }
7921
+ function multiplyGf128(x, y) {
7922
+ const z = createZeroWords();
7923
+ const v = [y[0], y[1], y[2], y[3]];
7924
+ for (let i = 0; i < 128; i++) {
7925
+ if ((x[i >>> 5] & 2147483648 >>> (i & 31)) !== 0) {
7926
+ xorWordsInPlace(z, v);
7927
+ }
7928
+ shiftRightAndReduce(v);
7929
+ }
7930
+ return z;
7931
+ }
7932
+ function shiftRightAndReduce(words) {
7933
+ const lsb = words[3] & 1;
7934
+ words[3] = (words[3] >>> 1 | (words[2] & 1) << 31) >>> 0;
7935
+ words[2] = (words[2] >>> 1 | (words[1] & 1) << 31) >>> 0;
7936
+ words[1] = (words[1] >>> 1 | (words[0] & 1) << 31) >>> 0;
7937
+ words[0] >>>= 1;
7938
+ if (lsb) {
7939
+ words[0] = (words[0] ^ R_WORD) >>> 0;
7940
+ }
7941
+ }
7942
+ function xorWords(left, right) {
7943
+ return [
7944
+ (left[0] ^ right[0]) >>> 0,
7945
+ (left[1] ^ right[1]) >>> 0,
7946
+ (left[2] ^ right[2]) >>> 0,
7947
+ (left[3] ^ right[3]) >>> 0
7948
+ ];
7949
+ }
7950
+ function xorWordsInPlace(left, right) {
7951
+ left[0] = (left[0] ^ right[0]) >>> 0;
7952
+ left[1] = (left[1] ^ right[1]) >>> 0;
7953
+ left[2] = (left[2] ^ right[2]) >>> 0;
7954
+ left[3] = (left[3] ^ right[3]) >>> 0;
7955
+ }
7956
+ function incrementCounter2(counter) {
7957
+ const value = (counter[12] << 24 >>> 0 | counter[13] << 16 | counter[14] << 8 | counter[15]) + 1 >>> 0;
7958
+ counter[12] = value >>> 24;
7959
+ counter[13] = value >>> 16;
7960
+ counter[14] = value >>> 8;
7961
+ counter[15] = value;
7962
+ }
7963
+ function padBlock(input) {
7964
+ if (input.length === BLOCK_SIZE) {
7965
+ return input;
7966
+ }
7967
+ const output = new Uint8Array(BLOCK_SIZE);
7968
+ output.set(input);
7969
+ return output;
7970
+ }
7971
+ function createLengthBlock(aadLength, ciphertextLength) {
7972
+ const output = new Uint8Array(BLOCK_SIZE);
7973
+ writeBitLength64BE(aadLength, output, 0);
7974
+ writeBitLength64BE(ciphertextLength, output, 8);
7975
+ return output;
7976
+ }
7977
+ function writeBitLength64BE(byteLength, output, offset) {
7978
+ const high = Math.floor(byteLength / 536870912);
7979
+ const low = byteLength % 536870912 * 8;
7980
+ writeUint32BE(high, output, offset);
7981
+ writeUint32BE(low, output, offset + 4);
7982
+ }
7983
+ function blockToWords(input) {
7984
+ return [
7985
+ readUint32BE(input, 0),
7986
+ readUint32BE(input, 4),
7987
+ readUint32BE(input, 8),
7988
+ readUint32BE(input, 12)
7989
+ ];
7990
+ }
7991
+ function wordsToBlock(words) {
7992
+ const output = new Uint8Array(BLOCK_SIZE);
7993
+ writeUint32BE(words[0], output, 0);
7994
+ writeUint32BE(words[1], output, 4);
7995
+ writeUint32BE(words[2], output, 8);
7996
+ writeUint32BE(words[3], output, 12);
7997
+ return output;
7998
+ }
7999
+ function readUint32BE(input, offset) {
8000
+ return (input[offset] << 24 >>> 0 | input[offset + 1] << 16 | input[offset + 2] << 8 | input[offset + 3]) >>> 0;
8001
+ }
8002
+ function writeUint32BE(value, output, offset) {
8003
+ output[offset] = value >>> 24;
8004
+ output[offset + 1] = value >>> 16;
8005
+ output[offset + 2] = value >>> 8;
8006
+ output[offset + 3] = value;
8007
+ }
8008
+ function getNonce(iv, options) {
8009
+ const nonce = getOptions(options).nonce;
8010
+ if (nonce !== void 0) {
8011
+ assertBytes(nonce, "GCM nonce");
8012
+ if (iv !== void 0) {
8013
+ throw new Error("GCM mode accepts either iv or nonce, not both.");
8014
+ }
8015
+ return nonce;
8016
+ }
8017
+ if (!iv) {
8018
+ throw new Error("GCM mode requires an IV/nonce.");
8019
+ }
8020
+ return iv;
8021
+ }
8022
+ function getAad(options) {
8023
+ const aad = getOptions(options).aad;
8024
+ if (aad === void 0) {
8025
+ return new Uint8Array(0);
8026
+ }
8027
+ assertBytes(aad, "GCM aad");
8028
+ return aad;
8029
+ }
8030
+ function getTag(options) {
8031
+ const tag = getOptions(options).tag;
8032
+ if (tag === void 0) {
8033
+ return void 0;
8034
+ }
8035
+ assertBytes(tag, "GCM tag");
8036
+ return tag;
8037
+ }
8038
+ function getTagLength(options) {
8039
+ const tag = getTag(options);
8040
+ if (tag) {
8041
+ return tag.length;
8042
+ }
8043
+ const tagLength = getOptions(options).tagLength;
8044
+ if (tagLength === void 0) {
8045
+ return DEFAULT_TAG_LENGTH;
8046
+ }
8047
+ if (typeof tagLength !== "number" || !Number.isInteger(tagLength) || tagLength < 4 || tagLength > BLOCK_SIZE) {
8048
+ throw new RangeError("GCM tagLength must be an integer between 4 and 16 bytes.");
8049
+ }
8050
+ return tagLength;
8051
+ }
8052
+ function getOptions(options) {
8053
+ return typeof options === "object" && options !== null ? options : {};
8054
+ }
8055
+ function assertGcmCipher(cipher) {
8056
+ if (cipher.blockSize !== BLOCK_SIZE) {
8057
+ throw new Error("GCM mode requires a 128-bit block cipher.");
8058
+ }
8059
+ }
7730
8060
 
7731
8061
  // packages/classic/src/modes/ofb.ts
7732
8062
  var ofb = {
@@ -7760,8 +8090,8 @@ var jscryptoClassic = (() => {
7760
8090
  }
7761
8091
  return output;
7762
8092
  },
7763
- finalize(input = new Uint8Array()) {
7764
- return input.length === 0 ? new Uint8Array() : this.process(input);
8093
+ finalize(input = new Uint8Array(0)) {
8094
+ return input.length === 0 ? new Uint8Array(0) : this.process(input);
7765
8095
  }
7766
8096
  };
7767
8097
  }
@@ -7834,11 +8164,12 @@ var jscryptoClassic = (() => {
7834
8164
  return getBlockPaddingLength(inputLength, blockSize);
7835
8165
  }
7836
8166
  function fillRandom(bytes) {
8167
+ var _a;
7837
8168
  if (bytes.length === 0) {
7838
8169
  return;
7839
8170
  }
7840
8171
  const crypto = globalThis;
7841
- crypto.crypto?.getRandomValues(bytes);
8172
+ (_a = crypto.crypto) == null ? void 0 : _a.getRandomValues(bytes);
7842
8173
  if (bytes.some((byte) => byte !== 0)) {
7843
8174
  return;
7844
8175
  }