Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
change LocZ
Cut, Copy, Paste behavior 1.1
Slider-Alphamania
myDialog
Multiple Monitors with Director
FilmLoop Controller
Show Time
Grabbing the Stage (D7)
Testing CPU speed
How to find all special system folders
 

 

 

Behavior MD5

Added on 3/2/2003

 

Compatibilities:
D7 D8 D8_5 D9 Mac PC Script Shockwave

This item has not yet been rated

Author: MediaMacros (website)

MD5 routine using only lingo. This also uses custom bitshift handlers to handle bitshift left and right.

--MD5 Encryption Routine
--Created by Chuck Neal and Thomas K. Laursen
--Copyright 2003, MediaMacros, Inc. / Chimp and Tiger, Inc.
--Debugging, javascript example testing, and moral support by Thomas Laursen
--Based on the Javascript version by Paul Johnston
--http://pajhome.org.uk/crypt/md5/md5src.html

-- Take a string and return the hex representation of its MD5.
on calcMD5 str
  x = str2blks_MD5(str)
  a =  1732584193
  b = -271733879
  c = -1732584194
  d =  271733878  
  i = 1
  repeat while i <= x.count
    olda = a
    oldb = b
    oldc = c
    oldd = d    
    a = ff(a, b, c, d, x[i+ 0], 7 , -680876936)
    d = ff(d, a, b, c, x[i+ 1], 12, -389564586)
    c = ff(c, d, a, b, x[i+ 2], 17,  606105819)
    b = ff(b, c, d, a, x[i+ 3], 22, -1044525330)
    a = ff(a, b, c, d, x[i+ 4], 7 , -176418897)
    d = ff(d, a, b, c, x[i+ 5], 12,  1200080426)
    c = ff(c, d, a, b, x[i+ 6], 17, -1473231341)
    b = ff(b, c, d, a, x[i+ 7], 22, -45705983)
    a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416)
    d = ff(d, a, b, c, x[i+ 9], 12, -1958414417)
    c = ff(c, d, a, b, x[i+10], 17, -42063)
    b = ff(b, c, d, a, x[i+11], 22, -1990404162)
    a = ff(a, b, c, d, x[i+12], 7 ,  1804603682)
    d = ff(d, a, b, c, x[i+13], 12, -40341101)
    c = ff(c, d, a, b, x[i+14], 17, -1502002290)
    b = ff(b, c, d, a, x[i+15], 22,  1236535329)
    a = gg(a, b, c, d, x[i+ 1], 5 , -165796510)
    
    d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632)
    c = gg(c, d, a, b, x[i+11], 14,  643717713)
    b = gg(b, c, d, a, x[i+ 0], 20, -373897302)
    a = gg(a, b, c, d, x[i+ 5], 5 , -701558691)
    d = gg(d, a, b, c, x[i+10], 9 ,  38016083)
    c = gg(c, d, a, b, x[i+15], 14, -660478335)
    b = gg(b, c, d, a, x[i+ 4], 20, -405537848)
    a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438)
    d = gg(d, a, b, c, x[i+14], 9 , -1019803690)
    c = gg(c, d, a, b, x[i+ 3], 14, -187363961)
    b = gg(b, c, d, a, x[i+ 8], 20,  1163531501)
    a = gg(a, b, c, d, x[i+13], 5 , -1444681467)
    d = gg(d, a, b, c, x[i+ 2], 9 , -51403784)
    c = gg(c, d, a, b, x[i+ 7], 14,  1735328473)
    b = gg(b, c, d, a, x[i+12], 20, -1926607734)
    
    a = hh(a, b, c, d, x[i+ 5], 4 , -378558)
    d = hh(d, a, b, c, x[i+ 8], 11, -2022574463)
    c = hh(c, d, a, b, x[i+11], 16,  1839030562)
    b = hh(b, c, d, a, x[i+14], 23, -35309556)
    a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060)
    d = hh(d, a, b, c, x[i+ 4], 11,  1272893353)
    c = hh(c, d, a, b, x[i+ 7], 16, -155497632)
    b = hh(b, c, d, a, x[i+10], 23, -1094730640)
    a = hh(a, b, c, d, x[i+13], 4 ,  681279174)
    d = hh(d, a, b, c, x[i+ 0], 11, -358537222)
    c = hh(c, d, a, b, x[i+ 3], 16, -722521979)
    b = hh(b, c, d, a, x[i+ 6], 23,  76029189)
    a = hh(a, b, c, d, x[i+ 9], 4 , -640364487)
    d = hh(d, a, b, c, x[i+12], 11, -421815835)
    c = hh(c, d, a, b, x[i+15], 16,  530742520)
    b = hh(b, c, d, a, x[i+ 2], 23, -995338651)
    
    a = ii(a, b, c, d, x[i+ 0], 6 , -198630844)
    d = ii(d, a, b, c, x[i+ 7], 10,  1126891415)
    c = ii(c, d, a, b, x[i+14], 15, -1416354905)
    b = ii(b, c, d, a, x[i+ 5], 21, -57434055)
    a = ii(a, b, c, d, x[i+12], 6 ,  1700485571)
    d = ii(d, a, b, c, x[i+ 3], 10, -1894986606)
    c = ii(c, d, a, b, x[i+10], 15, -1051523)
    b = ii(b, c, d, a, x[i+ 1], 21, -2054922799)
    a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359)
    d = ii(d, a, b, c, x[i+15], 10, -30611744)
    c = ii(c, d, a, b, x[i+ 6], 15, -1560198380)
    b = ii(b, c, d, a, x[i+13], 21,  1309151649)
    a = ii(a, b, c, d, x[i+ 4], 6 , -145523070)
    d = ii(d, a, b, c, x[i+11], 10, -1120210379)
    c = ii(c, d, a, b, x[i+ 2], 15,  718787259)
    b = ii(b, c, d, a, x[i+ 9], 21, -343485551)
    
    a = addB(a, olda)
    b = addB(b, oldb)
    c = addB(c, oldc)
    d = addB(d, oldd)
    i = i + 16      
  end repeat  
  return rhex(a) & rhex(b) & rhex(c) & rhex(d)
