MD5 ..... ALGO (Source)

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
andi256
Beiträge: 100
Registriert: 06.11.2004 11:23
Computerausstattung: PB 5.30 (x64) Win7
Wohnort: Österreich

MD5 ..... ALGO (Source)

Beitrag von andi256 »

hi ....

hab mal den MD5 Algo nachgebaut ... (brauchte den modifiziert)

Wenn sich einer fragt wozu man das so jetzt braucht .... naja garnicht "! :-)
da PB ja den Befehl "MD5Fingerprint" schon includiert hat .... und ausser das mein Algo langsamer ist bringt der nichts mehr

aber falls trotzdem mal wer den Algo einwenig angucken will ...

Code: Alles auswählen

;MD5 - RSA Data Security, Inc., MD5 message-digest algorithm
;Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All rights reserved.

;License To copy And use this software is granted provided that it
;is identified as the "RSA Data Security, Inc. MD5 Message-Digest
;Algorithm" in all material mentioning or referencing this software
;Or this function.

;License is also granted To make And use derivative works provided
;that such works are identified as "derived from the RSA Data
;Security, Inc. MD5 Message-Digest Algorithm" in all material
;mentioning Or referencing the derived work.  
                                                                    
;RSA Data Security, Inc. makes no representations concerning either
;the merchantability of this software Or the suitability of this
;software For any particular purpose. It is provided "as is"
;without express Or implied warranty of any kind.  
                                                                    
 ;These notices must be retained in any copies of any part of this
 ;documentation and/Or software.  

;Constants For MD5Transform routine.
#S11= 7
#S12=12
#S13=17
#S14=22
#S21= 5
#S22= 9
#S23=14
#S24=20
#S31= 4
#S32=11
#S33=16
#S34=23
#S41= 6
#S42=10
#S43=15
#S44=21

;MD5 context.
Structure MD5_CTX
 state.l[4]               ;state (ABCD) 
 count.l[2]               ;number of bits, modulo 2^64 (lsb first)
 buffer.b[64]             ;input buffer
EndStructure

DataSection
PADDING:
Data.b $80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data.b   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data.b   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data.b   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
EndDataSection

;F, G, H And I are basic MD5 functions.
Procedure F(x,y,z) 
 ProcedureReturn x&y|(~x&z)
EndProcedure 
Procedure G(x,y,z) 
 ProcedureReturn x&z|(y&~z)
EndProcedure 
Procedure H(x,y,z) 
 ProcedureReturn x!y!z
EndProcedure 
Procedure I(x,y,z) 
 ProcedureReturn y!(x|~z)
EndProcedure 

;ROTATE_LEFT rotates x left n bits.
Procedure ROTATE_LEFT(x,n) 
 ProcedureReturn x<<n|((x>>(32-n))&((1<<n)-1))
EndProcedure

;FF, GG, HH, And II transformations For rounds 1, 2, 3, And 4.
;Rotation is separate from addition To prevent recomputation.
Procedure FF(a,b,c,d,x,s,ac) 
 a+F(b,c,d)+x+ac
 a=ROTATE_LEFT(a,s)
 a+b
 ProcedureReturn a
EndProcedure
Procedure GG(a,b,c,d,x,s,ac) 
 a+G(b,c,d)+x+ac
 a=ROTATE_LEFT(a,s)
 a+b
 ProcedureReturn a
EndProcedure
Procedure HH(a,b,c,d,x,s,ac) 
 a+ H(b,c,d)+x+ac
 a=ROTATE_LEFT(a,s)
 a+b
 ProcedureReturn a
EndProcedure
Procedure II(a,b,c,d,x,s,ac) 
 a+I(b,c,d)+x+ac
 a=ROTATE_LEFT(a,s)
 a+b
 ProcedureReturn a
EndProcedure

;MD5 initialization. Begins an MD5 operation, writing a new context.
Procedure MD5Init(*context.MD5_CTX)
 *context\count[0]=0
 *context\count[1]=0
 ;Load magic initialization constants.
 *context\state[0]=$67452301
 *context\state[1]=$efcdab89
 *context\state[2]=$98badcfe
 *context\state[3]=$10325476 
EndProcedure

