Jump to content
Sign in to follow this  
azrael_11

Totally new to this area

Recommended Posts

Hello

 

I download an image from the internet and the author gives me the

sha1, md5 and crc or the image to check if the download image is correct.

 

1. Can i take somehow the sha1 or md5 or crc of the downloaded image with delphi.

2. Can i do this with Turbo LockBox components. And if i can a simple example how to do this or a web site to explain.

3. Can i add different sha1 or md5 or crc in the same image

 

Thank you

Share this post


Link to post
11 minutes ago, azrael_11 said:

3. Can i add different sha1 or md5 or crc in the same image

Hashes and Checksums are calculated over that file, it aint 'branded/marked'.

The only way is to modify image to get a modified result from one of those methods (sha1/md5/crc)

You can not define such.

Edited by KodeZwerg
  • Like 1
  • Thanks 1

Share this post


Link to post
23 minutes ago, azrael_11 said:

Can i take somehow the sha1 or md5 or crc of the downloaded image with delphi.

Yes, but it easier just to use a command line tool to do this. 

 

23 minutes ago, azrael_11 said:

Can i do this with Turbo LockBox components. And if i can a simple example how to do this or a web site to explain.

You don't need any components. Delphi comes with a library of hash functions. 

 

24 minutes ago, azrael_11 said:

Can i add different sha1 or md5 or crc in the same image

That doesn't make much sense. A hash function is a function that maps an arbitrary length array of bytes to a fixed length array of bytes. Your image file is the input to the hash function. Multiple different inputs can yield the same has for fairly obvious reasons. Reading about hash functions would be useful for you. 

  • Like 1

Share this post


Link to post

Here small code for Delphi to get hashes.

 

 

function GetFileHashMD5(FileName: WideString): String;
var
  HashMD5: THashMD5;
  Stream: TStream; 
  Readed: Integer; 
  Buffer: PByte; 
  BufLen: Integer; 
begin
  HashMD5 := THashMD5.Create; 
  BufLen := 16 * 1024; 
  Buffer := AllocMem(BufLen); 
  try 
    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); 
      try
        while Stream.Position < Stream.Size do 
          begin 
            Readed := Stream.Read(Buffer^, BufLen); 
            if Readed > 0 then 
              begin 
                HashMD5.update(Buffer^, Readed); 
              end; 
          end; 
      finally 
        Stream.Free; 
      end; 
  finally 
    FreeMem(Buffer) 
  end; 
  Result := HashMD5.HashAsString; 
end;   

function GetFileHashSHA1(FileName: WideString): String; 
var 
  HashSHA: THashSHA1; 
  Stream: TStream; 
  Readed: Integer; 
  Buffer: PByte; 
  BufLen: Integer; 
begin 
  HashSHA := THashSHA1.Create; 
  BufLen := 16 * 1024; 
  Buffer := AllocMem(BufLen); 
  try 
    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); 
    try 
      while Stream.Position < Stream.Size do
        begin 
          Readed := Stream.Read(Buffer^, BufLen);
          if Readed > 0 then 
            begin 
              HashSHA.update(Buffer^, Readed);
            end; 
        end; 
    finally 
      Stream.Free; 
    end; 
  finally 
    FreeMem(Buffer) 
  end; 
  Result := HashSHA.HashAsString; 
end;

Uses Sytem.Hash   <<- insert that into your uses clause

 

 

Maybe that code help you. Taken from Embarcadero Examples.

 

Ps. FMX might be wrong for this topic 🙂

Edited by KodeZwerg
  • Like 1

Share this post


Link to post
30 minutes ago, KodeZwerg said:

Here small code for Delphi to get hashes.

 

 


function GetFileHashMD5(FileName: WideString): String;
var
  HashMD5: THashMD5;
  Stream: TStream; 
  Readed: Integer; 
  Buffer: PByte; 
  BufLen: Integer; 
begin
  HashMD5 := THashMD5.Create; 
  BufLen := 16 * 1024; 
  Buffer := AllocMem(BufLen); 
  try 
    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); 
      try
        while Stream.Position < Stream.Size do 
          begin 
            Readed := Stream.Read(Buffer^, BufLen); 
            if Readed > 0 then 
              begin 
                HashMD5.update(Buffer^, Readed); 
              end; 
          end; 
      finally 
        Stream.Free; 
      end; 
  finally 
    FreeMem(Buffer) 
  end; 
  Result := HashMD5.HashAsString; 
