Python 函数检查 UNIX 密码
要验证 UNIX 密码,我们应该使用 crypt 模块。它有 crypt(3) 例程。它基本上是一种基于改进 DES 算法的单向哈希函数。
要使用 crypt 模块,我们应该使用以下方式导入它。
import crypt
方法 crypt.crypt(word, salt)
该方法带两个参数。第一个是单词,第二个是 salt。该单词基本上是用户密码,在提示符中提供。salt 是一个随机字符串。它用于以 4096 种方式之一改变 DES 算法。salt 仅包含大写、小写、数字值以及“/”、“.” 字符。
该方法返回一个哈希密码作为字符串。
示例代码
import crypt, getpass, spwd
def check_pass():
username = input("Enter The Username: ")
password = spwd.getspnam(username).sp_pwdp
if password:
clr_text = getpass.getpass()
return crypt.crypt(clr_text, password) == password
else:
return 1
if check_pass():
print("The password matched")
else:
print("The password does not match")
输出
(以 root 级别权限运行此程序)
$ sudo python3 example.py Enter The Username: unix_user Password: The password matched
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP