Skip to content

Instantly share code, notes, and snippets.

@hiranp
Last active December 29, 2023 18:39
Show Gist options
  • Save hiranp/56adf3ca9f653a848a75f92fae62a837 to your computer and use it in GitHub Desktop.
Save hiranp/56adf3ca9f653a848a75f92fae62a837 to your computer and use it in GitHub Desktop.
A pass phrase generator for Oracle database passwords
import os
import random
import re
import string
## OCI Console:
## Password must be 9 to 30 characters and contain at least 2 uppercase, 2 lowercase, 2 special, and 2 numeric characters.
## The special characters must be _, #, or -.
MAX_LENGTH = 30
# Load the word list
# https://github.com/first20hours/google-10000-english
CWD = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(CWD, "google-10000-english.txt")) as f:
words = [line.strip() for line in f]
# Filter the word list
words = [word for word in words if 1 <= len(word) <= 15]
def generate_password():
# Generate a passphrase of 3 words
passphrase = [random.choice(words) for _ in range(3)]
# Capitalize the first letter of each word
passphrase = [
re.sub(r"\b\w", lambda match: match.group().upper(), word)
for word in passphrase
]
# Generate 3 random numbers
numbers = "".join(random.choice(string.digits) for _ in range(3))
# Generate 2 random uppercase letters
letters = "".join(random.choice(string.ascii_uppercase) for _ in range(2))
# Append numbers and letters to the passphrase if they are not present
if not any(char.isdigit() for word in passphrase for char in word):
passphrase.append(numbers)
if not any(char.isupper() for word in passphrase for char in word):
passphrase.append(letters)
# Join the words with a random character
passphrase = "".join(word + random.choice("_#-") for word in passphrase)[:-1]
# If the length of the passphrase exceeds MAX_LENGTH, generate a new password
if len(passphrase) > MAX_LENGTH:
return generate_password()
return passphrase
print(generate_password())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment