Announcement

Collapse
No announcement yet.

How to fast calculate MD5 hash of a file?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to fast calculate MD5 hash of a file?

    I will calculate the MD5 hash of a given file.
    I have looked here examples of calculation MD5, but have not found suitable.
    Help please.
    Many thanks.

    P.S. It was possible this functions of calculation MD5 Hash (and CRC) include in standard functions of PBwin.
    Alexander Holzer
    Member
    Last edited by Alexander Holzer; 23 Sep 2008, 03:14 AM.
    Yours sincerely

  • #2
    Hi Alexander,

    How about Florents code here ?

    [shameless plug]
    Or you can always try HIME ...
    It has MD5,SHA-512, SHA-256, SHA-1 and CRC32 hash functions.
    It also has private and public key encryption functions, compression functions, functions to securely delete files and free disk space and lots more.
    [/shameless plug]

    Kind regards
    Eddy Van Esch
    Member
    Last edited by Eddy Van Esch; 23 Sep 2008, 03:31 AM.
    Eddy

    Comment


    • #3
      Thanks, I has found.
      I have changed David Roberts's code and use this function:
      Code:
      ...
      #INCLUDE "HMAC.INC"
      ...
      
      FUNCTION CalcFileToHashMD5(BYVAL sFileForCalc AS STRING, _
                                 sChkSumMD5 AS STRING) AS STRING
      
      LOCAL sFile      AS STRING
      LOCAL hs         AS LONG
      LOCAL zInput     AS ASCIIZ * 32768  'Chunk value
      LOCAL hHash      AS LONG
      LOCAL hProv      AS LONG
      LOCAL hProv2     AS LONG
      LOCAL Chunk      AS LONG
      LOCAL Balance    AS LONG
      LOCAL Completed  AS LONG
      LOCAL NextChunk  AS LONG
      LOCAL hSize      AS LONG
      LOCAL hByte()    AS BYTE
      LOCAL i          AS LONG
      
      FUNCTION = "OK"
      sChkSumMD5 = ""
      
      IF DIR$(sFileForCalc) = "" THEN
          FUNCTION = "the file <" + sFileForCalc + "> does not exist"
          EXIT FUNCTION
      END IF
      
      IF CryptAcquireContext( hProv2, BYVAL %Null, BYVAL %Null, %PROV_RSA_FULL, 0 ) = %FALSE THEN
        IF CryptAcquireContext( hProv2, BYVAL %Null, $MS_DEF_PROV, %PROV_RSA_FULL, _
        %CRYPT_NEWKEYSET ) = %FALSE THEN
          hProv = %FALSE
        ELSE
          hProv = hProv2
        END IF
      ELSE
        hProv = hProv2
      END IF
      
      IF hProv = 0 THEN
          FUNCTION = "crypting acquire context error"
          EXIT FUNCTION
      ELSE
      
          '- Create a hash object
          IF CryptCreateHash(hProv, %CALG_MD5, 0, 0, hHash) = %False THEN
            IF hProv THEN CryptReleaseContext hProv, 0
            FUNCTION = "creating a hash object failed"
            EXIT FUNCTION
          ELSE
            ON ERROR RESUME NEXT
            hs = FREEFILE
            OPEN sFileForCalc FOR BINARY SHARED AS hs
              IF ERR THEN
                FUNCTION = "opening file <" + sFileForCalc + "> failed"
                EXIT FUNCTION
              END IF
              Chunk = 32768 ' Best for my system; is slower if either < or >
              Balance = LOF(hs) MOD Chunk
              Completed = %False
              NextChunk = 1
              DO
                GET hs, NextChunk, zInput
                IF EOF(hs) THEN
                  IF Balance = 0 THEN EXIT LOOP
                  Chunk = Balance
                  Completed = %True
                END IF
                '- Hash in each Chunk
                IF CryptHashData(hHash, zInput, Chunk, 0) = %False THEN
                  IF hProv THEN CryptReleaseContext hProv, 0
                  IF hHash THEN CryptDestroyHash hHash
                  FUNCTION = "calculate MD5 hash failed"
                  CLOSE hs
                  EXIT FUNCTION
                END IF  '- CryptHashData
                NextChunk = NextChunk + Chunk
              LOOP UNTIL Completed '- all Chunks
            CLOSE hs
          END IF '- CryptCreateHash
        END IF  '- hProv = 0
      
        IF CryptGetHashParam(hHash, %HP_HASHSIZE, hSize, 4, 0) = %False THEN
          IF hProv THEN CryptReleaseContext hProv, 0
          IF hHash THEN CryptDestroyHash hHash
          FUNCTION = "calculate MD5 hash failed"
          EXIT FUNCTION
        ELSE
          REDIM hByte(hSize - 1) AS BYTE
          IF CryptGetHashParam(hHash, %HP_HASHVAL, hByte(0), hSize, 0) = %False THEN
            IF hProv THEN CryptReleaseContext hProv, 0
            IF hHash THEN CryptDestroyHash hHash
            FUNCTION = "calculate MD5 hash failed"
            EXIT FUNCTION
          ELSE
            FOR i = 0 TO hSize - 1
              sChkSumMD5 = sChkSumMD5 + HEX$(hByte(i), 2)
            NEXT
          END IF      '- Get hByte array
        END IF        '- Get hSize
      
        '- Destroy and release the hash
        CryptDestroyHash hHash
        CryptReleaseContext hProv, 0
      
      END FUNCTION
      Yours sincerely

      Comment

      Working...
      X