Nabend liebe Leute,
ich mühe mich grade mit der Konvertierung eines (V)C++ Codes nach Delphi (7).
Meine Konvertierung bis jetzt:
Beim Aufruf
bekomme ich aber falsche ergebnisse, richtig müssste es:
sein.
Ich vermute etweder, dass die Größe von password falsch berechnet wird oder dass ein allgemeines Problem mit PWidechar gibt.
Danke im Voraus
garfield
ich mühe mich grade mit der Konvertierung eines (V)C++ Codes nach Delphi (7).
Code:
#include <windows.h>
#include <wincrypt.h>
void GetHashStr(wchar_t *Password,char *HashStr)
{
HashStr[0]='\0';
HCRYPTPROV hProv = NULL;
HCRYPTHASH hHash = NULL;
CryptAcquireContext(&hProv, 0,0,PROV_RSA_FULL,0);
// instance of hash calculation
if(CryptCreateHash(hProv,CALG_SHA1, 0, 0,&hHash)){
//calculation of hash value
if(CryptHashData(hHash,(unsigned char
*)Password,(wcslen(Password)+1)*2,0)){
// retrieve 20 bytes of hash value
DWORD dwHashLen=20;
BYTE Buffer[20];
if(CryptGetHashParam(hHash,HP_HASHVAL,Buffer,&dwHashLen,0)){
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
// creation of character string based on hash
char TmpBuf[128];
unsigned char tail=0;// variable to calculate value
for the last 2 bytes
// convert to a character string in hexadecimal
notation
for(int i=0;i<20;i++){
unsigned char c = Buffer[i];
tail+=c;
wsprintf(TmpBuf,"%s%2.2X",HashStr,c);
strcpy(HashStr,TmpBuf);
}
// add the last 2 bytes
wsprintf(TmpBuf,"%s%2.2X",HashStr,tail);
strcpy(HashStr,TmpBuf);
}
}
}
}
Code:
procedure GetHashStr(password : PWidechar; var hashstr : String);
var
hProv : HCRYPTPROV;
hHash : HCRYPTHASH;
buffer: array[0..19] of byte;
dwhashlen : DWord;
i : Integer;
tail : Byte;
begin
tail := 0;
CryptAcquireContext(@hProv,0,0,PROV_RSA_FULL,0);
if CryptCreatehash(hProv, CALG_SHA1, 0, 0,@hHash) Then begin
if CryptHashData(hHash,@password, SizeOf(password),0) Then begin //möglicherweise falsche Größe?
dwHashLen := 20;
If CryptGetHashParam(hHash,HP_HASHVAL,@Buffer[0],@dwHashLen,0) Then begin
CryptDestroyHash(hHash);
CryptReleaseContext(hProv, 0);
For i := 0 To dwHashLen - 1 Do begin
tail := tail + buffer[i];
hashstr := hashstr + Format('%2.2X', [buffer[i]]);
end;
hashstr := hashstr + Format('%2.2X', [tail]);
end;
end;
end;
end;
Code:
GetHashStr('q',strq);
Code:
C6FB044EC2BD401521D6B1082276415638196D8004
Ich vermute etweder, dass die Größe von password falsch berechnet wird oder dass ein allgemeines Problem mit PWidechar gibt.
Danke im Voraus
garfield