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

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn To Integrate Python Cryptographic Services To A Delphi Windows GUI App
By Muhammad Azizul Hakim June 21, 2021

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 Для просмотра ссылки Войди или Зарегистрируйся for your platform. Follow the Для просмотра ссылки Войди или Зарегистрируйся installation instructions mentioned Для просмотра ссылки Войди или Зарегистрируйся. Alternatively, you can check out the easy instructions found in this video Для просмотра ссылки Войди или Зарегистрируйся.

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 Для просмотра ссылки Войди или Зарегистрируйся. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this Для просмотра ссылки Войди или Зарегистрируйся.
1624353683539.png

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:
Python:
import hashlib
 
print('Guaranteed:n{}n'.format(
    ', '.join(sorted(hashlib.algorithms_guaranteed))))
print('Available:n{}'.format(
    ', '.join(sorted(hashlib.algorithms_available))))
See the results in our P4D GUI:
1624353721872.png

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:
Python:
import hashlib
 
m = hashlib.sha256()
m.update(b"Hello")
m.update(b" World!")
 
print(m.digest())
print(m.digest_size)
print(m.block_size)
 
print(hashlib.sha256(b"Hello World!").hexdigest())
# More condensed example:
print(hashlib.sha224(b"Hello World!").hexdigest())
 
# Using new() with an algorithm provided by OpenSSL:
h = hashlib.new('ripemd160')
h.update(b"Hello World!")
print(h.hexdigest())
See the results in our P4D GUI:
1624353754685.png
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.