Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: SHA256 or SHA512 hash of file


This question is answered.


Permlink Replies: 2 - Last Post: Nov 23, 2017 11:53 AM Last Post By: Sergii Bengus
Chris Nillissen

Posts: 60
Registered: 11/5/06
SHA256 or SHA512 hash of file  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 27, 2014 11:47 PM
Does anyone have any examples of how to do a SHA256 or SHA512 hash of a file in FireMonkey using the OpenSSL library?

Thanks
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: SHA256 or SHA512 hash of file
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2014 9:49 AM   in response to: Chris Nillissen in response to: Chris Nillissen
Chris wrote:

Does anyone have any examples of how to do a SHA256 or SHA512
hash of a file in FireMonkey using the OpenSSL library?

Indy has TIdHashSHA256 and TIdHashSHA512 classes in its IdHashSHA unit which
use OpenSSL internally. They have HashStream() and HashStreamAsHex() methods
that you can pass a TStream to, such as a TFileStream. For example:

uses
  ..., IdHash, IdHashSHA, IdSSLOpenSSL;
 
var
  FS: TFileStream;
  SHA: TIdHashSHA256;
  Hash: TIdBytes;
begin
  LoadOpenSSLLibrary;
  if TIdHashSHA256.IsAvailable then
  begin
    FS := TFileStream.Create(..., fmOpenRead);
    try
      SHA := TIdHashSHA256.Create;
      try
        Hash := SHA.HashStream(FS);
      finally
        SHA.Free;
      end;
    finally
      FS.Free;
    end;
  end;
end;


uses
  ..., IdHash, IdHashSHA, IdSSLOpenSSL;
 
var
  FS: TFileStream;
  SHA: TIdHashSHA256;
  Hash: String;
begin
  LoadOpenSSLLibrary;
  if TIdHashSHA256.IsAvailable then
  begin
    FS := TFileStream.Create(..., fmOpenRead);
    try
      SHA := TIdHashSHA256.Create;
      try
        Hash := SHA.HashStreamAsHex(FS);
      finally
        SHA.Free;
      end;
    finally
      FS.Free;
    end;
  end;
end;


--
Remy Lebeau (TeamB)
Sergii Bengus

Posts: 1
Registered: 12/6/17
Re: SHA256 or SHA512 hash of file  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 6, 2017 8:14 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
please help.,
I faced with the hash problem using a secret key: data encrypted with method HMAC-SHA512 using secret key. How to involve the key to the hash?
Thank you.

=> during the time interval from "Posted: Nov 6, 2017 8:14 AM" to "Registered: 12/6/17" one solution for Windows has been found.

unit UnitName;
 
interface
 
uses
  Winapi.Windows, System.SysUtils, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.NetEncoding,
  IdSSLOpenSSL, IdHashSHA, IdGlobal;
 
type
  TForm1 = class(TForm) //three components added to the test-form
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    procedure Button1Click(Sender: TObject);
  private     { Private declarations }
  public      { Public declarations }
  end;
 
  TNewThread = class(TThread)
    procedure Execute; override;
  end;
 
var
  Form1: TForm1;
 
implementation
{$R *.dfm}
var
  HS: string;
  SHA: TIdHashSHA512;
 
const
  skey='S-4071c3f3a16b18a72853ae64b911c5942721a79a';
  DataToHash='text_to_be_hashed';
 
function EncryptHMACSha512(Input, AKey: string): TIdBytes;
var  key: TIdBytes;
begin
   SHA:= TIdHashSHA512.Create; // 'if SHA.IsAvailable then' ?
   with SHA do
   try
     Key := ToBytes(AKey);
     Result := SHA.HashBytes(ToBytes(Input));
   finally
     Free;
   end;
  end;
 
procedure TNewThread.Execute;
begin
      LoadOpenSSLLibrary; //we should load external HMACSHA512 functionality from libraries.
                          //for 32(64)-bit application -> 32(64)-bit libraries libeay32.dll, ssleay32.dll -
                          // - they should be in the same directory where your project is located
      HS:=LowerCase(ToHex(EncryptHMACSha512(DataToHash, skey)));
      Form1.Memo1.Lines.Clear;
      Form1.Memo1.Text:= skey;
      Form1.Memo2.Lines.Clear;
      Form1.Memo2.Text:= HS;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
     TNewThread.Create(False);
end;
 
end.


Regards,
Sergii.

Edited by: Sergii Bengus on Nov 23, 2017 5:07 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02