QB / QB64 Discussion Forum     RULES     Other Subforums, Links and Downloads    Index of Threads

 


  << Previous Topic | Next Topic >>Return to Index  

ProgramList Artelius

January 9 2009 at 12:27 PM
Artelius  (Login Mikrondel)
R

 

 
 Respond to this message   
AuthorReply
Artelius
(Login Mikrondel)
R

MD5 function

January 9 2009, 2:03 PM 

DECLARE FUNCTION hexp$ (A AS LONG)
DECLARE FUNCTION MD5$ (in AS STRING)
DECLARE FUNCTION add& (A AS LONG, B AS LONG)
DECLARE FUNCTION leftrotate& (A AS LONG, B AS INTEGER)

'Demo
PRINT MD5("")

FUNCTION add& (A AS LONG, B AS LONG)
DIM C AS LONG
C = (A AND &HFFFFFF) + (B AND &HFFFFFF)
POKE VARPTR(C) + 3, PEEK(VARPTR(A) + 3) + PEEK(VARPTR(B) + 3) - (C > &HFFFFFF)
add = C
END FUNCTION

'Remove LCASE$ if you want uppercase hex
FUNCTION hexp$ (A AS LONG)
DIM I AS INTEGER, T AS STRING
FOR I = 0 TO 3
T = T + RIGHT$(LCASE$(HEX$(256 + PEEK(VARPTR(A) + I))), 2)
NEXT
hexp = T
END FUNCTION

FUNCTION leftrotate& (A AS LONG, B AS INTEGER)
DIM C AS LONG, I AS INTEGER
C = A
FOR I = 1 TO B
C = add(C, C)
NEXT
leftrotate = C OR (((A AND &H7FFFFFFF) \ 2 OR (A < 0) AND &H40000000) \ (2 ^ (31 - B)))
END FUNCTION

FUNCTION MD5$ (in AS STRING)
STATIC init AS INTEGER, r() AS INTEGER, Garbage() AS LONG

DIM I AS INTEGER, J AS INTEGER, dtemp AS DOUBLE
DIM h0 AS LONG, h1 AS LONG, h2 AS LONG, h3 AS LONG
DIM A AS LONG, B AS LONG, C AS LONG, D AS LONG
DIM F AS LONG, G AS LONG, temp AS LONG
DIM mess(0 TO 15) AS LONG

IF init = 0 THEN
REDIM r(0 TO 63) AS INTEGER, Garbage(0 TO 63) AS LONG
r(0) = 7: r(1) = 12: r(2) = 17: r(3) = 22
r(4) = 5: r(5) = 9: r(6) = 14: r(7) = 20
r(8) = 4: r(9) = 11: r(10) = 16: r(11) = 23
r(12) = 6: r(13) = 10: r(14) = 15: r(15) = 21

FOR I = 63 TO 4 STEP -1
r(I) = r((I AND 3) OR ((I AND 48) \ 4))
NEXT

FOR I = 0 TO 63
dtemp = ABS(SIN(CDBL(I + 1))) * 4294967296#
IF dtemp >= 2147483648# THEN dtemp = dtemp - 4294967296#
Garbage(I) = INT(dtemp)
NEXT
init = 1
END IF


h0 = &H67452301
h1 = &HEFCDAB89
h2 = &H98BADCFE
h3 = &H10325476

in = in + CHR$(128) + STRING$((55 - LEN(in) AND 63), 0) + CHR$((LEN(in) * 8) AND 255) + CHR$(LEN(in) \ 32) + STRING$(6, 0)

FOR I = 0 TO LEN(in) - 1 STEP 64
FOR J = 0 TO 15
mess(J) = CVL(MID$(in, J * 4 + I + 1, 4))
NEXT
A = h0
B = h1
C = h2
D = h3
FOR J = 0 TO 15
F = ((C XOR D) AND B) XOR D
G = J
GOSUB shuffle
NEXT
FOR J = 16 TO 31
F = ((B XOR C) AND D) XOR C
G = (5 * J + 1) AND 15
GOSUB shuffle
NEXT
FOR J = 32 TO 47
F = B XOR C XOR D
G = (3 * J + 5) AND 15
GOSUB shuffle
NEXT
FOR J = 48 TO 63
F = (D IMP B) XOR C
G = (7 * J) AND 15
GOSUB shuffle
NEXT

h0 = add(h0, A)
h1 = add(h1, B)
h2 = add(h2, C)
h3 = add(h3, D)

NEXT

MD5 = hexp(h0) + hexp(h1) + hexp(h2) + hexp(h3)

EXIT FUNCTION
shuffle:
temp = A
A = D
D = C
C = B
B = add(B, leftrotate(add(add(add(temp, F), Garbage(J)), mess(G)), r(J)))
RETURN

END FUNCTION

 
 Respond to this message   
 
  << Previous Topic | Next Topic >>Return to Index  
 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement