Skip to content

Commit

Permalink
Generate cryptographically secure secrets
Browse files Browse the repository at this point in the history
 * Use os.urandom for generation
 * Store secrets using base64 encoding
  • Loading branch information
ekarulf committed Mar 22, 2011
1 parent 4e39895 commit c88bf8f
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/application/generate_keys.py
Expand Up @@ -16,20 +16,22 @@
"""

import base64
import string
import os
import os.path

from optparse import OptionParser
from random import choice
from string import Template


# File settings
file_name = 'secret_keys.py'
file_template = Template('''
# CSRF- and Session keys
CSRF_SECRET_KEY = '$csrf_key'
SESSION_KEY = '$session_key'
file_template = Template('''# CSRF- and Session keys
import base64
CSRF_SECRET_KEY = base64.b64decode('$csrf_key')
SESSION_KEY = base64.b64decode('$session_key')
''')


Expand All @@ -44,8 +46,8 @@

def generate_randomkey(length):
"""Generate random key, givin a number of characters"""
chars = string.letters + string.digits
return ''.join([choice(chars) for i in range(length)])
secret = os.urandom(length)
return base64.b64encode(secret)


def write_file(contents):
Expand Down

0 comments on commit c88bf8f

Please sign in to comment.