Newbie trying to get a Hash vlaue from a string using HashLib4Pascal library - any code examples?

Newbie trying to get a Hash value from a string using HashLib4Pascal library - any code examples?

Hi just trying to get a hash value in order to implement a block chain project. Never done this before so not sure how to proceed - have Googled and while installing the HashLib4Pascal library was straight forward, I can not work out without any documentation how to code the hash function - what hash functions to use, and the code syntax??. Would like to use SHA256 to create a 64 bit long byte string from any input string. Using Lazarus 2.2.2 on Windows. Thx in advance. Martin

This might be worth a look Martin (free and opensource)
SynCrossPlatformCrypto.pas

Ouch - forgot the link.
mORMot/SynCrossPlatformCrypto.pas at master · synopse/mORMot · GitHub

HI Robert,

Thanks for the heads up - so how to use this? Presumably use the SHA256 class. Any code examples? And do I need to install the whole MORMOT library to use just the Hash256 function? Seems a bit of overkill.

Cheers,

Martin

Wouldn’t have thought so Martin - but I’ll check it out for external dependencies tomorrow. One other option I noticed (and used quite a while ago) is DWScript by Eric Grange. I’ll check that out to (GitHub - EricGrange/DWScript: Delphi Web Script general purpose scripting engine in case you want to look).

Hi Robert,

Thanks for the info as there’s not much out there in Google land with documented code examples to run with. I’m pushed for time so can’t wade through multiple library units’ code to work out a) which is best, b) which has algorithm is suitable, and c) exactly how to code it. I’ve been looking more at HashLib4Delphi as its part of both Lazarus (package manager) and Delphi (GetIt) but then so is LockBox, Indy, DWScript as you say, and other libraries. Spoiled for choice and so without spending much time to go through each to weigh up pros and cons, its difficult to chose. Its surprising the number of libraries out there but so little documentation or code examples. My Googling skills seem to need improvement!

Anyone got prior knowledge here to say - this one is best, and this is how to use it ?

Cheers,

Martin

There’s nothing in the System.Hash unit that’s usable for the purpose?

Hey Martin
As per Elvind’s post…
"The basis of hash support first appeared in the RTL around the 2009 release but in XE8 (2015) we got the System.Hash unit, which brought the MD5, SHA-1, and Bob Jenkins hashes. Then in 10.0 Seattle (2015) it was expanded with SHA-2 support. Most recently in 10.2 Tokyo (2017) the hash functions were expanded to accept either a string or stream in addition to the original bytes input. " and so on - see https://blogs.embarcadero.com/sha-hash-with-c-builder-and-delphi/

Sorry if I’m stating the bleeding obvious :slightly_smiling_face:
Aneal

For an implementation example see line 49 - 177 of https://github.com/anealchandra/kdlogue/blob/master/login.cpp
Sure It’s in C++ but uses OOTB XE4 Delphi units only

Hi Aneal,

How are you doing? Chilly in Canberra? Melbourne’s brass monkeys at the moment.

Thanks for this. I haven’t looked at hashing or encryption till now so on the learning curve and not a lot on Google for Pascal, so thanks for your help, including the C++ code example. Much appreciated.

As you say, Delphi and Emb C++ seem to have it covered now in System.Hash but I was working in FPC/Lazarus and can not find a System.Hash there (is there an equivalent?) If not in the box in Laxzarus, then I can use HashLib4Pascal. I just found this article by Martin Fernstrom

which is an excellent tutorial with some code example Will try this out today. For everyone that has helped, thanks so much. Cheers, Martin

Good to hear you may have a solution Martin. If it doesn’t work out for you then let me know and I’ll have a play with SynCrypto.pas (stand alone). Here’s a rough overview :
/// fast cryptographic routines (hashing and cypher)
// - implements AES,XOR,ADLER32,MD5,RC4,SHA1,SHA256,SHA384,SHA512,SHA3 and JWT
// - optimized for speed (tuned assembler and SSE3/SSE4/AES-NI/PADLOCK support)

It supports Delphi and FPC, Windows and Linux - so might be useful (but I can’t be sure till I have a play).

Hi Rob, Anneal and others

OK with HashLib4Pascal it takes just three lines of code to get the 64 bit 512 byte hash value of a hex string. Easy peazy. That’s what I was looking for.

Thanks everyone

Cheers

Martin

uses

// include this in the users clause after installing HashLib4Pascal from the Lazarus online package manager

…HlpHashFactory, HlpIHash, HlpSHA2_512;

Function GetHashString(AString: String): String;
var
AHash: IHash;
begin;
AHash := TSHA2_512.Create();
Result := AHash.ComputeString(AString, TEncoding.UTF8).ToString();
// hash.free; // Seems like you don’t need to free an interface. Memory leak?
end;

1 Like

Actually Martin we have been enjoying unseasonally warm temperatures here in the sunny Capital, with overnight temperatures soaring to a balmy 7 degrees last night. Yes, we are certainly enjoying the effects of climate change…for now

WRT worknplay, due to Emb pulling the plug on Google Play support for some yet to be explained reason, I’ve been working with their competition who are instead continuing to improve their cross-platform offerings, e.g. VSCode and QT Creator. With Windows 11 some nice Linux integration has been forthcoming - see https://devblogs.microsoft.com/cppblog/build-and-debug-c-with-wsl-2-distributions-and-visual-studio-2022/

BTW the github code I posted does not use System.Hash. From memory, I say it uses Delphi’s OOTB Indy libraries

All the best
Aneal

(Interfaces free themselves.)

I’m late to the party, but saw I had this link saved …

Hi Paul,

That’s a great article. Thanks for that.

Cheers,

Martin

1 Like

Also … maybe this might be of interest …

(2015) Introduction to Encryption: Understanding Security Algorithm Use Cases
CodeRage X - Robert Love

Thx Paul,

That’s a really good tutorial too. Have some time this week so will watch that.

Cheers

Martin

1 Like