end;   

function GetFileHashSHA1(FileName: WideString): String; 
var 
  HashSHA: THashSHA1; 
  Stream: TStream; 
  Readed: Integer; 
  Buffer: PByte; 
  BufLen: Integer; 
begin 
  HashSHA := THashSHA1.Create; 
  BufLen := 16 * 1024; 
  Buffer := AllocMem(BufLen); 
  try 
    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite); 
    try 
      while Stream.Position < Stream.Size do
        begin 
          Readed := Stream.Read(Buffer^, BufLen);
          if Readed > 0 then 
            begin 
              HashSHA.update(Buffer^, Readed);
            end; 
        end; 
    finally 
      Stream.Free; 
    end; 
  finally 
    FreeMem(Buffer) 
  end; 
  Result := HashSHA.HashAsString; 
end;

Uses Sytem.Hash   <<- insert that into your uses clause

 

 

Maybe that code help you. Taken from Embarcadero Examples.

 

Ps. FMX might be wrong for this topic 🙂

This code can fall into an in infinite loop if Stream.Read fails

Share this post


Link to post

Thanks @David Heffernan for pointing that out.

 

Heres a quick and dirty fix.

 

It is limited by memory and fail if dont fit in RAM.

 

 

Uses System.SysUtils, System.IOUtils, System.Hash;

function HashFileSHA1(const FileName: string): string;
var
  HashSHA1: THashSHA1;
  Memory: TBytes;
begin
  Result := '';
  if FileExists(FileName) then
    begin
      Memory := TFile.ReadAllBytes(FileName);
      HashSHA1 := THashSHA1.Create;
      HashSHA1.Update(Memory);
      Result := HashSHA1.HashAsString;
    end;
end;
Edited by KodeZwerg
  • Thanks 1

Share this post


Link to post

Thank you very much for all the help.

If you have and html sites to read about md5 or sha1 or crc i appreciate.

Share this post


Link to post
14 minutes ago, azrael_11 said:

Thank you very much for all the help.

If you have and html sites to read about md5 or sha1 or crc i appreciate.

Use websearch. That's all we would do. 

  • Thanks 1

Share this post


Link to post
1 hour ago, KodeZwerg said:

Thanks @David Heffernan for pointing that out.

 

Heres a quick and dirty fix.

 

It is limited by memory and fail if dont fit in RAM.

 

 


Uses System.SysUtils, System.IOUtils, System.Hash;

function HashFileSHA1(const FileName: string): string;
var
  HashSHA1: THashSHA1;
  Memory: TBytes;
begin
  Result := '';
  if FileExists(FileName) then
    begin
      Memory := TFile.ReadAllBytes(FileName);
      HashSHA1 := THashSHA1.Create;
      HashSHA1.Update(Memory);
      Result := HashSHA1.HashAsString;
    end;
end;

It's better to fix the original code. Either by using ReadBuffer, or by comparing Read return value against the number of bytes requested. 

 

Is this really Embarcadero code? Hard to believe they'd write Readed! 

Edited by David Heffernan

Share this post


Link to post
53 minutes ago, David Heffernan said:

It's better to fix the original code. Either by using ReadBuffer, or by comparing Read return value against the number of bytes requested. 

 

Is this really Embarcadero code? Hard to believe they'd write Readed! 

I lost reference of source, it was in my file archives inside embarcadero folder.

After investigating my last source (i was aware of memory limit), i do trust what delphi is doing without going into every little bit of code and come to a new conclusion that all should make happy, hopefully.

 

 

function GetSHA1(const FileName: string): string;
begin
  Result := '';
  if not FileExists(FileName) then
    Exit;
  Result := THashSHA1.GetHashStringFromFile(FileName);
end;

function GetMD5(const FileName: string): string;
begin
  Result := '';
  if not FileExists(FileName) then
    Exit;
  Result := THashMD5.GetHashStringFromFile(FileName);
end;

 

Sorry, no fix for my first example will be made by me. I prefer doing on my own.

 

(You still need 'uses System.Hash' to work)

Edited by KodeZwerg

Share this post


Link to post
1 hour ago, azrael_11 said:

Thank you very much for all the help.

If you have and html sites to read about md5 or sha1 or crc i appreciate.

Wikipedia look under 'Computing' to getting started.

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×