【问题标题】:Test the Strength of a Password in Python在 Python 中测试密码的强度
【发布时间】:2018-11-12 21:26:49
【问题描述】:

在以下情况下,字符串是弱密码: 或者,它的长度少于 8 个字符, 或者,它是一个英文单词,函数is_english_word( ) 为True。

如果满足以下条件,则字符串是 STRONG 密码: 它包含至少 11 个字符 并且它至少包含 1 个小写字母 并且它至少包含 1 个大写字母 并且它至少包含 1 个数字。

如果字符串不是弱密码且不是强密码,则该字符串是中等密码。

def is_english_word( string ):
    with open("english_words.txt") as f:
        word_list = []
        for line in f.readlines():
            word_list.append(line.strip())
        if string in word_list: 
            return True
        elif string == string.upper() and string.lower() in word_list:
            return True
        elif string == string.title() and string.lower() in word_list:
            return True
        else:
            return False

def password_strength( string ):
    lower = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
    upper = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
    for item in string:
        if item in lower:
            string = string.replace(item, "x")
        elif item in upper:
            string = string.replace(item, "y")
        elif item.isnumeric():
            string = string.replace(item, "n")      
    for item in string:        
        if len( string ) < 8 or is_english_word( string ) :
            return 'WEAK'
        elif len( string ) >= 11 and string.count("x") >= 1 and string.count("y") >= 1 and string.count("n") >= 1: 
            return 'STRONG'
        else:
            return 'MEDIUM'

print( password_strength( 'Unimaginatively' ) )

这个密码应该是“WEAK”,但输出是“MEDIUM”,我不知道我的密码有什么问题。非常感谢。

【问题讨论】:

    标签: python python-3.x data-science data-analysis


    【解决方案1】:

    您的代码存在许多问题;值得注意的是,您将用x 替换小写字符,用y 替换大写字符,用n 替换数字,在调用is_english_word 之前 - 这意味着is_english_word() 将用'Xyyyyyyyyyyyyyy' 调用这不是一个英文单词。那就是让你的密码不是'WEAK'

    因为它也不是'STRONG',所以它最终是'MEDIUM'

    为了记录,这里是一个正确的代码示例:

    import string
    def password_strength(string):
        if len(string) < 8 or is_english_word(string):
            return 'WEAK'
        elif (len(string) > 11 and 
                any(ch in string.ascii_lowercase for ch in string) and
                any(ch in string.ascii_uppercase for ch in string) and
                any(ch.isdigit() for ch in string)):
            return 'STRONG'
        else:
            return 'MEDIUM'   
    

    【讨论】:

      【解决方案2】:

      如果您根据使用字符串方法执行的相同操作(检查至少一个字符是否为真)来考虑对字母类型的这 3 个限制,您可以显着简化您的代码:

      def is_weak(word):
          return len(word < 8 or is_english_word(word)
      
      def is_strong(word):
          return len(word) >= 11 and all(any(method(c) for c in word) 
                                         for method in (str.islower, str.isupper, str.isdigit))
      
      def password_strength(password):
          if is_weak(password):
              return 'WEAK'
          elif is_strong(password):
              return 'STRONG'
          else:
              return 'MEDIUM'
      

      【讨论】:

      • 感谢您的回答,能否请您告诉我此代码的格式'all(any(method(c) for c in word) for method in (str.islower, str.isupper, str .isdigit))',是一行吗?
      • @Cecilia 是的,为了便于阅读,我只是将它分成几行。如果括号内有表达式({}[]()),则中间可以有换行符,它仍将被解释为单个表达式。
      • 知道了!非常感谢
      猜你喜欢
      • 2020-08-22
      • 1970-01-01
      • 2013-06-10
      • 2021-09-07
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多