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

更新時間: 30-7-2019

549 次檢視

開啟您的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.