I started work on an MD5 hashing function for AppGameKit a few months ago, and although I managed to get it to almost work, I never finished it. (It outputs a hash which doesn't match what it's supposed to)

I went to have a look at it again today (I now have 1.08b4 installed), and it just came up with a strange error.

Has any one else ecountered this? It definitely used to compile and run under the stable version of AGK.

//********************************************************************************
//
// MD5 Hashing function
//
//********************************************************************************
dim debug$[20]
message$="The quick brown fox jumps over the lazy dog"
shouldbe$="9e107d9d372bb6826bd81d3542a419d6"
//message$=""
//shouldbe$="d41d8cd98f00b204e9800998ecf8427e"
start=timer()
value$=md5Hash(message$)
finish=timer()-start
do
print("Hashing "+message$)
print("Finished in "+str(finish)+" ms")
print("")
print("My Hash: "+value$)
print("Proper : "+shouldbe$)
//print("Bitshift test (32)"+bin(32))
//print("Bitshift test (32)"+bin(leftRotate(32,-1)))
print("")
for a = 0 to 20
print(debug$[a])
next a
sync()
loop
//print value
//********************************************************************************
// The end
//********************************************************************************
end
//********************************************************************************
//
// Avast, Ere be functions!
//
//********************************************************************************
//********************************************************************************
// md5Hash(message$)
//********************************************************************************
//
//********************************************************************************
function md5Hash(message$)
//********************************************************************************
// Sets up variables
//********************************************************************************
// Note: All variables are unsigned 32 bit and wrap modulo 2^32 when calculating (What does this mean?)
// r specifies the per-round shift amounts
dim r[64] = [7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]
// Use binary integer part of the sines of integers (Radians) as constants:
dim k[64] = [0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391]
// To store 32 bit words
dim w[15]
// Initialize variables:
h0 = 0x67452301 ://A
h1 = 0xefcdab89 ://B
h2 = 0x98badcfe ://C
h3 = 0x10325476 ://D
// Variables for the message as bits array
dim bits[4096] : // Limits message$ length to 256ish chars?
charCount=len(message$)
MessageLength=charCount*8
debug$[0]="charcount:"+str(charcount)
// Split message up into bits in the array
for c=1 to charCount
for z=1 to 8
if 2^(z-1) && asc(mid(message$,c,1))
//bits[((c-1)*8)+(8-z)]=1
bits[((c-1)*8)+z]=1
else
//bits[((c-1)*8)+(8-z)]=0
bits[((c-1)*8)+z]=0
endif
next z
next c
// Testing the bits[] array contents are correct
for debuga=0 to 7
debug$[1]=debug$[1]+str(bits[debuga])
next debuga
arrayLength=messageLength
// Add the 1
bits[arrayLength+1]=1
arrayLength=arrayLength+2
// Then the zero's
repeat
bits[arrayLength]=0
inc arrayLength
until arrayLength mod 512 = 448
for c=0 to 63
if (2^(63-c)) && messageLength
bits[arrayLength+1+c]=1
else
bits[arrayLength+1+c]=0
endif
next c
arrayLength=arrayLength+64
// Chunks loop
chunks=messageLength/512
debug$[2]="Chunks= "+str(chunks)
for s=0 to chunks
// Break chunk into sixteen 32-bit words w[j], 0 = j = 15
for z = 0 to 15
n = 0
for v = 0 to 7
//n = n + bits[(512*(chunks))+(8*z)+(8-v)]*2^v
n=add(n,bits[(512*(chunks))+(8*z)+(8-v)]*2^v)
next v
w[z]=n
next z
for a=0 to 15
debug$[4+a]="Debug$["+str(4+a)+"]="+bin(w[a])
next a
// Initialize hash value for this chunk:
a=h0
b=h1
c=h2
d=h3
//Main loop:
for i = 0 to 63
if 0=<i and i=<15
f = d xor (b and (c xor d))
g = i
endif
if 16<=i and i<=31
f = c xor (d and (b xor c))
//g = (5*i + 1) mod 16
g=add(5*i,1) mod 16
endif
if 32<=i and i<=47
f = b xor c xor d
//g = (3*i + 5) mod 16
g=add(3*i,5) mod 16
endif
if 48<=i and i<=63
f = c xor (b or (not d))
g = (7*i) mod 16
endif
temp = d
d = c
c = b
tempB=add(a,f)
tempB=add(temp,k[i])
tempB=add(temp,w[g])
b=add(b,leftRotate(tempB,r[i]))
//b = b + leftrotate((a + f + k[i] + w[g]) , r[i])
a = temp
next i
h0=add(h0,a)
h1=add(h1,b)
h2=add(h2,c)
h3=add(h3,d)
next s
digest$=hex(h0)+hex(h1)+hex(h2)+hex(h3)
endfunction digest$
//********************************************************************************
//
//********************************************************************************
//
//********************************************************************************
function leftrotate(x as integer,c as integer)
c=c mod 32
x=(x<<c)||(x>>(32-c))
endfunction x
//********************************************************************************
// Add integers, wrapping at 2^32. This uses 16-bit operations internally
// to work around bugs in some JS interpreters
//********************************************************************************
//********************************************************************************
function add(x, y)
lsw = (x and 0xFFFF) + (y and 0xFFFF)
msw = (x >> 16) + (y >> 16) + (lsw >> 16)
value=(msw << 16) or (lsw and 0xFFFF)
endfunction value

P.S.

Cake is on offer to anyone who can tell me why the MD5 function doesn't return a correct hash?