Recent

Author Topic: SHA-256 calculation  (Read 17536 times)

d3n1s

  • Newbie
  • Posts: 3
SHA-256 calculation
« on: April 16, 2017, 06:03:15 pm »
Hi!
Please tell how to calculate SHA-256 of String like '0AF3D2E1' in free pascal.
I need a simple way to use any 'function(S: String): String'

Bart

  • Hero Member
  • *****
  • Posts: 5288
    • Bart en Mariska's Webstek
Re: SHA-256 calculation
« Reply #1 on: April 16, 2017, 06:24:43 pm »

d3n1s

  • Newbie
  • Posts: 3
Re: SHA-256 calculation
« Reply #2 on: April 16, 2017, 06:33:31 pm »
I try to calculate SHA-256 with DCPCrypt, but I don't understand how to do it in a simple way.
Please tell me if you know how to use DCPCrypt

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: SHA-256 calculation
« Reply #3 on: April 16, 2017, 07:17:02 pm »
« Last Edit: April 16, 2017, 07:23:03 pm by Zoran »

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: SHA-256 calculation
« Reply #4 on: April 16, 2017, 07:40:43 pm »
Greetings my friends, this is my contribution  :)
Get SHA256 of string.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, DCPsha256, DCPripemd160, Forms, Controls,
  9.   Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     DCP_sha256_1: TDCP_sha256;
  18.     Edit1: TEdit;
  19.     Edit2: TEdit;
  20.     Label1: TLabel;
  21.     procedure Button1Click(Sender: TObject);
  22.   private
  23.     { private declarations }
  24.   public
  25.  
  26.     { public declarations }
  27.   end;
  28.  
  29. var
  30.   Form1: TForm1;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. function getsha256(S: String): String;
  39. var
  40.     Hash: TDCP_sha256;
  41.     Digest: array[0..31] of byte;  // sha256 produces a 256bit digest (32bytes)
  42.     Source: string;
  43.     i: integer;
  44.     str1: string;
  45.   begin
  46.     Source:= S;  // here your string for get sha256
  47.  
  48.     if Source <> '' then
  49.     begin
  50.       Hash:= TDCP_sha256.Create(nil);  // create the hash
  51.       Hash.Init;                        // initialize it
  52.       Hash.UpdateStr(Source);
  53.       Hash.Final(Digest);               // produce the digest
  54.       str1:= '';
  55.       for i:= 0 to 31 do
  56.         str1:= str1 + IntToHex(Digest[i],2);
  57.       //form1.Edit2.Text:= s;                   // display the digest in lower case
  58.       form1.Edit2.Text:=UpperCase(str1);         // display the digest in capital letter
  59.  
  60.     end;
  61.   end;
  62.  
  63. procedure TForm1.Button1Click(Sender: TObject);
  64. begin
  65.      getsha256(Edit1.Text);  // show the sha256 of string in edit1
  66. end;
  67.  
  68. end.

for now work fine  :) :)
PD: Attached image and example

« Last Edit: April 16, 2017, 08:11:04 pm by Ericktux »

Ericktux

  • Sr. Member
  • ****
  • Posts: 345
Re: SHA-256 calculation
« Reply #5 on: April 16, 2017, 08:09:22 pm »
For SHA256 of files you can try this example  :)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, DCPsha256, DCPripemd160, Forms, Controls,
  9.   Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     DCP_sha256_1: TDCP_sha256;
  19.     Edit1: TEdit;
  20.     Edit2: TEdit;
  21.     Label1: TLabel;
  22.     OpenDialog1: TOpenDialog;
  23.     procedure Button1Click(Sender: TObject);
  24.     procedure Button2Click(Sender: TObject);
  25.   private
  26.     { private declarations }
  27.   public
  28.  
  29.     { public declarations }
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. function getsha256_file(S: String): String;   // "S" is path of file
  42. var
  43.     Hash: TDCP_sha256;
  44.     Digest: array[0..31] of byte;  // sha256 produces a 256bit digest (32bytes)
  45.     Source: TFileStream;
  46.     i: integer;
  47.     str1: string;
  48.   begin
  49.     Source:= nil;
  50.     try
  51.       Source:= TFileStream.Create(s,fmOpenRead);  // open the file specified by Edit1
  52.     except
  53.       MessageDlg('Unable to open file',mtError,[mbOK],0);
  54.     end;
  55.  
  56.     if Source <> nil then
  57.     begin
  58.       Hash:= TDCP_sha256.Create(nil);  // create the hash
  59.       Hash.Init;                        // initialize it
  60.       Hash.UpdateStream(Source, Source.Size);
  61.       Hash.Final(Digest);               // produce the digest
  62.       Source.Free;
  63.       str1:= '';
  64.       for i:= 0 to 31 do
  65.         str1:= str1 + IntToHex(Digest[i],2);
  66.       //form1.Edit2.Text:= s;                   // display the digest in lower case
  67.       form1.Edit2.Text:=UpperCase(str1);         // display the digest in capital letter
  68.  
  69.     end;
  70.   end;
  71.  
  72. procedure TForm1.Button1Click(Sender: TObject);
  73. begin
  74.      getsha256_file(Edit1.Text);  // show the sha256 of file in edit1
  75. end;
  76.  
  77. procedure TForm1.Button2Click(Sender: TObject);
  78. begin
  79.     if OpenDialog1.Execute then
  80.     begin
  81.     Edit1.Text:=OpenDialog1.Filename;
  82.     end;
  83. end;
  84.  
  85. end.


Regards :) :)


« Last Edit: April 16, 2017, 08:11:42 pm by Ericktux »

d3n1s

  • Newbie
  • Posts: 3
Re: SHA-256 calculation
« Reply #6 on: April 16, 2017, 09:35:05 pm »
Thank you!!! It's working!

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: SHA-256 calculation
« Reply #7 on: April 17, 2017, 11:54:32 pm »
I've a lot of experience using DCPCrypt, and it is very good. But it's worth noting I recently migrated my data hashing project to HasLib4Pascal (https://github.com/Xor-el/HashLib4Pascal), which is preferable for my needs.

To compute for a string, it is as simple as :

 result := THashFactory.TCrypto.CreateSHA2_256().ComputeString(strToBeHashed, TEncoding.UTF8).ToString();
(https://github.com/tedsmith/quickhash/blob/master/unit2.pas#L2223)

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
Re: SHA-256 calculation
« Reply #8 on: March 21, 2018, 01:00:55 pm »
I also appreciate the HashLib4Pascal library -- it is very effective.

-ASB
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

 

TinyPortal © 2005-2018