You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_import.py 2.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from secrets import token_hex
  2. from lesspass import password
  3. def generate_random_entropy():
  4. return int(token_hex(32), 16)
  5. CHARACTER_SUBSETS = {
  6. "lowercase": "abcdefghijklmnopqrstuvwxyz",
  7. "uppercase": "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  8. "digits": "0123456789",
  9. "symbols": "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
  10. }
  11. def reverse_entropy(entropy, remainder, modulo):
  12. return entropy - modulo - remainder
  13. def remove_at(i, s):
  14. return s[:i] + s[i + 1 :]
  15. def get_rule_len(rule):
  16. if rule == "lowercase":
  17. return 26
  18. if rule == "digits":
  19. return 10
  20. rules = [
  21. "lowercase",
  22. "digits",
  23. ]
  24. old_password = "bar4"
  25. chars_remaining = "%s" % old_password
  26. one_char_per_rule = []
  27. for rule in reversed(rules):
  28. for i, c in enumerate(old_password):
  29. print(f"char {c} index {i}")
  30. rule_pool = CHARACTER_SUBSETS[rule]
  31. if c in rule_pool:
  32. print(f"{c} in {rule_pool}")
  33. char_per_rule = (c, rule, i)
  34. print(char_per_rule)
  35. one_char_per_rule.append(char_per_rule)
  36. chars_remaining = remove_at(i, chars_remaining)
  37. break
  38. else:
  39. print(f"{c} not in {rule_pool}. skipping")
  40. pass
  41. print("-------")
  42. print("chars to insert:", one_char_per_rule)
  43. entropy = 654762009171870546215294809657
  44. j = 7
  45. print(f"e{j}:", entropy)
  46. j -= 1
  47. for i, rule in enumerate(one_char_per_rule):
  48. print(f"e * ({len(old_password) - i - 1}) + {rule[2]}")
  49. entropy = entropy * (len(old_password) - i - 1) + rule[2]
  50. print(f"e{j}:", entropy)
  51. j -= 1
  52. for i, rule in enumerate(one_char_per_rule):
  53. _pool_of_char = CHARACTER_SUBSETS[rule[1]]
  54. print("len _pool_of_char:", len(_pool_of_char))
  55. position = _pool_of_char.find(rule[0])
  56. print("position:", position)
  57. entropy = entropy * len(_pool_of_char) + position
  58. print(f"e{j}:", entropy)
  59. j -= 1
  60. pool_of_char = CHARACTER_SUBSETS["lowercase"] + CHARACTER_SUBSETS["digits"]
  61. # print(chars_remaining)
  62. while chars_remaining != "":
  63. last_char = chars_remaining[-1]
  64. chars_remaining = chars_remaining[:-1]
  65. entropy = entropy * len(pool_of_char) + pool_of_char.find(last_char)
  66. print(f"e{j}:", entropy)
  67. j -= 1
  68. print(entropy)
  69. print("x" * 100)
  70. print(
  71. password._render_password(
  72. entropy,
  73. {
  74. "lowercase": True,
  75. "uppercase": False,
  76. "digits": True,
  77. "symbols": False,
  78. "length": 5,
  79. },
  80. )
  81. )