EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Checksum

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#24634
Posted: 04/21/2013 15:07:53
by Ciaran Costelloe (Standard support level)
Joined: 07/10/2009
Posts: 26

Is there a function that will calculate the checksum of a string? Ideally, SHA-2, or one with a low chance of collision?

Thanks,

Ciaran
#24635
Posted: 04/21/2013 15:56:56
by Ken Ivanov (EldoS Corp.)

Hello Ciaran,

Use the TElHashFunction class to get a message digest (hash) of a string:

Code
uses
  SBHashFunction, SBConstants;
...
var
  Func : TElHashFunction;
  Digest : BufferType;
...
  Func := TElHashFunction.Create(SB_ALGORITHM_DGST_SHA256);
  try
    Func.Update(...);  // <-- pass the string here
    Digest := Func.Finish();
  finally
    FreeAndNil(Func);
  end;
#24637
Posted: 04/22/2013 05:12:45
by Ciaran Costelloe (Standard support level)
Joined: 07/10/2009
Posts: 26

Hi Ken.

Brilliant! That is perfect. Thank you very much.

Ciaran
#27037
Posted: 10/31/2013 05:42:32
by Rickard Hultgren (Standard support level)
Joined: 10/11/2013
Posts: 17

I tried this, but it doesn't seem to work correctly as I do it. The hash returned is different from what I get at for example http://www.functions-online.com/md5.html.

The thing missing above (Update parameter) I have done like this:

Code
    func.Update(@AString[1], Length(AString) * SizeOf(Char));

To make a string out of it I call

Code
   BinaryToString(Digest);

Something must be wrong - please enlighten me :-)
#27038
Posted: 10/31/2013 05:45:36
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

Most likelly you are using Unicode Delphi and the service uses ANSI charset. Please try to convert your AString to AnsiString type and then pass to Update method.
#27040
Posted: 10/31/2013 05:46:57
by Eugene Mayevski (EldoS Corp.)

If you are using the precompiled version, remember to set the license key before calling any SecureBlackbox functions.


Sincerely yours
Eugene Mayevski
#27041
Posted: 10/31/2013 05:48:45
by Ken Ivanov (EldoS Corp.)

Hello Anders,

And please remember to pass a valid license key to the SetLicenseKey() method at the start of your application. The absence of the key may result in hashes being calculated in [intentionally] incorrect way.
#27048
Posted: 10/31/2013 09:38:57
by Rickard Hultgren (Standard support level)
Joined: 10/11/2013
Posts: 17

Thanks for your replies. License is not the problem, Unicode was :-)

I changed into some more generic function:

Code
function DoHash(AAlgorithm: integer; const AString: string): string;
var
  func: TElHashFunction;
  dgst: BufferType;
  s: AnsiString;
begin
  func := TElHashFunction.Create(AAlgorithm);
  try
    s := AString;
    func.Update(@s[1], Length(s));
    dgst := func.Finish();
  finally
    FreeAndNil(func);
  end;
  Result := BinaryToString(dgst);
end;


This seems to give the same result as the corresponding php function.

If I however write like this:
Code
function DoHash(AAlgorithm: integer; const AString: string): string;
var
  func: TElHashFunction;
  dgst: BufferType;
begin
  func := TElHashFunction.Create(AAlgorithm);
  try
    func.Update(@AString[1], Length(AString) * SizeOf(Char));
    dgst := func.Finish();
  finally
    FreeAndNil(func);
  end;
  Result := BinaryToString(dgst);
end;


I would get a different result, but one that is the same regardless of code pages and such stuff?

So I basically have a choice between "php compatibility" and code page ignorance!

Have I got this right?

/Anders
#27051
Posted: 10/31/2013 09:47:13
by Eugene Mayevski (EldoS Corp.)

Hash is calculated over binary data, not over text. It's up to you to convert text to the binary representation which you find acceptable. This includes any charset and encoding conversions you find required. The type of conversion should obviously correspond in one way or another to the conversion performed by the code which will verify the hash later.


Sincerely yours
Eugene Mayevski
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 1148 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!