Forum > Third party

Modular hashing/checksum library with 150+ algos

(1/3) > >>

domasz:
I started my own hashing/checksum library:
https://github.com/PascalVault/Lazarus_Hashing
Currently there are 150+ algorithms.

Why? I wanted a modular library. Other libraries I know of are quite bulky. In this library if you need just 1 algorithm for you program you can just copy a file with that algorithm + "HasherBase.pas" and you are good to go. There are no packages, no installation is required- just put the files in your project directory and add to "uses".

There is no assembly code, no DLLs, it doesn't require any other libraries so can be used from a clean Lazarus installation.

You can find here, among other things, almost every possible CRC variant. However this is the only library on the Internet in any programming language where all these CRC variants are lookup-table-based- so they are fast.

Most popular hashing functions like SHA-1 and MD-5 are not yet in this library. You can get those from other libraries. In the (near, hopefully) future these will also be available in my library.

Usage examples- hashing a String:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Hasher; var Hasher: THasher;    Hash: AnsiString;begin  try    Hasher := THasher.Create('CRC-32 JAMCRC');    Hasher.Update('123456789');    Hash := Hasher.Final;    Hasher.Free;        Memo1.Lines.Add( Hash );  finally  end;
Hashing a Stream:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses Hasher; var Hasher: THasher;    Hash: String;    Msg: TMemoryStream;begin  try    Msg := TMemoryStream.Create;    Hasher := THasher.Create('CRC-32 JAMCRC');    Hasher.Update(Msg);    Hash := Hasher.Final;    Hasher.Free;        Memo1.Lines.Add( Hash );  finally    Msg.Free;  end;
THasher is a just helper class. You can use the classes directly:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses CRC64; var Hasher: THasherCRC64;    Hash: String;    Msg: AnsiString;begin  Msg := '123456789';  Hasher := THasherCRC64.Create;  Hasher.Update(@Msg[1], Length(Msg));  Hash := Hasher.Final;  Hasher.Free;   Memo1.Lines.Add( Hash );end;
There is also a PHP port (but missing many algos):
https://github.com/PascalVault/Lazarus_Hashing/tree/main/PHP

Licensed under the terms of MIT so you can freely use it even in commercial projects.

BeniBela:
That reminds me that I am using One-at-a-time in my hashmaps, and should replace it with a faster hash eventually

But you do not have the fastest hashes like XXH3, City64, Murmur3 ?

And the output is bad for hashmaps because it needs an integer not a string.

It would be easier to use in one's own project, if it was procedural code without classes, and we only had to import a single unit, rather than two

Perhaps these Move(Msg^, Tmp2, 4); can be replaced with tmp2 := unaligned(PCardinal(Msg)^)

domasz:

--- Quote from: BeniBela on November 26, 2022, 12:20:25 am ---But you do not have the fastest hashes like XXH3, City64, Murmur3 ?

--- End quote ---
I started this library 2 weeks ago and just haven't coded those yet. I've just added Murmur3:
https://github.com/PascalVault/Lazarus_Hashing/blob/main/MurmurHash3.pas


--- Quote from: BeniBela on November 26, 2022, 12:20:25 am ---And the output is bad for hashmaps because it needs an integer not a string.

--- End quote ---
No library will satisfy everyone. You can adapt the code for your needs. I tried to write everything as simply as possible.


--- Quote from: BeniBela on November 26, 2022, 12:20:25 am ---It would be easier to use in one's own project, if it was procedural code without classes, and we only had to import a single unit, rather than two

--- End quote ---
Yes, but if someone needs multiple algorithms in one program then procedural code would be worse for him/her.


--- Quote from: BeniBela on November 26, 2022, 12:20:25 am ---Perhaps these Move(Msg^, Tmp2, 4); can be replaced with tmp2 := unaligned(PCardinal(Msg)^)

--- End quote ---
You are right, thank you.

abouchez:
Good idea.
Even if not used directly, it is a good reference for odd hash algorithms source code in pascal.  O:-)

There is a similar hash registration in mORMot 2.
You can either run directly the low-level hashing functions, or you have some high-level catalog.
https://github.com/synopse/mORMot2/blob/master/src/crypt/mormot.crypt.secure.pas#L946


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var hasher: ICryptHash;begin  hasher := Hash('crc32c');  hasher.Update('some text');  writeln(hasher.Final);end; or in a one-liner

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  writeln(Hash('crc32c').Full('some text')); 
mormot.core.secure unit supports 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'sha3_256', 'sha3_512' and 32-bit non-cryptographic 'crc32', 'crc32c','xxhash32', 'adler32', 'fnv32' for ICryptHash, and there are ICryptSigner for salted digest like 'hmac-sha1', 'hmac-sha256', 'hmac-sha384', 'hmac-sha512', and 'sha3-224', 'sha3-256', 'sha3-384', 'sha3-512', 'sha3-s128', 'sha3-s256'.

And there are the same catalog system for encryption, randomness and also certificates and asymetric cryptography, using either native mORMot code, or OpenSSL.

abouchez:

--- Quote from: domasz on November 25, 2022, 12:26:09 pm ---However this is the only library on the Internet in any programming language where all these CRC variants are lookup-table-based- so they are fast.

--- End quote ---
I am not sure it is the only one with lookup table, but for sure the most common versions (e.g. crc32 or crc32c) are slow in comparison to most production code.
For instance, you could use 8 lookup tables instead of a single one. The zlib code will be magnitude faster than this single-table code.
And... there are crc32c HW asm opcodes to reach more than 20GB/s on any recent CPU. And similar speed with as libdeflate crc32.

So performance is not the main point of this library.
Simplicity and algorithms coverage are its main points.
Nice work.

Navigation

[0] Message Index

[#] Next page

Go to full version