end


----Supporting Handlers-----
--Director is missing Bitshift handlers so These take care of this...
on getBinary vNum, vDepth
  if vDepth = void then vDepth = 32
  baseNums = ["0","1"]
  n = ""
  neg = 0
  if vnum < 0 then
    neg = 1
    vnum = integer(the maxInteger + 1.0 + vNum)
  end if  
  vNum = integer(vNum)
  repeat while vNum
    nMod = vNum mod 2
    put string(baseNums[nMod + 1]) before n
    vNum = vNum / 2
  end repeat
  --add preceeding 0
  repeat while n.char.count < vDepth - 1
    put "0" before n
  end repeat
  if n.char.count < vDepth then
   put neg before n
    end if
    return n
end

on getInt vBinary
  baseNums = ["0","1"]
  if vBinary.char[1] = "1" then negative = 1
  nConverted = 0
  nMax = vBinary.length
  repeat with nIndex = 1 to nMax
    nConverted = nConverted * 2
    nConverted = nConverted + baseNums.getOne(vBinary.char[nIndex]) - 1
  end repeat  
  return nConverted  
end

on bitShiftLeft vNum, vShift, vDepth
  --return integer((vNum * (power(2,vShift))) - .5)
  if vDepth = void then vDepth = 32
  iBinary = getBinary(vNum, vDepth)
  --get a positive value
  vShift = min(32, abs(vShift))
  nBinary = iBinary.char[(vShift + 1)..vDepth] --& iBinary.char[1..vShift]
  --pad with zeros
  repeat while nBinary.char.count < vDepth
    put "0" after nBinary
  end repeat
  
  nNumber = getInt(nBinary)
  return nNumber
end

on bitShiftRight vNum, VShift, vDepth
  if vDepth = void then vDepth = 32
  iBinary = getBinary(vNum, vDepth)
  --get a positive value
  vShift = min(vDepth, abs(vShift))
  nBinary = iBinary.char[1..(vDepth - vShift)]
  repeat while nBinary.char.count < vDepth
    put nBinary.char[1] before nBinary  
  end repeat
  nNumber = getInt(nBinary)
  return nNumber
end

on bitShiftRightZeroFill vNum, vShift, vDepth
  if vDepth = void then vDepth = 32
  iBinary = getBinary(vNum, vDepth)
  --get a positive value
  vShift = min(vDepth, abs(vShift))
  nBinary = iBinary.char[1..(vDepth - vShift)]
  repeat while nBinary.char.count < vDepth
  put "0" before nBinary  
  end repeat
    nNumber = getInt(nBinary)
  return nNumber
