MD5FingerprintBin()

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

MD5FingerprintBin()

Post by Rescator »

Since PureBasic currently do not have a MD5FingerprintBin() this is a usable solution.
I tried to make this as fast as possible, if anyone got any speed improvements please post.
It is roughly 176% slower than MD5Fingerprint()

(c) Public Domain

Code: Select all

EnableExplicit

Procedure.l MD5FingerprintBin(*buf,len.i,*md5bin)
 Protected hex$,*hex.Character, a.i, b.i, c.i, i.i, *md5b.Byte
 hex$=MD5Fingerprint(*buf,len)
 *hex=@hex$
 *md5b=*md5bin
 For i=0 To 31 Step 2
  *hex+(2*SizeOf(Character))
  a=*hex\c
  If a<58
   a=a-48
;  ElseIf a<97 ;since MD5Fingerprint() returns lowercase, this is not needed.
;   a=a-55
  Else
   a=a-87
  EndIf
  *hex+(1*SizeOf(Character))
  b=*hex\c
  If b<58
   b=b-48
;  ElseIf b<97
;   b=b-55
  Else
   b=b-87
  EndIf
  *md5b\b=(a<<4)+b
  *md5b+1
 Next
 ProcedureReturn *md5bin
EndProcedure 

Define *md5bin,text$
*md5bin=AllocateMemory(256)

text$="This is a test, a long line of text! Like a passphrase!"

MD5FingerprintBin(@text$,StringByteLength(text$),*md5bin)