919 lines
78 KiB
HTML
919 lines
78 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml">
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<title>hashlib — Secure hashes and message digests — Python 3.7.4 documentation</title>
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script type="text/javascript" src="../_static/jquery.js"></script>
|
||
<script type="text/javascript" src="../_static/underscore.js"></script>
|
||
<script type="text/javascript" src="../_static/doctools.js"></script>
|
||
<script type="text/javascript" src="../_static/language_data.js"></script>
|
||
|
||
<script type="text/javascript" src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="Search within Python 3.7.4 documentation"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="About these documents" href="../about.html" />
|
||
<link rel="index" title="Index" href="../genindex.html" />
|
||
<link rel="search" title="Search" href="../search.html" />
|
||
<link rel="copyright" title="Copyright" href="../copyright.html" />
|
||
<link rel="next" title="hmac — Keyed-Hashing for Message Authentication" href="hmac.html" />
|
||
<link rel="prev" title="Cryptographic Services" href="crypto.html" />
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/hashlib.html" />
|
||
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/switchers.js"></script>
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
|
||
|
||
</head><body>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
accesskey="I">index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="hmac.html" title="hmac — Keyed-Hashing for Message Authentication"
|
||
accesskey="N">next</a> |</li>
|
||
<li class="right" >
|
||
<a href="crypto.html" title="Cryptographic Services"
|
||
accesskey="P">previous</a> |</li>
|
||
<li><img src="../_static/py.png" alt=""
|
||
style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">Documentation </a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="crypto.html" accesskey="U">Cryptographic Services</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" type="text" name="q" />
|
||
<input type="submit" value="Go" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<div class="section" id="module-hashlib">
|
||
<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="Permalink to this headline">¶</a></h1>
|
||
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/hashlib.py">Lib/hashlib.py</a></p>
|
||
<span class="target" id="index-0"></span><hr class="docutils" />
|
||
<p>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, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA’s MD5
|
||
algorithm (defined in Internet <span class="target" id="index-1"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc1321.html"><strong>RFC 1321</strong></a>). The terms “secure hash” and
|
||
“message digest” are interchangeable. Older algorithms were called message
|
||
digests. The modern term is secure hash.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>If you want the adler32 or crc32 hash functions, they are available in
|
||
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>
|
||
</div>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">Warning</p>
|
||
<p>Some algorithms have known hash collision weaknesses, refer to the “See
|
||
also” section at the end.</p>
|
||
</div>
|
||
<div class="section" id="hash-algorithms">
|
||
<span id="id1"></span><h2>Hash algorithms<a class="headerlink" href="#hash-algorithms" title="Permalink to this headline">¶</a></h2>
|
||
<p>There is one constructor method named for each type of <em class="dfn">hash</em>. All return
|
||
a hash object with the same simple interface. For example: use <code class="xref py py-func docutils literal notranslate"><span class="pre">sha256()</span></code> to
|
||
create a SHA-256 hash object. You can now feed this object with <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 the <code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code> method.
|
||
At any point you can ask it for the <em class="dfn">digest</em> of the
|
||
concatenation of the data fed to it so far using the <code class="xref py py-meth docutils literal notranslate"><span class="pre">digest()</span></code> or
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">hexdigest()</span></code> methods.</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>For better multithreading performance, the Python <a class="reference internal" href="../glossary.html#term-gil"><span class="xref std std-term">GIL</span></a> is released for
|
||
data larger than 2047 bytes at object creation or on update.</p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>Feeding string objects into <code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code> is not supported, as hashes work
|
||
on bytes, not on characters.</p>
|
||
</div>
|
||
<p id="index-2">Constructors for hash algorithms that are always present in this module are
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sha1()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha224()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha256()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha384()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sha512()</span></code>, <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>.
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">md5()</span></code> is normally available as well, though it
|
||
may be missing if you are using a rare “FIPS compliant” build of Python.
|
||
Additional algorithms may also be available depending upon the OpenSSL
|
||
library that Python uses on your platform. On most platforms the
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_224()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_256()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_384()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_512()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">shake_128()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">shake_256()</span></code> are also available.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span>SHA3 (Keccak) and SHAKE constructors <code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_224()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_256()</span></code>,
|
||
<code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_384()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">sha3_512()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">shake_128()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">shake_256()</span></code>.</p>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6: </span><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>
|
||
</div>
|
||
<p>For example, to obtain the digest of the byte string <code class="docutils literal notranslate"><span class="pre">b'Nobody</span> <span class="pre">inspects</span> <span class="pre">the</span>
|
||
<span class="pre">spammish</span> <span class="pre">repetition'</span></code>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">hashlib</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </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">"Nobody inspects"</span><span class="p">)</span>
|
||
<span class="gp">>>> </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">" the spammish repetition"</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
|
||
<span class="go">b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'</span>
|
||
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">digest_size</span>
|
||
<span class="go">32</span>
|
||
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">block_size</span>
|
||
<span class="go">64</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>More condensed:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">hashlib</span><span class="o">.</span><span class="n">sha224</span><span class="p">(</span><span class="sa">b</span><span class="s2">"Nobody inspects the spammish repetition"</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'</span>
|
||
</pre></div>
|
||
</div>
|
||
<dl class="function">
|
||
<dt id="hashlib.new">
|
||
<code class="descclassname">hashlib.</code><code class="descname">new</code><span class="sig-paren">(</span><em>name</em><span class="optional">[</span>, <em>data</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.new" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Is a generic constructor that takes the string <em>name</em> of the desired
|
||
algorithm as its first parameter. It also exists to allow access to the
|
||
above listed hashes as well as any other algorithms that your OpenSSL
|
||
library may offer. The named constructors are much faster than <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>
|
||
and should be preferred.</p>
|
||
</dd></dl>
|
||
|
||
<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 provided by OpenSSL:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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">'ripemd160'</span><span class="p">)</span>
|
||
<span class="gp">>>> </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">"Nobody inspects the spammish repetition"</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'cc4a5ce1b3df48aec5d22d1f16b894a0b894eccc'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Hashlib provides the following constant attributes:</p>
|
||
<dl class="data">
|
||
<dt id="hashlib.algorithms_guaranteed">
|
||
<code class="descclassname">hashlib.</code><code class="descname">algorithms_guaranteed</code><a class="headerlink" href="#hashlib.algorithms_guaranteed" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>A set containing the names of the hash algorithms guaranteed to be supported
|
||
by this module on all platforms. Note that ‘md5’ is in this list despite
|
||
some upstream vendors offering an odd “FIPS compliant” Python build that
|
||
excludes it.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.2.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="hashlib.algorithms_available">
|
||
<code class="descclassname">hashlib.</code><code class="descname">algorithms_available</code><a class="headerlink" href="#hashlib.algorithms_available" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>A set containing the names of the hash algorithms that are available in the
|
||
running Python interpreter. These names will be recognized when passed 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>. <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
|
||
same algorithm may appear multiple times in this set under different names
|
||
(thanks to OpenSSL).</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.2.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<p>The following values are provided as constant attributes of the hash objects
|
||
returned by the constructors:</p>
|
||
<dl class="data">
|
||
<dt id="hashlib.hash.digest_size">
|
||
<code class="descclassname">hash.</code><code class="descname">digest_size</code><a class="headerlink" href="#hashlib.hash.digest_size" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The size of the resulting hash in bytes.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="hashlib.hash.block_size">
|
||
<code class="descclassname">hash.</code><code class="descname">block_size</code><a class="headerlink" href="#hashlib.hash.block_size" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The internal block size of the hash algorithm in bytes.</p>
|
||
</dd></dl>
|
||
|
||
<p>A hash object has the following attributes:</p>
|
||
<dl class="attribute">
|
||
<dt id="hashlib.hash.name">
|
||
<code class="descclassname">hash.</code><code class="descname">name</code><a class="headerlink" href="#hashlib.hash.name" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The canonical name of this hash, always lowercase and always suitable as a
|
||
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>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.4: </span>The name attribute has been present in CPython since its inception, but
|
||
until Python 3.4 was not formally specified, so may not exist on some
|
||
platforms.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<p>A hash object has the following methods:</p>
|
||
<dl class="method">
|
||
<dt id="hashlib.hash.update">
|
||
<code class="descclassname">hash.</code><code class="descname">update</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.update" title="Permalink to this definition">¶</a></dt>
|
||
<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>.
|
||
Repeated calls are equivalent to a single call with the
|
||
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
|
||
equivalent to <code class="docutils literal notranslate"><span class="pre">m.update(a+b)</span></code>.</p>
|
||
<div class="versionchanged">
|
||
<p><span class="versionmodified changed">Changed in version 3.1: </span>The Python GIL is released to allow other threads to run while hash
|
||
updates on data larger than 2047 bytes is taking place when using hash
|
||
algorithms supplied by OpenSSL.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="hashlib.hash.digest">
|
||
<code class="descclassname">hash.</code><code class="descname">digest</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.digest" title="Permalink to this definition">¶</a></dt>
|
||
<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.
|
||
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
|
||
the whole range from 0 to 255.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="hashlib.hash.hexdigest">
|
||
<code class="descclassname">hash.</code><code class="descname">hexdigest</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.hexdigest" title="Permalink to this definition">¶</a></dt>
|
||
<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
|
||
double length, containing only hexadecimal digits. This may be used to
|
||
exchange the value safely in email or other non-binary environments.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="hashlib.hash.copy">
|
||
<code class="descclassname">hash.</code><code class="descname">copy</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.hash.copy" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return a copy (“clone”) of the hash object. This can be used to efficiently
|
||
compute the digests of data sharing a common initial substring.</p>
|
||
</dd></dl>
|
||
|
||
</div>
|
||
<div class="section" id="shake-variable-length-digests">
|
||
<h2>SHAKE variable length digests<a class="headerlink" href="#shake-variable-length-digests" title="Permalink to this headline">¶</a></h2>
|
||
<p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">shake_128()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">shake_256()</span></code> algorithms provide variable
|
||
length digests with length_in_bits//2 up to 128 or 256 bits of security.
|
||
As such, their digest methods require a length. Maximum length is not limited
|
||
by the SHAKE algorithm.</p>
|
||
<dl class="method">
|
||
<dt id="hashlib.shake.digest">
|
||
<code class="descclassname">shake.</code><code class="descname">digest</code><span class="sig-paren">(</span><em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.shake.digest" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>Return the digest of the data passed to the <code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code> method so far.
|
||
This is a bytes object of size <em>length</em> which may contain bytes in
|
||
the whole range from 0 to 255.</p>
|
||
</dd></dl>
|
||
|
||
<dl class="method">
|
||
<dt id="hashlib.shake.hexdigest">
|
||
<code class="descclassname">shake.</code><code class="descname">hexdigest</code><span class="sig-paren">(</span><em>length</em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.shake.hexdigest" title="Permalink to this definition">¶</a></dt>
|
||
<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
|
||
double length, containing only hexadecimal digits. This may be used to
|
||
exchange the value safely in email or other non-binary environments.</p>
|
||
</dd></dl>
|
||
|
||
</div>
|
||
<div class="section" id="key-derivation">
|
||
<h2>Key derivation<a class="headerlink" href="#key-derivation" title="Permalink to this headline">¶</a></h2>
|
||
<p>Key derivation and key stretching algorithms are designed for secure password
|
||
hashing. Naive algorithms such as <code class="docutils literal notranslate"><span class="pre">sha1(password)</span></code> are not resistant against
|
||
brute-force attacks. A good password hashing function must be tunable, slow, and
|
||
include a <a class="reference external" href="https://en.wikipedia.org/wiki/Salt_%28cryptography%29">salt</a>.</p>
|
||
<dl class="function">
|
||
<dt id="hashlib.pbkdf2_hmac">
|
||
<code class="descclassname">hashlib.</code><code class="descname">pbkdf2_hmac</code><span class="sig-paren">(</span><em>hash_name</em>, <em>password</em>, <em>salt</em>, <em>iterations</em>, <em>dklen=None</em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.pbkdf2_hmac" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The function provides PKCS#5 password-based key derivation function 2. It
|
||
uses HMAC as pseudorandom function.</p>
|
||
<p>The string <em>hash_name</em> is the desired name of the hash digest algorithm for
|
||
HMAC, e.g. ‘sha1’ or ‘sha256’. <em>password</em> and <em>salt</em> are interpreted as
|
||
buffers of bytes. Applications and libraries should limit <em>password</em> to
|
||
a sensible length (e.g. 1024). <em>salt</em> should be about 16 or more 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>
|
||
<p>The number of <em>iterations</em> should be chosen based on the hash algorithm and
|
||
computing power. As of 2013, at least 100,000 iterations of SHA-256 are
|
||
suggested.</p>
|
||
<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
|
||
digest size of the hash algorithm <em>hash_name</em> is used, e.g. 64 for SHA-512.</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">hashlib</span><span class="o">,</span> <span class="nn">binascii</span>
|
||
<span class="gp">>>> </span><span class="n">dk</span> <span class="o">=</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">pbkdf2_hmac</span><span class="p">(</span><span class="s1">'sha256'</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'password'</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'salt'</span><span class="p">,</span> <span class="mi">100000</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">binascii</span><span class="o">.</span><span class="n">hexlify</span><span class="p">(</span><span class="n">dk</span><span class="p">)</span>
|
||
<span class="go">b'0394a2ede332c9a13eb82e9b24631604c31df978b4e2f0fbd2c549944f9d79a5'</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.4.</span></p>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>A fast implementation of <em>pbkdf2_hmac</em> is available with OpenSSL. The
|
||
Python implementation uses an inline version of <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>. It is about
|
||
three times slower and doesn’t release the GIL.</p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="hashlib.scrypt">
|
||
<code class="descclassname">hashlib.</code><code class="descname">scrypt</code><span class="sig-paren">(</span><em>password</em>, <em>*</em>, <em>salt</em>, <em>n</em>, <em>r</em>, <em>p</em>, <em>maxmem=0</em>, <em>dklen=64</em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.scrypt" title="Permalink to this definition">¶</a></dt>
|
||
<dd><p>The function provides scrypt password-based key derivation function as
|
||
defined in <span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7914.html"><strong>RFC 7914</strong></a>.</p>
|
||
<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>
|
||
to a sensible length (e.g. 1024). <em>salt</em> should be about 16 or more
|
||
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>
|
||
<p><em>n</em> is the CPU/Memory cost factor, <em>r</em> the block size, <em>p</em> parallelization
|
||
factor and <em>maxmem</em> limits memory (OpenSSL 1.1.0 defaults to 32 MiB).
|
||
<em>dklen</em> is the length of the derived key.</p>
|
||
<p class="availability"><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: OpenSSL 1.1+.</p>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">New in version 3.6.</span></p>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
</div>
|
||
<div class="section" id="blake2">
|
||
<h2>BLAKE2<a class="headerlink" href="#blake2" title="Permalink to this headline">¶</a></h2>
|
||
<p id="index-4"><a class="reference external" href="https://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://tools.ietf.org/html/rfc7693.html"><strong>RFC 7693</strong></a> that comes in two
|
||
flavors:</p>
|
||
<ul class="simple">
|
||
<li><p><strong>BLAKE2b</strong>, optimized for 64-bit platforms and produces digests of any size
|
||
between 1 and 64 bytes,</p></li>
|
||
<li><p><strong>BLAKE2s</strong>, optimized for 8- to 32-bit platforms and produces digests of any
|
||
size between 1 and 32 bytes.</p></li>
|
||
</ul>
|
||
<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>),
|
||
<strong>salted hashing</strong>, <strong>personalization</strong>, and <strong>tree hashing</strong>.</p>
|
||
<p>Hash objects from this module follow the API of standard library’s
|
||
<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>
|
||
<div class="section" id="creating-hash-objects">
|
||
<h3>Creating hash objects<a class="headerlink" href="#creating-hash-objects" title="Permalink to this headline">¶</a></h3>
|
||
<p>New hash objects are created by calling constructor functions:</p>
|
||
<dl class="function">
|
||
<dt id="hashlib.blake2b">
|
||
<code class="descclassname">hashlib.</code><code class="descname">blake2b</code><span class="sig-paren">(</span><em>data=b''</em>, <em>*</em>, <em>digest_size=64</em>, <em>key=b''</em>, <em>salt=b''</em>, <em>person=b''</em>, <em>fanout=1</em>, <em>depth=1</em>, <em>leaf_size=0</em>, <em>node_offset=0</em>, <em>node_depth=0</em>, <em>inner_size=0</em>, <em>last_node=False</em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.blake2b" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="hashlib.blake2s">
|
||
<code class="descclassname">hashlib.</code><code class="descname">blake2s</code><span class="sig-paren">(</span><em>data=b''</em>, <em>*</em>, <em>digest_size=32</em>, <em>key=b''</em>, <em>salt=b''</em>, <em>person=b''</em>, <em>fanout=1</em>, <em>depth=1</em>, <em>leaf_size=0</em>, <em>node_offset=0</em>, <em>node_depth=0</em>, <em>inner_size=0</em>, <em>last_node=False</em><span class="sig-paren">)</span><a class="headerlink" href="#hashlib.blake2s" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>These functions return the corresponding hash objects for calculating
|
||
BLAKE2b or BLAKE2s. They optionally take these general parameters:</p>
|
||
<ul class="simple">
|
||
<li><p><em>data</em>: initial chunk of data to hash, which must be
|
||
<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>
|
||
<li><p><em>digest_size</em>: size of output digest in bytes.</p></li>
|
||
<li><p><em>key</em>: key for keyed hashing (up to 64 bytes for BLAKE2b, up to 32 bytes for
|
||
BLAKE2s).</p></li>
|
||
<li><p><em>salt</em>: salt for randomized hashing (up to 16 bytes for BLAKE2b, up to 8
|
||
bytes for BLAKE2s).</p></li>
|
||
<li><p><em>person</em>: personalization string (up to 16 bytes for BLAKE2b, up to 8 bytes
|
||
for BLAKE2s).</p></li>
|
||
</ul>
|
||
<p>The following table shows limits for general parameters (in bytes):</p>
|
||
<table class="docutils align-center">
|
||
<colgroup>
|
||
<col style="width: 15%" />
|
||
<col style="width: 24%" />
|
||
<col style="width: 17%" />
|
||
<col style="width: 20%" />
|
||
<col style="width: 24%" />
|
||
</colgroup>
|
||
<thead>
|
||
<tr class="row-odd"><th class="head"><p>Hash</p></th>
|
||
<th class="head"><p>digest_size</p></th>
|
||
<th class="head"><p>len(key)</p></th>
|
||
<th class="head"><p>len(salt)</p></th>
|
||
<th class="head"><p>len(person)</p></th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr class="row-even"><td><p>BLAKE2b</p></td>
|
||
<td><p>64</p></td>
|
||
<td><p>64</p></td>
|
||
<td><p>16</p></td>
|
||
<td><p>16</p></td>
|
||
</tr>
|
||
<tr class="row-odd"><td><p>BLAKE2s</p></td>
|
||
<td><p>32</p></td>
|
||
<td><p>32</p></td>
|
||
<td><p>8</p></td>
|
||
<td><p>8</p></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">Note</p>
|
||
<p>BLAKE2 specification defines constant lengths for salt and personalization
|
||
parameters, however, for convenience, this implementation accepts byte
|
||
strings of any size up to the specified length. If the length of the
|
||
parameter is less than specified, it is padded with zeros, thus, for
|
||
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
|
||
the case for <em>key</em>.)</p>
|
||
</div>
|
||
<p>These sizes are available as module <a class="reference internal" href="#constants">constants</a> described below.</p>
|
||
<p>Constructor functions also accept the following tree hashing parameters:</p>
|
||
<ul class="simple">
|
||
<li><p><em>fanout</em>: fanout (0 to 255, 0 if unlimited, 1 in sequential mode).</p></li>
|
||
<li><p><em>depth</em>: maximal depth of tree (1 to 255, 255 if unlimited, 1 in
|
||
sequential mode).</p></li>
|
||
<li><p><em>leaf_size</em>: maximal byte length of leaf (0 to 2**32-1, 0 if unlimited or in
|
||
sequential mode).</p></li>
|
||
<li><p><em>node_offset</em>: node offset (0 to 2**64-1 for BLAKE2b, 0 to 2**48-1 for
|
||
BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode).</p></li>
|
||
<li><p><em>node_depth</em>: node depth (0 to 255, 0 for leaves, or in sequential mode).</p></li>
|
||
<li><p><em>inner_size</em>: inner digest size (0 to 64 for BLAKE2b, 0 to 32 for
|
||
BLAKE2s, 0 in sequential mode).</p></li>
|
||
<li><p><em>last_node</em>: boolean indicating whether the processed node is the last
|
||
one (<cite>False</cite> for sequential mode).</p></li>
|
||
</ul>
|
||
<div class="figure align-center">
|
||
<img alt="Explanation of tree mode parameters." src="../_images/hashlib-blake2-tree.png" />
|
||
</div>
|
||
<p>See section 2.10 in <a class="reference external" href="https://blake2.net/blake2_20130129.pdf">BLAKE2 specification</a> for comprehensive review of tree
|
||
hashing.</p>
|
||
</div>
|
||
<div class="section" id="constants">
|
||
<h3>Constants<a class="headerlink" href="#constants" title="Permalink to this headline">¶</a></h3>
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2b.SALT_SIZE">
|
||
<code class="descclassname">blake2b.</code><code class="descname">SALT_SIZE</code><a class="headerlink" href="#hashlib.blake2b.SALT_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2s.SALT_SIZE">
|
||
<code class="descclassname">blake2s.</code><code class="descname">SALT_SIZE</code><a class="headerlink" href="#hashlib.blake2s.SALT_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>Salt length (maximum length accepted by constructors).</p>
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2b.PERSON_SIZE">
|
||
<code class="descclassname">blake2b.</code><code class="descname">PERSON_SIZE</code><a class="headerlink" href="#hashlib.blake2b.PERSON_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2s.PERSON_SIZE">
|
||
<code class="descclassname">blake2s.</code><code class="descname">PERSON_SIZE</code><a class="headerlink" href="#hashlib.blake2s.PERSON_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>Personalization string length (maximum length accepted by constructors).</p>
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2b.MAX_KEY_SIZE">
|
||
<code class="descclassname">blake2b.</code><code class="descname">MAX_KEY_SIZE</code><a class="headerlink" href="#hashlib.blake2b.MAX_KEY_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2s.MAX_KEY_SIZE">
|
||
<code class="descclassname">blake2s.</code><code class="descname">MAX_KEY_SIZE</code><a class="headerlink" href="#hashlib.blake2s.MAX_KEY_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>Maximum key size.</p>
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2b.MAX_DIGEST_SIZE">
|
||
<code class="descclassname">blake2b.</code><code class="descname">MAX_DIGEST_SIZE</code><a class="headerlink" href="#hashlib.blake2b.MAX_DIGEST_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<dl class="data">
|
||
<dt id="hashlib.blake2s.MAX_DIGEST_SIZE">
|
||
<code class="descclassname">blake2s.</code><code class="descname">MAX_DIGEST_SIZE</code><a class="headerlink" href="#hashlib.blake2s.MAX_DIGEST_SIZE" title="Permalink to this definition">¶</a></dt>
|
||
<dd></dd></dl>
|
||
|
||
<p>Maximum digest size that the hash function can output.</p>
|
||
</div>
|
||
<div class="section" id="examples">
|
||
<h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
|
||
<div class="section" id="simple-hashing">
|
||
<h4>Simple hashing<a class="headerlink" href="#simple-hashing" title="Permalink to this headline">¶</a></h4>
|
||
<p>To calculate hash of some data, you should first construct a hash object by
|
||
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
|
||
<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 <code class="xref py py-meth docutils literal notranslate"><span class="pre">update()</span></code> on the
|
||
object, and, finally, get the digest out of the object by calling
|
||
<code class="xref py py-meth docutils literal notranslate"><span class="pre">digest()</span></code> (or <code class="xref py py-meth docutils literal notranslate"><span class="pre">hexdigest()</span></code> for hex-encoded string).</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<span class="gp">>>> </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">()</span>
|
||
<span class="gp">>>> </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">'Hello world'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>As a shortcut, you can pass the first chunk of data to update directly to the
|
||
constructor as the positional argument:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<span class="gp">>>> </span><span class="n">blake2b</span><span class="p">(</span><span class="sa">b</span><span class="s1">'Hello world'</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'</span>
|
||
</pre></div>
|
||
</div>
|
||
<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
|
||
update the hash:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<span class="gp">>>> </span><span class="n">items</span> <span class="o">=</span> <span class="p">[</span><span class="sa">b</span><span class="s1">'Hello'</span><span class="p">,</span> <span class="sa">b</span><span class="s1">' '</span><span class="p">,</span> <span class="sa">b</span><span class="s1">'world'</span><span class="p">]</span>
|
||
<span class="gp">>>> </span><span class="n">h</span> <span class="o">=</span> <span class="n">blake2b</span><span class="p">()</span>
|
||
<span class="gp">>>> </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>
|
||
<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>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="using-different-digest-sizes">
|
||
<h4>Using different digest sizes<a class="headerlink" href="#using-different-digest-sizes" title="Permalink to this headline">¶</a></h4>
|
||
<p>BLAKE2 has configurable size of digests up to 64 bytes for BLAKE2b and up to 32
|
||
bytes for BLAKE2s. For example, to replace SHA-1 with BLAKE2b without changing
|
||
the size of output, we can tell BLAKE2b to produce 20-byte digests:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<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="mi">20</span><span class="p">)</span>
|
||
<span class="gp">>>> </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">'Replacing SHA1 with the more secure function'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'d24f26cf8de66472d58d4e1b1774b4c9158b1f4c'</span>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">digest_size</span>
|
||
<span class="go">20</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">20</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Hash objects with different digest sizes have completely different outputs
|
||
(shorter hashes are <em>not</em> prefixes of longer hashes); BLAKE2b and BLAKE2s
|
||
produce different outputs even if the output length is the same:</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span><span class="p">,</span> <span class="n">blake2s</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">'6fa1d8fcfd719046d762'</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">'eb6ec15daf9546254f0809'</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">'1bf21a98c78a1c376ae9'</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">'567004bf96e4a25773ebf4'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="keyed-hashing">
|
||
<h4>Keyed hashing<a class="headerlink" href="#keyed-hashing" title="Permalink to this headline">¶</a></h4>
|
||
<p>Keyed hashing can be used for authentication as a faster and simpler
|
||
replacement for <a class="reference external" href="https://en.wikipedia.org/wiki/Hash-based_message_authentication_code">Hash-based message authentication code</a> (HMAC).
|
||
BLAKE2 can be securely used in prefix-MAC mode thanks to the
|
||
indifferentiability property inherited from BLAKE.</p>
|
||
<p>This example shows how to get a (hex-encoded) 128-bit authentication code for
|
||
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>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<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">key</span><span class="o">=</span><span class="sa">b</span><span class="s1">'pseudorandom key'</span><span class="p">,</span> <span class="n">digest_size</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span>
|
||
<span class="gp">>>> </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">'message data'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'3d363ff7401e02026f4a4687d4863ced'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>As a practical example, a web application can symmetrically sign cookies sent
|
||
to users and later verify them to make sure they weren’t tampered with:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hmac</span> <span class="k">import</span> <span class="n">compare_digest</span>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </span><span class="n">SECRET_KEY</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">'pseudorandomly generated server secret key'</span>
|
||
<span class="gp">>>> </span><span class="n">AUTH_SIZE</span> <span class="o">=</span> <span class="mi">16</span>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">sign</span><span class="p">(</span><span class="n">cookie</span><span class="p">):</span>
|
||
<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>
|
||
<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>
|
||
<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">'utf-8'</span><span class="p">)</span>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </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>
|
||
<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>
|
||
<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>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </span><span class="n">cookie</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">'user-alice'</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </span><span class="nb">print</span><span class="p">(</span><span class="s2">"</span><span class="si">{0}</span><span class="s2">,</span><span class="si">{1}</span><span class="s2">"</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">'utf-8'</span><span class="p">),</span> <span class="n">sig</span><span class="p">))</span>
|
||
<span class="go">user-alice,b'43b3c982cf697e0c5ab22172d1ca7421'</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">True</span>
|
||
<span class="gp">>>> </span><span class="n">verify</span><span class="p">(</span><span class="sa">b</span><span class="s1">'user-bob'</span><span class="p">,</span> <span class="n">sig</span><span class="p">)</span>
|
||
<span class="go">False</span>
|
||
<span class="gp">>>> </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">'0102030405060708090a0b0c0d0e0f00'</span><span class="p">)</span>
|
||
<span class="go">False</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Even though there’s a native keyed hashing mode, BLAKE2 can, of course, be used
|
||
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>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">hmac</span><span class="o">,</span> <span class="nn">hashlib</span>
|
||
<span class="gp">>>> </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">'secret key'</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>
|
||
<span class="gp">>>> </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">'message'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">m</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'e3c8102868d28b5ff85fc35dda07329970d1a01e273c37481326fe0c861c8142'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="randomized-hashing">
|
||
<h4>Randomized hashing<a class="headerlink" href="#randomized-hashing" title="Permalink to this headline">¶</a></h4>
|
||
<p>By setting <em>salt</em> parameter users can introduce randomization to the hash
|
||
function. Randomized hashing is useful for protecting against collision attacks
|
||
on the hash function used in digital signatures.</p>
|
||
<blockquote>
|
||
<div><p>Randomized hashing is designed for situations where one party, the message
|
||
preparer, generates all or part of a message to be signed by a second
|
||
party, the message signer. If the message preparer is able to find
|
||
cryptographic hash function collisions (i.e., two messages producing the
|
||
same hash value), then they might prepare meaningful versions of the message
|
||
that would produce the same hash value and digital signature, but with
|
||
different results (e.g., transferring $1,000,000 to an account, rather than
|
||
$10). Cryptographic hash functions have been designed with collision
|
||
resistance as a major goal, but the current concentration on attacking
|
||
cryptographic hash functions may result in a given cryptographic hash
|
||
function providing less collision resistance than expected. Randomized
|
||
hashing offers the signer additional protection by reducing the likelihood
|
||
that a preparer can generate two or more messages that ultimately yield the
|
||
same hash value during the digital signature generation process — even if
|
||
it is practical to find collisions for the hash function. However, the use
|
||
of randomized hashing may reduce the amount of security provided by a
|
||
digital signature when all portions of the message are prepared
|
||
by the signer.</p>
|
||
<p>(<a class="reference external" href="https://csrc.nist.gov/publications/detail/sp/800-106/final">NIST SP-800-106 “Randomized Hashing for Digital Signatures”</a>)</p>
|
||
</div></blockquote>
|
||
<p>In BLAKE2 the salt is processed as a one-time input to the hash function during
|
||
initialization, rather than as an input to each compression function.</p>
|
||
<div class="admonition warning">
|
||
<p class="admonition-title">Warning</p>
|
||
<p><em>Salted hashing</em> (or just hashing) with BLAKE2 or any other general-purpose
|
||
cryptographic hash function, such as SHA-256, is not suitable for hashing
|
||
passwords. See <a class="reference external" href="https://blake2.net/#qa">BLAKE2 FAQ</a> for more
|
||
information.</p>
|
||
</div>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">os</span>
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<span class="gp">>>> </span><span class="n">msg</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">'some message'</span>
|
||
<span class="gp">>>> </span><span class="c1"># Calculate the first hash with a random salt.</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </span><span class="c1"># Calculate the second hash with a different random salt.</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </span><span class="c1"># The digests are different.</span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">True</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="personalization">
|
||
<h4>Personalization<a class="headerlink" href="#personalization" title="Permalink to this headline">¶</a></h4>
|
||
<p>Sometimes it is useful to force hash function to produce different digests for
|
||
the same input for different purposes. Quoting the authors of the Skein hash
|
||
function:</p>
|
||
<blockquote>
|
||
<div><p>We recommend that all application designers seriously consider doing this;
|
||
we have seen many protocols where a hash that is computed in one part of
|
||
the protocol can be used in an entirely different part because two hash
|
||
computations were done on similar or related data, and the attacker can
|
||
force the application to make the hash inputs the same. Personalizing each
|
||
hash function used in the protocol summarily stops this type of attack.</p>
|
||
<p>(<a class="reference external" href="http://www.skein-hash.info/sites/default/files/skein1.3.pdf">The Skein Hash Function Family</a>,
|
||
p. 21)</p>
|
||
</div></blockquote>
|
||
<p>BLAKE2 can be personalized by passing bytes to the <em>person</em> argument:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<span class="gp">>>> </span><span class="n">FILES_HASH_PERSON</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">'MyApp Files Hash'</span>
|
||
<span class="gp">>>> </span><span class="n">BLOCK_HASH_PERSON</span> <span class="o">=</span> <span class="sa">b</span><span class="s1">'MyApp Block Hash'</span>
|
||
<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="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>
|
||
<span class="gp">>>> </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">'the same content'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'20d9cd024d4fb086aae819a1432dd2466de12947831b75c5a30cf2676095d3b4'</span>
|
||
<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="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>
|
||
<span class="gp">>>> </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">'the same content'</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">h</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'cf68fb5761b9c44e7878bfb2c4c9aea52264a80b75005e65619778de59f383a3'</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Personalization together with the keyed mode can also be used to derive different
|
||
keys from a single one.</p>
|
||
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2s</span>
|
||
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">base64</span> <span class="k">import</span> <span class="n">b64decode</span><span class="p">,</span> <span class="n">b64encode</span>
|
||
<span class="gp">>>> </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">'Rm5EPJai72qcK3RGBpW3vPNfZy5OZothY+kHY6h21KM='</span><span class="p">)</span>
|
||
<span class="gp">>>> </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">'kEncrypt'</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
|
||
<span class="gp">>>> </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">'kMAC'</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span>
|
||
<span class="gp">>>> </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">'utf-8'</span><span class="p">))</span>
|
||
<span class="go">rbPb15S/Z9t+agffno5wuhB77VbRi6F9Iv2qIxU7WHw=</span>
|
||
<span class="gp">>>> </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">'utf-8'</span><span class="p">))</span>
|
||
<span class="go">G9GtHFE1YluXY1zWPlYk1e/nWfu0WSEb0KRcjhDeP/o=</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="tree-mode">
|
||
<h4>Tree mode<a class="headerlink" href="#tree-mode" title="Permalink to this headline">¶</a></h4>
|
||
<p>Here’s an example of hashing a minimal tree with two leaf nodes:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span> <span class="mi">10</span>
|
||
<span class="o">/</span> \
|
||
<span class="mi">00</span> <span class="mi">01</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This example uses 64-byte internal digests, and returns the 32-byte final
|
||
digest:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hashlib</span> <span class="k">import</span> <span class="n">blake2b</span>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </span><span class="n">FANOUT</span> <span class="o">=</span> <span class="mi">2</span>
|
||
<span class="gp">>>> </span><span class="n">DEPTH</span> <span class="o">=</span> <span class="mi">2</span>
|
||
<span class="gp">>>> </span><span class="n">LEAF_SIZE</span> <span class="o">=</span> <span class="mi">4096</span>
|
||
<span class="gp">>>> </span><span class="n">INNER_SIZE</span> <span class="o">=</span> <span class="mi">64</span>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </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>
|
||
<span class="go">>>></span>
|
||
<span class="gp">>>> </span><span class="c1"># Left leaf</span>
|
||
<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>
|
||
<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>
|
||
<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>
|
||
<span class="gp">>>> </span><span class="c1"># Right leaf</span>
|
||
<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>
|
||
<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>
|
||
<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>
|
||
<span class="gp">>>> </span><span class="c1"># Root node</span>
|
||
<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>
|
||
<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>
|
||
<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>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </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>
|
||
<span class="gp">>>> </span><span class="n">h10</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>
|
||
<span class="go">'3ad2a9b37c6070e374c7a8c508fe20ca86b6ed54e286e93a0318e95e881db5aa'</span>
|
||
</pre></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="section" id="credits">
|
||
<h3>Credits<a class="headerlink" href="#credits" title="Permalink to this headline">¶</a></h3>
|
||
<p><a class="reference external" href="https://blake2.net">BLAKE2</a> was designed by <em>Jean-Philippe Aumasson</em>, <em>Samuel Neves</em>, <em>Zooko
|
||
Wilcox-O’Hearn</em>, and <em>Christian Winnerlein</em> based on <a class="reference external" href="https://en.wikipedia.org/wiki/NIST_hash_function_competition">SHA-3</a> finalist <a class="reference external" href="https://131002.net/blake/">BLAKE</a>
|
||
created by <em>Jean-Philippe Aumasson</em>, <em>Luca Henzen</em>, <em>Willi Meier</em>, and
|
||
<em>Raphael C.-W. Phan</em>.</p>
|
||
<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>
|
||
<p>The stdlib implementation is based on <a class="reference external" href="https://pythonhosted.org/pyblake2/">pyblake2</a> module. It was written by
|
||
<em>Dmitry Chestnykh</em> based on C implementation written by <em>Samuel Neves</em>. The
|
||
documentation was copied from <a class="reference external" href="https://pythonhosted.org/pyblake2/">pyblake2</a> and written by <em>Dmitry Chestnykh</em>.</p>
|
||
<p>The C code was partly rewritten for Python by <em>Christian Heimes</em>.</p>
|
||
<p>The following public domain dedication applies for both C hash function
|
||
implementation, extension code, and this documentation:</p>
|
||
<blockquote>
|
||
<div><p>To the extent possible under law, the author(s) have dedicated all copyright
|
||
and related and neighboring rights to this software to the public domain
|
||
worldwide. This software is distributed without any warranty.</p>
|
||
<p>You should have received a copy of the CC0 Public Domain Dedication along
|
||
with this software. If not, see
|
||
<a class="reference external" href="https://creativecommons.org/publicdomain/zero/1.0/">https://creativecommons.org/publicdomain/zero/1.0/</a>.</p>
|
||
</div></blockquote>
|
||
<p>The following people have helped with development or contributed their changes
|
||
to the project and the public domain according to the Creative Commons Public
|
||
Domain Dedication 1.0 Universal:</p>
|
||
<ul class="simple">
|
||
<li><p><em>Alexandr Sokolovskiy</em></p></li>
|
||
</ul>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">See also</p>
|
||
<dl class="simple">
|
||
<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>
|
||
</dd>
|
||
<dt>Module <a class="reference internal" href="base64.html#module-base64" title="base64: RFC 3548: 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>
|
||
</dd>
|
||
<dt><a class="reference external" href="https://blake2.net">https://blake2.net</a></dt><dd><p>Official BLAKE2 website.</p>
|
||
</dd>
|
||
<dt><a class="reference external" href="https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf">https://csrc.nist.gov/csrc/media/publications/fips/180/2/archive/2002-08-01/documents/fips180-2.pdf</a></dt><dd><p>The FIPS 180-2 publication on Secure Hash Algorithms.</p>
|
||
</dd>
|
||
<dt><a class="reference external" href="https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms">https://en.wikipedia.org/wiki/Cryptographic_hash_function#Cryptographic_hash_algorithms</a></dt><dd><p>Wikipedia article with information on which algorithms have known issues and
|
||
what that means regarding their use.</p>
|
||
</dd>
|
||
<dt><a class="reference external" href="https://www.ietf.org/rfc/rfc2898.txt">https://www.ietf.org/rfc/rfc2898.txt</a></dt><dd><p>PKCS #5: Password-Based Cryptography Specification Version 2.0</p>
|
||
</dd>
|
||
</dl>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">Table of Contents</a></h3>
|
||
<ul>
|
||
<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>
|
||
<li><a class="reference internal" href="#hash-algorithms">Hash algorithms</a></li>
|
||
<li><a class="reference internal" href="#shake-variable-length-digests">SHAKE variable length digests</a></li>
|
||
<li><a class="reference internal" href="#key-derivation">Key derivation</a></li>
|
||
<li><a class="reference internal" href="#blake2">BLAKE2</a><ul>
|
||
<li><a class="reference internal" href="#creating-hash-objects">Creating hash objects</a></li>
|
||
<li><a class="reference internal" href="#constants">Constants</a></li>
|
||
<li><a class="reference internal" href="#examples">Examples</a><ul>
|
||
<li><a class="reference internal" href="#simple-hashing">Simple hashing</a></li>
|
||
<li><a class="reference internal" href="#using-different-digest-sizes">Using different digest sizes</a></li>
|
||
<li><a class="reference internal" href="#keyed-hashing">Keyed hashing</a></li>
|
||
<li><a class="reference internal" href="#randomized-hashing">Randomized hashing</a></li>
|
||
<li><a class="reference internal" href="#personalization">Personalization</a></li>
|
||
<li><a class="reference internal" href="#tree-mode">Tree mode</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#credits">Credits</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>Previous topic</h4>
|
||
<p class="topless"><a href="crypto.html"
|
||
title="previous chapter">Cryptographic Services</a></p>
|
||
<h4>Next topic</h4>
|
||
<p class="topless"><a href="hmac.html"
|
||
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>
|
||
<div role="note" aria-label="source link">
|
||
<h3>This Page</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">Report a Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/hashlib.rst"
|
||
rel="nofollow">Show Source
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>Navigation</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="General Index"
|
||
>index</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python Module Index"
|
||
>modules</a> |</li>
|
||
<li class="right" >
|
||
<a href="hmac.html" title="hmac — Keyed-Hashing for Message Authentication"
|
||
>next</a> |</li>
|
||
<li class="right" >
|
||
<a href="crypto.html" title="Cryptographic Services"
|
||
>previous</a> |</li>
|
||
<li><img src="../_static/py.png" alt=""
|
||
style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li>
|
||
<span class="language_switcher_placeholder">en</span>
|
||
<span class="version_switcher_placeholder">3.7.4</span>
|
||
<a href="../index.html">Documentation </a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="crypto.html" >Cryptographic Services</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" style="display: none" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="Quick search" type="text" name="q" />
|
||
<input type="submit" value="Go" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
<script type="text/javascript">$('.inline-search').show(0);</script>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
|
||
<br />
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
Last updated on Jul 13, 2019.
|
||
<a href="../bugs.html">Found a bug</a>?
|
||
<br />
|
||
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |