In questo tutorial imparerete come implementare l’algoritmo SHA-1 in Javascript.
Questa funzione può ritornarvi utile per criptare le vostre stringhe.

 
/*
* The JavaScript implementation of the Secure Hash Algorithm 1
*
* Copyright (c) 2008 Takanori Ishikawa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. 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.
*
* 3. Neither the name of the authors 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.
*/

/**
* This is the javascript file for code which implements
* the Secure Hash Algorithm 1 as defined in FIPS 180-1 published April 17, 1995.
*
* Author: Takanori Ishikawa
* Copyright: Takanori Ishikawa 2008
* License: BSD License (see above)
*
* NOTE:
* Only 8-bit string is supported, please use encodeURIComponent() function
* if you want to hash multibyte string.
*
* Supported Browsers:
* [Win] IE 6, Firefox 2
* [Mac] Safari 3, Firefox 2
*
* Usage:
* var hexdigest = new SHA1("Hello.").hexdigest(); // "9b56d519ccd9e1e5b2a725e186184cdc68de0731"
*
* See Also:
* FIPS 180-1 – Secure Hash Standard
* http://www.itl.nist.gov/fipspubs/fip180-1.htm
*
*/

 
var SHA1 = (function(){
 
/**
* Spec is the BDD style test utilities.
*/

var Spec = {
/** Replace the Spec.describe function with empty function if false. */
enabled: true,
 
/** Indicates whether object ‘a’ is "equal to" ‘b’. */
equals: function(a, b) {
if (a instanceof Array && b instanceof Array) {
if (a.length != b.length) return false;
for (var i = 0; i < a.length; i++) if (!Spec.equals(a[i], b[i])) return false;
return true;
}
if ((a != null && b != null) && (typeof a == "object" && typeof b == "object")) {
for (var i in a) if (!Spec.equals(a[i], b[i])) return false;
return true;
}
return (a == b);
},
 
/** equivalent to xUint’s assert */
should: function(expection, message) {
Spec.currentIndicator++;
if (!expection) {
var warning = [
"[Spec failed",
Spec.currentTitle ? " (" + Spec.currentTitle + ")] " : "] ",
(message || (Spec.currentMessage + " " + Spec.currentIndicator) || "")
].join("");
 
alert(warning);
throw warning;
}
return !!expection;
},
 
/** Write your specification by using describe method. */
describe: function(title, spec) {
Spec.currentTitle = title;
for (var name in spec) {
Spec.currentMessage = name;
Spec.currentIndicator = 0;
spec[name]();
Spec.currentIndicator = null;
}
Spec.currentMessage = Spec.currentTitle = null;
},
Version: "0.1"
};
 
// Other BDD style stuffs.
Spec.should.equal = function(a, b, message) { return Spec.should(Spec.equals(a, b), message); };
Spec.should.not = function(a, message) { return Spec.should(!a, message); };
Spec.should.not.equal = function(a, b, message) { return Spec.should(!Spec.equals(a, b), message); };
if (!Spec.enabled) Spec.describe = function(){};
 
// self test
Spec.describe("Spec object", {
"should": function() {
Spec.should(true);
Spec.should(1);
},
"should.not": function() {
Spec.should.not(false);
Spec.should.not(0);
},
"should.equal": function() {
Spec.should.equal(null, null);
Spec.should.equal("", "");
Spec.should.equal(12345, 12345);
Spec.should.equal([0,1,2], [0,1,2]);
Spec.should.equal([0,1,[0,1,2]], [0,1,[0,1,2]]);
Spec.should.equal({}, {});
Spec.should.equal({x:1}, {x:1});
Spec.should.equal({x:[1]}, {x:[1]});
},
"should.not.equal": function() {
Spec.should.not.equal([1,2,3], [1,2,3,4]);
Spec.should.not.equal({x:1}, [1,2,3,4]);
}
});
 
// ———————————————————–
// Utilities
// ———————————————————–
// int32 -> hexdigits string (e.g. 0×123 -> ’00000123′)
function strfhex32(i32) {
i32 &= 0xffffffff;
if (i32 < 0) i32 += 0×100000000;
var hex = Number(i32).toString(16);
if (hex.length < hex = "00000000".substr(0, 8 – hex.length) + hex;
return hex;
}
Spec.describe("sha1", {
"strfhex32": function() {
Spec.should.equal(strfhex32(0×0), "00000000");
Spec.should.equal(strfhex32(0×123), "00000123");
Spec.should.equal(strfhex32(0xffffffff), "ffffffff");
}
});
 
// int32 -> string (e.g. 123 -> ’00000000 00000000 00000000 01111011′)
function strfbits(i32) {
if (typeof arguments.callee.ZERO32 == ‘undefined’) {
arguments.callee.ZERO32 = new Array(33).join("0");
}
 
var bits = Number(i32).toString(2);
// ’0′ padding
if (bits.length < 32) bits = arguments.callee.ZERO32.substr(0, 32 – bits.length) + bits;
// split by 8 bits
return bits.replace(/(\d{8})/g, ‘$1 ‘)
.replace(/^\s*(.*?)\s*$/, ‘$1′);
}
Spec.describe("sha1", {
"strfbits": function() {
Spec.should.equal(strfbits(0), "00000000 00000000 00000000 00000000");
Spec.should.equal(strfbits(1), "00000000 00000000 00000000 00000001");
Spec.should.equal(strfbits(123), "00000000 00000000 00000000 01111011");
}
});
 
// ———————————————————–
// SHA-1
// ———————————————————–
// Returns Number(32bit unsigned integer) array size to fit for blocks (512-bit strings)
function padding_size(nbits) {
var n = nbits + 1 + 64
return 512 * Math.ceil(n / 512) / 32;
}
Spec.describe("sha1", {
"padding_size": function() {
Spec.should.equal(padding_size(0), 16);
Spec.should.equal(padding_size(1), 16);
Spec.should.equal(padding_size(512641), 16);
Spec.should.equal(padding_size(51264), 32);
}
});
 
// 8bit string -> uint32[]
function word_array(m) {
var nchar = m.length;
var size = padding_size(nchar * 8);
var words = new Array(size);
for (var i = 0, j = 0; i < nchar; ) {
words[j++] = ((m.charCodeAt(i++) & 0xff) > 27)) + E + W[t];
 
if (t >= 0 && t < = 19) tmp += ((B & C) | ((~B) & D)) + 0x5a827999;
else if (t >= 20 && t < = 39) tmp += (B ^ C ^ D) + 0x6ed9eba1;
else if (t >= 40 && t < = 59) tmp += ((B & C) | (B & D) | (C & D)) + 0x8f1bbcdc;
else if (t >= 60 && t < = 79) tmp += (B ^ C ^ D) + 0xca62c1d6;
 
E = D; D = C; C = ((B >> 2)); B = A; A = tmp;
}
 
// (e) H0 = H0 + A, H1 = H1 + B, H2 = H2 + C, H3 = H3 + D, H4 = H4 + E.
H[0] = (H[0] + A) & 0xffffffff;
H[1] = (H[1] + B) & 0xffffffff;
H[2] = (H[2] + C) & 0xffffffff;
H[3] = (H[3] + D) & 0xffffffff;
H[4] = (H[4] + E) & 0xffffffff;
if (H[0] < 0) H[0] += 0×100000000;
if (H[1] < 0) H[1] += 0×100000000;
if (H[2] < 0) H[2] += 0×100000000;
if (H[3] < 0) H[3] += 0×100000000;
if (H[4] < 0) H[4] += 0×100000000;
}
 
return H;
}
 
// message: 8bit string
var SHA1 = function(message) {
this.message = message;
}
 
SHA1.prototype = {
digest: function() {
var nbits = this.message.length * 8;
var words = padding(word_array(this.message), nbits);
return digest(words);
},
 
hexdigest: function() {
var digest = this.digest();
for (var i = 0; i < digest.length; i++) digest[i] = strfhex32(digest[i]);
return digest.join("");
}
};
 
Spec.describe("sha1", {
"SHA1#hexdigest": function() {
Spec.should.equal(new SHA1("").hexdigest(), "da39a3ee5e6b4b0d3255bfef95601890afd80709");
Spec.should.equal(new SHA1("1").hexdigest(), "356a192b7913b04c54574d18c28d46e6395428ab");
Spec.should.equal(new SHA1("Hello.").hexdigest(), "9b56d519ccd9e1e5b2a725e186184cdc68de0731");
Spec.should.equal(new SHA1("9b56d519ccd9e1e5b2a725e186184cdc68de0731").hexdigest(), "f042dc98a62cbad68dbe21f11bbc1e9d416d2bf6");
Spec.should.equal(new SHA1("MD5abZRVSXZVRcasdfasdddddddddddddddds+BNRJFSLKJFN+SEONBBJFJXLKCJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wurJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wurJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wurJFSE)RUNVXDLILKVJRN)#NVFJ)WVFWRW#)NVS$Q=$dddddddddddddWV;no9wuraddddddasdfasdfd").hexdigest(), "662dbf4ebc9cdb4224766e87634e5ba9e6de672b");
}
});
 
return SHA1;
})();
 

fonte: www.sastgroup.com ? Vai al post originale