如何让 crypt 模块仅在参数值与给定值相同时才工作?

问题描述

我一直试图弄清楚如何让此代码仅在值通过 cli 与给定值匹配时才起作用,例如 ubuntu。该代码有效,但是当我给出另一个密码/哈希而不是 test/npDnXtpN5py4 时,它仍然说密码与原始密码匹配。谁能帮忙?问候。

import os,sys
import getopt
import pwd
import crypt


def checkPassword(pswd,cpswd):
    """ Check if `cpwsd` an encrypted version is of `pswd`.
        Return `True` of `False`
    """
    try:
        cpswd = 'npDnXtpN5py4U'
        pswd = 'test'
        cryptedpassword = crypt.crypt(pswd,cpswd)
        return cpswd == cryptedpassword
    except KeyError:
        return 0 # no such pw

if __name__ == '__main__':
    opts,args = getopt.getopt(sys.argv[1:],'V',[])
    if len(args) != 2:
        print('Usage: {} [-v] <cpswd> <pswd>'.format(sys.argv[0]))
        sys.exit(0)

    cpswd,pswd = args[0],args[1]
    res = checkPassword(pswd,cpswd)
    if res:
        print("Pass for '{}' is '{}'".format(
            cpswd,pswd))
    else:
        print("Pass for '{}' is not '{}'".format(
            cpswd,pswd))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)