title: Delphi 2010 DCPCrypt2 Sha256 Hash doesn't equal PHP Hash
body: <p>I'm writing a Delphi 2010 application that will connect to the PHP Web application to get specific data. Web page on the server accepts 3 <strong>GET</strong> parameters:</p>
  
  <ol>
  <li>cityid   // Value needed for web application to return specific data</li>
  <li>salt     // Current time stamp: Year Month Date Hour Minute Second Millisecond</li>
  <li>token    // SHA-256 hash with security key for protection</li>
  </ol>
  
  <p>The problem is with <strong>token</strong>. It's generated in Delphi using these functions:</p>
  
  <pre><code>function DigestToStr(Digest: array of byte): string;
  var
    i: Integer;
  begin
    Result := '';
    for i := 0 to Length(Digest) do
      Result := Result + LowerCase(IntToHex(Digest[i], 2));
  end;
  
  function GetStringHash(Source: string): string;
  var
    Hash: TDCP_sha256;
    Digest: array[0..31] of Byte;
  begin
    Hash := TDCP_sha256.Create(nil);
    Hash.Init;
    Hash.UpdateStr(Source);
    Hash.Final(Digest);
    Hash.Free;
    Result := DigestToStr(Digest);
  end;
  
  procedure TMain.Button3Click(Sendere: TObject);
  var
    TheToken,Sha256:string;
  begin
    salt:=FormatDateTime('yyyymmddhhnnsszzz',Now);
    TheToken:=salt+'THE_SECRET_KEY'+MCity;
    Sha256:=GetStringHash(TheToken);
  end;
  </code></pre>
  
  <p>Delphi gives this string for a <strong>token</strong>: <strong>ec4338bb3e7819a1148ffc01713f6d164f4f5940c5758d896b0a3d53f30f8c24f0</strong></p>
  
  <p>PHP's <strong>hash</strong> function gives totally different string:</p>
  
  <pre><code>&lt;?php
    hash('sha256','20120119115458592THE_SECRET_KEY1');
  ?&gt;
  </code></pre>
  
  <p><strong>8b4cff3b937bbcfae8fa7a67ce407b6185451a52e994ea2ef92a760676717490</strong></p>
  
  <p>I tried to look for similar questions on the Web, but found only symmetric encryption/description problems discussed, I don't need symmetric encryption for the <strong>token</strong></p>
  
  <hr>
  
  <p><strong>UPDATE</strong>
  I've figured out!!! The problem was in the wrong variable type!</p>
  
  <p>I used <strong>string</strong>, and when I replaced it with <strong>AnsiString</strong> it worked out!</p>
  
  <pre><code>function GetStringHash(Source: AnsiString): string;
  var
    ...
  </code></pre>
  
  <p>Besides that I replaced</p>
  
  <pre><code>  for i := 0 to Length(Digest) do
  </code></pre>
  
  <p>with</p>
  
  <pre><code>  for i := 0 to Length(Digest)-1 do
  </code></pre>
  
  <p>in <strong>DigestToStr</strong> function</p>
  
  <p>Sorry for causing disturbance, but I hope this will help somebody!</p>