Guest User

Untitled

a guest
Apr 23rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. import hashlib
  2.  
  3. """
  4. A gift from ki113d to feudin :)
  5. I thought I would show you a... cleaner
  6. more OOP approach to your last example
  7. in Rhynos thread.
  8. """
  9.  
  10. class LoginDetails:
  11.     """ Example class, not much use really. """
  12.    
  13.     def __init__(self, username, passwd):
  14.         """ Constructor.
  15.            Creates member variables used later.
  16.        """
  17.         self.username = username
  18.         self.password = passwd
  19.    
  20.     def __getEncType(self, encType):
  21.         """ This is a private method, only way to access
  22.            it from outside the class is as follows.
  23.            
  24.            ld = LoginDetails(foo, bar)
  25.            ld._LoginDetails__getEncType('md5')
  26.            
  27.            This method uses a hardcoded dictionary to
  28.            determine what encryption type to use
  29.            depending on the parameter.
  30.            
  31.            The key for the dictionary is the name
  32.            of a hash algorithm,
  33.            
  34.            The value is an object used in encryption.
  35.            It's better then swapping names around etc.
  36.        """
  37.         hashes = {'md5': hashlib.md5(),
  38.                   'sha1': hashlib.sha1(),
  39.                   'sha224': hashlib.sha224(),
  40.                   'sha256': hashlib.sha256()}
  41.         if not encType in hashes.keys():
  42.             # The key is not in the dictionary so let the programmer who
  43.             # is using the class know he is a douche and should read the
  44.             # documentation on the api a bit better :P
  45.             raise Exception("Hash algorithm {0} isn't supported!".format(
  46.                             encType))
  47.         return hashes[encType]
  48.    
  49.     def encryptData(self, encType='md5'):
  50.         """ The only public method of the class.
  51.            This method encrypts and returns the
  52.            username and password which were specified
  53.            in the constructor whilst creating the class.
  54.            
  55.            encType is defaulted to md5 but can be changed.
  56.        """
  57.         # Retrieve an object used to encrypt the information
  58.         h1 = self.__getEncType(encType)
  59.         # Literally copy the object so we can encrypt a second piece
  60.         # of data without calling __getEncType again.
  61.         h2 = h1
  62.         # h1.update(str) just adds to the string that needs to be encrypted.
  63.         h1.update(str.encode(self.username))
  64.         h2.update(str.encode(self.password))
  65.         # Return a list containing the username and password.
  66.         return (h1.hexdigest(), h2.hexdigest())
  67.  
  68. if __name__ == '__main__':
  69.     username = input('Username: ')
  70.     passwd = input('Password: ')
  71.     ld = LoginDetails(username, passwd) # Initialize the object.
  72.     # A list of encryption type, lol is the worlds best hash, it is
  73.     # so good infact that it throws and error saying it isn't supported,
  74.     # this was for testing purposes :P
  75.     encToTry = ('md5', 'sha1', 'sha224', 'sha256', 'lol')
  76.     # Iterate through the values of encToTry and attempt to encrypt
  77.     # the username and password each time.
  78.     for i in encToTry:
  79.         try:
  80.             data = ld.encryptData(i)
  81.             # Print a cool message lol
  82.             print("""
  83. {0}:
  84.  Username: {1}
  85.  Password: {2}
  86. """.format(i.upper(), data[0], data[1]))
  87.         except Exception as e:
  88.             # Obviously because lol isn't a hash algorithm...
  89.             print(e)
Add Comment
Please, Sign In to add comment