Latest OxygenBasic.zip at GitHub (Click on the Wizard)
0 Members and 1 Guest are viewing this topic.
'Use binary integer part of the sines of integers (Radians) as constants:' for i from 0 to 63' K[i] := floor(abs(sin(i + 1)) × (2 pow 32))'end for'(Or just use the following table):'indexbase 0long k[64]k[00]=0xd76aa478k[01]=0xe8c7b756k[02]=0x242070dbk[03]=0xc1bdceeek[04]=0xf57c0fafk[05]=0x4787c62ak[06]=0xa8304613k[07]=0xfd469501k[08]=0x698098d8k[09]=0x8b44f7afk[10]=0xffff5bb1k[11]=0x895cd7bek[12]=0x6b901122k[13]=0xfd987193k[14]=0xa679438ek[15]=0x49b40821k[16]=0xf61e2562k[17]=0xc040b340k[18]=0x265e5a51k[19]=0xe9b6c7aak[20]=0xd62f105dk[21]=0x02441453k[22]=0xd8a1e681k[23]=0xe7d3fbc8k[24]=0x21e1cde6k[25]=0xc33707d6k[26]=0xf4d50d87k[27]=0x455a14edk[28]=0xa9e3e905k[29]=0xfcefa3f8k[30]=0x676f02d9k[31]=0x8d2a4c8ak[32]=0xfffa3942k[33]=0x8771f681k[34]=0x6d9d6122k[35]=0xfde5380ck[36]=0xa4beea44k[37]=0x4bdecfa9k[38]=0xf6bb4b60k[39]=0xbebfbc70k[40]=0x289b7ec6k[41]=0xeaa127fak[42]=0xd4ef3085k[43]=0x04881d05k[44]=0xd9d4d039k[45]=0xe6db99e5k[46]=0x1fa27cf8k[47]=0xc4ac5665k[48]=0xf4292244k[49]=0x432aff97k[50]=0xab9423a7k[51]=0xfc93a039k[52]=0x655b59c3k[53]=0x8f0ccc92k[54]=0xffeff47dk[55]=0x85845dd1k[56]=0x6fa87e4fk[57]=0xfe2ce6e0k[58]=0xa3014314k[59]=0x4e0811a1k[60]=0xf7537e82k[61]=0xbd3af235k[62]=0x2ad7d2bbk[63]=0xeb86d391'r specifies the per-round shift amounts'long r[64]r[00]=7r[01]=12r[02]=17r[03]=22r[04]=7r[05]=12r[06]=17r[07]=22r[08]=7r[09]=12r[10]=17r[11]=22r[12]=7r[13]=12r[14]=17r[15]=22r[16]=5r[17]= 9r[18]=14r[19]=20r[20]=5r[21]= 9r[22]=14r[23]=20r[24]=5r[25]= 9r[26]=14r[27]=20r[28]=5r[29]= 9r[30]=14r[31]=20r[32]=4r[33]=11r[34]=16r[35]=23r[36]=4r[37]=11r[38]=16r[39]=23r[40]=4r[41]=11r[42]=16r[43]=23r[44]=4r[45]=11r[46]=16r[47]=23r[48]=6r[49]=10r[50]=15r[51]=21r[52]=6r[53]=10r[54]=15r[55]=21r[56]=6r[57]=10r[58]=15r[59]=21r[60]=6r[61]=10r[62]=15r[63]=21function md5(string message) as string'long hash[4]byte initial_msg at (strptr(message))byte *msglong initial_len=len(message)long new_len, offsetlong a, b, c, d, i, f, g, temp,x,ystring new_message'Initialize variables - simple count in nibbles:hash[0] = 0x67452301hash[1] = 0xefcdab89hash[2] = 0x98badcfehash[3] = 0x10325476'Pre-processing:'append "1" bit to message 'append "0" bits until message length in bits = 448 (mod 512) (56 bytes)'append length mod (2^64) to messagenew_len=initial_len 'allowing for appended bitdo new_len++ if (new_len and 63)=56 then exit doend do'print new_lennew_message=message+nuls(new_len - initial_len + 8)@msg=strptr new_messagemsg[initial_len] = 0x80 'append the "1" bit; most significant bit is "first"''append the len in bits at the end of the buffer.'to_bytes(initial_len*8, msg[new_len])'initial_len>>29 == initial_len*8>>32, but avoids overflow.'to_bytes(initial_len>>29, msg[new_len + 4])'int *l@l=@msg+new_lenl[0]=initial_len*8l[1]=initial_len>>29'initial_len>>29 == initial_len*8>>32, but avoids overflow.''Process the message in successive 512-bit chunks:'for each 512-bit chunk of message:long*wfor offset=0 to new_len-1 step 64 'break chunk into sixteen 32-bit words w[j], 0 = j = 15 'for i = 0 to 15 ' w[i] = to_int32(msg[offset + i*4]) 'next @w=@msg+offset 'Initialize hash value for this chunk: a = hash[0] b = hash[1] c = hash[2] d = hash[3] ' 'Main loop: i=0 do if i>63 exit do endif select i case 0 to 15 f = (b and c) or (not(b) and d) g = i case 16 to 31 f = (d and b) or (not(d) and c) g = ((5*i) + 1) and 15 case 32 to 47 f = b xor c xor d g = ((3*i) + 5) and 15 case else f = c xor (not(d) or b) g = (7*i) and 15 end select ' temp = d d = c c = b x=a+f+k[i]+w[g] b=b + (x rol r[i]) a = temp 'original d ' i++ loop 'Add this chunk's hash to result so far: hash[0] += a hash[1] += b hash[2] += c hash[3] += dnext''Output is in little-endianstring r=nuls 16copy strptr(r),@hash,16return rend function'TEST CASES:'==========='MD5("")'= d41d8cd98f00b204e9800998ecf8427e'MD5("The quick brown fox jumps over the lazy dog")'= 9e107d9d372bb6826bd81d3542a419d6'MD5("The quick brown fox jumps over the lazy dog.")'= e4d909c290d0fb1ca068ffaddf22cbd0'FROM RFC1321'============'MD5 test suite:'MD5 ("") = d41d8cd98f00b204e9800998ecf8427e'MD5 ("a") = 0cc175b9c0f1b6a831c399e269772661'MD5 ("abc") = 900150983cd24fb0d6963f7d28e17f72'MD5 ("message digest") = f96b697d7cb7938d525a2f31aaf161d0'MD5 ("abcdefghijklmnopqrstuvwxyz") = c3fcd3d76192e4007dfb496cca67e13b'MD5 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") ='d174ab98d277d9f5a5611c2c9f419d9f'MD5 ("123456789012345678901234567890123456789012345678901234567890123456'78901234567890") = 57edf4a22be3c955ac49da2e2107b67a'TEST DATA'=========string pr,tab,crstring msgstring fistring resultint i' msg=""' msg="abc"' msg="The quick brown fox jumps over the lazy dog" msg="The quick brown fox jumps over the lazy dog."' msg="ABC123"' msg=string(4000000,"A")' 80 char' msg="12345678901234567890123456789012345678901234567890123456789012345678901234567890"extern lib "kernel32.dll"! QueryPerformanceCounter(quad*c)! QueryPerformanceFrequency(quad*f)end externquad ts,te,frQueryPerformanceCounter ts'fi="Bible.txt"'msg=getfile firesult=md5(msg)QueryPerformanceCounter teQueryPerformanceFrequency fr'DISPLAY'=======tab=chr 9pr="MD5 Hash Generation:File: " tab fi "Data: " tab left(msg,80) "length:" tab len(msg) "Time: " tab str((te-ts)/fr,6) " SecsHash: " tabfor i=1 to 16 pr+=hex(asc(result,i),2)nextputfile "result.txt", prprint pr
for i=1 to 16 pr+=hex(asc(result,i),2)next