~hopem/charm-helpers/fix-ssl-install-from-config

« back to all changes in this revision

Viewing changes to charmhelpers/core/host.py

  • Committer: Liam Young
  • Date: 2015-03-12 11:31:43 UTC
  • mfrom: (333.1.2 pwgen-system-random)
  • Revision ID: liam.young@canonical.com-20150312113143-p8pyb08ixcjd4fqq
[adam-collard, r=gnuoy] Switch pwgen() code to using random.SystemRandom() instead of the default PRNG.

Show diffs side-by-side

added added

removed removed

Lines of Context:
339
339
def pwgen(length=None):
340
340
    """Generate a random pasword."""
341
341
    if length is None:
 
342
        # A random length is ok to use a weak PRNG
342
343
        length = random.choice(range(35, 45))
343
344
    alphanumeric_chars = [
344
345
        l for l in (string.ascii_letters + string.digits)
345
346
        if l not in 'l0QD1vAEIOUaeiou']
 
347
    # Use a crypto-friendly PRNG (e.g. /dev/urandom) for making the
 
348
    # actual password
 
349
    random_generator = random.SystemRandom()
346
350
    random_chars = [
347
 
        random.choice(alphanumeric_chars) for _ in range(length)]
 
351
        random_generator.choice(alphanumeric_chars) for _ in range(length)]
348
352
    return(''.join(random_chars))
349
353
 
350
354