This paper illustrates the method of JavaScript implementation of SHA-1 encryption algorithm. Share to everyone for your reference. The implementation methods are as follows:
Call method: Hex_sha1.
Copy Code code as follows:
/*
*
* A JavaScript Implementation of the Secure Hash algorithm, SHA-1, as defined
* in FIPS PUB 180-1
*
* by Lizq
*
* 2006-11-11
*
*/
/*
*
* Configurable variables.
*
*/
var hexcase = 0; /* Hex output format. 0-lowercase; 1-uppercase *
var chrsz = 8; /* bits per input character. 8-ascii; 16-unicode *
/*
*
* The main function to calculate message digest
*
*/
function Hex_sha1 (s) {
Return Binb2hex (CORE_SHA1 (AlignSHA1 (s)));
}
/*
*
* Perform a simple self-test to, if the VM is working
*
*/
function Sha1_vm_test () {
Return HEX_SHA1 ("abc") = = "a9993e364706816aba3e25717850c26c9cd0d89d";
}
/*
*
* Calculate The SHA-1 of an array of Big-endian words, and a bit length
*
*/
function Core_sha1 (Blockarray) {
var x = Blockarray; Append padding
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)//Handle 512 bits per 16*32
{
var Olda = A;
var oldb = b;
var oldc = c;
var oldd = D;
var olde = e;
for (var j = 0; J < J + +)///80 steps for each 512-bit
{
if (J < 16)
W[J] = X[i + j];
Else
W[J] = Rol (w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
var t = safe_add (Safe_add (Rol (A, 5), sha1_ft (J, B, C, D)), Safe_add (Safe_add (E, w[j)), Sha1_kt (j)));
e = D;
D = C;
c = Rol (b, 30);
b = A;
A = t;
}
A = Safe_add (A, Olda);
b = Safe_add (b, oldb);
c = Safe_add (c, OLDC);
D = Safe_add (d, OLDD);
E = Safe_add (e, Olde);
}
return new Array (A, B, C, D, E);
}
/*
*
* Perform the appropriate triplet combination function for the current
* Iteration
*
* Returns the value of the corresponding F function
*
*/
function sha1_ft (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; T<80
}
/*
*
* Determine the appropriate additive constant for the current iteration
*
* Return the corresponding KT value
*
*/
function Sha1_kt (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.
*
* The 32-digit number is split into a high 16-bit and a low 16-bit to add separately to achieve MOD 2^32 addition
*
*/
function Safe_add (x, y) {
var LSW = (x & 0xFFFF) + (Y & 0xFFFF);
var MSW = (x >>) + (y >>) + (LSW >> 16);
Return (MSW << 16) | (LSW & 0xFFFF);
}
/*
*
* Bitwise rotate a 32-bit number to the left.
*
* 32-bit binary number loop left shift
*
*/
function Rol (num, cnt) {
return (num << cnt) | (Num >>> (32-cnt));
}
/*
*
* The standard SHA1 needs the input string to fit into a block
*
* This function align the input string to meet the requirement
*
*/
function AlignSHA1 (str) {
var nblk = ((Str.length + 8) >> 6) + 1, blks = new Array (NBLK * 16);
for (var i = 0; i < NBLK * i++)
Blks[i] = 0;
for (i = 0; i < str.length; i++)
Blks[i >> 2] |= str.charcodeat (i) << (i & 3) * 8);
Blks[i >> 2] |= 0x80 << (i & 3) * 8);
BLKS[NBLK * 16-1] = str.length * 8;
return blks;
}
/*
*
* Convert An array of Big-endian words to a hex string.
*
*/
function Binb2hex (BinArray) {
var hex_tab = hexcase? "0123456789ABCDEF": "0123456789abcdef";
var str = "";
for (var i = 0; i < binarray.length * 4; i++) {
str = Hex_tab.charat (binarray[i >> 2] >> ((3-i% 4) * 8 + 4)) & 0xF) +
Hex_tab.charat ((binarray[i >> 2] >> ((3-i% 4) * 8)) & 0xF);
}
return str;
}
/*
*
* Calculate MessageDigest Accord to source "that inputted
*
*/
function Calcdigest () {
var digestm = hex_sha1 (document. SHAForm.SourceMessage.value);
Document. SHAForm.MessageDigest.value = digestm;
}
I hope this article will help you with your JavaScript programming.