/*//////////////////////////////////////////////////////////////////////////
* THIS PROGRAM IS A DERIVATIVE OF AN EXISTING WORK :: 7 September 2010
* See: http://pajhome.org.uk/crypt/md5/scripts.html
*
* Significant but mostly superficial changes have been made to the structure of the
* source so that it conforms to the Joose3 object system for JavaScript
* See: http://joose.it/
/*//////////////////////////////////////////////////////////////////////////
Joose.Class('Hash.SHA1', {
/*VERSION*/VERSION : 0.06,
my : {
methods : {
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
hex_sha1 : function (s) { return Encode.rstr2hex(this.rstr_sha1(Encode.str2rstr_utf8(s))) },
b64_sha1 : function (s) { return Encode.rstr2b64(this.rstr_sha1(Encode.str2rstr_utf8(s))) },
any_sha1 : function (s, e) { return Encode.rstr2any(this.rstr_sha1(Encode.str2rstr_utf8(s)), e) },
hex_hmac_sha1 : function (k, d) { return Encode.rstr2hex(this.rstr_hmac_sha1(Encode.str2rstr_utf8(k), Encode.str2rstr_utf8(d))) },
b64_hmac_sha1 : function (k, d) { return Encode.rstr2b64(this.rstr_hmac_sha1(Encode.str2rstr_utf8(k), Encode.str2rstr_utf8(d))) },
any_hmac_sha1 : function (k, d, e) { return Encode.rstr2any(this.rstr_hmac_sha1(Encode.str2rstr_utf8(k), Encode.str2rstr_utf8(d)), e) },
/*
* Perform a simple self-test to see if the VM is working
*/
sha1_vm_test : function () {
return this.hex_sha1('abc').toLowerCase() == 'a9993e364706816aba3e25717850c26c9cd0d89d'
},
/*
* Calculate the SHA1 of a raw string
*/
rstr_sha1 : function (s) {
return Encode.binb2rstr(this.binb_sha1(Encode.rstr2binb(s), s.length * 8))
},
/*
* Calculate the HMAC-SHA1 of a key and some data (raw strings)
*/
rstr_hmac_sha1 : function (key, data) {
var bkey = rstr2binb(key)
if(bkey.length > 16) { bkey = this.binb_sha1(bkey, key.length * 8) }
var ipad = Array(16), opad = Array(16)
for(var i = 0; i < 16; i++) {
ipad[i] = bkey[i] ^ 0x36363636
opad[i] = bkey[i] ^ 0x5C5C5C5C
}
var hash = this.binb_sha1(ipad.concat(Encode.rstr2binb(data)), 512 + data.length * 8)
return Encode.binb2rstr(this.binb_sha1(opad.concat(hash), 512 + 160))
},
/*
* Calculate the SHA-1 of an array of big-endian words, and a bit length
*/
binb_sha1 : function (x, len) {
/* append padding */
x[len >> 5] |= 0x80 << (24 - len % 32)
x[((len + 64 >> 9) << 4) + 15] = len
var w = Array(80)
var a = 1732584193
var b = -271733879
var c = -1732584194
var d = 271733878
var e = -1009589776
for(var i = 0; i < x.length; i += 16) {
var olda = a
var oldb = b
var oldc = c
var oldd = d
var olde = e
for(var j = 0; j < 80; j++) {
if(j < 16) { w[j] = x[i + j] }
else { w[j] = this.bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1) }
var t = this.safe_add(this.safe_add(this.bit_rol(a, 5), this.sha1_ft(j, b, c, d)),
this.safe_add(this.safe_add(e, w[j]), this.sha1_kt(j)))
e = d
d = c
c = this.bit_rol(b, 30)
b = a
a = t
}
a = this.safe_add(a, olda)
b = this.safe_add(b, oldb)
c = this.safe_add(c, oldc)
d = this.safe_add(d, oldd)
e = this.safe_add(e, olde)
}
return Array(a, b, c, d, e)
},
/*
* Perform the appropriate triplet combination function for the current
* iteration
*/
sha1_ft : function (t, b, c, d) {
if(t < 20) { return (b & c) | ((~b) & d) }
if(t < 40) { return b ^ c ^ d }
if(t < 60) { return (b & c) | (b & d) | (c & d) }
return b ^ c ^ d
},
/*
* Determine the appropriate additive constant for the current iteration
*/
sha1_kt : function (t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514
},
/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
safe_add : function (x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF)
var msw = (x >> 16) + (y >> 16) + (lsw >> 16)
return (msw << 16) | (lsw & 0xFFFF)
},
/*
* Bitwise rotate a 32-bit number to the left.
*/
bit_rol : function (num, cnt) {
return (num << cnt) | (num >>> (32 - cnt))
}
}
}
})
/*///////////////////// ORIGINAL LICENSE BELOW ////////////////////////////////
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS 180-1
* Version 2.2 Copyright Paul Johnston 2000 - 2009.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* All rights reserved.
*
* See http://pajhome.org.uk/crypt/md5 for details.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the <ORGANIZATION> nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
/*///////////////////////////////////////////////////////////////////////////