Thema: Delphi PHP hmac+salt in Delphi

Einzelnen Beitrag anzeigen

gammatester

Registriert seit: 6. Dez 2005
999 Beiträge
 
#5

AW: PHP hmac+salt in Delphi

  Alt 6. Feb 2012, 14:01
Hier ein Testprogramm mit dem Fallbeispiel von PHP-Net-Manula zu hash_hmac:
Delphi-Quellcode:
{-Test prog for HMACs, we 06.Feb.2012}

program t_hmac1;

{$i STD.INC}

{$ifdef APPCONS}
  {$apptype console}
{$endif}

uses
  hash,hmac,whirl512,rmd160,BTypes,mem_util;


{---------------------------------------------------------------------------}
procedure Test_RMD160;
const
  DigLen = sizeof(TRMD160Digest);
const
  fox : Bstring = 'The quick brown fox jumped over the lazy dog.';
  key : BString = 'secret';
var
  ctx : THMAC_Context;
  mac : THashDigest;
  phash: PHashDesc;
begin
  writeln('Test from http://php.net/manual/en/function.hash-hmac.php');
  writeln('hash_hmac(''ripemd160'', ''The quick brown fox jumped over the lazy dog.'', ''secret'')');
  phash := FindHash_by_Name('RIPEMD160');
  if phash=nil then begin
    writeln('Hash function not found/registered.');
    exit;
  end;
  hmac_init(ctx, phash, @key[1], length(key));
  hmac_update(ctx, @fox[1], length(fox));
  hmac_final(ctx, mac);
  writeln('Calc: ',HexStr(@mac, DigLen));
  writeln(' Ref: ','b8e7ae12510bdfb1812e463a7f086122cf37e4f7');
end;


{---------------------------------------------------------------------------}
procedure Test_Whirlpool;
const
  DigLen = sizeof(TWHirlDigest);
const
  fox : Bstring = 'The quick brown fox jumped over the lazy dog.';
  key : BString = 'secret';
var
  ctx : THMAC_Context;
  mac : THashDigest;
  phash: PHashDesc;
begin
  writeln('Test from http://php.net/manual/en/function.hash-hmac.php');
  phash := FindHash_by_Name('Whirlpool');
  if phash=nil then begin
    writeln('Hash function not found/registered.');
    exit;
  end;
  hmac_init(ctx, phash, @key[1], length(key));
  hmac_update(ctx, @fox[1], length(fox));
  hmac_final(ctx, mac);
  writeln('Calc: ',HexStr(@mac, DigLen));
end;


begin
  Test_RMD160;
  writeln;
  Test_Whirlpool;
end.
Die Ausgabe ist:
Code:
Test from http://php.net/manual/en/function.hash-hmac.php
hash_hmac('ripemd160', 'The quick brown fox jumped over the lazy dog.', 'secret')
Calc: b8e7ae12510bdfb1812e463a7f086122cf37e4f7
 Ref: b8e7ae12510bdfb1812e463a7f086122cf37e4f7

Test from http://php.net/manual/en/function.hash-hmac.php
Calc: 8bcb967604ee2f25718b1e9a9f9a87f16d41b678fb1537a56a0456400c78d6dc1b6ffe6d7a17761396f55119d853647e5746e88e7b3a3629b497d4e5f953cf0b
  Mit Zitat antworten Zitat