Jump to content



Photo

MD5 Hash Check


  • Please log in to reply
5 replies to this topic

#1 euverve

euverve

    Seeker

  • Active Members
  • 42 posts

Posted 15 January 2009 - 02:20 AM

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...







#2 tip

tip

    Adventurer

  • Active Members
  • PipPip
  • 103 posts

Posted 15 January 2009 - 05:46 AM

Hi,

Take a look at this

http://www.autoitscript.com/forum/index.ph...=76976&st=0

:)

Cheers,
Tip

MsgBox_Tipped: Eye candy msgboxes/inputboxes/loginboxes. | CreateBlankBox: Semi-transparent layers with borders and rounded corners.


#3 euverve

euverve

    Seeker

  • Active Members
  • 42 posts

Posted 16 January 2009 - 02:59 AM

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

#4 euverve

euverve

    Seeker

  • Active Members
  • 42 posts

Posted 16 January 2009 - 05:44 PM

*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

#5 trancexx

trancexx

    Hm, I really shouldn't.

  • Active Members
  • PipPipPipPipPipPip
  • 5,194 posts

Posted 16 January 2009 - 06:25 PM

*bump*

Here's what I wanted to implement.



An input guys... Thanks

You are kind of confusing.
This helps?
AutoIt         
$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


#6 euverve

euverve

    Seeker

  • Active Members
  • 42 posts

Posted 17 January 2009 - 01:06 AM

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, 17 January 2009 - 01:08 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users