إعادة كتابة مولد كلمة المرور

سياسة كلمة المرور


كلمات المرور خارج السياسة


لدي شعور بأنني كتبت وظيفة لإنشاء كلمات المرور خمس مرات بالفعل. وكان يفعل ذلك بشكل مختلف في كل مرة. والسبب في ذلك هو متطلبات كلمة المرور المختلفة لمشاريع وأدوات مختلفة. لن يكون هناك رمز معقد ، مجرد ملخص لحل جديد بسيط وصلني أمس.


لنبدأ بمتطلبات كلمة مرور بسيطة:


  • يجب أن تكون ذات طول تعسفي
  • يجب أن يتكون من أي أحرف مطبوعة

import string
import random
from typing import List

def generate_password(length: int) -> str:
     """
     Generate a password of a given `length`.
     """
     result: List[str] = []
     choices = string.printable #    ,    
     while len(result) < length:
         symbol = random.choice(string.printable)
         result.append(symbol)
     return "".join(result)

نحاول:


>>> generate_password(8)
... "1{k]/2)h"
>>> generate_password(13)
... "9ar|&:a+U]Il$"

حسنًا ، تم الانتهاء من المهمة ، يمكننا مشاهدة الصور بالقطط حتى نهاية يوم العمل.


السياسة المفاجئة


, , MyDB . , :


  • 8
  • - (!&? )
  • , bash-

, . :


  • , , -
  • ,


حسنًا ، هذا معقد بالفعل ، لذلك دعونا نبدأ بوظيفة generate_random_stringستولد ببساطة سلاسل عشوائية مما قدموه.


import string
import random
from typing import List

def generate_random_string(length: int, *choices: str) -> str:
    """
    Generate a string of a given `length`.

    The result has at least one symbol from each of `choices` if `length` allows.

    Arguments:
        length -- Result string length.
        choices -- Strings with available symbols.
    """
    if not choices:
        #        ,    
        choices = (string.ascii_letters, ) 

    #      
    all_choices = "".join(choices)
    result: List[str] = []
    choice_index = 0
    while len(result) < length:
        #      , 
        #        
        if choice_index < len(choices):
            symbol = random.choice(choices[choice_index])
            result.append(symbol)
            choice_index += 1
            continue

        #        
        symbol = random.choice(all_choices)
        result.append(symbol)

    #       
    random.shuffle(result)
    return "".join(result)

إذا دعنا نحاول:


>>> #    
>>> generate_random_string(8, string.digits)
... "59197550"
>>> #       
>>> generate_random_string(8, string.ascii_letters, "!") 
... "vIOWXN!o"

رائع ، حان الوقت لإنشاء كلمة مرور تلبي جميع متطلباتنا.


def generate_mydb_password(length: int) -> str:
    """
    Generate a random password for MyDB of a given `length`.

    The result has at least:
    - one uppercase letter
    - one lowercase letter
    - one digit
    - one special character

    Raises:
        ValueError -- If `length` is lesser than 8.
    """
    if length < 8:
        raise ValueError("Password length should be at least 8")

    return generate_random_string(
        length,
        string.ascii_uppercase, #      
        string.ascii_lowercase, #  
        string.digits, #  
        "!&?", #  -,    
    )

يبقى فقط للتحقق:


>>> generate_mydb_password(8)
... "P?P1&7zL"
>>> generate_mydb_password(13)
... "tR!QslK!Sl7EO"
>>> generate_mydb_password(2)
... ValueError: Password length should be at least 8

مجموع


لقد كتبنا أداة سهلة الفهم وفي نفس الوقت مولد كلمات مرور عشوائي ، ولا يزال هناك الكثير من الوقت حتى نهاية يوم العمل. إذا لم تكن هناك ثقة في المكتبة random، فيمكنك استبدالها بالأخرى التي تحبها.


شكرا للانتباه!

Source: https://habr.com/ru/post/undefined/


All Articles