Recent

Author Topic: HashLib4Pascal  (Read 20594 times)

ASBzone

  • Sr. Member
  • ****
  • Posts: 261
  • 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.5 r62023 / FPC v3.2.0-beta-r43192 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1903 (Build 18362.476)
Other Systems: Windows 10 Pro x64, Version 1903 or greater

ASBzone

  • Sr. Member
  • ****
  • Posts: 261
  • 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.5 r62023 / FPC v3.2.0-beta-r43192 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1903 (Build 18362.476)
Other Systems: Windows 10 Pro x64, Version 1903 or greater

Xor-el

  • Sr. Member
  • ****
  • Posts: 382
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

  • Sr. Member
  • ****
  • Posts: 261
  • 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.5 r62023 / FPC v3.2.0-beta-r43192 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1903 (Build 18362.476)
Other Systems: Windows 10 Pro x64, Version 1903 or greater

Xor-el

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

Xor-el

  • Sr. Member
  • ****
  • Posts: 382
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: 382
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: 12
Re: HashLib4Pascal
« Reply #37 on: December 19, 2018, 11:39:22 pm »
Thank You  :)

ASBzone

  • Sr. Member
  • ****
  • Posts: 261
  • 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.5 r62023 / FPC v3.2.0-beta-r43192 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1903 (Build 18362.476)
Other Systems: Windows 10 Pro x64, Version 1903 or greater

Xor-el

  • Sr. Member
  • ****
  • Posts: 382
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

** Now updated in OPM
« Last Edit: March 28, 2019, 02:59:42 pm by Xor-el »

totya

  • Hero Member
  • *****
  • Posts: 577
Re: HashLib4Pascal
« Reply #40 on: May 26, 2019, 11:47:53 pm »
Hi!

Thanks for this great library!

My question is, what is the default blocksize for TBlake2B?

And... I want 32 byte blocksize for TBlake2B (64bit) like as TBlake2S, so:

Code: Pascal  [Select]
  1.     {$IFDEF WIN32}
  2.     Hash := TBlake2S.Create();
  3.     {$ENDIF}
  4.     {$IFDEF WIN64}
  5.     Hash := TBlake2B.Create(32, 65535);
  6.     {$ENDIF}
  7.  

I got error after run: "config."

Xor-el

  • Sr. Member
  • ****
  • Posts: 382
Re: HashLib4Pascal
« Reply #41 on: May 27, 2019, 09:14:39 am »
Hi!

Thanks for this great library!

My question is, what is the default blocksize for TBlake2B?

And... I want 32 byte blocksize for TBlake2B (64bit) like as TBlake2S, so:

Code: Pascal  [Select]
  1.     {$IFDEF WIN32}
  2.     Hash := TBlake2S.Create();
  3.     {$ENDIF}
  4.     {$IFDEF WIN64}
  5.     Hash := TBlake2B.Create(32, 65535);
  6.     {$ENDIF}
  7.  

I got error after run: "config."

1. I guess what you mean is HashSize (which is probably what you mean). Default HashSize for Blake2B is 512bit (64 bytes)

2. With respect to HashSize, this is what you need

https://github.com/Xor-el/HashLib4Pascal/blob/master/HashLib/src/Base/HlpHashFactory.pas#L386

totya

  • Hero Member
  • *****
  • Posts: 577
Re: HashLib4Pascal
« Reply #42 on: May 27, 2019, 08:00:27 pm »
1. I guess what you mean is HashSize (which is probably what you mean). Default HashSize for Blake2B is 512bit (64 bytes)

2. With respect to HashSize, this is what you need

https://github.com/Xor-el/HashLib4Pascal/blob/master/HashLib/src/Base/HlpHashFactory.pas#L386

Hi!

Thank you for the answer!

You are guess right, I'm sorry. But the problem is stay, because the last line hint for Hash := TBlake2B.Create() this:
Quote
Create(a_hash_size, a_block_size:int32)
So, if I set a_hash_size and  a_block_size why the result is: "config. exception"? Something wrong with it.

Second, thanks for this tip:
Code: Pascal  [Select]
  1. class function CreateBlake2B_256(): IHash; static;

Seems to me I can use it in this way:
Code: Pascal  [Select]
  1. {$IFDEF WIN64}
  2.   Hash := THashFactory.TCrypto.CreateBlake2B_256();
  3. {$ENDIF}
  4.  

Third, I read the svn changelog, tree hashing for blake2 is supported. I think this it is about speed up on multi-cores systems. How can I use it/force it, or works automatically?

Thank you, and sorry for my bad english.

Xor-el

  • Sr. Member
  • ****
  • Posts: 382
Re: HashLib4Pascal
« Reply #43 on: May 27, 2019, 08:15:30 pm »
What are you trying to achieve in question 1?

Tree Hashing in Blake has nothing to speed up.
The speed up I think you are referring to is Blake Parallel Hashing found in Blake2bp and Blake2sp which HashLib4Pascal doesn't implement at the moment.

totya

  • Hero Member
  • *****
  • Posts: 577
Re: HashLib4Pascal
« Reply #44 on: May 27, 2019, 08:29:23 pm »
What are you trying to achieve in question 1?

Trying? I'm done... with this code
Code: Pascal  [Select]
  1. THashFactory.TCrypto.CreateBlake2B_256();

It's simple, my log memo show many hash value, and with default size of log memo, the 64 byte hash result is very long, and wrapped in the window, it's ugly, thats all. ;)

I choose between of two methods, because 2b is faster on 64bit, 2s is faster under 64 bit (source: docs of Blake2). I don't test these speeds with your implementation, but I guess it's true here too.