Implementation of MD5 algorithm in JS

xiaoxiao2021-03-06  109

Implementation of MD5 algorithm in JS

/ ************************************************** ********* MD5.JS ** A JavaScript Implementation of the RSA Data Security, Inc. Md5 * Message-Digest Algorithm. ** Copyright (c) Paul Johnston 1999. Distributed Under The LGPL. ** *********************************************************** ******* /

/ * TO Convert strings * / var sascii = "! /" # $% & '() * , -. / 0123456789:; <=>? @ AbcdefghijklmnopqrStuvwxyz "var sascii = sascii " [ //] ^ _ `Abcdefghijklmnopqrstuvwxyz {|} ~";

/ * Convert Integer to Hex String * / var shex = "0123456789abcdef"; Function HEX (i) {h = "; for (j = 0; j <= 3; J ) {h = shex.charat (i >> (j * 8 4)) & 0x0f) Shex.Charat ((i >> (j * 8)) & 0x0f);} Return H;

/ * add, handling overflows correctly * / function add (x, y) {return ((x & 0x7fffffff) (Y & 0x7FFFFFF)) ^ (x & 0x80000000) ^ (Y & 0x80000000);

/ * MD5 ROUNDS FUNCTION * / FUNCTION R1 (A, B, C, D, X, S, T) {q = add (add (A, (B & C) | (~ B & D)), add (x , T)); RETURN ADD ((q << s) | ((q >> (32 - s)) & (Math.Pow (2, s) - 1)), b);

Function R2 (A, B, C, D, X, S, T) {= add (add (A, (B & D) | (C & ~ D)), add (x, t)); Return Add ((Q << S) | ((Q >> (32 - S)) & (Math.Pow (2, S) - 1)), b);

Function R3 (A, B, C, D, X, S, T) {= add (add (A, B ^ C ^ D), ADD (X, T)); Return Add ((q << s) ((Q >> (32 - S)) & (Math.Pow (2, S) - 1)), B);

Function R4 (A, B, C, D, X, S, T) {= add (ADD (A, C ^ (B | ~ D)), ADD (X, T)); RETURN ADD ((q < > (32 - s)) & (Math.Pow (2, S) - 1)), b);

/ * Main entry point * / function calcmd5 (SINP) {

/ * Calculate Length in Machine Words, INCLUDING PADDING * / WLEN = ((SINP.LENGTH 8) >> 6) 1) << 4; var x = new array (WLEN); / * Convert String to array of Words * / j = 4; for (i = 0; (i * 4)

/ * Append padding bits and length * / if (j == 4) {x [i ] = 0x80;} else {x [i - 1] = 0x80 << (j * 8);} for (; i < Wlen; i ) {x [i] = 0;} x [Wlen - 2] = SINP.LENGTH * 8;

/ * Hard-coded initial value * / a = 0x67452301; b = 0xefcdab89; c = 0x98badcfe; d = 0x10325476;

/ * Process Each 16-Word Block in turn * / for (i = 0; i

A = R1 (A, B, C, D, X [i 0], 7, 0xD76AA478); D = R1 (D, A, B, C, X [i 1], 12, 0xE8C7B756); C = R1 ( C, D, A, B, X [i 2], 17, 0x242070dB); B = R1 (B, C, D, A, X [i 3], 22, 0xC1BDCEEE); A = R1 (A, B, C, D, X [i 4], 7, 0xF57c0FAF); D = R1 (D, A, B, C, X [i 5], 12, 0x4787C62A); C = R1 (C, D, A, B, X [i 6], 17, 0xA8304613); B = R1 (B, C, D, A, X [i 7], 22, 0xFD469501); A = R1 (A, B, C, D, X [i 8 ], 7, 0x698098d8); D = R1 (D, A, B, C, X [i 9], 12, 0x8B44F7AF); C = R1 (C, D, A, B, X [i 10], 17 0xffff5bb1); B = R1 (B, C, D, A, X [i 11], 22, 0x895cd7be); A = R1 (A, B, C, D, X [i 12], 7, 0x6b901122 ); D = R1 (D, A, B, C, X [i 13], 12, 0xFD987193); C = R1 (C, D, A, B, X [i 14], 17, 0xA679438E); B = R1 (B, C, D, A, X [i 15], 22, 0x49b40821);

A = R2 (A, B, C, D, X [i 1], 5, 0xF61e2562); D = R2 (D, A, B, C, X [i 6], 9, 0xc040b340); c = r2 ( C, D, A, B, X [i 11], 14, 0x265E5A51); B = R2 (B, C, D, A, X [i 0], 20, 0xE9B6C7AA); A = R2 (A, B , C, D, X [i 5], 5, 0xD62F105D); D = R2 (D, A, B, C, X [i 10], 9, 0x2441453); C = R2 (C, D, A, B, X [i 15], 14, 0xD8A1E681); B = R2 (B, C, D, A, X [i 4], 20, 0xE7D3FBC8); A = R2 (A, B, C, D, X [i 9], 5, 0x21e1cde6); D = R2 (D, A, B, C, X [i 14], 9, 0xc33707d6); C = R2 (C, D, A, B, X [i 3 ], 14, 0xF4D50D87); B = R2 (B, C, D, A, X [i 8], 20, 0x455A14ED); A = R2 (A, B, C, D, X [i 13], 5 0xA9E3E905); D = R2 (D, A, B, C, X [i 2], 9, 0xFCEFA3F8); C = R2 (C, D, A, B, X [i 7], 14, 0x676F02D9); B = R2 (B, C, D, A, X [i 12], 20, 0x8D2A4C8A); A = R3 (A, B, C, D, X [i 5], 4, 0xFFFA3942); D = R3 (D, A, B, C, X [i 8], 11, 0x8771F681); C = R3 (C, D, A, B, X [i 11], 16, 0x6d9d6122); b = r3 (b, C, D, A, X [i 14], 23, 0xFDE5380C); A = R3 (A, B, C, D, X [i 1], 4, 0xA4Beea44); D = R3 (D, A, B , C, X [i 4], 11, 0x4bdecfa9); C = R3 (C, D, A, B, X [i 7], 16, 0 XF6BB4B60); B = R3 (B, C, D, A, X [i 10], 23, 0xBebfbc70); A = R3 (A, B, C, D, X [i 13], 4, 0x289B7EC6) D = R3 (D, A, B, C, X [i 0], 11, 0xEAA127FA); C = R3 (C, D, A, B, X [i 3], 16, 0xD4ef3085); b = R3 (B, C, D, A, X [i 6], 23, 0x4881D05); A = R3 (A, B, C, D, X [i 9], 4, 0xD9D4D039); D = R3 (D, A , B, C, X [I 12], 11, 0xE6DB99E5); C = R3 (C, D, A, B, X [i 15], 16, 0x1FA27CF8); B = R3 (B, C, D , A, X [i 2], 23, 0xc4ac5665);

A = R4 (A, B, C, D, X [I 0], 6, 0xF4292244); D = R4 (D, A, B, C, X [i 7], 10, 0x432AFF97); C = R4 ( C, D, A, B, X [i 14], 15, 0xAb9423A7); B = R4 (B, C, D, A, X [i 5], 21, 0xFC93A039); A = R4 (A, B , C, D, X [i 12], 6, 0x655B59C3); D = R4 (D, A, B, C, X [i 3], 10, 0x8F0CC92); C = R4 (C, D, A, B, X [i 10], 15, 0xffeff47d); B = R4 (B, C, D, A, X [i 1], 21, 0x85845DD1); A = R4 (A, B, C, D, X [i 8], 6, 0x6FA87E4F); D = R4 (D, A, B, C, X [i 15], 10, 0xFe2Ce6e0); C = R4 (C, D, A, B, X [i 6 ], 15, 0xA3014314); B = R4 (B, C, D, A, X [i 13], 21, 0x4e0811a1); A = R4 (A, B, C, D, X [i 4], 6 0xF7537E82; D = R4 (D, A, B, C, X [i 11], 10, 0xBD3AF235); C = R4 (C, D, A, B, X [i 2], 15, 0x2AD7D2BB) B = R4 (B, C, D, A, X [i 9], 21, 0xEB86D391); A = Add (A, AO); B = Add (B, BO); C = Add (C, Co) ; D = add (d, do);} return (a) hex (b) hex (c) hex (d);}

转载请注明原文地址:https://www.9cbs.com/read-100351.html

New Post(0)