const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.cjs');
const require_jsbn$1 = require('../libs/jsbn.cjs');

//#region ../../node_modules/node-rsa/src/schemes/oaep.js
var require_oaep = /* @__PURE__ */ require_rolldown_runtime.__commonJS({ "../../node_modules/node-rsa/src/schemes/oaep.js": ((exports, module) => {
	require_jsbn$1.default;
	var crypt = require("crypto");
	module.exports = {
		isEncryption: true,
		isSignature: false
	};
	module.exports.digestLength = {
		md4: 16,
		md5: 16,
		ripemd160: 20,
		rmd160: 20,
		sha1: 20,
		sha224: 28,
		sha256: 32,
		sha384: 48,
		sha512: 64
	};
	var DEFAULT_HASH_FUNCTION = "sha1";
	module.exports.eme_oaep_mgf1 = function(seed, maskLength, hashFunction) {
		hashFunction = hashFunction || DEFAULT_HASH_FUNCTION;
		var hLen = module.exports.digestLength[hashFunction];
		var count = Math.ceil(maskLength / hLen);
		var T = Buffer.alloc(hLen * count);
		var c = Buffer.alloc(4);
		for (var i = 0; i < count; ++i) {
			var hash = crypt.createHash(hashFunction);
			hash.update(seed);
			c.writeUInt32BE(i, 0);
			hash.update(c);
			hash.digest().copy(T, i * hLen);
		}
		return T.slice(0, maskLength);
	};
	module.exports.makeScheme = function(key, options) {
		function Scheme(key$1, options$1) {
			this.key = key$1;
			this.options = options$1;
		}
		Scheme.prototype.maxMessageLength = function() {
			return this.key.encryptedDataLength - 2 * module.exports.digestLength[this.options.encryptionSchemeOptions.hash || DEFAULT_HASH_FUNCTION] - 2;
		};
		/**
		* Pad input
		* alg: PKCS1_OAEP
		*
		* https://tools.ietf.org/html/rfc3447#section-7.1.1
		*/
		Scheme.prototype.encPad = function(buffer) {
			var hash = this.options.encryptionSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
			var mgf = this.options.encryptionSchemeOptions.mgf || module.exports.eme_oaep_mgf1;
			var label = this.options.encryptionSchemeOptions.label || Buffer.alloc(0);
			var emLen = this.key.encryptedDataLength;
			var hLen = module.exports.digestLength[hash];
			if (buffer.length > emLen - 2 * hLen - 2) throw new Error("Message is too long to encode into an encoded message with a length of " + emLen + " bytes, increaseemLen to fix this error (minimum value for given parameters and options: " + (emLen - 2 * hLen - 2) + ")");
			var lHash = crypt.createHash(hash);
			lHash.update(label);
			lHash = lHash.digest();
			var PS = Buffer.alloc(emLen - buffer.length - 2 * hLen - 1);
			PS.fill(0);
			PS[PS.length - 1] = 1;
			var DB = Buffer.concat([
				lHash,
				PS,
				buffer
			]);
			var seed = crypt.randomBytes(hLen);
			var mask = mgf(seed, DB.length, hash);
			for (var i = 0; i < DB.length; i++) DB[i] ^= mask[i];
			mask = mgf(DB, hLen, hash);
			for (i = 0; i < seed.length; i++) seed[i] ^= mask[i];
			var em = Buffer.alloc(1 + seed.length + DB.length);
			em[0] = 0;
			seed.copy(em, 1);
			DB.copy(em, 1 + seed.length);
			return em;
		};
		/**
		* Unpad input
		* alg: PKCS1_OAEP
		*
		* Note: This method works within the buffer given and modifies the values. It also returns a slice of the EM as the return Message.
		* If the implementation requires that the EM parameter be unmodified then the implementation should pass in a clone of the EM buffer.
		*
		* https://tools.ietf.org/html/rfc3447#section-7.1.2
		*/
		Scheme.prototype.encUnPad = function(buffer) {
			var hash = this.options.encryptionSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
			var mgf = this.options.encryptionSchemeOptions.mgf || module.exports.eme_oaep_mgf1;
			var label = this.options.encryptionSchemeOptions.label || Buffer.alloc(0);
			var hLen = module.exports.digestLength[hash];
			if (buffer.length < 2 * hLen + 2) throw new Error("Error decoding message, the supplied message is not long enough to be a valid OAEP encoded message");
			var seed = buffer.slice(1, hLen + 1);
			var DB = buffer.slice(1 + hLen);
			var mask = mgf(DB, hLen, hash);
			for (var i = 0; i < seed.length; i++) seed[i] ^= mask[i];
			mask = mgf(seed, DB.length, hash);
			for (i = 0; i < DB.length; i++) DB[i] ^= mask[i];
			var lHash = crypt.createHash(hash);
			lHash.update(label);
			lHash = lHash.digest();
			if (DB.slice(0, hLen).toString("hex") != lHash.toString("hex")) throw new Error("Error decoding message, the lHash calculated from the label provided and the lHash in the encrypted data do not match.");
			i = hLen;
			while (DB[i++] === 0 && i < DB.length);
			if (DB[i - 1] != 1) throw new Error("Error decoding message, there is no padding message separator byte");
			return DB.slice(i);
		};
		return new Scheme(key, options);
	};
}) });

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