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
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.
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 ?
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/
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).
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;
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