Method of implementing SHA-1 Encryption Algorithm in JavaScript, javascriptsha-1

Source: Internet
Author: User

Method of implementing SHA-1 Encryption Algorithm in JavaScript, javascriptsha-1

This example describes how to implement the SHA-1 Encryption Algorithm in JavaScript. Share it with you for your reference. The specific implementation method is as follows:

Call method: hex_sha1.
Copy codeThe Code is as follows :/*
*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* In fips pub 180-1
*
* By lizq
*
* 2006-11-
*
*/
/*
*
* Retriable 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 see if the VM is working
*
*/
Function shaw.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) // 512-bit 16*32
{
Var olda =;
Var oldb = B;
Var oldc = c;
Var oldd = d;
Var olde = e;
For (var j = 0; j <80; j ++) // perform step 80 for each 512 bits
{
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 (rol (a, 5), sha1_ft (j, B, c, d), safe_add (e, w [j]), shaw.kt (j )));
E = d;
D = c;
C = rol (B, 30 );
B =;
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
*
* Returns 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 und bugs in some JS interpreters.
*
* Divide the 32-bit values into 16-bit high and 16-bit low, respectively, to add MOD 2 ^ 32.
*
*/
Function safe_add (x, y ){
Var lsw = (x & 0 xFFFF) + (y & 0 xFFFF );
Var msw = (x> 16) + (y> 16) + (lsw> 16 );
Return (msw <16) | (lsw & 0 xFFFF );
}
/*
*
* Bitwise rotate a 32-bit number to the left.
*
* 32-bit binary number shifts left
*
*/
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 * 16; I ++)
Blks [I] = 0;
For (I = 0; I <str. length; I ++)
Blks [I> 2] | = str. charCodeAt (I) <(24-(I & 3) * 8 );
Blks [I> 2] | = 0x80 <(24-(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? "0123456789 ABCDEF": "0123456789 abcdef ";
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 message that inputted
*
*/
Function calcDigest (){
Var digestM = hex_sha1 (document. SHAForm. SourceMessage. value );
Document. SHAForm. MessageDigest. value = digestM;
}

I hope this article will help you design javascript programs.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.