Guest User

Untitled

a guest
May 26th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import random
  2.  
  3. class Key:
  4.  
  5. def __init__(self, key=''):
  6. if key == '':
  7. self.key= self.generate()
  8. else:
  9. self.key = key.lower()
  10.  
  11. def verify(self):
  12. score = 0
  13. check_digit = self.key[0]
  14. check_digit_count = 0
  15. chunks = self.key.split('-')
  16. for chunk in chunks:
  17. if len(chunk) != 4:
  18. return False
  19. for char in chunk:
  20. if char == check_digit:
  21. check_digit_count += 1
  22. score += ord(char)
  23. if score == 1772 and check_digit_count == 5:
  24. return True
  25. return False
  26.  
  27. def generate(self):
  28. key = ''
  29. chunk = ''
  30. check_digit_count = 0
  31. alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890'
  32. while True:
  33. while len(key) < 25:
  34. char = random.choice(alphabet)
  35. key += char
  36. chunk += char
  37. if len(chunk) == 4:
  38. key += '-'
  39. chunk = ''
  40. key = key[:-1]
  41. if Key(key).verify():
  42. return key
  43. else:
  44. key = ''
  45.  
  46. def __str__(self):
  47. valid = 'Invalid'
  48. if self.verify():
  49. valid = 'Valid'
  50. return self.key.upper() + ':' + valid
Add Comment
Please, Sign In to add comment