Guest User

Untitled

a guest
Feb 21st, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. from string import ascii_lowercase, ascii_uppercase, digits
  2. from itertools import product
  3. import sys
  4.  
  5. def stringToBinary(string):
  6. return ''.join(format(ord(i), 'b').zfill(8) for i in string)
  7.  
  8. def getSliceLength(passphrase):
  9. binaryLen = len(stringToBinary(passphrase))
  10. result = int(binaryLen - 1)
  11. while binaryLen % result > 0:
  12. result -= 1
  13. return result
  14.  
  15.  
  16. def slicePassPhrase(passphrase, sliceLength):
  17. binaryPhrase = stringToBinary(passphrase)
  18. # print('binary phrase: ',binaryPhrase)
  19. index = 0
  20. wordsArray = []
  21. while index < len(binaryPhrase):
  22. wordsArray.append(binaryPhrase[index:index + sliceLength])
  23. index += sliceLength
  24. # print(wordsArray)
  25. return wordsArray
  26.  
  27.  
  28. def sliceBinaryPhrase(binaryPhrase, sliceLength):
  29. # binaryPhrase = stringToBinary(passphrase)
  30. # print('binary phrase: ',binaryPhrase)
  31. index = 0
  32. wordsArray = []
  33. while index < len(binaryPhrase):
  34. wordsArray.append(binaryPhrase[index:index + sliceLength])
  35. index += sliceLength
  36. # print(wordsArray)
  37. return wordsArray
  38.  
  39.  
  40. def makeNumArray(wordsArray):
  41. numArray = []
  42. for word in wordsArray:
  43. num = int(word)
  44. numArray.append(num)
  45. return numArray
  46.  
  47.  
  48. def hashNumArray(numArray, sliceLength):
  49. result = 12
  50. for num in numArray:
  51. result *= num
  52. return result % (2**sliceLength)
  53.  
  54.  
  55. def digest(passphrase, debug):
  56. sliceLength = getSliceLength(passphrase)
  57. result = hashNumArray(makeNumArray(slicePassPhrase(
  58. stringToBinary(passphrase), sliceLength)), sliceLength)
  59. if debug:
  60. print('hash: ', result)
  61. return result
  62.  
  63. def bruteforce(hashedData):
  64. charset = ""
  65. charset += ascii_lowercase
  66. charset += ascii_uppercase
  67. charset += digits
  68. min_length = 8
  69. counter = 0
  70. found = False
  71. for length in range(int(min_length), int(min_length) + 1):
  72. for attempt in product(charset, repeat=length):
  73. testData = "".join(attempt)
  74. testHash = digest(testData, False)
  75. counter += 1
  76. if counter % 1000 == 0:
  77. print('already tried: ', counter, 'times')
  78. if hashedData == testHash:
  79. print('found collistion ', testData, 'in ', counter, ' runs')
  80. break
  81. if found:
  82. break
  83.  
  84. def main():
  85. data = sys.argv[1]
  86. hashed = digest(data, True)
  87. bruteforce(hashed)
  88.  
  89. if __name__ == "__main__":
  90. main()
Add Comment
Please, Sign In to add comment