EKON 25 — PyPas

Max Kleiner
3 min readSep 3, 2021

Imagine you need a 512bit hash (or another master of reality procedure) and you don’t have the available function. This time with a small effort to compare Pascal with Python. SHA256 or SHA512 is a secure hash algorithm which creates a fixed length one way string from any input data. OK you start the Python-engine in your maXbox script and load the DLL.

eng:= TPythonEngine.Create(Nil); 
eng.pythonhome:= PYHOME;
eng.opendll(PYDLL) //eng.IO:= pyMemo;
try
eng.Execstring('with open(r"'+exepath+'maXbox4.exe","rb") as afile:'+ ' fbuf = afile.read()'); println(eng.evalstr('__import__("hashlib").sha512('+ 'fbuf).hexdigest().upper()'));
except
eng.raiseError;
finally
eng.Free;
aPythonVersion.Free;
end;

So we open with with open() 😉 a file to pass fbuf to the hashlib function, or a bit shorter:

println(eng.evalstr('__import__("hashlib").sha1(fbuf).hexdigest().upper()')); 
>>> 3E38A48072D4F828A4BE4A52320F092FE50AE9C3

It may be important to notice the read function. When it is called with no arguments, like in this case, it will read all the contents of the file and load them into memory. If you need a list of supported hash algorithms in your system use hashlib.algorithms_available.

println(eng.evalstr('__import__("hashlib").algorithms_available'));

>>> {‘SHA256’, ‘SHA384’, ‘md5’, ‘sha3_384’, ‘SHA512’, ‘blake2s’, ‘blake2b’, ‘MD5’, ‘SHA224’, ‘dsaWithSHA’, ‘md4’, ‘MD4’, ‘sha1’, ‘sha3_512’, ‘sha512’, ‘sha256’, ‘whirlpool’, ‘sha384’, ‘ecdsa-with-SHA1’, ‘RIPEMD160’, ‘sha’, ‘sha224’, ‘DSA’, ‘DSA-SHA’, ‘sha3_224’, ‘dsaEncryption’, ‘shake_256’, ‘SHA1’, ‘shake_128’, ‘ripemd160’, ‘SHA’, ‘sha3_256’}

Now lets compare the hash lib with the Advapi32 Dll of Windows:

function Advapi32_SHA512: string; 
var shaStr: string; begin writeln('crypcontext: '+botostr(CryptAcquireContext(hProv, '', '', PROV_RSA_AES, CRYPT_VERIFYCONTEXT)));
writeln('crypcreate: '+ botostr(CryptCreateHash(hProv,CALG_SHA512,hkey,0,hHash)));
sr:= filetoString(exepath+'maXbox4.exe');
writeln('crypdata: ' +botostr(CryptHashData(hhash,sr,length(sr),0)));
cbHashDataLen:= 64;
if (CryptGetHashParam512(hHash, HP_HASHVAL,shares4,cbHashDataLen,0)) then begin for it:= 1 to cbHashDataLen do shaStr:= shaStr +UpperCase(IntToHex((shares4[it]),2));
result:= shaStr;
end;
println('destroy cryphash-hndl: '+botostr(CryptDestroyHash(hhash))); println('cryp_ReleaseContext: '+botostr(CryptReleaseContext(hProv,0))); writeln('SHA512 posttest: '+(binToHEX_Str(shares4)))
end;

More work but also more versatile. This code assumes that the handle of a cryptographic context has been acquired and that a hash object has been created and its handle ( hHash) is available. Of course in maXbox or Delphi you can use LockBox.

The routines are unit tested and it compiles and works almost flawlessly on all versions from XE7. All you need is just to adjust the TPLB3.Common.inc file (if required). And one more thing you would probably need is adding {$Q-, R-} in the INC file — as if you are testing your projects with range and overflow check (which should be enabled for debugging).

LockBox3 is a Delphi library for cryptography. It provides support for AES, DES, 3DES, Blowfish, Twofish, SHA, MD5, a variety of chaining modes, RSA digital signature and verification. For Lazarus I installed correctly CryptoLib4Pascal; but I don’t know exactly how to use this library.

Originally published at http://maxbox4.wordpress.com on September 3, 2021.

--

--

Max Kleiner

Max Kleiner's professional environment is in the areas of OOP, UML and coding - among other things as a trainer, developer and consultant.