Javascrip implements MD5 and sha1 Encryption

Source: Internet
Author: User
Tags crypt hmac sha1 encryption
Sha1 Algorithm
Call method: hex_sha1 ("ABC") program code/* sha1
* A JavaScript Implementation of the secure hash algorithm, SHA-1, as defined
* In FIPS pub 180-1
* Version 2.1a copyright Paul Johnston 2000-2002.
* Other contributors: Greg Holt, Andrew kepert, ydnar, lostinet
* Distributed under the BSD license
* See http://pajhome.org.uk/crypt/md5 for details.
*/

/*
* retriable variables. you may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0;/* hex output format. 0-lowercase; 1-UPPERCASE */
var b64pad = "";/* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8;/* bits per input character. 8-ASCII; 16-Unicode */

/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
Function hex_sha1 (s) {return binb2hex (core_sha1 (str2binb (s), S. length * chrsz ));}
Function b64_sha1 (s) {return binb2b64 (core_sha1 (str2binb (s), S. length * chrsz ));}
Function str_sha1 (s) {return binb2str (core_sha1 (str2binb (s), S. length * chrsz ));}
Function hex_hmac_sha1 (Key, data) {return binb2hex (core_hmac_sha1 (Key, data ));}
Function b64_hmac_sha1 (Key, data) {return binb2b64 (core_hmac_sha1 (Key, data ));}
Function str_hmac_sha1 (Key, data) {return binb2str (core_hmac_sha1 (Key, data ));}

/*
* 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 (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 =;
VaR oldb = B;
VaR oldc = C;
VaR oldd = D;
VaR Olde = E;

for (var j = 0; j <80; j ++)
{< br> 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]), 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 array (A, B, C, D, E );

}

/*
* Perform the appropriate triplet combination function for the current
* Iteration
*/
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;
}

/*
* Determine the appropriate additive constant for the current iteration
*/
Function shaw.kt (t)
{
Return (T <20 )? 1518500249: (t <40 )? 1859775393:
(T <60 )? -1894007588:-899497514;
}

/*
* Calculate the HMAC-SHA1 of a key and some data
*/
Function core_hmac_sha1 (Key, data)
{
VaR bkey = str2binb (key );
If (bkey. length> 16) bkey = core_sha1 (bkey, key. length * chrsz );

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 = core_sha1 (IPAD. Concat (str2binb (data), 512 + data. length * chrsz );
Return core_sha1 (OPAD. Concat (hash), 512 + 160 );
}

/*
* Add integers, wrapping at 2 ^ 32. This uses 16-bit operations internally
* To work around und bugs in some JS interpreters.
*/
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.
*/
Function ROL (Num, CNT)
{
Return (Num <CNT) | (Num >>> (32-CNT ));
}

/*
* Convert an 8-bit or 16-bit string to an array of big-Endian words
* In 8-bit function, characters> 255 have their hi-byte silently ignored.
*/
Function str2binb (STR)
{
VaR bin = array ();
VaR mask = (1 <chrsz)-1;
For (VAR I = 0; I <Str. length * chrsz; I + = chrsz)
Bin [I> 5] | = (Str. charcodeat (I/chrsz) & Mask) <(32-chrsz-I % 32 );
Return bin;
}

/*
* Convert an array of big-Endian words to a string
*/
Function binb2str (BIN)
{
VaR STR = "";
VaR mask = (1 <chrsz)-1;
For (VAR I = 0; I <bin. length * 32; I + = chrsz)
STR + = string. fromcharcode (bin [I> 5] >>> (32-chrsz-I % 32) & Mask );
Return STR;
}

/*
* 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;
}

/*
* Convert an array of big-Endian words to a base-64 string
*/
Function binb2b64 (binarray)
{
VaR tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
VaR STR = "";
For (VAR I = 0; I <binarray. length * 4; I + = 3)
{
VaR triplet = (binarray [I> 2]> 8 * (3-I % 4) & 0xff) <16)
| (Binarray [I + 1> 2]> 8 * (3-(I + 1) % 4) & 0xff) <8)
| (Binarray [I + 2> 2]> 8 * (3-(I + 2) % 4) & 0xff );
For (var j = 0; j <4; j ++)
{
If (I * 8 + J * 6> binarray. length * 32) STR + = b64pad;
Else STR + = tab. charat (triplet> 6 * (3-J) & 0x3f );
}
}
Return STR;
}

MD5 Algorithm
Call: hex_md5 ("ABC") program code/* MD5
* A JavaScript Implementation of the RSA Data Security, Inc. MD5 message
* Digest algorithm, as defined in RFC 1321.
* Version 2.1 copyright (c) Paul Johnston 1999-2002.
* Other contributors: Greg Holt, Andrew kepert, ydnar, lostinet
* Distributed under the BSD license
* See http://pajhome.org.uk/crypt/md5 for more info.
*/

/*
* retriable variables. you may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0;/* hex output format. 0-lowercase; 1-UPPERCASE */
var b64pad = "";/* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8;/* bits per input character. 8-ASCII; 16-Unicode */

