2012-07-05 13:07:49 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
2016-07-26 02:47:45 +00:00
|
|
|
# Copyright (c) 2012-2016 Seafile Ltd.
|
|
|
|
|
2015-06-15 02:44:54 +00:00
|
|
|
import hashlib
|
2012-07-05 13:07:49 +00:00
|
|
|
import random
|
2015-06-15 02:44:54 +00:00
|
|
|
import sys
|
|
|
|
import time
|
2012-07-05 13:07:49 +00:00
|
|
|
|
2015-06-15 02:44:54 +00:00
|
|
|
# Use the system PRNG if possible
|
|
|
|
try:
|
|
|
|
random = random.SystemRandom()
|
|
|
|
using_sysrandom = True
|
|
|
|
except NotImplementedError:
|
|
|
|
import warnings
|
|
|
|
warnings.warn('A secure pseudo-random number generator is not available '
|
|
|
|
'on your system. Falling back to Mersenne Twister.')
|
|
|
|
using_sysrandom = False
|
2012-07-05 13:07:49 +00:00
|
|
|
|
2015-06-15 02:44:54 +00:00
|
|
|
def get_random_string(length=12,
|
|
|
|
allowed_chars='abcdefghijklmnopqrstuvwxyz'
|
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'):
|
2012-07-05 13:07:49 +00:00
|
|
|
"""
|
2015-06-15 02:44:54 +00:00
|
|
|
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
|
2012-07-05 13:07:49 +00:00
|
|
|
"""
|
2015-06-15 02:44:54 +00:00
|
|
|
if not using_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(),
|
|
|
|
'')).encode('utf-8')
|
|
|
|
).digest())
|
|
|
|
return ''.join(random.choice(allowed_chars) for i in range(length))
|
2012-07-05 13:07:49 +00:00
|
|
|
|
2012-07-06 11:14:07 +00:00
|
|
|
if __name__ == "__main__":
|
2015-06-15 02:44:54 +00:00
|
|
|
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
|
|
|
|
key = get_random_string(50, chars)
|
2013-04-20 18:17:25 +00:00
|
|
|
if len(sys.argv) == 2:
|
|
|
|
fp = open(sys.argv[1], 'w')
|
|
|
|
fp.write("SECRET_KEY = \"%s\"\n" % key)
|
|
|
|
fp.close()
|
|
|
|
else:
|
2019-09-11 03:46:43 +00:00
|
|
|
print(key)
|