var BigInteger=require("../libs/jsbn"),crypt=require("crypto");module.exports={isEncryption:!0,isSignature:!1},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(e,t,o){o=o||DEFAULT_HASH_FUNCTION;for(var r=module.exports.digestLength[o],n=Math.ceil(t/r),s=Buffer.alloc(r*n),i=Buffer.alloc(4),a=0;a<n;++a){var h=crypt.createHash(o);h.update(e),i.writeUInt32BE(a,0),h.update(i),h.digest().copy(s,a*r)}return s.slice(0,t)},module.exports.makeScheme=function(e,t){function o(e,t){this.key=e,this.options=t}return o.prototype.maxMessageLength=function(){return this.key.encryptedDataLength-2*module.exports.digestLength[this.options.encryptionSchemeOptions.hash||DEFAULT_HASH_FUNCTION]-2},o.prototype.encPad=function(e){var t=this.options.encryptionSchemeOptions.hash||DEFAULT_HASH_FUNCTION,o=this.options.encryptionSchemeOptions.mgf||module.exports.eme_oaep_mgf1,r=this.options.encryptionSchemeOptions.label||Buffer.alloc(0),n=this.key.encryptedDataLength,s=module.exports.digestLength[t];if(e.length>n-2*s-2)throw new Error("Message is too long to encode into an encoded message with a length of "+n+" bytes, increaseemLen to fix this error (minimum value for given parameters and options: "+(n-2*s-2)+")");var i=crypt.createHash(t);i.update(r),i=i.digest();var a=Buffer.alloc(n-e.length-2*s-1);a.fill(0),a[a.length-1]=1;for(var h=Buffer.concat([i,a,e]),p=crypt.randomBytes(s),c=o(p,h.length,t),l=0;l<h.length;l++)h[l]^=c[l];for(c=o(h,s,t),l=0;l<p.length;l++)p[l]^=c[l];var g=Buffer.alloc(1+p.length+h.length);return g[0]=0,p.copy(g,1),h.copy(g,1+p.length),g},o.prototype.encUnPad=function(e){var t=this.options.encryptionSchemeOptions.hash||DEFAULT_HASH_FUNCTION,o=this.options.encryptionSchemeOptions.mgf||module.exports.eme_oaep_mgf1,r=this.options.encryptionSchemeOptions.label||Buffer.alloc(0),n=module.exports.digestLength[t];if(e.length<2*n+2)throw new Error("Error decoding message, the supplied message is not long enough to be a valid OAEP encoded message");for(var s=e.slice(1,n+1),i=e.slice(1+n),a=o(i,n,t),h=0;h<s.length;h++)s[h]^=a[h];for(a=o(s,i.length,t),h=0;h<i.length;h++)i[h]^=a[h];var p=crypt.createHash(t);if(p.update(r),p=p.digest(),i.slice(0,n).toString("hex")!=p.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.");for(h=n;0===i[h++]&&h<i.length;);if(1!=i[h-1])throw new Error("Error decoding message, there is no padding message separator byte");return i.slice(h)},new o(e,t)};