Task7.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Oct 1 08:41:54 2018
  5. @author: varshath
  6. """
  7. from cryptography.hazmat.backends import default_backend
  8. from cryptography.hazmat.primitives import hashes
  9. from cryptography.hazmat.primitives.asymmetric import dsa
  10. from timeit import default_timer as timer
  11. def generateFiles():
  12. with open('kbfile.txt', 'w+') as f:
  13. num_chars = 256
  14. f.write("abcd" * num_chars)
  15. with open('mbfile.txt', 'w+') as f:
  16. num_chars = 256 * 1024
  17. f.write("abcd" * num_chars)
  18. def getData(fileName):
  19. x=""
  20. with open(fileName,'r') as file:
  21. x=file.read()
  22. return x
  23. def generatePrivateKey(size):
  24. start=timer()
  25. private_key = dsa.generate_private_key(key_size=size,backend=default_backend())
  26. end=timer()
  27. print("Time Taken to generate key is ",(end-start))
  28. return private_key
  29. def sign(private_key,fileName):
  30. data=getData(fileName)
  31. data=data.encode("utf-8")
  32. start=timer()
  33. signature = private_key.sign(data,hashes.SHA256())
  34. end=timer()
  35. print("Time Taken to generate signature is ",(end-start))
  36. print("Time Taken per byte to sign is ",((end-start)/len(data)))
  37. return signature
  38. def verify(signature,private_key,fileName):
  39. data=getData(fileName)
  40. data=data.encode("utf-8")
  41. start=timer()
  42. public_key = private_key.public_key()
  43. public_key.verify(
  44. signature,
  45. data,
  46. hashes.SHA256()
  47. )
  48. end=timer()
  49. print("Time Taken to verify the signature is ",(end-start))
  50. print("Time Taken per byte to verify is ",((end-start)/len(data)))
  51. generateFiles()
  52. private_key=generatePrivateKey(2048)
  53. signature=sign(private_key,"kbfile.txt")
  54. verify(signature,private_key,"kbfile.txt")
  55. signature=sign(private_key,"mbfile.txt")
  56. verify(signature,private_key,"mbfile.txt")