0% found this document useful (0 votes)
18 views6 pages

Digital Assignment - Crypto

The document outlines a digital assignment involving the computation of Message Authentication Codes (MAC) using SHA-128 and SHA-256 hash algorithms, measuring time consumption for varying message sizes. It also details the development of a Digital Signature Standard (DSS) for verifying communications between parties, including key generation, signing, and signature verification. The code provided includes functions for MAC computation, timing measurements, and plotting results using matplotlib.

Uploaded by

akshatdubey0710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

Digital Assignment - Crypto

The document outlines a digital assignment involving the computation of Message Authentication Codes (MAC) using SHA-128 and SHA-256 hash algorithms, measuring time consumption for varying message sizes. It also details the development of a Digital Signature Standard (DSS) for verifying communications between parties, including key generation, signing, and signature verification. The code provided includes functions for MAC computation, timing measurements, and plotting results using matplotlib.

Uploaded by

akshatdubey0710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Digital assignment

Akshat Dubey

22BCE3364

1. Find a Message Authentication Code (MAC) for a given variable size


message by using SHA-128 and SHA-256 Hash algorithm variable
size. Measure the Time consumption for varying message sizes for
both SHA-128 and SHA256.

import time

import hmac

import hashlib

import secrets

import matplotlib.pyplot as plt

def compute_mac(message: bytes, key: bytes, hash_algo):

return hmac.new(key, message, hash_algo).hexdigest()

def measure_time_mac(hash_algo, message_sizes):

key = secrets.token_bytes(16) # Generate a random 16-byte key

times = {}

for size in message_sizes:

message = secrets.token_bytes(size)

start_time = time.time()

compute_mac(message, key, hash_algo)

end_time = time.time()

times[size] = end_time - start_time

return times

def plot_time_consumption(sha128_times, sha256_times,


message_sizes):

plt.figure()
plt.plot(message_sizes, list(sha128_times.values()), label='SHA-128
(MD5 Approximation)', marker='o')

plt.plot(message_sizes, list(sha256_times.values()), label='SHA-256',


marker='s')

plt.xlabel('Message Size (bytes)')

plt.ylabel('Time Taken (seconds)')

plt.title('Time Consumption of MAC Calculation')

plt.legend()

plt.grid()

plt.show()

if __name__ == "__main__":

# MAC computation timing

message_sizes = [16, 64, 256, 1024, 4096, 16384]

sha128_times = measure_time_mac(hashlib.md5, message_sizes) #


Using MD5 as SHA-128 is not standard

sha256_times = measure_time_mac(hashlib.sha256, message_sizes)

print("SHA-128 Timing:", sha128_times)

print("SHA-256 Timing:", sha256_times)

# Plot the results

plot_time_consumption(sha128_times, sha256_times,
message_sizes)
2.Develop the Digital Signature Standard(DSS)for verifying the legal
communicating Parties

import time

import hmac

import hashlib

import secrets

import matplotlib.pyplot as plt


from Crypto.PublicKey import DSA

from Crypto.Signature import DSS

from Crypto.Hash import SHA256

def compute_mac(message: bytes, key: bytes, hash_algo):

return hmac.new(key, message, hash_algo).hexdigest()

def measure_time_mac(hash_algo, message_sizes):

key = secrets.token_bytes(16) # Generate a random 16-byte key

times = {}

for size in message_sizes:

message = secrets.token_bytes(size)

start_time = time.time()

compute_mac(message, key, hash_algo)

end_time = time.time()

times[size] = end_time - start_time

return times

def plot_time_consumption(sha128_times, sha256_times, message_sizes):

plt.figure()

plt.plot(message_sizes, list(sha128_times.values()), label='SHA-128


(MD5 Approximation)', marker='o')

plt.plot(message_sizes, list(sha256_times.values()), label='SHA-256',


marker='s')

plt.xlabel('Message Size (bytes)')

plt.ylabel('Time Taken (seconds)')

plt.title('Time Consumption of MAC Calculation')

plt.legend()

plt.grid()

plt.show()
def generate_dss_keys():

key = DSA.generate(1024) # Generate a 1024-bit DSA key pair

return key

def sign_message_dss(message: bytes, key):

hash_obj = SHA256.new(message)

signer = DSS.new(key, 'fips-186-3')

signature = signer.sign(hash_obj)

return signature

def verify_signature_dss(message: bytes, signature, key):

hash_obj = SHA256.new(message)

verifier = DSS.new(key.publickey(), 'fips-186-3')

try:

verifier.verify(hash_obj, signature)

return "Signature is valid."

except ValueError:

return "Signature is invalid."

if __name__ == "__main__":

# MAC computation timing

message_sizes = [16, 64, 256, 1024, 4096, 16384]

sha128_times = measure_time_mac(hashlib.md5, message_sizes) #


Using MD5 as SHA-128 is not standard

sha256_times = measure_time_mac(hashlib.sha256, message_sizes)

print("SHA-128 Timing:", sha128_times)

print("SHA-256 Timing:", sha256_times)


# Plot the results

plot_time_consumption(sha128_times, sha256_times, message_sizes)

# Digital Signature using DSS

dss_key = generate_dss_keys()

message = b"This is a test message."

signature = sign_message_dss(message, dss_key)

verification_result = verify_signature_dss(message, signature, dss_key)

print("DSS Verification Result:", verification_result)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy