Password generation, validation and more random things

Random Alpha-numeric passwords

import random, string; "".join([random.choice(string.ascii_letters + string.digits) for i in range(8)])

random.choice(string.ascii_letters + string.digits) picks a character from string.ascii_letter and string.digits. Iterating over it will return 8 random alpha-numeric characters which can be used as a password.

Pronounceable passwords

import random;words=open('/usr/share/dict/words').read().split(); "-".join([random.choice(words) for _ in range(4)])

Creates a random ‘-‘ separated 4 word password from the given input text file to be used as a password

Passwords with alternate vowels

import random, string, itertools;
"".join(itertools.chain(*zip([random.choice(string.ascii_lowercase) for _ in range(6)],  [random.choice('aeiou') for _ in range(6)])))

Get a password where every alternate character is a vowel.

Validate password complexity

Validate that a password is least 8 chars, and contains at least 3 of lowercase, uppercase, numbers and symbols.

Caesar Cipher

from string import ascii_uppercase as upr, ascii_lowercase as lwr

def caesar_cipher(txt, offset=1):
    _map = dict(list(zip(upr, upr[offset:] + upr[:offset])) + list((zip(lwr, lwr[offset:] + lwr[:offset]))))
    return "".join([_map[el] if el in upr+lwr else el for el in txt])

Caesar cipher is one of the earliest known and simplest ciphers. It is a substitution cipher with a given offset. Each letter is shifted by the offset, default value is 1.

from string import ascii_uppercase as upr, ascii_lowercase as lwr

Standard python imports, not much to see here.

list(zip(upr, upr[offset:] + upr[:offset]))

This gives a us a list of two-tuples similar to the one shown below based on the value of offset

We do the same thing for lower case letters.

_map = dict(list(zip(upr, upr[offset:] + upr[:offset])) + list((zip(lwr, lwr[offset:] + lwr[:offset]))))

Now we lookup the substitution letter from the map and switch join the together to get the Rot-13 word.

return "".join([_map[el] if el in upr+lwr else el for el in txt])

Generate a UUID

import uuid
print(uuid.uuid4())

Generates a random uuid with 36 characters.

Generate a url safe UUID

(For example for use as a slug)

import uuid, base64
base64.urlsafe_b64encode(uuid.uuid4().bytes)

This will give a 24 character UUID. If you need a shorter one, you can take a slice The code below gives you an unique 16 character UUID

import uuid, base64
base64.urlsafe_b64encode(uuid.uuid4().bytes)[:16]