MD5 Function for ColdFusion

boughtonp
boughtonp used Ask the Experts™
on
Unless I'm missing one, there doesn't seem to be one built in?

First person who can point me in the direction of a free function/custom tag/etc that can encrypt using the md5 algorith gets the points...

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Okay, there IS a function that does md5 as part of CFML, it's called "Hash".

Now, I can't be bothered to go get the question closed and I'm feeling generous, so the first person to answer with something remotely funny will get the points. :)

Commented:
Hi,

The "HASH" function ur talking abt works only with CFMX - I suppose

K'Rgds
Anand
Commented:
Here it is

http://cflib.org/udf.cfm?ID=35&dl=1

function MD5(message)
{
  Var hex_msg = "";
  Var hex_msg_len = 0;  
  Var padded_hex_msg = "";
  Var temp = "";  
  Var var1 = ArrayNew(1);
  Var f = 0;
  Var h = ArrayNew(1);
  Var i = 0;
  Var j = 0;
  Var k = ArrayNew(1);
  Var m = ArrayNew(1);
  Var n = 0;
  Var s = ArrayNew(1);  
  Var t = ArrayNew(1);
  // convert the msg to ASCII binary-coded form
  for (i=1; i LTE Len(message); i=i+1) {  
        hex_msg = hex_msg & Right("0"&FormatBaseN(Asc(Mid(message,i,1)),16),2);
  }    

  // compute the msg length in bits
  hex_msg_len = Right(RepeatString("0",15)&FormatBaseN(8*Len(message),16),16);
  for (i=1; i LTE 8; i=i+1) {
        temp = temp & Mid(hex_msg_len,-2*(i-8)+1,2);
  }
  hex_msg_len = temp;

  // pad the msg to make it a multiple of 512 bits long
  padded_hex_msg = hex_msg & "80" & RepeatString("0",128-((Len(hex_msg)+2+16) Mod 128)) & hex_msg_len;

  // initialize MD buffer
  h[1] = InputBaseN("0x67452301",16);
  h[2] = InputBaseN("0xefcdab89",16);
  h[3] = InputBaseN("0x98badcfe",16);
  h[4] = InputBaseN("0x10325476",16);

  var1[1] = "a";
  var1[2] = "b";
  var1[3] = "c";
  var1[4] = "d";
  // look at my crazy nested if action - courtesy of no elseif statement!
  for (i=1; i LTE 64; i=i+1) {
        t[i] = Int(2^32*abs(sin(i)));
        if (i LE 16){
              if (i EQ 1){
                    k[i] = 0;
      }
      else {
        k[i] = k[i-1] + 1;
      }
              s[i] = 5*((i-1) MOD 4) + 7;
        }
    else {
      if (i LE 32) {
        if (i EQ 17) {
                      k[i] = 1;
        }
        else {
          k[i] = (k[i-1]+5) MOD 16;
        }
              s[i] = 0.5*((i-1) MOD 4)*((i-1) MOD 4) + 3.5*((i-1) MOD 4) + 5;
      }
          else {
        if(i LE 48) {
          if (i EQ 33) {
                        k[i] = 5;
          }
          else {
            k[i] = (k[i-1]+3) MOD 16;
          }
                 s[i] = 6*((i-1) MOD 4) + ((i-1) MOD 2) + 4;
            }
        else {
          if (i EQ 49) {
            k[i] = 0;
          }
          else {
                        k[i] = (k[i-1]+7) MOD 16;
          }
                  s[i] = 0.5*((i-1) MOD 4)*((i-1) MOD 4) + 3.5*((i-1) MOD 4) + 6;
            }
      }
    }
  }

  // process the msg 512 bits at a time
  for (n=1; n LTE Evaluate(Len(padded_hex_msg)/128); n=n+1) {
        a = h[1];
        b = h[2];
        c = h[3];
        d = h[4];
      
        msg_block = Mid(padded_hex_msg,128*(n-1)+1,128);

    for (i=1; i LTE 16; i=i+1) {  
              sub_block = "";
      for (j=1; j LTE 4; j=j+1) {  
                sub_block = sub_block & Mid(msg_block,8*i-2*j+1,2);
              }
              m[i] = InputBaseN(sub_block,16);
        }

    for (i=1; i LTE 64; i=i+1) {        
          if (i LE 16) {
                    f = BitOr(BitAnd(Evaluate(var1[2]),Evaluate(var1[3])),BitAnd(BitNot(Evaluate(var1[2])),Evaluate(var1[4])));
              }
      else {
        if (i LE 32) {
          f = BitOr(BitAnd(Evaluate(var1[2]),Evaluate(var1[4])),BitAnd(Evaluate(var1[3]),BitNot(Evaluate(var1[4]))));
        }
        else {
          if (i LE 48) {
            f = BitXor(BitXor(Evaluate(var1[2]),Evaluate(var1[3])),Evaluate(var1[4]));
          }
                  else {
            f = BitXor(Evaluate(var1[3]),BitOr(Evaluate(var1[2]),BitNot(Evaluate(var1[4]))));
          }
                }
      }
              temp = Evaluate(var1[1]) + f + m[k[i]+1] + t[i];
              while ((temp LT -2^31) OR (temp GE 2^31)) {
        temp = temp - Sgn(temp)*2^32;
              }
              temp = Evaluate(var1[2]) + BitOr(BitSHLN(temp,s[i]),BitSHRN(temp,32-s[i]));
      while ((temp LT -2^31) OR (temp GE 2^31)) {
                    temp = temp - Sgn(temp)*2^32;
      }
              temp = SetVariable(var1[1],temp);
              temp = var1[4];
              var1[4] = var1[3];
              var1[3] = var1[2];
              var1[2] = var1[1];
              var1[1] = temp;
         }
      
        h[1] = h[1] + a;
         h[2] = h[2] + b;
        h[3] = h[3] + c;
        h[4] = h[4] + d;
       
    for (i=1; i LTE 4; i=i+1) {
      while ((h[i] LT -2^31) OR (h[i] GE 2^31)) {
                    h[i] = h[i] - Sgn(h[i])*2^32;
              }
        }
  }

  for (i=1; i LTE 4; i=i+1) {
        h[i] = Right(RepeatString("0",7)&UCase(FormatBaseN(h[i],16)),8);
  }

  for (i=1; i LTE 4; i=i+1) {  
        temp = "";
    for (j=1; j LTE 4; j=j+1) {
              temp = temp & Mid(h[i],-2*(j-4)+1,2);
        }
        h[i] = temp;
  }
  Return h[1] & h[2] & h[3] & h[4];
}

Author

Commented:
Anand, I'm only using CFMX so that's fine.

Jimmy, cool. That could come in useful if I want to write my own encryption methods I suppose. :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial