aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/built-in/index.rst27
-rw-r--r--src/edify/library/__init__.py1
-rw-r--r--src/edify/library/password.py30
-rw-r--r--tests/test_password.py9
4 files changed, 67 insertions, 0 deletions
diff --git a/docs/built-in/index.rst b/docs/built-in/index.rst
index 91b5d4e..d63d4c2 100644
--- a/docs/built-in/index.rst
+++ b/docs/built-in/index.rst
@@ -960,5 +960,32 @@ Here's an example of how to use the ``guid`` function:
guid('{51d52cf1-83c9-4f02-b117-703ecb728b74}') # returns True
guid('{51d52cf1-83c9-4f02-b117-703ecb728-b74}') # returns False
+password(password: str, min_length?: int, max_length?: int, min_upper?: int, min_lower?: int, min_digit?: int, min_special?: int, special_chars?: str)
+------------------------------------------------------------------------------------------------------------------------------------------------------------
+The ``password`` function validates a password string. The function returns ``True`` if the string is a valid password, and ``False`` otherwise.
+
+The ``password`` function takes the following arguments:
+
+ * ``password``: The password string to validate.
+ * ``min_length``: The minimum length of the password. The default value is 8.
+ * ``max_length``: The maximum length of the password. The default value is 64.
+ * ``min_upper``: The minimum number of uppercase characters in the password. The default value is 1.
+ * ``min_lower``: The minimum number of lowercase characters in the password. The default value is 1.
+ * ``min_digit``: The minimum number of digits in the password. The default value is 1.
+ * ``min_special``: The minimum number of special characters in the password. The default value is 1.
+ * ``special_chars``: The special characters to use in the password. The default value is ``!@#$%^&*()_+-=[]{}|;':\",./<>?``.
+
+Here's an example of how to use the ``password`` function:
+
+.. code-block:: python
+
+ from edify.library import password
+
+ password('password') # returns False
+ password("Password123!") # returns True
+ password("Password123!", max_length=8) # returns False
+ password("Password123!", min_upper=2) # returns False
+ password("password", min_upper=0, min_digit=0, min_special=0) # returns True
+ password("pass@#1", min_special=1, special_chars="!", min_digit=0, min_upper=0, min_length=4) # returns False
diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py
index fe1f116..62d6528 100644
--- a/src/edify/library/__init__.py
+++ b/src/edify/library/__init__.py
@@ -12,3 +12,4 @@ from .url import url
from .uuid import uuid
from .zip import zip
from .guid import guid
+from .password import password
diff --git a/src/edify/library/password.py b/src/edify/library/password.py
new file mode 100644
index 0000000..85ef09c
--- /dev/null
+++ b/src/edify/library/password.py
@@ -0,0 +1,30 @@
+import re
+
+def password(password: str, min_length: int = 8, max_length: int = 64, min_upper: int = 1, min_lower: int = 1, min_digit: int = 1, min_special: int = 1, special_chars: str = "!@#$%^&*()_+-=[]{}|;':\",./<>?") -> bool:
+ """Check if the given string is a valid password.
+
+ Args:
+ password (str): The string to check.
+ min_length (int): The minimum length of the password.
+ max_length (int): The maximum length of the password.
+ min_upper (int): The minimum number of upper case characters.
+ min_lower (int): The minimum number of lower case characters.
+ min_digit (int): The minimum number of digits.
+ min_special (int): The minimum number of special characters.
+ special_chars (str): The special characters to check for.
+
+ Returns:
+ bool: True if the string is a valid password, False otherwise.
+ """
+ if len(password) < min_length or len(password) > max_length:
+ return False
+
+ upper = re.findall("[A-Z]", password or "")
+ lower = re.findall("[a-z]", password or "")
+ digit = re.findall("[0-9]", password or "")
+ special = [c for c in password if c in special_chars]
+
+ if len(upper) < min_upper or len(lower) < min_lower or len(digit) < min_digit or len(special) < min_special:
+ return False
+
+ return True
diff --git a/tests/test_password.py b/tests/test_password.py
new file mode 100644
index 0000000..b4fc7fb
--- /dev/null
+++ b/tests/test_password.py
@@ -0,0 +1,9 @@
+from edify.library import password
+
+def test_password():
+ assert password("password") == False
+ assert password("Password123!") == True
+ assert password("Password123!", max_length=8) == False
+ assert password("Password123!", min_upper=2) == False
+ assert password("password", min_upper=0, min_digit=0, min_special=0) == True
+ assert password("pass@#1", min_special=1, special_chars="!", min_digit=0, min_upper=0, min_length=4) == False