Created
March 12, 2011 19:01
-
-
Save james-prickett/867477 to your computer and use it in GitHub Desktop.
a simple utility to enccrypt / decrypt user id's
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from Crypto.Cipher import AES | |
| import base64 | |
| import urllib | |
| class SecurityUtils(object): | |
| key = '^&#$SuP3rCR@zy$t%piDSEcu1+yK3Y$%&#' | |
| mode = AES.MODE_CBC | |
| pad_character = 'X' | |
| @classmethod | |
| def encode_user_id(cls, user_id): | |
| aes = AES.new(cls.key, cls.mode) | |
| padded_key = str(user_id).ljust(32, cls.pad_character) | |
| encrypted_key = aes.encrypt(padded_key) | |
| encoded_key = base64.b64encode(encrypted_key) | |
| quoted_key = urllib.quote(encoded_key, '') | |
| return quoted_key | |
| @classmethod | |
| def decode_user_id(cls, encoded_user_id): | |
| aes = AES.new(cls.key, cls.mode) | |
| unquoted_key = urllib.unquote(encoded_user_id) | |
| unencoded_key = base64.b64decode(unquoted_k) | |
| user_id = aes.decrypt(unencoded_key) | |
| user_id = user_id[: user_id.index(cls.pad_character)] | |
| return int(user_id) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment