“Python/docs/3.9/library/crypt”的版本间差异

来自菜鸟教程
Python/docs/3.9/library/crypt
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:crypt — 检查 Unix 密码的函数 — Python 文档}}
 
<div id="module-crypt" class="section">
 
<div id="module-crypt" class="section">
  
 
<span id="crypt-function-to-check-unix-passwords"></span>
 
<span id="crypt-function-to-check-unix-passwords"></span>
= [[#module-crypt|<code>crypt</code>]] --- Function to check Unix passwords =
+
= crypt — 检查 Unix 密码的函数 =
  
'''Source code:''' [https://github.com/python/cpython/tree/3.9/Lib/crypt.py Lib/crypt.py]
+
'''源代码:''' [[#id1|<span id="id2" class="problematic">:source:`Lib/crypt.py`</span>]]
  
This module implements an interface to the ''[https://manpages.debian.org/crypt(3) crypt(3)]'' routine, which is
 
a one-way hash function based upon a modified DES algorithm; see the Unix man
 
page for further details. Possible uses include storing hashed passwords
 
so you can check passwords without storing the actual password, or attempting
 
to crack Unix passwords with a dictionary.
 
  
Notice that the behavior of this module depends on the actual implementation of
+
-----
the ''[https://manpages.debian.org/crypt(3) crypt(3)]'' routine in the running system. Therefore, any
 
extensions available on the current implementation will also be available on
 
this module.
 
  
[[../intro#availability|<span class="std std-ref">Availability</span>]]: Unix. Not available on VxWorks.
+
该模块实现了 ''crypt(3)'' 例程的接口,该例程是基于修改的 DES 算法的单向哈希函数; 有关更多详细信息,请参阅 Unix 手册页。 可能的用途包括存储散列密码,以便您可以在不存储实际密码的情况下检查密码,或者尝试使用字典破解 Unix 密码。
 +
 
 +
请注意,该模块的行为取决于运行系统中 ''crypt(3)'' 例程的实际实现。 因此,当前实现上可用的任何扩展也将在此模块上可用。
  
 
<div id="hashing-methods" class="section">
 
<div id="hashing-methods" class="section">
  
== Hashing Methods ==
+
== 散列方法 ==
  
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.3 新版功能.</span>
+
<span class="versionmodified added">3.3 版中的新功能。</span>
  
  
 
</div>
 
</div>
The [[#module-crypt|<code>crypt</code>]] module defines the list of hashing methods (not all methods
+
[[#module-crypt|crypt]] 模块定义了散列方法列表(并非所有方法都适用于所有平台):
are available on all platforms):
 
  
; <code>crypt.</code><code>METHOD_SHA512</code>
+
; <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_SHA512</span></span>
: A Modular Crypt Format method with 16 character salt and 86 character hash based on the SHA-512 hash function. This is the strongest method.
+
: 基于 SHA-512 散列函数的具有 16 个字符盐和 86 个字符散列的模块化密码格式方法。 这是最强的方法。
  
; <code>crypt.</code><code>METHOD_SHA256</code>
+
; <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_SHA256</span></span>
: Another Modular Crypt Format method with 16 character salt and 43 character hash based on the SHA-256 hash function.
+
: 另一种基于 SHA-256 散列函数的模块化密码格式方法,具有 16 个字符的盐和 43 个字符的散列。
  
 
<dl>
 
<dl>
<dt><code>crypt.</code><code>METHOD_BLOWFISH</code></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_BLOWFISH</span></span></dt>
<dd><p>Another Modular Crypt Format method with 22 character salt and 31
+
<dd><p>另一种基于 Blowfish 密码的模块化密码格式方法,具有 22 个字符的盐和 31 个字符的散列。</p>
character hash based on the Blowfish cipher.</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.7 新版功能.</span></p>
+
<p><span class="versionmodified added">3.7 版中的新功能。</span></p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
; <code>crypt.</code><code>METHOD_MD5</code>
+
; <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_MD5</span></span>
: Another Modular Crypt Format method with 8 character salt and 22 character hash based on the MD5 hash function.
+
: 另一种基于 MD5 散列函数的模块化密码格式方法,具有 8 个字符的盐和 22 个字符的散列。
  
; <code>crypt.</code><code>METHOD_CRYPT</code>
+
; <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_CRYPT</span></span>
: The traditional method with a 2 character salt and 13 characters of hash. This is the weakest method.
+
: 使用 2 个字符的盐和 13 个字符的哈希的传统方法。 这是最弱的方法。
  
  
第58行: 第51行:
 
<div id="module-attributes" class="section">
 
<div id="module-attributes" class="section">
  
== Module Attributes ==
+
== 模块属性 ==
  
 
<div class="versionadded">
 
<div class="versionadded">
  
<span class="versionmodified added">3.3 新版功能.</span>
+
<span class="versionmodified added">3.3 版中的新功能。</span>
  
  
 
</div>
 
</div>
; <code>crypt.</code><code>methods</code>
+
; <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">methods</span></span>
: A list of available password hashing algorithms, as <code>crypt.METHOD_*</code> objects. This list is sorted from strongest to weakest.
+
: 可用密码散列算法的列表,作为 <code>crypt.METHOD_*</code> 对象。 此列表从最强到最弱排序。
  
  
第73行: 第66行:
 
<div id="module-functions" class="section">
 
<div id="module-functions" class="section">
  
== Module Functions ==
+
== 模块功能 ==
  
The [[#module-crypt|<code>crypt</code>]] module defines the following functions:
+
[[#module-crypt|crypt]] 模块定义了以下函数:
  
 
<dl>
 
<dl>
<dt><code>crypt.</code><code>crypt</code><span class="sig-paren">(</span>''<span class="n">word</span>'', ''<span class="n">salt</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">crypt</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">word</span></span>'', ''<span class="n"><span class="pre">salt</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>''word'' will usually be a user's password as typed at a prompt or in a graphical
+
<dd><p>''word'' 通常是在提示或图形界面中输入的用户密码。 可选的 ''salt'' 是从 [[#crypt.mksalt|mksalt()]] 返回的字符串,<code>crypt.METHOD_*</code> 值之一(尽管并非所有平台都可以使用),或者此函数返回的包含盐的完整加密密码。 如果未提供 ''salt'',则将使用最强的方法(由 [[#crypt.methods|methods()]] 返回)。</p>
interface. The optional ''salt'' is either a string as returned from
+
<p>检查密码通常是通过将明文密码作为 ''word'' 和之前 [[#module-crypt|crypt()]] 调用的完整结果传递来完成的,这应该与这次调用的结果相同.</p>
[[#crypt.mksalt|<code>mksalt()</code>]], one of the <code>crypt.METHOD_*</code> values (though not all
+
<p>''salt''(一个随机的 2 16 个字符串,可能以 <code>$digit$</code> 为前缀来指示方法)将用于扰乱加密算法。 ''salt'' 中的字符必须在集合 <code>[./a-zA-Z0-9]</code> 中,除了前缀为 <code>$digit$</code> 的模块化加密格式。</p>
may be available on all platforms), or a full encrypted password
+
<p>以字符串形式返回散列密码,该字符串将由与盐相同字母表中的字符组成。</p>
including salt, as returned by this function. If ''salt'' is not
+
<p>由于一些 ''crypt(3)'' 扩展允许不同的值,在 ''salt'' 中具有不同的大小,建议在检查密码时使用完整的加密密码作为盐。</p>
provided, the strongest method will be used (as returned by
 
[[#crypt.methods|<code>methods()</code>]]).</p>
 
<p>Checking a password is usually done by passing the plain-text password
 
as ''word'' and the full results of a previous [[#module-crypt|<code>crypt()</code>]] call,
 
which should be the same as the results of this call.</p>
 
<p>''salt'' (either a random 2 or 16 character string, possibly prefixed with
 
<code>$digit$</code> to indicate the method) which will be used to perturb the
 
encryption algorithm. The characters in ''salt'' must be in the set
 
<code>[./a-zA-Z0-9]</code>, with the exception of Modular Crypt Format which
 
prefixes a <code>$digit$</code>.</p>
 
<p>Returns the hashed password as a string, which will be composed of
 
characters from the same alphabet as the salt.</p>
 
<p>Since a few ''[https://manpages.debian.org/crypt(3) crypt(3)]'' extensions allow different values, with
 
different sizes in the ''salt'', it is recommended to use the full crypted
 
password as salt when checking for a password.</p>
 
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">在 3.3 版更改: </span>Accept <code>crypt.METHOD_*</code> values in addition to strings for ''salt''.</p>
+
<p><span class="versionmodified changed">在 3.3 版中更改:</span>除了 ''salt'' 的字符串外,还接受 <code>crypt.METHOD_*</code> 值。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
  
 
<dl>
 
<dl>
<dt><code>crypt.</code><code>mksalt</code><span class="sig-paren">(</span>''<span class="n">method</span><span class="o">=</span><span class="default_value">None</span>'', ''<span class="o">*</span>'', ''<span class="n">rounds</span><span class="o">=</span><span class="default_value">None</span>''<span class="sig-paren">)</span></dt>
+
<dt><span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">mksalt</span></span><span class="sig-paren">(</span>''<span class="n"><span class="pre">method</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>'', ''<span class="o"><span class="pre">*</span></span>'', ''<span class="n"><span class="pre">rounds</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span>''<span class="sig-paren">)</span></dt>
<dd><p>Return a randomly generated salt of the specified method. If no
+
<dd><p>返回指定方法的随机生成的盐。 如果没有给出 ''method'',则使用由 [[#crypt.methods|methods()]] 返回的最强方法。</p>
''method'' is given, the strongest method available as returned by
+
<p>返回值是一个适合作为 ''salt'' 参数传递给 [[#module-crypt|crypt()]] 的字符串。</p>
[[#crypt.methods|<code>methods()</code>]] is used.</p>
+
<p>''rounds'' 指定 <code>METHOD_SHA256</code><code>METHOD_SHA512</code> <code>METHOD_BLOWFISH</code> 的轮数。 对于 <code>METHOD_SHA256</code> <code>METHOD_SHA512</code>,它必须是 <code>1000</code> <code>999_999_999</code> 之间的整数,默认为 <code>5000</code>。 对于 <code>METHOD_BLOWFISH</code>,它必须是 <code>16</code> (2<sup>4</sup>) <code>2_147_483_648</code> (2<sup>31</sup>) 之间的 2 次幂,即默认值为 <code>4096</code> (2<sup>12</sup>)</p>
<p>The return value is a string suitable for passing as the ''salt'' argument
 
to [[#module-crypt|<code>crypt()</code>]].</p>
 
<p>''rounds'' specifies the number of rounds for <code>METHOD_SHA256</code>,
 
<code>METHOD_SHA512</code> and <code>METHOD_BLOWFISH</code>.
 
For <code>METHOD_SHA256</code> and <code>METHOD_SHA512</code> it must be an integer between
 
<code>1000</code> and <code>999_999_999</code>, the default is <code>5000</code>. For
 
<code>METHOD_BLOWFISH</code> it must be a power of two between <code>16</code> (2<sup>4</sup>)
 
and <code>2_147_483_648</code> (2<sup>31</sup>), the default is <code>4096</code>
 
(2<sup>12</sup>).</p>
 
 
<div class="versionadded">
 
<div class="versionadded">
  
<p><span class="versionmodified added">3.3 新版功能.</span></p>
+
<p><span class="versionmodified added">3.3 版中的新功能。</span></p>
  
 
</div>
 
</div>
 
<div class="versionchanged">
 
<div class="versionchanged">
  
<p><span class="versionmodified changed">3.7 版更改: </span>Added the ''rounds'' parameter.</p>
+
<p><span class="versionmodified changed"> 3.7 版更改: </span> 添加 ''rounds'' 参数。</p>
  
 
</div></dd></dl>
 
</div></dd></dl>
第134行: 第103行:
 
<div id="examples" class="section">
 
<div id="examples" class="section">
  
== Examples ==
+
== 例子 ==
  
A simple example illustrating typical use (a constant-time comparison
+
一个说明典型用途的简单示例(需要一个恒定时间比较操作来限制暴露于定时攻击。 [[../hmac#hmac|hmac.compare_digest()]] 适用于此目的):
operation is needed to limit exposure to timing attacks.
 
[[../hmac#hmac|<code>hmac.compare_digest()</code>]] is suitable for this purpose):
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第144行: 第111行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import pwd
+
<syntaxhighlight lang="python3">import pwd
 
import crypt
 
import crypt
 
import getpass
 
import getpass
第158行: 第125行:
 
         return compare_hash(crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
 
         return compare_hash(crypt.crypt(cleartext, cryptedpasswd), cryptedpasswd)
 
     else:
 
     else:
         return True</pre>
+
         return True</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
To generate a hash of a password using the strongest available method and
+
要使用最强的可用方法生成密码的哈希值并对照原始方法进行检查:
check it against the original:
 
  
 
<div class="highlight-python3 notranslate">
 
<div class="highlight-python3 notranslate">
第170行: 第136行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import crypt
+
<syntaxhighlight lang="python3">import crypt
 
from hmac import compare_digest as compare_hash
 
from hmac import compare_digest as compare_hash
  
 
hashed = crypt.crypt(plaintext)
 
hashed = crypt.crypt(plaintext)
 
if not compare_hash(hashed, crypt.crypt(plaintext, hashed)):
 
if not compare_hash(hashed, crypt.crypt(plaintext, hashed)):
     raise ValueError(&quot;hashed version doesn't validate against original&quot;)</pre>
+
     raise ValueError("hashed version doesn't validate against original")</syntaxhighlight>
  
 
</div>
 
</div>
第182行: 第148行:
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Python 3.9 中文文档]]
+
[[Category:Python 3.9 文档]]

2021年10月31日 (日) 04:51的最新版本

crypt — 检查 Unix 密码的函数

源代码: :source:`Lib/crypt.py`



该模块实现了 crypt(3) 例程的接口,该例程是基于修改的 DES 算法的单向哈希函数; 有关更多详细信息,请参阅 Unix 手册页。 可能的用途包括存储散列密码,以便您可以在不存储实际密码的情况下检查密码,或者尝试使用字典破解 Unix 密码。

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

散列方法

3.3 版中的新功能。


crypt 模块定义了散列方法列表(并非所有方法都适用于所有平台):

crypt.METHOD_SHA512
基于 SHA-512 散列函数的具有 16 个字符盐和 86 个字符散列的模块化密码格式方法。 这是最强的方法。
crypt.METHOD_SHA256
另一种基于 SHA-256 散列函数的模块化密码格式方法,具有 16 个字符的盐和 43 个字符的散列。
crypt.METHOD_BLOWFISH

另一种基于 Blowfish 密码的模块化密码格式方法,具有 22 个字符的盐和 31 个字符的散列。

3.7 版中的新功能。

crypt.METHOD_MD5
另一种基于 MD5 散列函数的模块化密码格式方法,具有 8 个字符的盐和 22 个字符的散列。
crypt.METHOD_CRYPT
使用 2 个字符的盐和 13 个字符的哈希的传统方法。 这是最弱的方法。


模块属性

3.3 版中的新功能。


crypt.methods
可用密码散列算法的列表,作为 crypt.METHOD_* 对象。 此列表从最强到最弱排序。


模块功能

crypt 模块定义了以下函数:

crypt.crypt(word, salt=None)

word 通常是在提示或图形界面中输入的用户密码。 可选的 salt 是从 mksalt() 返回的字符串,crypt.METHOD_* 值之一(尽管并非所有平台都可以使用),或者此函数返回的包含盐的完整加密密码。 如果未提供 salt,则将使用最强的方法(由 methods() 返回)。

检查密码通常是通过将明文密码作为 word 和之前 crypt() 调用的完整结果传递来完成的,这应该与这次调用的结果相同.

salt(一个随机的 2 或 16 个字符串,可能以 $digit$ 为前缀来指示方法)将用于扰乱加密算法。 salt 中的字符必须在集合 [./a-zA-Z0-9] 中,除了前缀为 $digit$ 的模块化加密格式。

以字符串形式返回散列密码,该字符串将由与盐相同字母表中的字符组成。

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

在 3.3 版中更改:除了 salt 的字符串外,还接受 crypt.METHOD_* 值。

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

返回指定方法的随机生成的盐。 如果没有给出 method,则使用由 methods() 返回的最强方法。

返回值是一个适合作为 salt 参数传递给 crypt() 的字符串。

rounds 指定 METHOD_SHA256METHOD_SHA512METHOD_BLOWFISH 的轮数。 对于 METHOD_SHA256METHOD_SHA512,它必须是 1000999_999_999 之间的整数,默认为 5000。 对于 METHOD_BLOWFISH,它必须是 16 (24) 和 2_147_483_648 (231) 之间的 2 次幂,即默认值为 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")