diff options
author | xray7224 <xray7224@googlemail.com> | 2013-06-28 17:59:32 +0100 |
---|---|---|
committer | xray7224 <jessica@megworld.co.uk> | 2013-07-11 18:21:43 +0100 |
commit | 4990b47ce401dc86353a261825771a6811be4a8c (patch) | |
tree | 450facb956139ae4b1c99ab8d124241c187cb8c2 /mediagoblin/tools/crypto.py | |
parent | c840cb66180a77e630c261c21967a6afc87411e9 (diff) | |
download | mediagoblin-4990b47ce401dc86353a261825771a6811be4a8c.tar.lz mediagoblin-4990b47ce401dc86353a261825771a6811be4a8c.tar.xz mediagoblin-4990b47ce401dc86353a261825771a6811be4a8c.zip |
Working client registration
Diffstat (limited to 'mediagoblin/tools/crypto.py')
-rw-r--r-- | mediagoblin/tools/crypto.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/mediagoblin/tools/crypto.py b/mediagoblin/tools/crypto.py index 1379d21b..917e674c 100644 --- a/mediagoblin/tools/crypto.py +++ b/mediagoblin/tools/crypto.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import base64 +import string import errno import itsdangerous import logging @@ -24,6 +26,9 @@ from mediagoblin import mg_globals _log = logging.getLogger(__name__) +# produces base64 alphabet +alphabet = string.ascii_letters + "-_" +base = len(alphabet) # Use the system (hardware-based) random number generator if it exists. # -- this optimization is lifted from Django @@ -111,3 +116,13 @@ def get_timed_signer_url(namespace): assert __itsda_secret is not None return itsdangerous.URLSafeTimedSerializer(__itsda_secret, salt=namespace) + +def random_string(length): + """ Returns a URL safe base64 encoded crypographically strong string """ + rstring = "" + for i in range(length): + n = getrandbits(6) # 6 bytes = 2^6 = 64 + n = divmod(n, base)[1] + rstring += alphabet[n] + + return rstring |