src/egonomy/utils/__init__.py
author ymh <ymh.work@gmail.com>
Fri, 21 Mar 2014 16:25:21 +0100
changeset 271 4e7178ce5688
parent 105 2b004344ebf2
permissions -rw-r--r--
Implement single sign on with egonomy - mobenfact
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
271
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     1
import base64
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     2
import datetime
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     3
import hashlib
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     4
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     5
from Crypto.Cipher import AES  # encryption library
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     6
from django.conf import settings
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     7
import pytz
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     8
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
     9
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    10
def unix_time(dt):
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    11
    epoch = datetime.datetime.utcfromtimestamp(0).replace(tzinfo=pytz.UTC)
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    12
    delta = dt - epoch
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    13
    return delta.total_seconds()
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    14
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    15
BLOCK_SIZE = 32
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    16
# the character used for padding--with a block cipher such as AES, the value
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    17
# you encrypt must be a multiple of BLOCK_SIZE in length.  This character is
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    18
# used to ensure that your value is always a multiple of BLOCK_SIZE
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    19
PADDING = '{'
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    20
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    21
# one-liner to sufficiently pad the text to be encrypted
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    22
pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    23
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    24
# create a cipher object using the random secret
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    25
cipher = AES.new(hashlib.md5(settings.SECRET_KEY).hexdigest())
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    26
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    27
# one-liners to encrypt/encode and decrypt/decode a string
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    28
# encrypt with AES, encode with base64
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    29
encodeAES = lambda s: base64.urlsafe_b64encode(cipher.encrypt(pad(s)))
4e7178ce5688 Implement single sign on with egonomy - mobenfact
ymh <ymh.work@gmail.com>
parents: 105
diff changeset
    30
decodeAES = lambda e: cipher.decrypt(base64.urlsafe_b64decode(e)).rstrip(PADDING)