Encryption algorithms such as MD5, SHA1, and TEA of AS2

Source: Internet
Author: User

The encryption algorithm class of AS2 written by meychi.
Package, download, and save it as a rarfile.
1. MD5: static class
/**
* MD5 Class v1.0
* Author: Mika Palmu
* Licence: Use freely, giving credit when you can.
* Website: http://www.meychi.com/
*
* Orginal Javascript implementation:
* RSA Data Security, Inc. MD5 Message Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright Paul Johnston 1999-2002
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* See http://pajhome.org.uk/crypt/md5 for more info.
*/

Class MD5 {

/**
* Variables
* Hexcase: hex output format. 0-lowercase; 1-uppercase
* B64pad: base-64 pad character. "=" for strict RFC compliance
* Chrsz: bits per input character. 8-ASCII; 16-Unicode
*/
Public static var hexcase: Number = 0;
Public static var b64pad: String = "";
Public static var chrsz: Number = 8;

/**
* Public methods
*/
Public static function test (): String {return md5_vm_test ();};
Public static function calculate (s: String): String {return hex_md5 (s );};
Public static function hex_hmac_md5 (key, data) {return binl2hex (core_hmac_md5 (key, data ));}
Public static function b64_hmac_md5 (key, data) {return binl2b64 (core_hmac_md5 (key, data ));}
Public static function str_hmac_md5 (key, data) {return binl2str (core_hmac_md5 (key, data ));}

/**
* Private methods
*/
Private static function hex_md5 (s) {return binl2hex (core_md5 (str2binl (s), s. length * chrsz ));}
Private static function b64_md5 (s) {return binl2b64 (core_md5 (str2binl (s), s. length * chrsz ));}
Private static function str_md5 (s) {return binl2str (core_md5 (str2binl (s), s. length * chrsz ));}
Private static function md5_vm_test () {return hex_md5 ("abc") = "900150983cd24fb0d6963f7d28e17f72 ";}
Private static function core_md5 (x, len ){
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 = a; 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 new Array (a, B, c, d );
}
Private static function md5_cen (q, a, B, x, s, t) {return safe_add (bit_rol (safe_add (a, q), safe_add (x, t )), s), B );}
Private static function md5_ff (a, B, c, d, x, s, t) {return md5_cen (B & c) | ((~ B) & d), a, B, x, s, t );}
Private static function md5_gg (a, B, c, d, x, s, t) {return md5_cen (B & d) | (c &(~ D), a, B, x, s, t );}
Private static function md5_hh (a, B, c, d, x, s, t) {return md5_cen (B ^ c ^ d, a, B, x, s, t );}
Private static function md5_ii (a, B, c, d, x, s, t) {return md5_cen (c ^ (B | (~ D), a, B, x, s, t );}
Private static function core_hmac_md5 (key, data ){
Var bkey = str2binl (key );
If (bkey. length> 16) bkey = core_md5 (bkey, key. length * chrsz );
Var ipad = new Array (16), opad = new Array (16 );
For (var I = 0; I <16; I ++ ){
Ipad= Bkey ^ 0x36363636; opad = bkey ^ 0x5C5C5C5C;
}
Var hash = core_md5 (ipad. concat (str2binl (data), 512 + data. length * chrsz );
Return core_md5 (opad. concat (hash), 512 + 128 );
}
Private static 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 );
}
Private static function bit_rol (num, cnt) {return (num <cnt) | (num >>> (32-cnt ));}
Private static function str2binl (str ){
Var bin = new 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;
}
Private static 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;
}
Private static 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;
}
Private static 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;
}
}

Usage:Import MD5;
Var tt: String = "JimLee ";

Trace (MD5.calculate (tt ));

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.