0% found this document useful (0 votes)
26 views

CSSPR 4

This document describes the MD5 and SHA-1 hashing algorithms. It includes Python code to implement the algorithms and examples of hashing sample input strings.

Uploaded by

dekap10798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CSSPR 4

This document describes the MD5 and SHA-1 hashing algorithms. It includes Python code to implement the algorithms and examples of hashing sample input strings.

Uploaded by

dekap10798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical 4 : MD5 algorithm

import hashlib

def left_rotate(x, c):


return ((x << c) | (x >> (32 - c))) & 0xFFFFFFFF

def md5_padding(message):
original_length = len(message) * 8
message += b'\x80'
while len(message) % 64 != 56:
message += b'\x00'
message += original_length.to_bytes(8, byteorder='little')
return message

def md5(message):
# Initialize variables
a = 0x67452301
b = 0xEFCDAB89
c = 0x98BADCFE
d = 0x10325476

message = md5_padding(message)

for i in range(0, len(message), 64):


chunk = message[i:i+64]
a_temp = a
b_temp = b
c_temp = c
d_temp = d

# Main loop
for j in range(64):
if j < 16:
f = (b & c) | ((~b) & d)
g=j
elif j < 32:
f = (d & b) | ((~d) & c)
g = (5*j + 1) % 16
elif j < 48:
f=b^c^d
g = (3*j + 5) % 16
else:
f = c ^ (b | (~d))
g = (7*j) % 16

temp = d
d=c
c=b
b = b + left_rotate((a + f + int.from_bytes(chunk[4*g:4*g+4], byteorder='little') +
int.from_bytes(hashlib.md5(b'').digest(), byteorder='little')),
[7,12,17,22][j//16])
a = temp

# Update variables with new hash values


a = (a + a_temp) & 0xFFFFFFFF
b = (b + b_temp) & 0xFFFFFFFF
c = (c + c_temp) & 0xFFFFFFFF
d = (d + d_temp) & 0xFFFFFFFF

return '{:08x}{:08x}{:08x}{:08x}'.format(a, b, c, d) #

Example usage
input_message = b"THIS IS A SYSTEM GENERATED MESSAGE" md5_result_custom =
md5(input_message)
print("Custom MD5 hash of '{}' is: {}".format(input_message.decode(), md5_result_custom))

OUTPUT :

Pr 4.2 SHA-1

import struct

def left_rotate(n, b):


return ((n << b) | (n >> (32 - b))) & 0xffffffff

def sha1(message):
h0 = 0x67452301
h1 = 0xEFCDAB89
h2 = 0x98BADCFE
h3 = 0x10325476
h4 = 0xC3D2E1F0
original_byte_len = len(message)
original_bit_len = original_byte_len * 8
message += b'\x80'
while len(message) % 64 != 56:
message += b'\x00'
message += struct.pack('>Q', original_bit_len)

for i in range(0, len(message), 64):


chunk = message[i:i+64]
words = [0] * 80
for j in range(0, 64, 4):
words[j//4] = struct.unpack('>I', chunk[j:j+4])[0]
for j in range(16, 80):
words[j] = left_rotate((words[j-3] ^ words[j-8] ^ words[j-14] ^ words[j-16]), 1)

a = h0
b = h1
c = h2
d = h3
e = h4

for j in range(0, 80):


if 0 <= j <= 19:
f = (b & c) | ((~b) & d)
k = 0x5A827999
elif 20 <= j <= 39:
f=b^c^d
k = 0x6ED9EBA1
elif 40 <= j <= 59:
f = (b & c) | (b & d) | (c & d)
k = 0x8F1BBCDC
elif 60 <= j <= 79:
f=b^c^d
k = 0xCA62C1D6

temp = left_rotate(a, 5) + f + e + k + words[j]


e=d
d=c
c = left_rotate(b, 30)
b=a
a = temp & 0xffffffff

h0 = (h0 + a) & 0xffffffff


h1 = (h1 + b) & 0xffffffff
h2 = (h2 + c) & 0xffffffff
h3 = (h3 + d) & 0xffffffff
h4 = (h4 + e) & 0xffffffff

return '%08x%08x%08x%08x%08x' % (h0, h1, h2, h3, h4)

message = input("Enter a string to hash: ").encode()


print("SHA-1:", sha1(message))

output:

You might also like