Recent

Author Topic: HashLib4Pascal  (Read 15435 times)

ASBzone

  • Full Member
  • ***
  • Posts: 150
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
Re: HashLib4Pascal
« Reply #30 on: August 13, 2018, 11:53:49 pm »
yes it is very possible but why do you want to complicate things for yourself?

I can see why you did it the way you did, but for me, it doesn't seem complicated at all to add just the groupings or families that I need at any given time.


if it's because of size, don't worry, smartlinking in FreePascal removes unused details in the final binary.

I expected Smartlinking to help in that way, but for this particular package, it hasn't played out that way, I'm afraid.


well to answer your question, you can do this.

Code: Pascal  [Select]
  1. uses
  2. SysUtils,
  3. HlpIHash,
  4. HlpMD5;
  5.  
  6. var
  7. MD5Hash: IHash;
  8. Value : TBytes;
  9. begin
  10.  
  11. MD5Hash := TMD5.Create();
  12. Value := MD5Hash.ComputeBytes(TBytes.Create($01, $02));
  13.  
  14. end.
  15.  

Much appreciated.  I will see how this works for me.
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.1 r60647 / FPC v3.2.0-beta-r41629 (via FpcUpDeluxe)
-- 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.195)
Other Systems: Windows 10 Pro x64, Version 1803 or greater

ASBzone

  • Full Member
  • ***
  • Posts: 150
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
Re: HashLib4Pascal
« Reply #31 on: August 14, 2018, 12:57:51 am »
Code: Pascal  [Select]
  1. uses
  2. SysUtils,
  3. HlpIHash,
  4. HlpMD5;
  5.  
  6. var
  7. MD5Hash: IHash;
  8. Value : TBytes;
  9. begin
  10.  
  11. MD5Hash := TMD5.Create();
  12. Value := MD5Hash.ComputeBytes(TBytes.Create($01, $02));
  13.  
  14. end.
  15.  


I get the following error for this:

Error: Incompatible types: got "IHashResult" expected "TBytes"

SomeString := MD5Hash.ComputeBytes(TBytes.Create($01,$02)).ToString();    // This doesn't generate any errors

SomeString := MD5Hash.ComputeFile(ThisFile).ToString();    // This results in an access violation.


The following, of course, works:
SomeString := LowerCase(THashFactory.TCrypto.CreateMD5().ComputeFile(ThisFile).ToString());

But I want to be able to target only select hash families, as stated earlier.
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.1 r60647 / FPC v3.2.0-beta-r41629 (via FpcUpDeluxe)
-- 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.195)
Other Systems: Windows 10 Pro x64, Version 1803 or greater

Xor-el

  • Sr. Member
  • ****
  • Posts: 257
Re: HashLib4Pascal
« Reply #32 on: August 14, 2018, 08:27:57 am »
You are right, my bad.
My previous example will not compile.
I wrote the code in my browser.

Regarding
Quote
SomeString := MD5Hash.ComputeFile(ThisFile).ToString();    // This results in an access violation.
Are you sure you created the MD5Hash instance before calling MD5Hash.ComputeFile?

Can you upload your sample code that throws the access violation?
by the way, what version of Lazarus/FPC are you using?
« Last Edit: August 14, 2018, 09:01:27 am by Xor-el »

ASBzone

  • Full Member
  • ***
  • Posts: 150
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
Re: HashLib4Pascal
« Reply #33 on: August 14, 2018, 10:02:40 pm »
You are right, my bad.
My previous example will not compile.

No worries.  I was able to get around it.


Are you sure you created the MD5Hash instance before calling MD5Hash.ComputeFile?

Yes, I did.  It turns out that I was using the wrong file variable.  In the process of making a smaller sample to upload, I realized my error.   The sample, of course, works.

Admittedly, had I done the smaller sample from the beginning, this would have been fine.


Can you upload your sample code that throws the access violation?
by the way, what version of Lazarus/FPC are you using?

I'm using 32-bit Lazarus 1.8.4 and FPC 3.0.4 on Windows 10 x64

And here's the now fully working code.  :)

Code: Pascal  [Select]
  1.  
  2. {$MODE objfpc}{$LONGSTRINGS ON}
  3. {$APPTYPE CONSOLE}
  4. {$R *.res}
  5.  
  6. Program GenerateHashes;
  7.  
  8. Uses
  9.         SysUtils, StrUtils, HlpIHash, HlpMD5, HlpSHA1, HlpSHA2_256, HlpSHA3;
  10.  
  11.  
  12. Const
  13.         TestText = 'This is some test text';
  14.    MD5TextResult = 'c93037c9c2d7be6d6ad51a0e694fad5f';
  15.    SHA1TextResult = '510ec59b0eac4a84dab06d910d7f69445a2d54c6';
  16.    SHA2TextResult = 'f4544fb4c5af57bf6a823fbb623de44363cb3384928ca95360baede7b2e578c5';
  17.    SHA3TextResult = 'd3baea49d0668c3c0d20c9ff64cc97a5947db1333d9e8c53e57c8b283557c15b458f5923fe6ef9b1704c889f0edf0ba9';
  18.  
  19.         TestFile = 'C:\Temp\TestFile.TXT';
  20.    MD5FileResult = '5fa5f3ef591f54557564da635b169ad7';
  21.    SHA1FileResult = 'f2f739fb7c1a755563520d818abf6c892295e292';
  22.    SHA2FileResult = 'ef76f11b7e364532fac399bcf97c63ecff4b3697cf693b0ee2022011e6ba79cb';
  23.    SHA3FileResult = '2514447bfca1950b8718f3d55dba830fee11da94d9af315725b06763c27c099dea4ddee417c2ddcbeaf00e72d14b82a0';
  24.  
  25.  
  26. Var
  27.    MD5Hash, SHA1Hash, SHA2Hash, SHA3Hash : IHash;
  28.    ThisHash : String;
  29.  
  30.  
  31. Begin
  32.    WriteLn('Calculating hash for following file: ', TestFile);
  33.    MD5Hash  := TMD5.Create();
  34.    ThisHash := MD5Hash.ComputeFile(TestFile).ToString();
  35.    WriteLn('MD5 Hash (Test) ........... ', ThisHash);
  36.    WriteLn('MD5 Hash (Real) ........... ', MD5FileResult);
  37.    WriteLn;
  38.  
  39.    SHA1Hash := TSHA1.Create();
  40.    ThisHash := SHA1Hash.ComputeFile(TestFile).ToString();
  41.    WriteLn('SHA1 Hash (Test) .......... ', ThisHash);
  42.    WriteLn('SHA1 Hash (Real) .......... ', SHA1FileResult);
  43.    WriteLn;
  44.  
  45.    SHA2Hash := TSHA2_256.Create();
  46.    ThisHash := SHA2Hash.ComputeFile(TestFile).ToString();
  47.    WriteLn('SHA2(256) Hash (Test) ..... ', ThisHash);
  48.    WriteLn('SHA2(256) Hash (Real) ..... ', SHA2FileResult);
  49.    WriteLn;
  50.  
  51.    SHA3Hash := TSHA3_384.Create();
  52.    ThisHash := SHA3Hash.ComputeFile(TestFile).ToString();
  53.    WriteLn('SHA3(384) Hash (Test) ..... ', ThisHash);
  54.    WriteLn('SHA3(384) Hash (Real) ..... ', SHA3FileResult);
  55.    WriteLn;
  56.  
  57.    WriteLn;
  58.    WriteLn('Calculating hash for following text: ', TestText);
  59.    MD5Hash  := TMD5.Create();
  60.    ThisHash := MD5Hash.ComputeString(TestText, TEncoding.UTF8).ToString();
  61.    WriteLn('MD5 Hash (Test) ........... ', ThisHash);
  62.    WriteLn('MD5 Hash (Real) ........... ', MD5TextResult);
  63.    WriteLn;
  64.  
  65.    SHA1Hash := TSHA1.Create();
  66.    ThisHash := SHA1Hash.ComputeString(TestText, TEncoding.UTF8).ToString();
  67.    WriteLn('SHA1 Hash (Test) .......... ', ThisHash);
  68.    WriteLn('SHA1 Hash (Real) .......... ', SHA1TextResult);
  69.    WriteLn;
  70.  
  71.    SHA2Hash := TSHA2_256.Create();
  72.    ThisHash := SHA2Hash.ComputeString(TestText, TEncoding.UTF8).ToString();
  73.    WriteLn('SHA2(256) Hash (Test) ..... ', ThisHash);
  74.    WriteLn('SHA2(256) Hash (Real) ..... ', SHA2TextResult);
  75.    WriteLn;
  76.  
  77.    SHA3Hash := TSHA3_384.Create();
  78.    ThisHash := SHA3Hash.ComputeString(TestText, TEncoding.UTF8).ToString();
  79.    WriteLn('SHA3(384) Hash (Test) ..... ', ThisHash);
  80.    WriteLn('SHA3(384) Hash (Real) ..... ', SHA3TextResult);
  81.    WriteLn;
  82. End.
  83.  
  84.  

