var crypto,forge=require("./forge");require("./hmac"),require("./md"),require("./util");var pkcs5=forge.pkcs5=forge.pkcs5||{};forge.util.isNodejs&&!forge.options.usePureJavaScript&&(crypto=require("crypto")),module.exports=forge.pbkdf2=pkcs5.pbkdf2=function(r,t,e,o,f,n){if("function"==typeof f&&(n=f,f=null),forge.util.isNodejs&&!forge.options.usePureJavaScript&&crypto.pbkdf2&&(null===f||"object"!=typeof f)&&(crypto.pbkdf2Sync.length>4||!f||"sha1"===f))return("string"!=typeof f&&(f="sha1"),r=Buffer.from(r,"binary"),t=Buffer.from(t,"binary"),n)?4===crypto.pbkdf2Sync.length?crypto.pbkdf2(r,t,e,o,function(r,t){if(r)return n(r);n(null,t.toString("binary"))}):crypto.pbkdf2(r,t,e,o,f,function(r,t){if(r)return n(r);n(null,t.toString("binary"))}):4===crypto.pbkdf2Sync.length?crypto.pbkdf2Sync(r,t,e,o).toString("binary"):crypto.pbkdf2Sync(r,t,e,o,f).toString("binary");if(null==f&&(f="sha1"),"string"==typeof f){if(!(f in forge.md.algorithms))throw Error("Unknown hash algorithm: "+f);f=forge.md[f].create()}var i=f.digestLength;if(o>0xffffffff*i){var u=Error("Derived key is too long.");if(n)return n(u);throw u}var l=Math.ceil(o/i),s=o-(l-1)*i,a=forge.hmac.create();a.start(f,r);var p="";if(!n){for(var g=1;g<=l;++g){a.start(null,null),a.update(t),a.update(forge.util.int32ToBytes(g)),c=b=a.digest().getBytes();for(var y=2;y<=e;++y)a.start(null,null),a.update(b),d=a.digest().getBytes(),c=forge.util.xorBytes(c,d,i),b=d;p+=g<l?c:c.substr(0,s)}return p}var c,d,b,y,g=1;!function r(){if(g>l)return n(null,p);a.start(null,null),a.update(t),a.update(forge.util.int32ToBytes(g)),c=b=a.digest().getBytes(),y=2,function t(){if(y<=e)return a.start(null,null),a.update(b),d=a.digest().getBytes(),c=forge.util.xorBytes(c,d,i),b=d,++y,forge.util.setImmediate(t);p+=g<l?c:c.substr(0,s),++g,r()}()}()};