Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2011-03

SHA1 of a TMemoryStream

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jorge Aldo G. de F. Junior

I need to perform a Hash of data stored at a TMemoryStream...

But unfortunately, the following program spits out different results if i run it in sucession... Note that the file being read (test6.pas) never changes.

Uses
Classes,

	SHA1;
Var
	Data : TMemoryStream;
	Hash : TSHA1Digest;
Begin
	Data := TMemoryStream.Create;
	Data.LoadFromFile('test6.pas');
	Hash := SHA1Buffer(Data.Memory, Data.Size);
	WriteLn(SHA1Print(Hash));
	Data.Free;
End.

Am i doing (probably) anything wrong ?

Thanks in advance.

Reply by email

Marco van de Voort
to Jorge Aldo G. de F. Junior

In our previous episode, Jorge Aldo G. de F. Junior said:

Show quoted text 11 lines
Uses Classes, SHA1; Var Data : TMemoryStream; Hash : TSHA1Digest; Begin Data := TMemoryStream.Create; Data.LoadFromFile('test6.pas');
Data.Position:=0;
Hash := SHA1Buffer(Data.Memory, Data.Size); WriteLn(SHA1Print(Hash)); Data.Free; End.

Reply by email

Jorge Aldo G. de F. Junior
to Marco van de Voort

Found the solution,

untyped var parameters expects a real variable so it "internally" generates a pointer out of the address of the variable.

SHA1Buffer uses an untyped parameter, so, passing a pointer to it makes it try to hash the pointer itself (and the rest of the memory positioned exactly besides it, until data.size is meet).

The solution was to dereference the pointer :

SHA1Buffer(Data.Memory^, Data.Size); // damm little ^ !

Thanks !!

2011/3/5 Marco van de Voort :

Show quoted text 25 lines
In our previous episode, Jorge Aldo G. de F. Junior said: > Uses >       Classes, >       SHA1; > > Var >       Data : TMemoryStream; >       Hash : TSHA1Digest; > > Begin >       Data := TMemoryStream.Create; >       Data.LoadFromFile('test6.pas'); Data.Position:=0; >       Hash := SHA1Buffer(Data.Memory, Data.Size); >       WriteLn(SHA1Print(Hash)); >       Data.Free; > End. _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email