source: publico/il.spdo/trunk/PasteScript-1.7.4.2-py2.6.egg/paste/script/util/secret.py @ 5327

Última Alteração nesse arquivo desde 5327 foi 5327, incluída por fabianosantos, 8 anos atrás

Import inicial.

File size: 989 bytes
Linha 
1# (c) 2005 Ian Bicking and contributors; written for Paste (http://pythonpaste.org)
2# Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
3"""
4Create random secrets.
5"""
6
7import os
8import random
9
10def random_bytes(length):
11    """
12    Return a string of the given length.  Uses ``os.urandom`` if it
13    can, or just pseudo-random numbers otherwise.
14    """
15    try:
16        return os.urandom(length)
17    except AttributeError:
18        return ''.join([
19            chr(random.randrange(256)) for i in xrange(length)])
20
21def secret_string(length=25):
22    """
23    Returns a random string of the given length.  The string
24    is a base64-encoded version of a set of random bytes, truncated
25    to the given length (and without any newlines).
26    """
27    s = random_bytes(length).encode('base64')
28    for badchar in '\n\r=':
29        s = s.replace(badchar, '')
30    # We're wasting some characters here.  But random characters are
31    # cheap ;)
32    return s[:length]
Note: Veja TracBrowser para ajuda no uso do navegador do trac.
 

The contents and data of this website are published under license:
Creative Commons 4.0 Brasil - Atribuir Fonte - Compartilhar Igual.