cleaned up JSLint stuff; wrapped in SHA1 namespace
This commit is contained in:
1 parent
279e0e7391
commit
af799f2833
2 files changed
+77
-51
No files matched your search
@@ -8,9 +8,8 @@
|
|||||||
<script type="text/javascript" charset="utf-8">
|
<script type="text/javascript" charset="utf-8">
|
||||||
function run()
|
function run()
|
||||||
{
|
{
|
||||||
sha1_profile();
|
if ( SHA1.test() )
|
||||||
if ( sha1_verify() && hmac_verify() )
|
document.getElementById('result').innerHTML = SHA1.hash("abcde");
|
||||||
document.getElementById('result').innerHTML = sha1_string("abcde");
|
|
||||||
else
|
else
|
||||||
document.getElementById('result').innerHTML = "Error!!!";
|
document.getElementById('result').innerHTML = "Error!!!";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,11 @@
|
|||||||
|
/*jslint onevar: true, undef: true, nomen: true, regexp: true, newcap: true, immed: true, strict: true */
|
||||||
|
|
||||||
|
SHA1 = (function() // SHA1 "namespace"
|
||||||
|
{
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var hextab = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
|
||||||
|
|
||||||
function char_code_to_bytes(code)
|
function char_code_to_bytes(code)
|
||||||
{
|
{
|
||||||
var result = [];
|
var result = [];
|
||||||
@@ -11,11 +19,11 @@ function char_code_to_bytes(code)
|
|||||||
|
|
||||||
function str_to_bytes(str)
|
function str_to_bytes(str)
|
||||||
{
|
{
|
||||||
var result = [];
|
var result = [], i, len = str.length;
|
||||||
for (var i = 0; i < str.length; i++)
|
for (i=0; i<len; i++)
|
||||||
{
|
{
|
||||||
result = result.concat( char_code_to_bytes(str.charCodeAt(i)) );
|
result = result.concat( char_code_to_bytes(str.charCodeAt(i)) );
|
||||||
};
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,40 +43,46 @@ function num_to_big_endian_64(num)
|
|||||||
|
|
||||||
function bytes_to_big_endian_32(array)
|
function bytes_to_big_endian_32(array)
|
||||||
{
|
{
|
||||||
var result = [];
|
var result = [], i, len = array.length;
|
||||||
for (var i = 0; i < array.length; i += 4)
|
for (i=0; i<len; i+=4)
|
||||||
{
|
{
|
||||||
result.push((array[i] << 24) | (array[i+1] << 16) | (array[i+2] << 8) | array[i+3]);
|
result.push((array[i] << 24) | (array[i+1] << 16) | (array[i+2] << 8) | array[i+3]);
|
||||||
};
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function bytes_to_hex(x)
|
function bytes_to_hex(x)
|
||||||
{
|
{
|
||||||
var h = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
|
var
|
||||||
var result = "";
|
result = "",
|
||||||
|
len = x.length,
|
||||||
|
i;
|
||||||
|
|
||||||
for (var i = 0; i < x.length; i++) {
|
for (i=0; i<len; i++)
|
||||||
result += (h[x[i] >>> 4]);
|
{
|
||||||
result += (h[x[i] & 0xf]);
|
result += (hextab[x[i] >>> 4]) + (hextab[x[i] & 0xf]);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function sha1(byte_array)
|
function sha1(byte_array)
|
||||||
{
|
{
|
||||||
|
var
|
||||||
// copy array
|
// copy array
|
||||||
var message = byte_array.slice();
|
message = byte_array.slice(),
|
||||||
|
|
||||||
// store message size for later use
|
// store message size for later use
|
||||||
var message_size_in_bits = message.length * 8;
|
message_size_in_bits = message.length * 8,
|
||||||
|
|
||||||
// Initialize variables:
|
// Initialize variables:
|
||||||
var h0 = 0x67452301;
|
h0 = 0x67452301,
|
||||||
var h1 = 0xefcdab89;
|
h1 = 0xefcdab89,
|
||||||
var h2 = 0x98badcfe;
|
h2 = 0x98badcfe,
|
||||||
var h3 = 0x10325476;
|
h3 = 0x10325476,
|
||||||
var h4 = 0xc3d2e1f0;
|
h4 = 0xc3d2e1f0,
|
||||||
|
|
||||||
|
// variables
|
||||||
|
a, b, c, d, e, s, temp, w, i, i_chunk;
|
||||||
|
|
||||||
// Pre-processing:
|
// Pre-processing:
|
||||||
|
|
||||||
@@ -79,7 +93,7 @@ function sha1(byte_array)
|
|||||||
while ((message.length + 8) % 64)
|
while ((message.length + 8) % 64)
|
||||||
{
|
{
|
||||||
message.push(0);
|
message.push(0);
|
||||||
};
|
}
|
||||||
|
|
||||||
// append length of message (before pre-processing), in bits, as 64-bit big-endian integer
|
// append length of message (before pre-processing), in bits, as 64-bit big-endian integer
|
||||||
message = message.concat(num_to_big_endian_64(message_size_in_bits));
|
message = message.concat(num_to_big_endian_64(message_size_in_bits));
|
||||||
@@ -87,22 +101,26 @@ function sha1(byte_array)
|
|||||||
// Process the message in successive 512-bit chunks:
|
// Process the message in successive 512-bit chunks:
|
||||||
|
|
||||||
// break message into 512-bit chunks
|
// break message into 512-bit chunks
|
||||||
for (var i_chunk = 0; i_chunk < (message.length/64); i_chunk++)
|
for (i_chunk=0; i_chunk<(message.length/64); i_chunk++)
|
||||||
{
|
{
|
||||||
// break chunk into sixteen 32-bit big-endian words w[i], 0 <= i <= 15
|
// break chunk into sixteen 32-bit big-endian words w[i], 0 <= i <= 15
|
||||||
var w = bytes_to_big_endian_32(message.slice(i_chunk*64,(i_chunk*64)+64));
|
w = bytes_to_big_endian_32(message.slice(i_chunk*64,(i_chunk*64)+64));
|
||||||
|
|
||||||
// Initialize hash value for this chunk:
|
// Initialize hash value for this chunk:
|
||||||
var a = h0, b = h1, c = h2, d = h3, e = h4, s, temp;
|
a = h0;
|
||||||
|
b = h1;
|
||||||
|
c = h2;
|
||||||
|
d = h3;
|
||||||
|
e = h4;
|
||||||
|
|
||||||
// Main loop:
|
// Main loop:
|
||||||
for (var i = 0; i < 80; i++)
|
for (i=0; i<80; i++)
|
||||||
{
|
{
|
||||||
s = i & 0xf;
|
s = i & 0xf;
|
||||||
if (i >= 16)
|
if (i >= 16)
|
||||||
{
|
{
|
||||||
var _w = w[(s + 13) & 0xf] ^ w[(s + 8) & 0xf] ^ w[(s + 2) & 0xf] ^ w[s];
|
temp = w[(s + 13) & 0xf] ^ w[(s + 8) & 0xf] ^ w[(s + 2) & 0xf] ^ w[s];
|
||||||
w[s] = (_w << 1) | (_w >>> 31);
|
w[s] = (temp << 1) | (temp >>> 31); // rol(temp,1)
|
||||||
}
|
}
|
||||||
if (i < 20)
|
if (i < 20)
|
||||||
{
|
{
|
||||||
@@ -130,7 +148,7 @@ function sha1(byte_array)
|
|||||||
c = (b << 30) | (b >>> 2); // c = rol(b,30)
|
c = (b << 30) | (b >>> 2); // c = rol(b,30)
|
||||||
b = a;
|
b = a;
|
||||||
a = temp;
|
a = temp;
|
||||||
};
|
}
|
||||||
|
|
||||||
// Add this chunk's hash to result so far:
|
// Add this chunk's hash to result so far:
|
||||||
h0 = (h0 + a) & 0xffffffff;
|
h0 = (h0 + a) & 0xffffffff;
|
||||||
@@ -138,7 +156,7 @@ function sha1(byte_array)
|
|||||||
h2 = (h2 + c) & 0xffffffff;
|
h2 = (h2 + c) & 0xffffffff;
|
||||||
h3 = (h3 + d) & 0xffffffff;
|
h3 = (h3 + d) & 0xffffffff;
|
||||||
h4 = (h4 + e) & 0xffffffff;
|
h4 = (h4 + e) & 0xffffffff;
|
||||||
};
|
}
|
||||||
|
|
||||||
// Produce the final hash value (big-endian):
|
// Produce the final hash value (big-endian):
|
||||||
return [
|
return [
|
||||||
@@ -148,19 +166,13 @@ function sha1(byte_array)
|
|||||||
(h3 >>> 24) & 0xff, (h3 >>> 16) & 0xff, (h3 >>> 8) & 0xff, h3 & 0xff,
|
(h3 >>> 24) & 0xff, (h3 >>> 16) & 0xff, (h3 >>> 8) & 0xff, h3 & 0xff,
|
||||||
(h4 >>> 24) & 0xff, (h4 >>> 16) & 0xff, (h4 >>> 8) & 0xff, h4 & 0xff
|
(h4 >>> 24) & 0xff, (h4 >>> 16) & 0xff, (h4 >>> 8) & 0xff, h4 & 0xff
|
||||||
]; // note: returning this as a byte array is quite expensive!
|
]; // note: returning this as a byte array is quite expensive!
|
||||||
};
|
}
|
||||||
|
|
||||||
function sha1_string(str)
|
function sha1_string(str)
|
||||||
{
|
{
|
||||||
// get text into byte array
|
// get text into byte array, hash, convert to hex string
|
||||||
var message = str_to_bytes(str);
|
return bytes_to_hex(sha1(str_to_bytes(str)));
|
||||||
|
}
|
||||||
// perform SHA-1
|
|
||||||
h = sha1(message);
|
|
||||||
|
|
||||||
// convert to string
|
|
||||||
return bytes_to_hex(sha1(message));
|
|
||||||
};
|
|
||||||
|
|
||||||
function sha1_verify()
|
function sha1_verify()
|
||||||
{
|
{
|
||||||
@@ -168,21 +180,30 @@ function sha1_verify()
|
|||||||
(sha1_string("The quick brown fox jumps over the lazy dog") == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") &&
|
(sha1_string("The quick brown fox jumps over the lazy dog") == "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") &&
|
||||||
(sha1_string("") == "da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
(sha1_string("") == "da39a3ee5e6b4b0d3255bfef95601890afd80709")
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
function sha1_profile()
|
function sha1_profile()
|
||||||
{
|
{
|
||||||
var data = [1,2,3,4,5,6];
|
var data = [1,2,3,4,5,6], i;
|
||||||
for (var i = 0; i < 5000; i++) {
|
for (i=0; i<5000; i++)
|
||||||
|
{
|
||||||
sha1(data);
|
sha1(data);
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
function hmac_sha1(key_str, message_str)
|
function hmac_sha1(key_str, message_str)
|
||||||
{
|
{
|
||||||
|
var
|
||||||
// convert key & message to byte arrays
|
// convert key & message to byte arrays
|
||||||
var key = str_to_bytes(key_str);
|
key = str_to_bytes(key_str),
|
||||||
var message = str_to_bytes(message_str);
|
message = str_to_bytes(message_str),
|
||||||
|
|
||||||
|
// our pads
|
||||||
|
opad = new Array(20),
|
||||||
|
ipad = new Array(20),
|
||||||
|
|
||||||
|
// misc
|
||||||
|
i;
|
||||||
|
|
||||||
// setup key
|
// setup key
|
||||||
if (key.length > 20)
|
if (key.length > 20)
|
||||||
@@ -194,15 +215,14 @@ function hmac_sha1(key_str, message_str)
|
|||||||
key.push(0);
|
key.push(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
var opad = new Array(20), ipad = new Array(20);
|
for (i=0; i<20; i++)
|
||||||
for (var i = 0; i < 20; i++)
|
|
||||||
{
|
{
|
||||||
opad[i] = 0x5c ^ key[i];
|
opad[i] = 0x5c ^ key[i];
|
||||||
ipad[i] = 0x36 ^ key[i];
|
ipad[i] = 0x36 ^ key[i];
|
||||||
};
|
}
|
||||||
|
|
||||||
return bytes_to_hex(sha1(opad.concat(sha1(ipad.concat(message)))));
|
return bytes_to_hex(sha1(opad.concat(sha1(ipad.concat(message)))));
|
||||||
};
|
}
|
||||||
|
|
||||||
function hmac_verify()
|
function hmac_verify()
|
||||||
{
|
{
|
||||||
@@ -211,4 +231,11 @@ function hmac_verify()
|
|||||||
//return Boolean(hmac_sha1("a","b") == "6657855686823986c874362731139752014cb60b");
|
//return Boolean(hmac_sha1("a","b") == "6657855686823986c874362731139752014cb60b");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// our exports
|
||||||
|
return {
|
||||||
|
hash:sha1_string,
|
||||||
|
hmac:hmac_sha1,
|
||||||
|
test:hmac_verify
|
||||||
|
};
|
||||||
|
|
||||||
|
}()); // SHA1 "namespace"
|
||||||
Reference in new issue
Block a user