import { __commonJS } from "../../../_virtual/rolldown_runtime.mjs";
import { require_forge } from "./forge.mjs";
import { require_util } from "./util.mjs";
import { require_random } from "./random.mjs";

//#region ../../node_modules/node-forge/lib/pss.js
var require_pss = /* @__PURE__ */ __commonJS({ "../../node_modules/node-forge/lib/pss.js": ((exports, module) => {
	/**
	* Javascript implementation of PKCS#1 PSS signature padding.
	*
	* @author Stefan Siegl
	*
	* Copyright (c) 2012 Stefan Siegl <stesie@brokenpipe.de>
	*/
	var forge = require_forge();
	require_random();
	require_util();
	var pss = module.exports = forge.pss = forge.pss || {};
	/**
	* Creates a PSS signature scheme object.
	*
	* There are several ways to provide a salt for encoding:
	*
	* 1. Specify the saltLength only and the built-in PRNG will generate it.
	* 2. Specify the saltLength and a custom PRNG with 'getBytesSync' defined that
	*   will be used.
	* 3. Specify the salt itself as a forge.util.ByteBuffer.
	*
	* @param options the options to use:
	*          md the message digest object to use, a forge md instance.
	*          mgf the mask generation function to use, a forge mgf instance.
	*          [saltLength] the length of the salt in octets.
	*          [prng] the pseudo-random number generator to use to produce a salt.
	*          [salt] the salt to use when encoding.
	*
	* @return a signature scheme object.
	*/
	pss.create = function(options) {
		if (arguments.length === 3) options = {
			md: arguments[0],
			mgf: arguments[1],
			saltLength: arguments[2]
		};
		var hash = options.md;
		var mgf = options.mgf;
		var hLen = hash.digestLength;
		var salt_ = options.salt || null;
		if (typeof salt_ === "string") salt_ = forge.util.createBuffer(salt_);
		var sLen;
		if ("saltLength" in options) sLen = options.saltLength;
		else if (salt_ !== null) sLen = salt_.length();
		else throw new Error("Salt length not specified or specific salt not given.");
		if (salt_ !== null && salt_.length() !== sLen) throw new Error("Given salt length does not match length of given salt.");
		var prng = options.prng || forge.random;
		var pssobj = {};
		/**
		* Encodes a PSS signature.
		*
		* This function implements EMSA-PSS-ENCODE as per RFC 3447, section 9.1.1.
		*
		* @param md the message digest object with the hash to sign.
		* @param modsBits the length of the RSA modulus in bits.
		*
		* @return the encoded message as a binary-encoded string of length
		*           ceil((modBits - 1) / 8).
		*/
		pssobj.encode = function(md, modBits) {
			var i;
			var emBits = modBits - 1;
			var emLen = Math.ceil(emBits / 8);
			var mHash = md.digest().getBytes();
			if (emLen < hLen + sLen + 2) throw new Error("Message is too long to encrypt.");
			var salt;
			if (salt_ === null) salt = prng.getBytesSync(sLen);
			else salt = salt_.bytes();
			var m_ = new forge.util.ByteBuffer();
			m_.fillWithByte(0, 8);
			m_.putBytes(mHash);
			m_.putBytes(salt);
			hash.start();
			hash.update(m_.getBytes());
			var h = hash.digest().getBytes();
			var ps = new forge.util.ByteBuffer();
			ps.fillWithByte(0, emLen - sLen - hLen - 2);
			ps.putByte(1);
			ps.putBytes(salt);
			var db = ps.getBytes();
			var maskLen = emLen - hLen - 1;
			var dbMask = mgf.generate(h, maskLen);
			var maskedDB = "";
			for (i = 0; i < maskLen; i++) maskedDB += String.fromCharCode(db.charCodeAt(i) ^ dbMask.charCodeAt(i));
			var mask = 65280 >> 8 * emLen - emBits & 255;
			maskedDB = String.fromCharCode(maskedDB.charCodeAt(0) & ~mask) + maskedDB.substr(1);
			return maskedDB + h + String.fromCharCode(188);
		};
		/**
		* Verifies a PSS signature.
		*
		* This function implements EMSA-PSS-VERIFY as per RFC 3447, section 9.1.2.
		*
		* @param mHash the message digest hash, as a binary-encoded string, to
		*         compare against the signature.
		* @param em the encoded message, as a binary-encoded string
		*          (RSA decryption result).
		* @param modsBits the length of the RSA modulus in bits.
		*
		* @return true if the signature was verified, false if not.
		*/
		pssobj.verify = function(mHash, em, modBits) {
			var i;
			var emBits = modBits - 1;
			var emLen = Math.ceil(emBits / 8);
			em = em.substr(-emLen);
			if (emLen < hLen + sLen + 2) throw new Error("Inconsistent parameters to PSS signature verification.");
			if (em.charCodeAt(emLen - 1) !== 188) throw new Error("Encoded message does not end in 0xBC.");
			var maskLen = emLen - hLen - 1;
			var maskedDB = em.substr(0, maskLen);
			var h = em.substr(maskLen, hLen);
			var mask = 65280 >> 8 * emLen - emBits & 255;
			if ((maskedDB.charCodeAt(0) & mask) !== 0) throw new Error("Bits beyond keysize not zero as expected.");
			var dbMask = mgf.generate(h, maskLen);
			var db = "";
			for (i = 0; i < maskLen; i++) db += String.fromCharCode(maskedDB.charCodeAt(i) ^ dbMask.charCodeAt(i));
			db = String.fromCharCode(db.charCodeAt(0) & ~mask) + db.substr(1);
			var checkLen = emLen - hLen - sLen - 2;
			for (i = 0; i < checkLen; i++) if (db.charCodeAt(i) !== 0) throw new Error("Leftmost octets not zero as expected");
			if (db.charCodeAt(checkLen) !== 1) throw new Error("Inconsistent PSS signature, 0x01 marker not found");
			var salt = db.substr(-sLen);
			var m_ = new forge.util.ByteBuffer();
			m_.fillWithByte(0, 8);
			m_.putBytes(mHash);
			m_.putBytes(salt);
			hash.start();
			hash.update(m_.getBytes());
			return h === hash.digest().getBytes();
		};
		return pssobj;
	};
}) });

//#endregion
export default require_pss();

export { require_pss };
//# sourceMappingURL=pss.mjs.map