DelphiLearn PythonPythonPython GUIRAD Studio

Learn To Integrate Python Cryptographic Services To A Delphi Windows GUI App

pexels tima miroshnichenko 5380590

Python offers various built-in algorithms for cryptographic tasks. On Unix systems, the crypt module may also be available. This post will demonstrate hashlib: One of the built-in algorithms for cryptographic tasks, used for secure hashes and message digests using Python and run it in the Python GUI by Python4Delphi to get the results.

hashlib implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA’s MD5 algorithm (defined in Internet RFC 1321). 

The terms “secure hash” and “message digest” are interchangeable. Older algorithms were called message digests. The modern term is secure hash.

This post will guide you on implementing Python cryptographic services using the built-in hashlib library in Python GUI for Delphi Windows App.

Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0 rundemo1 6813476
Open Demo01.dproj

 

1. Python hashlib Module

The hashlib module defines an API for accessing different cryptographic hashing algorithms. To work with a specific hash algorithm, use the appropriate constructor function or new() to create a hash object. From there, the objects use the same API, no matter what algorithm is being used.

Since hashlib is “backed” by OpenSSL, all of the algorithms provided by that library are available, including:

  • md5
  • sha1
  • sha224
  • sha256
  • sha384
  • sha512

Some algorithms are available on all platforms, and some depend on the underlying libraries. For lists of each, look at algorithms_guaranteed and algorithms_available respectively using the following code:

See the results in our P4D GUI:

1 1188226

 

2. Example

Let’s try another example, to obtain the digest of the byte string b’Hello World!’, it’s more condensed version, and try an algorithm provided by OpenSSL:

See the results in our P4D GUI:

2 4849714

Here are more details on hashlib methods:

  • hashlib.new(name[, data]): This is a generic constructor that takes the string name of the desired algorithm as its first parameter. It also exists to allow access to the above-listed hashes as well as any other algorithms that your OpenSSL library may offer. The named constructors are much faster than new() and should be preferred.
  • hashlib.algorithms_guaranteed: A set containing the names of the hash algorithms guaranteed to be supported by this module on all platforms. Note that ‘md5’ is in this list despite some upstream vendors offering an odd “FIPS compliant” Python build that excludes it.
  • hashlib.algorithms_available: A set containing the names of the hash algorithms that are available in the running Python interpreter. These names will be recognized when passed to new(). algorithms_guaranteed will always be a subset. The same algorithm may appear multiple times in this set under different names (thanks to OpenSSL).
  • hash.digest_size: The size of the resulting hash in bytes.
  • hash.block_size: The internal block size of the hash algorithm in bytes.
  • hash.name: The canonical name of this hash, always lowercase and always suitable as a parameter to new() to create another hash of this type.
  • hash.update(data): Update the hash object with the bytes-like object. Repeated calls are equivalent to a single call with the concatenation of all the arguments: m.update(a); m.update(b) is equivalent to m.update(a+b).
  • hash.digest(): Return the digest of the data passed to the update() method so far. This is a bytes object of size digest_size which may contain bytes in the whole range from 0 to 255.
  • hash.hexdigest(): Like digest() except the digest is returned as a string object of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments.
  • hash.copy(): Return a copy (“clone”) of the hash object. This can be used to efficiently compute the digests of data sharing a common initial substring.

 

Congratulations! You have learned how to implement Python cryptographic services using the built-in hashlib library in Python GUI for Delphi Windows App.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Weather App With The Python Delphi Ecosystem and Weatherstack API

CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create A Music Player With The Python Delphi Ecosystem

Leave a Reply

Your email address will not be published. Required fields are marked *