end

on hexToInt hexNum
  hexdigits = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"]
  numstring = 0
  repeat with x = 1 to hexnum.char.count
    numString = numString * 16
    numString = numString + hexDigits.getOne(hexNum.char[x]) - 1  
  end repeat
  return numString
end

-- Convert a 32-bit number to a hex string with ls-byte first
on rhex num
  hex_chr = "0123456789abcdef"
  str = ""
  repeat with j = 0 to 3    
    put hex_chr.char[(bitAnd(bitShiftRight(num, (j * 8 + 4)) , 15) mod 16) + 1] & hex_chr.char[(bitAnd(bitShiftRight(num, (j * 8)), 15) mod 16) + 1] after str
  end repeat
  return str
end

-- Convert a string to a sequence of 16-word blocks, stored as an array.
-- Append paddBing bits and the length, as described in the MD5 standard.

on str2blks_MD5 str
  nblk = bitShiftRight((str.char.count + 8), 6) + 1
  blks = []
  --fill the array with 0's  
  repeat with i = 1 to (nblk * 16)
    blks.add(0)    
  end repeat
  --cycle through the strings length
  repeat with i = 0 to (str.char.count - 1)    
    blks[(bitShiftRight(i, 2)) + 1] = bitOr(blks[(bitShiftRight(i, 2)) + 1], bitShiftLeft(charToNum(str.char[i + 1]), (i mod 4) * 8))
    
  end repeat
  --blks[i >> 2] |= 0x80 << ((i % 4) * 8);
  blks[(bitShiftRight(i, 2)) + 1] = bitOr(blks[(bitShiftRight(i, 2)) + 1], bitShiftLeft(128, ((i mod 4) * 8)))
  --blks[(bitShiftRight(i, 2)) + 1] = bitShiftLeft(bitOr( blks[(bitShiftRight(i, 2)) + 1],  hexToInt("80")), ((i mod 4) * 8))
  
  blks[nblk * 16 - 1] = str.char.count * 8
  return blks
end

-- addB integers, wrapping at 2^32. This uses 16-bit operations internally
-- to work around bugs in some JS interpreters.

on addB x, y
    lsw = bitand(x, 65535) + bitand(y, 65535)
  msw = bitShiftRight(x, 16) + bitShiftRight(y, 16) + bitShiftRight(lsw ,16)
  return bitOr(bitShiftLeft(msw, 16), bitAnd(lsw, 65535))
end

-- Bitwise rotate a 32-bit number to the left

on rol num, cnt
  cnt = cnt mod 32
  -- return (num << cnt) | (num >>> (32 - cnt));
  iBinary = getBinary(num, 32)
  --shift the numbers to the left
  shift = cnt mod 32
  if cnt > 0 then
    nBinary = iBinary.char[cnt + 1..iBinary.char.count] & ibinary.char[1..cnt]
  else
    nBinary = iBinary
  end if    
  --return bitOr(bitSHiftLeft(num, cnt), bitShftRightZeroFill(num, (32 - cnt)))
  return getInt(nBinary)
end

-- These ons implement the basic operation for each round of the
-- algorithm.
on cmn q, a, b, x, s, t
  return addB(rol(addB(addB(a, q), addB(x, t)), s), b)  
end

on ff a, b, c, d, x, s, t
  --return cmn(bitor((b & c), ((~b) & d)), a, b, x, s, t)
  --return cmn((b & c) | ((~b) & d), a, b, x, s, t);
  
  return cmn(bitor(bitAnd(b, c), bitAnd(bitnot(b),  d)), a, b, x, s, t)
end

on gg a, b, c, d, x, s, t
  -- return cmn((b & d) | (c & (~d)), a, b, x, s, t)
  return cmn(bitOr(bitand(b, d), bitand(c, bitnot(d))), a, b, x, s, t)
end

on hh a, b, c, d, x, s, t
  return cmn(bitxor(bitxor(b, c), d), a, b, x, s, t)
end

on ii a, b, c, d, x, s, t  
  return cmn(bitxor(c, bitor(b, (bitnot(d)))), a, b, x, s, t)
end

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail