Jump to content

SHA-256


Danyfirex
 Share

Recommended Posts

I was needing to get SHA-256 from some file. and I made this.

#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

Link to comment
Share on other sites

...like for updating crypt, tomorrow?   :guitar:

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Thanks for sharing it - might be useful someday...

 

Br,

UEZ

 

Thank's UEZ

...like for updating crypt, tomorrow?   :guitar:

It should be great.

Saludos

Link to comment
Share on other sites

thanks kaesereibe. I notice that with crypt.au3 was easy to make the funtion. (after I made it :S)

Saludos

Link to comment
Share on other sites

Those constants are declared in the UDF and therefore don't need to be included at the top of your script.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Those constants are declared in the UDF and therefore don't need to be included at the top of your script.

In the version 3.3.12.0 are as comment.

Saludos

Link to comment
Share on other sites

  • 10 months later...

I compared several files using HashMyFiles by NirSoft, and HashTab by Implbits

I noticed that the two programs listed above when hashing files in sha256 were always the same when comparing random files.

Using the code here to compare the same random file i picked can at times generate a different hash. Not all the time or per say the same file type. 

Can anyone confirm of deny this?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...