Site icon Python GUI

How To Sign and Verify Cryptographic Messages In Your Apps

blogbanner3 56

In this post, you’ll learn more about Python, which offers various built-in algorithms for cryptographic tasks.
We’ll also demonstrate hmac: One of the built-in algorithms for cryptographic tasks, used for cryptographic message signing and verification using Python and run it in the Python Debugging Tools by Python4Delphi to get the results.

The hmac module implements keyed-hashing for message authentication, as described in RFC 2104. The HMAC algorithm can be used to verify the integrity of information passed between applications or stored in a potentially vulnerable location. 

The basic idea is to generate a cryptographic hash of the actual data combined with a shared secret key. The resulting hash can then be used to check the transmitted or stored message to determine a level of trust, without transmitting the secret key.

How to implement Python cryptographic services using the built-in hmac library in Windows GUI 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.

Open Demo01dproj

 

How can we use binary digests to produce printable digests?

Let’s try an example of Binary Digests to produce printable digests. Some web services (Google checkout, Amazon S3) use the base64 encoded version of the binary digest instead of the hexdigest. Run the following code in Python4Delphi GUI:

[crayon-66336caccaad6737105304/]

The base64 encoded string ends in a new line, which frequently needs to be stripped off when embedding the string in http headers or other formatting-sensitive contexts. Let’s see the output in Python GUI:

 

How to implement applications of message signatures using Python?

Next, let’s try a more advanced example: Applications of Message Signatures. HMAC authentication should be used for any public network service, and any time data is stored where security is important. For example, when sending data through a pipe or socket, that data should be signed and then the signature should be tested before the data is used. 

Here are the steps to implement the Applications of Message Signatures:

[crayon-66336caccaae2967097593/]

Comparing two digests with a simple string or bytes comparison can be used in a timing attack to expose part or all of the secret key by passing digests of different lengths. compare_digest() implements a fast but constant-time comparison function to protect against timing attacks.

 

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

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

Exit mobile version