Task4.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Sun Sep 30 23:42:54 2018
  5. @author: varshath
  6. """
  7. import base64
  8. from timeit import default_timer as timer
  9. from Crypto.Hash import SHA256,SHA512
  10. from Crypto.Hash import SHA3_256
  11. def writeFile(data,fileName):
  12. with open(fileName, 'w+') as f:
  13. f.write(data)
  14. def writeFileInBytes(data,fileName):
  15. with open(fileName, 'wb+') as f:
  16. f.write(data)
  17. def generateFiles():
  18. with open('kbfile.txt', 'w+') as f:
  19. num_chars = 256
  20. f.write("abcd" * num_chars)
  21. #f.write("\n"*1)
  22. with open('mbfile.txt', 'w+') as f:
  23. num_chars = 256 * 1024
  24. f.write("abcd" * num_chars)
  25. f.write("\n"*1)
  26. def getData(fileName):
  27. x=""
  28. with open(fileName,'r') as file:
  29. x=file.read()
  30. return x
  31. def getDataInBytes(fileName):
  32. x=""
  33. with open(fileName,'rb') as file:
  34. x=file.read()
  35. return x
  36. def generateHashUsingSHA256(fileName):
  37. print("SHA_256")
  38. data=getData(fileName)
  39. data=bytes(data,"utf-8")
  40. start = timer()
  41. h = SHA256.new()
  42. h.update(data)
  43. #Because this looks better when viewed instead of h.digest()
  44. digest=h.hexdigest()
  45. end = timer()
  46. #print(digest)
  47. print("Time Taken to generate Hash is ",(end-start))
  48. print("Time Taken to Hash per byte is ",(end-start)/len(data))
  49. def generateHashUsingSHA512(fileName):
  50. print("SHA_512")
  51. data=getData(fileName)
  52. data=bytes(data,"utf-8")
  53. start = timer()
  54. h = SHA512.new()
  55. h.update(data)
  56. #Because this looks better when viewed instead of h.digest()
  57. digest=h.hexdigest()
  58. end = timer()
  59. #print(digest)
  60. print("Time Taken to generate Hash is ",(end-start))
  61. print("Time Taken to Hash per byte is ",(end-start)/len(data))
  62. def generateHashUsingSHA3_256(fileName):
  63. print("SHA3_256")
  64. data=getData(fileName)
  65. data=bytes(data,"utf-8")
  66. start = timer()
  67. h = SHA3_256.new()
  68. h.update(data)
  69. #Because this looks better when viewed instead of h.digest()
  70. digest=h.hexdigest()
  71. end = timer()
  72. #print(digest)
  73. print("Time Taken to generate Hash is ",(end-start))
  74. print("Time Taken to Hash per byte is ",(end-start)/len(data))
  75. generateFiles()
  76. generateHashUsingSHA256("kbfile.txt")
  77. generateHashUsingSHA256("mbfile.txt")
  78. generateHashUsingSHA512("kbfile.txt")
  79. generateHashUsingSHA512("mbfile.txt")
  80. generateHashUsingSHA3_256("kbfile.txt")
  81. generateHashUsingSHA3_256("mbfile.txt")