var forge=require("./forge");require("./md"),require("./util");var sha1=module.exports=forge.sha1=forge.sha1||{};forge.md.sha1=forge.md.algorithms.sha1=sha1,sha1.create=function(){_initialized||_init();var e=null,t=forge.util.createBuffer(),h=Array(80),g={algorithm:"sha1",blockLength:64,digestLength:20,messageLength:0,fullMessageLength:null,messageLengthSize:8};return g.start=function(){g.messageLength=0,g.fullMessageLength=g.messageLength64=[];for(var h=g.messageLengthSize/4,a=0;a<h;++a)g.fullMessageLength.push(0);return t=forge.util.createBuffer(),e={h0:0x67452301,h1:0xefcdab89,h2:0x98badcfe,h3:0x10325476,h4:0xc3d2e1f0},g},g.start(),g.update=function(a,r){"utf8"===r&&(a=forge.util.encodeUtf8(a));var n=a.length;g.messageLength+=n,n=[n/0x100000000>>>0,n>>>0];for(var s=g.fullMessageLength.length-1;s>=0;--s)g.fullMessageLength[s]+=n[1],n[1]=n[0]+(g.fullMessageLength[s]/0x100000000>>>0),g.fullMessageLength[s]=g.fullMessageLength[s]>>>0,n[0]=n[1]/0x100000000>>>0;return t.putBytes(a),_update(e,h,t),(t.read>2048||0===t.length())&&t.compact(),g},g.digest=function(){var a,r=forge.util.createBuffer();r.putBytes(t.bytes());var n=g.fullMessageLength[g.fullMessageLength.length-1]+g.messageLengthSize&g.blockLength-1;r.putBytes(_padding.substr(0,g.blockLength-n));for(var s=8*g.fullMessageLength[0],u=0;u<g.fullMessageLength.length-1;++u)s+=(a=8*g.fullMessageLength[u+1])/0x100000000>>>0,r.putInt32(s>>>0),s=a>>>0;r.putInt32(s);var f={h0:e.h0,h1:e.h1,h2:e.h2,h3:e.h3,h4:e.h4};_update(f,h,r);var l=forge.util.createBuffer();return l.putInt32(f.h0),l.putInt32(f.h1),l.putInt32(f.h2),l.putInt32(f.h3),l.putInt32(f.h4),l},g};var _padding=null,_initialized=!1;function _init(){_padding=String.fromCharCode(128)+forge.util.fillString("\0",64),_initialized=!0}function _update(e,t,h){for(var g,a,r,n,s,u,f,l,i=h.length();i>=64;){for(a=e.h0,r=e.h1,n=e.h2,s=e.h3,u=e.h4,l=0;l<16;++l)g=h.getInt32(),t[l]=g,g=(a<<5|a>>>27)+(s^r&(n^s))+u+0x5a827999+g,u=s,s=n,n=(r<<30|r>>>2)>>>0,r=a,a=g;for(;l<20;++l)g=(g=t[l-3]^t[l-8]^t[l-14]^t[l-16])<<1|g>>>31,t[l]=g,g=(a<<5|a>>>27)+(s^r&(n^s))+u+0x5a827999+g,u=s,s=n,n=(r<<30|r>>>2)>>>0,r=a,a=g;for(;l<32;++l)g=(g=t[l-3]^t[l-8]^t[l-14]^t[l-16])<<1|g>>>31,t[l]=g,g=(a<<5|a>>>27)+(r^n^s)+u+0x6ed9eba1+g,u=s,s=n,n=(r<<30|r>>>2)>>>0,r=a,a=g;for(;l<40;++l)g=(g=t[l-6]^t[l-16]^t[l-28]^t[l-32])<<2|g>>>30,t[l]=g,g=(a<<5|a>>>27)+(r^n^s)+u+0x6ed9eba1+g,u=s,s=n,n=(r<<30|r>>>2)>>>0,r=a,a=g;for(;l<60;++l)g=(g=t[l-6]^t[l-16]^t[l-28]^t[l-32])<<2|g>>>30,t[l]=g,g=(a<<5|a>>>27)+(r&n|s&(r^n))+u+0x8f1bbcdc+g,u=s,s=n,n=(r<<30|r>>>2)>>>0,r=a,a=g;for(;l<80;++l)g=(g=t[l-6]^t[l-16]^t[l-28]^t[l-32])<<2|g>>>30,t[l]=g,g=(a<<5|a>>>27)+(r^n^s)+u+0xca62c1d6+g,u=s,s=n,n=(r<<30|r>>>2)>>>0,r=a,a=g;e.h0=e.h0+a|0,e.h1=e.h1+r|0,e.h2=e.h2+n|0,e.h3=e.h3+s|0,e.h4=e.h4+u|0,i-=64}}