Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- python3 encoding notes
- """
- ################################################################################
- # python 2
- >>> import md5
- >>> secret_key = md5.new("rppowellemail@gmail.com").hexdigest()
- # python 3
- >>> import hashlib
- >>> secret_key = hashlib.md5("rppowellemail@gmail.com".encode()).hexdigest()
- >>> secret_key = hashlib.md5("rppowellemail@gmail.com".encode("ascii")).hexdigest()
- >>> secret_key = hashlib.md5("rppowellemail@gmail.com".encode("utf-8")).hexdigest()
- ################################################################################
- >>> import uuid
- >>> u = uuid.UUID(secret_key)
- >>> str(u)
- ################################################################################
- import base64
- >>> base64.b64encode("This is a test".encode()).decode()
- 'VGhpcyBpcyBhIHRlc3Q='
- >>> base64.b64encode("This is a test".encode("ascii"))
- b'VGhpcyBpcyBhIHRlc3Q='
- >>> base64.b64encode("This is a test".encode("utf-8"))
- b'VGhpcyBpcyBhIHRlc3Q='
- >>> base64.b64decode(
- ... base64.b64encode("This is a test".encode())
- ... ).decode()
- 'This is a test'
- ################################################################################
- # pip install Jasypt2Python
- >>> from j2p.JASYPT import Decryptor
- >>> decryptor = Decryptor(jasypt_password)
- >>> plaintext = decryptor.decrypt(some_encrypted_text)
- ################################################################################
- # pip install pycrypto
- >>> from Crypto.Cipher import AES
- >>> encryptor = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
- >>> message = "The answer is no" # len(message) == 16
- >>> ciphertext = encryptor.encrypt(message)
- >>> decryptor = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
- >>> decryptor.decrypt(ciphertext).decode()
- 'The answer is no'
- >>> from Crypto.Cipher import AES
- >>> encryptor = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
- >>> message = "This is a test."
- >>> ciphertext = encryptor.encrypt(message)
- >>> decryptor = AES.new('This is a key123', AES.MODE_CFB, 'This is an IV456')
- >>> decryptor.decrypt(ciphertext).decode()
- 'This is a test.'
- ################################################################################
- >>> jsondata = {'key':'value', 'numberarray':[1,2,3]}
- >>> jsondata
- {'key': 'value', 'numberarray': [1, 2, 3]}
- >>> jsonstring = json.dumps(jsondata)
- >>> jsonstring
- '{"key": "value", "numberarray": [1, 2, 3]}'
- >>> escapedjsonstring = json.dumps(jsonstring)
- >>> escapedjsonstring
- '"{\\"key\\": \\"value\\", \\"numberarray\\": [1, 2, 3]}"'
- >>> json.loads(escapedjsonstring)
- '{"key": "value", "numberarray": [1, 2, 3]}'
- >>> json.loads(json.loads(escapedjsonstring))
- {'key': 'value', 'numberarray': [1, 2, 3]}
Add Comment
Please, Sign In to add comment