日期:2008-10-13  浏览次数:21032 次

/* MD5 Message-Digest Algorithm - JavaScript
' MODIFICATION HISTORY:
' 1.0    16-Feb-2001 - Phil Fresle (sales@frez.co.uk) - Initial Version (VB/ASP code)

' 1.0    21-Feb-2001 - Enrico Mosanghini (erik504@yahoo.com) - JavaScript porting
*/
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<&l