Skip to content

Instantly share code, notes, and snippets.

@bpot
Created January 16, 2010 07:20
Show Gist options
  • Save bpot/278717 to your computer and use it in GitHub Desktop.
Save bpot/278717 to your computer and use it in GitHub Desktop.
exports.SHA1 = function() {
this.H0 = 0x67452301;
this.H1 = 0xEFCDAB89;
this.H2 = 0x98BADCFE;
this.H3 = 0x10325476;
this.H4 = 0xC3D2E1F0;
this.buffer = "";
this.message_length = 0;
}
SHA1 = exports.SHA1;
SHA1.prototype.update = function(data) {
this.buffer += data;
this.message_length += data.length;
if(this.buffer.length >= 64) {
this.processBlock();
}
}
SHA1.prototype.finish = function() {
// Pad buffer
this.buffer += String.fromCharCode(0x80);
if(this.buffer.length <= 56) {
padding = 56 - this.buffer.length;
} else {
padding = 56 + (64-this.buffer.length);
}
for(i = 0;i < padding;i++) {
this.buffer += String.fromCharCode(0x00);
}
// Append Message Length
second = (this.message_length*8) & 0xffffffff;
first = Math.floor(((this.message_length*8)>>32)/Math.pow(2,32)) & 0xffffffff;
this.buffer += this.int_to_string(first);
this.buffer += this.int_to_string(second);
this.processBlock();
return this.H0.toHexStr() + this.H1.toHexStr() + this.H2.toHexStr() + this.H3.toHexStr() + this.H4.toHexStr();
}
SHA1.prototype.int_to_string = function(i) {
s = ""
s += String.fromCharCode((i >> 24) & 0xff);
s += String.fromCharCode((i >> 16) & 0xff);
s += String.fromCharCode((i >> 8) & 0xff);
s += String.fromCharCode((i >> 0) & 0xff);
return s;
}
SHA1.prototype.processBlock = function() {
w = this.buffer_to_words();
// Remove this block from buffer
this.buffer = this.buffer.substr(64);
var a = this.H0,
b = this.H1,
c = this.H2,
d = this.H3,
e = this.H4;
for(i = 0;i < 80;i++) {
if(i < 20) {
f = (b & c) | ((~b) & d);
k = 0x5A827999;
} else if (i < 40) {
f = b ^ c ^ d;
k = 0x6ED9EBA1;
} else if (i < 60) {
f = (b & c) | (b & d) | (c & d);
k = 0x8F1BBCDC;
} else if (i < 80) {
f = b ^ c ^ d;
k = 0xCA62C1D6;
}
temp = (this.rotate_left(a, 5) + f + e + k + w[i]) & 0xffffffff;
e = d;
d = c;
c = this.rotate_left(b, 30);
b = a;
a = temp;
}
this.H0 = (this.H0 + a) & 0xffffffff;
this.H1 = (this.H1 + b) & 0xffffffff;
this.H2 = (this.H2 + c) & 0xffffffff;
this.H3 = (this.H3 + d) & 0xffffffff;
this.H4 = (this.H4 + e) & 0xffffffff;
if(this.buffer.length >= 64) {
this.processBlock();
}
}
SHA1.prototype.buffer_to_words = function() {
words = new Array(80);
for(i = 0;i < 16;i++) {
n = 0;
for(j = 0;j < 4;j++) {
n = n + (this.buffer.charCodeAt(i*4+j) << ((3-j)*8));
}
words[i] = n;
}
return this.expand_words(words);
}
SHA1.prototype.expand_words = function(w) {
for(i = 16;i < 80;i++) {
w[i] = this.rotate_left((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]),1);
}
return w;
}
SHA1.prototype.rotate_left = function(n,s) {
var t4 = ( n<<s ) | (n>>>(32-s));
return t4;
};
Number.prototype.toHexStr = function()
{
var s="", v;
for (var i=7; i>=0; i--) { v = (this>>>(i*4)) & 0xf; s += v.toString(16); }
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment