Sie sind auf Seite 1von 5

Program: Python Program to Find Hash of File

Description:

Hash functions take an arbitrary amount of data and return a fixed-length bit string.
The output of the function is called the digest message.

They are widely used in cryptography for authentication purposes. There are many
hashing functions like MD5, SHA-1 etc. In this example, we will illustrate how to hash
a file. We will use the SHA-1 hashing algorithm. The digest of SHA-1 is 160 bits long.

We do not feed the data from the file all at once, because some files are very large
to fit in memory all at once. Breaking the file into small chunks will make the process
memory efficient.

Program code:

# Python rogram to find the SHA-1 message digest of a file

# import hashlib module


import hashlib

def hash_file(filename):
""""This function returns the SHA-1 hash
of the file passed into it"""

# make a hash object


h = hashlib.sha1()

# open file for reading in binary mode


with open(filename,'rb') as file:

# loop till the end of the file


chunk = 0
while chunk != b'':
# read only 1024 bytes at a time
chunk = file.read(1024)
h.update(chunk)
# return the hex representation of digest
return h.hexdigest()

message = hash_file("track1.mp3")
print(message)

Output

633d7356947eec543c50b76a1852f92427f4dca9

In this program, we open the file in binary mode. Hash functions are available in
the hashlib module. We loop till the end of the file using a while loop. On reaching
the end, we get empty bytes object.

In each iteration we only read 1024 bytes (this value can be changed according to
our wish) from the file and update the hashing function.

Finally, we return the digest message in hexadecimal representation using


the hexdigest()method.

Program No.2

Program : Python Program to Find the Size (Resolution) of a Image

Description:

JPEG (pronounced "jay-peg") stands for Joint Photographic Experts Group. It is one
of the most widely used compression techniques for image compression.

Most of the file formats have headers (initial few bytes) which contain useful
information about the file.

For example, jpeg headers contain information like height, width, number of color
(grayscale or RGB) etc. In this program, we find the resolution of a jpeg image
reading these headers, without using any external library.

Program code:
def jpeg_res(filename):
""""This function prints the resolution of the jpeg image file
passed into it"""

# open image for reading in binary mode


with open(filename,'rb') as img_file:

# height of image (in 2 bytes) is at 164th position


img_file.seek(163)

# read the 2 bytes


a = img_file.read(2)

# calculate height
height = (a[0] << 8) + a[1]

# next 2 bytes is width


a = img_file.read(2)

# calculate width
width = (a[0] << 8) + a[1]

print("The resolution of the image is",width,"x",height)

jpeg_res("img1.jpg")

Output:

The resolution of the image is 280 x 280

In this program, we opened the image in binary mode. Non-text files must be open in
this mode. The height of the image is at 164th position followed by width of the
image. Both are 2 bytes long.
Note that this is true only for JPEG File Interchange Format (JFIF) standard. If your
image is encode using other standard (like EXIF), the code will not work.

We convert the 2 bytes into a number using bitwise shifting oper

Program No.3

Program : Python Program to Merge Mails

Description:

When we want to send the same invitations to many people, the body of the mail
does not change. Only the name (and maybe address) needs to be changed.

Mail merge is a process of doing this. Instead of writing each mail separately, we
have a template for body of the mail and a list of names that we merge together to
form all the mails.

Program code:

# Python program to mail merger


# Names are in the file names.txt
# Body of the mail is in body.txt

# open names.txt for reading


with open("names.txt",'r',encoding = 'utf-8') as names_file:

# open body.txt for reading


with open("body.txt",'r',encoding = 'utf-8') as body_file:

# read entire content of the body


body = body_file.read()

# iterate over names


for name in names_file:
mail = "Hello "+name+body
# write the mails to individual files
with open(name.strip()+".txt",'w',encoding = 'utf-8') as
mail_file:
mail_file.write(mail)

Output:

For this program, we have written all the names in separate lines in the file
"names.txt". The body is in the "body.txt" file.

We open both the files in reading mode and iterate over each name using a for loop.
A new file with the name "[name].txt" is created, where name is the name of that
person.

We use strip() method to clean up leading and trailing whitespaces (reading a line
from the file also reads the newline '\n' character). Finally, we write the content of the
mail into this file using the write() method.

Program No.

Program :

Description:

Program code:

Output:

Program No.

Program :

Description:

Program code:

Output:

Das könnte Ihnen auch gefallen