No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

test_import.py 2.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import unittest
  2. from secrets import token_hex
  3. from lesspass import password
  4. def get_pool_of_char(password_profile):
  5. return "abcdefghijklmnopqrstuvwxyz"
  6. def get_rules(password_profile):
  7. return ["lowercase"]
  8. def get_rule_len(rule):
  9. return 26
  10. def generate_random_entropy():
  11. return int(token_hex(32), 16)
  12. def _rev_pseudo_insert_chars_to_add(entropy, old_password):
  13. rules = get_rules(old_password)
  14. chars_to_add = []
  15. for rule in rules:
  16. position = 0
  17. chars_to_add.append((old_password[0], rule,))
  18. old_password = old_password[1:]
  19. entropy = (entropy + 0) * len(old_password)
  20. return chars_to_add, old_password, entropy
  21. def _rev_chars_to_add(chars_to_add, entropy, pool_of_char):
  22. for char, rule in chars_to_add:
  23. entropy = entropy * get_rule_len(rule) + pool_of_char.find(char)
  24. return entropy
  25. def _rev_consume_entropy(old_password, entropy, pool_of_char):
  26. while len(old_password) != 0:
  27. last_char = old_password[-1]
  28. old_password = old_password[:-1]
  29. entropy = entropy * len(pool_of_char) + pool_of_char.find(last_char)
  30. return entropy
  31. def reverse_entropy(old_password, password_profile):
  32. entropy = generate_random_entropy()
  33. e1 = entropy
  34. rules = get_rules(old_password)
  35. one_char_per_rule = []
  36. for rule in rules:
  37. position = 0
  38. one_char_per_rule.append((old_password[0], rule,))
  39. old_password = old_password[1:]
  40. entropy = (entropy + 0) * len(old_password)
  41. pool_of_char = get_pool_of_char(old_password)
  42. for char, rule in one_char_per_rule:
  43. entropy = entropy * get_rule_len(rule) + pool_of_char.find(char)
  44. while old_password != "":
  45. last_char = old_password[-1]
  46. old_password = old_password[:-1]
  47. entropy = entropy * len(pool_of_char) + pool_of_char.find(last_char)
  48. return entropy
  49. class TestImportPOC(unittest.TestCase):
  50. def test_can_reverse_entropy(self):
  51. password_profile = {
  52. "site": "lesspass.com",
  53. "login": "contact@lesspass.com",
  54. "lowercase": True,
  55. "uppercase": False,
  56. "digits": False,
  57. "symbols": False,
  58. "length": 11,
  59. "counter": 1,
  60. }
  61. master_password = "password"
  62. entropy = password._calc_entropy(password_profile, master_password)
  63. old_entropy = reverse_entropy("oldpassword", password_profile)
  64. oldpassword = password._render_password(old_entropy, password_profile)
  65. self.assertEqual(oldpassword, "oldpassword")