#1 2019-04-25 12:46:02

MC
Member
Registered: 2012-10-04
Posts: 21

SHA256 of an array

Hello,

I am a little confused about this simple program that calculates the SHA256 of an array of Byte.
The line marked "<=== PB here" does not compile if I write:

vSHA256Static: = SHA256(pointer(vStatic), MAX_T);

The line

vSHA256Static: = SHA256(Pointer(vStatic [0]), MAX_T);

compile but I get an access violation at execution.
The calculation of SHA256 with the dynamic array works perfectly.
I do not understand why.
Thank you for your help

P.S. : I am using Delphi 7 Pro

const
  MAX_T = 22400;
var
  vStatic  : array[0..MAX_T-1] of Byte;
  vDynamic : array of Byte;
  vFS      : TFileStream;
  vSHA256Static : AnsiString;
  vSHA256Dynamic : AnsiString;
begin
  SetLength(vDynamic, MAX_T);
  vFS := TFileStream.Create(---some file greater then MAX_T---, fmOpenRead);
  vFS.Position := 0;
  vFS.ReadBuffer(vStatic[0], MAX_T);
  vFS.Position := 0;
  vFS.ReadBuffer(vDynamic[0], MAX_T);
  FreeAndNil(vFS);
  vSHA256Static := SynCrypto.SHA256(Pointer(vStatic[0]), MAX_T);  // <==== PB here
  vSHA256Dynamic := SynCrypto.SHA256(Pointer(vDynamic), MAX_T);
  ShowMessage
  (
    'Static'#9 + vSHA256Static + #10
  + 'Dynamic'#9 + vSHA256Dynamic
  );
end;

Last edited by MC (2019-04-25 12:47:12)

Offline

#2 2019-04-26 07:41:28

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

Re: SHA256 of an array

Your data is on the stack: the vStatic is not a pointer (like with a dynamic array or a string), but real data.

So you need to write

vSHA256Static: = SHA256(@vStatic, MAX_T);

Offline

#3 2019-04-26 07:49:03

MC
Member
Registered: 2012-10-04
Posts: 21

Re: SHA256 of an array

@ab Thank you, I understand now !

Offline

Board footer

Powered by FluxBB