Einzelnen Beitrag anzeigen

Benutzerbild von Garfield
Garfield

Registriert seit: 9. Jul 2004
Ort: Aken (Anhalt-Bitterfeld)
1.335 Beiträge
 
Delphi XE5 Professional
 
#14

AW: SHA! Unit gesucht

  Alt 29. Mai 2012, 16:05
Was hälst Du von http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx ?

Sieht bei mir so aus:

Delphi-Quellcode:
type
  ULONG = Cardinal;
  ULONG_PTR = ^ULONG;
  THCRYPTPROV = ULONG;
  PTHCRYPTPROV = ULONG_PTR;
  LPCTSTR = PAnsiChar;
  PBYTE = ^Byte;
  ALG_ID = Cardinal;
  HCRYPTKEY = THandle;
  PHCRYPTKEY = HCRYPTKEY;
  PHCRYPTHASH = ULONG_PTR;

const
  ALG_CLASS_HASH = 4 shl 13;
  ALG_TYPE_ANY = 0;
  ALG_SID_MD5 = 3;
  ALG_SID_SHA = 4;
  ALG_SID_SHA1 = ALG_SID_SHA;

  CALG_MD5 = ALG_CLASS_HASH or ALG_TYPE_ANY or ALG_SID_MD5;
  CALG_SHA1 = ALG_CLASS_HASH or ALG_TYPE_ANY or ALG_SID_SHA1;

  PROV_RSA_FULL = 1;

  CRYPT_NEWKEYSET = $08;

  HP_HASHVAL = $0002;

  NTE_BAD_KEYSET = HRESULT($80090016);

function CryptAcquireContext(phProv: PTHCRYPTPROV; pszContainer, pszProvider: LPCTSTR; dwProvType, dwFlags: Cardinal): LongBool; stdcall; external 'Advapi32.dllname 'CryptAcquireContextA';
function CryptCreateHash(hProv: THCRYPTPROV; Algid: ALG_ID; hKey: HCRYPTKEY; dwFlags: Cardinal; phHash: PHCRYPTHASH): LongBool; stdcall; external 'Advapi32.dllname 'CryptCreateHash';
function CryptHashData(hHash: PHCRYPTHASH; pbData: PBYTE; dwDataLen, dwFlags: Cardinal): LongBool; stdcall; external 'Advapi32.dllname 'CryptHashData';
function CryptGetHashParam(hHash: PHCRYPTHASH; dwParam: Cardinal; pbData: PBYTE; pdwDataLen: PCardinal; dwFlags: Cardinal): LongBool; stdcall; external 'Advapi32.dllname 'CryptGetHashParam';
function CryptDestroyHash(hHash: PHCRYPTHASH): LongBool; stdcall; external 'Advapi32.dllname 'CryptDestroyHash';
function CryptReleaseContext(hProv: THCRYPTPROV; dwFlags: Cardinal): LongBool; stdcall; external 'Advapi32.dllname 'CryptReleaseContext';

function CreateHash(aString: AnsiString; aAlgoID: Cardinal): AnsiString;
var
  hCryptProv : THCRYPTPROV;
  hHash : PHCRYPTHASH;
  dwDataLen : Cardinal;
  Data : Array[1..20] of Byte;
  i : Integer;
begin
  {
  *  http://msdn.microsoft.com/en-us/library/windows/desktop/aa379886.aspx
  *  Handle zum Provider Content.
  }

  if CryptAcquireContext(@hCryptProv, nil, nil, PROV_RSA_FULL, 0)
  or ((GetLastError = NTE_BAD_KEYSET)
  and CryptAcquireContext(@hCryptProv, nil, nil, PROV_RSA_FULL, CRYPT_NEWKEYSET))
  then begin
    {
    *  http://msdn.microsoft.com/en-us/library/windows/desktop/aa379908.aspx
    *  Handle zum Hashobject.
    }

    if CryptCreateHash(hCryptProv, aAlgoID, 0, 0, @hHash)
    then begin
      {
      *  http://msdn.microsoft.com/en-us/library/windows/desktop/aa380202.aspx
      *  Daten übergeben.
      }

      CryptHashData(hHash, Pointer(PByte(aString)), Length(aString), 0);
      {
      *  http://msdn.microsoft.com/en-us/library/windows/desktop/aa379947.aspx
      *  Hash abholen.
      }

      FillChar(Data, SizeOf(Data), 0);
// if aAlgoID = CALG_SHA1 then dwDataLen := 20;
// if aAlgoID = CALG_MD5 then dwDataLen := 16;
      dwDataLen := 20;
      if CryptGetHashParam(hHash, HP_HASHVAL, @Data, @dwDataLen, 0)
      then begin
        {
        *  Ergebnis übergeben
        }

        i := 1;
        Result := '';
        while (Data[i] <> 0) and (i < dwDataLen + 1)
        do begin
          Result := Result + Chr(Integer(Data[i]));
          inc(i);
        end;
      end;
      {
      *  [url]http://msdn.microsoft.com/en-us/library/windows/desktop/aa379917.aspx[/url]
      *  Handle zum Hashobject freigeben.
      }

      if hHash <> nil
      then CryptDestroyHash(hHash);
      {
      *  [url]http://msdn.microsoft.com/en-us/library/windows/desktop/aa380268.aspx[/url]
      *  Den Content releasen.
      }

      if hCryptProv <> 0
      then CryptReleaseContext(hCryptProv, 0);
    end;
  end;
end;
Hinweis: Data muss so lang sein, dass die Hashes hineinpassen. Der Wert für dwDataLen kann von der Länge des Hashes bis zur Länge von Data liegen. Nach Aufruf von CryptGetHashParam enthält er die Länge des Hashes.
Gruss Garfield
Ubuntu 22.04: Laz2.2.2/FPC3.2.2 - VirtBox6.1+W10: D7PE, DXE5Prof

Geändert von Garfield (29. Mai 2012 um 16:21 Uhr)
  Mit Zitat antworten Zitat