Einzelnen Beitrag anzeigen

CorVu5

Registriert seit: 31. Dez 2007
26 Beiträge
 
Delphi 7 Professional
 
#1

Problem beim Portieren eines C++ Sources nach Delphi

  Alt 21. Okt 2008, 16:12
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 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: GetHashStr('q',strq); sollte sein:
Zitat:
C6FB044EC2BD401521D6B1082276415638196D8004
Ich hoffe, dass einer von den Freizeit-C-Göttern hier sich meiner erbarmen wird.
Danke im Voraus
CorVu5
Das Leben ist wie ein Strand...und dann stirbt man.
  Mit Zitat antworten Zitat