/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
Function hex_md5 (s) {return binl2hex (core_md5 (str2binl (s), S. length * chrsz ));}
Function b64_md5 (s) {return binl2b64 (core_md5 (str2binl (s), S. length * chrsz ));}
Function str_md5 (s) {return binl2str (core_md5 (str2binl (s), S. length * chrsz ));}
Function hex_hmac_md5 (Key, data) {return binl2hex (core_hmac_md5 (Key, data ));}
Function b64_hmac_md5 (Key, data) {return binl2b64 (core_hmac_md5 (Key, data ));}
Function str_hmac_md5 (Key, data) {return binl2str (core_hmac_md5 (Key, data ));}

/*
* Perform a simple self-test to see if the VM is working
*/
Function md5_vm_test ()
{
Return hex_md5 ("ABC") = "900150983cd24fb0d6963f7d28e17f72 ";
}

/*
* Calculate the MD5 of an array of little-Endian words, and a bit length
*/
Function core_md5 (x, Len)
{
/* Append padding */
X [Len> 5] | = 0x80 <(LEN) % 32 );
X [(LEN + 64) >>> 9) <4) + 14] = Len;

VaR A = 1732584193;
VaR B =-271733879;
VaR c =-1732584194;
VaR d = 271733878;

For (VAR I = 0; I <X. length; I + = 16)
{
VaR olda =;
VaR oldb = B;
VaR oldc = C;
VaR oldd = D;

A = md5_ff (a, B, c, d, X [I + 0], 7,-680876936 );
D = md5_ff (D, a, B, c, X [I + 1], 12,-389564586 );
C = md5_ff (c, d, A, B, X [I + 2], 17,606 105819 );
B = md5_ff (B, c, d, A, X [I + 3], 22,-1044525330 );
A = md5_ff (a, B, c, d, X [I + 4], 7,-176418897 );
D = md5_ff (D, a, B, c, X [I + 5], 12,120 0080426 );
C = md5_ff (c, d, A, B, X [I + 6], 17,- 1473231341 );
B = md5_ff (B, c, d, A, X [I + 7], 22,-45705983 );
A = md5_ff (a, B, c, d, X [I + 8], 7, 1770035416 );
D = md5_ff (D, a, B, c, X [I + 9], 12,-1958414417 );
C = md5_ff (c, d, A, B, X [I + 10], 17,- 42063 );
B = md5_ff (B, c, d, A, X [I + 11], 22,-1990404162 );
A = md5_ff (a, B, c, d, X [I + 12], 7, 1804603682 );
D = md5_ff (D, a, B, c, X [I + 13], 12,-40341101 );
C = md5_ff (c, d, A, B, X [I + 14], 17,- 1502002290 );
B = md5_ff (B, c, d, A, X [I + 15], 22,123 6521329 );

A = md5_gg (a, B, c, d, X [I + 1], 5,-165796510 );
D = md5_gg (D, a, B, c, X [I + 6], 9,-1069501632 );
C = md5_gg (c, d, A, B, X [I + 11], 14,643 717713 );
B = md5_gg (B, c, d, A, X [I + 0], 20,-373897302 );
A = md5_gg (a, B, c, d, X [I + 5], 5,-701558691 );
D = md5_gg (D, a, B, c, X [I + 10], 9, 38016083 );
C = md5_gg (c, d, A, B, X [I + 15], 14,-660478335 );
B = md5_gg (B, c, d, A, X [I + 4], 20,-405537848 );
A = md5_gg (a, B, c, d, X [I + 9], 5, 568446438 );
D = md5_gg (D, a, B, c, X [I + 14], 9,-1019803690 );
C = md5_gg (c, d, A, B, X [I + 3], 14,-187363961 );
B = md5_gg (B, c, d, A, X [I + 8], 20,116 limit 1501 );
A = md5_gg (a, B, c, d, X [I + 13], 5,-1444681467 );
D = md5_gg (D, a, B, c, X [I + 2], 9,-51403784 );
C = md5_gg (c, d, A, B, X [I + 7], 14,173 5328473 );
B = md5_gg (B, c, d, A, X [I + 12], 20,-1926607734 );

A = md5_hh (a, B, c, d, X [I + 5], 4,-378558 );
D = md5_hh (D, a, B, c, X [I + 8], 11,-2022574463 );
C = md5_hh (c, d, A, B, X [I + 11], 16,183 9030133 );
B = md5_hh (B, c, d, A, X [I + 14], 23,-35309556 );
A = md5_hh (a, B, c, d, X [I + 1], 4,-1530992060 );
D = md5_hh (D, a, B, c, X [I + 4], 11,127 2893353 );
C = md5_hh (c, d, A, B, X [I + 7], 16,-155497632 );
B = md5_hh (B, c, d, A, X [I + 10], 23,-1094730640 );
A = md5_hh (a, B, c, d, X [I + 13], 4, 681279174 );
D = md5_hh (D, a, B, c, X [I + 0], 11,-358537222 );
C = md5_hh (c, d, A, B, X [I + 3], 16,-722521979 );
B = md5_hh (B, c, d, A, X [I + 6], 23,760 29189 );
A = md5_hh (a, B, c, d, X [I + 9], 4,-640364487 );
D = md5_hh (D, a, B, c, X [I + 12], 11,-421815835 );
C = md5_hh (c, d, A, B, X [I + 15], 16,530 742520 );
B = md5_hh (B, c, d, A, X [I + 2], 23,-995338651 );

A = md5_ii (a, B, c, d, X [I + 0], 6,-198630844 );
D = md5_ii (D, a, B, c, X [I + 7], 10,112 6891415 );
C = md5_ii (c, d, A, B, X [I + 14], 15,-1416354905 );
B = md5_ii (B, c, d, A, X [I + 5], 21,-57434055 );
A = md5_ii (a, B, c, d, X [I + 12], 6, 1700485571 );
D = md5_ii (D, a, B, c, X [I + 3], 10,-1894986606 );
C = md5_ii (c, d, A, B, X [I + 10], 15,-1051523 );
B = md5_ii (B, c, d, A, X [I + 1], 21,-2054922799 );
A = md5_ii (a, B, c, d, X [I + 8], 6, 1873313359 );
D = md5_ii (D, a, B, c, X [I + 15], 10,-30611744 );
C = md5_ii (c, d, A, B, X [I + 6], 15,-1560198380 );
B = md5_ii (B, c, d, A, X [I + 13], 21,130 9151649 );
A = md5_ii (a, B, c, d, X [I + 4], 6,-145523070 );
D = md5_ii (D, a, B, c, X [I + 11], 10,-1120210379 );
C = md5_ii (c, d, A, B, X [I + 2], 15,718 787259 );
B = md5_ii (B, c, d, A, X [I + 9], 21,-343485551 );

A = safe_add (A, olda );
B = safe_add (B, oldb );
C = safe_add (C, oldc );
D = safe_add (D, oldd );
}
Return array (A, B, C, D );

}

/*
* These functions implement the four basic operations the algorithm uses.
*/
Function md5_cen (Q, A, B, X, S, T)
{
Return safe_add (bit_rol (safe_add (A, q), safe_add (x, t), S), B );
}
Function md5_ff (a, B, c, d, X, S, T)
{
Return md5_cen (B & C) | ((~ B) & D), a, B, X, S, T );
}
Function md5_gg (a, B, c, d, X, S, T)
{
Return md5_cen (B & D) | (C &(~ D), a, B, X, S, T );
}
Function md5_hh (a, B, c, d, X, S, T)
{
Return md5_cen (B ^ C ^ D, a, B, X, S, T );
}
Function md5_ii (a, B, c, d, X, S, T)
{
Return md5_cen (C ^ (B | (~ D), a, B, X, S, T );
}

/*
* Calculate the HMAC-MD5, of a key and some data
*/
Function core_hmac_md5 (Key, data)
{
VaR bkey = str2binl (key );
If (bkey. length> 16) bkey = core_md5 (bkey, key. length * chrsz );

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 = core_md5 (IPAD. Concat (str2binl (data), 512 + data. length * chrsz );
Return core_md5 (OPAD. Concat (hash), 512 + 128 );
}

/*
* Add integers, wrapping at 2 ^ 32. This uses 16-bit operations internally
* To work around und bugs in some JS interpreters.
*/
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.
*/
Function bit_rol (Num, CNT)
{
Return (Num <CNT) | (Num >>> (32-CNT ));
}

/*
* Convert a string to an array of little-Endian words
* If chrsz is ASCII, characters> 255 have their hi-byte silently ignored.
*/
Function str2binl (STR)
{
VaR bin = array ();
VaR mask = (1 <chrsz)-1;
For (VAR I = 0; I <Str. length * chrsz; I + = chrsz)
Bin [I> 5] | = (Str. charcodeat (I/chrsz) & Mask) <(I % 32 );
Return bin;
}

/*
* Convert an array of little-Endian words to a string
*/
Function binl2str (BIN)
{
VaR STR = "";
VaR mask = (1 <chrsz)-1;
For (VAR I = 0; I <bin. length * 32; I + = chrsz)
STR + = string. fromcharcode (bin [I> 5] >>> (I % 32) & Mask );
Return STR;
}

/*
* Convert an array of little-Endian words to a hex string.
*/
Function binl2hex (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]> (I % 4) * 8 + 4) & 0xf) +
Hex_tab.charat (binarray [I> 2]> (I % 4) * 8) & 0xf );
}
Return STR;
}

/*
* Convert an array of little-Endian words to a base-64 string
*/
Function binl2b64 (binarray)
{
VaR tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 + /";
VaR STR = "";
For (VAR I = 0; I <binarray. length * 4; I + = 3)
{
VaR triplet = (binarray [I> 2]> 8 * (I % 4) & 0xff) <16)
| (Binarray [I + 1> 2]> 8 * (I + 1) % 4) & 0xff) <8)
| (Binarray [I + 2> 2]> 8 * (I + 2) % 4) & 0xff );
For (var j = 0; j <4; j ++)
{
If (I * 8 + J * 6> binarray. length * 32) STR + = b64pad;
Else STR + = tab. charat (triplet> 6 * (3-J) & 0x3f );
}
}
Return STR;
}

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.