Link: http://www.web-tinker.com/article/20710.html
function sha1(data){
/**************************************************
Author:次碳酸钴([email protected])
Input:Uint8Array
Output:Uint8Array
**************************************************/
var i,j,t;
var l=((data.length+8)>>>6<<4)+16,s=new Uint8Array(l<<2);
s.set(new Uint8Array(data.buffer)),s=new Uint32Array(s.buffer);
for(t=new DataView(s.buffer),i=0;i2);
s[data.length>>2]|=0x80<<(24-(data.length&3)*8);
s[l-1]=data.length<<3;
var w=[],f=[
function(){return m[1]&m[2]|~m[1]&m[3];},
function(){return m[1]^m[2]^m[3];},
function(){return m[1]&m[2]|m[1]&m[3]|m[2]&m[3];},
function(){return m[1]^m[2]^m[3];}
],rol=function(n,c){return n<>>(32-c);},
k=[1518500249,1859775393,-1894007588,-899497514],
m=[1732584193,-271733879,null,null,-1009589776];
m[2]=~m[0],m[3]=~m[1];
for(i=0;i16){
var o=m.slice(0);
for(j=0;j<80;j++)
w[j]=j<16?s[i+j]:rol(w[j-3]^w[j-8]^w[j-14]^w[j-16],1),
t=rol(m[0],5)+f[j/20|0]()+m[4]+w[j]+k[j/20|0]|0,
m[1]=rol(m[1],30),m.pop(),m.unshift(t);
for(j=0;j<5;j++)m[j]=m[j]+o[j]|0;
};
t=new DataView(new Uint32Array(m).buffer);
for(var i=0;i<5;i++)m[i]=t.getUint32(i<<2);
return new Uint8Array(new Uint32Array(m).buffer);
};
使用演示:
运行
请选择需要计算SHA1的文件: type="file" />
如果需要计算字符串的SHA1,先要将字符串转换为所需的编码,比如 UTF-8:
运行
>
type="button" value="计算文本SHA1(UTF-8)" />