Author Topic: MD5, HASH, RC4, BASE64....  (Read 1534 times)

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

  • Guest
MD5, HASH, RC4, BASE64....
« on: August 06, 2019, 08:53:47 PM »

 Has anybody done something regarding this?

 I have tackled RC4 but i dont have anything for the others...

Brian Alvarez

  • Guest
Re: MD5, HASH, RC4, BASE64....
« Reply #1 on: August 06, 2019, 11:29:54 PM »
Done with Base64.

Charles Pegge

  • Guest
Re: MD5, HASH, RC4, BASE64....
« Reply #2 on: August 07, 2019, 04:20:08 AM »
Hi Brian,

MD5:

MD5 is deprecated though widely used.
https://en.wikipedia.org/wiki/MD5

Code: [Select]
'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 0

long k[64]
k[00]=0xd76aa478
k[01]=0xe8c7b756
k[02]=0x242070db
k[03]=0xc1bdceee
k[04]=0xf57c0faf
k[05]=0x4787c62a
k[06]=0xa8304613
k[07]=0xfd469501
k[08]=0x698098d8
k[09]=0x8b44f7af
k[10]=0xffff5bb1
k[11]=0x895cd7be
k[12]=0x6b901122
k[13]=0xfd987193
k[14]=0xa679438e
k[15]=0x49b40821
k[16]=0xf61e2562
k[17]=0xc040b340
k[18]=0x265e5a51
k[19]=0xe9b6c7aa
k[20]=0xd62f105d
k[21]=0x02441453
k[22]=0xd8a1e681
k[23]=0xe7d3fbc8
k[24]=0x21e1cde6
k[25]=0xc33707d6
k[26]=0xf4d50d87
k[27]=0x455a14ed
k[28]=0xa9e3e905
k[29]=0xfcefa3f8
k[30]=0x676f02d9
k[31]=0x8d2a4c8a
k[32]=0xfffa3942
k[33]=0x8771f681
k[34]=0x6d9d6122
k[35]=0xfde5380c
k[36]=0xa4beea44
k[37]=0x4bdecfa9
k[38]=0xf6bb4b60
k[39]=0xbebfbc70
k[40]=0x289b7ec6
k[41]=0xeaa127fa
k[42]=0xd4ef3085
k[43]=0x04881d05
k[44]=0xd9d4d039
k[45]=0xe6db99e5
k[46]=0x1fa27cf8
k[47]=0xc4ac5665
k[48]=0xf4292244
k[49]=0x432aff97
k[50]=0xab9423a7
k[51]=0xfc93a039
k[52]=0x655b59c3
k[53]=0x8f0ccc92
k[54]=0xffeff47d
k[55]=0x85845dd1
k[56]=0x6fa87e4f
k[57]=0xfe2ce6e0
k[58]=0xa3014314
k[59]=0x4e0811a1
k[60]=0xf7537e82
k[61]=0xbd3af235
k[62]=0x2ad7d2bb
k[63]=0xeb86d391

'r specifies the per-round shift amounts
'
long r[64]
r[00]=7
r[01]=12
r[02]=17
r[03]=22
r[04]=7
r[05]=12
r[06]=17
r[07]=22
r[08]=7
r[09]=12
r[10]=17
r[11]=22
r[12]=7
r[13]=12
r[14]=17
r[15]=22
r[16]=5
r[17]= 9
r[18]=14
r[19]=20
r[20]=5
r[21]= 9
r[22]=14
r[23]=20
r[24]=5
r[25]= 9
r[26]=14
r[27]=20
r[28]=5
r[29]= 9
r[30]=14
r[31]=20
r[32]=4
r[33]=11
r[34]=16
r[35]=23
r[36]=4
r[37]=11
r[38]=16
r[39]=23
r[40]=4
r[41]=11
r[42]=16
r[43]=23
r[44]=4
r[45]=11
r[46]=16
r[47]=23
r[48]=6
r[49]=10
r[50]=15
r[51]=21
r[52]=6
r[53]=10
r[54]=15
r[55]=21
r[56]=6
r[57]=10
r[58]=15
r[59]=21
r[60]=6
r[61]=10
r[62]=15
r[63]=21

function md5(string message) as string
'
long hash[4]
byte  initial_msg at (strptr(message))
byte  *msg
long initial_len=len(message)
long   new_len, offset
long a, b, c, d, i, f, g, temp,x,y
string new_message

'Initialize variables - simple count in nibbles:
hash[0] = 0x67452301
hash[1] = 0xefcdab89
hash[2] = 0x98badcfe
hash[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 message
new_len=initial_len 'allowing for appended bit
do
  new_len++
  if (new_len and 63)=56 then exit do
end do
'print new_len
new_message=message+nuls(new_len - initial_len + 8)
@msg=strptr new_message
msg[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_len
l[0]=initial_len*8
l[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*w
for 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] += d
next
'
'Output is in little-endian
string r=nuls 16
copy strptr(r),@hash,16
return r
end 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,cr
string msg
string fi
string result
int 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 extern
quad ts,te,fr
QueryPerformanceCounter ts
'fi="Bible.txt"
'msg=getfile fi
result=md5(msg)
QueryPerformanceCounter te
QueryPerformanceFrequency fr

'DISPLAY
'=======

tab=chr 9
pr="MD5 Hash Generation:

File:  " tab fi "
Data:  " tab left(msg,80) "
length:" tab  len(msg) "
Time:  " tab str((te-ts)/fr,6) " Secs
Hash:  " tab
for i=1 to 16
  pr+=hex(asc(result,i),2)
next
putfile "result.txt", pr
print pr

Brian Alvarez

  • Guest
Re: MD5, HASH, RC4, BASE64....
« Reply #3 on: August 07, 2019, 11:20:23 AM »
Thanks! I knew you were most likely to have something for this! :)

Unfortunately it is giving me weird results... something in the last updates must have made it behave different.

Charles Pegge

  • Guest
Re: MD5, HASH, RC4, BASE64....
« Reply #4 on: August 07, 2019, 11:58:42 AM »
MD5 returns a 16 byte binary string, so you will need to translate the bytes into hexadecimal to see its value

Code: [Select]
for i=1 to 16
  pr+=hex(asc(result,i),2)
next

Brian Alvarez

  • Guest
Re: MD5, HASH, RC4, BASE64....
« Reply #5 on: August 07, 2019, 12:07:35 PM »
I was about to post that... silly me! thanks Charles. :)

 It works very nice.