Skip to content

Commit

Permalink
add password validator hack
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypernova101 committed Sep 26, 2024
1 parent 8eab36d commit a33cfb3
Showing 1 changed file with 158 additions and 0 deletions.
158 changes: 158 additions & 0 deletions _notebooks/Foundation/2024-09-24-team_teach.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,164 @@
"source": [
" - Use Case: Combine a list of strings into a single string, helpful for reformatting content"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sample Hack - Password Validator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Simple Password Validator"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password is valid! Here’s a fun version: HelloWorldabc\n"
]
}
],
"source": [
"def password_validator(password):\n",
" if len(password) < 8:\n",
" return \"Password too short. Must be at least 8 characters.\"\n",
"\n",
" if password == password.lower() or password == password.upper():\n",
" return \"Password must contain both uppercase and lowercase letters.\"\n",
"\n",
" if not any(char.isdigit() for char in password):\n",
" return \"Password must contain at least one number.\"\n",
"\n",
" # Optional\n",
" password = password.replace(\"123\", \"abc\")\n",
"\n",
" words = password.split(\" \")\n",
" customized_password = \"-\".join(words)\n",
"\n",
" return f\"Password is valid! Here’s a fun version: {customized_password}\"\n",
"\n",
"# Example usage\n",
"password = \"HelloWorld123\"\n",
"print(password_validator(password))\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password must contain both uppercase and lowercase letters.\n"
]
}
],
"source": [
"print(password_validator(\"HELLO123\"))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password is valid! Here’s a fun version: Helloabc\n"
]
}
],
"source": [
"print(password_validator(\"Hello123\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Advanced Password Validator"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Password is valid and Strong! Here’s a fun version: HiWorld13475!\n"
]
}
],
"source": [
"import re\n",
"\n",
"def password_validator(password):\n",
" if len(password) < 8:\n",
" return \"Password too short. Must be at least 8 characters.\"\n",
" \n",
" if password == password.lower() or password == password.upper():\n",
" return \"Password must contain both uppercase and lowercase letters.\"\n",
" \n",
" if not any(char.isdigit() for char in password):\n",
" return \"Password must contain at least one number.\"\n",
" \n",
" if not re.search(r\"[!@#$%^&*()_+]\", password):\n",
" return \"Password must contain at least one special character (e.g. !, @, #, etc.)\"\n",
" \n",
" common_passwords = [\"password\", \"123456\", \"letmein\", \"qwerty\"]\n",
" if password.lower() in common_passwords:\n",
" return \"Password is too common. Choose something less predictable.\"\n",
" \n",
" sequential_patterns = [\"123\", \"abc\", \"xyz\"]\n",
" for pattern in sequential_patterns:\n",
" if pattern in password.lower():\n",
" return \"Password should not contain sequential characters like '123' or 'abc'.\"\n",
" \n",
" score = 0\n",
" if len(password) >= 10:\n",
" score += 1\n",
" if re.search(r\"[A-Z]\", password) and re.search(r\"[a-z]\", password):\n",
" score += 1\n",
" if re.search(r\"\\d\", password):\n",
" score += 1\n",
" if re.search(r\"[!@#$%^&*()_+]\", password):\n",
" score += 1\n",
"\n",
" strength = \"Weak\"\n",
" if score == 2:\n",
" strength = \"Medium\"\n",
" elif score >= 3:\n",
" strength = \"Strong\"\n",
"\n",
" password = password.replace(\"Hello\", \"Hi\")\n",
" words = password.split(\" \")\n",
" customized_password = \"-\".join(words)\n",
"\n",
" return f\"Password is valid and {strength}! Here’s a fun version: {customized_password}\"\n",
"\n",
"# Example usage\n",
"password = \"HelloWorld13475!\"\n",
"print(password_validator(password))\n",
"\n"
]
}
],
"metadata": {
Expand Down

0 comments on commit a33cfb3

Please sign in to comment.