EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Hash of bytes

Also by EldoS: CallbackDisk
Create virtual disks backed by memory or custom location, expose disk images as disks and more.
#21132
Posted: 08/22/2012 12:12:26
by Bremen Sistemas (Basic support level)
Joined: 08/20/2012
Posts: 17

Ok, i got this code to hash a file and string.

Code
var
  hashFunction : TElHashFunction;
  data : ByteArray;
  hashedString : string;
  hash : BufferType;
begin
  hashedString := 'Hello, world!';
  hashFunction := TElHashFunction.Create(SB_ALGORITHM_DGST_SHA256);
  hashFunction.Update(@hashedString[1], Length(hashedString) * SizeOf(hashedString[1])); // to make it work with ansi/unicode strings
  hash := hashFunction.Finish;
end;


But I need the hash of a ArrayofBytes
How to do this?

Code
      data := StrToByte(aString);  // Convert String to Bytes

            // Calc hash
            Stream := TStream.Create;
            try
              hashFunction := TElHashFunction.Create(SB_ALGORITHM_DGST_SHA1);
              try
                ???
                ???
              finally
                FreeAndNil(hashFunction);
              end;
              Dgst := hashFunction.Finish;
            finally
              FreeAndNil(Stream);
            end;
#21133
Posted: 08/22/2012 12:16:39
by Eugene Mayevski (EldoS Corp.)

Code
hashFunction.Update(@hashedString[1], Length(hashedString) * SizeOf(hashedString[1]));


in fact handles an array of bytes, contained in hashedString.

If you have

Code
var arr : array of byte;


then you call

Code
hashFunction.Update(@arr[0], Length(arr));


Sincerely yours
Eugene Mayevski
#21140
Posted: 08/22/2012 13:09:55
by Bremen Sistemas (Basic support level)
Joined: 08/20/2012
Posts: 17

What real result of this hashFunction.Finish ?

Bynary or String ?
#21141
Posted: 08/22/2012 13:16:24
by Eugene Mayevski (EldoS Corp.)

Hashes are *always* performed on binary data and produce a binary result. Forget about strings in cryptography world.


Sincerely yours
Eugene Mayevski

Reply

Statistics

Topic viewed 665 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!