Recent

Author Topic: HashLib4Pascal  (Read 34027 times)

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
HashLib4Pascal
« on: July 30, 2016, 04:13:53 pm »

HashLib4Pascal is a Delphi/FreePascal compatible library that provides an easy to use interface for computing hashes and checksums of strings (with a specified encoding), files, streams, byte arrays and untyped data to mention but a few. It also supports Incremental Hashing.

Supported Algorithms:

non-cryptographic 32-bits hash algorithms: AP, BKDR, Bernstein, Bernstein1, DEK, DJB,
ELF, FNV, FNV1a, JS, Jenkins3, Murmur2, MurmurHash3_x86_32, OneAtTime, PJW, RS,
Rotating, SDBM, ShiftAndXor, SuperFast, XXHash32.

non-cryptographic 64-bits algorithms: FNV, FNV1a, Murmur2_64, SipHash2_4, XXHash64.

non-cryptographic 128-bits algorithms: MurmurHash3_x86_128, MurmurHash3_x64_128.

checksum algorithms: Adler32, All CRC Variants from CRC3 to CRC64.

cryptographic algorithms: GOST, Grindahl, HAS160, Haval, MD2, MD4, MD5, Panama,
RadioGatun, RIPEMD, RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320, SHA0, SHA1, SHA2-224,
SHA2-256, SHA2-384, SHA2-512, SHA2-512-224, SHA2-512-256, SHA3-224, SHA3-256, SHA3-384,
SHA3-512,Snefru128, Snefru256, Tiger, Tiger2, WhirlPool.

HMAC for any of the above.

PBKDF2_HMAC for any of the above.

Supported Compilers

FreePascal 3.0.0 and Above.

Delphi 2010 and Above.

Installing the Library.

Method One:

Use the Provided Packages in the "Packages" Folder.

Method Two:

Add the Library Path and Sub Path to your Project Search Path.

Usage Examples.

Coming Soon.
But in the mean time, you can poke around the sources and Unit Tests.

Unit Tests.

To Run Unit Tests,

For FPC 3.0.0 and above

Simply compile and run "HashLib.Tests" project in "FreePascal.Tests" Folder.

For Delphi 2010 and above

Method One (Using DUnit Test Runner)

 To Build and Run the Unit Tests For Delphi 10 Seattle (should be similar for
 other versions)

1). Open Project Options of Unit Test (HashLib.Tests) in "Delphi.Tests" Folder.

2). Change Target to All Configurations (Or "Base" In Older Delphi Versions.)

3). In Output directory add ".\$(Platform)\$(Config)" without the quotes.

4). In Search path add "$(BDS)\Source\DUnit\src" without the quotes.

5). In Unit output directory add "." without the quotes.

6). In Unit scope names (If Available), Delete "DUnitX" from the List.

Press Ok and save, then build and run.

Method Two (Using TestInsight) (Preferred).

1). Download and Install TestInsight.

2). Open Project Options of Unit Test (HashLib.Tests.TestInsight) in "Delphi.Tests"
    Folder.

3). Change Target to All Configurations (Or "Base" In Older Delphi Versions.)

4). In Unit scope names (If Available), Delete "DUnitX" from the List.

5). To Use TestInsight, right-click on the project, then select
    "Enable for TestInsight" or "TestInsight Project".
    Save Project then Build and Run Test Project through TestInsight.

License

This "Software" is Licensed Under MIT License (MIT) .


https://github.com/Xor-el/HashLib4Pascal
« Last Edit: July 30, 2016, 04:16:18 pm by Xor-el »

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: HashLib4Pascal
« Reply #1 on: July 30, 2016, 06:17:26 pm »
Thanks for sharing. Although I don't need it now, but I may use it someday.

AlexTP

  • Hero Member
  • *****
  • Posts: 2386
    • UVviewsoft
Re: HashLib4Pascal
« Reply #2 on: July 30, 2016, 08:28:41 pm »
Same wish. Pls make wiki page like this one http://wiki.freepascal.org/JvXPBar

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: HashLib4Pascal
« Reply #3 on: July 31, 2016, 03:48:25 am »
I made a start here: http://wiki.freepascal.org/HashLib4Pascal

Anyone can continue/correct any mistake.
« Last Edit: July 31, 2016, 03:52:20 am by kapibara »
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

totya

  • Hero Member
  • *****
  • Posts: 720
Re: HashLib4Pascal
« Reply #4 on: October 01, 2016, 01:25:51 pm »
Hi!

