import { __commonJS } from "../../../_virtual/rolldown_runtime.mjs";
import { require_forge } from "./forge.mjs";
import { require_util } from "./util.mjs";
import { require_md } from "./md.mjs";
import { require_hmac } from "./hmac.mjs";
import { require_lib } from "../lib.mjs";

//#region ../../node_modules/node-forge/lib/pbkdf2.js
var require_pbkdf2 = /* @__PURE__ */ __commonJS({ "../../node_modules/node-forge/lib/pbkdf2.js": ((exports, module) => {
	/**
	* Password-Based Key-Derivation Function #2 implementation.
	*
	* See RFC 2898 for details.
	*
	* @author Dave Longley
	*
	* Copyright (c) 2010-2013 Digital Bazaar, Inc.
	*/
	var forge = require_forge();
	require_hmac();
	require_md();
	require_util();
	var pkcs5 = forge.pkcs5 = forge.pkcs5 || {};
	var crypto;
	if (forge.util.isNodejs && !forge.options.usePureJavaScript) crypto = require_lib();
	/**
	* Derives a key from a password.
	*
	* @param p the password as a binary-encoded string of bytes.
	* @param s the salt as a binary-encoded string of bytes.
	* @param c the iteration count, a positive integer.
	* @param dkLen the intended length, in bytes, of the derived key,
	*          (max: 2^32 - 1) * hash length of the PRF.
	* @param [md] the message digest (or algorithm identifier as a string) to use
	*          in the PRF, defaults to SHA-1.
	* @param [callback(err, key)] presence triggers asynchronous version, called
	*          once the operation completes.
	*
	* @return the derived key, as a binary-encoded string of bytes, for the
	*           synchronous version (if no callback is specified).
	*/
	module.exports = forge.pbkdf2 = pkcs5.pbkdf2 = function(p, s, c, dkLen, md, callback) {
		if (typeof md === "function") {
			callback = md;
			md = null;
		}
		if (forge.util.isNodejs && !forge.options.usePureJavaScript && crypto.pbkdf2 && (md === null || typeof md !== "object") && (crypto.pbkdf2Sync.length > 4 || !md || md === "sha1")) {
			if (typeof md !== "string") md = "sha1";
			p = Buffer.from(p, "binary");
			s = Buffer.from(s, "binary");
			if (!callback) {
				if (crypto.pbkdf2Sync.length === 4) return crypto.pbkdf2Sync(p, s, c, dkLen).toString("binary");
				return crypto.pbkdf2Sync(p, s, c, dkLen, md).toString("binary");
			}
			if (crypto.pbkdf2Sync.length === 4) return crypto.pbkdf2(p, s, c, dkLen, function(err$1, key) {
				if (err$1) return callback(err$1);
				callback(null, key.toString("binary"));
			});
			return crypto.pbkdf2(p, s, c, dkLen, md, function(err$1, key) {
				if (err$1) return callback(err$1);
				callback(null, key.toString("binary"));
			});
		}
		if (typeof md === "undefined" || md === null) md = "sha1";
		if (typeof md === "string") {
			if (!(md in forge.md.algorithms)) throw new Error("Unknown hash algorithm: " + md);
			md = forge.md[md].create();
		}
		var hLen = md.digestLength;
		if (dkLen > 4294967295 * hLen) {
			var err = /* @__PURE__ */ new Error("Derived key is too long.");
			if (callback) return callback(err);
			throw err;
		}
		var len = Math.ceil(dkLen / hLen);
		var r = dkLen - (len - 1) * hLen;
		var prf = forge.hmac.create();
		prf.start(md, p);
		var dk = "";
		var xor, u_c, u_c1;
		if (!callback) {
			for (var i = 1; i <= len; ++i) {
				prf.start(null, null);
				prf.update(s);
				prf.update(forge.util.int32ToBytes(i));
				xor = u_c1 = prf.digest().getBytes();
				for (var j = 2; j <= c; ++j) {
					prf.start(null, null);
					prf.update(u_c1);
					u_c = prf.digest().getBytes();
					xor = forge.util.xorBytes(xor, u_c, hLen);
					u_c1 = u_c;
				}
				dk += i < len ? xor : xor.substr(0, r);
			}
			return dk;
		}
		var i = 1, j;
		function outer() {
			if (i > len) return callback(null, dk);
			prf.start(null, null);
			prf.update(s);
			prf.update(forge.util.int32ToBytes(i));
			xor = u_c1 = prf.digest().getBytes();
			j = 2;
			inner();
		}
		function inner() {
			if (j <= c) {
				prf.start(null, null);
				prf.update(u_c1);
				u_c = prf.digest().getBytes();
				xor = forge.util.xorBytes(xor, u_c, hLen);
				u_c1 = u_c;
				++j;
				return forge.util.setImmediate(inner);
			}
			dk += i < len ? xor : xor.substr(0, r);
			++i;
			outer();
		}
		outer();
	};
}) });

//#endregion
export default require_pbkdf2();

export { require_pbkdf2 };
//# sourceMappingURL=pbkdf2.mjs.map