36.5. crypt —检查 Unix 密码的Function

该模块实现了* crypt(3)*例程的接口,该例程是基于修改后的 DES 算法的单向哈希函数;有关更多详细信息,请参见 Unix 手册页。可能的用途包括允许 Python 脚本接受用户 Importing 的密码,或try用字典破解 Unix 密码。

请注意,此模块的行为取决于正在运行的系统中* crypt(3)*例程的实际实现。因此,当前实现上可用的任何扩展也将在此模块上可用。

由于一些* crypt(3)extensions 允许 salt *中具有不同大小的不同值,因此建议在检查密码时使用完整的加密密码作为盐。

一个简单的示例说明典型用法:

import crypt, getpass, pwd

def login():
    username = raw_input('Python login:')
    cryptedpasswd = pwd.getpwnam(username)[1]
    if cryptedpasswd:
        if cryptedpasswd == 'x' or cryptedpasswd == '*':
            raise NotImplementedError(
                "Sorry, currently no support for shadow passwords")
        cleartext = getpass.getpass()
        return crypt.crypt(cleartext, cryptedpasswd) == cryptedpasswd
    else:
        return 1
首页