Jump to content
Sign in to follow this  
euverve

MD5 Hash Check

Recommended Posts

euverve

Good morning to all...

I just wanted to ask for help.

I wanted to perform a md5 check and delete.

For example:

$a = "2DDE0E8E105E98527E1E55AC19C9E34B"

$b = "c:\sample.exe"

if filehash($:) = $a then

filedelete($:lmao:

endif

I hope you help me guys...

Share this post


Link to post
Share on other sites
euverve

Thanks for the link, UDF credits to Ward.

I have created a file md5, sha1, crc32 checker...

Posted Image

#include <ButtonConstants.au3>

#include <EditConstants.au3>

#include <GUIConstantsEx.au3>

#include <StaticConstants.au3>

#include <WindowsConstants.au3>

; hashing files

#include "md5.au3"

#include "sha1.au3"

#include "crc32.au3"

Global $Filename

#Region ### START Koda GUI section ### Form=C:\Documents and Settings\Thatskie\Desktop\Autoit Projects\File Hash Checker\Form1.kxf

$Form1 = GUICreate("File Hash Checker 1.0 | Team Pinoy Geeks", 387, 187, -1, -1)

GUISetBkColor(0xFFFBF0)

$Group1 = GUICtrlCreateGroup("Browse File", 8, 8, 369, 57)

$Button1 = GUICtrlCreateButton("...", 344, 32, 27, 25, 0)

GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")

$Label4 = GUICtrlCreateLabel("File:", 24, 38, 28, 17)

GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

$Input1 = GUICtrlCreateInput("File Hash Checker By euverve, © 2009 - 2010", 72, 34, 265, 21, BitOR($ES_CENTER,$ES_AUTOHSCROLL))

GUICtrlCreateGroup("", -99, -99, 1, 1)

$Group2 = GUICtrlCreateGroup("Hash Results", 8, 72, 369, 105)

$Label1 = GUICtrlCreateLabel("MD5:", 24, 98, 34, 17)

GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

$Label2 = GUICtrlCreateLabel("SHA1:", 24, 126, 40, 17)

GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

$Label3 = GUICtrlCreateLabel("CRC32:", 24, 150, 47, 17)

GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")

$Input2 = GUICtrlCreateInput("", 72, 96, 297, 21)

$Input3 = GUICtrlCreateInput("", 72, 120, 297, 21)

$Input4 = GUICtrlCreateInput("", 72, 144, 297, 21)

GUICtrlCreateGroup("", -99, -99, 1, 1)

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1

$nMsg = GUIGetMsg()

Switch $nMsg

Case $GUI_EVENT_CLOSE

Exit

Case $Button1

$message = "Open file."

$Filename = FileOpenDialog($message, @ScriptDir & "\", "Any file (*.*)" , 1)

GUICtrlSetData($Input1, $Filename)

If @error Then

MsgBox(4096,"","No File(s) chosen")

ContinueLoop

Else

md5hash()

sha1()

crc32()

EndIf

EndSwitch

WEnd

func md5hash()

Dim $BufferSize = 0x20000

$FileHandle = FileOpen($Filename, 16)

$MD5CTX = _MD5Init()

For $i = 1 To Ceiling(FileGetSize($Filename) / $BufferSize)

_MD5Input($MD5CTX, FileRead($FileHandle, $BufferSize))

Next

$Hash = _MD5Result($MD5CTX)

FileClose($FileHandle)

GUICtrlSetData($Input2, $Hash)

EndFunc

func sha1()

Dim $BufferSize = 0x20000

$FileHandle = FileOpen($Filename, 16)

$SHA1CTX = _SHA1Init()

For $i = 1 To Ceiling(FileGetSize($Filename) / $BufferSize)

_SHA1Input($SHA1CTX, FileRead($FileHandle, $BufferSize))

Next

$Hash = _SHA1Result($SHA1CTX)

FileClose($FileHandle)

GUICtrlSetData($Input3, $Hash)

EndFunc

func crc32()

Dim $BufferSize = 0x20000

Dim $CRC32 = 0

$FileHandle = FileOpen($Filename, 16)

For $i = 1 To Ceiling(FileGetSize($Filename) / $BufferSize)

$CRC32 = _CRC32(FileRead($FileHandle, $BufferSize), BitNot($CRC32))

Next

GUICtrlSetData($Input4, Hex($CRC32, 8))

EndFunc

My question is how could i perform a file hash compare, like i posted in the first post.

I wanted to delete a file based on its hashes. Please help....

Thanks

Share this post


Link to post
Share on other sites
euverve

*bump*

Here's what I wanted to implement.

#include "md5.au3"

$a = "2DDE0E8E105E98527E1E55AC19C9E34B"

$b = "c:\sample.exe"

if filehash($:) = $a then

filedelete($:lmao:

endif

An input guys... Thanks

Share this post


Link to post
Share on other sites
trancexx

*bump*

Here's what I wanted to implement.

An input guys... Thanks

You are kind of confusing.

This helps?

$sFile = "c:\sample.exe"

$hFile = FileOpen($sFile, 16)
$bFile = FileRead($hFile)
FileClose($hFile)

$sHash = "2DDE0E8E105E98527E1E55AC19C9E34B"

If Not (_Crypt_HashData($bFile) = $sHash) Then
    FileDelete($sFile)
EndIf





;===============================================================================
; Function Name:    _Crypt_HashData()
; Description:      Calculate hash from data
; Syntax:
; Parameter(s):  $vData - data to hash, can be binary or a string
;               $iAlgID - hash algorithm identifier, can be one of the following:
;                  0x8001 = MD2
;                  0x8002 = MD4
;                  0x8003 = MD5 (default)
;                  0x8004 = SHA1
;                  also see http://msdn.microsoft.com/en-us/library/aa375549(VS.85).aspx
; Requirement(s):
; Return Value(s):  Success = Returns hash string
;               Failure = Returns empty string and sets error:
;                  @error -1 = error opening advapi32.dll
;                  @error 1 = failed CryptAcquireContext
;                  @error 2 = failed CryptCreateHash
;                  @error 3 = failed CryptHashData
; Author(s):   Siao
; Modification(s):
;===============================================================================
Func _Crypt_HashData($vData, $iAlgID = 0x8003)
    Local $iLen = BinaryLen($vData), $hContext, $hHash, $aRet, $sRet = "", $iErr = 0, $tDat = DllStructCreate("byte[" & $iLen + 1 & "]"), $tBuf
    DllStructSetData($tDat, 1, $vData)
    $aRet = DllCall("advapi32.dll", 'int', 'CryptAcquireContext', 'ptr*', 0, 'ptr', 0, 'ptr', 0, 'dword', 1, 'dword', 0xF0000000) ;PROV_RSA_FULL = 1; CRYPT_VERIFYCONTEXT = 0xF0000000
    If Not @error And $aRet[0] Then
        $hContext = $aRet[1]
        $aRet = DllCall("advapi32.dll", 'int', 'CryptCreateHash', 'ptr', $hContext, 'dword', $iAlgID, 'ptr', 0, 'dword', 0, 'ptr*', 0)
        If $aRet[0] Then
            $hHash = $aRet[5]
            $aRet = DllCall("advapi32.dll", 'int', 'CryptHashData', 'ptr', $hHash, 'ptr', DllStructGetPtr($tDat), 'dword', $iLen, 'dword', 0)
            If $aRet[0] Then
                $aRet = DllCall("advapi32.dll", 'int', 'CryptGetHashParam', 'ptr', $hHash, 'dword', 2, 'ptr', 0, 'int*', 0, 'dword', 0) ;HP_HASHVAL = 2
                $tBuf = DllStructCreate("byte[" & $aRet[4] & "]")
                DllCall("advapi32.dll", 'int', 'CryptGetHashParam', 'ptr', $hHash, 'dword', 2, 'ptr', DllStructGetPtr($tBuf), 'int*', $aRet[4], 'dword', 0)
                $sRet = Hex(DllStructGetData($tBuf, 1))
            Else
                $iErr = 3
            EndIf
            DllCall("advapi32.dll", 'int', 'CryptDestroyHash', 'ptr', $hHash)
        Else
            $iErr = 2
        EndIf
        DllCall("advapi32.dll", 'int', 'CryptReleaseContext', 'ptr', $hContext, 'dword', 0)
    Else
        $iErr = 1
    EndIf
    Return SetError($iErr, 0, $sRet)
EndFunc   ;==>_Crypt_HashData

btw, error checking is needed outside the function and maybe to check if $iLen <> 0 inside the function.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
euverve

Thanks for the function and example...

This works...

$sFile = "c:\sample.exe"

$hFile = FileOpen($sFile, 16)

$bFile = FileRead($hFile)

FileClose($hFile)

$sHash = "93D6C4A9122F6E7F86C6D790ECBCA237"

If (_Crypt_HashData($bFile) = $sHash) Then

FileDelete($sFile)

EndIf

;===============================================================================

; Function Name: _Crypt_HashData()

; Description: Calculate hash from data

; Syntax:

; Parameter(s): $vData - data to hash, can be binary or a string

; $iAlgID - hash algorithm identifier, can be one of the following:

; 0x8001 = MD2

; 0x8002 = MD4

; 0x8003 = MD5 (default)

; 0x8004 = SHA1

; also see http://msdn.microsoft.com/en-us/library/aa375549(VS.85).aspx

; Requirement(s):

; Return Value(s): Success = Returns hash string

; Failure = Returns empty string and sets error:

; @error -1 = error opening advapi32.dll

; @error 1 = failed CryptAcquireContext

; @error 2 = failed CryptCreateHash

; @error 3 = failed CryptHashData

; Author(s): Siao

; Modification(s):

;===============================================================================

Func _Crypt_HashData($vData, $iAlgID = 0x8003)

Local $iLen = BinaryLen($vData), $hContext, $hHash, $aRet, $sRet = "", $iErr = 0, $tDat = DllStructCreate("byte[" & $iLen + 1 & "]"), $tBuf

DllStructSetData($tDat, 1, $vData)

$aRet = DllCall("advapi32.dll", 'int', 'CryptAcquireContext', 'ptr*', 0, 'ptr', 0, 'ptr', 0, 'dword', 1, 'dword', 0xF0000000) ;PROV_RSA_FULL = 1; CRYPT_VERIFYCONTEXT = 0xF0000000

If Not @error And $aRet[0] Then

$hContext = $aRet[1]

$aRet = DllCall("advapi32.dll", 'int', 'CryptCreateHash', 'ptr', $hContext, 'dword', $iAlgID, 'ptr', 0, 'dword', 0, 'ptr*', 0)

If $aRet[0] Then

$hHash = $aRet[5]

$aRet = DllCall("advapi32.dll", 'int', 'CryptHashData', 'ptr', $hHash, 'ptr', DllStructGetPtr($tDat), 'dword', $iLen, 'dword', 0)

If $aRet[0] Then

$aRet = DllCall("advapi32.dll", 'int', 'CryptGetHashParam', 'ptr', $hHash, 'dword', 2, 'ptr', 0, 'int*', 0, 'dword', 0) ;HP_HASHVAL = 2

$tBuf = DllStructCreate("byte[" & $aRet[4] & "]")

DllCall("advapi32.dll", 'int', 'CryptGetHashParam', 'ptr', $hHash, 'dword', 2, 'ptr', DllStructGetPtr($tBuf), 'int*', $aRet[4], 'dword', 0)

$sRet = Hex(DllStructGetData($tBuf, 1))

Else

$iErr = 3

EndIf

DllCall("advapi32.dll", 'int', 'CryptDestroyHash', 'ptr', $hHash)

Else

$iErr = 2

EndIf

DllCall("advapi32.dll", 'int', 'CryptReleaseContext', 'ptr', $hContext, 'dword', 0)

Else

$iErr = 1

EndIf

Return SetError($iErr, 0, $sRet)

EndFunc ;==>_Crypt_HashData

Edited by Thatskie

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×