日期:2014-05-16  浏览次数:20374 次

JavaScript 实现MD5算法
对MD5算法简要的叙述可以为:MD5以512位分组来处理输入的信息,且每一分组又被划分为16个32位子分组,经过了一系列的处理后,算法的输出由四个32位分组组成,将这四个32位分组级联后将生成一个128位散列值。

function MD5(sMessage) {  
function RotateLeft(lValue, iShiftBits) { return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits)); }  
function AddUnsigned(lX,lY) {  
    var lX4,lY4,lX8,lY8,lResult;  
    lX8 = (lX & 0x80000000);  
    lY8 = (lY & 0x80000000);  
    lX4 = (lX & 0x40000000);  
    lY4 = (lY & 0x40000000);  
    lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);  
    if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);  
    if (lX4 | lY4) {  
        if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);  
        else return (lResult ^ 0x40000000 ^ lX8 ^ lY8);  
    } else return (lResult ^ lX8 ^ lY8);  
}  
function F(x,y,z) { return (x & y) | ((~x) & z); }  
function G(x,y,z) { return (x & z) | (y & (~z)); }  
function H(x,y,z) { return (x ^ y ^ z); }  
function I(x,y,z) { return (y ^ (x | (~z))); }  
function FF(a,b,c,d,x,s,ac) {  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));  
    return AddUnsigned(RotateLeft(a, s), b);  
}  
function GG(a,b,c,d,x,s,ac) {  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));  
    return AddUnsigned(RotateLeft(a, s), b);  
}  
function HH(a,b,c,d,x,s,ac) {  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));  
    return AddUnsigned(RotateLeft(a, s), b);  
}  
function II(a,b,c,d,x,s,ac) {  
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));  
    return AddUnsigned(RotateLeft(a, s), b);  
}  
function ConvertToWordArray(sMessage) {  
    var lWordCount;  
    var lMessageLength = sMessage.length;  
    var lNumberOfWords_temp1=lMessageLength + 8;  
    var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;  
    var lNumberOfWords = (lNumberOfWords_temp2+1)*16;  
    var lWordArray=Array(lNumberOfWords-1);  
    var lBytePosition = 0;  
    var lByteCount = 0;  
    while ( lByteCount < lMessageLength ) {  
        lWordCount = (lByteCount-(lByteCount % 4))/4;  
        lBytePosition = (lByteCount % 4)*8;  
        lWordArray[lWordCount] = (lWordArray[lWordCount] | (sMessage.charCodeAt(lByteCount)<<lBytePosition));  
        lByteCount++;  
    }  
    lWordCount = (lByteCount-(lByteCount % 4))/4;  
    lBytePosition = (lByteCount % 4)*8;  
    lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);  
    lWordArray[lNumberOfWords-2] = lMessageLength<<3;  
    lWordArray[lNumberOfWords-1] = lMessageLength>>>29;  
    return lWordArray;  
}  
function WordToHex(lValue) {  
    var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;  
    for (lCount = 0;lCount<=3;lCount++) {  
        lByte = (lValue>>>(lCount*8)) & 255;  
        WordToHexValue_temp = "0" + lByte.toString(16);  
        WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);  
    }  
    return WordToHexValue;  
}  
    var x=Array();  
    var k,AA,BB,CC,DD,a,b,c,d  
    var S11=7, S12=12, S13=17, S14=22;  
    var S21=5, S22=9 , S23=14, S24=20;  
    var S31=4, S32=11, S33=16, S34=23;  
    var S41=6, S42=10, S43=15, S44=21;  
    // Steps 1 and 2.  Append padding bits and length and convert to words  
    x = ConvertToWordArray(sMessage);  
    // Step 3.  Initialise  
    a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;  
    // Step 4.  Process the message in 16-word blocks  
    for (k=0;k<x.length;k+=16) {  
        AA=a; BB=b; CC=c; DD=d;  
        a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);  
        d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);  
        c=FF(c,d,a,b,x[k+2], S13,0x242070DB);  
        b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);  
        a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);  
        d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);