Generate A Django Secret Key
$ python -c 'import random; print '.join([random.choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])' |
This is a web tool to generate SECRETKEY and also have a Django package that does this simply with a command. Djecrety is a Django secret key generator. This is a web tool to generate SECRETKEY and also have a Django package that does this simply with a command. If you put this in your project, each separate Django process you have will have a different key. This means for instance that if you run multiple concurrent Django processes (or in separate servers), they will get different secret keys, so stuff signed from one instance won't work on another. Generate migration files without Django version and timestamp header. Secretkey – a random key for the SECRETKEY setting; docsversion – the version of the documentation: 'dev' or '1.x' djangoversion – the version of Django, e.g. '2.0.3' Please also see the rendering warning as mentioned for startapp.
commented Aug 22, 2018
For Python 3, this should work:
|
commented Jun 2, 2019
commented Jun 3, 2019
I think you should use parenthesis while using |
commented Oct 13, 2019
Generate A Django Secret Key Generator
Seems exceedingly unlikely the In Linux you can read from /dev/urandom or /dev/random(slower) and see someone suggesting |
commented Feb 27, 2020 • edited
edited
Generate Django Secret Key
#!/usr/bin/env python |
# coding: utf-8 |
''Generate a 50-char random string, adequate for Django's ``SECRET_KEY``. |
source: part of |
https://github.com/django/django/blob/1.8.5/django/utils/crypto.py |
'' |
from __future__ importabsolute_import, print_function, unicode_literals |
importhashlib |
importrandom |
importtime |
try: |
random=random.SystemRandom() |
using_sysrandom=True |
exceptNotImplementedError: |
importwarnings |
warnings.warn('A secure pseudo-random number generator is not available ' |
'on your system. Falling back to Mersenne Twister.') |
using_sysrandom=False |
defget_random_string(length=12, |
allowed_chars='abcdefghijklmnopqrstuvwxyz' |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'): |
'' |
Returns a securely generated random string. |
The default length of 12 with the a-z, A-Z, 0-9 character set returns |
a 71-bit value. log_2((26+26+10)^12) =~ 71 bits |
'' |
ifnotusing_sysrandom: |
# This is ugly, and a hack, but it makes things better than |
# the alternative of predictability. This re-seeds the PRNG |
# using a value that is hard for an attacker to predict, every |
# time a random string is required. This may change the |
# properties of the chosen random sequence slightly, but this |
# is better than absolute predictability. |
random.seed( |
hashlib.sha256( |
('%s%s%s'% ( |
random.getstate(), |
time.time(), |
# settings.SECRET_KEY, |
', |
)).encode('utf-8') |
).digest()) |
return'.join(random.choice(allowed_chars) foriinrange(length)) |
defmain(): |
# chars and length as defined in Django command 'startproject' |
# https://github.com/django/django/blob/1.8.5/django/core/management/commands/startproject.py#L30 |
chars='abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' |
returnget_random_string(50, chars) |
if__name__'__main__': |
print(main()) |