gooderp18绿色标准版
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

509 rindas
32KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="crypt — Function to check Unix passwords" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/crypt.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/crypt.py This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm; see the Unix man page for further detail..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/crypt.py This module implements an interface to the crypt(3) routine, which is a one-way hash function based upon a modified DES algorithm; see the Unix man page for further detail..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>crypt — Function to check Unix passwords &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="imghdr — Determine the type of an image" href="imghdr.html" />
  33. <link rel="prev" title="chunk — Read IFF chunked data" href="chunk.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/crypt.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> — Function to check Unix passwords</a><ul>
  86. <li><a class="reference internal" href="#hashing-methods">Hashing Methods</a></li>
  87. <li><a class="reference internal" href="#module-attributes">Module Attributes</a></li>
  88. <li><a class="reference internal" href="#module-functions">Module Functions</a></li>
  89. <li><a class="reference internal" href="#examples">Examples</a></li>
  90. </ul>
  91. </li>
  92. </ul>
  93. </div>
  94. <div>
  95. <h4>Previous topic</h4>
  96. <p class="topless"><a href="chunk.html"
  97. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">chunk</span></code> — Read IFF chunked data</a></p>
  98. </div>
  99. <div>
  100. <h4>Next topic</h4>
  101. <p class="topless"><a href="imghdr.html"
  102. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imghdr</span></code> — Determine the type of an image</a></p>
  103. </div>
  104. <div role="note" aria-label="source link">
  105. <h3>This Page</h3>
  106. <ul class="this-page-menu">
  107. <li><a href="../bugs.html">Report a Bug</a></li>
  108. <li>
  109. <a href="https://github.com/python/cpython/blob/main/Doc/library/crypt.rst"
  110. rel="nofollow">Show Source
  111. </a>
  112. </li>
  113. </ul>
  114. </div>
  115. </nav>
  116. </div>
  117. </div>
  118. <div class="related" role="navigation" aria-label="related navigation">
  119. <h3>Navigation</h3>
  120. <ul>
  121. <li class="right" style="margin-right: 10px">
  122. <a href="../genindex.html" title="General Index"
  123. accesskey="I">index</a></li>
  124. <li class="right" >
  125. <a href="../py-modindex.html" title="Python Module Index"
  126. >modules</a> |</li>
  127. <li class="right" >
  128. <a href="imghdr.html" title="imghdr — Determine the type of an image"
  129. accesskey="N">next</a> |</li>
  130. <li class="right" >
  131. <a href="chunk.html" title="chunk — Read IFF chunked data"
  132. accesskey="P">previous</a> |</li>
  133. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  134. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  135. <li class="switchers">
  136. <div class="language_switcher_placeholder"></div>
  137. <div class="version_switcher_placeholder"></div>
  138. </li>
  139. <li>
  140. </li>
  141. <li id="cpython-language-and-version">
  142. <a href="../index.html">3.12.3 Documentation</a> &#187;
  143. </li>
  144. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  145. <li class="nav-item nav-item-2"><a href="superseded.html" accesskey="U">Superseded Modules</a> &#187;</li>
  146. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> — Function to check Unix passwords</a></li>
  147. <li class="right">
  148. <div class="inline-search" role="search">
  149. <form class="inline-search" action="../search.html" method="get">
  150. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  151. <input type="submit" value="Go" />
  152. </form>
  153. </div>
  154. |
  155. </li>
  156. <li class="right">
  157. <label class="theme-selector-label">
  158. Theme
  159. <select class="theme-selector" oninput="activateTheme(this.value)">
  160. <option value="auto" selected>Auto</option>
  161. <option value="light">Light</option>
  162. <option value="dark">Dark</option>
  163. </select>
  164. </label> |</li>
  165. </ul>
  166. </div>
  167. <div class="document">
  168. <div class="documentwrapper">
  169. <div class="bodywrapper">
  170. <div class="body" role="main">
  171. <section id="module-crypt">
  172. <span id="crypt-function-to-check-unix-passwords"></span><h1><a class="reference internal" href="#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (deprecated) (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code></a> — Function to check Unix passwords<a class="headerlink" href="#module-crypt" title="Link to this heading">¶</a></h1>
  173. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/crypt.py">Lib/crypt.py</a></p>
  174. <div class="deprecated-removed" id="index-0">
  175. <p><span class="versionmodified">Deprecated since version 3.11, will be removed in version 3.13: </span>The <a class="reference internal" href="#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (deprecated) (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code></a> module is deprecated
  176. (see <span class="target" id="index-1"></span><a class="pep reference external" href="https://peps.python.org/pep-0594/#crypt"><strong>PEP 594</strong></a> for details and alternatives).
  177. The <a class="reference internal" href="hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code></a> module is a potential replacement for certain use cases.
  178. The <a class="reference external" href="https://pypi.org/project/passlib/">passlib</a> package can replace all use cases of this module.</p>
  179. </div>
  180. <hr class="docutils" />
  181. <p>This module implements an interface to the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/crypt(3)">crypt(3)</a></em> routine, which is
  182. a one-way hash function based upon a modified DES algorithm; see the Unix man
  183. page for further details. Possible uses include storing hashed passwords
  184. so you can check passwords without storing the actual password, or attempting
  185. to crack Unix passwords with a dictionary.</p>
  186. <p id="index-2">Notice that the behavior of this module depends on the actual implementation of
  187. the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/crypt(3)">crypt(3)</a></em> routine in the running system. Therefore, any
  188. extensions available on the current implementation will also be available on
  189. this module.</p>
  190. <div class="availability docutils container">
  191. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: Unix, not VxWorks.</p>
  192. </div>
  193. <div class="availability docutils container">
  194. <p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not Emscripten, not WASI.</p>
  195. <p>This module does not work or is not available on WebAssembly platforms
  196. <code class="docutils literal notranslate"><span class="pre">wasm32-emscripten</span></code> and <code class="docutils literal notranslate"><span class="pre">wasm32-wasi</span></code>. See
  197. <a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>
  198. </div>
  199. <section id="hashing-methods">
  200. <h2>Hashing Methods<a class="headerlink" href="#hashing-methods" title="Link to this heading">¶</a></h2>
  201. <div class="versionadded">
  202. <p><span class="versionmodified added">New in version 3.3.</span></p>
  203. </div>
  204. <p>The <a class="reference internal" href="#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (deprecated) (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code></a> module defines the list of hashing methods (not all methods
  205. are available on all platforms):</p>
  206. <dl class="py data">
  207. <dt class="sig sig-object py" id="crypt.METHOD_SHA512">
  208. <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_SHA512</span></span><a class="headerlink" href="#crypt.METHOD_SHA512" title="Link to this definition">¶</a></dt>
  209. <dd><p>A Modular Crypt Format method with 16 character salt and 86 character
  210. hash based on the SHA-512 hash function. This is the strongest method.</p>
  211. </dd></dl>
  212. <dl class="py data">
  213. <dt class="sig sig-object py" id="crypt.METHOD_SHA256">
  214. <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_SHA256</span></span><a class="headerlink" href="#crypt.METHOD_SHA256" title="Link to this definition">¶</a></dt>
  215. <dd><p>Another Modular Crypt Format method with 16 character salt and 43
  216. character hash based on the SHA-256 hash function.</p>
  217. </dd></dl>
  218. <dl class="py data">
  219. <dt class="sig sig-object py" id="crypt.METHOD_BLOWFISH">
  220. <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_BLOWFISH</span></span><a class="headerlink" href="#crypt.METHOD_BLOWFISH" title="Link to this definition">¶</a></dt>
  221. <dd><p>Another Modular Crypt Format method with 22 character salt and 31
  222. character hash based on the Blowfish cipher.</p>
  223. <div class="versionadded">
  224. <p><span class="versionmodified added">New in version 3.7.</span></p>
  225. </div>
  226. </dd></dl>
  227. <dl class="py data">
  228. <dt class="sig sig-object py" id="crypt.METHOD_MD5">
  229. <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_MD5</span></span><a class="headerlink" href="#crypt.METHOD_MD5" title="Link to this definition">¶</a></dt>
  230. <dd><p>Another Modular Crypt Format method with 8 character salt and 22
  231. character hash based on the MD5 hash function.</p>
  232. </dd></dl>
  233. <dl class="py data">
  234. <dt class="sig sig-object py" id="crypt.METHOD_CRYPT">
  235. <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">METHOD_CRYPT</span></span><a class="headerlink" href="#crypt.METHOD_CRYPT" title="Link to this definition">¶</a></dt>
  236. <dd><p>The traditional method with a 2 character salt and 13 characters of
  237. hash. This is the weakest method.</p>
  238. </dd></dl>
  239. </section>
  240. <section id="module-attributes">
  241. <h2>Module Attributes<a class="headerlink" href="#module-attributes" title="Link to this heading">¶</a></h2>
  242. <div class="versionadded">
  243. <p><span class="versionmodified added">New in version 3.3.</span></p>
  244. </div>
  245. <dl class="py attribute">
  246. <dt class="sig sig-object py" id="crypt.methods">
  247. <span class="sig-prename descclassname"><span class="pre">crypt.</span></span><span class="sig-name descname"><span class="pre">methods</span></span><a class="headerlink" href="#crypt.methods" title="Link to this definition">¶</a></dt>
  248. <dd><p>A list of available password hashing algorithms, as
  249. <code class="docutils literal notranslate"><span class="pre">crypt.METHOD_*</span></code> objects. This list is sorted from strongest to
  250. weakest.</p>
  251. </dd></dl>
  252. </section>
  253. <section id="module-functions">
  254. <h2>Module Functions<a class="headerlink" href="#module-functions" title="Link to this heading">¶</a></h2>
  255. <p>The <a class="reference internal" href="#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (deprecated) (Unix)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code></a> module defines the following functions:</p>
  256. <dl class="py function">
  257. <dt class="sig sig-object py" id="crypt.crypt">
  258. <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><em class="sig-param"><span class="n"><span class="pre">word</span></span></em>, <em class="sig-param"><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></em><span class="sig-paren">)</span><a class="headerlink" href="#crypt.crypt" title="Link to this definition">¶</a></dt>
  259. <dd><p><em>word</em> will usually be a user’s password as typed at a prompt or in a graphical
  260. interface. The optional <em>salt</em> is either a string as returned from
  261. <a class="reference internal" href="#crypt.mksalt" title="crypt.mksalt"><code class="xref py py-func docutils literal notranslate"><span class="pre">mksalt()</span></code></a>, one of the <code class="docutils literal notranslate"><span class="pre">crypt.METHOD_*</span></code> values (though not all
  262. may be available on all platforms), or a full encrypted password
  263. including salt, as returned by this function. If <em>salt</em> is not
  264. provided, the strongest method available in <a class="reference internal" href="#crypt.methods" title="crypt.methods"><code class="xref py py-attr docutils literal notranslate"><span class="pre">methods</span></code></a> will be used.</p>
  265. <p>Checking a password is usually done by passing the plain-text password
  266. as <em>word</em> and the full results of a previous <a class="reference internal" href="#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (deprecated) (Unix)"><code class="xref py py-func docutils literal notranslate"><span class="pre">crypt()</span></code></a> call,
  267. which should be the same as the results of this call.</p>
  268. <p><em>salt</em> (either a random 2 or 16 character string, possibly prefixed with
  269. <code class="docutils literal notranslate"><span class="pre">$digit$</span></code> to indicate the method) which will be used to perturb the
  270. encryption algorithm. The characters in <em>salt</em> must be in the set
  271. <code class="docutils literal notranslate"><span class="pre">[./a-zA-Z0-9]</span></code>, with the exception of Modular Crypt Format which
  272. prefixes a <code class="docutils literal notranslate"><span class="pre">$digit$</span></code>.</p>
  273. <p>Returns the hashed password as a string, which will be composed of
  274. characters from the same alphabet as the salt.</p>
  275. <p id="index-3">Since a few <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/crypt(3)">crypt(3)</a></em> extensions allow different values, with
  276. different sizes in the <em>salt</em>, it is recommended to use the full crypted
  277. password as salt when checking for a password.</p>
  278. <div class="versionchanged">
  279. <p><span class="versionmodified changed">Changed in version 3.3: </span>Accept <code class="docutils literal notranslate"><span class="pre">crypt.METHOD_*</span></code> values in addition to strings for <em>salt</em>.</p>
  280. </div>
  281. </dd></dl>
  282. <dl class="py function">
  283. <dt class="sig sig-object py" id="crypt.mksalt">
  284. <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><em class="sig-param"><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></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><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></em><span class="sig-paren">)</span><a class="headerlink" href="#crypt.mksalt" title="Link to this definition">¶</a></dt>
  285. <dd><p>Return a randomly generated salt of the specified method. If no
  286. <em>method</em> is given, the strongest method available in <a class="reference internal" href="#crypt.methods" title="crypt.methods"><code class="xref py py-attr docutils literal notranslate"><span class="pre">methods</span></code></a> is
  287. used.</p>
  288. <p>The return value is a string suitable for passing as the <em>salt</em> argument
  289. to <a class="reference internal" href="#module-crypt" title="crypt: The crypt() function used to check Unix passwords. (deprecated) (Unix)"><code class="xref py py-func docutils literal notranslate"><span class="pre">crypt()</span></code></a>.</p>
  290. <p><em>rounds</em> specifies the number of rounds for <code class="docutils literal notranslate"><span class="pre">METHOD_SHA256</span></code>,
  291. <code class="docutils literal notranslate"><span class="pre">METHOD_SHA512</span></code> and <code class="docutils literal notranslate"><span class="pre">METHOD_BLOWFISH</span></code>.
  292. For <code class="docutils literal notranslate"><span class="pre">METHOD_SHA256</span></code> and <code class="docutils literal notranslate"><span class="pre">METHOD_SHA512</span></code> it must be an integer between
  293. <code class="docutils literal notranslate"><span class="pre">1000</span></code> and <code class="docutils literal notranslate"><span class="pre">999_999_999</span></code>, the default is <code class="docutils literal notranslate"><span class="pre">5000</span></code>. For
  294. <code class="docutils literal notranslate"><span class="pre">METHOD_BLOWFISH</span></code> it must be a power of two between <code class="docutils literal notranslate"><span class="pre">16</span></code> (2<sup>4</sup>)
  295. and <code class="docutils literal notranslate"><span class="pre">2_147_483_648</span></code> (2<sup>31</sup>), the default is <code class="docutils literal notranslate"><span class="pre">4096</span></code>
  296. (2<sup>12</sup>).</p>
  297. <div class="versionadded">
  298. <p><span class="versionmodified added">New in version 3.3.</span></p>
  299. </div>
  300. <div class="versionchanged">
  301. <p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>rounds</em> parameter.</p>
  302. </div>
  303. </dd></dl>
  304. </section>
  305. <section id="examples">
  306. <h2>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h2>
  307. <p>A simple example illustrating typical use (a constant-time comparison
  308. operation is needed to limit exposure to timing attacks.
  309. <a class="reference internal" href="hmac.html#hmac.compare_digest" title="hmac.compare_digest"><code class="xref py py-func docutils literal notranslate"><span class="pre">hmac.compare_digest()</span></code></a> is suitable for this purpose):</p>
  310. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pwd</span>
  311. <span class="kn">import</span> <span class="nn">crypt</span>
  312. <span class="kn">import</span> <span class="nn">getpass</span>
  313. <span class="kn">from</span> <span class="nn">hmac</span> <span class="kn">import</span> <span class="n">compare_digest</span> <span class="k">as</span> <span class="n">compare_hash</span>
  314. <span class="k">def</span> <span class="nf">login</span><span class="p">():</span>
  315. <span class="n">username</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s1">&#39;Python login: &#39;</span><span class="p">)</span>
  316. <span class="n">cryptedpasswd</span> <span class="o">=</span> <span class="n">pwd</span><span class="o">.</span><span class="n">getpwnam</span><span class="p">(</span><span class="n">username</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>
  317. <span class="k">if</span> <span class="n">cryptedpasswd</span><span class="p">:</span>
  318. <span class="k">if</span> <span class="n">cryptedpasswd</span> <span class="o">==</span> <span class="s1">&#39;x&#39;</span> <span class="ow">or</span> <span class="n">cryptedpasswd</span> <span class="o">==</span> <span class="s1">&#39;*&#39;</span><span class="p">:</span>
  319. <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;no support for shadow passwords&#39;</span><span class="p">)</span>
  320. <span class="n">cleartext</span> <span class="o">=</span> <span class="n">getpass</span><span class="o">.</span><span class="n">getpass</span><span class="p">()</span>
  321. <span class="k">return</span> <span class="n">compare_hash</span><span class="p">(</span><span class="n">crypt</span><span class="o">.</span><span class="n">crypt</span><span class="p">(</span><span class="n">cleartext</span><span class="p">,</span> <span class="n">cryptedpasswd</span><span class="p">),</span> <span class="n">cryptedpasswd</span><span class="p">)</span>
  322. <span class="k">else</span><span class="p">:</span>
  323. <span class="k">return</span> <span class="kc">True</span>
  324. </pre></div>
  325. </div>
  326. <p>To generate a hash of a password using the strongest available method and
  327. check it against the original:</p>
  328. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">crypt</span>
  329. <span class="kn">from</span> <span class="nn">hmac</span> <span class="kn">import</span> <span class="n">compare_digest</span> <span class="k">as</span> <span class="n">compare_hash</span>
  330. <span class="n">hashed</span> <span class="o">=</span> <span class="n">crypt</span><span class="o">.</span><span class="n">crypt</span><span class="p">(</span><span class="n">plaintext</span><span class="p">)</span>
  331. <span class="k">if</span> <span class="ow">not</span> <span class="n">compare_hash</span><span class="p">(</span><span class="n">hashed</span><span class="p">,</span> <span class="n">crypt</span><span class="o">.</span><span class="n">crypt</span><span class="p">(</span><span class="n">plaintext</span><span class="p">,</span> <span class="n">hashed</span><span class="p">)):</span>
  332. <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;hashed version doesn&#39;t validate against original&quot;</span><span class="p">)</span>
  333. </pre></div>
  334. </div>
  335. </section>
  336. </section>
  337. <div class="clearer"></div>
  338. </div>
  339. </div>
  340. </div>
  341. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  342. <div class="sphinxsidebarwrapper">
  343. <div>
  344. <h3><a href="../contents.html">Table of Contents</a></h3>
  345. <ul>
  346. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> — Function to check Unix passwords</a><ul>
  347. <li><a class="reference internal" href="#hashing-methods">Hashing Methods</a></li>
  348. <li><a class="reference internal" href="#module-attributes">Module Attributes</a></li>
  349. <li><a class="reference internal" href="#module-functions">Module Functions</a></li>
  350. <li><a class="reference internal" href="#examples">Examples</a></li>
  351. </ul>
  352. </li>
  353. </ul>
  354. </div>
  355. <div>
  356. <h4>Previous topic</h4>
  357. <p class="topless"><a href="chunk.html"
  358. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">chunk</span></code> — Read IFF chunked data</a></p>
  359. </div>
  360. <div>
  361. <h4>Next topic</h4>
  362. <p class="topless"><a href="imghdr.html"
  363. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">imghdr</span></code> — Determine the type of an image</a></p>
  364. </div>
  365. <div role="note" aria-label="source link">
  366. <h3>This Page</h3>
  367. <ul class="this-page-menu">
  368. <li><a href="../bugs.html">Report a Bug</a></li>
  369. <li>
  370. <a href="https://github.com/python/cpython/blob/main/Doc/library/crypt.rst"
  371. rel="nofollow">Show Source
  372. </a>
  373. </li>
  374. </ul>
  375. </div>
  376. </div>
  377. <div id="sidebarbutton" title="Collapse sidebar">
  378. <span>«</span>
  379. </div>
  380. </div>
  381. <div class="clearer"></div>
  382. </div>
  383. <div class="related" role="navigation" aria-label="related navigation">
  384. <h3>Navigation</h3>
  385. <ul>
  386. <li class="right" style="margin-right: 10px">
  387. <a href="../genindex.html" title="General Index"
  388. >index</a></li>
  389. <li class="right" >
  390. <a href="../py-modindex.html" title="Python Module Index"
  391. >modules</a> |</li>
  392. <li class="right" >
  393. <a href="imghdr.html" title="imghdr — Determine the type of an image"
  394. >next</a> |</li>
  395. <li class="right" >
  396. <a href="chunk.html" title="chunk — Read IFF chunked data"
  397. >previous</a> |</li>
  398. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  399. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  400. <li class="switchers">
  401. <div class="language_switcher_placeholder"></div>
  402. <div class="version_switcher_placeholder"></div>
  403. </li>
  404. <li>
  405. </li>
  406. <li id="cpython-language-and-version">
  407. <a href="../index.html">3.12.3 Documentation</a> &#187;
  408. </li>
  409. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  410. <li class="nav-item nav-item-2"><a href="superseded.html" >Superseded Modules</a> &#187;</li>
  411. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">crypt</span></code> — Function to check Unix passwords</a></li>
  412. <li class="right">
  413. <div class="inline-search" role="search">
  414. <form class="inline-search" action="../search.html" method="get">
  415. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  416. <input type="submit" value="Go" />
  417. </form>
  418. </div>
  419. |
  420. </li>
  421. <li class="right">
  422. <label class="theme-selector-label">
  423. Theme
  424. <select class="theme-selector" oninput="activateTheme(this.value)">
  425. <option value="auto" selected>Auto</option>
  426. <option value="light">Light</option>
  427. <option value="dark">Dark</option>
  428. </select>
  429. </label> |</li>
  430. </ul>
  431. </div>
  432. <div class="footer">
  433. &copy;
  434. <a href="../copyright.html">
  435. Copyright
  436. </a>
  437. 2001-2024, Python Software Foundation.
  438. <br />
  439. This page is licensed under the Python Software Foundation License Version 2.
  440. <br />
  441. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  442. <br />
  443. See <a href="/license.html">History and License</a> for more information.<br />
  444. <br />
  445. The Python Software Foundation is a non-profit corporation.
  446. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  447. <br />
  448. <br />
  449. Last updated on Apr 09, 2024 (13:47 UTC).
  450. <a href="/bugs.html">Found a bug</a>?
  451. <br />
  452. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  453. </div>
  454. </body>
  455. </html>
上海开阖软件有限公司 沪ICP备12045867号-1