Programs Code
Programs Code
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:
def hash_file(filename):
""""This function returns the SHA-1 hash
of the file passed into it"""
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.
Program No.2
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"""
# calculate height
height = (a[0] << 8) + a[1]
# calculate width
width = (a[0] << 8) + a[1]
jpeg_res("img1.jpg")
Output:
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.
Program No.3
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:
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: