const require_rolldown_runtime = require('../../../_virtual/rolldown_runtime.cjs');
const require_forge$1 = require('./forge.cjs');
const require_util$1 = require('./util.cjs');
const require_random$1 = require('./random.cjs');
const require_sha1$1 = require('./sha1.cjs');

//#region ../../node_modules/node-forge/lib/pkcs1.js
var require_pkcs1 = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/node-forge/lib/pkcs1.js": ((exports, module) => {
	/**
	* Partial implementation of PKCS#1 v2.2: RSA-OEAP
	*
	* Modified but based on the following MIT and BSD licensed code:
	*
	* https://github.com/kjur/jsjws/blob/master/rsa.js:
	*
	* The 'jsjws'(JSON Web Signature JavaScript Library) License
	*
	* Copyright (c) 2012 Kenji Urushima
	*
	* Permission is hereby granted, free of charge, to any person obtaining a copy
	* of this software and associated documentation files (the "Software"), to deal
	* in the Software without restriction, including without limitation the rights
	* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	* copies of the Software, and to permit persons to whom the Software is
	* furnished to do so, subject to the following conditions:
	*
	* The above copyright notice and this permission notice shall be included in
	* all copies or substantial portions of the Software.
	*
	* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	* THE SOFTWARE.
	*
	* http://webrsa.cvs.sourceforge.net/viewvc/webrsa/Client/RSAES-OAEP.js?content-type=text%2Fplain:
	*
	* RSAES-OAEP.js
	* $Id: RSAES-OAEP.js,v 1.1.1.1 2003/03/19 15:37:20 ellispritchard Exp $
	* JavaScript Implementation of PKCS #1 v2.1 RSA CRYPTOGRAPHY STANDARD (RSA Laboratories, June 14, 2002)
	* Copyright (C) Ellis Pritchard, Guardian Unlimited 2003.
	* Contact: ellis@nukinetics.com
	* Distributed under the BSD License.
	*
	* Official documentation: http://www.rsa.com/rsalabs/node.asp?id=2125
	*
	* @author Evan Jones (http://evanjones.ca/)
	* @author Dave Longley
	*
	* Copyright (c) 2013-2014 Digital Bazaar, Inc.
	*/
	var forge = require_forge$1.default;
	require_util$1.default;
	require_random$1.default;
	require_sha1$1.default;
	var pkcs1 = module.exports = forge.pkcs1 = forge.pkcs1 || {};
	/**
	* Encode the given RSAES-OAEP message (M) using key, with optional label (L)
	* and seed.
	*
	* This method does not perform RSA encryption, it only encodes the message
	* using RSAES-OAEP.
	*
	* @param key the RSA key to use.
	* @param message the message to encode.
	* @param options the options to use:
	*          label an optional label to use.
	*          seed the seed to use.
	*          md the message digest object to use, undefined for SHA-1.
	*          mgf1 optional mgf1 parameters:
	*            md the message digest object to use for MGF1.
	*
	* @return the encoded message bytes.
	*/
	pkcs1.encode_rsa_oaep = function(key, message, options) {
		var label;
		var seed;
		var md;
		var mgf1Md;
		if (typeof options === "string") {
			label = options;
			seed = arguments[3] || void 0;
			md = arguments[4] || void 0;
		} else if (options) {
			label = options.label || void 0;
			seed = options.seed || void 0;
			md = options.md || void 0;
			if (options.mgf1 && options.mgf1.md) mgf1Md = options.mgf1.md;
		}
		if (!md) md = forge.md.sha1.create();
		else md.start();
		if (!mgf1Md) mgf1Md = md;
		var keyLength = Math.ceil(key.n.bitLength() / 8);
		var maxLength = keyLength - 2 * md.digestLength - 2;
		if (message.length > maxLength) {
			var error = /* @__PURE__ */ new Error("RSAES-OAEP input message length is too long.");
			error.length = message.length;
			error.maxLength = maxLength;
			throw error;
		}
		if (!label) label = "";
		md.update(label, "raw");
		var lHash = md.digest();
		var PS = "";
		var PS_length = maxLength - message.length;
		for (var i = 0; i < PS_length; i++) PS += "\0";
		var DB = lHash.getBytes() + PS + "" + message;
		if (!seed) seed = forge.random.getBytes(md.digestLength);
		else if (seed.length !== md.digestLength) {
			var error = /* @__PURE__ */ new Error("Invalid RSAES-OAEP seed. The seed length must match the digest length.");
			error.seedLength = seed.length;
			error.digestLength = md.digestLength;
			throw error;
		}
		var dbMask = rsa_mgf1(seed, keyLength - md.digestLength - 1, mgf1Md);
		var maskedDB = forge.util.xorBytes(DB, dbMask, DB.length);
		var seedMask = rsa_mgf1(maskedDB, md.digestLength, mgf1Md);
		return "\0" + forge.util.xorBytes(seed, seedMask, seed.length) + maskedDB;
	};
	/**
	* Decode the given RSAES-OAEP encoded message (EM) using key, with optional
	* label (L).
	*
	* This method does not perform RSA decryption, it only decodes the message
	* using RSAES-OAEP.
	*
	* @param key the RSA key to use.
	* @param em the encoded message to decode.
	* @param options the options to use:
	*          label an optional label to use.
	*          md the message digest object to use for OAEP, undefined for SHA-1.
	*          mgf1 optional mgf1 parameters:
	*            md the message digest object to use for MGF1.
	*
	* @return the decoded message bytes.
	*/
	pkcs1.decode_rsa_oaep = function(key, em, options) {
		var label;
		var md;
		var mgf1Md;
		if (typeof options === "string") {
			label = options;
			md = arguments[3] || void 0;
		} else if (options) {
			label = options.label || void 0;
			md = options.md || void 0;
			if (options.mgf1 && options.mgf1.md) mgf1Md = options.mgf1.md;
		}
		var keyLength = Math.ceil(key.n.bitLength() / 8);
		if (em.length !== keyLength) {
			var error = /* @__PURE__ */ new Error("RSAES-OAEP encoded message length is invalid.");
			error.length = em.length;
			error.expectedLength = keyLength;
			throw error;
		}
		if (md === void 0) md = forge.md.sha1.create();
		else md.start();
		if (!mgf1Md) mgf1Md = md;
		if (keyLength < 2 * md.digestLength + 2) throw new Error("RSAES-OAEP key is too short for the hash function.");
		if (!label) label = "";
		md.update(label, "raw");
		var lHash = md.digest().getBytes();
		var y = em.charAt(0);
		var maskedSeed = em.substring(1, md.digestLength + 1);
		var maskedDB = em.substring(1 + md.digestLength);
		var seedMask = rsa_mgf1(maskedDB, md.digestLength, mgf1Md);
		var dbMask = rsa_mgf1(forge.util.xorBytes(maskedSeed, seedMask, maskedSeed.length), keyLength - md.digestLength - 1, mgf1Md);
		var db = forge.util.xorBytes(maskedDB, dbMask, maskedDB.length);
		var lHashPrime = db.substring(0, md.digestLength);
		var error = y !== "\0";
		for (var i = 0; i < md.digestLength; ++i) error |= lHash.charAt(i) !== lHashPrime.charAt(i);
		var in_ps = 1;
		var index = md.digestLength;
		for (var j = md.digestLength; j < db.length; j++) {
			var code = db.charCodeAt(j);
			var is_0 = code & 1 ^ 1;
			error |= code & (in_ps ? 65534 : 0);
			in_ps = in_ps & is_0;
			index += in_ps;
		}
		if (error || db.charCodeAt(index) !== 1) throw new Error("Invalid RSAES-OAEP padding.");
		return db.substring(index + 1);
	};
	function rsa_mgf1(seed, maskLength, hash) {
		if (!hash) hash = forge.md.sha1.create();
		var t = "";
		var count = Math.ceil(maskLength / hash.digestLength);
		for (var i = 0; i < count; ++i) {
			var c = String.fromCharCode(i >> 24 & 255, i >> 16 & 255, i >> 8 & 255, i & 255);
			hash.start();
			hash.update(seed + c);
			t += hash.digest().getBytes();
		}
		return t.substring(0, maskLength);
	}
}) });

//#endregion
Object.defineProperty(exports, 'default', {
  enumerable: true,
  get: function () {
    return require_pkcs1();
  }
});
//# sourceMappingURL=pkcs1.cjs.map