voici quelque fonctions wrappers qui faciliterons les calculs de sha1 via la dll crypto d'openssl : libeay32.dll. Ce fichier est généralement dans @systemdir. Si ce n'est pas le cas, on peut la télécharger via le net.
voici la librairie en question openssl_lib.au3 :
► Afficher le texte
Code : Tout sélectionner
#include-once
Func Init_OpenSSL(ByRef $hnd_openssl)
$hnd_openssl=DllOpen(@SystemDir&"\libeay32.dll")
If $hnd_openssl=-1 Then Return 0
Return 1
EndFunc
Func Release_Openssl($hnd_openssl)
If $hnd_openssl>0 Then DllClose($hnd_openssl)
EndFunc
Func Build_sha1_struct($mess,$len_mess)
Local $loc_strsha1,$loc_len
If $len_mess=0 Then
SetError(1)
return 0
EndIf
$loc_strsha1=DllStructCreate("ubyte tohash["&$len_mess&"];uint lenoftohash;ubyte sha1[20]")
DllStructSetData($loc_strsha1,"tohash",$mess)
DllStructSetData($loc_strsha1,"lenoftohash",$len_mess)
SetError(0)
Return $loc_strsha1
EndFunc
Func Destroye_sha1_struct(ByRef $strsha1)
$strsha1=0
EndFunc
Func Sha1_compute($hnd_openssl,$strsha1)
Local $ptr_tohash,$ptr_sha1,$loc_err
If $strsha1="" Or Number($hnd_openssl)=0 Then
SetError(1)
Return 0
EndIf
$ptr_tohash=DllStructGetPtr($strsha1,"tohash")
$ptr_sha1=DllStructGetPtr($strsha1,"sha1")
if $ptr_sha1=0 Or $ptr_tohash=0 Then
SetError(2)
Return 0
EndIf
;unsigned char *SHA1(const unsigned char *d, unsigned long n,unsigned char *md);
DllCall($hnd_openssl,"ubyte*:cdecl","SHA1","ptr",$ptr_tohash,"ulong",DllStructGetData($strsha1,"lenoftohash"),"ptr",$ptr_sha1)
$loc_err=@error
If $loc_err Then
SetError(10+$loc_err)
Return 0
EndIf
Return DllStructGetData($strsha1,"sha1")
EndFunc
► Afficher le texte
Code : Tout sélectionner
#include "openssl_lib.au3"
Opt("OnExitFunc","My_Exit")
Dim $hnd_openssl
if Not Init_OpenSSL($hnd_openssl) Then
ConsoleWriteError("Pb d'init de libeay32.dll")
Exit 1
EndIf
#Region
;Sha1 d'une string
#EndRegion
$str_to_hash="test"
$ctx_sha1=Build_sha1_struct($str_to_hash,StringLen($str_to_hash))
If @error Then
ConsoleWriteError("Pb de creation de contexte sha1")
Exit 1
EndIf
$sha1=Sha1_compute($hnd_openssl,$ctx_sha1)
if @error Then
ConsoleWriteError("Pb de calcule de sha1")
Destroye_sha1_struct($ctx_sha1)
Exit 1
EndIf
ConsoleWrite("Sha1("&$str_to_hash&") = "&$sha1&@CRLF)
Destroye_sha1_struct($ctx_sha1)
#Region
;Sha1 d'une séquence binaire pure
#EndRegion
$str_to_hash=Binary("0x01020304")
$ctx_sha1=Build_sha1_struct($str_to_hash,BinaryLen($str_to_hash))
If @error Then
ConsoleWriteError("Pb de creation de contexte sha1")
Exit 1
EndIf
$sha1=Sha1_compute($hnd_openssl,$ctx_sha1)
if @error Then
ConsoleWriteError("Pb de calcule de sha1")
Destroye_sha1_struct($ctx_sha1)
Exit 1
EndIf
ConsoleWrite("Sha1("&$str_to_hash&") = "&$sha1&@CRLF)
Destroye_sha1_struct($ctx_sha1)
Func My_Exit()
Release_Openssl($hnd_openssl)
EndFunc