Let’s Find Unique SHA-256 #Code Using Python GUI

In this blog, we will learn how to create an SHA 256 Generator using Python (Tkinter Module).

What is SHA 256?

SHA stands for Secure Hash Algorithm 256, where 256 is defined as one of the most secure ways to protect digital information.

SHA is a set of cryptographic hash functions defined by the language to be used for various applications such as password security, etc.

For example, “Hello World” can be hashed and will equal: a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146

If we use small “h” and “w”, in “hello world” the hash will be completely different:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9

(https://www.geeksforgeeks.org/sha-in-python/)

Now let us start a project using Python GUI for SHA 256 Encryption:

Code for the project:

For the code of this project click on the link below:

(https://github.com/Platforuma/medium_blog/blob/cf770c355439ec4d4175eaf71f07514df677460c/SHA-256.py)

Define code working :-

from tkinter import *

import hashlib

Tk class is used to create a root window. The root is the name of a root window. Here Tk function provides a window of GUI as well as provides so many features like setting the title, set the geometry of the GUI window

Here we import Python hashlib Module.

Let’s know what is hash?

Hash values are just integers values. The hash() method returns the hash value of an object.

screen = Tk()

screen.title(“Password Generator”)

screen.geometry(‘600x400’)

screen.configure(background =”bisque”)

Tk class is used to create a screen window. The screen is the name of the root window.

In these steps, we are giving the title of the GUI root window. The title is defined about the project in one line about what we will do in the project.

Tkinter provides many methods, one of them is the geometry() method.

What is Tkinter Window Background Color?

The default background color of a GUI with Tkinter is Grey. You can change the color also. We will set​ the screen.configure(bg=’bisque’) configures the background color of the screen to be bisque. Bisque is the color name.

def gen():

global sc1, result, c1

sc1.set(“”)

passw=””

Coming to the next line of the code we define gen function.

Here we define global variables in the program sc1, result, c1.We set the variable values sc1 and passw is null.

if c1.get() :

result = hashlib.sha256(c1.get().encode())

passw = result.hexdigest()

sc1.set(passw)

In this line, we are defining the if condition when getting the c1 value, and then apply the condition.

(For more information on Conditional Statement, kindly refer to this documentation)

Here in this line, we define the result variable to hold the value of sha256 encodes the value of the c1 variable value. Now in this line, we define a passw variable to hold the value of the encoded data in hexadecimal format. Set the value of sc1 value from the passw variable value.

(For more information on set function, kindly refer to the documentation)

sc1=StringVar(‘’)

t1=Label(screen,text=’Automatic # sha256 Generator’,font=(‘Arial’,25),fg=’red’,background =”bisque”)

t1.place(x=60,y=0)

screen = Tk()

screen.title(“Password Generator”)

screen.geometry(‘600x400’)

screen.configure(background =”bisque”)

Here in this line, we define the result variable to hold the value of sha256 encodes the value of the c1 variable value. Now in this line, we define a passw variable to hold the value of the encoded data in hexadecimal format.

Set the value of sc1 value from the passw variable value.

(For more information on set function, kindly refer to the documentation)

Here we define the variable sc1. We used sc1 Variable type is StringVar.

Now come to Designing part of the GUI project. Here we

Here in this line define a Label t1 on GUI window and write text on the label and set font and set fg and bg of the label.

Now in this line, we are working on the place method. In this, we assign the place of the t1 label using x or y coordinates values.

t2=Label(screen,text=’password:’,font=(‘Arial’,14),background =”bisque”)

t2.place(x=145,y=90)

il=Entry(screen,font=(‘Arial’,14),textvariable=sc1)

il.place(x=270,y=90)

Now we take one another Label t2 on the GUI window and define the same as above

(For more information on Label, kindly refer to the documentation)

Now about the Entry Box.

In this line of code, we define an Entry il on GUI window. Assign the place of the il Entry using coordinates values.

t3=Label(screen,text=’Type word ‘,font=(‘Arial’,14),background =”bisque”)

t3.place(x=145,y=120)

c1=Entry(screen,font=(‘Arial’,14),width=10)

c1.place(x=270,y=120)

Here again, we define a Label t3 on the GUI window and define it as above. Assign the place as above.

Again we use another Entry Box and define the same as above. Assign the place as above.

sc1=StringVar(‘’)

t1=Label(screen,text=’Automatic # sha256 Generator’,font=(‘Arial’,25),fg=’red’,background =”bisque”)

t1.place(x=60,y=0)

screen = Tk()

screen.title(“Password Generator”)

screen.geometry(‘600x400’)

screen.configure(background =”bisque”)

b=Button(screen,text=’Generate’,font=(‘Arial’,14),fg=’red’,background =”white”,command=gen)

b.place(x=230,y=195)

Now come to the main part of the project is Button because all functionality is added on the button.

In this line of code, we define the b Button on the GUI window and set text and font fg, bg, and also give the gen command on the b Button for adding the functionality. Assign the place of the b Button using coordinates values.

screen.mainloop()

Now come to the last line of code of the project is the mainloop function that provides an infinite loop.

(For more information on SHA-256, kindly refer to the documentation)

Here we complete a full GUI Project using the Python Tkinter module and hashlib module.

Refer to our more Blogs on Python from the link below:

https://medium.com/@platforuma

Visit Our Website:

Author: Priyanka Prasad

Editor: Riya Patidar, Devendra Patidar, Ashutosh Raghuwanshi