@bigbinary/neeto-playwright-commons 1.9.33 → 1.10.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.
package/index.cjs.js CHANGED
@@ -6,23 +6,24 @@ var child_process = require('child_process');
6
6
  var fs$d = require('fs');
7
7
  var test$1 = require('@playwright/test');
8
8
  var ramda = require('ramda');
9
- var require$$0$2 = require('util');
9
+ var require$$0$1 = require('util');
10
10
  var Path = require('path');
11
11
  var faker = require('@faker-js/faker');
12
12
  var MailosaurClient = require('mailosaur');
13
13
  var dayjs = require('dayjs');
14
- var require$$0$4 = require('tty');
15
- var require$$0$3 = require('os');
14
+ var require$$0$3 = require('tty');
15
+ var require$$0$2 = require('os');
16
+ var stealth$1 = require('puppeteer-extra-plugin-stealth');
16
17
  var Stream$4 = require('stream');
17
- var require$$0$5 = require('events');
18
+ var require$$0$4 = require('events');
18
19
  var playwrightI18nextFixture = require('playwright-i18next-fixture');
19
20
  var neetoCist = require('@bigbinary/neeto-cist');
20
21
  var http$1 = require('http');
21
22
  var Url = require('url');
22
- var require$$0$6 = require('punycode');
23
+ var require$$0$5 = require('punycode');
23
24
  var https$1 = require('https');
24
25
  var zlib$5 = require('zlib');
25
- var require$$0$7 = require('assert');
26
+ var require$$0$6 = require('assert');
26
27
  var require$$3 = require('buffer');
27
28
  var require$$3$1 = require('querystring');
28
29
  var require$$1$1 = require('string_decoder');
@@ -52,21 +53,22 @@ function _interopNamespace(e) {
52
53
  var fs__namespace = /*#__PURE__*/_interopNamespace(fs$d);
53
54
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs$d);
54
55
  var test__default = /*#__PURE__*/_interopDefaultLegacy(test$1);
55
- var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
56
+ var require$$0__default = /*#__PURE__*/_interopDefaultLegacy(require$$0$1);
56
57
  var Path__default = /*#__PURE__*/_interopDefaultLegacy(Path);
57
58
  var Path__namespace = /*#__PURE__*/_interopNamespace(Path);
58
59
  var MailosaurClient__default = /*#__PURE__*/_interopDefaultLegacy(MailosaurClient);
59
60
  var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
60
- var require$$0__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$0$4);
61
- var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$3);
61
+ var require$$0__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$0$3);
62
+ var require$$0__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$0$2);
63
+ var stealth__default = /*#__PURE__*/_interopDefaultLegacy(stealth$1);
62
64
  var Stream__default = /*#__PURE__*/_interopDefaultLegacy(Stream$4);
63
- var require$$0__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$0$5);
65
+ var require$$0__default$3 = /*#__PURE__*/_interopDefaultLegacy(require$$0$4);
64
66
  var http__default = /*#__PURE__*/_interopDefaultLegacy(http$1);
65
67
  var Url__default = /*#__PURE__*/_interopDefaultLegacy(Url);
66
- var require$$0__default$4 = /*#__PURE__*/_interopDefaultLegacy(require$$0$6);
68
+ var require$$0__default$4 = /*#__PURE__*/_interopDefaultLegacy(require$$0$5);
67
69
  var https__default = /*#__PURE__*/_interopDefaultLegacy(https$1);
68
70
  var zlib__default = /*#__PURE__*/_interopDefaultLegacy(zlib$5);
69
- var require$$0__default$5 = /*#__PURE__*/_interopDefaultLegacy(require$$0$7);
71
+ var require$$0__default$5 = /*#__PURE__*/_interopDefaultLegacy(require$$0$6);
70
72
  var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3);
71
73
  var require$$3__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$3$1);
72
74
  var require$$1__default = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
@@ -2595,7 +2597,7 @@ var arrayToObject = function arrayToObject(source, options) {
2595
2597
  return obj;
2596
2598
  };
2597
2599
 
