| 1 |
/* from http://www.movable-type.co.uk/scripts/sha1.html */ |
| 2 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ |
| 3 |
/* SHA-1 implementation in JavaScript (c) Chris Veness 2002-2014 / MIT Licence */ |
| 4 |
/* */ |
| 5 |
/* - see http://csrc.nist.gov/groups/ST/toolkit/secure_hashing.html */ |
| 6 |
/* http://csrc.nist.gov/groups/ST/toolkit/examples.html */ |
| 7 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ |
| 8 |
(function (_window) { |
| 9 |
/* jshint node:true *//* global define, escape, unescape */ |
| 10 |
'use strict'; |
| 11 |
|
| 12 |
|
| 13 |
/** |
| 14 |
* SHA-1 hash function reference implementation. |
| 15 |
* |
| 16 |
* @namespace |
| 17 |
*/ |
| 18 |
var Sha1 = {}; |
| 19 |
_window.Sha1 = Sha1; |
| 20 |
|
| 21 |
/** |
| 22 |
* Generates SHA-1 hash of string. |
| 23 |
* |
| 24 |
* @param {string} msg - (Unicode) string to be hashed. |
| 25 |
* @returns {string} Hash of msg as hex character string. |
| 26 |
*/ |
| 27 |
Sha1.hash = function(msg) { |
| 28 |
// convert string to UTF-8, as SHA only deals with byte-streams |
| 29 |
msg = msg.utf8Encode(); |
| 30 |
|
| 31 |
// constants [§4.2.1] |
| 32 |
var K = [ 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6 ]; |
| 33 |
|
| 34 |
// PREPROCESSING |
| 35 |
|
| 36 |
msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1] |
| 37 |
|
| 38 |
// convert string msg into 512-bit/16-integer blocks arrays of ints [§5.2.1] |
| 39 |
var l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length |
| 40 |
var N = Math.ceil(l/16); // number of 16-integer-blocks required to hold 'l' ints |
| 41 |
var M = new Array(N); |
| 42 |
|
| 43 |
for (var i=0; i<N; i++) { |
| 44 |
M[i] = new Array(16); |
| 45 |
for (var j=0; j<16; j++) { // encode 4 chars per integer, big-endian encoding |
| 46 |
M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) | |
| 47 |
(msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3)); |
| 48 |
} // note running off the end of msg is ok 'cos bitwise ops on NaN return 0 |
| 49 |
} |
| 50 |
// add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1] |
| 51 |
// note: most significant word would be (len-1)*8 >>> 32, but since JS converts |
| 52 |
// bitwise-op args to 32 bits, we need to simulate this by arithmetic operators |
| 53 |
M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14]); |
| 54 |
M[N-1][15] = ((msg.length-1)*8) & 0xffffffff; |
| 55 |
|
| 56 |
// set initial hash value [§5.3.1] |
| 57 |
var H0 = 0x67452301; |
| 58 |
var H1 = 0xefcdab89; |
| 59 |
var H2 = 0x98badcfe; |
| 60 |
var H3 = 0x10325476; |
| 61 |
var H4 = 0xc3d2e1f0; |
| 62 |
|
| 63 |
// HASH COMPUTATION [§6.1.2] |
| 64 |
|
| 65 |
var W = new Array(80); var a, b, c, d, e; |
| 66 |
for (var i=0; i<N; i++) { |
| 67 |
|
| 68 |
// 1 - prepare message schedule 'W' |
| 69 |
for (var t=0; t<16; t++) W[t] = M[i][t]; |
| 70 |
for (var t=16; t<80; t++) W[t] = Sha1.ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1); |
| 71 |
|
| 72 |
// 2 - initialise five working variables a, b, c, d, e with previous hash value |
| 73 |
a = H0; b = H1; c = H2; d = H3; e = H4; |
| 74 |
|
| 75 |
// 3 - main loop |
| 76 |
for (var t=0; t<80; t++) { |
| 77 |
var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants |
| 78 |
var T = (Sha1.ROTL(a,5) + Sha1.f(s,b,c,d) + e + K[s] + W[t]) & 0xffffffff; |
| 79 |
e = d; |
| 80 |
d = c; |
| 81 |
c = Sha1.ROTL(b, 30); |
| 82 |
b = a; |
| 83 |
a = T; |
| 84 |
} |
| 85 |
|
| 86 |
// 4 - compute the new intermediate hash value (note 'addition modulo 2^32') |
| 87 |
H0 = (H0+a) & 0xffffffff; |
| 88 |
H1 = (H1+b) & 0xffffffff; |
| 89 |
H2 = (H2+c) & 0xffffffff; |
| 90 |
H3 = (H3+d) & 0xffffffff; |
| 91 |
H4 = (H4+e) & 0xffffffff; |
| 92 |
} |
| 93 |
|
| 94 |
return Sha1.toHexStr(H0) + Sha1.toHexStr(H1) + Sha1.toHexStr(H2) + |
| 95 |
Sha1.toHexStr(H3) + Sha1.toHexStr(H4); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
/** |
| 100 |
* Function 'f' [§4.1.1]. |
| 101 |
* @private |
| 102 |
*/ |
| 103 |
Sha1.f = function(s, x, y, z) { |
| 104 |
switch (s) { |
| 105 |
case 0: return (x & y) ^ (~x & z); // Ch() |
| 106 |
case 1: return x ^ y ^ z; // Parity() |
| 107 |
case 2: return (x & y) ^ (x & z) ^ (y & z); // Maj() |
| 108 |
case 3: return x ^ y ^ z; // Parity() |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
/** |
| 113 |
* Rotates left (circular left shift) value x by n positions [§3.2.5]. |
| 114 |
* @private |
| 115 |
*/ |
| 116 |
Sha1.ROTL = function(x, n) { |
| 117 |
return (x<<n) | (x>>>(32-n)); |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Hexadecimal representation of a number. |
| 123 |
* @private |
| 124 |
*/ |
| 125 |
Sha1.toHexStr = function(n) { |
| 126 |
// note can't use toString(16) as it is implementation-dependant, |
| 127 |
// and in IE returns signed numbers when used on full words |
| 128 |
var s="", v; |
| 129 |
for (var i=7; i>=0; i--) { v = (n>>>(i*4)) & 0xf; s += v.toString(16); } |
| 130 |
return s; |
| 131 |
}; |
| 132 |
|
| 133 |
|
| 134 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ |
| 135 |
|
| 136 |
|
| 137 |
/** Extend String object with method to encode multi-byte string to utf8 |
| 138 |
* - monsur.hossa.in/2012/07/20/utf-8-in-javascript.html */ |
| 139 |
if (typeof String.prototype.utf8Encode == 'undefined') { |
| 140 |
String.prototype.utf8Encode = function() { |
| 141 |
return unescape( encodeURIComponent( this ) ); |
| 142 |
}; |
| 143 |
} |
| 144 |
|
| 145 |
/** Extend String object with method to decode utf8 string to multi-byte */ |
| 146 |
if (typeof String.prototype.utf8Decode == 'undefined') { |
| 147 |
String.prototype.utf8Decode = function() { |
| 148 |
try { |
| 149 |
return decodeURIComponent( escape( this ) ); |
| 150 |
} catch (e) { |
| 151 |
return this; // invalid UTF-8? return as-is |
| 152 |
} |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ |
| 158 |
if (typeof module != 'undefined' && module.exports) module.exports = Sha1; // CommonJs export |
| 159 |
if (typeof define == 'function' && define.amd) define([], function() { return Sha1; }); // AMD |
| 160 |
})(window) |