Thanks again for your assistance.   Very much appreciated.
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.1 r60647 / FPC v3.2.0-beta-r41629 (via FpcUpDeluxe)
-- 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.195)
Other Systems: Windows 10 Pro x64, Version 1803 or greater

Xor-el

  • Sr. Member
  • ****
  • Posts: 257
Re: HashLib4Pascal
« Reply #34 on: August 14, 2018, 10:46:25 pm »
you are welcome.  ;)

Xor-el

  • Sr. Member
  • ****
  • Posts: 257
Re: HashLib4Pascal
« Reply #35 on: September 08, 2018, 10:15:38 am »
Just released v2.8 of this Library.

https://github.com/Xor-el/HashLib4Pascal

New Important Feature

Added the ability to persist the internal state of a hash object via the .Clone() method.
this is useful when computing hashes of large data sets which share the same initial subdata.
similar to the python hashlib.copy method
https://docs.python.org/3/library/hashlib.html#hashlib.hash.copy
Return a copy (“clone”) of the hash object. This can be used to efficiently compute the digests of data that share a common initial subdata.

Regards.

Xor-el

  • Sr. Member
  • ****
  • Posts: 257
Re: HashLib4Pascal
« Reply #36 on: December 16, 2018, 10:19:15 pm »
Just released v3.0 of this Library.

https://github.com/Xor-el/HashLib4Pascal

Added support for Shake128 and Shake256 XOF.

Regards.

gorkamorka

  • New member
  • *
  • Posts: 10
Re: HashLib4Pascal
« Reply #37 on: December 19, 2018, 11:39:22 pm »
Thank You  :)

ASBzone

  • Full Member
  • ***
  • Posts: 150
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
Re: HashLib4Pascal
« Reply #38 on: December 20, 2018, 01:02:28 am »
Just released v3.0 of this Library.

https://github.com/Xor-el/HashLib4Pascal

Added support for Shake128 and Shake256 XOF.

Regards.

Thanks for the update, @Xor-el
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.1 r60647 / FPC v3.2.0-beta-r41629 (via FpcUpDeluxe)
-- 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.195)
Other Systems: Windows 10 Pro x64, Version 1803 or greater

Xor-el

  • Sr. Member
  • ****
  • Posts: 257
Re: HashLib4Pascal
« Reply #39 on: March 15, 2019, 07:47:24 am »
Just added support for Argon2 KDF (2i, 2d and 2id) in version 3.4

Commit link

https://github.com/Xor-el/HashLib4Pascal/commit/fabd0a57bf6ef3665a4e0f753fe724d393dc17a4

** Not yet updated in OPM