Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Problem beim Portieren eines C++ Sources nach Delphi (https://www.delphipraxis.net/122754-problem-beim-portieren-eines-c-sources-nach-delphi.html)

CorVu5 21. Okt 2008 16:12


Problem beim Portieren eines C++ Sources nach Delphi
 
Hallo liebe Leute,
ich habe mal wieder ein Problem und zwar versuche ich derzeit einen C++-Code nach Delphi (7) zu übersetzen.
Die fragliche C-Funktion:
Delphi-Quellcode:
#include <windows.h>
#include <wincrypt.h>
Delphi-Quellcode:
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&#12288;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);
            }
        }
    }
}
Meine konvertierung bis jetzt:
Delphi-Quellcode:
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;
Damit kann ich mir auch sehr schön Hashes generieren lassen, es scheinen jedoch nicht die Richtige zu sein.
Meine Vermutungen wo der Fehler liegen könnte habe ich kenntlich gemacht.
Ein gültiger Output für:
Delphi-Quellcode:
GetHashStr('q',strq);
sollte sein:
Zitat:

C6FB044EC2BD401521D6B1082276415638196D8004
Ich hoffe, dass einer von den Freizeit-C-Göttern hier sich meiner erbarmen wird. :-D
Danke im Voraus
CorVu5


Alle Zeitangaben in WEZ +1. Es ist jetzt 15:59 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz