- Supported Hash Algorithms:
MD2
MD4
MD5
SHA1
SHA256
SHA384
SHA512
Have fun
Code: Select all
EnableExplicit
;------------------------------------------------------------------------------------
;hashi - HASH INCLUDE (x86/x64)
;Platform: Windows Vista, Windows Server 2008 (minimum supported)
;Version: Draft 2
;Author: Mijikai
;------------------------------------------------------------------------------------
;Note:
;Why the BCrypt API?
;The old Crypt API is deprecated!
;So this will only run on more modern machines but its the correct way to do it.
;------------------------------------------------------------------------------------
;ToDo:
;Maybe add a way to output the result as hex string!
;------------------------------------------------------------------------------------
Import "bcrypt.lib";<- Use the correct lib (x86/x64) from the MS SDK!
BCryptOpenAlgorithmProvider.i(*Algorithm,AlgId.s,Implementation.i,Flags.i)
BCryptCloseAlgorithmProvider.i(Algorithm.i,Flags.i)
BCryptGetProperty.i(Object.i,Property.s,*Output,OutputSize.i,*Result,Flags.i)
BCryptCreateHash.i(Algorithm.i,*Hash,*HashObject,HashObjectSize.i,*Secret,SecretSize.i,Flags.i)
BCryptHashData.i(Hash.i,*Input,InputSize.i,Flags.i)
BCryptFinishHash.i(Hash.i,*Output,OutputSize.i,Flags.i)
BCryptDestroyHash.i(Hash.i)
EndImport
Enumeration HASHI_HASH
#HASHI_MD2 ;16 bytes
#HASHI_MD4 ;16 bytes
#HASHI_MD5 ;16 bytes
#HASHI_SHA1 ;20 bytes
#HASHI_SHA256 ;32 bytes
#HASHI_SHA384 ;48 bytes
#HASHI_SHA512 ;64 bytes
EndEnumeration
Procedure.i hashiHash(*Buffer,BufferSize.i,Type.i,*Hash = #Null);optional: point *Hash to a buffer that should receive the hash!
Protected result.i
Protected handle.i
Protected *object
Protected object_size.i
Protected hash_size.i
Protected bytes.i
Protected hash.i
If *Buffer
Select Type
Case #HASHI_MD2
result = Bool(BCryptOpenAlgorithmProvider(@handle,"MD2",#Null,#Null) = #Null)
hash_size = 16
Case #HASHI_MD4
result = Bool(BCryptOpenAlgorithmProvider(@handle,"MD4",#Null,#Null) = #Null)
hash_size = 16
Case #HASHI_MD5
result = Bool(BCryptOpenAlgorithmProvider(@handle,"MD5",#Null,#Null) = #Null)
hash_size = 16
Case #HASHI_SHA1
result = Bool(BCryptOpenAlgorithmProvider(@handle,"SHA1",#Null,#Null) = #Null)
hash_size = 20
Case #HASHI_SHA256
result = Bool(BCryptOpenAlgorithmProvider(@handle,"SHA256",#Null,#Null) = #Null)
hash_size = 32
Case #HASHI_SHA384
result = Bool(BCryptOpenAlgorithmProvider(@handle,"SHA384",#Null,#Null) = #Null)
hash_size = 48
Case #HASHI_SHA512
result = Bool(BCryptOpenAlgorithmProvider(@handle,"SHA512",#Null,#Null) = #Null)
hash_size = 64
EndSelect
If result
result = #False
If BCryptGetProperty(handle,"ObjectLength",@object_size,SizeOf(Integer),@bytes,#Null) = #Null
If object_size
*object = AllocateMemory(object_size)
If *object
If BCryptCreateHash(handle,@hash,*object,object_size,#Null,#Null,#Null) = #Null
If BCryptHashData(hash,*Buffer,BufferSize,#Null) = #Null
If *Hash
result = Bool(BCryptFinishHash(hash,*Hash,hash_size,#Null) = #Null)
Else
*Hash = AllocateMemory(hash_size)
If *Hash
If BCryptFinishHash(hash,*Hash,hash_size,#Null) = #Null
result = *Hash
Else
FreeMemory(*Hash)
EndIf
EndIf
EndIf
EndIf
BCryptDestroyHash(hash)
EndIf
FreeMemory(*object)
EndIf
EndIf
EndIf
BCryptCloseAlgorithmProvider(handle,#Null)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure.i hashiSize(*Hash)
ProcedureReturn MemorySize(*Hash)
EndProcedure
Procedure.i hashiFree(*Hash)
ProcedureReturn FreeMemory(*Hash)
EndProcedure
Procedure.i Demo()
Protected dummy.s
Protected *hash
dummy = "Hello World!"
*hash = hashiHash(@dummy,StringByteLength(dummy),#HASHI_SHA512)
If *hash
Debug *hash
ShowMemoryViewer(*hash,hashiSize(*hash))
hashiFree(*hash)
EndIf
ProcedureReturn #Null
EndProcedure
Demo()
End