crypt —— 驗證 Unix 口令的函數?

源代碼: Lib/struct.py

Deprecated since version 3.11, will be removed in version 3.13: The crypt module is deprecated (see PEP 594 for details and alternatives). The hashlib module is a potential replacement for certain use cases.


本模塊實現了連接 crypt(3) 的接口,是一個基于改進 DES 算法的單向散列函數;更多細節(jié)請參閱 Unix man 手冊??赡艿挠猛景ū4娼涍^哈希的口令,這樣就可以在不存儲實際口令的情況下對其進行驗證,或者嘗試用字典來破解 Unix 口令。

請注意,本模塊的執(zhí)行取決于當前系統(tǒng)中 crypt(3) 的實際實現。 因此,當前實現版本可用的擴展均可在本模塊使用。

可用性 :Unix。在 VxWorks 中不可用。

哈希方法?

3.3 新版功能.

crypt 模塊定義了哈希方法的列表(不是所有的方法在所有平臺上都可用)。

crypt.METHOD_SHA512?

基于 SHA-512 哈希函數的模塊化加密格式方法,具備 16 個字符的 salt 和 86個字符的哈希算法。這是最強的哈希算法。

crypt.METHOD_SHA256?

另一種基于 SHA-256 哈希函數的模塊化加密格式方法,具備 16 個字符的 salt 和 43 個字符的哈希算法。

crypt.METHOD_BLOWFISH?

另一種基于 Blowfish 的模塊化加密格式方法,有 22 個字符的 salt 和 31 個字符的哈希算法。

3.7 新版功能.

crypt.METHOD_MD5?

另一種基于 MD5 哈希函數的模塊化加密格式方法,具備 8 個字符的 salt 和 22 個字符的哈希算法。

crypt.METHOD_CRYPT?

傳統(tǒng)的方法,具備 2 個字符的 salt 和 13 個字符的哈希算法。這是最弱的方法。

模塊屬性?

3.3 新版功能.

crypt.methods?

可用口令哈希算法的列表,形式為 crypt.METHOD_* 對象。該列表從最強到最弱進行排序。

模塊函數?

crypt 模塊定義了以下函數:

crypt.crypt(word, salt=None)?

word will usually be a user's password as typed at a prompt or in a graphical interface. The optional salt is either a string as returned from mksalt(), one of the crypt.METHOD_* values (though not all may be available on all platforms), or a full encrypted password including salt, as returned by this function. If salt is not provided, the strongest method available in methods will be used.

查驗口令通常是傳入純文本密碼 word ,和之前 crypt() 調用的結果進行比較,應該與本次調用的結果相同。

salt (隨機的 2 或 16 個字符的字符串,可能帶有 $digit{TX-PL-LABEL}#x60; 前綴以提示相關方法) 將被用來擾亂加密算法。 salt 中的字符必須在 [./a-zA-Z0-9] 集合中,但 Modular Crypt Format 除外,它會帶有 $digit{TX-PL-LABEL}#x60; 前綴。

返回哈希后的口令字符串,將由 salt 所在字母表中的字符組成。

由于有些 crypt(3) 擴展可以接受各種大小的 salt 值,建議在查驗口令時采用完整的加密后口令作為 salt。

在 3.3 版更改: 除了字符串之外, salt 還可接受 crypt.METHOD_* 值。

crypt.mksalt(method=None, *, rounds=None)?

Return a randomly generated salt of the specified method. If no method is given, the strongest method available in methods is used.

返回一個字符串,可用作傳入 crypt()salt 參數。

rounds 指定了 METHOD_SHA256, METHOD_SHA512METHOD_BLOWFISH 的循環(huán)次數。 對于 METHOD_SHA256METHOD_SHA512 而言,必須為介于 1000999_999_999 之間的整數,默認值為 5000。 而對于 METHOD_BLOWFISH,則必須為 16 (24) 和 2_147_483_648 (231) 之間的二的冪,默認值為 4096 (212)。

3.3 新版功能.

在 3.7 版更改: 加入 rounds 參數。

例子?

以下簡單示例演示了典型用法(需要一個時間固定的比較操作來限制留給計時攻擊的暴露面。 hmac.compare_digest() 即很適用):

import pwd
import crypt
import getpass
from hmac import compare_digest as compare_hash

def login():
    username = input('Python login: ')
    cryptedpasswd = pwd.getpwnam(username)[1]
    if cryptedpasswd:
        if cryptedpasswd == 'x' or cryptedpasswd == '*':
            raise ValueError('no support for shadow passwords')
        cleartext = getpass.getpass()
        return compare_hash(crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
    else:
        return True

采用當前強度最高的方法生成哈希值,并與原口令進行核對:

import crypt
from hmac import compare_digest as compare_hash

hashed = crypt.crypt(plaintext)
if not compare_hash(hashed, crypt.crypt(plaintext, hashed)):
    raise ValueError("hashed version doesn't validate against original")