Thank you for this library! :) The freepascal examples is missing from this library, but after some time, I can create hash for a file, for example:

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   SysUtils,
  5.   HlpIHashInfo,
  6.   HlpConverters,
  7.   HlpHashFactory;
  8.  
  9. var
  10.   MyKey, MyResult: String;
  11.   LHMAC: IHMAC;
  12.  
  13. begin
  14.   LHMAC := THashFactory.THMAC.CreateHMAC(THashFactory.TCrypto.CreateSHA2_512);
  15.  
  16.   MyKey:='';
  17.   LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);
  18.  
  19.   MyResult:= LHMAC.ComputeFile('testfile.exe').ToString();
  20.  
  21.   WriteLn(MyResult);
  22.  
  23.   ReadLn();
  24.  
  25. end.
  26.  

But I have one problem, the computed SHA512 hash is different from created by the TotalCommander. Why are different these hash?

Thanks!

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: HashLib4Pascal
« Reply #5 on: October 01, 2016, 03:05:51 pm »
you are using the HMAC version and I assume that is not what you want.
for calculating a normal SHA512 hash, do this

program project1 ;

// posted from my phone so formatting might be bad.
//also attached a little demo

 uses
  SysUtils,
 HlpHashFactory;

 var
 MyResult: String;

 begin

 MyResult := THashFactory.TCrypto.CreateSHA2_512().ComputeFile( 'testfile.exe' ).ToString();
 WriteLn ( MyResult) ;

 ReadLn() ;

 end .
« Last Edit: October 01, 2016, 03:08:25 pm by Xor-el »

totya

  • Hero Member
  • *****
  • Posts: 720
Re: HashLib4Pascal
« Reply #6 on: October 01, 2016, 03:25:08 pm »
Hi!

Thanks for the quick answer!

I see what is the "HAMAC", this is the "Hash-based message authentication code".

Well, my question is, if the key is empty, see my last message:

Code: Pascal  [Select][+][-]
  1. MyKey:='';
  2. LHMAC.Key := TConverters.ConvertStringToBytes(MyKey, TEncoding.UTF8);;

then the result must be equal with the non-HAMC verison, don't you think?

It isn't a uneccessary question, because if the result is good, I can use same as code with two times:
1. if key is empty, then the result same as non-HAMAC version, comparable to other hash, like as TC created hash
2. if key is present, then result is the HAMAC version.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: HashLib4Pascal
« Reply #7 on: October 01, 2016, 03:34:18 pm »
HMAC has a totally different implementation so "key or no key", the results will be different because of the okeypad and ikeypad values contencated in the HMAC algorithm.

you can read up more on HMAC here

https://en.m.wikipedia.org/wiki/Hash-based_message_authentication_code

totya

  • Hero Member
  • *****
  • Posts: 720
Re: HashLib4Pascal
« Reply #8 on: October 01, 2016, 03:35:49 pm »
HMAC has a totally different implementation so "key or no key"

Okay, thanks for this information, and thanks again for this library! :)

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: HashLib4Pascal
« Reply #9 on: October 01, 2016, 03:38:48 pm »
you welcome.

Petr Nehez

  • New Member
  • *
  • Posts: 14
Re: HashLib4Pascal
« Reply #10 on: October 04, 2016, 03:01:21 pm »
I have started to use this great library and have found 1 issue and made 2 small improvements for inline hints.
Changes are in my fork. Check it out at https://github.com/petr-nehez/HashLib4Pascal/commits/master.

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: HashLib4Pascal
« Reply #11 on: October 04, 2016, 05:12:14 pm »
Hi, thanks for your improvements.
will surely add it up to the master branch.
one question by the way,
which compiler gave the inline hint?

Petr Nehez

  • New Member
  • *
  • Posts: 14
Re: HashLib4Pascal
« Reply #12 on: October 05, 2016, 04:26:06 am »
Hi, thanks for your improvements.
Hi, you're welcome :)

which compiler gave the inline hint?
The thing with inline hints was interesting and it was D2010.
I was wondering why I was getting the hints in our main project and not in testing one.
And finally it turned out that you have to have this directive active:
Code: Pascal  [Select][+][-]
  1. {$RTTI EXPLICIT FIELDS([]) PROPERTIES([]) METHODS([])}

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: HashLib4Pascal
« Reply #13 on: October 05, 2016, 08:21:15 am »
thanks for replying.
I do not think disabling rtti is the best solution even though it works.
however I feel we could just give Delphi 2010 what it wants like you did in your commit.

according to your commit, I can see that only two units, hlpmurmur2 and hlpsiphash2_4 gives that hint?
please confirm if I am correct.

thanks.
« Last Edit: October 05, 2016, 09:36:47 am by Xor-el »

Petr Nehez

  • New Member
  • *
  • Posts: 14
Re: HashLib4Pascal
« Reply #14 on: October 05, 2016, 10:09:19 am »
I do not think disabling rtti is the best solution even though it works.
I do agree but we use RTTI in our project only for certain classes and that's why the hints have appeared.

according to your commit, I can see that only two units, hlpmurmur2 and hlpsiphash2_4 gives that hint?
please confirm if I am correct.
Yes, exactly.

 

TinyPortal © 2005-2018