Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. class SecureString(str):
  2. MASKED = '*****'
  3.  
  4. @classmethod
  5. def __masked__(cls, *args, **kwargs):
  6. return SecureString.MASKED
  7.  
  8. def __repr__(self):
  9. return SecureString.__masked__()
  10.  
  11. def __str__(self):
  12. return SecureString.__masked__()
  13.  
  14. def __getitem__(self, item):
  15. if isinstance(item, slice): # detect slicing
  16. return SecureString.__masked__()
  17.  
  18. def __getattribute__(self, item):
  19. if 'get' == item: # Return normal string
  20. return super(SecureString, self).__str__
  21. else: # Return masked string
  22. return SecureString.__masked__
  23.  
  24. password = SecureString('abc')
  25.  
  26. # good
  27. print('\n>>> positive')
  28. print(password.get())
  29. print(password.get().lower())
  30.  
  31. # bad
  32. print('\n>>> negative')
  33. print(password)
  34. print(password.lower())
  35. print(password.upper())
  36. print(repr(password))
  37. print(str(password))
  38. print(password[::-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement