Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz
  • 0

Program oceniający bezpieczeństwo hasła - problem z pętlami while


jachu044

Pytanie

Opublikowano

Witam,

Mam 13 lat i mieszkam w Anglii. W szkole od okołu 2 miesięcy uczę się języka Python. Nie dawno, postawiono mnie przed zadaniem stworzenia programu który ocenia bezpieczeństwo hasła, pod względem użycia dużych i małych liter, cyfr oraz znaków specjalnych. Program generalnie działa bardzo sprawnie, poza częścią w której hasło jest sprawdzane pod względem długości (6-12), oraz obecności znaków rozszerzonych, tzn. znaki ASCII pow. 126. Problem polega na tym że jeśli hasło ma nieodpowiednią długość i użytkownik poproszony zostanie o ponowne wpisanie hasła, program nie jest w stanie sprawdzić kolejnego hasła pod względem obecności znaków rozszerzonych i vice-versa, jeśli chodzi o znaki rozszerzone a następnie długość hasła.

Dlatego, zdublowałem pętle while, jednak nie przyniosło to pożądanego efektu. Macie może jakiś pomysł jak rozwiązać to zagadnienie?

import re
import time
#score variable, responsible for later defining if passwords is weak, medium or strong
Score = 0
Password = ""
#defines function for checking for presence of extended characters (i.e. ascii 128-255)
def hasExtended(s):
    return any(ord(i) > 127 for i in s)
#inputs password
Password = input("Please enter a password:")
Password_length = len(Password)
Extended_presence = hasExtended(Password)
#checks password length (6-12), asks user to re-input until password is within boundaries
#checks if password contains extended characters (double while loops to allow for two conditions simultaneously)
while Password_length < 6 or Password_length > 12:
    if Password_length < 6:
        Outputted_length = "too short"
        Outputted_criteria = "no shorter than 6 characters"
    else:
        Outputted_length = "too long"
        Outputted_criteria = "no longer than 12 characters"
    print("Your password is", Outputted_length, ". It has to be", Outputted_criteria, ".")
    Password = input("Please enter a password:")
    Password_length = len(Password)
    
    while Extended_presence:
        print("Your password contains characters from the extended ASCII list. Please don't use these.")
        Password = input("Please enter a password:")
        Extended_presence = hasExtended(Password)
while Extended_presence:
    print("Your password contains characters from the extended ASCII list. Please don't use these.")
    Password = input("Please enter a password:")
    Extended_presence = hasExtended(Password)
    
    while Password_length < 6 or Password_length > 12:
        if Password_length < 6:
            Outputted_length = "too short"
            Outputted_criteria = "no shorter than 6 characters"
        else:
            Outputted_length = "too long"
            Outputted_criteria = "no longer than 12 characters"
        print("Your password is", Outputted_length, ". It has to be", Outputted_criteria, ".")
        Password = input("Please enter a password:")
        Password_length = len(Password)
else:
    #defines function for checking for presence of numbers in password
    def hasNumbers(s):
        return any(i.isdigit() for i in s)
    #defines function for checking for presence of letters
    def hasLetters(s):
        return any(i.isalpha() for i in s)
    #defines function for checking for presence of special characters
    def hasSpecial(s):
        return any(ord(i) < 48 for i in s)
    #checks if password contains letters
    Letter_presence = hasLetters(Password)
    if not Letter_presence:
        Score = Score - 1
    else:
        Score = Score + 1
    #checks if password is all upper case
    Is_upper = Password.isupper()
    if not Is_upper:
        Score = Score + 1
    else:
        Score = Score - 1
    #checks if passwords is all lower case
    Is_lower = Password.islower()
    if not Is_lower:
        Score = Score + 1
    else:
        Score = Score - 1
    #checks if password contains a number
    Number_presence = hasNumbers(Password)
    if not Number_presence:
        Score = Score + 0
    else:
        Score = Score + 1
    #checks if password is just numbers
    Only_numbers = Password.isdigit()
    if not Only_numbers:
        Score = Score + 0
    else:
        Score = Score - 1
    #checks if password contains special characters
    Special_presence = hasSpecial(Password)
    if not Special_presence:
        Score = Score + 0
    else:
        Score = Score + 1
    #outputs weak, medium or strong to user based on score value
    if Score <= 2:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password is absolutely rubbish! Try again, please.")
    if Score == 3:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password is weak, you should try again.")
    elif Score == 4:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password is medium, it should be OK.")
    elif Score == 5:
        print("The program is processing your password...")
        time.sleep(2)
        print("Your password is strong, it is absolutely fine.")

Dziękuję za wszelką pomoc i pozdrawiam.

1 odpowiedź na to pytanie

Rekomendowane odpowiedzi

Opublikowano

W pętli while gdzie sprawdzana jest długość hasła zapomniałeś na końcu przy ustawianiu nowego hasła dodać "Extended_presence = hasExtended(Password)"

Ogólnie kod wygląda paskudnie, z pewnością dało by się zrobić go lepiej, ale jak na 13 latka to nieźle

1149730ee7ddaf7UVMV7ZB.gif

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...