#1 2020-07-15 21:11:24

ertank
Member
Registered: 2016-03-16
Posts: 168

Problem with SynCrypto.SHA256Digest()

Hello,

I am using Delphi 10.3.3, Synopse commit version 1.18.6045, generating Win32 executable.
I had some legacy code that I am modifying. I used to pass parameter as array of byte and now I am changing it into TBytes. However, I am getting different result for same byte values. My test code is as following:

uses
  SynCrypto;

const
  TestArray: Array [0..5] of Byte = (1, 2, 3, 4, 5, 6);
  TestBytes: TBytes = [1, 2, 3, 4, 5, 6];


procedure TForm1.Button1Click(Sender: TObject);
var
  Sha2561: TSHA256Digest;
  Sha2562: TSHA256Digest;
begin
  Sha2561 := SHA256Digest(Pointer(@TestArray), SizeOf(TestArray));
  Sha2562 := SHA256Digest(Pointer(@TestBytes), Length(TestBytes)); // For TBytes I must use Length, SizeOf always returns 4 
  if not CompareMem(@Sha2561, @Sha2562, 32) then
    ShowMessage('different')
  else
    ShowMessage('identical');
end;

For above test, I get different message displayed.

I am not sure if I am doing something wrong. I could not remember why I did write a code like

Pointer(@TestArray)

That code is possibly a copy-paste from this forum, years ago. In any way, using it as

@TestArray

also displays different message for me.

Any help is appreciated.

Thanks & Regards,
Ertan

Offline

#2 2020-07-15 21:59:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: Problem with SynCrypto.SHA256Digest()

Try Pointer(TestBytes) instead.
TBytes is a dynamic array, therefore already a pointer.

Offline

#3 2020-07-15 22:14:02

ertank
Member
Registered: 2016-03-16
Posts: 168

Re: Problem with SynCrypto.SHA256Digest()

That worked just fine.
Pointer(TestBytes) generates identical hash.

Thank you.

Offline

Board footer

Powered by FluxBB