415 lines
28 KiB
HTML
415 lines
28 KiB
HTML
|
||
<!DOCTYPE html>
|
||
|
||
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
|
||
<head>
|
||
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
|
||
|
||
<title>secrets --- 生成管理密码的安全随机数 — Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
|
||
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
|
||
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
||
|
||
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
|
||
<script src="../_static/jquery.js"></script>
|
||
<script src="../_static/underscore.js"></script>
|
||
<script src="../_static/doctools.js"></script>
|
||
<script src="../_static/language_data.js"></script>
|
||
<script src="../_static/translations.js"></script>
|
||
|
||
<script src="../_static/sidebar.js"></script>
|
||
|
||
<link rel="search" type="application/opensearchdescription+xml"
|
||
title="在 Python 3.8.20 文档 中搜索"
|
||
href="../_static/opensearch.xml"/>
|
||
<link rel="author" title="关于这些文档" href="../about.html" />
|
||
<link rel="index" title="索引" href="../genindex.html" />
|
||
<link rel="search" title="搜索" href="../search.html" />
|
||
<link rel="copyright" title="版权所有" href="../copyright.html" />
|
||
<link rel="next" title="通用操作系统服务" href="allos.html" />
|
||
<link rel="prev" title="hmac --- 基于密钥的消息验证" href="hmac.html" />
|
||
<link rel="canonical" href="https://docs.python.org/3/library/secrets.html" />
|
||
|
||
|
||
|
||
|
||
|
||
<style>
|
||
@media only screen {
|
||
table.full-width-table {
|
||
width: 100%;
|
||
}
|
||
}
|
||
</style>
|
||
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
|
||
<script type="text/javascript" src="../_static/copybutton.js"></script>
|
||
<script type="text/javascript" src="../_static/menu.js"></script>
|
||
|
||
</head>
|
||
<body>
|
||
<div class="mobile-nav">
|
||
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
|
||
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
|
||
<label for="menuToggler" class="toggler__label">
|
||
<span></span>
|
||
</label>
|
||
<nav class="nav-content" role="navigation">
|
||
<a href="https://www.python.org/" class="nav-logo">
|
||
<img src="../_static/py.svg" alt="Logo"/>
|
||
</a>
|
||
<div class="version_switcher_placeholder"></div>
|
||
<form role="search" class="search" action="../search.html" method="get">
|
||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
|
||
<path fill-rule="nonzero"
|
||
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" fill="#444"></path>
|
||
</svg>
|
||
<input type="text" name="q" aria-label="快速搜索"/>
|
||
<input type="submit" value="转向"/>
|
||
</form>
|
||
</nav>
|
||
<div class="menu-wrapper">
|
||
<nav class="menu" role="navigation" aria-label="main navigation">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code> --- 生成管理密码的安全随机数</a><ul>
|
||
<li><a class="reference internal" href="#random-numbers">随机数</a></li>
|
||
<li><a class="reference internal" href="#generating-tokens">生成 Token</a><ul>
|
||
<li><a class="reference internal" href="#how-many-bytes-should-tokens-use">Token 应当使用多少个字节?</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#other-functions">其他功能</a></li>
|
||
<li><a class="reference internal" href="#recipes-and-best-practices">应用技巧与最佳实践</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="hmac.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code> --- 基于密钥的消息验证</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="allos.html"
|
||
title="下一章">通用操作系统服务</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/secrets.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</nav>
|
||
</div>
|
||
</div>
|
||
|
||
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
|
||
这个文档所针对的是一个已不再受支持的 Python 旧版本。
|
||
你应当升级版本,并阅读
|
||
<a href="/3/library/secrets.html"> Python 当前稳定版本的文档</a>.
|
||
</div>
|
||
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
accesskey="I">索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="allos.html" title="通用操作系统服务"
|
||
accesskey="N">下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="hmac.html" title="hmac --- 基于密钥的消息验证"
|
||
accesskey="P">上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="crypto.html" accesskey="U">加密服务</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
|
||
<div class="document">
|
||
<div class="documentwrapper">
|
||
<div class="bodywrapper">
|
||
<div class="body" role="main">
|
||
|
||
<section id="module-secrets">
|
||
<span id="secrets-generate-secure-random-numbers-for-managing-secrets"></span><h1><a class="reference internal" href="#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> --- 生成管理密码的安全随机数<a class="headerlink" href="#module-secrets" title="永久链接至标题">¶</a></h1>
|
||
<div class="versionadded">
|
||
<p><span class="versionmodified added">3.6 新版功能.</span></p>
|
||
</div>
|
||
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/secrets.py">Lib/secrets.py</a></p>
|
||
<hr class="docutils" />
|
||
<p><a class="reference internal" href="#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> 模块用于生成高度加密的随机数,适于管理密码、账户验证、安全凭据及机密数据。</p>
|
||
<p>最好用 <a class="reference internal" href="#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> 替代 <a class="reference internal" href="random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">random</span></code></a> 模块的默认伪随机数生成器,该生成器适用于建模和模拟,不宜用于安全与加密。</p>
|
||
<div class="admonition seealso">
|
||
<p class="admonition-title">参见</p>
|
||
<p><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0506"><strong>PEP 506</strong></a></p>
|
||
</div>
|
||
<section id="random-numbers">
|
||
<h2>随机数<a class="headerlink" href="#random-numbers" title="永久链接至标题">¶</a></h2>
|
||
<p><a class="reference internal" href="#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> 模块是操作系统提供的最安全地随机性来源。</p>
|
||
<dl class="class">
|
||
<dt id="secrets.SystemRandom">
|
||
<em class="property">class </em><code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">SystemRandom</code><a class="headerlink" href="#secrets.SystemRandom" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>用操作系统提供的最高质量源生成随机数的类。详见 <a class="reference internal" href="random.html#random.SystemRandom" title="random.SystemRandom"><code class="xref py py-class docutils literal notranslate"><span class="pre">random.SystemRandom</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="secrets.choice">
|
||
<code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">choice</code><span class="sig-paren">(</span><em class="sig-param">sequence</em><span class="sig-paren">)</span><a class="headerlink" href="#secrets.choice" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回从非空序列中随机选取的元素。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="secrets.randbelow">
|
||
<code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">randbelow</code><span class="sig-paren">(</span><em class="sig-param">n</em><span class="sig-paren">)</span><a class="headerlink" href="#secrets.randbelow" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回 [0, <em>n</em>) 范围内的随机整数。</p>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="secrets.randbits">
|
||
<code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">randbits</code><span class="sig-paren">(</span><em class="sig-param">k</em><span class="sig-paren">)</span><a class="headerlink" href="#secrets.randbits" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回 <em>k</em> 个随机比特位的整数。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="generating-tokens">
|
||
<h2>生成 Token<a class="headerlink" href="#generating-tokens" title="永久链接至标题">¶</a></h2>
|
||
<p><a class="reference internal" href="#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> 模块提供了生成安全 Token 的函数,适用于密码重置、密保 URL 等应用场景。</p>
|
||
<dl class="function">
|
||
<dt id="secrets.token_bytes">
|
||
<code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">token_bytes</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">nbytes=None</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#secrets.token_bytes" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回含 <em>nbytes</em> 个字节的随机字节字符串。如果未提供 <em>nbytes</em>,或*nbytes* 为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,则使用合理的默认值。</p>
|
||
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">token_bytes</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span>
|
||
<span class="go">b'\xebr\x17D*t\xae\xd4\xe3S\xb6\xe2\xebP1\x8b'</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="secrets.token_hex">
|
||
<code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">token_hex</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">nbytes=None</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#secrets.token_hex" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回十六进制随机文本字符串。字符串有 <em>nbytes</em> 个随机字节,每个字节转换为两个十六进制数码。未提供 <em>nbytes</em> 或为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时,则使用合理的默认值。</p>
|
||
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">token_hex</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span>
|
||
<span class="go">'f9bf78b9a18ce6d46a0cd2b0b86df9da'</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<dl class="function">
|
||
<dt id="secrets.token_urlsafe">
|
||
<code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">token_urlsafe</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">nbytes=None</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#secrets.token_urlsafe" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>返回安全的 URL 随机文本字符串,包含 <em>nbytes</em> 个随机字节。文本用 Base64 编码,平均来说,每个字节对应 1.3 个结果字符。未提供 <em>nbytes</em> 或为 <code class="docutils literal notranslate"><span class="pre">None</span></code> 时,则使用合理的默认值。</p>
|
||
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">token_urlsafe</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span>
|
||
<span class="go">'Drmhze6EPcv0fN_81Bj-nA'</span>
|
||
</pre></div>
|
||
</div>
|
||
</dd></dl>
|
||
|
||
<section id="how-many-bytes-should-tokens-use">
|
||
<h3>Token 应当使用多少个字节?<a class="headerlink" href="#how-many-bytes-should-tokens-use" title="永久链接至标题">¶</a></h3>
|
||
<p>为了在面对 <a class="reference external" href="https://en.wikipedia.org/wiki/Brute-force_attack">暴力攻击</a> 时保证安全,Token 的随机性必须足够高。随着计算机推衍能力的不断提升,随机性的安全标准也要不断提高。比如 2015 年,32 字节(256 位)的随机性对于 <a class="reference internal" href="#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> 模块的典型用例就已经足够了。</p>
|
||
<p>要自行管理 Token 长度的用户,可以通过为 <code class="docutils literal notranslate"><span class="pre">token_*</span></code> 函数指定 <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> 参数显式指定 Token 要使用多大的随机性。该参数以字节数表示随机性大小。</p>
|
||
<p>反之,如果未提供参数,或参数为 <code class="docutils literal notranslate"><span class="pre">None</span></code>,则 <code class="docutils literal notranslate"><span class="pre">token_*</span></code> 函数将使用合理的默认值。</p>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>该默认值随时可能会改变,比如,版本更新的时候。</p>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
<section id="other-functions">
|
||
<h2>其他功能<a class="headerlink" href="#other-functions" title="永久链接至标题">¶</a></h2>
|
||
<dl class="function">
|
||
<dt id="secrets.compare_digest">
|
||
<code class="sig-prename descclassname">secrets.</code><code class="sig-name descname">compare_digest</code><span class="sig-paren">(</span><em class="sig-param">a</em>, <em class="sig-param">b</em><span class="sig-paren">)</span><a class="headerlink" href="#secrets.compare_digest" title="永久链接至目标">¶</a></dt>
|
||
<dd><p>字符串 <em>a</em> 与 <em>b</em> 相等则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>,否则返回 <code class="docutils literal notranslate"><span class="pre">False</span></code>,这种方式可降低 <a class="reference external" href="https://codahale.com/a-lesson-in-timing-attacks/">定时攻击</a> 的风险。详见 <a class="reference internal" href="hmac.html#hmac.compare_digest" title="hmac.compare_digest"><code class="xref py py-func docutils literal notranslate"><span class="pre">hmac.compare_digest()</span></code></a>。</p>
|
||
</dd></dl>
|
||
|
||
</section>
|
||
<section id="recipes-and-best-practices">
|
||
<h2>应用技巧与最佳实践<a class="headerlink" href="#recipes-and-best-practices" title="永久链接至标题">¶</a></h2>
|
||
<p>本节展示了一些使用 <a class="reference internal" href="#module-secrets" title="secrets: Generate secure random numbers for managing secrets."><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code></a> 管理基本安全级别的应用技巧和最佳实践。</p>
|
||
<p>生成长度为八个字符的字母数字密码:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">string</span>
|
||
<span class="kn">import</span> <span class="nn">secrets</span>
|
||
<span class="n">alphabet</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">ascii_letters</span> <span class="o">+</span> <span class="n">string</span><span class="o">.</span><span class="n">digits</span>
|
||
<span class="n">password</span> <span class="o">=</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">secrets</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">alphabet</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">8</span><span class="p">))</span>
|
||
</pre></div>
|
||
</div>
|
||
<div class="admonition note">
|
||
<p class="admonition-title">注解</p>
|
||
<p>应用程序不能 <a class="reference external" href="http://cwe.mitre.org/data/definitions/257.html">以可恢复的格式存储密码</a>,无论是用纯文本还是加密。 它们应当使用高加密强度的单向(不可恢复)哈希函数来加盐并生成哈希值。</p>
|
||
</div>
|
||
<p>生成长度为十个字符的字母数字密码,包含至少一个小写字母,至少一个大写字母以及至少三个数字:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">string</span>
|
||
<span class="kn">import</span> <span class="nn">secrets</span>
|
||
<span class="n">alphabet</span> <span class="o">=</span> <span class="n">string</span><span class="o">.</span><span class="n">ascii_letters</span> <span class="o">+</span> <span class="n">string</span><span class="o">.</span><span class="n">digits</span>
|
||
<span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
|
||
<span class="n">password</span> <span class="o">=</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">secrets</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">alphabet</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
|
||
<span class="k">if</span> <span class="p">(</span><span class="nb">any</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">islower</span><span class="p">()</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">password</span><span class="p">)</span>
|
||
<span class="ow">and</span> <span class="nb">any</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">isupper</span><span class="p">()</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">password</span><span class="p">)</span>
|
||
<span class="ow">and</span> <span class="nb">sum</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">isdigit</span><span class="p">()</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">password</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">3</span><span class="p">):</span>
|
||
<span class="k">break</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>生成 <a class="reference external" href="https://xkcd.com/936/">XKCD 风格的密码串</a>:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">secrets</span>
|
||
<span class="c1"># On standard Linux systems, use a convenient dictionary file.</span>
|
||
<span class="c1"># Other platforms may need to provide their own word-list.</span>
|
||
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'/usr/share/dict/words'</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
|
||
<span class="n">words</span> <span class="o">=</span> <span class="p">[</span><span class="n">word</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="n">f</span><span class="p">]</span>
|
||
<span class="n">password</span> <span class="o">=</span> <span class="s1">' '</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">secrets</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">words</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>生成临时密保 URL,包含密码恢复应用的安全 Token:</p>
|
||
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">secrets</span>
|
||
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://mydomain.com/reset='</span> <span class="o">+</span> <span class="n">secrets</span><span class="o">.</span><span class="n">token_urlsafe</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
</section>
|
||
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
|
||
<div class="sphinxsidebarwrapper">
|
||
<h3><a href="../contents.html">目录</a></h3>
|
||
<ul>
|
||
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">secrets</span></code> --- 生成管理密码的安全随机数</a><ul>
|
||
<li><a class="reference internal" href="#random-numbers">随机数</a></li>
|
||
<li><a class="reference internal" href="#generating-tokens">生成 Token</a><ul>
|
||
<li><a class="reference internal" href="#how-many-bytes-should-tokens-use">Token 应当使用多少个字节?</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#other-functions">其他功能</a></li>
|
||
<li><a class="reference internal" href="#recipes-and-best-practices">应用技巧与最佳实践</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<h4>上一个主题</h4>
|
||
<p class="topless"><a href="hmac.html"
|
||
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">hmac</span></code> --- 基于密钥的消息验证</a></p>
|
||
<h4>下一个主题</h4>
|
||
<p class="topless"><a href="allos.html"
|
||
title="下一章">通用操作系统服务</a></p>
|
||
<div role="note" aria-label="source link">
|
||
<h3>本页</h3>
|
||
<ul class="this-page-menu">
|
||
<li><a href="../bugs.html">报告 Bug</a></li>
|
||
<li>
|
||
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/secrets.rst"
|
||
rel="nofollow">显示源代码
|
||
</a>
|
||
</li>
|
||
</ul>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="clearer"></div>
|
||
</div>
|
||
<div class="related" role="navigation" aria-label="related navigation">
|
||
<h3>导航</h3>
|
||
<ul>
|
||
<li class="right" style="margin-right: 10px">
|
||
<a href="../genindex.html" title="总目录"
|
||
>索引</a></li>
|
||
<li class="right" >
|
||
<a href="../py-modindex.html" title="Python 模块索引"
|
||
>模块</a> |</li>
|
||
<li class="right" >
|
||
<a href="allos.html" title="通用操作系统服务"
|
||
>下一页</a> |</li>
|
||
<li class="right" >
|
||
<a href="hmac.html" title="hmac --- 基于密钥的消息验证"
|
||
>上一页</a> |</li>
|
||
|
||
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
|
||
<li><a href="https://www.python.org/">Python</a> »</li>
|
||
<li class="switchers">
|
||
<div class="language_switcher_placeholder"></div>
|
||
<div class="version_switcher_placeholder"></div>
|
||
</li>
|
||
<li>
|
||
|
||
</li>
|
||
<li id="cpython-language-and-version">
|
||
<a href="../index.html">3.8.20 Documentation</a> »
|
||
</li>
|
||
|
||
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> »</li>
|
||
<li class="nav-item nav-item-2"><a href="crypto.html" >加密服务</a> »</li>
|
||
<li class="right">
|
||
|
||
|
||
<div class="inline-search" role="search">
|
||
<form class="inline-search" action="../search.html" method="get">
|
||
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
|
||
<input type="submit" value="转向" />
|
||
<input type="hidden" name="check_keywords" value="yes" />
|
||
<input type="hidden" name="area" value="default" />
|
||
</form>
|
||
</div>
|
||
|
|
||
</li>
|
||
|
||
</ul>
|
||
</div>
|
||
<div class="footer">
|
||
© <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
|
||
<br />
|
||
This page is licensed under the Python Software Foundation License Version 2.
|
||
<br />
|
||
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
|
||
<br />
|
||
|
||
<br />
|
||
|
||
The Python Software Foundation is a non-profit corporation.
|
||
<a href="https://www.python.org/psf/donations/">Please donate.</a>
|
||
<br />
|
||
<br />
|
||
|
||
最后更新于 12月 09, 2024.
|
||
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
|
||
<br />
|
||
|
||
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
|
||
</div>
|
||
|
||
</body>
|
||
</html> |