;MD5 basic transformation. Transforms state based on block.
Procedure MD5Transform(*state.MD5_CTX,*x)
 a=*state\state[0]
 b=*state\state[1]
 c=*state\state[2]
 d=*state\state[3]
 ;Round 1 
 a=FF(a,b,c,d,PeekL(*x   ),#S11,$d76aa478); *  1 *
 d=FF(d,a,b,c,PeekL(*x+ 4),#S12,$e8c7b756); *  2 *
 c=FF(c,d,a,b,PeekL(*x+ 8),#S13,$242070db); *  3 *
 b=FF(b,c,d,a,PeekL(*x+12),#S14,$c1bdceee); *  4 *
 a=FF(a,b,c,d,PeekL(*x+16),#S11,$f57c0faf); *  5 *
 d=FF(d,a,b,c,PeekL(*x+20),#S12,$4787c62a); *  6 *
 c=FF(c,d,a,b,PeekL(*x+24),#S13,$a8304613); *  7 *
 b=FF(b,c,d,a,PeekL(*x+28),#S14,$fd469501); *  8 *
 a=FF(a,b,c,d,PeekL(*x+32),#S11,$698098d8); *  9 *
 d=FF(d,a,b,c,PeekL(*x+36),#S12,$8b44f7af); * 10 *
 c=FF(c,d,a,b,PeekL(*x+40),#S13,$ffff5bb1); * 11 *
 b=FF(b,c,d,a,PeekL(*x+44),#S14,$895cd7be); * 12 *
 a=FF(a,b,c,d,PeekL(*x+48),#S11,$6b901122); * 13 *
 d=FF(d,a,b,c,PeekL(*x+52),#S12,$fd987193); * 14 *
 c=FF(c,d,a,b,PeekL(*x+56),#S13,$a679438e); * 15 *
 b=FF(b,c,d,a,PeekL(*x+60),#S14,$49b40821); * 16 *
 ;Round 2 
 a=GG(a,b,c,d,PeekL(*x+ 4),#S21,$f61e2562); * 17 *
 d=GG(d,a,b,c,PeekL(*x+24),#S22,$c040b340); * 18 *
 c=GG(c,d,a,b,PeekL(*x+44),#S23,$265e5a51); * 19 *
 b=GG(b,c,d,a,PeekL(*x   ),#S24,$e9b6c7aa); * 20 *
 a=GG(a,b,c,d,PeekL(*x+20),#S21,$d62f105d); * 21 *
 d=GG(d,a,b,c,PeekL(*x+40),#S22,$2441453) ; * 22 *
 c=GG(c,d,a,b,PeekL(*x+60),#S23,$d8a1e681); * 23 *
 b=GG(b,c,d,a,PeekL(*x+16),#S24,$e7d3fbc8); * 24 *
 a=GG(a,b,c,d,PeekL(*x+36),#S21,$21e1cde6); * 25 *
 d=GG(d,a,b,c,PeekL(*x+56),#S22,$c33707d6); * 26 *
 c=GG(c,d,a,b,PeekL(*x+12),#S23,$f4d50d87); * 27 *
 b=GG(b,c,d,a,PeekL(*x+32),#S24,$455a14ed); * 28 *
 a=GG(a,b,c,d,PeekL(*x+52),#S21,$a9e3e905); * 29 *
 d=GG(d,a,b,c,PeekL(*x+ 8),#S22,$fcefa3f8); * 30 *
 c=GG(c,d,a,b,PeekL(*x+28),#S23,$676f02d9); * 31 *
 b=GG(b,c,d,a,PeekL(*x+48),#S24,$8d2a4c8a); * 32 *
 ;Round 3 
 a=HH(a,b,c,d,PeekL(*x+20),#S31,$fffa3942); * 33 *
 d=HH(d,a,b,c,PeekL(*x+32),#S32,$8771f681); * 34 *
 c=HH(c,d,a,b,PeekL(*x+44),#S33,$6d9d6122); * 35 *
 b=HH(b,c,d,a,PeekL(*x+56),#S34,$fde5380c); * 36 *
 a=HH(a,b,c,d,PeekL(*x+ 4),#S31,$a4beea44); * 37 *
 d=HH(d,a,b,c,PeekL(*x+16),#S32,$4bdecfa9); * 38 *
 c=HH(c,d,a,b,PeekL(*x+28),#S33,$f6bb4b60); * 39 *
 b=HH(b,c,d,a,PeekL(*x+40),#S34,$bebfbc70); * 40 *
 a=HH(a,b,c,d,PeekL(*x+42),#S31,$289b7ec6); * 41 * 
 d=HH(d,a,b,c,PeekL(*x   ),#S32,$eaa127fa); * 42 *
 c=HH(c,d,a,b,PeekL(*x+12),#S33,$d4ef3085); * 43 *
 b=HH(b,c,d,a,PeekL(*x+24),#S34,$4881d05) ; * 44 *
 a=HH(a,b,c,d,PeekL(*x+36),#S31,$d9d4d039); * 45 *
 d=HH(d,a,b,c,PeekL(*x+48),#S32,$e6db99e5); * 46 *
 c=HH(c,d,a,b,PeekL(*x+60),#S33,$1fa27cf8); * 47 *
 b=HH(b,c,d,a,PeekL(*x+ 8),#S34,$c4ac5665); * 48 *
 ;Round 4 
 a=II(a,b,c,d,PeekL(*x   ),#S41,$f4292244); * 49 *
 d=II(d,a,b,c,PeekL(*x+28),#S42,$432aff97); * 50 * 
 c=II(c,d,a,b,PeekL(*x+56),#S43,$ab9423a7); * 51 *
 b=II(b,c,d,a,PeekL(*x+20),#S44,$fc93a039); * 52 *
 a=II(a,b,c,d,PeekL(*x+48),#S41,$655b59c3); * 53 *
 d=II(d,a,b,c,PeekL(*x+12),#S42,$8f0ccc92); * 54 *
 c=II(c,d,a,b,PeekL(*x+40),#S43,$ffeff47d); * 55 *
 b=II(b,c,d,a,PeekL(*x+ 4),#S44,$85845dd1); * 56 *
 a=II(a,b,c,d,PeekL(*x+32),#S41,$6fa87e4f); * 57 *
 d=II(d,a,b,c,PeekL(*x+60),#S42,$fe2ce6e0); * 58 *
 c=II(c,d,a,b,PeekL(*x+24),#S43,$a3014314); * 59 *
 b=II(b,c,d,a,PeekL(*x+52),#S44,$4e0811a1); * 60 *
 a=II(a,b,c,d,PeekL(*x+16),#S41,$f7537e82); * 61 *
 d=II(d,a,b,c,PeekL(*x+44),#S42,$bd3af235); * 62 *
 c=II(c,d,a,b,PeekL(*x+ 8),#S43,$2ad7d2bb); * 63 *
 b=II(b,c,d,a,PeekL(*x+36),#S44,$eb86d391); * 64 *
 *state\state[0]+a
 *state\state[1]+b
 *state\state[2]+c
 *state\state[3]+d
EndProcedure

;MD5 block update operation. Continues an MD5 message-digest
;operation, processing another message block, And updating the context.
Procedure MD5Update(*context.MD5_CTX,*input,inputLen)
 ;Compute number of bytes mod 64 
 index=(*context\count[0]>>3)&$3F
 ;Update number of bits 
 *context\count[0]+inputLen<<3
 If *context\count[0]<(inputLen<<3)
  *context\count[1]+1
 EndIf 
 *context\count[1]+(inputLen>>29)
 partLen=64-index
;Transform as many times as possible.
 If inputLen>=partLen
  CopyMemory(*input,@*context\buffer[index],partLen)
  MD5Transform(@*context\state,@*context\buffer)
  For i=partLen To inputlen-64 Step 64
   MD5Transform(@*context\state,*input+i)
  Next i
  index=0
 Else
  i=0
 EndIf 
 ;Buffer remaining input
 CopyMemory(*input+i,@*context\buffer[index],inputLen-i)
EndProcedure

;MD5 finalization. Ends an MD5 message-digest operation, writing the
;the message digest And zeroizing the context.
Procedure MD5Final(*digest,*context.MD5_CTX)
 *bits=AllocateMemory(8)
 ;Save number of bits 
 CopyMemory(@*context\count,*bits,8)
;Pad out To 56 mod 64.
 index=(*context\count[0]>>3)&$3f
 If index<56
  padLen=56-index
 Else
  padlen=120-index
 EndIf
 MD5Update(*context,?PADDING,padLen)
;Append length (before padding) 
 MD5Update(*context,*bits,8)
 ;Store state in digest 
 CopyMemory(@*context\state,*digest,16)
 ;Zeroize sensitive information.
 FreeMemory (*context);not save! (Daten werden nicht überschrieben!)
 FreeMemory (*bits)
EndProcedure

; --------------------------M A I N---------------------------------------

Procedure.s speicherout_hex(*mem,l)
 For i=0 To l-1
  q$=q$+RSet(Hex(Val(StrU(PeekB(*mem+i),#BYTE))),2,"0")
 Next i
ProcedureReturn q$
EndProcedure

Procedure.s MD5Fingerprint_(*a,lena)
 *context.MD5_CTX=AllocateMemory(SizeOf(MD5_CTX))
 MD5Init(*context)
 MD5Update(*context,*a,lena)
 *output=AllocateMemory(16)
 MD5Final(*output,*context)
 ProcedureReturn speicherout_hex(*output,16)
EndProcedure 
 
a$ = "12345678901234567890123456789012345678901234567890123456789012345678901234567890"

Debug "ist:  "+      MD5Fingerprint_(@a$,Len(a$))
Debug "soll: "+UCase(MD5Fingerprint(@a$,Len(a$))) 
Andi256
tranquil
Beiträge: 117
Registriert: 22.09.2004 22:07
Kontaktdaten:

Beitrag von tranquil »

Super! Danke für den Source. Wollte mich da damals auch durchwühlen war mit aber zu komplex und umständlich. Das lässt nun keine Wünsche mehr offen. :)

Danke nochmals
andi256
Beiträge: 100
Registriert: 06.11.2004 11:23
Computerausstattung: PB 5.30 (x64) Win7
Wohnort: Österreich

Beitrag von andi256 »

muss den nochmals überarbeiten ... irgendwo ist noch ein bug drinnen

bei Stringlängen ab 42 kann ein falsches Ergebniss kommen ... sitzte da schon über 8 Stunden drann ... find aber den Fehler nicht :oops:

habs momentan mal pausiert, da ich sonst meine PC was böses angetan hätte :mrgreen:

also wer den "Hund" Findet bekommt von mir ne virtuelle Kiste Bier :mrgreen:

Andi256
Benutzeravatar
Helle
Beiträge: 566
Registriert: 11.11.2004 16:13
Wohnort: Magdeburg

Re: MD5 ..... ALGO (Source)

Beitrag von Helle »

Da ich auch Alt-Bier trinke: Der Fehler ist:

Code: Alles auswählen

;Round 3 
a=HH(a,b,c,d,PeekL(*x+20),#S31,$fffa3942); * 33 *
d=HH(d,a,b,c,PeekL(*x+32),#S32,$8771f681); * 34 *
c=HH(c,d,a,b,PeekL(*x+44),#S33,$6d9d6122); * 35 *
b=HH(b,c,d,a,PeekL(*x+56),#S34,$fde5380c); * 36 *
a=HH(a,b,c,d,PeekL(*x+ 4),#S31,$a4beea44); * 37 *
d=HH(d,a,b,c,PeekL(*x+16),#S32,$4bdecfa9); * 38 *
c=HH(c,d,a,b,PeekL(*x+28),#S33,$f6bb4b60); * 39 *
b=HH(b,c,d,a,PeekL(*x+40),#S34,$bebfbc70); * 40 *
a=HH(a,b,c,d,PeekL(*x+52),#S31,$289b7ec6); * 41 *    !!!  52 nicht 42
d=HH(d,a,b,c,PeekL(*x   ),#S32,$eaa127fa); * 42 *
c=HH(c,d,a,b,PeekL(*x+12),#S33,$d4ef3085); * 43 *
b=HH(b,c,d,a,PeekL(*x+24),#S34,$4881d05) ; * 44 *
a=HH(a,b,c,d,PeekL(*x+36),#S31,$d9d4d039); * 45 *
d=HH(d,a,b,c,PeekL(*x+48),#S32,$e6db99e5); * 46 *
c=HH(c,d,a,b,PeekL(*x+60),#S33,$1fa27cf8); * 47 *
b=HH(b,c,d,a,PeekL(*x+ 8),#S34,$c4ac5665); * 48 *
Dann werde ich mal den realen Schalke-Schal umbinden und virtuelles Bier trinken (na, nicht wirklich :lol: )
Gruß
Helle
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Re: MD5 ..... ALGO (Source)

Beitrag von ts-soft »

Helle hat geschrieben:Da ich auch Alt-Bier trinke: Der Fehler ist:
5,5 Jahre für eine virtuelle Kiste Bier? Dat lohnt ja gar nicht :lol:
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Antworten