gooderp18绿色标准版
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

1211 lines
115KB

  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="hashlib — Secure hashes and message digests" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/hashlib.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/hashlib.py This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256,..." />
  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/hashlib.py This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256,..." />
  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>hashlib — Secure hashes and message digests &#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="hmac — Keyed-Hashing for Message Authentication" href="hmac.html" />
  33. <link rel="prev" title="Cryptographic Services" href="crypto.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/hashlib.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">hashlib</span></code> — Secure hashes and message digests</a><ul>
  86. <li><a class="reference internal" href="#hash-algorithms">Hash algorithms</a></li>
  87. <li><a class="reference internal" href="#usage">Usage</a></li>
  88. <li><a class="reference internal" href="#constructors">Constructors</a></li>
  89. <li><a class="reference internal" href="#attributes">Attributes</a></li>
  90. <li><a class="reference internal" href="#hash-objects">Hash Objects</a></li>
  91. <li><a class="reference internal" href="#shake-variable-length-digests">SHAKE variable length digests</a></li>
  92. <li><a class="reference internal" href="#file-hashing">File hashing</a></li>
  93. <li><a class="reference internal" href="#key-derivation">Key derivation</a></li>
  94. <li><a class="reference internal" href="#blake2">BLAKE2</a><ul>
  95. <li><a class="reference internal" href="#creating-hash-objects">Creating hash objects</a></li>
  96. <li><a class="reference internal" href="#constants">Constants</a></li>
  97. <li><a class="reference internal" href="#examples">Examples</a><ul>
  98. <li><a class="reference internal" href="#simple-hashing">Simple hashing</a></li>
  99. <li><a class="reference internal" href="#using-different-digest-sizes">Using different digest sizes</a></li>
  100. <li><a class="reference internal" href="#keyed-hashing">Keyed hashing</a></li>
  101. <li><a class="reference internal" href="#randomized-hashing">Randomized hashing</a></li>
  102. <li><a class="reference internal" href="#personalization">Personalization</a></li>
  103. <li><a class="reference internal" href="#tree-mode">Tree mode</a></li>
  104. </ul>
  105. </li>
  106. <li><a class="reference internal" href="#credits">Credits</a></li>
  107. </ul>
  108. </li>
  109. </ul>
  110. </li>
  111. </ul>
  112. </div>
  113. <div>
  114. <h4>Previous topic</h4>
  115. <p class="topless"><a href="crypto.html"
  116. title="previous chapter">Cryptographic Services</a></p>
  117. </div>
  118. <div>
  119. <h4>Next topic</h4>
  120. <p class="topless"><a href="hmac.html"
  121. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code> — Keyed-Hashing for Message Authentication</a></p>
  122. </div>
  123. <div role="note" aria-label="source link">
  124. <h3>This Page</h3>
  125. <ul class="this-page-menu">
  126. <li><a href="../bugs.html">Report a Bug</a></li>
  127. <li>
  128. <a href="https://github.com/python/cpython/blob/main/Doc/library/hashlib.rst"
  129. rel="nofollow">Show Source
  130. </a>
  131. </li>
  132. </ul>
  133. </div>
  134. </nav>
  135. </div>
  136. </div>
  137. <div class="related" role="navigation" aria-label="related navigation">
  138. <h3>Navigation</h3>
  139. <ul>
  140. <li class="right" style="margin-right: 10px">
  141. <a href="../genindex.html" title="General Index"
  142. accesskey="I">index</a></li>
  143. <li class="right" >
  144. <a href="../py-modindex.html" title="Python Module Index"
  145. >modules</a> |</li>
  146. <li class="right" >
  147. <a href="hmac.html" title="hmac — Keyed-Hashing for Message Authentication"
  148. accesskey="N">next</a> |</li>
  149. <li class="right" >
  150. <a href="crypto.html" title="Cryptographic Services"
  151. accesskey="P">previous</a> |</li>
  152. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  153. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  154. <li class="switchers">
  155. <div class="language_switcher_placeholder"></div>
  156. <div class="version_switcher_placeholder"></div>
  157. </li>
  158. <li>
  159. </li>
  160. <li id="cpython-language-and-version">
  161. <a href="../index.html">3.12.3 Documentation</a> &#187;
  162. </li>
  163. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  164. <li class="nav-item nav-item-2"><a href="crypto.html" accesskey="U">Cryptographic Services</a> &#187;</li>
  165. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code> — Secure hashes and message digests</a></li>
  166. <li class="right">
  167. <div class="inline-search" role="search">
  168. <form class="inline-search" action="../search.html" method="get">
  169. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  170. <input type="submit" value="Go" />
  171. </form>
  172. </div>
  173. |
  174. </li>
  175. <li class="right">
  176. <label class="theme-selector-label">
  177. Theme
  178. <select class="theme-selector" oninput="activateTheme(this.value)">
  179. <option value="auto" selected>Auto</option>
  180. <option value="light">Light</option>
  181. <option value="dark">Dark</option>
  182. </select>
  183. </label> |</li>
  184. </ul>
  185. </div>
  186. <div class="document">
  187. <div class="documentwrapper">
  188. <div class="bodywrapper">
  189. <div class="body" role="main">
  190. <section id="module-hashlib">
  191. <span id="hashlib-secure-hashes-and-message-digests"></span><h1><a class="reference internal" href="#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> — Secure hashes and message digests<a class="headerlink" href="#module-hashlib" title="Link to this heading">¶</a></h1>
  192. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/hashlib.py">Lib/hashlib.py</a></p>
  193. <span class="target" id="index-0"></span><hr class="docutils" />
  194. <p>This module implements a common interface to many different secure hash and
  195. message digest algorithms. Included are the FIPS secure hash algorithms SHA1,
  196. SHA224, SHA256, SHA384, SHA512, (defined in <a class="reference external" href="https://csrc.nist.gov/publications/detail/fips/180/4/final">the FIPS 180-4 standard</a>),
  197. the SHA-3 series (defined in <a class="reference external" href="https://csrc.nist.gov/publications/detail/fips/202/final">the FIPS 202 standard</a>) as well as RSA’s MD5
  198. algorithm (defined in internet <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1321.html"><strong>RFC 1321</strong></a>). The terms “secure hash” and
  199. “message digest” are interchangeable. Older algorithms were called message
  200. digests. The modern term is secure hash.</p>
  201. <div class="admonition note">
  202. <p class="admonition-title">Note</p>
  203. <p>If you want the adler32 or crc32 hash functions, they are available in
  204. the <a class="reference internal" href="zlib.html#module-zlib" title="zlib: Low-level interface to compression and decompression routines compatible with gzip."><code class="xref py py-mod docutils literal notranslate"><span class="pre">zlib</span></code></a> module.</p>
  205. </div>
  206. <section id="hash-algorithms">
  207. <span id="id1"></span><h2>Hash algorithms<a class="headerlink" href="#hash-algorithms" title="Link to this heading">¶</a></h2>
  208. <p>There is one constructor method named for each type of <em class="dfn">hash</em>. All return
  209. a hash object with the same simple interface. For example: use <a class="reference internal" href="#hashlib.sha256" title="hashlib.sha256"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha256()</span></code></a>
  210. to create a SHA-256 hash object. You can now feed this object with
  211. <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> (normally <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>) using
  212. the <a class="reference internal" href="#hashlib.hash.update" title="hashlib.hash.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update</span></code></a> method. At any point you can ask it for the
  213. <em class="dfn">digest</em> of the concatenation of the data fed to it so far using the
  214. <a class="reference internal" href="#hashlib.hash.digest" title="hashlib.hash.digest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">digest()</span></code></a> or <a class="reference internal" href="#hashlib.hash.hexdigest" title="hashlib.hash.hexdigest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hexdigest()</span></code></a> methods.</p>
  215. <p>To allow multithreading, the Python <a class="reference internal" href="../glossary.html#term-GIL"><span class="xref std std-term">GIL</span></a> is released while computing a
  216. hash supplied more than 2047 bytes of data at once in its constructor or
  217. <a class="reference internal" href="#hashlib.hash.update" title="hashlib.hash.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">.update</span></code></a> method.</p>
  218. <p id="index-2">Constructors for hash algorithms that are always present in this module are
  219. <a class="reference internal" href="#hashlib.sha1" title="hashlib.sha1"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha1()</span></code></a>, <a class="reference internal" href="#hashlib.sha224" title="hashlib.sha224"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha224()</span></code></a>, <a class="reference internal" href="#hashlib.sha256" title="hashlib.sha256"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha256()</span></code></a>, <a class="reference internal" href="#hashlib.sha384" title="hashlib.sha384"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha384()</span></code></a>, <a class="reference internal" href="#hashlib.sha512" title="hashlib.sha512"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha512()</span></code></a>,
  220. <a class="reference internal" href="#hashlib.sha3_224" title="hashlib.sha3_224"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_224()</span></code></a>, <a class="reference internal" href="#hashlib.sha3_256" title="hashlib.sha3_256"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_256()</span></code></a>, <a class="reference internal" href="#hashlib.sha3_384" title="hashlib.sha3_384"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_384()</span></code></a>, <a class="reference internal" href="#hashlib.sha3_512" title="hashlib.sha3_512"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_512()</span></code></a>,
  221. <a class="reference internal" href="#hashlib.shake_128" title="hashlib.shake_128"><code class="xref py py-func docutils literal notranslate"><span class="pre">shake_128()</span></code></a>, <a class="reference internal" href="#hashlib.shake_256" title="hashlib.shake_256"><code class="xref py py-func docutils literal notranslate"><span class="pre">shake_256()</span></code></a>, <a class="reference internal" href="#hashlib.blake2b" title="hashlib.blake2b"><code class="xref py py-func docutils literal notranslate"><span class="pre">blake2b()</span></code></a>, and <a class="reference internal" href="#hashlib.blake2s" title="hashlib.blake2s"><code class="xref py py-func docutils literal notranslate"><span class="pre">blake2s()</span></code></a>.
  222. <a class="reference internal" href="#hashlib.md5" title="hashlib.md5"><code class="xref py py-func docutils literal notranslate"><span class="pre">md5()</span></code></a> is normally available as well, though it may be missing or blocked
  223. if you are using a rare “FIPS compliant” build of Python.
  224. These correspond to <a class="reference internal" href="#hashlib.algorithms_guaranteed" title="hashlib.algorithms_guaranteed"><code class="xref py py-data docutils literal notranslate"><span class="pre">algorithms_guaranteed</span></code></a>.</p>
  225. <p>Additional algorithms may also be available if your Python distribution’s
  226. <a class="reference internal" href="#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> was linked against a build of OpenSSL that provides others.
  227. Others <em>are not guaranteed available</em> on all installations and will only be
  228. accessible by name via <a class="reference internal" href="#hashlib.new" title="hashlib.new"><code class="xref py py-func docutils literal notranslate"><span class="pre">new()</span></code></a>. See <a class="reference internal" href="#hashlib.algorithms_available" title="hashlib.algorithms_available"><code class="xref py py-data docutils literal notranslate"><span class="pre">algorithms_available</span></code></a>.</p>
  229. <div class="admonition warning">
  230. <p class="admonition-title">Warning</p>
  231. <p>Some algorithms have known hash collision weaknesses (including MD5 and
  232. SHA1). Refer to <a class="reference external" href="https://en.wikipedia.org/wiki/Cryptographic_hash_function#Attacks_on_cryptographic_hash_algorithms">Attacks on cryptographic hash algorithms</a> and the
  233. <a class="reference internal" href="#hashlib-seealso">hashlib-seealso</a> section at the end of this document.</p>
  234. </div>
  235. <div class="versionadded">
  236. <p><span class="versionmodified added">New in version 3.6: </span>SHA3 (Keccak) and SHAKE constructors <a class="reference internal" href="#hashlib.sha3_224" title="hashlib.sha3_224"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_224()</span></code></a>, <a class="reference internal" href="#hashlib.sha3_256" title="hashlib.sha3_256"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_256()</span></code></a>,
  237. <a class="reference internal" href="#hashlib.sha3_384" title="hashlib.sha3_384"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_384()</span></code></a>, <a class="reference internal" href="#hashlib.sha3_512" title="hashlib.sha3_512"><code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_512()</span></code></a>, <a class="reference internal" href="#hashlib.shake_128" title="hashlib.shake_128"><code class="xref py py-func docutils literal notranslate"><span class="pre">shake_128()</span></code></a>, <a class="reference internal" href="#hashlib.shake_256" title="hashlib.shake_256"><code class="xref py py-func docutils literal notranslate"><span class="pre">shake_256()</span></code></a>
  238. were added.
  239. <a class="reference internal" href="#hashlib.blake2b" title="hashlib.blake2b"><code class="xref py py-func docutils literal notranslate"><span class="pre">blake2b()</span></code></a> and <a class="reference internal" href="#hashlib.blake2s" title="hashlib.blake2s"><code class="xref py py-func docutils literal notranslate"><span class="pre">blake2s()</span></code></a> were added.</p>
  240. </div>
  241. <div class="versionchanged" id="hashlib-usedforsecurity">
  242. <p><span class="versionmodified changed">Changed in version 3.9: </span>All hashlib constructors take a keyword-only argument <em>usedforsecurity</em>
  243. with default value <code class="docutils literal notranslate"><span class="pre">True</span></code>. A false value allows the use of insecure and
  244. blocked hashing algorithms in restricted environments. <code class="docutils literal notranslate"><span class="pre">False</span></code> indicates
  245. that the hashing algorithm is not used in a security context, e.g. as a
  246. non-cryptographic one-way compression function.</p>
  247. </div>
  248. <div class="versionchanged">
  249. <p><span class="versionmodified changed">Changed in version 3.9: </span>Hashlib now uses SHA3 and SHAKE from OpenSSL if it provides it.</p>
  250. </div>
  251. <div class="versionchanged">
  252. <p><span class="versionmodified changed">Changed in version 3.12: </span>For any of the MD5, SHA1, SHA2, or SHA3 algorithms that the linked
  253. OpenSSL does not provide we fall back to a verified implementation from
  254. the <a class="reference external" href="https://github.com/hacl-star/hacl-star">HACL* project</a>.</p>
  255. </div>
  256. </section>
  257. <section id="usage">
  258. <h2>Usage<a class="headerlink" href="#usage" title="Link to this heading">¶</a></h2>
  259. <p>To obtain the digest of the byte string <code class="docutils literal notranslate"><span class="pre">b&quot;Nobody</span> <span class="pre">inspects</span> <span class="pre">the</span> <span class="pre">spammish</span>
  260. <span class="pre">repetition&quot;</span></code>:</p>
  261. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">hashlib</span>
  262. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">sha256</span><span class="p">()</span>
  263. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Nobody inspects&quot;</span><span class="p">)</span>
  264. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot; the spammish repetition&quot;</span><span class="p">)</span>
  265. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
  266. <span class="go">b&#39;\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06&#39;</span>
  267. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  268. <span class="go">&#39;031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406&#39;</span>
  269. </pre></div>
  270. </div>
  271. <p>More condensed:</p>
  272. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">hashlib</span><span class="o">.</span><span class="n">sha256</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Nobody inspects the spammish repetition&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  273. <span class="go">&#39;031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406&#39;</span>
  274. </pre></div>
  275. </div>
  276. </section>
  277. <section id="constructors">
  278. <h2>Constructors<a class="headerlink" href="#constructors" title="Link to this heading">¶</a></h2>
  279. <dl class="py function">
  280. <dt class="sig sig-object py" id="hashlib.new">
  281. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">new</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.new" title="Link to this definition">¶</a></dt>
  282. <dd><p>Is a generic constructor that takes the string <em>name</em> of the desired
  283. algorithm as its first parameter. It also exists to allow access to the
  284. above listed hashes as well as any other algorithms that your OpenSSL
  285. library may offer.</p>
  286. </dd></dl>
  287. <p>Using <a class="reference internal" href="#hashlib.new" title="hashlib.new"><code class="xref py py-func docutils literal notranslate"><span class="pre">new()</span></code></a> with an algorithm name:</p>
  288. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s1">&#39;sha256&#39;</span><span class="p">)</span>
  289. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Nobody inspects the spammish repetition&quot;</span><span class="p">)</span>
  290. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  291. <span class="go">&#39;031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406&#39;</span>
  292. </pre></div>
  293. </div>
  294. <dl class="py function">
  295. <dt class="sig sig-object py" id="hashlib.md5">
  296. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">md5</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.md5" title="Link to this definition">¶</a></dt>
  297. <dd></dd></dl>
  298. <dl class="py function">
  299. <dt class="sig sig-object py" id="hashlib.sha1">
  300. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha1</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha1" title="Link to this definition">¶</a></dt>
  301. <dd></dd></dl>
  302. <dl class="py function">
  303. <dt class="sig sig-object py" id="hashlib.sha224">
  304. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha224</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha224" title="Link to this definition">¶</a></dt>
  305. <dd></dd></dl>
  306. <dl class="py function">
  307. <dt class="sig sig-object py" id="hashlib.sha256">
  308. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha256</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha256" title="Link to this definition">¶</a></dt>
  309. <dd></dd></dl>
  310. <dl class="py function">
  311. <dt class="sig sig-object py" id="hashlib.sha384">
  312. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha384</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha384" title="Link to this definition">¶</a></dt>
  313. <dd></dd></dl>
  314. <dl class="py function">
  315. <dt class="sig sig-object py" id="hashlib.sha512">
  316. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha512</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha512" title="Link to this definition">¶</a></dt>
  317. <dd></dd></dl>
  318. <dl class="py function">
  319. <dt class="sig sig-object py" id="hashlib.sha3_224">
  320. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha3_224</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha3_224" title="Link to this definition">¶</a></dt>
  321. <dd></dd></dl>
  322. <dl class="py function">
  323. <dt class="sig sig-object py" id="hashlib.sha3_256">
  324. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha3_256</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha3_256" title="Link to this definition">¶</a></dt>
  325. <dd></dd></dl>
  326. <dl class="py function">
  327. <dt class="sig sig-object py" id="hashlib.sha3_384">
  328. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha3_384</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha3_384" title="Link to this definition">¶</a></dt>
  329. <dd></dd></dl>
  330. <dl class="py function">
  331. <dt class="sig sig-object py" id="hashlib.sha3_512">
  332. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">sha3_512</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.sha3_512" title="Link to this definition">¶</a></dt>
  333. <dd></dd></dl>
  334. <p>Named constructors such as these are faster than passing an algorithm name to
  335. <a class="reference internal" href="#hashlib.new" title="hashlib.new"><code class="xref py py-func docutils literal notranslate"><span class="pre">new()</span></code></a>.</p>
  336. </section>
  337. <section id="attributes">
  338. <h2>Attributes<a class="headerlink" href="#attributes" title="Link to this heading">¶</a></h2>
  339. <p>Hashlib provides the following constant module attributes:</p>
  340. <dl class="py data">
  341. <dt class="sig sig-object py" id="hashlib.algorithms_guaranteed">
  342. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">algorithms_guaranteed</span></span><a class="headerlink" href="#hashlib.algorithms_guaranteed" title="Link to this definition">¶</a></dt>
  343. <dd><p>A set containing the names of the hash algorithms guaranteed to be supported
  344. by this module on all platforms. Note that ‘md5’ is in this list despite
  345. some upstream vendors offering an odd “FIPS compliant” Python build that
  346. excludes it.</p>
  347. <div class="versionadded">
  348. <p><span class="versionmodified added">New in version 3.2.</span></p>
  349. </div>
  350. </dd></dl>
  351. <dl class="py data">
  352. <dt class="sig sig-object py" id="hashlib.algorithms_available">
  353. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">algorithms_available</span></span><a class="headerlink" href="#hashlib.algorithms_available" title="Link to this definition">¶</a></dt>
  354. <dd><p>A set containing the names of the hash algorithms that are available in the
  355. running Python interpreter. These names will be recognized when passed to
  356. <a class="reference internal" href="#hashlib.new" title="hashlib.new"><code class="xref py py-func docutils literal notranslate"><span class="pre">new()</span></code></a>. <a class="reference internal" href="#hashlib.algorithms_guaranteed" title="hashlib.algorithms_guaranteed"><code class="xref py py-attr docutils literal notranslate"><span class="pre">algorithms_guaranteed</span></code></a> will always be a subset. The
  357. same algorithm may appear multiple times in this set under different names
  358. (thanks to OpenSSL).</p>
  359. <div class="versionadded">
  360. <p><span class="versionmodified added">New in version 3.2.</span></p>
  361. </div>
  362. </dd></dl>
  363. </section>
  364. <section id="hash-objects">
  365. <h2>Hash Objects<a class="headerlink" href="#hash-objects" title="Link to this heading">¶</a></h2>
  366. <p>The following values are provided as constant attributes of the hash objects
  367. returned by the constructors:</p>
  368. <dl class="py data">
  369. <dt class="sig sig-object py" id="hashlib.hash.digest_size">
  370. <span class="sig-prename descclassname"><span class="pre">hash.</span></span><span class="sig-name descname"><span class="pre">digest_size</span></span><a class="headerlink" href="#hashlib.hash.digest_size" title="Link to this definition">¶</a></dt>
  371. <dd><p>The size of the resulting hash in bytes.</p>
  372. </dd></dl>
  373. <dl class="py data">
  374. <dt class="sig sig-object py" id="hashlib.hash.block_size">
  375. <span class="sig-prename descclassname"><span class="pre">hash.</span></span><span class="sig-name descname"><span class="pre">block_size</span></span><a class="headerlink" href="#hashlib.hash.block_size" title="Link to this definition">¶</a></dt>
  376. <dd><p>The internal block size of the hash algorithm in bytes.</p>
  377. </dd></dl>
  378. <p>A hash object has the following attributes:</p>
  379. <dl class="py attribute">
  380. <dt class="sig sig-object py" id="hashlib.hash.name">
  381. <span class="sig-prename descclassname"><span class="pre">hash.</span></span><span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#hashlib.hash.name" title="Link to this definition">¶</a></dt>
  382. <dd><p>The canonical name of this hash, always lowercase and always suitable as a
  383. parameter to <a class="reference internal" href="#hashlib.new" title="hashlib.new"><code class="xref py py-func docutils literal notranslate"><span class="pre">new()</span></code></a> to create another hash of this type.</p>
  384. <div class="versionchanged">
  385. <p><span class="versionmodified changed">Changed in version 3.4: </span>The name attribute has been present in CPython since its inception, but
  386. until Python 3.4 was not formally specified, so may not exist on some
  387. platforms.</p>
  388. </div>
  389. </dd></dl>
  390. <p>A hash object has the following methods:</p>
  391. <dl class="py method">
  392. <dt class="sig sig-object py" id="hashlib.hash.update">
  393. <span class="sig-prename descclassname"><span class="pre">hash.</span></span><span class="sig-name descname"><span class="pre">update</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.update" title="Link to this definition">¶</a></dt>
  394. <dd><p>Update the hash object with the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>.
  395. Repeated calls are equivalent to a single call with the
  396. concatenation of all the arguments: <code class="docutils literal notranslate"><span class="pre">m.update(a);</span> <span class="pre">m.update(b)</span></code> is
  397. equivalent to <code class="docutils literal notranslate"><span class="pre">m.update(a+b)</span></code>.</p>
  398. </dd></dl>
  399. <dl class="py method">
  400. <dt class="sig sig-object py" id="hashlib.hash.digest">
  401. <span class="sig-prename descclassname"><span class="pre">hash.</span></span><span class="sig-name descname"><span class="pre">digest</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.digest" title="Link to this definition">¶</a></dt>
  402. <dd><p>Return the digest of the data passed to the <a class="reference internal" href="#hashlib.hash.update" title="hashlib.hash.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a> method so far.
  403. This is a bytes object of size <a class="reference internal" href="#hashlib.hash.digest_size" title="hashlib.hash.digest_size"><code class="xref py py-attr docutils literal notranslate"><span class="pre">digest_size</span></code></a> which may contain bytes in
  404. the whole range from 0 to 255.</p>
  405. </dd></dl>
  406. <dl class="py method">
  407. <dt class="sig sig-object py" id="hashlib.hash.hexdigest">
  408. <span class="sig-prename descclassname"><span class="pre">hash.</span></span><span class="sig-name descname"><span class="pre">hexdigest</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.hexdigest" title="Link to this definition">¶</a></dt>
  409. <dd><p>Like <a class="reference internal" href="#hashlib.hash.digest" title="hashlib.hash.digest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">digest()</span></code></a> except the digest is returned as a string object of
  410. double length, containing only hexadecimal digits. This may be used to
  411. exchange the value safely in email or other non-binary environments.</p>
  412. </dd></dl>
  413. <dl class="py method">
  414. <dt class="sig sig-object py" id="hashlib.hash.copy">
  415. <span class="sig-prename descclassname"><span class="pre">hash.</span></span><span class="sig-name descname"><span class="pre">copy</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.copy" title="Link to this definition">¶</a></dt>
  416. <dd><p>Return a copy (“clone”) of the hash object. This can be used to efficiently
  417. compute the digests of data sharing a common initial substring.</p>
  418. </dd></dl>
  419. </section>
  420. <section id="shake-variable-length-digests">
  421. <h2>SHAKE variable length digests<a class="headerlink" href="#shake-variable-length-digests" title="Link to this heading">¶</a></h2>
  422. <dl class="py function">
  423. <dt class="sig sig-object py" id="hashlib.shake_128">
  424. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">shake_128</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.shake_128" title="Link to this definition">¶</a></dt>
  425. <dd></dd></dl>
  426. <dl class="py function">
  427. <dt class="sig sig-object py" id="hashlib.shake_256">
  428. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">shake_256</span></span><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param"><span class="n"><span class="pre">data</span></span></em>, <span class="optional">]</span><em class="sig-param"><span class="n"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity=True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.shake_256" title="Link to this definition">¶</a></dt>
  429. <dd></dd></dl>
  430. <p>The <a class="reference internal" href="#hashlib.shake_128" title="hashlib.shake_128"><code class="xref py py-func docutils literal notranslate"><span class="pre">shake_128()</span></code></a> and <a class="reference internal" href="#hashlib.shake_256" title="hashlib.shake_256"><code class="xref py py-func docutils literal notranslate"><span class="pre">shake_256()</span></code></a> algorithms provide variable
  431. length digests with length_in_bits//2 up to 128 or 256 bits of security.
  432. As such, their digest methods require a length. Maximum length is not limited
  433. by the SHAKE algorithm.</p>
  434. <dl class="py method">
  435. <dt class="sig sig-object py" id="hashlib.shake.digest">
  436. <span class="sig-prename descclassname"><span class="pre">shake.</span></span><span class="sig-name descname"><span class="pre">digest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">length</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.shake.digest" title="Link to this definition">¶</a></dt>
  437. <dd><p>Return the digest of the data passed to the <a class="reference internal" href="#hashlib.hash.update" title="hashlib.hash.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a> method so far.
  438. This is a bytes object of size <em>length</em> which may contain bytes in
  439. the whole range from 0 to 255.</p>
  440. </dd></dl>
  441. <dl class="py method">
  442. <dt class="sig sig-object py" id="hashlib.shake.hexdigest">
  443. <span class="sig-prename descclassname"><span class="pre">shake.</span></span><span class="sig-name descname"><span class="pre">hexdigest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">length</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.shake.hexdigest" title="Link to this definition">¶</a></dt>
  444. <dd><p>Like <a class="reference internal" href="#hashlib.shake.digest" title="hashlib.shake.digest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">digest()</span></code></a> except the digest is returned as a string object of
  445. double length, containing only hexadecimal digits. This may be used to
  446. exchange the value in email or other non-binary environments.</p>
  447. </dd></dl>
  448. <p>Example use:</p>
  449. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">shake_256</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Nobody inspects the spammish repetition&#39;</span><span class="p">)</span>
  450. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
  451. <span class="go">&#39;44709d6fcb83d92a76dcb0b668c98e1b1d3dafe7&#39;</span>
  452. </pre></div>
  453. </div>
  454. </section>
  455. <section id="file-hashing">
  456. <h2>File hashing<a class="headerlink" href="#file-hashing" title="Link to this heading">¶</a></h2>
  457. <p>The hashlib module provides a helper function for efficient hashing of
  458. a file or file-like object.</p>
  459. <dl class="py function">
  460. <dt class="sig sig-object py" id="hashlib.file_digest">
  461. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">file_digest</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fileobj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">digest</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.file_digest" title="Link to this definition">¶</a></dt>
  462. <dd><p>Return a digest object that has been updated with contents of file object.</p>
  463. <p><em>fileobj</em> must be a file-like object opened for reading in binary mode.
  464. It accepts file objects from builtin <a class="reference internal" href="functions.html#open" title="open"><code class="xref py py-func docutils literal notranslate"><span class="pre">open()</span></code></a>, <a class="reference internal" href="io.html#io.BytesIO" title="io.BytesIO"><code class="xref py py-class docutils literal notranslate"><span class="pre">BytesIO</span></code></a>
  465. instances, SocketIO objects from <a class="reference internal" href="socket.html#socket.socket.makefile" title="socket.socket.makefile"><code class="xref py py-meth docutils literal notranslate"><span class="pre">socket.socket.makefile()</span></code></a>, and
  466. similar. The function may bypass Python’s I/O and use the file descriptor
  467. from <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> directly. <em>fileobj</em> must be assumed to be
  468. in an unknown state after this function returns or raises. It is up to
  469. the caller to close <em>fileobj</em>.</p>
  470. <p><em>digest</em> must either be a hash algorithm name as a <em>str</em>, a hash
  471. constructor, or a callable that returns a hash object.</p>
  472. <p>Example:</p>
  473. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">io</span><span class="o">,</span> <span class="nn">hashlib</span><span class="o">,</span> <span class="nn">hmac</span>
  474. <span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">hashlib</span><span class="o">.</span><span class="vm">__file__</span><span class="p">,</span> <span class="s2">&quot;rb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
  475. <span class="gp">... </span> <span class="n">digest</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">file_digest</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s2">&quot;sha256&quot;</span><span class="p">)</span>
  476. <span class="gp">...</span>
  477. <span class="gp">&gt;&gt;&gt; </span><span class="n">digest</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  478. <span class="go">&#39;...&#39;</span>
  479. </pre></div>
  480. </div>
  481. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">buf</span> <span class="o">=</span> <span class="n">io</span><span class="o">.</span><span class="n">BytesIO</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;somedata&quot;</span><span class="p">)</span>
  482. <span class="gp">&gt;&gt;&gt; </span><span class="n">mac1</span> <span class="o">=</span> <span class="n">hmac</span><span class="o">.</span><span class="n">HMAC</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;key&quot;</span><span class="p">,</span> <span class="n">digestmod</span><span class="o">=</span><span class="n">hashlib</span><span class="o">.</span><span class="n">sha512</span><span class="p">)</span>
  483. <span class="gp">&gt;&gt;&gt; </span><span class="n">digest</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">file_digest</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span> <span class="k">lambda</span><span class="p">:</span> <span class="n">mac1</span><span class="p">)</span>
  484. </pre></div>
  485. </div>
  486. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digest</span> <span class="ow">is</span> <span class="n">mac1</span>
  487. <span class="go">True</span>
  488. <span class="gp">&gt;&gt;&gt; </span><span class="n">mac2</span> <span class="o">=</span> <span class="n">hmac</span><span class="o">.</span><span class="n">HMAC</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;key&quot;</span><span class="p">,</span> <span class="sa">b</span><span class="s2">&quot;somedata&quot;</span><span class="p">,</span> <span class="n">digestmod</span><span class="o">=</span><span class="n">hashlib</span><span class="o">.</span><span class="n">sha512</span><span class="p">)</span>
  489. <span class="gp">&gt;&gt;&gt; </span><span class="n">mac1</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span> <span class="o">==</span> <span class="n">mac2</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
  490. <span class="go">True</span>
  491. </pre></div>
  492. </div>
  493. <div class="versionadded">
  494. <p><span class="versionmodified added">New in version 3.11.</span></p>
  495. </div>
  496. </dd></dl>
  497. </section>
  498. <section id="key-derivation">
  499. <h2>Key derivation<a class="headerlink" href="#key-derivation" title="Link to this heading">¶</a></h2>
  500. <p>Key derivation and key stretching algorithms are designed for secure password
  501. hashing. Naive algorithms such as <code class="docutils literal notranslate"><span class="pre">sha1(password)</span></code> are not resistant against
  502. brute-force attacks. A good password hashing function must be tunable, slow, and
  503. include a <a class="reference external" href="https://en.wikipedia.org/wiki/Salt_%28cryptography%29">salt</a>.</p>
  504. <dl class="py function">
  505. <dt class="sig sig-object py" id="hashlib.pbkdf2_hmac">
  506. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">pbkdf2_hmac</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">hash_name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">password</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">salt</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">iterations</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dklen</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="#hashlib.pbkdf2_hmac" title="Link to this definition">¶</a></dt>
  507. <dd><p>The function provides PKCS#5 password-based key derivation function 2. It
  508. uses HMAC as pseudorandom function.</p>
  509. <p>The string <em>hash_name</em> is the desired name of the hash digest algorithm for
  510. HMAC, e.g. ‘sha1’ or ‘sha256’. <em>password</em> and <em>salt</em> are interpreted as
  511. buffers of bytes. Applications and libraries should limit <em>password</em> to
  512. a sensible length (e.g. 1024). <em>salt</em> should be about 16 or more bytes from
  513. a proper source, e.g. <a class="reference internal" href="os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.urandom()</span></code></a>.</p>
  514. <p>The number of <em>iterations</em> should be chosen based on the hash algorithm and
  515. computing power. As of 2022, hundreds of thousands of iterations of SHA-256
  516. are suggested. For rationale as to why and how to choose what is best for
  517. your application, read <em>Appendix A.2.2</em> of <a class="reference external" href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">NIST-SP-800-132</a>. The answers
  518. on the <a class="reference external" href="https://security.stackexchange.com/questions/3959/recommended-of-iterations-when-using-pbkdf2-sha256/">stackexchange pbkdf2 iterations question</a> explain in detail.</p>
  519. <p><em>dklen</em> is the length of the derived key. If <em>dklen</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> then the
  520. digest size of the hash algorithm <em>hash_name</em> is used, e.g. 64 for SHA-512.</p>
  521. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">pbkdf2_hmac</span>
  522. <span class="gp">&gt;&gt;&gt; </span><span class="n">our_app_iters</span> <span class="o">=</span> <span class="mi">500_000</span> <span class="c1"># Application specific, read above.</span>
  523. <span class="gp">&gt;&gt;&gt; </span><span class="n">dk</span> <span class="o">=</span> <span class="n">pbkdf2_hmac</span><span class="p">(</span><span class="s1">&#39;sha256&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;password&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;bad salt&#39;</span> <span class="o">*</span> <span class="mi">2</span><span class="p">,</span> <span class="n">our_app_iters</span><span class="p">)</span>
  524. <span class="gp">&gt;&gt;&gt; </span><span class="n">dk</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
  525. <span class="go">&#39;15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9&#39;</span>
  526. </pre></div>
  527. </div>
  528. <p>Function only available when Python is compiled with OpenSSL.</p>
  529. <div class="versionadded">
  530. <p><span class="versionmodified added">New in version 3.4.</span></p>
  531. </div>
  532. <div class="versionchanged">
  533. <p><span class="versionmodified changed">Changed in version 3.12: </span>Function now only available when Python is built with OpenSSL. The slow
  534. pure Python implementation has been removed.</p>
  535. </div>
  536. </dd></dl>
  537. <dl class="py function">
  538. <dt class="sig sig-object py" id="hashlib.scrypt">
  539. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">scrypt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">password</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">salt</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">r</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">maxmem</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">dklen</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">64</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.scrypt" title="Link to this definition">¶</a></dt>
  540. <dd><p>The function provides scrypt password-based key derivation function as
  541. defined in <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7914.html"><strong>RFC 7914</strong></a>.</p>
  542. <p><em>password</em> and <em>salt</em> must be <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a>. Applications and libraries should limit <em>password</em>
  543. to a sensible length (e.g. 1024). <em>salt</em> should be about 16 or more
  544. bytes from a proper source, e.g. <a class="reference internal" href="os.html#os.urandom" title="os.urandom"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.urandom()</span></code></a>.</p>
  545. <p><em>n</em> is the CPU/Memory cost factor, <em>r</em> the block size, <em>p</em> parallelization
  546. factor and <em>maxmem</em> limits memory (OpenSSL 1.1.0 defaults to 32 MiB).
  547. <em>dklen</em> is the length of the derived key.</p>
  548. <div class="versionadded">
  549. <p><span class="versionmodified added">New in version 3.6.</span></p>
  550. </div>
  551. </dd></dl>
  552. </section>
  553. <section id="blake2">
  554. <h2>BLAKE2<a class="headerlink" href="#blake2" title="Link to this heading">¶</a></h2>
  555. <p id="index-4"><a class="reference external" href="https://www.blake2.net">BLAKE2</a> is a cryptographic hash function defined in <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7693.html"><strong>RFC 7693</strong></a> that comes in two
  556. flavors:</p>
  557. <ul class="simple">
  558. <li><p><strong>BLAKE2b</strong>, optimized for 64-bit platforms and produces digests of any size
  559. between 1 and 64 bytes,</p></li>
  560. <li><p><strong>BLAKE2s</strong>, optimized for 8- to 32-bit platforms and produces digests of any
  561. size between 1 and 32 bytes.</p></li>
  562. </ul>
  563. <p>BLAKE2 supports <strong>keyed mode</strong> (a faster and simpler replacement for <a class="reference external" href="https://en.wikipedia.org/wiki/Hash-based_message_authentication_code">HMAC</a>),
  564. <strong>salted hashing</strong>, <strong>personalization</strong>, and <strong>tree hashing</strong>.</p>
  565. <p>Hash objects from this module follow the API of standard library’s
  566. <a class="reference internal" href="#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> objects.</p>
  567. <section id="creating-hash-objects">
  568. <h3>Creating hash objects<a class="headerlink" href="#creating-hash-objects" title="Link to this heading">¶</a></h3>
  569. <p>New hash objects are created by calling constructor functions:</p>
  570. <dl class="py function">
  571. <dt class="sig sig-object py" id="hashlib.blake2b">
  572. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">blake2b</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</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">digest_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">64</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</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">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">person</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fanout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">leaf_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">node_offset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">node_depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">inner_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">last_node</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.blake2b" title="Link to this definition">¶</a></dt>
  573. <dd></dd></dl>
  574. <dl class="py function">
  575. <dt class="sig sig-object py" id="hashlib.blake2s">
  576. <span class="sig-prename descclassname"><span class="pre">hashlib.</span></span><span class="sig-name descname"><span class="pre">blake2s</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">data</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</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">digest_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">32</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">key</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</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">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">person</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fanout</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">leaf_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">node_offset</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">node_depth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">inner_size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">last_node</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">usedforsecurity</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.blake2s" title="Link to this definition">¶</a></dt>
  577. <dd></dd></dl>
  578. <p>These functions return the corresponding hash objects for calculating
  579. BLAKE2b or BLAKE2s. They optionally take these general parameters:</p>
  580. <ul class="simple">
  581. <li><p><em>data</em>: initial chunk of data to hash, which must be
  582. <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>. It can be passed only as positional argument.</p></li>
  583. <li><p><em>digest_size</em>: size of output digest in bytes.</p></li>
  584. <li><p><em>key</em>: key for keyed hashing (up to 64 bytes for BLAKE2b, up to 32 bytes for
  585. BLAKE2s).</p></li>
  586. <li><p><em>salt</em>: salt for randomized hashing (up to 16 bytes for BLAKE2b, up to 8
  587. bytes for BLAKE2s).</p></li>
  588. <li><p><em>person</em>: personalization string (up to 16 bytes for BLAKE2b, up to 8 bytes
  589. for BLAKE2s).</p></li>
  590. </ul>
  591. <p>The following table shows limits for general parameters (in bytes):</p>
  592. <table class="docutils align-default">
  593. <thead>
  594. <tr class="row-odd"><th class="head"><p>Hash</p></th>
  595. <th class="head"><p>digest_size</p></th>
  596. <th class="head"><p>len(key)</p></th>
  597. <th class="head"><p>len(salt)</p></th>
  598. <th class="head"><p>len(person)</p></th>
  599. </tr>
  600. </thead>
  601. <tbody>
  602. <tr class="row-even"><td><p>BLAKE2b</p></td>
  603. <td><p>64</p></td>
  604. <td><p>64</p></td>
  605. <td><p>16</p></td>
  606. <td><p>16</p></td>
  607. </tr>
  608. <tr class="row-odd"><td><p>BLAKE2s</p></td>
  609. <td><p>32</p></td>
  610. <td><p>32</p></td>
  611. <td><p>8</p></td>
  612. <td><p>8</p></td>
  613. </tr>
  614. </tbody>
  615. </table>
  616. <div class="admonition note">
  617. <p class="admonition-title">Note</p>
  618. <p>BLAKE2 specification defines constant lengths for salt and personalization
  619. parameters, however, for convenience, this implementation accepts byte
  620. strings of any size up to the specified length. If the length of the
  621. parameter is less than specified, it is padded with zeros, thus, for
  622. example, <code class="docutils literal notranslate"><span class="pre">b'salt'</span></code> and <code class="docutils literal notranslate"><span class="pre">b'salt\x00'</span></code> is the same value. (This is not
  623. the case for <em>key</em>.)</p>
  624. </div>
  625. <p>These sizes are available as module <a class="reference internal" href="#constants">constants</a> described below.</p>
  626. <p>Constructor functions also accept the following tree hashing parameters:</p>
  627. <ul class="simple">
  628. <li><p><em>fanout</em>: fanout (0 to 255, 0 if unlimited, 1 in sequential mode).</p></li>
  629. <li><p><em>depth</em>: maximal depth of tree (1 to 255, 255 if unlimited, 1 in
  630. sequential mode).</p></li>
  631. <li><p><em>leaf_size</em>: maximal byte length of leaf (0 to <code class="docutils literal notranslate"><span class="pre">2**32-1</span></code>, 0 if unlimited or in
  632. sequential mode).</p></li>
  633. <li><p><em>node_offset</em>: node offset (0 to <code class="docutils literal notranslate"><span class="pre">2**64-1</span></code> for BLAKE2b, 0 to <code class="docutils literal notranslate"><span class="pre">2**48-1</span></code> for
  634. BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode).</p></li>
  635. <li><p><em>node_depth</em>: node depth (0 to 255, 0 for leaves, or in sequential mode).</p></li>
  636. <li><p><em>inner_size</em>: inner digest size (0 to 64 for BLAKE2b, 0 to 32 for
  637. BLAKE2s, 0 in sequential mode).</p></li>
  638. <li><p><em>last_node</em>: boolean indicating whether the processed node is the last
  639. one (<code class="docutils literal notranslate"><span class="pre">False</span></code> for sequential mode).</p></li>
  640. </ul>
  641. <figure class="align-default">
  642. <img alt="Explanation of tree mode parameters." class="invert-in-dark-mode" src="../_images/hashlib-blake2-tree.png" />
  643. </figure>
  644. <p>See section 2.10 in <a class="reference external" href="https://www.blake2.net/blake2_20130129.pdf">BLAKE2 specification</a> for comprehensive review of tree
  645. hashing.</p>
  646. </section>
  647. <section id="constants">
  648. <h3>Constants<a class="headerlink" href="#constants" title="Link to this heading">¶</a></h3>
  649. <dl class="py data">
  650. <dt class="sig sig-object py" id="hashlib.blake2b.SALT_SIZE">
  651. <span class="sig-prename descclassname"><span class="pre">blake2b.</span></span><span class="sig-name descname"><span class="pre">SALT_SIZE</span></span><a class="headerlink" href="#hashlib.blake2b.SALT_SIZE" title="Link to this definition">¶</a></dt>
  652. <dd></dd></dl>
  653. <dl class="py data">
  654. <dt class="sig sig-object py" id="hashlib.blake2s.SALT_SIZE">
  655. <span class="sig-prename descclassname"><span class="pre">blake2s.</span></span><span class="sig-name descname"><span class="pre">SALT_SIZE</span></span><a class="headerlink" href="#hashlib.blake2s.SALT_SIZE" title="Link to this definition">¶</a></dt>
  656. <dd></dd></dl>
  657. <p>Salt length (maximum length accepted by constructors).</p>
  658. <dl class="py data">
  659. <dt class="sig sig-object py" id="hashlib.blake2b.PERSON_SIZE">
  660. <span class="sig-prename descclassname"><span class="pre">blake2b.</span></span><span class="sig-name descname"><span class="pre">PERSON_SIZE</span></span><a class="headerlink" href="#hashlib.blake2b.PERSON_SIZE" title="Link to this definition">¶</a></dt>
  661. <dd></dd></dl>
  662. <dl class="py data">
  663. <dt class="sig sig-object py" id="hashlib.blake2s.PERSON_SIZE">
  664. <span class="sig-prename descclassname"><span class="pre">blake2s.</span></span><span class="sig-name descname"><span class="pre">PERSON_SIZE</span></span><a class="headerlink" href="#hashlib.blake2s.PERSON_SIZE" title="Link to this definition">¶</a></dt>
  665. <dd></dd></dl>
  666. <p>Personalization string length (maximum length accepted by constructors).</p>
  667. <dl class="py data">
  668. <dt class="sig sig-object py" id="hashlib.blake2b.MAX_KEY_SIZE">
  669. <span class="sig-prename descclassname"><span class="pre">blake2b.</span></span><span class="sig-name descname"><span class="pre">MAX_KEY_SIZE</span></span><a class="headerlink" href="#hashlib.blake2b.MAX_KEY_SIZE" title="Link to this definition">¶</a></dt>
  670. <dd></dd></dl>
  671. <dl class="py data">
  672. <dt class="sig sig-object py" id="hashlib.blake2s.MAX_KEY_SIZE">
  673. <span class="sig-prename descclassname"><span class="pre">blake2s.</span></span><span class="sig-name descname"><span class="pre">MAX_KEY_SIZE</span></span><a class="headerlink" href="#hashlib.blake2s.MAX_KEY_SIZE" title="Link to this definition">¶</a></dt>
  674. <dd></dd></dl>
  675. <p>Maximum key size.</p>
  676. <dl class="py data">
  677. <dt class="sig sig-object py" id="hashlib.blake2b.MAX_DIGEST_SIZE">
  678. <span class="sig-prename descclassname"><span class="pre">blake2b.</span></span><span class="sig-name descname"><span class="pre">MAX_DIGEST_SIZE</span></span><a class="headerlink" href="#hashlib.blake2b.MAX_DIGEST_SIZE" title="Link to this definition">¶</a></dt>
  679. <dd></dd></dl>
  680. <dl class="py data">
  681. <dt class="sig sig-object py" id="hashlib.blake2s.MAX_DIGEST_SIZE">
  682. <span class="sig-prename descclassname"><span class="pre">blake2s.</span></span><span class="sig-name descname"><span class="pre">MAX_DIGEST_SIZE</span></span><a class="headerlink" href="#hashlib.blake2s.MAX_DIGEST_SIZE" title="Link to this definition">¶</a></dt>
  683. <dd></dd></dl>
  684. <p>Maximum digest size that the hash function can output.</p>
  685. </section>
  686. <section id="examples">
  687. <h3>Examples<a class="headerlink" href="#examples" title="Link to this heading">¶</a></h3>
  688. <section id="simple-hashing">
  689. <h4>Simple hashing<a class="headerlink" href="#simple-hashing" title="Link to this heading">¶</a></h4>
  690. <p>To calculate hash of some data, you should first construct a hash object by
  691. calling the appropriate constructor function (<a class="reference internal" href="#hashlib.blake2b" title="hashlib.blake2b"><code class="xref py py-func docutils literal notranslate"><span class="pre">blake2b()</span></code></a> or
  692. <a class="reference internal" href="#hashlib.blake2s" title="hashlib.blake2s"><code class="xref py py-func docutils literal notranslate"><span class="pre">blake2s()</span></code></a>), then update it with the data by calling <a class="reference internal" href="#hashlib.hash.update" title="hashlib.hash.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code></a> on the
  693. object, and, finally, get the digest out of the object by calling
  694. <a class="reference internal" href="#hashlib.hash.digest" title="hashlib.hash.digest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">digest()</span></code></a> (or <a class="reference internal" href="#hashlib.hash.hexdigest" title="hashlib.hash.hexdigest"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hexdigest()</span></code></a> for hex-encoded string).</p>
  695. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  696. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">()</span>
  697. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Hello world&#39;</span><span class="p">)</span>
  698. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  699. <span class="go">&#39;6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183&#39;</span>
  700. </pre></div>
  701. </div>
  702. <p>As a shortcut, you can pass the first chunk of data to update directly to the
  703. constructor as the positional argument:</p>
  704. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  705. <span class="gp">&gt;&gt;&gt; </span><span class="n">blake2b</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Hello world&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  706. <span class="go">&#39;6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183&#39;</span>
  707. </pre></div>
  708. </div>
  709. <p>You can call <a class="reference internal" href="#hashlib.hash.update" title="hashlib.hash.update"><code class="xref py py-meth docutils literal notranslate"><span class="pre">hash.update()</span></code></a> as many times as you need to iteratively
  710. update the hash:</p>
  711. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  712. <span class="gp">&gt;&gt;&gt; </span><span class="n">items</span> <span class="o">=</span> <span class="p">[</span><span class="sa">b</span><span class="s1">&#39;Hello&#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;world&#39;</span><span class="p">]</span>
  713. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">()</span>
  714. <span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">items</span><span class="p">:</span>
  715. <span class="gp">... </span> <span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
  716. <span class="gp">...</span>
  717. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  718. <span class="go">&#39;6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183&#39;</span>
  719. </pre></div>
  720. </div>
  721. </section>
  722. <section id="using-different-digest-sizes">
  723. <h4>Using different digest sizes<a class="headerlink" href="#using-different-digest-sizes" title="Link to this heading">¶</a></h4>
  724. <p>BLAKE2 has configurable size of digests up to 64 bytes for BLAKE2b and up to 32
  725. bytes for BLAKE2s. For example, to replace SHA-1 with BLAKE2b without changing
  726. the size of output, we can tell BLAKE2b to produce 20-byte digests:</p>
  727. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  728. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span>
  729. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Replacing SHA1 with the more secure function&#39;</span><span class="p">)</span>
  730. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  731. <span class="go">&#39;d24f26cf8de66472d58d4e1b1774b4c9158b1f4c&#39;</span>
  732. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">digest_size</span>
  733. <span class="go">20</span>
  734. <span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">h</span><span class="o">.</span><span class="n">digest</span><span class="p">())</span>
  735. <span class="go">20</span>
  736. </pre></div>
  737. </div>
  738. <p>Hash objects with different digest sizes have completely different outputs
  739. (shorter hashes are <em>not</em> prefixes of longer hashes); BLAKE2b and BLAKE2s
  740. produce different outputs even if the output length is the same:</p>
  741. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span><span class="p">,</span> <span class="n">blake2s</span>
  742. <span class="gp">&gt;&gt;&gt; </span><span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  743. <span class="go">&#39;6fa1d8fcfd719046d762&#39;</span>
  744. <span class="gp">&gt;&gt;&gt; </span><span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  745. <span class="go">&#39;eb6ec15daf9546254f0809&#39;</span>
  746. <span class="gp">&gt;&gt;&gt; </span><span class="n">blake2s</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  747. <span class="go">&#39;1bf21a98c78a1c376ae9&#39;</span>
  748. <span class="gp">&gt;&gt;&gt; </span><span class="n">blake2s</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">11</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  749. <span class="go">&#39;567004bf96e4a25773ebf4&#39;</span>
  750. </pre></div>
  751. </div>
  752. </section>
  753. <section id="keyed-hashing">
  754. <h4>Keyed hashing<a class="headerlink" href="#keyed-hashing" title="Link to this heading">¶</a></h4>
  755. <p>Keyed hashing can be used for authentication as a faster and simpler
  756. replacement for <a class="reference external" href="https://en.wikipedia.org/wiki/HMAC">Hash-based message authentication code</a> (HMAC).
  757. BLAKE2 can be securely used in prefix-MAC mode thanks to the
  758. indifferentiability property inherited from BLAKE.</p>
  759. <p>This example shows how to get a (hex-encoded) 128-bit authentication code for
  760. message <code class="docutils literal notranslate"><span class="pre">b'message</span> <span class="pre">data'</span></code> with key <code class="docutils literal notranslate"><span class="pre">b'pseudorandom</span> <span class="pre">key'</span></code>:</p>
  761. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  762. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;pseudorandom key&#39;</span><span class="p">,</span> <span class="n">digest_size</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span>
  763. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;message data&#39;</span><span class="p">)</span>
  764. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  765. <span class="go">&#39;3d363ff7401e02026f4a4687d4863ced&#39;</span>
  766. </pre></div>
  767. </div>
  768. <p>As a practical example, a web application can symmetrically sign cookies sent
  769. to users and later verify them to make sure they weren’t tampered with:</p>
  770. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  771. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hmac</span> <span class="kn">import</span> <span class="n">compare_digest</span>
  772. <span class="gp">&gt;&gt;&gt;</span>
  773. <span class="gp">&gt;&gt;&gt; </span><span class="n">SECRET_KEY</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;pseudorandomly generated server secret key&#39;</span>
  774. <span class="gp">&gt;&gt;&gt; </span><span class="n">AUTH_SIZE</span> <span class="o">=</span> <span class="mi">16</span>
  775. <span class="gp">&gt;&gt;&gt;</span>
  776. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="n">cookie</span><span class="p">):</span>
  777. <span class="gp">... </span> <span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="n">AUTH_SIZE</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">SECRET_KEY</span><span class="p">)</span>
  778. <span class="gp">... </span> <span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">cookie</span><span class="p">)</span>
  779. <span class="gp">... </span> <span class="k">return</span> <span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
  780. <span class="gp">&gt;&gt;&gt;</span>
  781. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">verify</span><span class="p">(</span><span class="n">cookie</span><span class="p">,</span> <span class="n">sig</span><span class="p">):</span>
  782. <span class="gp">... </span> <span class="n">good_sig</span> <span class="o">=</span> <span class="n">sign</span><span class="p">(</span><span class="n">cookie</span><span class="p">)</span>
  783. <span class="gp">... </span> <span class="k">return</span> <span class="n">compare_digest</span><span class="p">(</span><span class="n">good_sig</span><span class="p">,</span> <span class="n">sig</span><span class="p">)</span>
  784. <span class="gp">&gt;&gt;&gt;</span>
  785. <span class="gp">&gt;&gt;&gt; </span><span class="n">cookie</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;user-alice&#39;</span>
  786. <span class="gp">&gt;&gt;&gt; </span><span class="n">sig</span> <span class="o">=</span> <span class="n">sign</span><span class="p">(</span><span class="n">cookie</span><span class="p">)</span>
  787. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{0}</span><span class="s2">,</span><span class="si">{1}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">cookie</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">),</span> <span class="n">sig</span><span class="p">))</span>
  788. <span class="go">user-alice,b&#39;43b3c982cf697e0c5ab22172d1ca7421&#39;</span>
  789. <span class="gp">&gt;&gt;&gt; </span><span class="n">verify</span><span class="p">(</span><span class="n">cookie</span><span class="p">,</span> <span class="n">sig</span><span class="p">)</span>
  790. <span class="go">True</span>
  791. <span class="gp">&gt;&gt;&gt; </span><span class="n">verify</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;user-bob&#39;</span><span class="p">,</span> <span class="n">sig</span><span class="p">)</span>
  792. <span class="go">False</span>
  793. <span class="gp">&gt;&gt;&gt; </span><span class="n">verify</span><span class="p">(</span><span class="n">cookie</span><span class="p">,</span> <span class="sa">b</span><span class="s1">&#39;0102030405060708090a0b0c0d0e0f00&#39;</span><span class="p">)</span>
  794. <span class="go">False</span>
  795. </pre></div>
  796. </div>
  797. <p>Even though there’s a native keyed hashing mode, BLAKE2 can, of course, be used
  798. in HMAC construction with <a class="reference internal" href="hmac.html#module-hmac" title="hmac: Keyed-Hashing for Message Authentication (HMAC) implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code></a> module:</p>
  799. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">hmac</span><span class="o">,</span> <span class="nn">hashlib</span>
  800. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">hmac</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;secret key&#39;</span><span class="p">,</span> <span class="n">digestmod</span><span class="o">=</span><span class="n">hashlib</span><span class="o">.</span><span class="n">blake2s</span><span class="p">)</span>
  801. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;message&#39;</span><span class="p">)</span>
  802. <span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  803. <span class="go">&#39;e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142&#39;</span>
  804. </pre></div>
  805. </div>
  806. </section>
  807. <section id="randomized-hashing">
  808. <h4>Randomized hashing<a class="headerlink" href="#randomized-hashing" title="Link to this heading">¶</a></h4>
  809. <p>By setting <em>salt</em> parameter users can introduce randomization to the hash
  810. function. Randomized hashing is useful for protecting against collision attacks
  811. on the hash function used in digital signatures.</p>
  812. <blockquote>
  813. <div><p>Randomized hashing is designed for situations where one party, the message
  814. preparer, generates all or part of a message to be signed by a second
  815. party, the message signer. If the message preparer is able to find
  816. cryptographic hash function collisions (i.e., two messages producing the
  817. same hash value), then they might prepare meaningful versions of the message
  818. that would produce the same hash value and digital signature, but with
  819. different results (e.g., transferring $1,000,000 to an account, rather than
  820. $10). Cryptographic hash functions have been designed with collision
  821. resistance as a major goal, but the current concentration on attacking
  822. cryptographic hash functions may result in a given cryptographic hash
  823. function providing less collision resistance than expected. Randomized
  824. hashing offers the signer additional protection by reducing the likelihood
  825. that a preparer can generate two or more messages that ultimately yield the
  826. same hash value during the digital signature generation process — even if
  827. it is practical to find collisions for the hash function. However, the use
  828. of randomized hashing may reduce the amount of security provided by a
  829. digital signature when all portions of the message are prepared
  830. by the signer.</p>
  831. <p>(<a class="reference external" href="https://csrc.nist.gov/publications/detail/sp/800-106/archive/2009-02-25">NIST SP-800-106 “Randomized Hashing for Digital Signatures”</a>)</p>
  832. </div></blockquote>
  833. <p>In BLAKE2 the salt is processed as a one-time input to the hash function during
  834. initialization, rather than as an input to each compression function.</p>
  835. <div class="admonition warning">
  836. <p class="admonition-title">Warning</p>
  837. <p><em>Salted hashing</em> (or just hashing) with BLAKE2 or any other general-purpose
  838. cryptographic hash function, such as SHA-256, is not suitable for hashing
  839. passwords. See <a class="reference external" href="https://www.blake2.net/#qa">BLAKE2 FAQ</a> for more
  840. information.</p>
  841. </div>
  842. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">os</span>
  843. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  844. <span class="gp">&gt;&gt;&gt; </span><span class="n">msg</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;some message&#39;</span>
  845. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Calculate the first hash with a random salt.</span>
  846. <span class="gp">&gt;&gt;&gt; </span><span class="n">salt1</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">urandom</span><span class="p">(</span><span class="n">blake2b</span><span class="o">.</span><span class="n">SALT_SIZE</span><span class="p">)</span>
  847. <span class="gp">&gt;&gt;&gt; </span><span class="n">h1</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">salt</span><span class="o">=</span><span class="n">salt1</span><span class="p">)</span>
  848. <span class="gp">&gt;&gt;&gt; </span><span class="n">h1</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  849. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Calculate the second hash with a different random salt.</span>
  850. <span class="gp">&gt;&gt;&gt; </span><span class="n">salt2</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">urandom</span><span class="p">(</span><span class="n">blake2b</span><span class="o">.</span><span class="n">SALT_SIZE</span><span class="p">)</span>
  851. <span class="gp">&gt;&gt;&gt; </span><span class="n">h2</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">salt</span><span class="o">=</span><span class="n">salt2</span><span class="p">)</span>
  852. <span class="gp">&gt;&gt;&gt; </span><span class="n">h2</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
  853. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># The digests are different.</span>
  854. <span class="gp">&gt;&gt;&gt; </span><span class="n">h1</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span> <span class="o">!=</span> <span class="n">h2</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
  855. <span class="go">True</span>
  856. </pre></div>
  857. </div>
  858. </section>
  859. <section id="personalization">
  860. <h4>Personalization<a class="headerlink" href="#personalization" title="Link to this heading">¶</a></h4>
  861. <p>Sometimes it is useful to force hash function to produce different digests for
  862. the same input for different purposes. Quoting the authors of the Skein hash
  863. function:</p>
  864. <blockquote>
  865. <div><p>We recommend that all application designers seriously consider doing this;
  866. we have seen many protocols where a hash that is computed in one part of
  867. the protocol can be used in an entirely different part because two hash
  868. computations were done on similar or related data, and the attacker can
  869. force the application to make the hash inputs the same. Personalizing each
  870. hash function used in the protocol summarily stops this type of attack.</p>
  871. <p>(<a class="reference external" href="https://www.schneier.com/wp-content/uploads/2016/02/skein.pdf">The Skein Hash Function Family</a>,
  872. p. 21)</p>
  873. </div></blockquote>
  874. <p>BLAKE2 can be personalized by passing bytes to the <em>person</em> argument:</p>
  875. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  876. <span class="gp">&gt;&gt;&gt; </span><span class="n">FILES_HASH_PERSON</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;MyApp Files Hash&#39;</span>
  877. <span class="gp">&gt;&gt;&gt; </span><span class="n">BLOCK_HASH_PERSON</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">&#39;MyApp Block Hash&#39;</span>
  878. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">person</span><span class="o">=</span><span class="n">FILES_HASH_PERSON</span><span class="p">)</span>
  879. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;the same content&#39;</span><span class="p">)</span>
  880. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  881. <span class="go">&#39;20d9cd024d4fb086aae819a1432dd2466de12947831b75c5a30cf2676095d3b4&#39;</span>
  882. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">person</span><span class="o">=</span><span class="n">BLOCK_HASH_PERSON</span><span class="p">)</span>
  883. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;the same content&#39;</span><span class="p">)</span>
  884. <span class="gp">&gt;&gt;&gt; </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  885. <span class="go">&#39;cf68fb5761b9c44e7878bfb2c4c9aea52264a80b75005e65619778de59f383a3&#39;</span>
  886. </pre></div>
  887. </div>
  888. <p>Personalization together with the keyed mode can also be used to derive different
  889. keys from a single one.</p>
  890. <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2s</span>
  891. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">base64</span> <span class="kn">import</span> <span class="n">b64decode</span><span class="p">,</span> <span class="n">b64encode</span>
  892. <span class="gp">&gt;&gt;&gt; </span><span class="n">orig_key</span> <span class="o">=</span> <span class="n">b64decode</span><span class="p">(</span><span class="sa">b</span><span class="s1">&#39;Rm5EPJai72qcK3RGBpW3vPNfZy5OZothY+kHY6h21KM=&#39;</span><span class="p">)</span>
  893. <span class="gp">&gt;&gt;&gt; </span><span class="n">enc_key</span> <span class="o">=</span> <span class="n">blake2s</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">orig_key</span><span class="p">,</span> <span class="n">person</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;kEncrypt&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
  894. <span class="gp">&gt;&gt;&gt; </span><span class="n">mac_key</span> <span class="o">=</span> <span class="n">blake2s</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">orig_key</span><span class="p">,</span> <span class="n">person</span><span class="o">=</span><span class="sa">b</span><span class="s1">&#39;kMAC&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
  895. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">b64encode</span><span class="p">(</span><span class="n">enc_key</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  896. <span class="go">rbPb15S/Z9t+agffno5wuhB77VbRi6F9Iv2qIxU7WHw=</span>
  897. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">b64encode</span><span class="p">(</span><span class="n">mac_key</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">))</span>
  898. <span class="go">G9GtHFE1YluXY1zWPlYk1e/nWfu0WSEb0KRcjhDeP/o=</span>
  899. </pre></div>
  900. </div>
  901. </section>
  902. <section id="tree-mode">
  903. <h4>Tree mode<a class="headerlink" href="#tree-mode" title="Link to this heading">¶</a></h4>
  904. <p>Here’s an example of hashing a minimal tree with two leaf nodes:</p>
  905. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="mi">10</span>
  906. <span class="o">/</span> \
  907. <span class="mi">00</span> <span class="mi">01</span>
  908. </pre></div>
  909. </div>
  910. <p>This example uses 64-byte internal digests, and returns the 32-byte final
  911. digest:</p>
  912. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="kn">import</span> <span class="n">blake2b</span>
  913. <span class="gp">&gt;&gt;&gt;</span>
  914. <span class="gp">&gt;&gt;&gt; </span><span class="n">FANOUT</span> <span class="o">=</span> <span class="mi">2</span>
  915. <span class="gp">&gt;&gt;&gt; </span><span class="n">DEPTH</span> <span class="o">=</span> <span class="mi">2</span>
  916. <span class="gp">&gt;&gt;&gt; </span><span class="n">LEAF_SIZE</span> <span class="o">=</span> <span class="mi">4096</span>
  917. <span class="gp">&gt;&gt;&gt; </span><span class="n">INNER_SIZE</span> <span class="o">=</span> <span class="mi">64</span>
  918. <span class="gp">&gt;&gt;&gt;</span>
  919. <span class="gp">&gt;&gt;&gt; </span><span class="n">buf</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="mi">6000</span><span class="p">)</span>
  920. <span class="gp">&gt;&gt;&gt;</span>
  921. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Left leaf</span>
  922. <span class="gp">... </span><span class="n">h00</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">buf</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">LEAF_SIZE</span><span class="p">],</span> <span class="n">fanout</span><span class="o">=</span><span class="n">FANOUT</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="n">DEPTH</span><span class="p">,</span>
  923. <span class="gp">... </span> <span class="n">leaf_size</span><span class="o">=</span><span class="n">LEAF_SIZE</span><span class="p">,</span> <span class="n">inner_size</span><span class="o">=</span><span class="n">INNER_SIZE</span><span class="p">,</span>
  924. <span class="gp">... </span> <span class="n">node_offset</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">node_depth</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">last_node</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
  925. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Right leaf</span>
  926. <span class="gp">... </span><span class="n">h01</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">buf</span><span class="p">[</span><span class="n">LEAF_SIZE</span><span class="p">:],</span> <span class="n">fanout</span><span class="o">=</span><span class="n">FANOUT</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="n">DEPTH</span><span class="p">,</span>
  927. <span class="gp">... </span> <span class="n">leaf_size</span><span class="o">=</span><span class="n">LEAF_SIZE</span><span class="p">,</span> <span class="n">inner_size</span><span class="o">=</span><span class="n">INNER_SIZE</span><span class="p">,</span>
  928. <span class="gp">... </span> <span class="n">node_offset</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">node_depth</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">last_node</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  929. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Root node</span>
  930. <span class="gp">... </span><span class="n">h10</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">(</span><span class="n">digest_size</span><span class="o">=</span><span class="mi">32</span><span class="p">,</span> <span class="n">fanout</span><span class="o">=</span><span class="n">FANOUT</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="n">DEPTH</span><span class="p">,</span>
  931. <span class="gp">... </span> <span class="n">leaf_size</span><span class="o">=</span><span class="n">LEAF_SIZE</span><span class="p">,</span> <span class="n">inner_size</span><span class="o">=</span><span class="n">INNER_SIZE</span><span class="p">,</span>
  932. <span class="gp">... </span> <span class="n">node_offset</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">node_depth</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">last_node</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  933. <span class="gp">&gt;&gt;&gt; </span><span class="n">h10</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">h00</span><span class="o">.</span><span class="n">digest</span><span class="p">())</span>
  934. <span class="gp">&gt;&gt;&gt; </span><span class="n">h10</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">h01</span><span class="o">.</span><span class="n">digest</span><span class="p">())</span>
  935. <span class="gp">&gt;&gt;&gt; </span><span class="n">h10</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
  936. <span class="go">&#39;3ad2a9b37c6070e374c7a8c508fe20ca86b6ed54e286e93a0318e95e881db5aa&#39;</span>
  937. </pre></div>
  938. </div>
  939. </section>
  940. </section>
  941. <section id="credits">
  942. <h3>Credits<a class="headerlink" href="#credits" title="Link to this heading">¶</a></h3>
  943. <p><a class="reference external" href="https://www.blake2.net">BLAKE2</a> was designed by <em>Jean-Philippe Aumasson</em>, <em>Samuel Neves</em>, <em>Zooko
  944. Wilcox-O’Hearn</em>, and <em>Christian Winnerlein</em> based on <a class="reference external" href="https://en.wikipedia.org/wiki/Secure_Hash_Algorithms">SHA-3</a> finalist <a class="reference external" href="https://web.archive.org/web/20200918190133/https://131002.net/blake/">BLAKE</a>
  945. created by <em>Jean-Philippe Aumasson</em>, <em>Luca Henzen</em>, <em>Willi Meier</em>, and
  946. <em>Raphael C.-W. Phan</em>.</p>
  947. <p>It uses core algorithm from <a class="reference external" href="https://cr.yp.to/chacha.html">ChaCha</a> cipher designed by <em>Daniel J. Bernstein</em>.</p>
  948. <p>The stdlib implementation is based on <a class="reference external" href="https://pythonhosted.org/pyblake2/">pyblake2</a> module. It was written by
  949. <em>Dmitry Chestnykh</em> based on C implementation written by <em>Samuel Neves</em>. The
  950. documentation was copied from <a class="reference external" href="https://pythonhosted.org/pyblake2/">pyblake2</a> and written by <em>Dmitry Chestnykh</em>.</p>
  951. <p>The C code was partly rewritten for Python by <em>Christian Heimes</em>.</p>
  952. <p>The following public domain dedication applies for both C hash function
  953. implementation, extension code, and this documentation:</p>
  954. <blockquote>
  955. <div><p>To the extent possible under law, the author(s) have dedicated all copyright
  956. and related and neighboring rights to this software to the public domain
  957. worldwide. This software is distributed without any warranty.</p>
  958. <p>You should have received a copy of the CC0 Public Domain Dedication along
  959. with this software. If not, see
  960. <a class="reference external" href="https://creativecommons.org/publicdomain/zero/1.0/">https://creativecommons.org/publicdomain/zero/1.0/</a>.</p>
  961. </div></blockquote>
  962. <p>The following people have helped with development or contributed their changes
  963. to the project and the public domain according to the Creative Commons Public
  964. Domain Dedication 1.0 Universal:</p>
  965. <ul class="simple">
  966. <li><p><em>Alexandr Sokolovskiy</em></p></li>
  967. </ul>
  968. <div class="admonition seealso" id="hashlib-seealso">
  969. <p class="admonition-title">See also</p>
  970. <dl class="simple">
  971. <dt>Module <a class="reference internal" href="hmac.html#module-hmac" title="hmac: Keyed-Hashing for Message Authentication (HMAC) implementation"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code></a></dt><dd><p>A module to generate message authentication codes using hashes.</p>
  972. </dd>
  973. <dt>Module <a class="reference internal" href="base64.html#module-base64" title="base64: RFC 4648: Base16, Base32, Base64 Data Encodings; Base85 and Ascii85"><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code></a></dt><dd><p>Another way to encode binary hashes for non-binary environments.</p>
  974. </dd>
  975. <dt><a class="reference external" href="https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.180-4.pdf">https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.180-4.pdf</a></dt><dd><p>The FIPS 180-4 publication on Secure Hash Algorithms.</p>
  976. </dd>
  977. <dt><a class="reference external" href="https://csrc.nist.gov/publications/detail/fips/202/final">https://csrc.nist.gov/publications/detail/fips/202/final</a></dt><dd><p>The FIPS 202 publication on the SHA-3 Standard.</p>
  978. </dd>
  979. <dt><a class="reference external" href="https://www.blake2.net/">https://www.blake2.net/</a></dt><dd><p>Official BLAKE2 website.</p>
  980. </dd>
  981. <dt><a class="reference external" href="https://en.wikipedia.org/wiki/Cryptographic_hash_function">https://en.wikipedia.org/wiki/Cryptographic_hash_function</a></dt><dd><p>Wikipedia article with information on which algorithms have known issues
  982. and what that means regarding their use.</p>
  983. </dd>
  984. <dt><a class="reference external" href="https://www.ietf.org/rfc/rfc8018.txt">https://www.ietf.org/rfc/rfc8018.txt</a></dt><dd><p>PKCS #5: Password-Based Cryptography Specification Version 2.1</p>
  985. </dd>
  986. <dt><a class="reference external" href="https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf">https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-132.pdf</a></dt><dd><p>NIST Recommendation for Password-Based Key Derivation.</p>
  987. </dd>
  988. </dl>
  989. </div>
  990. </section>
  991. </section>
  992. </section>
  993. <div class="clearer"></div>
  994. </div>
  995. </div>
  996. </div>
  997. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  998. <div class="sphinxsidebarwrapper">
  999. <div>
  1000. <h3><a href="../contents.html">Table of Contents</a></h3>
  1001. <ul>
  1002. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code> — Secure hashes and message digests</a><ul>
  1003. <li><a class="reference internal" href="#hash-algorithms">Hash algorithms</a></li>
  1004. <li><a class="reference internal" href="#usage">Usage</a></li>
  1005. <li><a class="reference internal" href="#constructors">Constructors</a></li>
  1006. <li><a class="reference internal" href="#attributes">Attributes</a></li>
  1007. <li><a class="reference internal" href="#hash-objects">Hash Objects</a></li>
  1008. <li><a class="reference internal" href="#shake-variable-length-digests">SHAKE variable length digests</a></li>
  1009. <li><a class="reference internal" href="#file-hashing">File hashing</a></li>
  1010. <li><a class="reference internal" href="#key-derivation">Key derivation</a></li>
  1011. <li><a class="reference internal" href="#blake2">BLAKE2</a><ul>
  1012. <li><a class="reference internal" href="#creating-hash-objects">Creating hash objects</a></li>
  1013. <li><a class="reference internal" href="#constants">Constants</a></li>
  1014. <li><a class="reference internal" href="#examples">Examples</a><ul>
  1015. <li><a class="reference internal" href="#simple-hashing">Simple hashing</a></li>
  1016. <li><a class="reference internal" href="#using-different-digest-sizes">Using different digest sizes</a></li>
  1017. <li><a class="reference internal" href="#keyed-hashing">Keyed hashing</a></li>
  1018. <li><a class="reference internal" href="#randomized-hashing">Randomized hashing</a></li>
  1019. <li><a class="reference internal" href="#personalization">Personalization</a></li>
  1020. <li><a class="reference internal" href="#tree-mode">Tree mode</a></li>
  1021. </ul>
  1022. </li>
  1023. <li><a class="reference internal" href="#credits">Credits</a></li>
  1024. </ul>
  1025. </li>
  1026. </ul>
  1027. </li>
  1028. </ul>
  1029. </div>
  1030. <div>
  1031. <h4>Previous topic</h4>
  1032. <p class="topless"><a href="crypto.html"
  1033. title="previous chapter">Cryptographic Services</a></p>
  1034. </div>
  1035. <div>
  1036. <h4>Next topic</h4>
  1037. <p class="topless"><a href="hmac.html"
  1038. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code> — Keyed-Hashing for Message Authentication</a></p>
  1039. </div>
  1040. <div role="note" aria-label="source link">
  1041. <h3>This Page</h3>
  1042. <ul class="this-page-menu">
  1043. <li><a href="../bugs.html">Report a Bug</a></li>
  1044. <li>
  1045. <a href="https://github.com/python/cpython/blob/main/Doc/library/hashlib.rst"
  1046. rel="nofollow">Show Source
  1047. </a>
  1048. </li>
  1049. </ul>
  1050. </div>
  1051. </div>
  1052. <div id="sidebarbutton" title="Collapse sidebar">
  1053. <span>«</span>
  1054. </div>
  1055. </div>
  1056. <div class="clearer"></div>
  1057. </div>
  1058. <div class="related" role="navigation" aria-label="related navigation">
  1059. <h3>Navigation</h3>
  1060. <ul>
  1061. <li class="right" style="margin-right: 10px">
  1062. <a href="../genindex.html" title="General Index"
  1063. >index</a></li>
  1064. <li class="right" >
  1065. <a href="../py-modindex.html" title="Python Module Index"
  1066. >modules</a> |</li>
  1067. <li class="right" >
  1068. <a href="hmac.html" title="hmac — Keyed-Hashing for Message Authentication"
  1069. >next</a> |</li>
  1070. <li class="right" >
  1071. <a href="crypto.html" title="Cryptographic Services"
  1072. >previous</a> |</li>
  1073. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1074. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1075. <li class="switchers">
  1076. <div class="language_switcher_placeholder"></div>
  1077. <div class="version_switcher_placeholder"></div>
  1078. </li>
  1079. <li>
  1080. </li>
  1081. <li id="cpython-language-and-version">
  1082. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1083. </li>
  1084. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1085. <li class="nav-item nav-item-2"><a href="crypto.html" >Cryptographic Services</a> &#187;</li>
  1086. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">hashlib</span></code> — Secure hashes and message digests</a></li>
  1087. <li class="right">
  1088. <div class="inline-search" role="search">
  1089. <form class="inline-search" action="../search.html" method="get">
  1090. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1091. <input type="submit" value="Go" />
  1092. </form>
  1093. </div>
  1094. |
  1095. </li>
  1096. <li class="right">
  1097. <label class="theme-selector-label">
  1098. Theme
  1099. <select class="theme-selector" oninput="activateTheme(this.value)">
  1100. <option value="auto" selected>Auto</option>
  1101. <option value="light">Light</option>
  1102. <option value="dark">Dark</option>
  1103. </select>
  1104. </label> |</li>
  1105. </ul>
  1106. </div>
  1107. <div class="footer">
  1108. &copy;
  1109. <a href="../copyright.html">
  1110. Copyright
  1111. </a>
  1112. 2001-2024, Python Software Foundation.
  1113. <br />
  1114. This page is licensed under the Python Software Foundation License Version 2.
  1115. <br />
  1116. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1117. <br />
  1118. See <a href="/license.html">History and License</a> for more information.<br />
  1119. <br />
  1120. The Python Software Foundation is a non-profit corporation.
  1121. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1122. <br />
  1123. <br />
  1124. Last updated on Apr 09, 2024 (13:47 UTC).
  1125. <a href="/bugs.html">Found a bug</a>?
  1126. <br />
  1127. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1128. </div>
  1129. </body>
  1130. </html>
上海开阖软件有限公司 沪ICP备12045867号-1