Skip to content

Instantly share code, notes, and snippets.

@coffebar
Forked from airtonix/generate_key.py
Last active August 29, 2015 14:07
Show Gist options
  • Save coffebar/1500f494cec751ac0299 to your computer and use it in GitHub Desktop.
Save coffebar/1500f494cec751ac0299 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from base64 import urlsafe_b64encode as b64encode
import random
random.seed()
pattern = "%%0%dX"
junk_len = 1024
def generate_key(max_length, seed_length):
"""
Generate a Base64-encoded 'random' key by hashing the data.
data is a tuple of seeding values. Pass arbitrary encoder and
digester for specific hashing and formatting of keys
"""
encoding = 'utf-8'
junk = (pattern % (junk_len * 2)) % random.getrandbits(junk_len * seed_length)
key = bytes(junk, encoding=encoding)
return b64encode(key)[:max_length].decode(encoding)
def random_hash():
return generate_key(96, 1024)
if __name__ == "__main__":
for count in range(150):
print(random_hash())
#
# Automatically Build a Secret Key
#
# We do this so we can put base/settings/key.py into .gitignore for this project template.
# You'd obviously remove that constraint for your own projects.
try:
from .key import SECRET_KEY
except ImportError:
from base.lib.generate_key import generate_key
with open(os.path.join(BASE_DIR, 'this_dir', "key.py"), "w") as secret_key_file:
secret_key_file.write('SECRET_KEY = "{0}"'.format(generate_key(40, 128)))
from .key import SECRET_KEY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment