Skip to content

Instantly share code, notes, and snippets.

@alejandrobernardis
Created April 8, 2013 04:10
Show Gist options
  • Select an option

  • Save alejandrobernardis/5334183 to your computer and use it in GitHub Desktop.

Select an option

Save alejandrobernardis/5334183 to your computer and use it in GitHub Desktop.
Python 3 Migrate ... :D
import base64
import datetime
import string
import hashlib
from random import choice
def secret_key(length=64):
h = '%s-%s-%s-%s' % (
datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S%f'),
string.ascii_letters, string.digits, string.punctuation)
return ''.join([choice(h) for _ in range(length)])
def secret_key_b64(length=64):
return base64.b64encode(secret_key(length).encode())
def activation_key(username, email):
h = hashlib.md5()
h.update(('%s-%s-%s' % (username, email, secret_key())).encode())
return h.hexdigest()
def activation_key_b64(username, email):
return base64.b64encode(activation_key(username, email).encode())
def token(length=32, include_date=False):
h = hashlib.md5()
h.update(secret_key(length).encode())
v = ''.join([choice(h.hexdigest()) for _ in range(length)])
if include_date:
d = datetime.datetime.utcnow().strftime('%Y%m%d%H%M%S%f')
v = v[0:length - len(d)] + d
return v
def token_b64(length=32, include_date=False):
return base64.b64encode(token(length, include_date).encode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment