md2

Wikipedia: Message Digest Algorithm 2 (MD2) is a cryptographic hash function developed by Ronald Rivest in 1989. The algorithm is optimized for 8-bit computers. MD2 is specified in RFC 1319. Although other algorithms have been proposed since, such as MD4, MD5 and SHA, even as of 2004 MD2 remains in use in public key infrastructures as part of certificates generated with MD2 and RSA. The 128-bit (16-byte) MD2 hashes (also termed message digests) are typically represented as 32-digit hexadecimal numbers. Even a small change in the message will (with overwhelming probability) result in a completely different hash:
MD2("The quick brown fox jumps over the lazy dog")
= 03d85a0d629d2c442e987525319fc471
MD2("The quick brown fox jumps over the lazy cog")
= 6b890c9292668cdbbfda00a4ebf31f05

! NOTE: This is presented as a GM6-compatible alternative to MD5.

Downloadmd2(str)   Returns an MD2 hash (RFC 1319) for the given string.
/*
**  Usage:
**      md2(str)
**
**  Arguments:
**      str     string from which to compute an MD2 hash (RFC 1319)
**
**  Returns:
**      an MD2 hash (RFC 1319) computed from the given string
**
**  Notes:
**      This creates a lookup table in the form of a 256-entry
**      global array called MD2tab.
**
**  GMLscripts.com
*/

{
    var str,char,digest,i,j,k,L,M,N,X,pad;
    str = argument0;
    if (!variable_global_exists("MD2tab")) {
        var tab,i;
        tab  = "041046067201162216124001061054084161236240006019";
        tab += "098167005243192199115140152147043217188076130202";
        tab += "030155087060253212224022103066111024138023229018";
        tab += "190078196214218158222073160251245142187047238122";
        tab += "169104121145021178007063148194016137011034095033";
        tab += "128127093154090144050039053062204231191247151003";
        tab += "255025048179072165181209215094146042172086170198";
        tab += "079184056210150164125182118252107226156116004241";
        tab += "069157112089100113135032134091207101230045168002";
        tab += "027096037173174176185246028070097105052064126015";
        tab += "085071163035221081175058195092249206186197234038";
        tab += "044083013110133040132009211223205244065129077082";
        tab += "106220055200108193171250036225123008012189177074";
        tab += "120136149139227099232109233203213254059000029057";
        tab += "242239183014102088208228166119114248235117075010";
        tab += "049068080180143237031026219153141051159017131020";
        for(i=0;i<256;i+=1) {
            global.MD2tab[i] = real(string_copy(tab,i*3+1,3));
        }
    }
    N = string_length(str);
    pad = 16 - (N mod 16);
    str += string_repeat(chr(pad),pad);
    N = N + pad;
    for (i=0;i<N;i+=1) M[i]=ord(string_char_at(str,i+1));
    for (i=0;i<=15;i+=1) C[i] = 0;
    L = 0;
    for (i=0;i<(N div 16);i+=1) {
        for(j=0;j<=15;j+=1) {
            char = M[i*16+j];
            C[j] = C[j] ^ global.MD2tab[char ^ L];
            L = C[j];
        }
    }
    for (i=0;i<=15;i+=1) M[i+N] = C[i];
    N += 16;
    for (i=0;i<48;i+=1) X[i] = 0;
    for (i=0;i<(N div 16);i+=1) {
        for(j=0;j<=15;j+=1) {
            X[16+j] = M[i*16+j];
            X[32+j] = (X[16+j] ^ X[j]);
        }
        t = 0;
        for (j=0;j<18;j+=1) {
            for (k=0;k<48;k+=1) {
                t = X[k] ^ global.MD2tab[t];
                X[k] = t;
            }
            t = (t+j) mod 256;
        }
    }
    digest = "";
    for (i=0;i<=15;i+=1) {
        digest += string_copy("0123456789abcdef", X[i] div 16 + 1,1);
        digest += string_copy("0123456789abcdef", X[i] mod 16 + 1,1);
    }
    return digest;
}

Click if you've used this script[Please Login]
Projects: 3


comments powered by Disqus