2598
- var merge$3 = function merge(target, source, options) {
2600
+ var merge$2 = function merge(target, source, options) {
2599
2601
  /* eslint no-param-reassign: 0 */
2600
2602
  if (!source) {
2601
2603
  return target;
@@ -2812,7 +2814,7 @@ var utils$o = {
2812
2814
  isBuffer: isBuffer,
2813
2815
  isRegExp: isRegExp,
2814
2816
  maybeMap: maybeMap,
2815
- merge: merge$3
2817
+ merge: merge$2
2816
2818
  };
2817
2819
 
2818
2820
  var getSideChannel = sideChannel;
@@ -3220,6 +3222,7 @@ var parseValues = function parseQueryStringValues(str, options) {
3220
3222
  var obj = { __proto__: null };
3221
3223
 
3222
3224
  var cleanStr = options.ignoreQueryPrefix ? str.replace(/^\?/, '') : str;
3225
+ cleanStr = cleanStr.replace(/%5B/gi, '[').replace(/%5D/gi, ']');
3223
3226
  var limit = options.parameterLimit === Infinity ? undefined : options.parameterLimit;
3224
3227
  var parts = cleanStr.split(options.delimiter, limit);
3225
3228
  var skipIndex = -1; // Keep track of where the utf8 sentinel was found
@@ -4889,7 +4892,7 @@ if (typeof process === 'undefined' || process.type === 'renderer' || process.bro
4889
4892
  }
4890
4893
 
4891
4894
  var srcExports = src$1.exports;
4892
- var debug$3 = /*@__PURE__*/getDefaultExportFromCjs(srcExports);
4895
+ var Debug = /*@__PURE__*/getDefaultExportFromCjs(srcExports);
4893
4896
 
4894
4897
  /*!
4895
4898
  * playwright-extra v4.3.5 by berstend
@@ -4972,7 +4975,7 @@ const playwrightLoader = new Loader('playwright', [
4972
4975
  'playwright'
4973
4976
  ]);
4974
4977
 
4975
- const debug = debug$3('playwright-extra:puppeteer-compat');
4978
+ const debug = Debug('playwright-extra:puppeteer-compat');
4976
4979
  const isPlaywrightPage = (obj) => {
4977
4980
  return 'unroute' in obj;
4978
4981
  };
@@ -5162,7 +5165,7 @@ function createBrowserShim(browser) {
5162
5165
  return shim;
5163
5166
  }
5164
5167
 
5165
- const debug$1 = debug$3('playwright-extra:plugins');
5168
+ const debug$1 = Debug('playwright-extra:plugins');
5166
5169
  class PluginList {
5167
5170
  constructor() {
5168
5171
  this._plugins = [];
@@ -5503,7 +5506,7 @@ If your bundler has issues with dynamic imports take a look at '.plugins.setDepe
5503
5506
  }
5504
5507
  }
5505
5508
 
5506
- const debug$2 = debug$3('playwright-extra');
5509
+ const debug$2 = Debug('playwright-extra');
5507
5510
  /**
5508
5511
  * Modular plugin framework to teach `playwright` new tricks.
5509
5512
  */
@@ -5781,719 +5784,10 @@ playwrightLoader.lazyloadExportOrDie('selectors');
5781
5784
  playwrightLoader.lazyloadExportOrDie('devices');
5782
5785
  playwrightLoader.lazyloadExportOrDie('errors');
5783
5786
 
5784
- /*!
5785
- * puppeteer-extra-plugin v3.2.2 by berstend
5786
- * https://github.com/berstend/puppeteer-extra/tree/master/packages/puppeteer-extra-plugin
5787
- * @license MIT
5788
- */
5789
-
5790
- /** @private */
5791
- const merge$2 = require('merge-deep');
5792
- /**
5793
- * Base class for `puppeteer-extra` plugins.
5794
- *
5795
- * Provides convenience methods to avoid boilerplate.
5796
- *
5797
- * All common `puppeteer` browser events will be bound to
5798
- * the plugin instance, if a respectively named class member is found.
5799
- *
5800
- * Please refer to the [puppeteer API documentation](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md) as well.
5801
- *
5802
- * @example
5803
- * // hello-world-plugin.js
5804
- * const { PuppeteerExtraPlugin } = require('puppeteer-extra-plugin')
5805
- *
5806
- * class Plugin extends PuppeteerExtraPlugin {
5807
- * constructor (opts = { }) { super(opts) }
5808
- *
5809
- * get name () { return 'hello-world' }
5810
- *
5811
- * async onPageCreated (page) {
5812
- * this.debug('page created', page.url())
5813
- * const ua = await page.browser().userAgent()
5814
- * this.debug('user agent', ua)
5815
- * }
5816
- * }
5817
- *
5818
- * module.exports = function (pluginConfig) { return new Plugin(pluginConfig) }
5819
- *
5820
- *
5821
- * // foo.js
5822
- * const puppeteer = require('puppeteer-extra')
5823
- * puppeteer.use(require('./hello-world-plugin')())
5824
- *
5825
- * ;(async () => {
5826
- * const browser = await puppeteer.launch({headless: false})
5827
- * const page = await browser.newPage()
5828
- * await page.goto('http://example.com', {waitUntil: 'domcontentloaded'})
5829
- * await browser.close()
5830
- * })()
5831
- *
5832
- */
5833
- class PuppeteerExtraPlugin$1 {
5834
- constructor(opts) {
5835
- this._debugBase = debug$3(`puppeteer-extra-plugin:base:${this.name}`);
5836
- this._childClassMembers = [];
5837
- this._opts = merge$2(this.defaults, opts || {});
5838
- this._debugBase('Initialized.');
5839
- }
5840
- /**
5841
- * Plugin name (required).
5842
- *
5843
- * Convention:
5844
- * - Package: `puppeteer-extra-plugin-anonymize-ua`
5845
- * - Name: `anonymize-ua`
5846
- *
5847
- * @example
5848
- * get name () { return 'anonymize-ua' }
5849
- */
5850
- get name() {
5851
- throw new Error('Plugin must override "name"');
5852
- }
5853
- /**
5854
- * Plugin defaults (optional).
5855
- *
5856
- * If defined will be ([deep-](https://github.com/jonschlinkert/merge-deep))merged with the (optional) user supplied options (supplied during plugin instantiation).
5857
- *
5858
- * The result of merging defaults with user supplied options can be accessed through `this.opts`.
5859
- *
5860
- * @see [[opts]]
5861
- *
5862
- * @example
5863
- * get defaults () {
5864
- * return {
5865
- * stripHeadless: true,
5866
- * makeWindows: true,
5867
- * customFn: null
5868
- * }
5869
- * }
5870
- *
5871
- * // Users can overwrite plugin defaults during instantiation:
5872
- * puppeteer.use(require('puppeteer-extra-plugin-foobar')({ makeWindows: false }))
5873
- */
5874
- get defaults() {
5875
- return {};
5876
- }
5877
- /**
5878
- * Plugin requirements (optional).
5879
- *
5880
- * Signal certain plugin requirements to the base class and the user.
5881
- *
5882
- * Currently supported:
5883
- * - `launch`
5884
- * - If the plugin only supports locally created browser instances (no `puppeteer.connect()`),
5885
- * will output a warning to the user.
5886
- * - `headful`
5887
- * - If the plugin doesn't work in `headless: true` mode,
5888
- * will output a warning to the user.
5889
- * - `dataFromPlugins`
5890
- * - In case the plugin requires data from other plugins.
5891
- * will enable usage of `this.getDataFromPlugins()`.
5892
- * - `runLast`
5893
- * - In case the plugin prefers to run after the others.
5894
- * Useful when the plugin needs data from others.
5895
- *
5896
- * @example
5897
- * get requirements () {
5898
- * return new Set(['runLast', 'dataFromPlugins'])
5899
- * }
5900
- */
5901
- get requirements() {
5902
- return new Set([]);
5903
- }
5904
- /**
5905
- * Plugin dependencies (optional).
5906
- *
5907
- * Missing plugins will be required() by puppeteer-extra.
5908
- *
5909
- * @example
5910
- * get dependencies () {
5911
- * return new Set(['user-preferences'])
5912
- * }
5913
- * // Will ensure the 'puppeteer-extra-plugin-user-preferences' plugin is loaded.
5914
- */
5915
- get dependencies() {
5916
- return new Set([]);
5917
- }
5918
- /**
5919
- * Plugin data (optional).
5920
- *
5921
- * Plugins can expose data (an array of objects), which in turn can be consumed by other plugins,
5922
- * that list the `dataFromPlugins` requirement (by using `this.getDataFromPlugins()`).
5923
- *
5924
- * Convention: `[ {name: 'Any name', value: 'Any value'} ]`
5925
- *
5926
- * @see [[getDataFromPlugins]]
5927
- *
5928
- * @example
5929
- * // plugin1.js
5930
- * get data () {
5931
- * return [
5932
- * {
5933
- * name: 'userPreferences',
5934
- * value: { foo: 'bar' }
5935
- * },
5936
- * {
5937
- * name: 'userPreferences',
5938
- * value: { hello: 'world' }
5939
- * }
5940
- * ]
5941
- *
5942
- * // plugin2.js
5943
- * get requirements () { return new Set(['dataFromPlugins']) }
5944
- *
5945
- * async beforeLaunch () {
5946
- * const prefs = this.getDataFromPlugins('userPreferences').map(d => d.value)
5947
- * this.debug(prefs) // => [ { foo: 'bar' }, { hello: 'world' } ]
5948
- * }
5949
- */
5950
- get data() {
5951
- return [];
5952
- }
5953
- /**
5954
- * Access the plugin options (usually the `defaults` merged with user defined options)
5955
- *
5956
- * To skip the auto-merging of defaults with user supplied opts don't define a `defaults`
5957
- * property and set the `this._opts` Object in your plugin constructor directly.
5958
- *
5959
- * @see [[defaults]]
5960
- *
5961
- * @example
5962
- * get defaults () { return { foo: "bar" } }
5963
- *
5964
- * async onPageCreated (page) {
5965
- * this.debug(this.opts.foo) // => bar
5966
- * }
5967
- */
5968
- get opts() {
5969
- return this._opts;
5970
- }
5971
- /**
5972
- * Convenience debug logger based on the [debug] module.
5973
- * Will automatically namespace the logging output to the plugin package name.
5974
- * [debug]: https://www.npmjs.com/package/debug
5975
- *
5976
- * ```bash
5977
- * # toggle output using environment variables
5978
- * DEBUG=puppeteer-extra-plugin:<plugin_name> node foo.js
5979
- * # to debug all the things:
5980
- * DEBUG=puppeteer-extra,puppeteer-extra-plugin:* node foo.js
5981
- * ```
5982
- *
5983
- * @example
5984
- * this.debug('hello world')
5985
- * // will output e.g. 'puppeteer-extra-plugin:anonymize-ua hello world'
5986
- */
5987
- get debug() {
5988
- return debug$3(`puppeteer-extra-plugin:${this.name}`);
5989
- }
5990
- /**
5991
- * Before a new browser instance is created/launched.
5992
- *
5993
- * Can be used to modify the puppeteer launch options by modifying or returning them.
5994
- *
5995
- * Plugins using this method will be called in sequence to each
5996
- * be able to update the launch options.
5997
- *
5998
- * @example
5999
- * async beforeLaunch (options) {
6000
- * if (this.opts.flashPluginPath) {
6001
- * options.args.push(`--ppapi-flash-path=${this.opts.flashPluginPath}`)
6002
- * }
6003
- * }
6004
- *
6005
- * @param options - Puppeteer launch options
6006
- */
6007
- async beforeLaunch(options) {
6008
- // noop
6009
- }
6010
- /**
6011
- * After the browser has launched.
6012
- *
6013
- * Note: Don't assume that there will only be a single browser instance during the lifecycle of a plugin.
6014
- * It's possible that `pupeeteer.launch` will be called multiple times and more than one browser created.
6015
- * In order to make the plugins as stateless as possible don't store a reference to the browser instance
6016
- * in the plugin but rather consider alternatives.
6017
- *
6018
- * E.g. when using `onPageCreated` you can get a browser reference by using `page.browser()`.
6019
- *
6020
- * Alternatively you could expose a class method that takes a browser instance as a parameter to work with:
6021
- *
6022
- * ```es6
6023
- * const fancyPlugin = require('puppeteer-extra-plugin-fancy')()
6024
- * puppeteer.use(fancyPlugin)
6025
- * const browser = await puppeteer.launch()
6026
- * await fancyPlugin.killBrowser(browser)
6027
- * ```
6028
- *
6029
- * @param browser - The `puppeteer` browser instance.
6030
- * @param opts.options - Puppeteer launch options used.
6031
- *
6032
- * @example
6033
- * async afterLaunch (browser, opts) {
6034
- * this.debug('browser has been launched', opts.options)
6035
- * }
6036
- */
6037
- async afterLaunch(browser, opts = { options: {} }) {
6038
- // noop
6039
- }
6040
- /**
6041
- * Before connecting to an existing browser instance.
6042
- *
6043
- * Can be used to modify the puppeteer connect options by modifying or returning them.
6044
- *
6045
- * Plugins using this method will be called in sequence to each
6046
- * be able to update the launch options.
6047
- *
6048
- * @param {Object} options - Puppeteer connect options
6049
- * @return {Object=}
6050
- */
6051
- async beforeConnect(options) {
6052
- // noop
6053
- }
6054
- /**
6055
- * After connecting to an existing browser instance.
6056
- *
6057
- * > Note: Don't assume that there will only be a single browser instance during the lifecycle of a plugin.
6058
- *
6059
- * @param browser - The `puppeteer` browser instance.
6060
- * @param {Object} opts
6061
- * @param {Object} opts.options - Puppeteer connect options used.
6062
- *
6063
- */
6064
- async afterConnect(browser, opts = {}) {
6065
- // noop
6066
- }
6067
- /**
6068
- * Called when a browser instance is available.
6069
- *
6070
- * This applies to both `puppeteer.launch()` and `puppeteer.connect()`.
6071
- *
6072
- * Convenience method created for plugins that need access to a browser instance
6073
- * and don't mind if it has been created through `launch` or `connect`.
6074
- *
6075
- * > Note: Don't assume that there will only be a single browser instance during the lifecycle of a plugin.
6076
- *
6077
- * @param browser - The `puppeteer` browser instance.
6078
- */
6079
- async onBrowser(browser, opts) {
6080
- // noop
6081
- }
6082
- /**
6083
- * Called when a target is created, for example when a new page is opened by window.open or browser.newPage.
6084
- *
6085
- * > Note: This includes target creations in incognito browser contexts.
6086
- *
6087
- * > Note: This includes browser instances created through `.launch()` as well as `.connect()`.
6088
- *
6089
- * @param {Puppeteer.Target} target
6090
- */
6091
- async onTargetCreated(target) {
6092
- // noop
6093
- }
6094
- /**
6095
- * Same as `onTargetCreated` but prefiltered to only contain Pages, for convenience.
6096
- *
6097
- * > Note: This includes page creations in incognito browser contexts.
6098
- *
6099
- * > Note: This includes browser instances created through `.launch()` as well as `.connect()`.
6100
- *
6101
- * @param {Puppeteer.Target} target
6102
- *
6103
- * @example
6104
- * async onPageCreated (page) {
6105
- * let ua = await page.browser().userAgent()
6106
- * if (this.opts.stripHeadless) {
6107
- * ua = ua.replace('HeadlessChrome/', 'Chrome/')
6108
- * }
6109
- * this.debug('new ua', ua)
6110
- * await page.setUserAgent(ua)
6111
- * }
6112
- */
6113
- async onPageCreated(page) {
6114
- // noop
6115
- }
6116
- /**
6117
- * Called when the url of a target changes.
6118
- *
6119
- * > Note: This includes target changes in incognito browser contexts.
6120
- *
6121
- * > Note: This includes browser instances created through `.launch()` as well as `.connect()`.
6122
- *
6123
- * @param {Puppeteer.Target} target
6124
- */
6125
- async onTargetChanged(target) {
6126
- // noop
6127
- }
6128
- /**
6129
- * Called when a target is destroyed, for example when a page is closed.
6130
- *
6131
- * > Note: This includes target destructions in incognito browser contexts.
6132
- *
6133
- * > Note: This includes browser instances created through `.launch()` as well as `.connect()`.
6134
- *
6135
- * @param {Puppeteer.Target} target
6136
- */
6137
- async onTargetDestroyed(target) {
6138
- // noop
6139
- }
6140
- /**
6141
- * Called when Puppeteer gets disconnected from the Chromium instance.
6142
- *
6143
- * This might happen because of one of the following:
6144
- * - Chromium is closed or crashed
6145
- * - The `browser.disconnect` method was called
6146
- */
6147
- async onDisconnected() {
6148
- // noop
6149
- }
6150
- /**
6151
- * **Deprecated:** Since puppeteer v1.6.0 `onDisconnected` has been improved
6152
- * and should be used instead of `onClose`.
6153
- *
6154
- * In puppeteer < v1.6.0 `onDisconnected` was not catching all exit scenarios.
6155
- * In order for plugins to clean up properly (e.g. deleting temporary files)
6156
- * the `onClose` method had been introduced.
6157
- *
6158
- * > Note: Might be called multiple times on exit.
6159
- *
6160
- * > Note: This only includes browser instances created through `.launch()`.
6161
- */
6162
- async onClose() {
6163
- // noop
6164
- }
6165
- /**
6166
- * After the plugin has been registered in `puppeteer-extra`.
6167
- *
6168
- * Normally right after `puppeteer.use(plugin)` is called
6169
- */
6170
- async onPluginRegistered() {
6171
- // noop
6172
- }
6173
- /**
6174
- * Helper method to retrieve `data` objects from other plugins.
6175
- *
6176
- * A plugin needs to state the `dataFromPlugins` requirement
6177
- * in order to use this method. Will be mapped to `puppeteer.getPluginData`.
6178
- *
6179
- * @param name - Filter data by `name` property
6180
- *
6181
- * @see [data]
6182
- * @see [requirements]
6183
- */
6184
- getDataFromPlugins(name) {
6185
- return [];
6186
- }
6187
- /**
6188
- * Will match plugin dependencies against all currently registered plugins.
6189
- * Is being called by `puppeteer-extra` and used to require missing dependencies.
6190
- *
6191
- * @param {Array<Object>} plugins
6192
- * @return {Set} - list of missing plugin names
6193
- *
6194
- * @private
6195
- */
6196
- _getMissingDependencies(plugins) {
6197
- const pluginNames = new Set(plugins.map((p) => p.name));
6198
- const missing = new Set(Array.from(this.dependencies.values()).filter(x => !pluginNames.has(x)));
6199
- return missing;
6200
- }
6201
- /**
6202
- * Conditionally bind browser/process events to class members.
6203
- * The idea is to reduce event binding boilerplate in plugins.
6204
- *
6205
- * For efficiency we make sure the plugin is using the respective event
6206
- * by checking the child class members before registering the listener.
6207
- *
6208
- * @param {<Puppeteer.Browser>} browser
6209
- * @param {Object} opts - Options
6210
- * @param {string} opts.context - Puppeteer context (launch/connect)
6211
- * @param {Object} [opts.options] - Puppeteer launch or connect options
6212
- * @param {Array<string>} [opts.defaultArgs] - The default flags that Chromium will be launched with
6213
- *
6214
- * @private
6215
- */
6216
- async _bindBrowserEvents(browser, opts = {}) {
6217
- if (this._hasChildClassMember('onTargetCreated') ||
6218
- this._hasChildClassMember('onPageCreated')) {
6219
- browser.on('targetcreated', this._onTargetCreated.bind(this));
6220
- }
6221
- if (this._hasChildClassMember('onTargetChanged') && this.onTargetChanged) {
6222
- browser.on('targetchanged', this.onTargetChanged.bind(this));
6223
- }
6224
- if (this._hasChildClassMember('onTargetDestroyed') &&
6225
- this.onTargetDestroyed) {
6226
- browser.on('targetdestroyed', this.onTargetDestroyed.bind(this));
6227
- }
6228
- if (this._hasChildClassMember('onDisconnected') && this.onDisconnected) {
6229
- browser.on('disconnected', this.onDisconnected.bind(this));
6230
- }
6231
- if (opts.context === 'launch' && this._hasChildClassMember('onClose')) {
6232
- // The disconnect event has been improved since puppeteer v1.6.0
6233
- // onClose is being kept mostly for legacy reasons
6234
- if (this.onClose) {
6235
- process.on('exit', this.onClose.bind(this));
6236
- browser.on('disconnected', this.onClose.bind(this));
6237
- if (opts.options.handleSIGINT !== false) {
6238
- process.on('SIGINT', this.onClose.bind(this));
6239
- }
6240
- if (opts.options.handleSIGTERM !== false) {
6241
- process.on('SIGTERM', this.onClose.bind(this));
6242
- }
6243
- if (opts.options.handleSIGHUP !== false) {
6244
- process.on('SIGHUP', this.onClose.bind(this));
6245
- }
6246
- }
6247
- }
6248
- if (opts.context === 'launch' && this.afterLaunch) {
6249
- await this.afterLaunch(browser, opts);
6250
- }
6251
- if (opts.context === 'connect' && this.afterConnect) {
6252
- await this.afterConnect(browser, opts);
6253
- }
6254
- if (this.onBrowser)
6255
- await this.onBrowser(browser, opts);
6256
- }
6257
- /**
6258
- * @private
6259
- */
6260
- async _onTargetCreated(target) {
6261
- if (this.onTargetCreated)
6262
- await this.onTargetCreated(target);
6263
- // Pre filter pages for plugin developers convenience
6264
- if (target.type() === 'page') {
6265
- try {
6266
- const page = await target.page();
6267
- if (!page) {
6268
- return;
6269
- }
6270
- const validPage = 'isClosed' in page && !page.isClosed();
6271
- if (this.onPageCreated && validPage) {
6272
- await this.onPageCreated(page);
6273
- }
6274
- }
6275
- catch (err) {
6276
- console.error(err);
6277
- }
6278
- }
6279
- }
6280
- /**
6281
- * @private
6282
- */
6283
- _register(prototype) {
6284
- this._registerChildClassMembers(prototype);
6285
- if (this.onPluginRegistered)
6286
- this.onPluginRegistered();
6287
- }
6288
- /**
6289
- * @private
6290
- */
6291
- _registerChildClassMembers(prototype) {
6292
- this._childClassMembers = Object.getOwnPropertyNames(prototype);
6293
- }
6294
- /**
6295
- * @private
6296
- */
6297
- _hasChildClassMember(name) {
6298
- return !!this._childClassMembers.includes(name);
6299
- }
6300
- /**
6301
- * @private
6302
- */
6303
- get _isPuppeteerExtraPlugin() {
6304
- return true;
6305
- }
6306
- }
6307
-
6308
- var index_esm = /*#__PURE__*/Object.freeze({
6309
- __proto__: null,
6310
- PuppeteerExtraPlugin: PuppeteerExtraPlugin$1
6311
- });
6312
-
6313
- var require$$0$1 = /*@__PURE__*/getAugmentedNamespace(index_esm);
6314
-
6315
- const { PuppeteerExtraPlugin } = require$$0$1;
6316
-
6317
- /**
6318
- * Stealth mode: Applies various techniques to make detection of headless puppeteer harder. 💯
6319
- *
6320
- * ### Purpose
6321
- * There are a couple of ways the use of puppeteer can easily be detected by a target website.
6322
- * The addition of `HeadlessChrome` to the user-agent being only the most obvious one.
6323
- *
6324
- * The goal of this plugin is to be the definite companion to puppeteer to avoid
6325
- * detection, applying new techniques as they surface.
6326
- *
6327
- * As this cat & mouse game is in it's infancy and fast-paced the plugin
6328
- * is kept as flexibile as possible, to support quick testing and iterations.
6329
- *
6330
- * ### Modularity
6331
- * This plugin uses `puppeteer-extra`'s dependency system to only require
6332
- * code mods for evasions that have been enabled, to keep things modular and efficient.
6333
- *
6334
- * The `stealth` plugin is a convenience wrapper that requires multiple [evasion techniques](./evasions/)
6335
- * automatically and comes with defaults. You could also bypass the main module and require
6336
- * specific evasion plugins yourself, if you whish to do so (as they're standalone `puppeteer-extra` plugins):
6337
- *
6338
- * ```es6
6339
- * // bypass main module and require a specific stealth plugin directly:
6340
- * puppeteer.use(require('puppeteer-extra-plugin-stealth/evasions/console.debug')())
6341
- * ```
6342
- *
6343
- * ### Contributing
6344
- * PRs are welcome, if you want to add a new evasion technique I suggest you
6345
- * look at the [template](./evasions/_template) to kickstart things.
6346
- *
6347
- * ### Kudos
6348
- * Thanks to [Evan Sangaline](https://intoli.com/blog/not-possible-to-block-chrome-headless/) and [Paul Irish](https://github.com/paulirish/headless-cat-n-mouse) for kickstarting the discussion!
6349
- *
6350
- * ---
6351
- *
6352
- * @todo
6353
- * - white-/blacklist with url globs (make this a generic plugin method?)
6354
- * - dynamic whitelist based on function evaluation
6355
- *
6356
- * @example
6357
- * const puppeteer = require('puppeteer-extra')
6358
- * // Enable stealth plugin with all evasions
6359
- * puppeteer.use(require('puppeteer-extra-plugin-stealth')())
6360
- *
6361
- *
6362
- * ;(async () => {
6363
- * // Launch the browser in headless mode and set up a page.
6364
- * const browser = await puppeteer.launch({ args: ['--no-sandbox'], headless: true })
6365
- * const page = await browser.newPage()
6366
- *
6367
- * // Navigate to the page that will perform the tests.
6368
- * const testUrl = 'https://intoli.com/blog/' +
6369
- * 'not-possible-to-block-chrome-headless/chrome-headless-test.html'
6370
- * await page.goto(testUrl)
6371
- *
6372
- * // Save a screenshot of the results.
6373
- * const screenshotPath = '/tmp/headless-test-result.png'
6374
- * await page.screenshot({path: screenshotPath})
6375
- * console.log('have a look at the screenshot:', screenshotPath)
6376
- *
6377
- * await browser.close()
6378
- * })()
6379
- *
6380
- * @param {Object} [opts] - Options
6381
- * @param {Set<string>} [opts.enabledEvasions] - Specify which evasions to use (by default all)
6382
- *
6383
- */
6384
- class StealthPlugin extends PuppeteerExtraPlugin {
6385
- constructor(opts = {}) {
6386
- super(opts);
6387
- }
6388
-
6389
- get name() {
6390
- return 'stealth'
6391
- }
6392
-
6393
- get defaults() {
6394
- const availableEvasions = new Set([
6395
- 'chrome.app',
6396
- 'chrome.csi',
6397
- 'chrome.loadTimes',
6398
- 'chrome.runtime',
6399
- 'defaultArgs',
6400
- 'iframe.contentWindow',
6401
- 'media.codecs',
6402
- 'navigator.hardwareConcurrency',
6403
- 'navigator.languages',
6404
- 'navigator.permissions',
6405
- 'navigator.plugins',
6406
- 'navigator.webdriver',
6407
- 'sourceurl',
6408
- 'user-agent-override',
6409
- 'webgl.vendor',
6410
- 'window.outerdimensions'
6411
- ]);
6412
- return {
6413
- availableEvasions,
6414
- // Enable all available evasions by default
6415
- enabledEvasions: new Set([...availableEvasions])
6416
- }
6417
- }
6418
-
6419
- /**
6420
- * Requires evasion techniques dynamically based on configuration.
6421
- *
6422
- * @private
6423
- */
6424
- get dependencies() {
6425
- return new Set(
6426
- [...this.opts.enabledEvasions].map(e => `${this.name}/evasions/${e}`)
6427
- )
6428
- }
6429
-
6430
- /**
6431
- * Get all available evasions.
6432
- *
6433
- * Please look into the [evasions directory](./evasions/) for an up to date list.
6434
- *
6435
- * @type {Set<string>} - A Set of all available evasions.
6436
- *
6437
- * @example
6438
- * const pluginStealth = require('puppeteer-extra-plugin-stealth')()
6439
- * console.log(pluginStealth.availableEvasions) // => Set { 'user-agent', 'console.debug' }
6440
- * puppeteer.use(pluginStealth)
6441
- */
6442
- get availableEvasions() {
6443
- return this.defaults.availableEvasions
6444
- }
6445
-
6446
- /**
6447
- * Get all enabled evasions.
6448
- *
6449
- * Enabled evasions can be configured either through `opts` or by modifying this property.
6450
- *
6451
- * @type {Set<string>} - A Set of all enabled evasions.
6452
- *
6453
- * @example
6454
- * // Remove specific evasion from enabled ones dynamically
6455
- * const pluginStealth = require('puppeteer-extra-plugin-stealth')()
6456
- * pluginStealth.enabledEvasions.delete('console.debug')
6457
- * puppeteer.use(pluginStealth)
6458
- */
6459
- get enabledEvasions() {
6460
- return this.opts.enabledEvasions
6461
- }
6462
-
6463
- /**
6464
- * @private
6465
- */
6466
- set enabledEvasions(evasions) {
6467
- this.opts.enabledEvasions = evasions;
6468
- }
6469
-
6470
- async onBrowser(browser) {
6471
- if (browser && browser.setMaxListeners) {
6472
- // Increase event emitter listeners to prevent MaxListenersExceededWarning
6473
- browser.setMaxListeners(30);
6474
- }
6475
- }
6476
- }
6477
-
6478
- /**
6479
- * Default export, PuppeteerExtraStealthPlugin
6480
- *
6481
- * @param {Object} [opts] - Options
6482
- * @param {Set<string>} [opts.enabledEvasions] - Specify which evasions to use (by default all)
6483
- */
6484
- const defaultExport = opts => new StealthPlugin(opts);
6485
- var puppeteerExtraPluginStealth = defaultExport;
6486
-
6487
- // const moduleExport = defaultExport
6488
- // moduleExport.StealthPlugin = StealthPlugin
6489
- // module.exports = moduleExport
6490
-
6491
- var stealth$1 = /*@__PURE__*/getDefaultExportFromCjs(puppeteerExtraPluginStealth);
6492
-
6493
5787
  var stealth = test$1.test.extend({
6494
5788
  browser: async ({ browser }, use) => {
6495
5789
  await browser.close();
6496
- chromium.use(stealth$1());
5790
+ chromium.use(stealth__default["default"]());
6497
5791
  const stealthBrowser = await chromium.launch();
6498
5792
  await use(stealthBrowser);
6499
5793
  await stealthBrowser.close();
@@ -13551,7 +12845,7 @@ class EmbedBase {
13551
12845
  }
13552
12846
 
13553
12847
  const CHAT_WIDGET_TEXTS = {
13554
- newConversation: "New Conversation",
12848
+ newConversation: "Ask a question",
13555
12849
  welcomeChatBubble: "Hi! I'm here to assist you with any questions you may have. What can I do for you?",
13556
12850
  };
13557
12851
  const MEMBER_TEXTS = {
@@ -17402,12 +16696,12 @@ function intToRGBA(i) {
17402
16696
  a
17403
16697
  };
17404
16698
  }
17405
- const c$1 = [];
16699
+ const c = [];
17406
16700
  function initCoefficients(size) {
17407
16701
  for (let i = 1; i < size; i++) {
17408
- c$1[i] = 1;
16702
+ c[i] = 1;
17409
16703
  }
17410
- c$1[0] = 1 / Math.sqrt(2.0);
16704
+ c[0] = 1 / Math.sqrt(2.0);
17411
16705
  }
17412
16706
  function applyDCT(f, size) {
17413
16707
  const N = size;
@@ -17421,7 +16715,7 @@ function applyDCT(f, size) {
17421
16715
  sum += Math.cos((2 * i + 1) / (2.0 * N) * u * Math.PI) * Math.cos((2 * j + 1) / (2.0 * N) * v * Math.PI) * f[i][j];
17422
16716
  }
17423
16717
  }
17424
- sum *= c$1[u] * c$1[v] / 4;
16718
+ sum *= c[u] * c[v] / 4;
17425
16719
  F[u][v] = sum;
17426
16720
  }
17427
16721
  }
@@ -99128,7 +98422,7 @@ function multiply(src, dst) {
99128
98422
  a
99129
98423
  };
99130
98424
  }
99131
- function add(src, dst) {
98425
+ function add$1(src, dst) {
99132
98426
  let ops = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
99133
98427
  src.a *= ops;
99134
98428
  const a = dst.a + src.a - dst.a * src.a;
@@ -99294,7 +98588,7 @@ var compositeModes = /*#__PURE__*/Object.freeze({
99294
98588
  srcOver: srcOver,
99295
98589
  dstOver: dstOver,
99296
98590
  multiply: multiply,
99297
- add: add,
98591
+ add: add$1,
99298
98592
  screen: screen,
99299
98593
  overlay: overlay,
99300
98594
  darken: darken,
@@ -120784,31 +120078,31 @@ __export(constants_exports, {
120784
120078
  // src/constants/bt709.ts
120785
120079
  var bt709_exports = {};
120786
120080
  __export(bt709_exports, {
120787
- Y: () => Y$1,
120788
- x: () => x$1,
120789
- y: () => y$1
120081
+ Y: () => Y,
120082
+ x: () => x,
120083
+ y: () => y
120790
120084
  });
120791
- var Y$1 = /* @__PURE__ */ ((Y2) => {
120085
+ var Y = /* @__PURE__ */ ((Y2) => {
120792
120086
  Y2[Y2["RED"] = 0.2126] = "RED";
120793
120087
  Y2[Y2["GREEN"] = 0.7152] = "GREEN";
120794
120088
  Y2[Y2["BLUE"] = 0.0722] = "BLUE";
120795
120089
  Y2[Y2["WHITE"] = 1] = "WHITE";
120796
120090
  return Y2;
120797
- })(Y$1 || {});
120798
- var x$1 = /* @__PURE__ */ ((x2) => {
120091
+ })(Y || {});
120092
+ var x = /* @__PURE__ */ ((x2) => {
120799
120093
  x2[x2["RED"] = 0.64] = "RED";
120800
120094
  x2[x2["GREEN"] = 0.3] = "GREEN";
120801
120095
  x2[x2["BLUE"] = 0.15] = "BLUE";
120802
120096
  x2[x2["WHITE"] = 0.3127] = "WHITE";
120803
120097
  return x2;
120804
- })(x$1 || {});
120805
- var y$1 = /* @__PURE__ */ ((y2) => {
120098
+ })(x || {});
120099
+ var y = /* @__PURE__ */ ((y2) => {
120806
120100
  y2[y2["RED"] = 0.33] = "RED";
120807
120101
  y2[y2["GREEN"] = 0.6] = "GREEN";
120808
120102
  y2[y2["BLUE"] = 0.06] = "BLUE";
120809
120103
  y2[y2["WHITE"] = 0.329] = "WHITE";
120810
120104
  return y2;
120811
- })(y$1 || {});
120105
+ })(y || {});
120812
120106
 
120813
120107
  // src/conversion/index.ts
120814
120108
  var conversion_exports = {};
@@ -147104,1094 +146398,1420 @@ const decodeQRCodeFromFile = async (filePath) => {
147104
146398
  return decodedString;
147105
146399
  };
147106
146400
 
146401
+ //! otpauth 9.3.1 | (c) Héctor Molinero Fernández | MIT | https://github.com/hectorm/otpauth
146402
+ //! noble-hashes 1.4.0 | (c) Paul Miller | MIT | https://github.com/paulmillr/noble-hashes
147107
146403
  /// <reference types="./otpauth.d.ts" />
146404
+ // @ts-nocheck
147108
146405
  /**
147109
- * Converts an integer to an ArrayBuffer.
146406
+ * Converts an integer to an Uint8Array.
147110
146407
  * @param {number} num Integer.
147111
- * @returns {ArrayBuffer} ArrayBuffer.
147112
- */
147113
- const uintToBuf = num => {
147114
- const buf = new ArrayBuffer(8);
147115
- const arr = new Uint8Array(buf);
147116
- let acc = num;
147117
- for (let i = 7; i >= 0; i--) {
147118
- if (acc === 0) break;
147119
- arr[i] = acc & 255;
147120
- acc -= arr[i];
147121
- acc /= 256;
147122
- }
147123
- return buf;
146408
+ * @returns {Uint8Array} Uint8Array.
146409
+ */ const uintDecode = (num)=>{
146410
+ const buf = new ArrayBuffer(8);
146411
+ const arr = new Uint8Array(buf);
146412
+ let acc = num;
146413
+ for(let i = 7; i >= 0; i--){
146414
+ if (acc === 0) break;
146415
+ arr[i] = acc & 255;
146416
+ acc -= arr[i];
146417
+ acc /= 256;
146418
+ }
146419
+ return arr;
147124
146420
  };
147125
146421
 
146422
+ function number(n) {
146423
+ if (!Number.isSafeInteger(n) || n < 0) throw new Error(`positive integer expected, not ${n}`);
146424
+ }
146425
+ // copied from utils
146426
+ function isBytes(a) {
146427
+ return a instanceof Uint8Array || a != null && typeof a === 'object' && a.constructor.name === 'Uint8Array';
146428
+ }
146429
+ function bytes(b, ...lengths) {
146430
+ if (!isBytes(b)) throw new Error('Uint8Array expected');
146431
+ if (lengths.length > 0 && !lengths.includes(b.length)) throw new Error(`Uint8Array expected of length ${lengths}, not of length=${b.length}`);
146432
+ }
146433
+ function hash(h) {
146434
+ if (typeof h !== 'function' || typeof h.create !== 'function') throw new Error('Hash should be wrapped by utils.wrapConstructor');
146435
+ number(h.outputLen);
146436
+ number(h.blockLen);
146437
+ }
146438
+ function exists(instance, checkFinished = true) {
146439
+ if (instance.destroyed) throw new Error('Hash instance has been destroyed');
146440
+ if (checkFinished && instance.finished) throw new Error('Hash#digest() has already been called');
146441
+ }
146442
+ function output(out, instance) {
146443
+ bytes(out);
146444
+ const min = instance.outputLen;
146445
+ if (out.length < min) {
146446
+ throw new Error(`digestInto() expects output buffer of length at least ${min}`);
146447
+ }
146448
+ }
146449
+
146450
+ /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */ // We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
146451
+ // node.js versions earlier than v19 don't declare it in global scope.
146452
+ // For node.js, package.json#exports field mapping rewrites import
146453
+ // from `crypto` to `cryptoNode`, which imports native module.
146454
+ // Makes the utils un-importable in browsers without a bundler.
146455
+ // Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
146456
+ const u32 = (arr)=>new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
146457
+ // Cast array to view
146458
+ const createView = (arr)=>new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
146459
+ // The rotate right (circular right shift) operation for uint32
146460
+ const rotr = (word, shift)=>word << 32 - shift | word >>> shift;
146461
+ // The rotate left (circular left shift) operation for uint32
146462
+ const rotl = (word, shift)=>word << shift | word >>> 32 - shift >>> 0;
146463
+ const isLE = new Uint8Array(new Uint32Array([
146464
+ 0x11223344
146465
+ ]).buffer)[0] === 0x44;
146466
+ // The byte swap operation for uint32
146467
+ const byteSwap = (word)=>word << 24 & 0xff000000 | word << 8 & 0xff0000 | word >>> 8 & 0xff00 | word >>> 24 & 0xff;
146468
+ // In place byte swap for Uint32Array
146469
+ function byteSwap32(arr) {
146470
+ for(let i = 0; i < arr.length; i++){
146471
+ arr[i] = byteSwap(arr[i]);
146472
+ }
146473
+ }
147126
146474
  /**
147127
- * A JavaScript implementation of the SHA family of hashes - defined in FIPS PUB 180-4, FIPS PUB 202,
147128
- * and SP 800-185 - as well as the corresponding HMAC implementation as defined in FIPS PUB 198-1.
147129
- *
147130
- * Copyright 2008-2023 Brian Turek, 1998-2009 Paul Johnston & Contributors
147131
- * Distributed under the BSD License
147132
- * See http://caligatio.github.com/jsSHA/ for more information
147133
- */
147134
- const t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
147135
- n = "ARRAYBUFFER not supported by this environment",
147136
- e = "UINT8ARRAY not supported by this environment";
147137
- function r(t, n, e, r) {
147138
- let i, s, o;
147139
- const h = n || [0],
147140
- u = (e = e || 0) >>> 3,
147141
- w = -1 === r ? 3 : 0;
147142
- for (i = 0; i < t.length; i += 1) o = i + u, s = o >>> 2, h.length <= s && h.push(0), h[s] |= t[i] << 8 * (w + r * (o % 4));
147143
- return {
147144
- value: h,
147145
- binLen: 8 * t.length + e
147146
- };
146475
+ * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
146476
+ */ function utf8ToBytes(str) {
146477
+ if (typeof str !== 'string') throw new Error(`utf8ToBytes expected string, got ${typeof str}`);
146478
+ return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
147147
146479
  }
147148
- function i(i, s, o) {
147149
- switch (s) {
147150
- case "UTF8":
147151
- case "UTF16BE":
147152
- case "UTF16LE":
147153
- break;
147154
- default:
147155
- throw new Error("encoding must be UTF8, UTF16BE, or UTF16LE");
147156
- }
147157
- switch (i) {
147158
- case "HEX":
147159
- return function (t, n, e) {
147160
- return function (t, n, e, r) {
147161
- let i, s, o, h;
147162
- if (0 != t.length % 2) throw new Error("String of HEX type must be in byte increments");
147163
- const u = n || [0],
147164
- w = (e = e || 0) >>> 3,
147165
- c = -1 === r ? 3 : 0;
147166
- for (i = 0; i < t.length; i += 2) {
147167
- if (s = parseInt(t.substr(i, 2), 16), isNaN(s)) throw new Error("String of HEX type contains invalid characters");
147168
- for (h = (i >>> 1) + w, o = h >>> 2; u.length <= o;) u.push(0);
147169
- u[o] |= s << 8 * (c + r * (h % 4));
147170
- }
147171
- return {
147172
- value: u,
147173
- binLen: 4 * t.length + e
147174
- };
147175
- }(t, n, e, o);
147176
- };
147177
- case "TEXT":
147178
- return function (t, n, e) {
147179
- return function (t, n, e, r, i) {
147180
- let s,
147181
- o,
147182
- h,
147183
- u,
147184
- w,
147185
- c,
147186
- f,
147187
- a,
147188
- l = 0;
147189
- const A = e || [0],
147190
- E = (r = r || 0) >>> 3;
147191
- if ("UTF8" === n) for (f = -1 === i ? 3 : 0, h = 0; h < t.length; h += 1) for (s = t.charCodeAt(h), o = [], 128 > s ? o.push(s) : 2048 > s ? (o.push(192 | s >>> 6), o.push(128 | 63 & s)) : 55296 > s || 57344 <= s ? o.push(224 | s >>> 12, 128 | s >>> 6 & 63, 128 | 63 & s) : (h += 1, s = 65536 + ((1023 & s) << 10 | 1023 & t.charCodeAt(h)), o.push(240 | s >>> 18, 128 | s >>> 12 & 63, 128 | s >>> 6 & 63, 128 | 63 & s)), u = 0; u < o.length; u += 1) {
147192
- for (c = l + E, w = c >>> 2; A.length <= w;) A.push(0);
147193
- A[w] |= o[u] << 8 * (f + i * (c % 4)), l += 1;
147194
- } else for (f = -1 === i ? 2 : 0, a = "UTF16LE" === n && 1 !== i || "UTF16LE" !== n && 1 === i, h = 0; h < t.length; h += 1) {
147195
- for (s = t.charCodeAt(h), !0 === a && (u = 255 & s, s = u << 8 | s >>> 8), c = l + E, w = c >>> 2; A.length <= w;) A.push(0);
147196
- A[w] |= s << 8 * (f + i * (c % 4)), l += 2;
147197
- }
147198
- return {
147199
- value: A,
147200
- binLen: 8 * l + r
147201
- };
147202
- }(t, s, n, e, o);
147203
- };
147204
- case "B64":
147205
- return function (n, e, r) {
147206
- return function (n, e, r, i) {
147207
- let s,
147208
- o,
147209
- h,
147210
- u,
147211
- w,
147212
- c,
147213
- f,
147214
- a = 0;
147215
- const l = e || [0],
147216
- A = (r = r || 0) >>> 3,
147217
- E = -1 === i ? 3 : 0,
147218
- H = n.indexOf("=");
147219
- if (-1 === n.search(/^[a-zA-Z0-9=+/]+$/)) throw new Error("Invalid character in base-64 string");
147220
- if (n = n.replace(/=/g, ""), -1 !== H && H < n.length) throw new Error("Invalid '=' found in base-64 string");
147221
- for (o = 0; o < n.length; o += 4) {
147222
- for (w = n.substr(o, 4), u = 0, h = 0; h < w.length; h += 1) s = t.indexOf(w.charAt(h)), u |= s << 18 - 6 * h;
147223
- for (h = 0; h < w.length - 1; h += 1) {
147224
- for (f = a + A, c = f >>> 2; l.length <= c;) l.push(0);
147225
- l[c] |= (u >>> 16 - 8 * h & 255) << 8 * (E + i * (f % 4)), a += 1;
147226
- }
147227
- }
147228
- return {
147229
- value: l,
147230
- binLen: 8 * a + r
147231
- };
147232
- }(n, e, r, o);
147233
- };
147234
- case "BYTES":
147235
- return function (t, n, e) {
147236
- return function (t, n, e, r) {
147237
- let i, s, o, h;
147238
- const u = n || [0],
147239
- w = (e = e || 0) >>> 3,
147240
- c = -1 === r ? 3 : 0;
147241
- for (s = 0; s < t.length; s += 1) i = t.charCodeAt(s), h = s + w, o = h >>> 2, u.length <= o && u.push(0), u[o] |= i << 8 * (c + r * (h % 4));
147242
- return {
147243
- value: u,
147244
- binLen: 8 * t.length + e
147245
- };
147246
- }(t, n, e, o);
147247
- };
147248
- case "ARRAYBUFFER":
147249
- try {
147250
- new ArrayBuffer(0);
147251
- } catch (t) {
147252
- throw new Error(n);
147253
- }
147254
- return function (t, n, e) {
147255
- return function (t, n, e, i) {
147256
- return r(new Uint8Array(t), n, e, i);
147257
- }(t, n, e, o);
147258
- };
147259
- case "UINT8ARRAY":
147260
- try {
147261
- new Uint8Array(0);
147262
- } catch (t) {
147263
- throw new Error(e);
147264
- }
147265
- return function (t, n, e) {
147266
- return r(t, n, e, o);
147267
- };
147268
- default:
147269
- throw new Error("format must be HEX, TEXT, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY");
147270
- }
147271
- }
147272
- function s(r, i, s, o) {
147273
- switch (r) {
147274
- case "HEX":
147275
- return function (t) {
147276
- return function (t, n, e, r) {
147277
- const i = "0123456789abcdef";
147278
- let s,
147279
- o,
147280
- h = "";
147281
- const u = n / 8,
147282
- w = -1 === e ? 3 : 0;
147283
- for (s = 0; s < u; s += 1) o = t[s >>> 2] >>> 8 * (w + e * (s % 4)), h += i.charAt(o >>> 4 & 15) + i.charAt(15 & o);
147284
- return r.outputUpper ? h.toUpperCase() : h;
147285
- }(t, i, s, o);
147286
- };
147287
- case "B64":
147288
- return function (n) {
147289
- return function (n, e, r, i) {
147290
- let s,
147291
- o,
147292
- h,
147293
- u,
147294
- w,
147295
- c = "";
147296
- const f = e / 8,
147297
- a = -1 === r ? 3 : 0;
147298
- for (s = 0; s < f; s += 3) for (u = s + 1 < f ? n[s + 1 >>> 2] : 0, w = s + 2 < f ? n[s + 2 >>> 2] : 0, h = (n[s >>> 2] >>> 8 * (a + r * (s % 4)) & 255) << 16 | (u >>> 8 * (a + r * ((s + 1) % 4)) & 255) << 8 | w >>> 8 * (a + r * ((s + 2) % 4)) & 255, o = 0; o < 4; o += 1) c += 8 * s + 6 * o <= e ? t.charAt(h >>> 6 * (3 - o) & 63) : i.b64Pad;
147299
- return c;
147300
- }(n, i, s, o);
147301
- };
147302
- case "BYTES":
147303
- return function (t) {
147304
- return function (t, n, e) {
147305
- let r,
147306
- i,
147307
- s = "";
147308
- const o = n / 8,
147309
- h = -1 === e ? 3 : 0;
147310
- for (r = 0; r < o; r += 1) i = t[r >>> 2] >>> 8 * (h + e * (r % 4)) & 255, s += String.fromCharCode(i);
147311
- return s;
147312
- }(t, i, s);
147313
- };
147314
- case "ARRAYBUFFER":
147315
- try {
147316
- new ArrayBuffer(0);
147317
- } catch (t) {
147318
- throw new Error(n);
147319
- }
147320
- return function (t) {
147321
- return function (t, n, e) {
147322
- let r;
147323
- const i = n / 8,
147324
- s = new ArrayBuffer(i),
147325
- o = new Uint8Array(s),
147326
- h = -1 === e ? 3 : 0;
147327
- for (r = 0; r < i; r += 1) o[r] = t[r >>> 2] >>> 8 * (h + e * (r % 4)) & 255;
147328
- return s;
147329
- }(t, i, s);
147330
- };
147331
- case "UINT8ARRAY":
147332
- try {
147333
- new Uint8Array(0);
147334
- } catch (t) {
147335
- throw new Error(e);
147336
- }
147337
- return function (t) {
147338
- return function (t, n, e) {
147339
- let r;
147340
- const i = n / 8,
147341
- s = -1 === e ? 3 : 0,
147342
- o = new Uint8Array(i);
147343
- for (r = 0; r < i; r += 1) o[r] = t[r >>> 2] >>> 8 * (s + e * (r % 4)) & 255;
147344
- return o;
147345
- }(t, i, s);
147346
- };
147347
- default:
147348
- throw new Error("format must be HEX, B64, BYTES, ARRAYBUFFER, or UINT8ARRAY");
147349
- }
147350
- }
147351
- const o = 4294967296,
147352
- h = [1116352408, 1899447441, 3049323471, 3921009573, 961987163, 1508970993, 2453635748, 2870763221, 3624381080, 310598401, 607225278, 1426881987, 1925078388, 2162078206, 2614888103, 3248222580, 3835390401, 4022224774, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, 2554220882, 2821834349, 2952996808, 3210313671, 3336571891, 3584528711, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, 2177026350, 2456956037, 2730485921, 2820302411, 3259730800, 3345764771, 3516065817, 3600352804, 4094571909, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, 2227730452, 2361852424, 2428436474, 2756734187, 3204031479, 3329325298],
147353
- u = [3238371032, 914150663, 812702999, 4144912697, 4290775857, 1750603025, 1694076839, 3204075428],
147354
- w = [1779033703, 3144134277, 1013904242, 2773480762, 1359893119, 2600822924, 528734635, 1541459225],
147355
- c = "Chosen SHA variant is not supported",
147356
- f = "Cannot set numRounds with MAC";
147357
- function a(t, n) {
147358
- let e, r;
147359
- const i = t.binLen >>> 3,
147360
- s = n.binLen >>> 3,
147361
- o = i << 3,
147362
- h = 4 - i << 3;
147363
- if (i % 4 != 0) {
147364
- for (e = 0; e < s; e += 4) r = i + e >>> 2, t.value[r] |= n.value[e >>> 2] << o, t.value.push(0), t.value[r + 1] |= n.value[e >>> 2] >>> h;
147365
- return (t.value.length << 2) - 4 >= s + i && t.value.pop(), {
147366
- value: t.value,
147367
- binLen: t.binLen + n.binLen
147368
- };
147369
- }
147370
- return {
147371
- value: t.value.concat(n.value),
147372
- binLen: t.binLen + n.binLen
147373
- };
146480
+ /**
146481
+ * Normalizes (non-hex) string or Uint8Array to Uint8Array.
146482
+ * Warning: when Uint8Array is passed, it would NOT get copied.
146483
+ * Keep in mind for future mutable operations.
146484
+ */ function toBytes(data) {
146485
+ if (typeof data === 'string') data = utf8ToBytes(data);
146486
+ bytes(data);
146487
+ return data;
146488
+ }
146489
+ // For runtime check if class implements interface
146490
+ class Hash {
146491
+ // Safe version that clones internal state
146492
+ clone() {
146493
+ return this._cloneInto();
146494
+ }
146495
+ }
146496
+ function wrapConstructor(hashCons) {
146497
+ const hashC = (msg)=>hashCons().update(toBytes(msg)).digest();
146498
+ const tmp = hashCons();
146499
+ hashC.outputLen = tmp.outputLen;
146500
+ hashC.blockLen = tmp.blockLen;
146501
+ hashC.create = ()=>hashCons();
146502
+ return hashC;
146503
+ }
146504
+
146505
+ // HMAC (RFC 2104)
146506
+ class HMAC extends Hash {
146507
+ update(buf) {
146508
+ exists(this);
146509
+ this.iHash.update(buf);
146510
+ return this;
146511
+ }
146512
+ digestInto(out) {
146513
+ exists(this);
146514
+ bytes(out, this.outputLen);
146515
+ this.finished = true;
146516
+ this.iHash.digestInto(out);
146517
+ this.oHash.update(out);
146518
+ this.oHash.digestInto(out);
146519
+ this.destroy();
146520
+ }
146521
+ digest() {
146522
+ const out = new Uint8Array(this.oHash.outputLen);
146523
+ this.digestInto(out);
146524
+ return out;
146525
+ }
146526
+ _cloneInto(to) {
146527
+ // Create new instance without calling constructor since key already in state and we don't know it.
146528
+ to || (to = Object.create(Object.getPrototypeOf(this), {}));
146529
+ const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
146530
+ to = to;
146531
+ to.finished = finished;
146532
+ to.destroyed = destroyed;
146533
+ to.blockLen = blockLen;
146534
+ to.outputLen = outputLen;
146535
+ to.oHash = oHash._cloneInto(to.oHash);
146536
+ to.iHash = iHash._cloneInto(to.iHash);
146537
+ return to;
146538
+ }
146539
+ destroy() {
146540
+ this.destroyed = true;
146541
+ this.oHash.destroy();
146542
+ this.iHash.destroy();
146543
+ }
146544
+ constructor(hash$1, _key){
146545
+ super();
146546
+ this.finished = false;
146547
+ this.destroyed = false;
146548
+ hash(hash$1);
146549
+ const key = toBytes(_key);
146550
+ this.iHash = hash$1.create();
146551
+ if (typeof this.iHash.update !== 'function') throw new Error('Expected instance of class which extends utils.Hash');
146552
+ this.blockLen = this.iHash.blockLen;
146553
+ this.outputLen = this.iHash.outputLen;
146554
+ const blockLen = this.blockLen;
146555
+ const pad = new Uint8Array(blockLen);
146556
+ // blockLen can be bigger than outputLen
146557
+ pad.set(key.length > blockLen ? hash$1.create().update(key).digest() : key);
146558
+ for(let i = 0; i < pad.length; i++)pad[i] ^= 0x36;
146559
+ this.iHash.update(pad);
146560
+ // By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
146561
+ this.oHash = hash$1.create();
146562
+ // Undo internal XOR && apply outer XOR
146563
+ for(let i = 0; i < pad.length; i++)pad[i] ^= 0x36 ^ 0x5c;
146564
+ this.oHash.update(pad);
146565
+ pad.fill(0);
146566
+ }
147374
146567
  }
147375
- function l(t) {
147376
- const n = {
147377
- outputUpper: !1,
147378
- b64Pad: "=",
147379
- outputLen: -1
147380
- },
147381
- e = t || {},
147382
- r = "Output length must be a multiple of 8";
147383
- if (n.outputUpper = e.outputUpper || !1, e.b64Pad && (n.b64Pad = e.b64Pad), e.outputLen) {
147384
- if (e.outputLen % 8 != 0) throw new Error(r);
147385
- n.outputLen = e.outputLen;
147386
- } else if (e.shakeLen) {
147387
- if (e.shakeLen % 8 != 0) throw new Error(r);
147388
- n.outputLen = e.shakeLen;
147389
- }
147390
- if ("boolean" != typeof n.outputUpper) throw new Error("Invalid outputUpper formatting option");
147391
- if ("string" != typeof n.b64Pad) throw new Error("Invalid b64Pad formatting option");
147392
- return n;
146568
+ /**
146569
+ * HMAC: RFC2104 message authentication code.
146570
+ * @param hash - function that would be used e.g. sha256
146571
+ * @param key - message key
146572
+ * @param message - message data
146573
+ */ const hmac = (hash, key, message)=>new HMAC(hash, key).update(message).digest();
146574
+ hmac.create = (hash, key)=>new HMAC(hash, key);
146575
+
146576
+ // Polyfill for Safari 14
146577
+ function setBigUint64(view, byteOffset, value, isLE) {
146578
+ if (typeof view.setBigUint64 === 'function') return view.setBigUint64(byteOffset, value, isLE);
146579
+ const _32n = BigInt(32);
146580
+ const _u32_max = BigInt(0xffffffff);
146581
+ const wh = Number(value >> _32n & _u32_max);
146582
+ const wl = Number(value & _u32_max);
146583
+ const h = isLE ? 4 : 0;
146584
+ const l = isLE ? 0 : 4;
146585
+ view.setUint32(byteOffset + h, wh, isLE);
146586
+ view.setUint32(byteOffset + l, wl, isLE);
146587
+ }
146588
+ // Choice: a ? b : c
146589
+ const Chi = (a, b, c)=>a & b ^ ~a & c;
146590
+ // Majority function, true if any two inpust is true
146591
+ const Maj = (a, b, c)=>a & b ^ a & c ^ b & c;
146592
+ /**
146593
+ * Merkle-Damgard hash construction base class.
146594
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
146595
+ */ class HashMD extends Hash {
146596
+ update(data) {
146597
+ exists(this);
146598
+ const { view, buffer, blockLen } = this;
146599
+ data = toBytes(data);
146600
+ const len = data.length;
146601
+ for(let pos = 0; pos < len;){
146602
+ const take = Math.min(blockLen - this.pos, len - pos);
146603
+ // Fast path: we have at least one block in input, cast it to view and process
146604
+ if (take === blockLen) {
146605
+ const dataView = createView(data);
146606
+ for(; blockLen <= len - pos; pos += blockLen)this.process(dataView, pos);
146607
+ continue;
146608
+ }
146609
+ buffer.set(data.subarray(pos, pos + take), this.pos);
146610
+ this.pos += take;
146611
+ pos += take;
146612
+ if (this.pos === blockLen) {
146613
+ this.process(view, 0);
146614
+ this.pos = 0;
146615
+ }
146616
+ }
146617
+ this.length += data.length;
146618
+ this.roundClean();
146619
+ return this;
146620
+ }
146621
+ digestInto(out) {
146622
+ exists(this);
146623
+ output(out, this);
146624
+ this.finished = true;
146625
+ // Padding
146626
+ // We can avoid allocation of buffer for padding completely if it
146627
+ // was previously not allocated here. But it won't change performance.
146628
+ const { buffer, view, blockLen, isLE } = this;
146629
+ let { pos } = this;
146630
+ // append the bit '1' to the message
146631
+ buffer[pos++] = 0b10000000;
146632
+ this.buffer.subarray(pos).fill(0);
146633
+ // we have less than padOffset left in buffer, so we cannot put length in
146634
+ // current block, need process it and pad again
146635
+ if (this.padOffset > blockLen - pos) {
146636
+ this.process(view, 0);
146637
+ pos = 0;
146638
+ }
146639
+ // Pad until full block byte with zeros
146640
+ for(let i = pos; i < blockLen; i++)buffer[i] = 0;
146641
+ // Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
146642
+ // You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
146643
+ // So we just write lowest 64 bits of that value.
146644
+ setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
146645
+ this.process(view, 0);
146646
+ const oview = createView(out);
146647
+ const len = this.outputLen;
146648
+ // NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
146649
+ if (len % 4) throw new Error('_sha2: outputLen should be aligned to 32bit');
146650
+ const outLen = len / 4;
146651
+ const state = this.get();
146652
+ if (outLen > state.length) throw new Error('_sha2: outputLen bigger than state');
146653
+ for(let i = 0; i < outLen; i++)oview.setUint32(4 * i, state[i], isLE);
146654
+ }
146655
+ digest() {
146656
+ const { buffer, outputLen } = this;
146657
+ this.digestInto(buffer);
146658
+ const res = buffer.slice(0, outputLen);
146659
+ this.destroy();
146660
+ return res;
146661
+ }
146662
+ _cloneInto(to) {
146663
+ to || (to = new this.constructor());
146664
+ to.set(...this.get());
146665
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
146666
+ to.length = length;
146667
+ to.pos = pos;
146668
+ to.finished = finished;
146669
+ to.destroyed = destroyed;
146670
+ if (length % blockLen) to.buffer.set(buffer);
146671
+ return to;
146672
+ }
146673
+ constructor(blockLen, outputLen, padOffset, isLE){
146674
+ super();
146675
+ this.blockLen = blockLen;
146676
+ this.outputLen = outputLen;
146677
+ this.padOffset = padOffset;
146678
+ this.isLE = isLE;
146679
+ this.finished = false;
146680
+ this.length = 0;
146681
+ this.pos = 0;
146682
+ this.destroyed = false;
146683
+ this.buffer = new Uint8Array(blockLen);
146684
+ this.view = createView(this.buffer);
146685
+ }
146686
+ }
146687
+
146688
+ // SHA1 (RFC 3174) was cryptographically broken. It's still used. Don't use it for a new protocol.
146689
+ // Initial state
146690
+ const SHA1_IV = /* @__PURE__ */ new Uint32Array([
146691
+ 0x67452301,
146692
+ 0xefcdab89,
146693
+ 0x98badcfe,
146694
+ 0x10325476,
146695
+ 0xc3d2e1f0
146696
+ ]);
146697
+ // Temporary buffer, not used to store anything between runs
146698
+ // Named this way because it matches specification.
146699
+ const SHA1_W = /* @__PURE__ */ new Uint32Array(80);
146700
+ class SHA1 extends HashMD {
146701
+ get() {
146702
+ const { A, B, C, D, E } = this;
146703
+ return [
146704
+ A,
146705
+ B,
146706
+ C,
146707
+ D,
146708
+ E
146709
+ ];
146710
+ }
146711
+ set(A, B, C, D, E) {
146712
+ this.A = A | 0;
146713
+ this.B = B | 0;
146714
+ this.C = C | 0;
146715
+ this.D = D | 0;
146716
+ this.E = E | 0;
146717
+ }
146718
+ process(view, offset) {
146719
+ for(let i = 0; i < 16; i++, offset += 4)SHA1_W[i] = view.getUint32(offset, false);
146720
+ for(let i = 16; i < 80; i++)SHA1_W[i] = rotl(SHA1_W[i - 3] ^ SHA1_W[i - 8] ^ SHA1_W[i - 14] ^ SHA1_W[i - 16], 1);
146721
+ // Compression function main loop, 80 rounds
146722
+ let { A, B, C, D, E } = this;
146723
+ for(let i = 0; i < 80; i++){
146724
+ let F, K;
146725
+ if (i < 20) {
146726
+ F = Chi(B, C, D);
146727
+ K = 0x5a827999;
146728
+ } else if (i < 40) {
146729
+ F = B ^ C ^ D;
146730
+ K = 0x6ed9eba1;
146731
+ } else if (i < 60) {
146732
+ F = Maj(B, C, D);
146733
+ K = 0x8f1bbcdc;
146734
+ } else {
146735
+ F = B ^ C ^ D;
146736
+ K = 0xca62c1d6;
146737
+ }
146738
+ const T = rotl(A, 5) + F + E + K + SHA1_W[i] | 0;
146739
+ E = D;
146740
+ D = C;
146741
+ C = rotl(B, 30);
146742
+ B = A;
146743
+ A = T;
146744
+ }
146745
+ // Add the compressed chunk to the current hash value
146746
+ A = A + this.A | 0;
146747
+ B = B + this.B | 0;
146748
+ C = C + this.C | 0;
146749
+ D = D + this.D | 0;
146750
+ E = E + this.E | 0;
146751
+ this.set(A, B, C, D, E);
146752
+ }
146753
+ roundClean() {
146754
+ SHA1_W.fill(0);
146755
+ }
146756
+ destroy() {
146757
+ this.set(0, 0, 0, 0, 0);
146758
+ this.buffer.fill(0);
146759
+ }
146760
+ constructor(){
146761
+ super(64, 20, 8, false);
146762
+ this.A = SHA1_IV[0] | 0;
146763
+ this.B = SHA1_IV[1] | 0;
146764
+ this.C = SHA1_IV[2] | 0;
146765
+ this.D = SHA1_IV[3] | 0;
146766
+ this.E = SHA1_IV[4] | 0;
146767
+ }
146768
+ }
146769
+ const sha1 = /* @__PURE__ */ wrapConstructor(()=>new SHA1());
146770
+
146771
+ // SHA2-256 need to try 2^128 hashes to execute birthday attack.
146772
+ // BTC network is doing 2^67 hashes/sec as per early 2023.
146773
+ // Round constants:
146774
+ // first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311)
146775
+ // prettier-ignore
146776
+ const SHA256_K = /* @__PURE__ */ new Uint32Array([
146777
+ 0x428a2f98,
146778
+ 0x71374491,
146779
+ 0xb5c0fbcf,
146780
+ 0xe9b5dba5,
146781
+ 0x3956c25b,
146782
+ 0x59f111f1,
146783
+ 0x923f82a4,
146784
+ 0xab1c5ed5,
146785
+ 0xd807aa98,
146786
+ 0x12835b01,
146787
+ 0x243185be,
146788
+ 0x550c7dc3,
146789
+ 0x72be5d74,
146790
+ 0x80deb1fe,
146791
+ 0x9bdc06a7,
146792
+ 0xc19bf174,
146793
+ 0xe49b69c1,
146794
+ 0xefbe4786,
146795
+ 0x0fc19dc6,
146796
+ 0x240ca1cc,
146797
+ 0x2de92c6f,
146798
+ 0x4a7484aa,
146799
+ 0x5cb0a9dc,
146800
+ 0x76f988da,
146801
+ 0x983e5152,
146802
+ 0xa831c66d,
146803
+ 0xb00327c8,
146804
+ 0xbf597fc7,
146805
+ 0xc6e00bf3,
146806
+ 0xd5a79147,
146807
+ 0x06ca6351,
146808
+ 0x14292967,
146809
+ 0x27b70a85,
146810
+ 0x2e1b2138,
146811
+ 0x4d2c6dfc,
146812
+ 0x53380d13,
146813
+ 0x650a7354,
146814
+ 0x766a0abb,
146815
+ 0x81c2c92e,
146816
+ 0x92722c85,
146817
+ 0xa2bfe8a1,
146818
+ 0xa81a664b,
146819
+ 0xc24b8b70,
146820
+ 0xc76c51a3,
146821
+ 0xd192e819,
146822
+ 0xd6990624,
146823
+ 0xf40e3585,
146824
+ 0x106aa070,
146825
+ 0x19a4c116,
146826
+ 0x1e376c08,
146827
+ 0x2748774c,
146828
+ 0x34b0bcb5,
146829
+ 0x391c0cb3,
146830
+ 0x4ed8aa4a,
146831
+ 0x5b9cca4f,
146832
+ 0x682e6ff3,
146833
+ 0x748f82ee,
146834
+ 0x78a5636f,
146835
+ 0x84c87814,
146836
+ 0x8cc70208,
146837
+ 0x90befffa,
146838
+ 0xa4506ceb,
146839
+ 0xbef9a3f7,
146840
+ 0xc67178f2
146841
+ ]);
146842
+ // Initial state:
146843
+ // first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
146844
+ // prettier-ignore
146845
+ const SHA256_IV = /* @__PURE__ */ new Uint32Array([
146846
+ 0x6a09e667,
146847
+ 0xbb67ae85,
146848
+ 0x3c6ef372,
146849
+ 0xa54ff53a,
146850
+ 0x510e527f,
146851
+ 0x9b05688c,
146852
+ 0x1f83d9ab,
146853
+ 0x5be0cd19
146854
+ ]);
146855
+ // Temporary buffer, not used to store anything between runs
146856
+ // Named this way because it matches specification.
146857
+ const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
146858
+ class SHA256 extends HashMD {
146859
+ get() {
146860
+ const { A, B, C, D, E, F, G, H } = this;
146861
+ return [
146862
+ A,
146863
+ B,
146864
+ C,
146865
+ D,
146866
+ E,
146867
+ F,
146868
+ G,
146869
+ H
146870
+ ];
146871
+ }
146872
+ // prettier-ignore
146873
+ set(A, B, C, D, E, F, G, H) {
146874
+ this.A = A | 0;
146875
+ this.B = B | 0;
146876
+ this.C = C | 0;
146877
+ this.D = D | 0;
146878
+ this.E = E | 0;
146879
+ this.F = F | 0;
146880
+ this.G = G | 0;
146881
+ this.H = H | 0;
146882
+ }
146883
+ process(view, offset) {
146884
+ // Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
146885
+ for(let i = 0; i < 16; i++, offset += 4)SHA256_W[i] = view.getUint32(offset, false);
146886
+ for(let i = 16; i < 64; i++){
146887
+ const W15 = SHA256_W[i - 15];
146888
+ const W2 = SHA256_W[i - 2];
146889
+ const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ W15 >>> 3;
146890
+ const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ W2 >>> 10;
146891
+ SHA256_W[i] = s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16] | 0;
146892
+ }
146893
+ // Compression function main loop, 64 rounds
146894
+ let { A, B, C, D, E, F, G, H } = this;
146895
+ for(let i = 0; i < 64; i++){
146896
+ const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
146897
+ const T1 = H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i] | 0;
146898
+ const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
146899
+ const T2 = sigma0 + Maj(A, B, C) | 0;
146900
+ H = G;
146901
+ G = F;
146902
+ F = E;
146903
+ E = D + T1 | 0;
146904
+ D = C;
146905
+ C = B;
146906
+ B = A;
146907
+ A = T1 + T2 | 0;
146908
+ }
146909
+ // Add the compressed chunk to the current hash value
146910
+ A = A + this.A | 0;
146911
+ B = B + this.B | 0;
146912
+ C = C + this.C | 0;
146913
+ D = D + this.D | 0;
146914
+ E = E + this.E | 0;
146915
+ F = F + this.F | 0;
146916
+ G = G + this.G | 0;
146917
+ H = H + this.H | 0;
146918
+ this.set(A, B, C, D, E, F, G, H);
146919
+ }
146920
+ roundClean() {
146921
+ SHA256_W.fill(0);
146922
+ }
146923
+ destroy() {
146924
+ this.set(0, 0, 0, 0, 0, 0, 0, 0);
146925
+ this.buffer.fill(0);
146926
+ }
146927
+ constructor(){
146928
+ super(64, 32, 8, false);
146929
+ // We cannot use array here since array allows indexing by variable
146930
+ // which means optimizer/compiler cannot use registers.
146931
+ this.A = SHA256_IV[0] | 0;
146932
+ this.B = SHA256_IV[1] | 0;
146933
+ this.C = SHA256_IV[2] | 0;
146934
+ this.D = SHA256_IV[3] | 0;
146935
+ this.E = SHA256_IV[4] | 0;
146936
+ this.F = SHA256_IV[5] | 0;
146937
+ this.G = SHA256_IV[6] | 0;
146938
+ this.H = SHA256_IV[7] | 0;
146939
+ }
146940
+ }
146941
+ // Constants from https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
146942
+ class SHA224 extends SHA256 {
146943
+ constructor(){
146944
+ super();
146945
+ this.A = 0xc1059ed8 | 0;
146946
+ this.B = 0x367cd507 | 0;
146947
+ this.C = 0x3070dd17 | 0;
146948
+ this.D = 0xf70e5939 | 0;
146949
+ this.E = 0xffc00b31 | 0;
146950
+ this.F = 0x68581511 | 0;
146951
+ this.G = 0x64f98fa7 | 0;
146952
+ this.H = 0xbefa4fa4 | 0;
146953
+ this.outputLen = 28;
146954
+ }
147393
146955
  }
147394
- function A(t, n, e, r) {
147395
- const s = t + " must include a value and format";
147396
- if (!n) {
147397
- if (!r) throw new Error(s);
147398
- return r;
147399
- }
147400
- if (void 0 === n.value || !n.format) throw new Error(s);
147401
- return i(n.format, n.encoding || "UTF8", e)(n.value);
146956
+ /**
146957
+ * SHA2-256 hash function
146958
+ * @param message - data that would be hashed
146959
+ */ const sha256 = /* @__PURE__ */ wrapConstructor(()=>new SHA256());
146960
+ const sha224 = /* @__PURE__ */ wrapConstructor(()=>new SHA224());
146961
+
146962
+ const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
146963
+ const _32n = /* @__PURE__ */ BigInt(32);
146964
+ // We are not using BigUint64Array, because they are extremely slow as per 2022
146965
+ function fromBig(n, le = false) {
146966
+ if (le) return {
146967
+ h: Number(n & U32_MASK64),
146968
+ l: Number(n >> _32n & U32_MASK64)
146969
+ };
146970
+ return {
146971
+ h: Number(n >> _32n & U32_MASK64) | 0,
146972
+ l: Number(n & U32_MASK64) | 0
146973
+ };
147402
146974
  }
147403
- class E {
147404
- constructor(t, n, e) {
147405
- const r = e || {};
147406
- if (this.t = n, this.i = r.encoding || "UTF8", this.numRounds = r.numRounds || 1, isNaN(this.numRounds) || this.numRounds !== parseInt(this.numRounds, 10) || 1 > this.numRounds) throw new Error("numRounds must a integer >= 1");
147407
- this.o = t, this.h = [], this.u = 0, this.l = !1, this.A = 0, this.H = !1, this.S = [], this.p = [];
147408
- }
147409
- update(t) {
147410
- let n,
147411
- e = 0;
147412
- const r = this.m >>> 5,
147413
- i = this.C(t, this.h, this.u),
147414
- s = i.binLen,
147415
- o = i.value,
147416
- h = s >>> 5;
147417
- for (n = 0; n < h; n += r) e + this.m <= s && (this.U = this.v(o.slice(n, n + r), this.U), e += this.m);
147418
- return this.A += e, this.h = o.slice(e >>> 5), this.u = s % this.m, this.l = !0, this;
147419
- }
147420
- getHash(t, n) {
147421
- let e,
147422
- r,
147423
- i = this.R;
147424
- const o = l(n);
147425
- if (this.K) {
147426
- if (-1 === o.outputLen) throw new Error("Output length must be specified in options");
147427
- i = o.outputLen;
147428
- }
147429
- const h = s(t, i, this.T, o);
147430
- if (this.H && this.g) return h(this.g(o));
147431
- for (r = this.F(this.h.slice(), this.u, this.A, this.L(this.U), i), e = 1; e < this.numRounds; e += 1) this.K && i % 32 != 0 && (r[r.length - 1] &= 16777215 >>> 24 - i % 32), r = this.F(r, i, 0, this.B(this.o), i);
147432
- return h(r);
147433
- }
147434
- setHMACKey(t, n, e) {
147435
- if (!this.M) throw new Error("Variant does not support HMAC");
147436
- if (this.l) throw new Error("Cannot set MAC key after calling update");
147437
- const r = i(n, (e || {}).encoding || "UTF8", this.T);
147438
- this.k(r(t));
147439
- }
147440
- k(t) {
147441
- const n = this.m >>> 3,
147442
- e = n / 4 - 1;
147443
- let r;
147444
- if (1 !== this.numRounds) throw new Error(f);
147445
- if (this.H) throw new Error("MAC key already set");
147446
- for (n < t.binLen / 8 && (t.value = this.F(t.value, t.binLen, 0, this.B(this.o), this.R)); t.value.length <= e;) t.value.push(0);
147447
- for (r = 0; r <= e; r += 1) this.S[r] = 909522486 ^ t.value[r], this.p[r] = 1549556828 ^ t.value[r];
147448
- this.U = this.v(this.S, this.U), this.A = this.m, this.H = !0;
147449
- }
147450
- getHMAC(t, n) {
147451
- const e = l(n);
147452
- return s(t, this.R, this.T, e)(this.Y());
147453
- }
147454
- Y() {
147455
- let t;
147456
- if (!this.H) throw new Error("Cannot call getHMAC without first setting MAC key");
147457
- const n = this.F(this.h.slice(), this.u, this.A, this.L(this.U), this.R);
147458
- return t = this.v(this.p, this.B(this.o)), t = this.F(n, this.R, this.m, t, this.R), t;
147459
- }
147460
- }
147461
- function H(t, n) {
147462
- return t << n | t >>> 32 - n;
147463
- }
147464
- function S(t, n) {
147465
- return t >>> n | t << 32 - n;
147466
- }
147467
- function b(t, n) {
147468
- return t >>> n;
147469
- }
147470
- function p(t, n, e) {
147471
- return t ^ n ^ e;
147472
- }
147473
- function d(t, n, e) {
147474
- return t & n ^ ~t & e;
147475
- }
147476
- function m(t, n, e) {
147477
- return t & n ^ t & e ^ n & e;
147478
- }
147479
- function C(t) {
147480
- return S(t, 2) ^ S(t, 13) ^ S(t, 22);
147481
- }
147482
- function y(t, n) {
147483
- const e = (65535 & t) + (65535 & n);
147484
- return (65535 & (t >>> 16) + (n >>> 16) + (e >>> 16)) << 16 | 65535 & e;
147485
- }
147486
- function U(t, n, e, r) {
147487
- const i = (65535 & t) + (65535 & n) + (65535 & e) + (65535 & r);
147488
- return (65535 & (t >>> 16) + (n >>> 16) + (e >>> 16) + (r >>> 16) + (i >>> 16)) << 16 | 65535 & i;
147489
- }
147490
- function v(t, n, e, r, i) {
147491
- const s = (65535 & t) + (65535 & n) + (65535 & e) + (65535 & r) + (65535 & i);
147492
- return (65535 & (t >>> 16) + (n >>> 16) + (e >>> 16) + (r >>> 16) + (i >>> 16) + (s >>> 16)) << 16 | 65535 & s;
147493
- }
147494
- function R(t) {
147495
- return S(t, 7) ^ S(t, 18) ^ b(t, 3);
147496
- }
147497
- function K(t) {
147498
- return S(t, 6) ^ S(t, 11) ^ S(t, 25);
147499
- }
147500
- function T(t) {
147501
- return [1732584193, 4023233417, 2562383102, 271733878, 3285377520];
147502
- }
147503
- function g(t, n) {
147504
- let e, r, i, s, o, h, u;
147505
- const w = [];
147506
- for (e = n[0], r = n[1], i = n[2], s = n[3], o = n[4], u = 0; u < 80; u += 1) w[u] = u < 16 ? t[u] : H(w[u - 3] ^ w[u - 8] ^ w[u - 14] ^ w[u - 16], 1), h = u < 20 ? v(H(e, 5), d(r, i, s), o, 1518500249, w[u]) : u < 40 ? v(H(e, 5), p(r, i, s), o, 1859775393, w[u]) : u < 60 ? v(H(e, 5), m(r, i, s), o, 2400959708, w[u]) : v(H(e, 5), p(r, i, s), o, 3395469782, w[u]), o = s, s = i, i = H(r, 30), r = e, e = h;
147507
- return n[0] = y(e, n[0]), n[1] = y(r, n[1]), n[2] = y(i, n[2]), n[3] = y(s, n[3]), n[4] = y(o, n[4]), n;
147508
- }
147509
- function F(t, n, e, r) {
147510
- let i;
147511
- const s = 15 + (n + 65 >>> 9 << 4),
147512
- h = n + e;
147513
- for (; t.length <= s;) t.push(0);
147514
- for (t[n >>> 5] |= 128 << 24 - n % 32, t[s] = 4294967295 & h, t[s - 1] = h / o | 0, i = 0; i < t.length; i += 16) r = g(t.slice(i, i + 16), r);
147515
- return r;
147516
- }
147517
- let L = class extends E {
147518
- constructor(t, n, e) {
147519
- if ("SHA-1" !== t) throw new Error(c);
147520
- super(t, n, e);
147521
- const r = e || {};
147522
- this.M = !0, this.g = this.Y, this.T = -1, this.C = i(this.t, this.i, this.T), this.v = g, this.L = function (t) {
147523
- return t.slice();
147524
- }, this.B = T, this.F = F, this.U = [1732584193, 4023233417, 2562383102, 271733878, 3285377520], this.m = 512, this.R = 160, this.K = !1, r.hmacKey && this.k(A("hmacKey", r.hmacKey, this.T));
147525
- }
147526
- };
147527
- function B(t) {
147528
- let n;
147529
- return n = "SHA-224" == t ? u.slice() : w.slice(), n;
147530
- }
147531
- function M(t, n) {
147532
- let e, r, i, s, o, u, w, c, f, a, l;
147533
- const A = [];
147534
- for (e = n[0], r = n[1], i = n[2], s = n[3], o = n[4], u = n[5], w = n[6], c = n[7], l = 0; l < 64; l += 1) A[l] = l < 16 ? t[l] : U(S(E = A[l - 2], 17) ^ S(E, 19) ^ b(E, 10), A[l - 7], R(A[l - 15]), A[l - 16]), f = v(c, K(o), d(o, u, w), h[l], A[l]), a = y(C(e), m(e, r, i)), c = w, w = u, u = o, o = y(s, f), s = i, i = r, r = e, e = y(f, a);
147535
- var E;
147536
- return n[0] = y(e, n[0]), n[1] = y(r, n[1]), n[2] = y(i, n[2]), n[3] = y(s, n[3]), n[4] = y(o, n[4]), n[5] = y(u, n[5]), n[6] = y(w, n[6]), n[7] = y(c, n[7]), n;
147537
- }
147538
- let k = class extends E {
147539
- constructor(t, n, e) {
147540
- if ("SHA-224" !== t && "SHA-256" !== t) throw new Error(c);
147541
- super(t, n, e);
147542
- const r = e || {};
147543
- this.g = this.Y, this.M = !0, this.T = -1, this.C = i(this.t, this.i, this.T), this.v = M, this.L = function (t) {
147544
- return t.slice();
147545
- }, this.B = B, this.F = function (n, e, r, i) {
147546
- return function (t, n, e, r, i) {
147547
- let s, h;
147548
- const u = 15 + (n + 65 >>> 9 << 4),
147549
- w = n + e;
147550
- for (; t.length <= u;) t.push(0);
147551
- for (t[n >>> 5] |= 128 << 24 - n % 32, t[u] = 4294967295 & w, t[u - 1] = w / o | 0, s = 0; s < t.length; s += 16) r = M(t.slice(s, s + 16), r);
147552
- return h = "SHA-224" === i ? [r[0], r[1], r[2], r[3], r[4], r[5], r[6]] : r, h;
147553
- }(n, e, r, i, t);
147554
- }, this.U = B(t), this.m = 512, this.R = "SHA-224" === t ? 224 : 256, this.K = !1, r.hmacKey && this.k(A("hmacKey", r.hmacKey, this.T));
147555
- }
147556
- };
147557
- class Y {
147558
- constructor(t, n) {
147559
- this.N = t, this.I = n;
147560
- }
147561
- }
147562
- function N(t, n) {
147563
- let e;
147564
- return n > 32 ? (e = 64 - n, new Y(t.I << n | t.N >>> e, t.N << n | t.I >>> e)) : 0 !== n ? (e = 32 - n, new Y(t.N << n | t.I >>> e, t.I << n | t.N >>> e)) : t;
147565
- }
147566
- function I(t, n) {
147567
- let e;
147568
- return n < 32 ? (e = 32 - n, new Y(t.N >>> n | t.I << e, t.I >>> n | t.N << e)) : (e = 64 - n, new Y(t.I >>> n | t.N << e, t.N >>> n | t.I << e));
147569
- }
147570
- function X(t, n) {
147571
- return new Y(t.N >>> n, t.I >>> n | t.N << 32 - n);
147572
- }
147573
- function z(t, n, e) {
147574
- return new Y(t.N & n.N ^ t.N & e.N ^ n.N & e.N, t.I & n.I ^ t.I & e.I ^ n.I & e.I);
147575
- }
147576
- function x(t) {
147577
- const n = I(t, 28),
147578
- e = I(t, 34),
147579
- r = I(t, 39);
147580
- return new Y(n.N ^ e.N ^ r.N, n.I ^ e.I ^ r.I);
147581
- }
147582
- function _(t, n) {
147583
- let e, r;
147584
- e = (65535 & t.I) + (65535 & n.I), r = (t.I >>> 16) + (n.I >>> 16) + (e >>> 16);
147585
- const i = (65535 & r) << 16 | 65535 & e;
147586
- e = (65535 & t.N) + (65535 & n.N) + (r >>> 16), r = (t.N >>> 16) + (n.N >>> 16) + (e >>> 16);
147587
- return new Y((65535 & r) << 16 | 65535 & e, i);
147588
- }
147589
- function O(t, n, e, r) {
147590
- let i, s;
147591
- i = (65535 & t.I) + (65535 & n.I) + (65535 & e.I) + (65535 & r.I), s = (t.I >>> 16) + (n.I >>> 16) + (e.I >>> 16) + (r.I >>> 16) + (i >>> 16);
147592
- const o = (65535 & s) << 16 | 65535 & i;
147593
- i = (65535 & t.N) + (65535 & n.N) + (65535 & e.N) + (65535 & r.N) + (s >>> 16), s = (t.N >>> 16) + (n.N >>> 16) + (e.N >>> 16) + (r.N >>> 16) + (i >>> 16);
147594
- return new Y((65535 & s) << 16 | 65535 & i, o);
147595
- }
147596
- function P(t, n, e, r, i) {
147597
- let s, o;
147598
- s = (65535 & t.I) + (65535 & n.I) + (65535 & e.I) + (65535 & r.I) + (65535 & i.I), o = (t.I >>> 16) + (n.I >>> 16) + (e.I >>> 16) + (r.I >>> 16) + (i.I >>> 16) + (s >>> 16);
147599
- const h = (65535 & o) << 16 | 65535 & s;
147600
- s = (65535 & t.N) + (65535 & n.N) + (65535 & e.N) + (65535 & r.N) + (65535 & i.N) + (o >>> 16), o = (t.N >>> 16) + (n.N >>> 16) + (e.N >>> 16) + (r.N >>> 16) + (i.N >>> 16) + (s >>> 16);
147601
- return new Y((65535 & o) << 16 | 65535 & s, h);
147602
- }
147603
- function V(t, n) {
147604
- return new Y(t.N ^ n.N, t.I ^ n.I);
147605
- }
147606
- function Z(t) {
147607
- const n = I(t, 19),
147608
- e = I(t, 61),
147609
- r = X(t, 6);
147610
- return new Y(n.N ^ e.N ^ r.N, n.I ^ e.I ^ r.I);
147611
- }
147612
- function j(t) {
147613
- const n = I(t, 1),
147614
- e = I(t, 8),
147615
- r = X(t, 7);
147616
- return new Y(n.N ^ e.N ^ r.N, n.I ^ e.I ^ r.I);
147617
- }
147618
- function q(t) {
147619
- const n = I(t, 14),
147620
- e = I(t, 18),
147621
- r = I(t, 41);
147622
- return new Y(n.N ^ e.N ^ r.N, n.I ^ e.I ^ r.I);
147623
- }
147624
- const D = [new Y(h[0], 3609767458), new Y(h[1], 602891725), new Y(h[2], 3964484399), new Y(h[3], 2173295548), new Y(h[4], 4081628472), new Y(h[5], 3053834265), new Y(h[6], 2937671579), new Y(h[7], 3664609560), new Y(h[8], 2734883394), new Y(h[9], 1164996542), new Y(h[10], 1323610764), new Y(h[11], 3590304994), new Y(h[12], 4068182383), new Y(h[13], 991336113), new Y(h[14], 633803317), new Y(h[15], 3479774868), new Y(h[16], 2666613458), new Y(h[17], 944711139), new Y(h[18], 2341262773), new Y(h[19], 2007800933), new Y(h[20], 1495990901), new Y(h[21], 1856431235), new Y(h[22], 3175218132), new Y(h[23], 2198950837), new Y(h[24], 3999719339), new Y(h[25], 766784016), new Y(h[26], 2566594879), new Y(h[27], 3203337956), new Y(h[28], 1034457026), new Y(h[29], 2466948901), new Y(h[30], 3758326383), new Y(h[31], 168717936), new Y(h[32], 1188179964), new Y(h[33], 1546045734), new Y(h[34], 1522805485), new Y(h[35], 2643833823), new Y(h[36], 2343527390), new Y(h[37], 1014477480), new Y(h[38], 1206759142), new Y(h[39], 344077627), new Y(h[40], 1290863460), new Y(h[41], 3158454273), new Y(h[42], 3505952657), new Y(h[43], 106217008), new Y(h[44], 3606008344), new Y(h[45], 1432725776), new Y(h[46], 1467031594), new Y(h[47], 851169720), new Y(h[48], 3100823752), new Y(h[49], 1363258195), new Y(h[50], 3750685593), new Y(h[51], 3785050280), new Y(h[52], 3318307427), new Y(h[53], 3812723403), new Y(h[54], 2003034995), new Y(h[55], 3602036899), new Y(h[56], 1575990012), new Y(h[57], 1125592928), new Y(h[58], 2716904306), new Y(h[59], 442776044), new Y(h[60], 593698344), new Y(h[61], 3733110249), new Y(h[62], 2999351573), new Y(h[63], 3815920427), new Y(3391569614, 3928383900), new Y(3515267271, 566280711), new Y(3940187606, 3454069534), new Y(4118630271, 4000239992), new Y(116418474, 1914138554), new Y(174292421, 2731055270), new Y(289380356, 3203993006), new Y(460393269, 320620315), new Y(685471733, 587496836), new Y(852142971, 1086792851), new Y(1017036298, 365543100), new Y(1126000580, 2618297676), new Y(1288033470, 3409855158), new Y(1501505948, 4234509866), new Y(1607167915, 987167468), new Y(1816402316, 1246189591)];
147625
- function G(t) {
147626
- return "SHA-384" === t ? [new Y(3418070365, u[0]), new Y(1654270250, u[1]), new Y(2438529370, u[2]), new Y(355462360, u[3]), new Y(1731405415, u[4]), new Y(41048885895, u[5]), new Y(3675008525, u[6]), new Y(1203062813, u[7])] : [new Y(w[0], 4089235720), new Y(w[1], 2227873595), new Y(w[2], 4271175723), new Y(w[3], 1595750129), new Y(w[4], 2917565137), new Y(w[5], 725511199), new Y(w[6], 4215389547), new Y(w[7], 327033209)];
147627
- }
147628
- function J(t, n) {
147629
- let e, r, i, s, o, h, u, w, c, f, a, l;
147630
- const A = [];
147631
- for (e = n[0], r = n[1], i = n[2], s = n[3], o = n[4], h = n[5], u = n[6], w = n[7], a = 0; a < 80; a += 1) a < 16 ? (l = 2 * a, A[a] = new Y(t[l], t[l + 1])) : A[a] = O(Z(A[a - 2]), A[a - 7], j(A[a - 15]), A[a - 16]), c = P(w, q(o), (H = h, S = u, new Y((E = o).N & H.N ^ ~E.N & S.N, E.I & H.I ^ ~E.I & S.I)), D[a], A[a]), f = _(x(e), z(e, r, i)), w = u, u = h, h = o, o = _(s, c), s = i, i = r, r = e, e = _(c, f);
147632
- var E, H, S;
147633
- return n[0] = _(e, n[0]), n[1] = _(r, n[1]), n[2] = _(i, n[2]), n[3] = _(s, n[3]), n[4] = _(o, n[4]), n[5] = _(h, n[5]), n[6] = _(u, n[6]), n[7] = _(w, n[7]), n;
147634
- }
147635
- let Q = class extends E {
147636
- constructor(t, n, e) {
147637
- if ("SHA-384" !== t && "SHA-512" !== t) throw new Error(c);
147638
- super(t, n, e);
147639
- const r = e || {};
147640
- this.g = this.Y, this.M = !0, this.T = -1, this.C = i(this.t, this.i, this.T), this.v = J, this.L = function (t) {
147641
- return t.slice();
147642
- }, this.B = G, this.F = function (n, e, r, i) {
147643
- return function (t, n, e, r, i) {
147644
- let s, h;
147645
- const u = 31 + (n + 129 >>> 10 << 5),
147646
- w = n + e;
147647
- for (; t.length <= u;) t.push(0);
147648
- for (t[n >>> 5] |= 128 << 24 - n % 32, t[u] = 4294967295 & w, t[u - 1] = w / o | 0, s = 0; s < t.length; s += 32) r = J(t.slice(s, s + 32), r);
147649
- return h = "SHA-384" === i ? [r[0].N, r[0].I, r[1].N, r[1].I, r[2].N, r[2].I, r[3].N, r[3].I, r[4].N, r[4].I, r[5].N, r[5].I] : [r[0].N, r[0].I, r[1].N, r[1].I, r[2].N, r[2].I, r[3].N, r[3].I, r[4].N, r[4].I, r[5].N, r[5].I, r[6].N, r[6].I, r[7].N, r[7].I], h;
147650
- }(n, e, r, i, t);
147651
- }, this.U = G(t), this.m = 1024, this.R = "SHA-384" === t ? 384 : 512, this.K = !1, r.hmacKey && this.k(A("hmacKey", r.hmacKey, this.T));
147652
- }
147653
- };
147654
- const W = [new Y(0, 1), new Y(0, 32898), new Y(2147483648, 32906), new Y(2147483648, 2147516416), new Y(0, 32907), new Y(0, 2147483649), new Y(2147483648, 2147516545), new Y(2147483648, 32777), new Y(0, 138), new Y(0, 136), new Y(0, 2147516425), new Y(0, 2147483658), new Y(0, 2147516555), new Y(2147483648, 139), new Y(2147483648, 32905), new Y(2147483648, 32771), new Y(2147483648, 32770), new Y(2147483648, 128), new Y(0, 32778), new Y(2147483648, 2147483658), new Y(2147483648, 2147516545), new Y(2147483648, 32896), new Y(0, 2147483649), new Y(2147483648, 2147516424)],
147655
- $ = [[0, 36, 3, 41, 18], [1, 44, 10, 45, 2], [62, 6, 43, 15, 61], [28, 55, 25, 21, 56], [27, 20, 39, 8, 14]];
147656
- function tt(t) {
147657
- let n;
147658
- const e = [];
147659
- for (n = 0; n < 5; n += 1) e[n] = [new Y(0, 0), new Y(0, 0), new Y(0, 0), new Y(0, 0), new Y(0, 0)];
147660
- return e;
147661
- }
147662
- function nt(t) {
147663
- let n;
147664
- const e = [];
147665
- for (n = 0; n < 5; n += 1) e[n] = t[n].slice();
147666
- return e;
147667
- }
147668
- function et(t, n) {
147669
- let e, r, i, s;
147670
- const o = [],
147671
- h = [];
147672
- if (null !== t) for (r = 0; r < t.length; r += 2) n[(r >>> 1) % 5][(r >>> 1) / 5 | 0] = V(n[(r >>> 1) % 5][(r >>> 1) / 5 | 0], new Y(t[r + 1], t[r]));
147673
- for (e = 0; e < 24; e += 1) {
147674
- for (s = tt(), r = 0; r < 5; r += 1) o[r] = (u = n[r][0], w = n[r][1], c = n[r][2], f = n[r][3], a = n[r][4], new Y(u.N ^ w.N ^ c.N ^ f.N ^ a.N, u.I ^ w.I ^ c.I ^ f.I ^ a.I));
147675
- for (r = 0; r < 5; r += 1) h[r] = V(o[(r + 4) % 5], N(o[(r + 1) % 5], 1));
147676
- for (r = 0; r < 5; r += 1) for (i = 0; i < 5; i += 1) n[r][i] = V(n[r][i], h[r]);
147677
- for (r = 0; r < 5; r += 1) for (i = 0; i < 5; i += 1) s[i][(2 * r + 3 * i) % 5] = N(n[r][i], $[r][i]);
147678
- for (r = 0; r < 5; r += 1) for (i = 0; i < 5; i += 1) n[r][i] = V(s[r][i], new Y(~s[(r + 1) % 5][i].N & s[(r + 2) % 5][i].N, ~s[(r + 1) % 5][i].I & s[(r + 2) % 5][i].I));
147679
- n[0][0] = V(n[0][0], W[e]);
147680
- }
147681
- var u, w, c, f, a;
147682
- return n;
146975
+ function split(lst, le = false) {
146976
+ let Ah = new Uint32Array(lst.length);
146977
+ let Al = new Uint32Array(lst.length);
146978
+ for(let i = 0; i < lst.length; i++){
146979
+ const { h, l } = fromBig(lst[i], le);
146980
+ [Ah[i], Al[i]] = [
146981
+ h,
146982
+ l
146983
+ ];
146984
+ }
146985
+ return [
146986
+ Ah,
146987
+ Al
146988
+ ];
147683
146989
  }
147684
- function rt(t) {
147685
- let n,
147686
- e,
147687
- r = 0;
147688
- const i = [0, 0],
147689
- s = [4294967295 & t, t / o & 2097151];
147690
- for (n = 6; n >= 0; n--) e = s[n >> 2] >>> 8 * n & 255, 0 === e && 0 === r || (i[r + 1 >> 2] |= e << 8 * (r + 1), r += 1);
147691
- return r = 0 !== r ? r : 1, i[0] |= r, {
147692
- value: r + 1 > 4 ? i : [i[0]],
147693
- binLen: 8 + 8 * r
147694
- };
146990
+ const toBig = (h, l)=>BigInt(h >>> 0) << _32n | BigInt(l >>> 0);
146991
+ // for Shift in [0, 32)
146992
+ const shrSH = (h, _l, s)=>h >>> s;
146993
+ const shrSL = (h, l, s)=>h << 32 - s | l >>> s;
146994
+ // Right rotate for Shift in [1, 32)
146995
+ const rotrSH = (h, l, s)=>h >>> s | l << 32 - s;
146996
+ const rotrSL = (h, l, s)=>h << 32 - s | l >>> s;
146997
+ // Right rotate for Shift in (32, 64), NOTE: 32 is special case.
146998
+ const rotrBH = (h, l, s)=>h << 64 - s | l >>> s - 32;
146999
+ const rotrBL = (h, l, s)=>h >>> s - 32 | l << 64 - s;
147000
+ // Right rotate for shift===32 (just swaps l&h)
147001
+ const rotr32H = (_h, l)=>l;
147002
+ const rotr32L = (h, _l)=>h;
147003
+ // Left rotate for Shift in [1, 32)
147004
+ const rotlSH = (h, l, s)=>h << s | l >>> 32 - s;
147005
+ const rotlSL = (h, l, s)=>l << s | h >>> 32 - s;
147006
+ // Left rotate for Shift in (32, 64), NOTE: 32 is special case.
147007
+ const rotlBH = (h, l, s)=>l << s - 32 | h >>> 64 - s;
147008
+ const rotlBL = (h, l, s)=>h << s - 32 | l >>> 64 - s;
147009
+ // JS uses 32-bit signed integers for bitwise operations which means we cannot
147010
+ // simple take carry out of low bit sum by shift, we need to use division.
147011
+ function add(Ah, Al, Bh, Bl) {
147012
+ const l = (Al >>> 0) + (Bl >>> 0);
147013
+ return {
147014
+ h: Ah + Bh + (l / 2 ** 32 | 0) | 0,
147015
+ l: l | 0
147016
+ };
147695
147017
  }
147696
- function it(t) {
147697
- return a(rt(t.binLen), t);
147698
- }
147699
- function st(t, n) {
147700
- let e,
147701
- r = rt(n);
147702
- r = a(r, t);
147703
- const i = n >>> 2,
147704
- s = (i - r.value.length % i) % i;
147705
- for (e = 0; e < s; e++) r.value.push(0);
147706
- return r.value;
147707
- }
147708
- let ot = class extends E {
147709
- constructor(t, n, e) {
147710
- let r = 6,
147711
- s = 0;
147712
- super(t, n, e);
147713
- const o = e || {};
147714
- if (1 !== this.numRounds) {
147715
- if (o.kmacKey || o.hmacKey) throw new Error(f);
147716
- if ("CSHAKE128" === this.o || "CSHAKE256" === this.o) throw new Error("Cannot set numRounds for CSHAKE variants");
147717
- }
147718
- switch (this.T = 1, this.C = i(this.t, this.i, this.T), this.v = et, this.L = nt, this.B = tt, this.U = tt(), this.K = !1, t) {
147719
- case "SHA3-224":
147720
- this.m = s = 1152, this.R = 224, this.M = !0, this.g = this.Y;
147721
- break;
147722
- case "SHA3-256":
147723
- this.m = s = 1088, this.R = 256, this.M = !0, this.g = this.Y;
147724
- break;
147725
- case "SHA3-384":
147726
- this.m = s = 832, this.R = 384, this.M = !0, this.g = this.Y;
147727
- break;
147728
- case "SHA3-512":
147729
- this.m = s = 576, this.R = 512, this.M = !0, this.g = this.Y;
147730
- break;
147731
- case "SHAKE128":
147732
- r = 31, this.m = s = 1344, this.R = -1, this.K = !0, this.M = !1, this.g = null;
147733
- break;
147734
- case "SHAKE256":
147735
- r = 31, this.m = s = 1088, this.R = -1, this.K = !0, this.M = !1, this.g = null;
147736
- break;
147737
- case "KMAC128":
147738
- r = 4, this.m = s = 1344, this.X(e), this.R = -1, this.K = !0, this.M = !1, this.g = this._;
147739
- break;
147740
- case "KMAC256":
147741
- r = 4, this.m = s = 1088, this.X(e), this.R = -1, this.K = !0, this.M = !1, this.g = this._;
147742
- break;
147743
- case "CSHAKE128":
147744
- this.m = s = 1344, r = this.O(e), this.R = -1, this.K = !0, this.M = !1, this.g = null;
147745
- break;
147746
- case "CSHAKE256":
147747
- this.m = s = 1088, r = this.O(e), this.R = -1, this.K = !0, this.M = !1, this.g = null;
147748
- break;
147749
- default:
147750
- throw new Error(c);
147751
- }
147752
- this.F = function (t, n, e, i, o) {
147753
- return function (t, n, e, r, i, s, o) {
147754
- let h,
147755
- u,
147756
- w = 0;
147757
- const c = [],
147758
- f = i >>> 5,
147759
- a = n >>> 5;
147760
- for (h = 0; h < a && n >= i; h += f) r = et(t.slice(h, h + f), r), n -= i;
147761
- for (t = t.slice(h), n %= i; t.length < f;) t.push(0);
147762
- for (h = n >>> 3, t[h >> 2] ^= s << h % 4 * 8, t[f - 1] ^= 2147483648, r = et(t, r); 32 * c.length < o && (u = r[w % 5][w / 5 | 0], c.push(u.I), !(32 * c.length >= o));) c.push(u.N), w += 1, 0 == 64 * w % i && (et(null, r), w = 0);
147763
- return c;
147764
- }(t, n, 0, i, s, r, o);
147765
- }, o.hmacKey && this.k(A("hmacKey", o.hmacKey, this.T));
147766
- }
147767
- O(t, n) {
147768
- const e = function (t) {
147769
- const n = t || {};
147770
- return {
147771
- funcName: A("funcName", n.funcName, 1, {
147772
- value: [],
147773
- binLen: 0
147774
- }),
147775
- customization: A("Customization", n.customization, 1, {
147776
- value: [],
147777
- binLen: 0
147778
- })
147779
- };
147780
- }(t || {});
147781
- n && (e.funcName = n);
147782
- const r = a(it(e.funcName), it(e.customization));
147783
- if (0 !== e.customization.binLen || 0 !== e.funcName.binLen) {
147784
- const t = st(r, this.m >>> 3);
147785
- for (let n = 0; n < t.length; n += this.m >>> 5) this.U = this.v(t.slice(n, n + (this.m >>> 5)), this.U), this.A += this.m;
147786
- return 4;
147787
- }
147788
- return 31;
147789
- }
147790
- X(t) {
147791
- const n = function (t) {
147792
- const n = t || {};
147793
- return {
147794
- kmacKey: A("kmacKey", n.kmacKey, 1),
147795
- funcName: {
147796
- value: [1128353099],
147797
- binLen: 32
147798
- },
147799
- customization: A("Customization", n.customization, 1, {
147800
- value: [],
147801
- binLen: 0
147802
- })
147803
- };
147804
- }(t || {});
147805
- this.O(t, n.funcName);
147806
- const e = st(it(n.kmacKey), this.m >>> 3);
147807
- for (let t = 0; t < e.length; t += this.m >>> 5) this.U = this.v(e.slice(t, t + (this.m >>> 5)), this.U), this.A += this.m;
147808
- this.H = !0;
147809
- }
147810
- _(t) {
147811
- const n = a({
147812
- value: this.h.slice(),
147813
- binLen: this.u
147814
- }, function (t) {
147815
- let n,
147816
- e,
147817
- r = 0;
147818
- const i = [0, 0],
147819
- s = [4294967295 & t, t / o & 2097151];
147820
- for (n = 6; n >= 0; n--) e = s[n >> 2] >>> 8 * n & 255, 0 === e && 0 === r || (i[r >> 2] |= e << 8 * r, r += 1);
147821
- return r = 0 !== r ? r : 1, i[r >> 2] |= r << 8 * r, {
147822
- value: r + 1 > 4 ? i : [i[0]],
147823
- binLen: 8 + 8 * r
147824
- };
147825
- }(t.outputLen));
147826
- return this.F(n.value, n.binLen, this.A, this.L(this.U), t.outputLen);
147827
- }
147018
+ // Addition with more than 2 elements
147019
+ const add3L = (Al, Bl, Cl)=>(Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
147020
+ const add3H = (low, Ah, Bh, Ch)=>Ah + Bh + Ch + (low / 2 ** 32 | 0) | 0;
147021
+ const add4L = (Al, Bl, Cl, Dl)=>(Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
147022
+ const add4H = (low, Ah, Bh, Ch, Dh)=>Ah + Bh + Ch + Dh + (low / 2 ** 32 | 0) | 0;
147023
+ const add5L = (Al, Bl, Cl, Dl, El)=>(Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
147024
+ const add5H = (low, Ah, Bh, Ch, Dh, Eh)=>Ah + Bh + Ch + Dh + Eh + (low / 2 ** 32 | 0) | 0;
147025
+ // prettier-ignore
147026
+ const u64 = {
147027
+ fromBig,
147028
+ split,
147029
+ toBig,
147030
+ shrSH,
147031
+ shrSL,
147032
+ rotrSH,
147033
+ rotrSL,
147034
+ rotrBH,
147035
+ rotrBL,
147036
+ rotr32H,
147037
+ rotr32L,
147038
+ rotlSH,
147039
+ rotlSL,
147040
+ rotlBH,
147041
+ rotlBL,
147042
+ add,
147043
+ add3L,
147044
+ add3H,
147045
+ add4L,
147046
+ add4H,
147047
+ add5H,
147048
+ add5L
147828
147049
  };
147829
- class ht {
147830
- constructor(t, n, e) {
147831
- if ("SHA-1" == t) this.P = new L(t, n, e);else if ("SHA-224" == t || "SHA-256" == t) this.P = new k(t, n, e);else if ("SHA-384" == t || "SHA-512" == t) this.P = new Q(t, n, e);else {
147832
- if ("SHA3-224" != t && "SHA3-256" != t && "SHA3-384" != t && "SHA3-512" != t && "SHAKE128" != t && "SHAKE256" != t && "CSHAKE128" != t && "CSHAKE256" != t && "KMAC128" != t && "KMAC256" != t) throw new Error(c);
147833
- this.P = new ot(t, n, e);
147050
+
147051
+ // Round contants (first 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409):
147052
+ // prettier-ignore
147053
+ const [SHA512_Kh, SHA512_Kl] = /* @__PURE__ */ (()=>u64.split([
147054
+ '0x428a2f98d728ae22',
147055
+ '0x7137449123ef65cd',
147056
+ '0xb5c0fbcfec4d3b2f',
147057
+ '0xe9b5dba58189dbbc',
147058
+ '0x3956c25bf348b538',
147059
+ '0x59f111f1b605d019',
147060
+ '0x923f82a4af194f9b',
147061
+ '0xab1c5ed5da6d8118',
147062
+ '0xd807aa98a3030242',
147063
+ '0x12835b0145706fbe',
147064
+ '0x243185be4ee4b28c',
147065
+ '0x550c7dc3d5ffb4e2',
147066
+ '0x72be5d74f27b896f',
147067
+ '0x80deb1fe3b1696b1',
147068
+ '0x9bdc06a725c71235',
147069
+ '0xc19bf174cf692694',
147070
+ '0xe49b69c19ef14ad2',
147071
+ '0xefbe4786384f25e3',
147072
+ '0x0fc19dc68b8cd5b5',
147073
+ '0x240ca1cc77ac9c65',
147074
+ '0x2de92c6f592b0275',
147075
+ '0x4a7484aa6ea6e483',
147076
+ '0x5cb0a9dcbd41fbd4',
147077
+ '0x76f988da831153b5',
147078
+ '0x983e5152ee66dfab',
147079
+ '0xa831c66d2db43210',
147080
+ '0xb00327c898fb213f',
147081
+ '0xbf597fc7beef0ee4',
147082
+ '0xc6e00bf33da88fc2',
147083
+ '0xd5a79147930aa725',
147084
+ '0x06ca6351e003826f',
147085
+ '0x142929670a0e6e70',
147086
+ '0x27b70a8546d22ffc',
147087
+ '0x2e1b21385c26c926',
147088
+ '0x4d2c6dfc5ac42aed',
147089
+ '0x53380d139d95b3df',
147090
+ '0x650a73548baf63de',
147091
+ '0x766a0abb3c77b2a8',
147092
+ '0x81c2c92e47edaee6',
147093
+ '0x92722c851482353b',
147094
+ '0xa2bfe8a14cf10364',
147095
+ '0xa81a664bbc423001',
147096
+ '0xc24b8b70d0f89791',
147097
+ '0xc76c51a30654be30',
147098
+ '0xd192e819d6ef5218',
147099
+ '0xd69906245565a910',
147100
+ '0xf40e35855771202a',
147101
+ '0x106aa07032bbd1b8',
147102
+ '0x19a4c116b8d2d0c8',
147103
+ '0x1e376c085141ab53',
147104
+ '0x2748774cdf8eeb99',
147105
+ '0x34b0bcb5e19b48a8',
147106
+ '0x391c0cb3c5c95a63',
147107
+ '0x4ed8aa4ae3418acb',
147108
+ '0x5b9cca4f7763e373',
147109
+ '0x682e6ff3d6b2b8a3',
147110
+ '0x748f82ee5defb2fc',
147111
+ '0x78a5636f43172f60',
147112
+ '0x84c87814a1f0ab72',
147113
+ '0x8cc702081a6439ec',
147114
+ '0x90befffa23631e28',
147115
+ '0xa4506cebde82bde9',
147116
+ '0xbef9a3f7b2c67915',
147117
+ '0xc67178f2e372532b',
147118
+ '0xca273eceea26619c',
147119
+ '0xd186b8c721c0c207',
147120
+ '0xeada7dd6cde0eb1e',
147121
+ '0xf57d4f7fee6ed178',
147122
+ '0x06f067aa72176fba',
147123
+ '0x0a637dc5a2c898a6',
147124
+ '0x113f9804bef90dae',
147125
+ '0x1b710b35131c471b',
147126
+ '0x28db77f523047d84',
147127
+ '0x32caab7b40c72493',
147128
+ '0x3c9ebe0a15c9bebc',
147129
+ '0x431d67c49c100d4c',
147130
+ '0x4cc5d4becb3e42b6',
147131
+ '0x597f299cfc657e2a',
147132
+ '0x5fcb6fab3ad6faec',
147133
+ '0x6c44198c4a475817'
147134
+ ].map((n)=>BigInt(n))))();
147135
+ // Temporary buffer, not used to store anything between runs
147136
+ const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
147137
+ const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
147138
+ class SHA512 extends HashMD {
147139
+ // prettier-ignore
147140
+ get() {
147141
+ const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
147142
+ return [
147143
+ Ah,
147144
+ Al,
147145
+ Bh,
147146
+ Bl,
147147
+ Ch,
147148
+ Cl,
147149
+ Dh,
147150
+ Dl,
147151
+ Eh,
147152
+ El,
147153
+ Fh,
147154
+ Fl,
147155
+ Gh,
147156
+ Gl,
147157
+ Hh,
147158
+ Hl
147159
+ ];
147834
147160
  }
147835
- }
147836
- update(t) {
147837
- return this.P.update(t), this;
147838
- }
147839
- getHash(t, n) {
147840
- return this.P.getHash(t, n);
147841
- }
147842
- setHMACKey(t, n, e) {
147843
- this.P.setHMACKey(t, n, e);
147844
- }
147845
- getHMAC(t, n) {
147846
- return this.P.getHMAC(t, n);
147847
- }
147848
- }
147161
+ // prettier-ignore
147162
+ set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
147163
+ this.Ah = Ah | 0;
147164
+ this.Al = Al | 0;
147165
+ this.Bh = Bh | 0;
147166
+ this.Bl = Bl | 0;
147167
+ this.Ch = Ch | 0;
147168
+ this.Cl = Cl | 0;
147169
+ this.Dh = Dh | 0;
147170
+ this.Dl = Dl | 0;
147171
+ this.Eh = Eh | 0;
147172
+ this.El = El | 0;
147173
+ this.Fh = Fh | 0;
147174
+ this.Fl = Fl | 0;
147175
+ this.Gh = Gh | 0;
147176
+ this.Gl = Gl | 0;
147177
+ this.Hh = Hh | 0;
147178
+ this.Hl = Hl | 0;
147179
+ }
147180
+ process(view, offset) {
147181
+ // Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
147182
+ for(let i = 0; i < 16; i++, offset += 4){
147183
+ SHA512_W_H[i] = view.getUint32(offset);
147184
+ SHA512_W_L[i] = view.getUint32(offset += 4);
147185
+ }
147186
+ for(let i = 16; i < 80; i++){
147187
+ // s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
147188
+ const W15h = SHA512_W_H[i - 15] | 0;
147189
+ const W15l = SHA512_W_L[i - 15] | 0;
147190
+ const s0h = u64.rotrSH(W15h, W15l, 1) ^ u64.rotrSH(W15h, W15l, 8) ^ u64.shrSH(W15h, W15l, 7);
147191
+ const s0l = u64.rotrSL(W15h, W15l, 1) ^ u64.rotrSL(W15h, W15l, 8) ^ u64.shrSL(W15h, W15l, 7);
147192
+ // s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
147193
+ const W2h = SHA512_W_H[i - 2] | 0;
147194
+ const W2l = SHA512_W_L[i - 2] | 0;
147195
+ const s1h = u64.rotrSH(W2h, W2l, 19) ^ u64.rotrBH(W2h, W2l, 61) ^ u64.shrSH(W2h, W2l, 6);
147196
+ const s1l = u64.rotrSL(W2h, W2l, 19) ^ u64.rotrBL(W2h, W2l, 61) ^ u64.shrSL(W2h, W2l, 6);
147197
+ // SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
147198
+ const SUMl = u64.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
147199
+ const SUMh = u64.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
147200
+ SHA512_W_H[i] = SUMh | 0;
147201
+ SHA512_W_L[i] = SUMl | 0;
147202
+ }
147203
+ let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
147204
+ // Compression function main loop, 80 rounds
147205
+ for(let i = 0; i < 80; i++){
147206
+ // S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
147207
+ const sigma1h = u64.rotrSH(Eh, El, 14) ^ u64.rotrSH(Eh, El, 18) ^ u64.rotrBH(Eh, El, 41);
147208
+ const sigma1l = u64.rotrSL(Eh, El, 14) ^ u64.rotrSL(Eh, El, 18) ^ u64.rotrBL(Eh, El, 41);
147209
+ //const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
147210
+ const CHIh = Eh & Fh ^ ~Eh & Gh;
147211
+ const CHIl = El & Fl ^ ~El & Gl;
147212
+ // T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
147213
+ // prettier-ignore
147214
+ const T1ll = u64.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
147215
+ const T1h = u64.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
147216
+ const T1l = T1ll | 0;
147217
+ // S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
147218
+ const sigma0h = u64.rotrSH(Ah, Al, 28) ^ u64.rotrBH(Ah, Al, 34) ^ u64.rotrBH(Ah, Al, 39);
147219
+ const sigma0l = u64.rotrSL(Ah, Al, 28) ^ u64.rotrBL(Ah, Al, 34) ^ u64.rotrBL(Ah, Al, 39);
147220
+ const MAJh = Ah & Bh ^ Ah & Ch ^ Bh & Ch;
147221
+ const MAJl = Al & Bl ^ Al & Cl ^ Bl & Cl;
147222
+ Hh = Gh | 0;
147223
+ Hl = Gl | 0;
147224
+ Gh = Fh | 0;
147225
+ Gl = Fl | 0;
147226
+ Fh = Eh | 0;
147227
+ Fl = El | 0;
147228
+ ({ h: Eh, l: El } = u64.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
147229
+ Dh = Ch | 0;
147230
+ Dl = Cl | 0;
147231
+ Ch = Bh | 0;
147232
+ Cl = Bl | 0;
147233
+ Bh = Ah | 0;
147234
+ Bl = Al | 0;
147235
+ const All = u64.add3L(T1l, sigma0l, MAJl);
147236
+ Ah = u64.add3H(All, T1h, sigma0h, MAJh);
147237
+ Al = All | 0;
147238
+ }
147239
+ // Add the compressed chunk to the current hash value
147240
+ ({ h: Ah, l: Al } = u64.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
147241
+ ({ h: Bh, l: Bl } = u64.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
147242
+ ({ h: Ch, l: Cl } = u64.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
147243
+ ({ h: Dh, l: Dl } = u64.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
147244
+ ({ h: Eh, l: El } = u64.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
147245
+ ({ h: Fh, l: Fl } = u64.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
147246
+ ({ h: Gh, l: Gl } = u64.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
147247
+ ({ h: Hh, l: Hl } = u64.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
147248
+ this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
147249
+ }
147250
+ roundClean() {
147251
+ SHA512_W_H.fill(0);
147252
+ SHA512_W_L.fill(0);
147253
+ }
147254
+ destroy() {
147255
+ this.buffer.fill(0);
147256
+ this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
147257
+ }
147258
+ constructor(){
147259
+ super(128, 64, 16, false);
147260
+ // We cannot use array here since array allows indexing by variable which means optimizer/compiler cannot use registers.
147261
+ // Also looks cleaner and easier to verify with spec.
147262
+ // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
147263
+ // h -- high 32 bits, l -- low 32 bits
147264
+ this.Ah = 0x6a09e667 | 0;
147265
+ this.Al = 0xf3bcc908 | 0;
147266
+ this.Bh = 0xbb67ae85 | 0;
147267
+ this.Bl = 0x84caa73b | 0;
147268
+ this.Ch = 0x3c6ef372 | 0;
147269
+ this.Cl = 0xfe94f82b | 0;
147270
+ this.Dh = 0xa54ff53a | 0;
147271
+ this.Dl = 0x5f1d36f1 | 0;
147272
+ this.Eh = 0x510e527f | 0;
147273
+ this.El = 0xade682d1 | 0;
147274
+ this.Fh = 0x9b05688c | 0;
147275
+ this.Fl = 0x2b3e6c1f | 0;
147276
+ this.Gh = 0x1f83d9ab | 0;
147277
+ this.Gl = 0xfb41bd6b | 0;
147278
+ this.Hh = 0x5be0cd19 | 0;
147279
+ this.Hl = 0x137e2179 | 0;
147280
+ }
147281
+ }
147282
+ class SHA384 extends SHA512 {
147283
+ constructor(){
147284
+ super();
147285
+ // h -- high 32 bits, l -- low 32 bits
147286
+ this.Ah = 0xcbbb9d5d | 0;
147287
+ this.Al = 0xc1059ed8 | 0;
147288
+ this.Bh = 0x629a292a | 0;
147289
+ this.Bl = 0x367cd507 | 0;
147290
+ this.Ch = 0x9159015a | 0;
147291
+ this.Cl = 0x3070dd17 | 0;
147292
+ this.Dh = 0x152fecd8 | 0;
147293
+ this.Dl = 0xf70e5939 | 0;
147294
+ this.Eh = 0x67332667 | 0;
147295
+ this.El = 0xffc00b31 | 0;
147296
+ this.Fh = 0x8eb44a87 | 0;
147297
+ this.Fl = 0x68581511 | 0;
147298
+ this.Gh = 0xdb0c2e0d | 0;
147299
+ this.Gl = 0x64f98fa7 | 0;
147300
+ this.Hh = 0x47b5481d | 0;
147301
+ this.Hl = 0xbefa4fa4 | 0;
147302
+ this.outputLen = 48;
147303
+ }
147304
+ }
147305
+ const sha512 = /* @__PURE__ */ wrapConstructor(()=>new SHA512());
147306
+ const sha384 = /* @__PURE__ */ wrapConstructor(()=>new SHA384());
147307
+
147308
+ // SHA3 (keccak) is based on a new design: basically, the internal state is bigger than output size.
147309
+ // It's called a sponge function.
147310
+ // Various per round constants calculations
147311
+ const SHA3_PI = [];
147312
+ const SHA3_ROTL = [];
147313
+ const _SHA3_IOTA = [];
147314
+ const _0n = /* @__PURE__ */ BigInt(0);
147315
+ const _1n = /* @__PURE__ */ BigInt(1);
147316
+ const _2n = /* @__PURE__ */ BigInt(2);
147317
+ const _7n = /* @__PURE__ */ BigInt(7);
147318
+ const _256n = /* @__PURE__ */ BigInt(256);
147319
+ const _0x71n = /* @__PURE__ */ BigInt(0x71);
147320
+ for(let round = 0, R = _1n, x = 1, y = 0; round < 24; round++){
147321
+ // Pi
147322
+ [x, y] = [
147323
+ y,
147324
+ (2 * x + 3 * y) % 5
147325
+ ];
147326
+ SHA3_PI.push(2 * (5 * y + x));
147327
+ // Rotational
147328
+ SHA3_ROTL.push((round + 1) * (round + 2) / 2 % 64);
147329
+ // Iota
147330
+ let t = _0n;
147331
+ for(let j = 0; j < 7; j++){
147332
+ R = (R << _1n ^ (R >> _7n) * _0x71n) % _256n;
147333
+ if (R & _2n) t ^= _1n << (_1n << /* @__PURE__ */ BigInt(j)) - _1n;
147334
+ }
147335
+ _SHA3_IOTA.push(t);
147336
+ }
147337
+ const [SHA3_IOTA_H, SHA3_IOTA_L] = /* @__PURE__ */ split(_SHA3_IOTA, true);
147338
+ // Left rotation (without 0, 32, 64)
147339
+ const rotlH = (h, l, s)=>s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s);
147340
+ const rotlL = (h, l, s)=>s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s);
147341
+ // Same as keccakf1600, but allows to skip some rounds
147342
+ function keccakP(s, rounds = 24) {
147343
+ const B = new Uint32Array(5 * 2);
147344
+ // NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
147345
+ for(let round = 24 - rounds; round < 24; round++){
147346
+ // Theta θ
147347
+ for(let x = 0; x < 10; x++)B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
147348
+ for(let x = 0; x < 10; x += 2){
147349
+ const idx1 = (x + 8) % 10;
147350
+ const idx0 = (x + 2) % 10;
147351
+ const B0 = B[idx0];
147352
+ const B1 = B[idx0 + 1];
147353
+ const Th = rotlH(B0, B1, 1) ^ B[idx1];
147354
+ const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
147355
+ for(let y = 0; y < 50; y += 10){
147356
+ s[x + y] ^= Th;
147357
+ s[x + y + 1] ^= Tl;
147358
+ }
147359
+ }
147360
+ // Rho (ρ) and Pi (π)
147361
+ let curH = s[2];
147362
+ let curL = s[3];
147363
+ for(let t = 0; t < 24; t++){
147364
+ const shift = SHA3_ROTL[t];
147365
+ const Th = rotlH(curH, curL, shift);
147366
+ const Tl = rotlL(curH, curL, shift);
147367
+ const PI = SHA3_PI[t];
147368
+ curH = s[PI];
147369
+ curL = s[PI + 1];
147370
+ s[PI] = Th;
147371
+ s[PI + 1] = Tl;
147372
+ }
147373
+ // Chi (χ)
147374
+ for(let y = 0; y < 50; y += 10){
147375
+ for(let x = 0; x < 10; x++)B[x] = s[y + x];
147376
+ for(let x = 0; x < 10; x++)s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
147377
+ }
147378
+ // Iota (ι)
147379
+ s[0] ^= SHA3_IOTA_H[round];
147380
+ s[1] ^= SHA3_IOTA_L[round];
147381
+ }
147382
+ B.fill(0);
147383
+ }
147384
+ class Keccak extends Hash {
147385
+ keccak() {
147386
+ if (!isLE) byteSwap32(this.state32);
147387
+ keccakP(this.state32, this.rounds);
147388
+ if (!isLE) byteSwap32(this.state32);
147389
+ this.posOut = 0;
147390
+ this.pos = 0;
147391
+ }
147392
+ update(data) {
147393
+ exists(this);
147394
+ const { blockLen, state } = this;
147395
+ data = toBytes(data);
147396
+ const len = data.length;
147397
+ for(let pos = 0; pos < len;){
147398
+ const take = Math.min(blockLen - this.pos, len - pos);
147399
+ for(let i = 0; i < take; i++)state[this.pos++] ^= data[pos++];
147400
+ if (this.pos === blockLen) this.keccak();
147401
+ }
147402
+ return this;
147403
+ }
147404
+ finish() {
147405
+ if (this.finished) return;
147406
+ this.finished = true;
147407
+ const { state, suffix, pos, blockLen } = this;
147408
+ // Do the padding
147409
+ state[pos] ^= suffix;
147410
+ if ((suffix & 0x80) !== 0 && pos === blockLen - 1) this.keccak();
147411
+ state[blockLen - 1] ^= 0x80;
147412
+ this.keccak();
147413
+ }
147414
+ writeInto(out) {
147415
+ exists(this, false);
147416
+ bytes(out);
147417
+ this.finish();
147418
+ const bufferOut = this.state;
147419
+ const { blockLen } = this;
147420
+ for(let pos = 0, len = out.length; pos < len;){
147421
+ if (this.posOut >= blockLen) this.keccak();
147422
+ const take = Math.min(blockLen - this.posOut, len - pos);
147423
+ out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
147424
+ this.posOut += take;
147425
+ pos += take;
147426
+ }
147427
+ return out;
147428
+ }
147429
+ xofInto(out) {
147430
+ // Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
147431
+ if (!this.enableXOF) throw new Error('XOF is not possible for this instance');
147432
+ return this.writeInto(out);
147433
+ }
147434
+ xof(bytes) {
147435
+ number(bytes);
147436
+ return this.xofInto(new Uint8Array(bytes));
147437
+ }
147438
+ digestInto(out) {
147439
+ output(out, this);
147440
+ if (this.finished) throw new Error('digest() was already called');
147441
+ this.writeInto(out);
147442
+ this.destroy();
147443
+ return out;
147444
+ }
147445
+ digest() {
147446
+ return this.digestInto(new Uint8Array(this.outputLen));
147447
+ }
147448
+ destroy() {
147449
+ this.destroyed = true;
147450
+ this.state.fill(0);
147451
+ }
147452
+ _cloneInto(to) {
147453
+ const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
147454
+ to || (to = new Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
147455
+ to.state32.set(this.state32);
147456
+ to.pos = this.pos;
147457
+ to.posOut = this.posOut;
147458
+ to.finished = this.finished;
147459
+ to.rounds = rounds;
147460
+ // Suffix can change in cSHAKE
147461
+ to.suffix = suffix;
147462
+ to.outputLen = outputLen;
147463
+ to.enableXOF = enableXOF;
147464
+ to.destroyed = this.destroyed;
147465
+ return to;
147466
+ }
147467
+ // NOTE: we accept arguments in bytes instead of bits here.
147468
+ constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24){
147469
+ super();
147470
+ this.blockLen = blockLen;
147471
+ this.suffix = suffix;
147472
+ this.outputLen = outputLen;
147473
+ this.enableXOF = enableXOF;
147474
+ this.rounds = rounds;
147475
+ this.pos = 0;
147476
+ this.posOut = 0;
147477
+ this.finished = false;
147478
+ this.destroyed = false;
147479
+ // Can be passed from user as dkLen
147480
+ number(outputLen);
147481
+ // 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
147482
+ if (0 >= this.blockLen || this.blockLen >= 200) throw new Error('Sha3 supports only keccak-f1600 function');
147483
+ this.state = new Uint8Array(200);
147484
+ this.state32 = u32(this.state);
147485
+ }
147486
+ }
147487
+ const gen = (suffix, blockLen, outputLen)=>wrapConstructor(()=>new Keccak(blockLen, suffix, outputLen));
147488
+ const sha3_224 = /* @__PURE__ */ gen(0x06, 144, 224 / 8);
147489
+ /**
147490
+ * SHA3-256 hash function
147491
+ * @param message - that would be hashed
147492
+ */ const sha3_256 = /* @__PURE__ */ gen(0x06, 136, 256 / 8);
147493
+ const sha3_384 = /* @__PURE__ */ gen(0x06, 104, 384 / 8);
147494
+ const sha3_512 = /* @__PURE__ */ gen(0x06, 72, 512 / 8);
147849
147495
 
147850
147496
  /**
147851
147497
  * "globalThis" ponyfill.
147852
147498
  * @see [A horrifying globalThis polyfill in universal JavaScript](https://mathiasbynens.be/notes/globalthis)
147853
147499
  * @type {Object.<string, *>}
147854
- */
147855
- const globalScope = (() => {
147856
- if (typeof globalThis === "object") return globalThis;else {
147857
- Object.defineProperty(Object.prototype, "__GLOBALTHIS__", {
147858
- get() {
147859
- return this;
147860
- },
147861
- configurable: true
147862
- });
147863
- try {
147864
- // @ts-ignore
147865
- // eslint-disable-next-line no-undef
147866
- if (typeof __GLOBALTHIS__ !== "undefined") return __GLOBALTHIS__;
147867
- } finally {
147868
- // @ts-ignore
147869
- delete Object.prototype.__GLOBALTHIS__;
147500
+ */ const globalScope = (()=>{
147501
+ if (typeof globalThis === "object") return globalThis;
147502
+ else {
147503
+ Object.defineProperty(Object.prototype, "__GLOBALTHIS__", {
147504
+ get () {
147505
+ return this;
147506
+ },
147507
+ configurable: true
147508
+ });
147509
+ try {
147510
+ // @ts-ignore
147511
+ // eslint-disable-next-line no-undef
147512
+ if (typeof __GLOBALTHIS__ !== "undefined") return __GLOBALTHIS__;
147513
+ } finally{
147514
+ // @ts-ignore
147515
+ delete Object.prototype.__GLOBALTHIS__;
147516
+ }
147870
147517
  }
147871
- }
147872
-
147873
- // Still unable to determine "globalThis", fall back to a naive method.
147874
- if (typeof self !== "undefined") return self;else if (typeof window !== "undefined") return window;else if (typeof global !== "undefined") return global;
147875
- return undefined;
147518
+ // Still unable to determine "globalThis", fall back to a naive method.
147519
+ if (typeof self !== "undefined") return self;
147520
+ else if (typeof window !== "undefined") return window;
147521
+ else if (typeof global !== "undefined") return global;
147522
+ return undefined;
147876
147523
  })();
147877
147524
 
147878
147525
  /**
147879
- * OpenSSL to jsSHA algorithms map.
147880
- * @type {Object.<string, "SHA-1"|"SHA-224"|"SHA-256"|"SHA-384"|"SHA-512"|"SHA3-224"|"SHA3-256"|"SHA3-384"|"SHA3-512">}
147881
- */
147882
- const OPENSSL_JSSHA_ALGO_MAP = {
147883
- SHA1: "SHA-1",
147884
- SHA224: "SHA-224",
147885
- SHA256: "SHA-256",
147886
- SHA384: "SHA-384",
147887
- SHA512: "SHA-512",
147888
- "SHA3-224": "SHA3-224",
147889
- "SHA3-256": "SHA3-256",
147890
- "SHA3-384": "SHA3-384",
147891
- "SHA3-512": "SHA3-512"
147526
+ * OpenSSL-Noble hashes map.
147527
+ * @type {Object.<string, sha1|sha224|sha256|sha384|sha512|sha3_224|sha3_256|sha3_384|sha3_512>}
147528
+ */ const OPENSSL_NOBLE_HASHES = {
147529
+ SHA1: sha1,
147530
+ SHA224: sha224,
147531
+ SHA256: sha256,
147532
+ SHA384: sha384,
147533
+ SHA512: sha512,
147534
+ "SHA3-224": sha3_224,
147535
+ "SHA3-256": sha3_256,
147536
+ "SHA3-384": sha3_384,
147537
+ "SHA3-512": sha3_512
147892
147538
  };
147893
-
147894
147539
  /**
147895
147540
  * Calculates an HMAC digest.
147896
147541
  * In Node.js, the command "openssl list -digest-algorithms" displays the available digest algorithms.
147897
147542
  * @param {string} algorithm Algorithm.
147898
- * @param {ArrayBuffer} key Key.
147899
- * @param {ArrayBuffer} message Message.
147900
- * @returns {ArrayBuffer} Digest.
147901
- */
147902
- const hmacDigest = (algorithm, key, message) => {
147903
- {
147904
- const variant = OPENSSL_JSSHA_ALGO_MAP[algorithm.toUpperCase()];
147905
- if (typeof variant === "undefined") {
147906
- throw new TypeError("Unknown hash function");
147543
+ * @param {Uint8Array} key Key.
147544
+ * @param {Uint8Array} message Message.
147545
+ * @returns {Uint8Array} Digest.
147546
+ */ const hmacDigest = (algorithm, key, message)=>{
147547
+ if (hmac) {
147548
+ const hash = OPENSSL_NOBLE_HASHES[algorithm.toUpperCase()];
147549
+ if (!hash) throw new TypeError("Unknown hash function");
147550
+ return hmac(hash, key, message);
147551
+ } else {
147552
+ throw new Error("Missing HMAC function");
147907
147553
  }
147908
- const hmac = new ht(variant, "ARRAYBUFFER");
147909
- hmac.setHMACKey(key, "ARRAYBUFFER");
147910
- hmac.update(message);
147911
- return hmac.getHMAC("ARRAYBUFFER");
147912
- }
147913
147554
  };
147914
147555
 
147915
147556
  /**
147916
147557
  * RFC 4648 base32 alphabet without pad.
147917
147558
  * @type {string}
147918
- */
147919
- const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
147920
-
147559
+ */ const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
147921
147560
  /**
147922
- * Converts a base32 string to an ArrayBuffer (RFC 4648).
147561
+ * Converts a base32 string to an Uint8Array (RFC 4648).
147923
147562
  * @see [LinusU/base32-decode](https://github.com/LinusU/base32-decode)
147924
147563
  * @param {string} str Base32 string.
147925
- * @returns {ArrayBuffer} ArrayBuffer.
147926
- */
147927
- const base32ToBuf = str => {
147928
- // Canonicalize to all upper case and remove padding if it exists.
147929
- let end = str.length;
147930
- while (str[end - 1] === "=") --end;
147931
- const cstr = (end < str.length ? str.substring(0, end) : str).toUpperCase();
147932
- const buf = new ArrayBuffer(cstr.length * 5 / 8 | 0);
147933
- const arr = new Uint8Array(buf);
147934
- let bits = 0;
147935
- let value = 0;
147936
- let index = 0;
147937
- for (let i = 0; i < cstr.length; i++) {
147938
- const idx = ALPHABET.indexOf(cstr[i]);
147939
- if (idx === -1) throw new TypeError(`Invalid character found: ${cstr[i]}`);
147940
- value = value << 5 | idx;
147941
- bits += 5;
147942
- if (bits >= 8) {
147943
- bits -= 8;
147944
- arr[index++] = value >>> bits;
147564
+ * @returns {Uint8Array} Uint8Array.
147565
+ */ const base32Decode = (str)=>{
147566
+ // Canonicalize to all upper case and remove padding if it exists.
147567
+ let end = str.length;
147568
+ while(str[end - 1] === "=")--end;
147569
+ const cstr = (end < str.length ? str.substring(0, end) : str).toUpperCase();
147570
+ const buf = new ArrayBuffer(cstr.length * 5 / 8 | 0);
147571
+ const arr = new Uint8Array(buf);
147572
+ let bits = 0;
147573
+ let value = 0;
147574
+ let index = 0;
147575
+ for(let i = 0; i < cstr.length; i++){
147576
+ const idx = ALPHABET.indexOf(cstr[i]);
147577
+ if (idx === -1) throw new TypeError(`Invalid character found: ${cstr[i]}`);
147578
+ value = value << 5 | idx;
147579
+ bits += 5;
147580
+ if (bits >= 8) {
147581
+ bits -= 8;
147582
+ arr[index++] = value >>> bits;
147583
+ }
147945
147584
  }
147946
- }
147947
- return buf;
147585
+ return arr;
147948
147586
  };
147949
-
147950
147587
  /**
147951
- * Converts an ArrayBuffer to a base32 string (RFC 4648).
147588
+ * Converts an Uint8Array to a base32 string (RFC 4648).
147952
147589
  * @see [LinusU/base32-encode](https://github.com/LinusU/base32-encode)
147953
- * @param {ArrayBuffer} buf ArrayBuffer.
147590
+ * @param {Uint8Array} arr Uint8Array.
147954
147591
  * @returns {string} Base32 string.
147955
- */
147956
- const base32FromBuf = buf => {
147957
- const arr = new Uint8Array(buf);
147958
- let bits = 0;
147959
- let value = 0;
147960
- let str = "";
147961
- for (let i = 0; i < arr.length; i++) {
147962
- value = value << 8 | arr[i];
147963
- bits += 8;
147964
- while (bits >= 5) {
147965
- str += ALPHABET[value >>> bits - 5 & 31];
147966
- bits -= 5;
147967
- }
147968
- }
147969
- if (bits > 0) {
147970
- str += ALPHABET[value << 5 - bits & 31];
147971
- }
147972
- return str;
147592
+ */ const base32Encode = (arr)=>{
147593
+ let bits = 0;
147594
+ let value = 0;
147595
+ let str = "";
147596
+ for(let i = 0; i < arr.length; i++){
147597
+ value = value << 8 | arr[i];
147598
+ bits += 8;
147599
+ while(bits >= 5){
147600
+ str += ALPHABET[value >>> bits - 5 & 31];
147601
+ bits -= 5;
147602
+ }
147603
+ }
147604
+ if (bits > 0) {
147605
+ str += ALPHABET[value << 5 - bits & 31];
147606
+ }
147607
+ return str;
147973
147608
  };
147974
147609
 
147975
147610
  /**
147976
- * Converts a hexadecimal string to an ArrayBuffer.
147611
+ * Converts a hexadecimal string to an Uint8Array.
147977
147612
  * @param {string} str Hexadecimal string.
147978
- * @returns {ArrayBuffer} ArrayBuffer.
147979
- */
147980
- const hexToBuf = str => {
147981
- const buf = new ArrayBuffer(str.length / 2);
147982
- const arr = new Uint8Array(buf);
147983
- for (let i = 0; i < str.length; i += 2) {
147984
- arr[i / 2] = parseInt(str.substring(i, i + 2), 16);
147985
- }
147986
- return buf;
147613
+ * @returns {Uint8Array} Uint8Array.
147614
+ */ const hexDecode = (str)=>{
147615
+ const buf = new ArrayBuffer(str.length / 2);
147616
+ const arr = new Uint8Array(buf);
147617
+ for(let i = 0; i < str.length; i += 2){
147618
+ arr[i / 2] = parseInt(str.substring(i, i + 2), 16);
147619
+ }
147620
+ return arr;
147987
147621
  };
147988
-
147989
147622
  /**
147990
- * Converts an ArrayBuffer to a hexadecimal string.
147991
- * @param {ArrayBuffer} buf ArrayBuffer.
147623
+ * Converts an Uint8Array to a hexadecimal string.
147624
+ * @param {Uint8Array} arr Uint8Array.
147992
147625
  * @returns {string} Hexadecimal string.
147993
- */
147994
- const hexFromBuf = buf => {
147995
- const arr = new Uint8Array(buf);
147996
- let str = "";
147997
- for (let i = 0; i < arr.length; i++) {
147998
- const hex = arr[i].toString(16);
147999
- if (hex.length === 1) str += "0";
148000
- str += hex;
148001
- }
148002
- return str.toUpperCase();
147626
+ */ const hexEncode = (arr)=>{
147627
+ let str = "";
147628
+ for(let i = 0; i < arr.length; i++){
147629
+ const hex = arr[i].toString(16);
147630
+ if (hex.length === 1) str += "0";
147631
+ str += hex;
147632
+ }
147633
+ return str.toUpperCase();
148003
147634
  };
148004
147635
 
148005
147636
  /**
148006
- * Converts a Latin-1 string to an ArrayBuffer.
147637
+ * Converts a Latin-1 string to an Uint8Array.
148007
147638
  * @param {string} str Latin-1 string.
148008
- * @returns {ArrayBuffer} ArrayBuffer.
148009
- */
148010
- const latin1ToBuf = str => {
148011
- const buf = new ArrayBuffer(str.length);
148012
- const arr = new Uint8Array(buf);
148013
- for (let i = 0; i < str.length; i++) {
148014
- arr[i] = str.charCodeAt(i) & 0xff;
148015
- }
148016
- return buf;
147639
+ * @returns {Uint8Array} Uint8Array.
147640
+ */ const latin1Decode = (str)=>{
147641
+ const buf = new ArrayBuffer(str.length);
147642
+ const arr = new Uint8Array(buf);
147643
+ for(let i = 0; i < str.length; i++){
147644
+ arr[i] = str.charCodeAt(i) & 0xff;
147645
+ }
147646
+ return arr;
148017
147647
  };
148018
-
148019
147648
  /**
148020
- * Converts an ArrayBuffer to a Latin-1 string.
148021
- * @param {ArrayBuffer} buf ArrayBuffer.
147649
+ * Converts an Uint8Array to a Latin-1 string.
147650
+ * @param {Uint8Array} arr Uint8Array.
148022
147651
  * @returns {string} Latin-1 string.
148023
- */
148024
- const latin1FromBuf = buf => {
148025
- const arr = new Uint8Array(buf);
148026
- let str = "";
148027
- for (let i = 0; i < arr.length; i++) {
148028
- str += String.fromCharCode(arr[i]);
148029
- }
148030
- return str;
147652
+ */ const latin1Encode = (arr)=>{
147653
+ let str = "";
147654
+ for(let i = 0; i < arr.length; i++){
147655
+ str += String.fromCharCode(arr[i]);
147656
+ }
147657
+ return str;
148031
147658
  };
148032
147659
 
148033
147660
  /**
148034
147661
  * TextEncoder instance.
148035
147662
  * @type {TextEncoder|null}
148036
- */
148037
- const ENCODER = globalScope.TextEncoder ? new globalScope.TextEncoder("utf-8") : null;
148038
-
147663
+ */ const ENCODER = globalScope.TextEncoder ? new globalScope.TextEncoder() : null;
148039
147664
  /**
148040
147665
  * TextDecoder instance.
148041
147666
  * @type {TextDecoder|null}
148042
- */
148043
- const DECODER = globalScope.TextDecoder ? new globalScope.TextDecoder("utf-8") : null;
148044
-
147667
+ */ const DECODER = globalScope.TextDecoder ? new globalScope.TextDecoder() : null;
148045
147668
  /**
148046
- * Converts an UTF-8 string to an ArrayBuffer.
147669
+ * Converts an UTF-8 string to an Uint8Array.
148047
147670
  * @param {string} str String.
148048
- * @returns {ArrayBuffer} ArrayBuffer.
148049
- */
148050
- const utf8ToBuf = str => {
148051
- if (!ENCODER) {
148052
- throw new Error("Encoding API not available");
148053
- }
148054
- return ENCODER.encode(str).buffer;
147671
+ * @returns {Uint8Array} Uint8Array.
147672
+ */ const utf8Decode = (str)=>{
147673
+ if (!ENCODER) {
147674
+ throw new Error("Encoding API not available");
147675
+ }
147676
+ return ENCODER.encode(str);
148055
147677
  };
148056
-
148057
147678
  /**
148058
- * Converts an ArrayBuffer to an UTF-8 string.
148059
- * @param {ArrayBuffer} buf ArrayBuffer.
147679
+ * Converts an Uint8Array to an UTF-8 string.
147680
+ * @param {Uint8Array} arr Uint8Array.
148060
147681
  * @returns {string} String.
148061
- */
148062
- const utf8FromBuf = buf => {
148063
- if (!DECODER) {
148064
- throw new Error("Encoding API not available");
148065
- }
148066
- return DECODER.decode(buf);
147682
+ */ const utf8Encode = (arr)=>{
147683
+ if (!DECODER) {
147684
+ throw new Error("Encoding API not available");
147685
+ }
147686
+ return DECODER.decode(arr);
148067
147687
  };
148068
147688
 
148069
147689
  /**
148070
147690
  * Returns random bytes.
148071
147691
  * @param {number} size Size.
148072
- * @returns {ArrayBuffer} Random bytes.
148073
- */
148074
- const randomBytes = size => {
148075
- {
148076
- if (!globalScope.crypto?.getRandomValues) {
148077
- throw new Error("Cryptography API not available");
147692
+ * @returns {Uint8Array} Random bytes.
147693
+ */ const randomBytes = (size)=>{
147694
+ {
147695
+ if (!globalScope.crypto?.getRandomValues) {
147696
+ throw new Error("Cryptography API not available");
147697
+ }
147698
+ return globalScope.crypto.getRandomValues(new Uint8Array(size));
148078
147699
  }
148079
- return globalScope.crypto.getRandomValues(new Uint8Array(size)).buffer;
148080
- }
148081
147700
  };
148082
147701
 
148083
147702
  /**
148084
147703
  * OTP secret key.
148085
- */
148086
- class Secret {
148087
- /**
148088
- * Creates a secret key object.
148089
- * @param {Object} [config] Configuration options.
148090
- * @param {ArrayBuffer} [config.buffer=randomBytes] Secret key.
148091
- * @param {number} [config.size=20] Number of random bytes to generate, ignored if 'buffer' is provided.
148092
- */
148093
- constructor({
148094
- buffer,
148095
- size = 20
148096
- } = {}) {
147704
+ */ class Secret {
148097
147705
  /**
148098
- * Secret key.
148099
- * @type {ArrayBuffer}
148100
- */
148101
- this.buffer = typeof buffer === "undefined" ? randomBytes(size) : buffer;
148102
- }
148103
-
148104
- /**
148105
147706
  * Converts a Latin-1 string to a Secret object.
148106
147707
  * @param {string} str Latin-1 string.
148107
147708
  * @returns {Secret} Secret object.
148108
- */
148109
- static fromLatin1(str) {
148110
- return new Secret({
148111
- buffer: latin1ToBuf(str)
148112
- });
148113
- }
148114
-
148115
- /**
147709
+ */ static fromLatin1(str) {
147710
+ return new Secret({
147711
+ buffer: latin1Decode(str).buffer
147712
+ });
147713
+ }
147714
+ /**
148116
147715
  * Converts an UTF-8 string to a Secret object.
148117
147716
  * @param {string} str UTF-8 string.
148118
147717
  * @returns {Secret} Secret object.
148119
- */
148120
- static fromUTF8(str) {
148121
- return new Secret({
148122
- buffer: utf8ToBuf(str)
148123
- });
148124
- }
148125
-
148126
- /**
147718
+ */ static fromUTF8(str) {
147719
+ return new Secret({
147720
+ buffer: utf8Decode(str).buffer
147721
+ });
147722
+ }
147723
+ /**
148127
147724
  * Converts a base32 string to a Secret object.
148128
147725
  * @param {string} str Base32 string.
148129
147726
  * @returns {Secret} Secret object.
148130
- */
148131
- static fromBase32(str) {
148132
- return new Secret({
148133
- buffer: base32ToBuf(str)
148134
- });
148135
- }
148136
-
148137
- /**
147727
+ */ static fromBase32(str) {
147728
+ return new Secret({
147729
+ buffer: base32Decode(str).buffer
147730
+ });
147731
+ }
147732
+ /**
148138
147733
  * Converts a hexadecimal string to a Secret object.
148139
147734
  * @param {string} str Hexadecimal string.
148140
147735
  * @returns {Secret} Secret object.
148141
- */
148142
- static fromHex(str) {
148143
- return new Secret({
148144
- buffer: hexToBuf(str)
148145
- });
148146
- }
148147
-
148148
- /**
147736
+ */ static fromHex(str) {
147737
+ return new Secret({
147738
+ buffer: hexDecode(str).buffer
147739
+ });
147740
+ }
147741
+ /**
147742
+ * Secret key buffer.
147743
+ * @deprecated For backward compatibility, the "bytes" property should be used instead.
147744
+ * @type {ArrayBufferLike}
147745
+ */ get buffer() {
147746
+ return this.bytes.buffer;
147747
+ }
147748
+ /**
148149
147749
  * Latin-1 string representation of secret key.
148150
147750
  * @type {string}
148151
- */
148152
- get latin1() {
148153
- Object.defineProperty(this, "latin1", {
148154
- enumerable: true,
148155
- value: latin1FromBuf(this.buffer)
148156
- });
148157
- return this.latin1;
148158
- }
148159
-
148160
- /**
147751
+ */ get latin1() {
147752
+ Object.defineProperty(this, "latin1", {
147753
+ enumerable: true,
147754
+ writable: false,
147755
+ configurable: false,
147756
+ value: latin1Encode(this.bytes)
147757
+ });
147758
+ return this.latin1;
147759
+ }
147760
+ /**
148161
147761
  * UTF-8 string representation of secret key.
148162
147762
  * @type {string}
148163
- */
148164
- get utf8() {
148165
- Object.defineProperty(this, "utf8", {
148166
- enumerable: true,
148167
- value: utf8FromBuf(this.buffer)
148168
- });
148169
- return this.utf8;
148170
- }
148171
-
148172
- /**
147763
+ */ get utf8() {
147764
+ Object.defineProperty(this, "utf8", {
147765
+ enumerable: true,
147766
+ writable: false,
147767
+ configurable: false,
147768
+ value: utf8Encode(this.bytes)
147769
+ });
147770
+ return this.utf8;
147771
+ }
147772
+ /**
148173
147773
  * Base32 string representation of secret key.
148174
147774
  * @type {string}
148175
- */
148176
- get base32() {
148177
- Object.defineProperty(this, "base32", {
148178
- enumerable: true,
148179
- value: base32FromBuf(this.buffer)
148180
- });
148181
- return this.base32;
148182
- }
148183
-
148184
- /**
147775
+ */ get base32() {
147776
+ Object.defineProperty(this, "base32", {
147777
+ enumerable: true,
147778
+ writable: false,
147779
+ configurable: false,
147780
+ value: base32Encode(this.bytes)
147781
+ });
147782
+ return this.base32;
147783
+ }
147784
+ /**
148185
147785
  * Hexadecimal string representation of secret key.
148186
147786
  * @type {string}
148187
- */
148188
- get hex() {
148189
- Object.defineProperty(this, "hex", {
148190
- enumerable: true,
148191
- value: hexFromBuf(this.buffer)
148192
- });
148193
- return this.hex;
148194
- }
147787
+ */ get hex() {
147788
+ Object.defineProperty(this, "hex", {
147789
+ enumerable: true,
147790
+ writable: false,
147791
+ configurable: false,
147792
+ value: hexEncode(this.bytes)
147793
+ });
147794
+ return this.hex;
147795
+ }
147796
+ /**
147797
+ * Creates a secret key object.
147798
+ * @param {Object} [config] Configuration options.
147799
+ * @param {ArrayBufferLike} [config.buffer] Secret key buffer.
147800
+ * @param {number} [config.size=20] Number of random bytes to generate, ignored if 'buffer' is provided.
147801
+ */ constructor({ buffer, size = 20 } = {}){
147802
+ /**
147803
+ * Secret key.
147804
+ * @type {Uint8Array}
147805
+ * @readonly
147806
+ */ this.bytes = typeof buffer === "undefined" ? randomBytes(size) : new Uint8Array(buffer);
147807
+ // Prevent the "bytes" property from being modified.
147808
+ Object.defineProperty(this, "bytes", {
147809
+ enumerable: true,
147810
+ writable: false,
147811
+ configurable: false,
147812
+ value: this.bytes
147813
+ });
147814
+ }
148195
147815
  }
148196
147816
 
148197
147817
  /**
@@ -148199,27 +147819,25 @@ class Secret {
148199
147819
  * @param {string} a String a.
148200
147820
  * @param {string} b String b.
148201
147821
  * @returns {boolean} Equality result.
148202
- */
148203
- const timingSafeEqual = (a, b) => {
148204
- {
148205
- if (a.length !== b.length) {
148206
- throw new TypeError("Input strings must have the same length");
148207
- }
148208
- let i = -1;
148209
- let out = 0;
148210
- while (++i < a.length) {
148211
- out |= a.charCodeAt(i) ^ b.charCodeAt(i);
147822
+ */ const timingSafeEqual = (a, b)=>{
147823
+ {
147824
+ if (a.length !== b.length) {
147825
+ throw new TypeError("Input strings must have the same length");
147826
+ }
147827
+ let i = -1;
147828
+ let out = 0;
147829
+ while(++i < a.length){
147830
+ out |= a.charCodeAt(i) ^ b.charCodeAt(i);
147831
+ }
147832
+ return out === 0;
148212
147833
  }
148213
- return out === 0;
148214
- }
148215
147834
  };
148216
147835
 
148217
147836
  /**
148218
147837
  * HOTP: An HMAC-based One-time Password Algorithm.
148219
147838
  * @see [RFC 4226](https://tools.ietf.org/html/rfc4226)
148220
- */
148221
- class HOTP {
148222
- /**
147839
+ */ class HOTP {
147840
+ /**
148223
147841
  * Default configuration.
148224
147842
  * @type {{
148225
147843
  * issuer: string,
@@ -148230,77 +147848,18 @@ class HOTP {
148230
147848
  * counter: number
148231
147849
  * window: number
148232
147850
  * }}
148233
- */
148234
- static get defaults() {
148235
- return {
148236
- issuer: "",
148237
- label: "OTPAuth",
148238
- issuerInLabel: true,
148239
- algorithm: "SHA1",
148240
- digits: 6,
148241
- counter: 0,
148242
- window: 1
148243
- };
148244
- }
148245
-
148246
- /**
148247
- * Creates an HOTP object.
148248
- * @param {Object} [config] Configuration options.
148249
- * @param {string} [config.issuer=''] Account provider.
148250
- * @param {string} [config.label='OTPAuth'] Account label.
148251
- * @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
148252
- * @param {Secret|string} [config.secret=Secret] Secret key.
148253
- * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
148254
- * @param {number} [config.digits=6] Token length.
148255
- * @param {number} [config.counter=0] Initial counter value.
148256
- */
148257
- constructor({
148258
- issuer = HOTP.defaults.issuer,
148259
- label = HOTP.defaults.label,
148260
- issuerInLabel = HOTP.defaults.issuerInLabel,
148261
- secret = new Secret(),
148262
- algorithm = HOTP.defaults.algorithm,
148263
- digits = HOTP.defaults.digits,
148264
- counter = HOTP.defaults.counter
148265
- } = {}) {
148266
- /**
148267
- * Account provider.
148268
- * @type {string}
148269
- */
148270
- this.issuer = issuer;
148271
- /**
148272
- * Account label.
148273
- * @type {string}
148274
- */
148275
- this.label = label;
148276
- /**
148277
- * Include issuer prefix in label.
148278
- * @type {boolean}
148279
- */
148280
- this.issuerInLabel = issuerInLabel;
148281
- /**
148282
- * Secret key.
148283
- * @type {Secret}
148284
- */
148285
- this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
148286
- /**
148287
- * HMAC hashing algorithm.
148288
- * @type {string}
148289
- */
148290
- this.algorithm = algorithm.toUpperCase();
148291
- /**
148292
- * Token length.
148293
- * @type {number}
148294
- */
148295
- this.digits = digits;
147851
+ */ static get defaults() {
147852
+ return {
147853
+ issuer: "",
147854
+ label: "OTPAuth",
147855
+ issuerInLabel: true,
147856
+ algorithm: "SHA1",
147857
+ digits: 6,
147858
+ counter: 0,
147859
+ window: 1
147860
+ };
147861
+ }
148296
147862
  /**
148297
- * Initial counter value.
148298
- * @type {number}
148299
- */
148300
- this.counter = counter;
148301
- }
148302
-
148303
- /**
148304
147863
  * Generates an HOTP token.
148305
147864
  * @param {Object} config Configuration options.
148306
147865
  * @param {Secret} config.secret Secret key.
@@ -148308,37 +147867,26 @@ class HOTP {
148308
147867
  * @param {number} [config.digits=6] Token length.
148309
147868
  * @param {number} [config.counter=0] Counter value.
148310
147869
  * @returns {string} Token.
148311
- */
148312
- static generate({
148313
- secret,
148314
- algorithm = HOTP.defaults.algorithm,
148315
- digits = HOTP.defaults.digits,
148316
- counter = HOTP.defaults.counter
148317
- }) {
148318
- const digest = new Uint8Array(hmacDigest(algorithm, secret.buffer, uintToBuf(counter)));
148319
- const offset = digest[digest.byteLength - 1] & 15;
148320
- const otp = ((digest[offset] & 127) << 24 | (digest[offset + 1] & 255) << 16 | (digest[offset + 2] & 255) << 8 | digest[offset + 3] & 255) % 10 ** digits;
148321
- return otp.toString().padStart(digits, "0");
148322
- }
148323
-
148324
- /**
147870
+ */ static generate({ secret, algorithm = HOTP.defaults.algorithm, digits = HOTP.defaults.digits, counter = HOTP.defaults.counter }) {
147871
+ const digest = hmacDigest(algorithm, secret.bytes, uintDecode(counter));
147872
+ const offset = digest[digest.byteLength - 1] & 15;
147873
+ const otp = ((digest[offset] & 127) << 24 | (digest[offset + 1] & 255) << 16 | (digest[offset + 2] & 255) << 8 | digest[offset + 3] & 255) % 10 ** digits;
147874
+ return otp.toString().padStart(digits, "0");
147875
+ }
147876
+ /**
148325
147877
  * Generates an HOTP token.
148326
147878
  * @param {Object} [config] Configuration options.
148327
147879
  * @param {number} [config.counter=this.counter++] Counter value.
148328
147880
  * @returns {string} Token.
148329
- */
148330
- generate({
148331
- counter = this.counter++
148332
- } = {}) {
148333
- return HOTP.generate({
148334
- secret: this.secret,
148335
- algorithm: this.algorithm,
148336
- digits: this.digits,
148337
- counter
148338
- });
148339
- }
148340
-
148341
- /**
147881
+ */ generate({ counter = this.counter++ } = {}) {
147882
+ return HOTP.generate({
147883
+ secret: this.secret,
147884
+ algorithm: this.algorithm,
147885
+ digits: this.digits,
147886
+ counter
147887
+ });
147888
+ }
147889
+ /**
148342
147890
  * Validates an HOTP token.
148343
147891
  * @param {Object} config Configuration options.
148344
147892
  * @param {string} config.token Token value.
@@ -148348,103 +147896,56 @@ class HOTP {
148348
147896
  * @param {number} [config.counter=0] Counter value.
148349
147897
  * @param {number} [config.window=1] Window of counter values to test.
148350
147898
  * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
148351
- */
148352
- static validate({
148353
- token,
148354
- secret,
148355
- algorithm,
148356
- digits,
148357
- counter = HOTP.defaults.counter,
148358
- window = HOTP.defaults.window
148359
- }) {
148360
- // Return early if the token length does not match the digit number.
148361
- if (token.length !== digits) return null;
148362
- let delta = null;
148363
- const check = ( /** @type {number} */i) => {
148364
- const generatedToken = HOTP.generate({
148365
- secret,
148366
- algorithm,
148367
- digits,
148368
- counter: i
148369
- });
148370
- if (timingSafeEqual(token, generatedToken)) {
148371
- delta = i - counter;
148372
- }
148373
- };
148374
- check(counter);
148375
- for (let i = 1; i <= window && delta === null; ++i) {
148376
- check(counter - i);
148377
- if (delta !== null) break;
148378
- check(counter + i);
148379
- if (delta !== null) break;
147899
+ */ static validate({ token, secret, algorithm, digits, counter = HOTP.defaults.counter, window = HOTP.defaults.window }) {
147900
+ // Return early if the token length does not match the digit number.
147901
+ if (token.length !== digits) return null;
147902
+ let delta = null;
147903
+ const check = (/** @type {number} */ i)=>{
147904
+ const generatedToken = HOTP.generate({
147905
+ secret,
147906
+ algorithm,
147907
+ digits,
147908
+ counter: i
147909
+ });
147910
+ if (timingSafeEqual(token, generatedToken)) {
147911
+ delta = i - counter;
147912
+ }
147913
+ };
147914
+ check(counter);
147915
+ for(let i = 1; i <= window && delta === null; ++i){
147916
+ check(counter - i);
147917
+ if (delta !== null) break;
147918
+ check(counter + i);
147919
+ if (delta !== null) break;
147920
+ }
147921
+ return delta;
148380
147922
  }
148381
- return delta;
148382
- }
148383
-
148384
- /**
147923
+ /**
148385
147924
  * Validates an HOTP token.
148386
147925
  * @param {Object} config Configuration options.
148387
147926
  * @param {string} config.token Token value.
148388
147927
  * @param {number} [config.counter=this.counter] Counter value.
148389
147928
  * @param {number} [config.window=1] Window of counter values to test.
148390
147929
  * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
148391
- */
148392
- validate({
148393
- token,
148394
- counter = this.counter,
148395
- window
148396
- }) {
148397
- return HOTP.validate({
148398
- token,
148399
- secret: this.secret,
148400
- algorithm: this.algorithm,
148401
- digits: this.digits,
148402
- counter,
148403
- window
148404
- });
148405
- }
148406
-
148407
- /**
147930
+ */ validate({ token, counter = this.counter, window }) {
147931
+ return HOTP.validate({
147932
+ token,
147933
+ secret: this.secret,
147934
+ algorithm: this.algorithm,
147935
+ digits: this.digits,
147936
+ counter,
147937
+ window
147938
+ });
147939
+ }
147940
+ /**
148408
147941
  * Returns a Google Authenticator key URI.
148409
147942
  * @returns {string} URI.
148410
- */
148411
- toString() {
148412
- const e = encodeURIComponent;
148413
- return "otpauth://hotp/" + `${this.issuer.length > 0 ? this.issuerInLabel ? `${e(this.issuer)}:${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?`}` + `secret=${e(this.secret.base32)}&` + `algorithm=${e(this.algorithm)}&` + `digits=${e(this.digits)}&` + `counter=${e(this.counter)}`;
148414
- }
148415
- }
148416
-
148417
- /**
148418
- * TOTP: Time-Based One-Time Password Algorithm.
148419
- * @see [RFC 6238](https://tools.ietf.org/html/rfc6238)
148420
- */
148421
- class TOTP {
148422
- /**
148423
- * Default configuration.
148424
- * @type {{
148425
- * issuer: string,
148426
- * label: string,
148427
- * issuerInLabel: boolean,
148428
- * algorithm: string,
148429
- * digits: number,
148430
- * period: number
148431
- * window: number
148432
- * }}
148433
- */
148434
- static get defaults() {
148435
- return {
148436
- issuer: "",
148437
- label: "OTPAuth",
148438
- issuerInLabel: true,
148439
- algorithm: "SHA1",
148440
- digits: 6,
148441
- period: 30,
148442
- window: 1
148443
- };
148444
- }
148445
-
148446
- /**
148447
- * Creates a TOTP object.
147943
+ */ toString() {
147944
+ const e = encodeURIComponent;
147945
+ return "otpauth://hotp/" + `${this.issuer.length > 0 ? this.issuerInLabel ? `${e(this.issuer)}:${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?`}` + `secret=${e(this.secret.base32)}&` + `algorithm=${e(this.algorithm)}&` + `digits=${e(this.digits)}&` + `counter=${e(this.counter)}`;
147946
+ }
147947
+ /**
147948
+ * Creates an HOTP object.
148448
147949
  * @param {Object} [config] Configuration options.
148449
147950
  * @param {string} [config.issuer=''] Account provider.
148450
147951
  * @param {string} [config.label='OTPAuth'] Account label.
@@ -148452,55 +147953,66 @@ class TOTP {
148452
147953
  * @param {Secret|string} [config.secret=Secret] Secret key.
148453
147954
  * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
148454
147955
  * @param {number} [config.digits=6] Token length.
148455
- * @param {number} [config.period=30] Token time-step duration.
148456
- */
148457
- constructor({
148458
- issuer = TOTP.defaults.issuer,
148459
- label = TOTP.defaults.label,
148460
- issuerInLabel = TOTP.defaults.issuerInLabel,
148461
- secret = new Secret(),
148462
- algorithm = TOTP.defaults.algorithm,
148463
- digits = TOTP.defaults.digits,
148464
- period = TOTP.defaults.period
148465
- } = {}) {
148466
- /**
147956
+ * @param {number} [config.counter=0] Initial counter value.
147957
+ */ constructor({ issuer = HOTP.defaults.issuer, label = HOTP.defaults.label, issuerInLabel = HOTP.defaults.issuerInLabel, secret = new Secret(), algorithm = HOTP.defaults.algorithm, digits = HOTP.defaults.digits, counter = HOTP.defaults.counter } = {}){
147958
+ /**
148467
147959
  * Account provider.
148468
147960
  * @type {string}
148469
- */
148470
- this.issuer = issuer;
148471
- /**
147961
+ */ this.issuer = issuer;
147962
+ /**
148472
147963
  * Account label.
148473
147964
  * @type {string}
148474
- */
148475
- this.label = label;
148476
- /**
147965
+ */ this.label = label;
147966
+ /**
148477
147967
  * Include issuer prefix in label.
148478
147968
  * @type {boolean}
148479
- */
148480
- this.issuerInLabel = issuerInLabel;
148481
- /**
147969
+ */ this.issuerInLabel = issuerInLabel;
147970
+ /**
148482
147971
  * Secret key.
148483
147972
  * @type {Secret}
148484
- */
148485
- this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
148486
- /**
147973
+ */ this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
147974
+ /**
148487
147975
  * HMAC hashing algorithm.
148488
147976
  * @type {string}
148489
- */
148490
- this.algorithm = algorithm.toUpperCase();
148491
- /**
147977
+ */ this.algorithm = algorithm.toUpperCase();
147978
+ /**
148492
147979
  * Token length.
148493
147980
  * @type {number}
148494
- */
148495
- this.digits = digits;
148496
- /**
148497
- * Token time-step duration.
147981
+ */ this.digits = digits;
147982
+ /**
147983
+ * Initial counter value.
148498
147984
  * @type {number}
148499
- */
148500
- this.period = period;
148501
- }
147985
+ */ this.counter = counter;
147986
+ }
147987
+ }
148502
147988
 
148503
- /**
147989
+ /**
147990
+ * TOTP: Time-Based One-Time Password Algorithm.
147991
+ * @see [RFC 6238](https://tools.ietf.org/html/rfc6238)
147992
+ */ class TOTP {
147993
+ /**
147994
+ * Default configuration.
147995
+ * @type {{
147996
+ * issuer: string,
147997
+ * label: string,
147998
+ * issuerInLabel: boolean,
147999
+ * algorithm: string,
148000
+ * digits: number,
148001
+ * period: number
148002
+ * window: number
148003
+ * }}
148004
+ */ static get defaults() {
148005
+ return {
148006
+ issuer: "",
148007
+ label: "OTPAuth",
148008
+ issuerInLabel: true,
148009
+ algorithm: "SHA1",
148010
+ digits: 6,
148011
+ period: 30,
148012
+ window: 1
148013
+ };
148014
+ }
148015
+ /**
148504
148016
  * Generates a TOTP token.
148505
148017
  * @param {Object} config Configuration options.
148506
148018
  * @param {Secret} config.secret Secret key.
@@ -148509,41 +148021,29 @@ class TOTP {
148509
148021
  * @param {number} [config.period=30] Token time-step duration.
148510
148022
  * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
148511
148023
  * @returns {string} Token.
148512
- */
148513
- static generate({
148514
- secret,
148515
- algorithm,
148516
- digits,
148517
- period = TOTP.defaults.period,
148518
- timestamp = Date.now()
148519
- }) {
148520
- return HOTP.generate({
148521
- secret,
148522
- algorithm,
148523
- digits,
148524
- counter: Math.floor(timestamp / 1000 / period)
148525
- });
148526
- }
148527
-
148528
- /**
148024
+ */ static generate({ secret, algorithm, digits, period = TOTP.defaults.period, timestamp = Date.now() }) {
148025
+ return HOTP.generate({
148026
+ secret,
148027
+ algorithm,
148028
+ digits,
148029
+ counter: Math.floor(timestamp / 1000 / period)
148030
+ });
148031
+ }
148032
+ /**
148529
148033
  * Generates a TOTP token.
148530
148034
  * @param {Object} [config] Configuration options.
148531
148035
  * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
148532
148036
  * @returns {string} Token.
148533
- */
148534
- generate({
148535
- timestamp = Date.now()
148536
- } = {}) {
148537
- return TOTP.generate({
148538
- secret: this.secret,
148539
- algorithm: this.algorithm,
148540
- digits: this.digits,
148541
- period: this.period,
148542
- timestamp
148543
- });
148544
- }
148545
-
148546
- /**
148037
+ */ generate({ timestamp = Date.now() } = {}) {
148038
+ return TOTP.generate({
148039
+ secret: this.secret,
148040
+ algorithm: this.algorithm,
148041
+ digits: this.digits,
148042
+ period: this.period,
148043
+ timestamp
148044
+ });
148045
+ }
148046
+ /**
148547
148047
  * Validates a TOTP token.
148548
148048
  * @param {Object} config Configuration options.
148549
148049
  * @param {string} config.token Token value.
@@ -148554,58 +148054,81 @@ class TOTP {
148554
148054
  * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
148555
148055
  * @param {number} [config.window=1] Window of counter values to test.
148556
148056
  * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
148557
- */
148558
- static validate({
148559
- token,
148560
- secret,
148561
- algorithm,
148562
- digits,
148563
- period = TOTP.defaults.period,
148564
- timestamp = Date.now(),
148565
- window
148566
- }) {
148567
- return HOTP.validate({
148568
- token,
148569
- secret,
148570
- algorithm,
148571
- digits,
148572
- counter: Math.floor(timestamp / 1000 / period),
148573
- window
148574
- });
148575
- }
148576
-
148577
- /**
148057
+ */ static validate({ token, secret, algorithm, digits, period = TOTP.defaults.period, timestamp = Date.now(), window }) {
148058
+ return HOTP.validate({
148059
+ token,
148060
+ secret,
148061
+ algorithm,
148062
+ digits,
148063
+ counter: Math.floor(timestamp / 1000 / period),
148064
+ window
148065
+ });
148066
+ }
148067
+ /**
148578
148068
  * Validates a TOTP token.
148579
148069
  * @param {Object} config Configuration options.
148580
148070
  * @param {string} config.token Token value.
148581
148071
  * @param {number} [config.timestamp=Date.now] Timestamp value in milliseconds.
148582
148072
  * @param {number} [config.window=1] Window of counter values to test.
148583
148073
  * @returns {number|null} Token delta or null if it is not found in the search window, in which case it should be considered invalid.
148584
- */
148585
- validate({
148586
- token,
148587
- timestamp,
148588
- window
148589
- }) {
148590
- return TOTP.validate({
148591
- token,
148592
- secret: this.secret,
148593
- algorithm: this.algorithm,
148594
- digits: this.digits,
148595
- period: this.period,
148596
- timestamp,
148597
- window
148598
- });
148599
- }
148600
-
148601
- /**
148074
+ */ validate({ token, timestamp, window }) {
148075
+ return TOTP.validate({
148076
+ token,
148077
+ secret: this.secret,
148078
+ algorithm: this.algorithm,
148079
+ digits: this.digits,
148080
+ period: this.period,
148081
+ timestamp,
148082
+ window
148083
+ });
148084
+ }
148085
+ /**
148602
148086
  * Returns a Google Authenticator key URI.
148603
148087
  * @returns {string} URI.
148604
- */
148605
- toString() {
148606
- const e = encodeURIComponent;
148607
- return "otpauth://totp/" + `${this.issuer.length > 0 ? this.issuerInLabel ? `${e(this.issuer)}:${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?`}` + `secret=${e(this.secret.base32)}&` + `algorithm=${e(this.algorithm)}&` + `digits=${e(this.digits)}&` + `period=${e(this.period)}`;
148608
- }
148088
+ */ toString() {
148089
+ const e = encodeURIComponent;
148090
+ return "otpauth://totp/" + `${this.issuer.length > 0 ? this.issuerInLabel ? `${e(this.issuer)}:${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?issuer=${e(this.issuer)}&` : `${e(this.label)}?`}` + `secret=${e(this.secret.base32)}&` + `algorithm=${e(this.algorithm)}&` + `digits=${e(this.digits)}&` + `period=${e(this.period)}`;
148091
+ }
148092
+ /**
148093
+ * Creates a TOTP object.
148094
+ * @param {Object} [config] Configuration options.
148095
+ * @param {string} [config.issuer=''] Account provider.
148096
+ * @param {string} [config.label='OTPAuth'] Account label.
148097
+ * @param {boolean} [config.issuerInLabel=true] Include issuer prefix in label.
148098
+ * @param {Secret|string} [config.secret=Secret] Secret key.
148099
+ * @param {string} [config.algorithm='SHA1'] HMAC hashing algorithm.
148100
+ * @param {number} [config.digits=6] Token length.
148101
+ * @param {number} [config.period=30] Token time-step duration.
148102
+ */ constructor({ issuer = TOTP.defaults.issuer, label = TOTP.defaults.label, issuerInLabel = TOTP.defaults.issuerInLabel, secret = new Secret(), algorithm = TOTP.defaults.algorithm, digits = TOTP.defaults.digits, period = TOTP.defaults.period } = {}){
148103
+ /**
148104
+ * Account provider.
148105
+ * @type {string}
148106
+ */ this.issuer = issuer;
148107
+ /**
148108
+ * Account label.
148109
+ * @type {string}
148110
+ */ this.label = label;
148111
+ /**
148112
+ * Include issuer prefix in label.
148113
+ * @type {boolean}
148114
+ */ this.issuerInLabel = issuerInLabel;
148115
+ /**
148116
+ * Secret key.
148117
+ * @type {Secret}
148118
+ */ this.secret = typeof secret === "string" ? Secret.fromBase32(secret) : secret;
148119
+ /**
148120
+ * HMAC hashing algorithm.
148121
+ * @type {string}
148122
+ */ this.algorithm = algorithm.toUpperCase();
148123
+ /**
148124
+ * Token length.
148125
+ * @type {number}
148126
+ */ this.digits = digits;
148127
+ /**
148128
+ * Token time-step duration.
148129
+ * @type {number}
148130
+ */ this.period = period;
148131
+ }
148609
148132
  }
148610
148133
 
148611
148134
  const initializeTotp = ({ issuer, secret, }) => {