SHA-256 Ejemplo
Publicado: 22 Dic 2014, 02:32
Bueno una función que necesitaba.
Saludos
Código: Seleccionar todo
#include <File.au3>
Opt("MustDeclareVars",1)
Global Const $iHashLen=32
Global Const $Advapi32="Advapi32.dll"
Global Const $PROV_RSA_AES=24
Global Const $CALG_SHA_256=0x0000800c
Global Const $CRYPT_VERIFYCONTEXT = 0xF0000000
Global Const $HP_HASHVAL=0x0002
Local $aFiles=_FileListToArray(@SystemDir,"*.exe",1,True)
_ArrayDisplay($aFiles)
Local $aFiles2[UBound($aFiles)-1][2]
For $i= 1 to $aFiles[0]
$aFiles2[$i-1][0]=$aFiles[$i]
$aFiles2[$i-1][1]=SHA256($aFiles[$i])
Next
_ArrayDisplay($aFiles2)
Func SHA256($sFile)
Local $hCryptProv=0
Local $hHash=0
Local $Ret = 0
Local $iSize=0
Local $tBuffer=0
Local $hHash=0
Local $tHashData=0
Local $sHASH=""
if not FileExists($sFile) Then Return ""
$iSize=FileGetSize($sFile)
$Ret = DllCall($Advapi32, "bool", "CryptAcquireContextW", "handle*", 0, "ptr", NULL, "ptr", Null,"dword",$PROV_RSA_AES,"dword",$CRYPT_VERIFYCONTEXT)
ConsoleWrite("+ CryptAcquireContextW Ret= " & $Ret[0] & @TAB & '>Error code: ' & @error & @CRLF)
$hCryptProv=$Ret[1]
ConsoleWrite("$hCryptProv= " & $hCryptProv & @CRLF)
$Ret = DllCall($Advapi32, "bool", "CryptCreateHash", "handle", $hCryptProv, "dword",$CALG_SHA_256,"dword",0,"dword",0,"handle*",0)
ConsoleWrite("+ CryptCreateHash Ret= " & $Ret[0] & @TAB & '>Error code: ' & @error & @CRLF)
$hHash=$Ret[5]
ConsoleWrite("$pHash= " & $hHash & @CRLF)
$tBuffer=DllStructCreate("byte Data[" & $iSize & "]")
$tBuffer.Data=FileRead($sFile)
$Ret = DllCall($Advapi32, "bool", "CryptHashData", "handle", $hHash,"ptr",DllStructGetPtr($tBuffer),"dword",DllStructGetSize($tBuffer),"dword",0)
ConsoleWrite("+ CryptHashData Ret= " & $Ret[0] & @TAB & '>Error code: ' & @error & @CRLF)
$tHashData=DllStructCreate("byte[" & $iHashLen & "]")
$Ret = DllCall($Advapi32, "bool", "CryptGetHashParam", "handle", $hHash, "dword",$HP_HASHVAL,"ptr",DllStructGetPtr($tHashData),"dword*",$iHashLen,"dword",0)
ConsoleWrite("+ CryptGetHashParam Ret= " & $Ret[0] & @TAB & '>Error code: ' & @error & @CRLF)
For $i=1 to $iHashLen
$sHASH&=Hex(DllStructGetData($tHashData, 1, $i), 2)
Next
if $hHash Then
$Ret = DllCall($Advapi32, "bool", "CryptDestroyHash", "handle",$hHash)
ConsoleWrite("+ CryptDestroyHash Ret= " & $Ret[0] & @TAB & '>Error code: ' & @error & @CRLF)
EndIf
If $hCryptProv Then
$Ret = DllCall($Advapi32, "bool", "CryptReleaseContext", "handle", $hCryptProv, "dword",0)
ConsoleWrite("+ CryptReleaseContext Ret= " & $Ret[0] & @TAB & '>Error code: ' & @error & @CRLF)
EndIf
$tBuffer=Null
$tHashData=Null
Return $sHASH
EndFunc
Saludos