EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Calculate SHA1 hash from String

Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.
#21583
Posted: 09/18/2012 04:18:45
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

Good day,
I try calculate the hash SHA-1 of text in String.
I use function:
Code
function HashSha1(HashedString: String; out Hash: String): Boolean;
var
  HashFunction: TElHashFunction;
  Data: ByteArray;
  Hash_: BufferType;
begin
  Result := False;
  Hash := '';
  HashFunction := TElHashFunction.Create(SB_ALGORITHM_DGST_SHA1);
  try
    HashFunction.Update(@HashedString[1], Length(HashedString) * SizeOf(Char)); // to make it work with ansi/unicode strings
    Hash_ := HashFunction.Finish;
    Hash := BinaryToString(Hash_);
    Result := True;
  finally
    HashFunction.Free;
  end;
end;


Test example:
Code
var
  AData: String;
  AHash: AnsiString;
begin
  AData := '<Envelope xmlns="http://example.org/envelope">' + #13 +
  '  <Body>' + #13 +
  '    Ol'#$C3#$A1' mundo' + #13 +
  '  </Body>' + #13 +
  '  ' + #13 +
  '</Envelope>';
  HashSha1(AData, AHash);
  ShowMessage(AHAsh);
end;

Function return value 9ED95B1F1622835150706A2BC0EEFA1BFC8F6C75, but rigth result value is 516B984D8BA0D7427593984A7E89F1B6182B011F.

When I change variable AData to type AnsiString and I change in function row "HashFunction.Update(@HashedString[1], Length(HashedString) * SizeOf(Char));" to row "HashFunction.Update(@HashedString[1], Length(HashedString));", then function result 1E586DCA4E15422148B7B5B9E176ADD1D932D396 - it's bad value.

When I load this text from file via TFileStream, use "HashFunction.UpdateStream(F);", than function return right value.

What am I doing wrong?
Thank you, Martin
#21584
Posted: 09/18/2012 04:24:46
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

Its possible that the input text is encoded somehow. Could you post here the input file for investigation.
#21585
Posted: 09/18/2012 04:27:07
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

Sending a test file.


[ Download ]
#21587
Posted: 09/18/2012 04:35:32
by Vsevolod Ievgiienko (EldoS Corp.)

The difference is that you should use #10 instead of #13 as the EOL marker.
#21588
Posted: 09/18/2012 04:52:57
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

In XML text I must use only #13 as separator marker. In file is no problem. Is problem use only #13 in String?
#21589
Posted: 09/18/2012 04:57:41
by Vsevolod Ievgiienko (EldoS Corp.)

Yes the problem is in #13 because a hash function doesn't operate with a text but with an array of bytes. If you use #13 instead of #10, then this array is different and you get a different hash value.
#21590
Posted: 09/18/2012 05:08:56
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

I can try use array of bytes (instead of string).
Thank you, Martin.
#21592
Posted: 09/18/2012 08:02:29
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

I convert AnsiString to ByteArray and it does not work.
Code
var
  HashString: AnsiString;
  HashFunction: TElHashFunction;
  Data: ByteArray;
  i: Integer;
begin
  HashString := 'data...';
  SetLength(Data, Length(HashString));
  for i := 1 to Length(HashString) do
    Data[i-1] := Ord(HashString[i]);

  HashFunction := TElHashFunction.Create(SB_ALGORITHM_DGST_SHA1);
  HashFunction.Update(@Data[0], Length(Data));
  ...


WhenI I filled TByteArray from code, hash calculated correctly.

Thank you, Martin
#21593
Posted: 09/18/2012 08:08:51
by Vsevolod Ievgiienko (EldoS Corp.)

If a hash is wrong then contents of ByteArray is not the same as in the file. Please open your file with some HEX editor to compare its content with the string processed by your code.
#21594
Posted: 09/18/2012 08:30:22
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

You are right. I found the text file and difference. I'm sorry. Martin
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

Topic viewed 2051 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!