Files
FootPrint/Python/pythoncore-3.15-64/Doc/html/library/base64.html
T

851 lines
77 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="base64 — Base16, Base32, Base64, Base85 Data Encodings" />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://docs.python.org/3/library/base64.html" />
<meta property="og:site_name" content="Python documentation" />
<meta property="og:description" content="Source code: Lib/base64.py This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. This includes the encodings specifi..." />
<meta property="og:image" content="_static/og-image.png" />
<meta property="og:image:alt" content="Python documentation" />
<meta name="description" content="Source code: Lib/base64.py This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. This includes the encodings specifi..." />
<meta name="theme-color" content="#3776ab">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<title>base64 — Base16, Base32, Base64, Base85 Data Encodings &#8212; Python 3.15.0 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />
<link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=48df1187" />
<link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=0fc419ee" />
<script src="../_static/documentation_options.js?v=0fe728b1"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 3.15.0 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="binascii — Convert between binary and ASCII" href="binascii.html" />
<link rel="prev" title="mimetypes — Map filenames to MIME types" href="mimetypes.html" />
<link rel="canonical" href="https://docs.python.org/3/library/base64.html">
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
<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>
<script type="text/javascript" src="../_static/search-focus.js"></script>
<script type="text/javascript" src="../_static/themetoggle.js"></script>
<script type="text/javascript" src="../_static/rtd_switcher.js"></script>
<meta name="readthedocs-addons-api-version" content="1">
</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">
<nav class="nav-content" role="navigation">
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<span class="nav-items-wrapper">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Python logo">
</a>
<span class="version_switcher_placeholder"></span>
<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" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
</svg>
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q">
<input type="submit" value="Go">
</form>
</span>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label>
<div>
<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">base64</span></code> — Base16, Base32, Base64, Base85 Data Encodings</a><ul>
<li><a class="reference internal" href="#rfc-4648-encodings">RFC 4648 Encodings</a></li>
<li><a class="reference internal" href="#base85-encodings">Base85 Encodings</a></li>
<li><a class="reference internal" href="#legacy-interface">Legacy Interface</a></li>
<li><a class="reference internal" href="#security-considerations">Security Considerations</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="mimetypes.html"
title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mimetypes</span></code> — Map filenames to MIME types</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="binascii.html"
title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> — Convert between binary and ASCII</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "library/base64.rst");
element.href = url.toString();
});
});
</script>
<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 class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.15/Doc/library/base64.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div class="related" role="navigation" aria-label="Related">
<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="binascii.html" title="binascii — Convert between binary and ASCII"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="mimetypes.html" title="mimetypes — Map filenames to MIME types"
accesskey="P">previous</a> |</li>
<li class="root-icon"><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> &#187;</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.15.0 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python standard library</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">Internet Data Handling</a> &#187;</li>
<li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code> — Base16, Base32, Base64, Base85 Data Encodings</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="module-base64">
<span id="base64-base16-base32-base64-base85-data-encodings"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code> — Base16, Base32, Base64, Base85 Data Encodings<a class="headerlink" href="#module-base64" title="Link to this heading"></a></h1>
<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/3.15/Lib/base64.py">Lib/base64.py</a></p>
<hr class="docutils" id="index-0" />
<p>This module provides functions for encoding binary data to printable
ASCII characters and decoding such encodings back to binary data.
This includes the <a class="reference internal" href="#base64-rfc-4648"><span class="std std-ref">encodings specified in</span></a>
<span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> (Base64, Base32 and Base16), the <a class="reference internal" href="#base64-base-85"><span class="std std-ref">Base85 encoding</span></a> specified in <a class="reference external" href="https://pdfa.org/resource/iso-32000-2/">PDF 2.0</a>, and non-standard variants
of Base85 used elsewhere.</p>
<p>There are two interfaces provided by this module. The modern interface
supports encoding <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> to ASCII
<a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>, and decoding <span class="xref std std-term">bytes-like objects</span> or
strings containing ASCII to <code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code>. Both base-64 alphabets
defined in <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> (normal, and URL- and filesystem-safe) are supported.</p>
<p>The <a class="reference internal" href="#base64-legacy"><span class="std std-ref">legacy interface</span></a> does not support decoding from strings, but it does
provide functions for encoding and decoding to and from <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>. It only supports the Base64 standard alphabet, and it adds
newlines every 76 characters as per <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a>. Note that if you are looking
for <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a> support you probably want to be looking at the <a class="reference internal" href="email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages."><code class="xref py py-mod docutils literal notranslate"><span class="pre">email</span></code></a>
package instead.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.3: </span>ASCII-only Unicode strings are now accepted by the decoding functions of
the modern interface.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.4: </span>Any <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like objects</span></a> are now accepted by all
encoding and decoding functions in this module. Ascii85/Base85 support added.</p>
</div>
<section id="rfc-4648-encodings">
<span id="base64-rfc-4648"></span><h2>RFC 4648 Encodings<a class="headerlink" href="#rfc-4648-encodings" title="Link to this heading"></a></h2>
<p>The <span class="target" id="index-5"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> encodings are suitable for encoding binary data so that it can be
safely sent by email, used as parts of URLs, or included as part of an HTTP
POST request.</p>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b64encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b64encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">altchars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64encode" title="Link to this definition"></a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Base64 and return the encoded
<a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>Optional <em>altchars</em> must be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> of length 2 which
specifies an alternative alphabet for the <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code> characters.
This allows an application to e.g. generate URL or filesystem safe Base64
strings. The default is <code class="docutils literal notranslate"><span class="pre">None</span></code>, for which the standard Base64 alphabet is used.</p>
<p>If <em>padded</em> is true (default), pad the encoded data with the ‘=’
character to a size multiple of 4.
If <em>padded</em> is false, do not add the pad characters.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not insert any newlines.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>padded</em> and <em>wrapcol</em> parameters.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b64decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b64decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">altchars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">validate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b64decode" title="Link to this definition"></a></dt>
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b64decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">altchars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">validate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span></dt>
<dd><p>Decode the Base64 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string
<em>s</em> and return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>Optional <em>altchars</em> must be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string
of length 2 which specifies the alternative alphabet used instead of the
<code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code> characters.</p>
<p>If <em>padded</em> is true, the last group of 4 base 64 alphabet characters must
be padded with the ‘=’ character.
If <em>padded</em> is false, padding is neither required nor recognized:
the ‘=’ character is not treated as padding but as a non-alphabet
character, which means it is silently discarded when <em>validate</em> is false,
or causes an <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a> when <em>validate</em> is true unless
b’=’ is included in <em>ignorechars</em>.</p>
<p>A <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a> exception is raised
if <em>s</em> is incorrectly padded.</p>
<p>If <em>ignorechars</em> is specified, it should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>
containing characters to ignore from the input when <em>validate</em> is true.
If <em>ignorechars</em> contains the pad character <code class="docutils literal notranslate"><span class="pre">'='</span></code>, the pad characters
presented before the end of the encoded data and the excess pad characters
will be ignored.
The default value of <em>validate</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>ignorechars</em> is specified,
<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<p>If <em>validate</em> is false, characters that are neither
in the normal base-64 alphabet nor (if <em>ignorechars</em> is not specified)
the alternative alphabet are
discarded prior to the padding check, but the <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code> characters
keep their meaning if they are not in <em>altchars</em> (they will be discarded
in future Python versions).</p>
<p>If <em>validate</em> is true, these non-alphabet characters in the input
result in a <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a>.</p>
<p>If <em>canonical</em> is true, non-zero padding bits are rejected.
See <a class="reference internal" href="binascii.html#binascii.a2b_base64" title="binascii.a2b_base64"><code class="xref py py-func docutils literal notranslate"><span class="pre">binascii.a2b_base64()</span></code></a> for details.</p>
<p>For more information about the strict base64 check, see <a class="reference internal" href="binascii.html#binascii.a2b_base64" title="binascii.a2b_base64"><code class="xref py py-func docutils literal notranslate"><span class="pre">binascii.a2b_base64()</span></code></a></p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>canonical</em>, <em>ignorechars</em>, and <em>padded</em> parameters.</p>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 3.15: </span>Accepting the <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code> characters with an alternative alphabet
is now deprecated.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.standard_b64encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">standard_b64encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.standard_b64encode" title="Link to this definition"></a></dt>
<dd><p>Encode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using the standard Base64 alphabet
and return the encoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.standard_b64decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">standard_b64decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.standard_b64decode" title="Link to this definition"></a></dt>
<dd><p>Decode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> using the standard
Base64 alphabet and return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.urlsafe_b64encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">urlsafe_b64encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.urlsafe_b64encode" title="Link to this definition"></a></dt>
<dd><p>Encode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using the
URL- and filesystem-safe alphabet, which
substitutes <code class="docutils literal notranslate"><span class="pre">-</span></code> instead of <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">_</span></code> instead of <code class="docutils literal notranslate"><span class="pre">/</span></code> in the
standard Base64 alphabet, and return the encoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>. The result
can still contain <code class="docutils literal notranslate"><span class="pre">=</span></code> if <em>padded</em> is true (default).</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>padded</em> parameter.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.urlsafe_b64decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">urlsafe_b64decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.urlsafe_b64decode" title="Link to this definition"></a></dt>
<dd><p>Decode <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em>
using the URL- and filesystem-safe
alphabet, which substitutes <code class="docutils literal notranslate"><span class="pre">-</span></code> instead of <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">_</span></code> instead of
<code class="docutils literal notranslate"><span class="pre">/</span></code> in the standard Base64 alphabet, and return the decoded
<a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>padded</em> parameter.
Padding of input is no longer required by default.</p>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 3.15: </span>Accepting the <code class="docutils literal notranslate"><span class="pre">+</span></code> and <code class="docutils literal notranslate"><span class="pre">/</span></code> characters is now deprecated.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b32encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b32encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32encode" title="Link to this definition"></a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Base32 and return the
encoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>If <em>padded</em> is true (default), pad the encoded data with the ‘=’
character to a size multiple of 8.
If <em>padded</em> is false, do not add the pad characters.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not add any newlines.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>padded</em> and <em>wrapcol</em> parameters.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b32decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b32decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">casefold</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">map01</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32decode" title="Link to this definition"></a></dt>
<dd><p>Decode the Base32 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> and
return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>Optional <em>casefold</em> is a flag specifying
whether a lowercase alphabet is acceptable as input. For security purposes,
the default is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p><span class="target" id="index-6"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> allows for optional mapping of the digit 0 (zero) to the letter O
(oh), and for optional mapping of the digit 1 (one) to either the letter I (eye)
or letter L (el). The optional argument <em>map01</em> when not <code class="docutils literal notranslate"><span class="pre">None</span></code>, specifies
which letter the digit 1 should be mapped to (when <em>map01</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>, the
digit 0 is always mapped to the letter O). For security purposes the default is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, so that 0 and 1 are not allowed in the input.</p>
<p>If <em>padded</em> is true, the last group of 8 base 32 alphabet characters must
be padded with the ‘=’ character.
If <em>padded</em> is false, padding is neither required nor recognized:
the ‘=’ character is not treated as padding but as a non-alphabet
character, which means it raises an <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Error</span></code></a> unless
b’=’ is included in <em>ignorechars</em>.</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.</p>
<p>If <em>canonical</em> is true, non-zero padding bits are rejected.
See <a class="reference internal" href="binascii.html#binascii.a2b_base32" title="binascii.a2b_base32"><code class="xref py py-func docutils literal notranslate"><span class="pre">binascii.a2b_base32()</span></code></a> for details.</p>
<p>A <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a> is raised if <em>s</em> is
incorrectly padded or if there are non-alphabet characters present in the
input.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>canonical</em>, <em>ignorechars</em>, and <em>padded</em> parameters.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b32hexencode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b32hexencode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32hexencode" title="Link to this definition"></a></dt>
<dd><p>Similar to <a class="reference internal" href="#base64.b32encode" title="base64.b32encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">b32encode()</span></code></a> but uses the Extended Hex Alphabet, as defined in
<span class="target" id="index-7"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.10.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>padded</em> and <em>wrapcol</em> parameters.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b32hexdecode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b32hexdecode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">casefold</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">padded</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b32hexdecode" title="Link to this definition"></a></dt>
<dd><p>Similar to <a class="reference internal" href="#base64.b32decode" title="base64.b32decode"><code class="xref py py-func docutils literal notranslate"><span class="pre">b32decode()</span></code></a> but uses the Extended Hex Alphabet, as defined in
<span class="target" id="index-8"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a>.</p>
<p>This version does not allow the digit 0 (zero) to the letter O (oh) and digit
1 (one) to either the letter I (eye) or letter L (el) mappings, all these
characters are included in the Extended Hex Alphabet and are not
interchangeable.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.10.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>canonical</em>, <em>ignorechars</em>, and <em>padded</em> parameters.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b16encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b16encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b16encode" title="Link to this definition"></a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Base16 and return the
encoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not add any newlines.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>wrapcol</em> parameter.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b16decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b16decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">casefold</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b16decode" title="Link to this definition"></a></dt>
<dd><p>Decode the Base16 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> and
return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>Optional <em>casefold</em> is a flag specifying whether a
lowercase alphabet is acceptable as input. For security purposes, the default
is <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.</p>
<p>A <a class="reference internal" href="binascii.html#binascii.Error" title="binascii.Error"><code class="xref py py-exc docutils literal notranslate"><span class="pre">binascii.Error</span></code></a> is raised if <em>s</em> is
incorrectly padded or if there are non-alphabet characters present in the
input.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>ignorechars</em> parameter.</p>
</div>
</dd></dl>
</section>
<section id="base85-encodings">
<span id="base64-base-85"></span><h2>Base85 Encodings<a class="headerlink" href="#base85-encodings" title="Link to this heading"></a></h2>
<p>Base85 encoding is a family of algorithms which represent four bytes
using five ASCII characters. Originally implemented in the Unix
<code class="docutils literal notranslate"><span class="pre">btoa(1)</span></code> utility, a version of it was later adopted by Adobe in the
PostScript language and is standardized in PDF 2.0 (ISO 32000-2).
This version, in both its <code class="docutils literal notranslate"><span class="pre">btoa</span></code> and PDF variants, is implemented by
<a class="reference internal" href="#base64.a85encode" title="base64.a85encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">a85encode()</span></code></a>.</p>
<p>A separate version, using a different output character set, was
defined as an April Fool’s joke in <span class="target" id="index-9"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1924.html"><strong>RFC 1924</strong></a> but is now used by Git
and other software. This version is implemented by <a class="reference internal" href="#base64.b85encode" title="base64.b85encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">b85encode()</span></code></a>.</p>
<p>Finally, a third version, using yet another output character set
designed for safe inclusion in programming language strings, is
defined by ZeroMQ and implemented here by <a class="reference internal" href="#base64.z85encode" title="base64.z85encode"><code class="xref py py-func docutils literal notranslate"><span class="pre">z85encode()</span></code></a>.</p>
<p>The functions present in this module differ in how they handle the following:</p>
<ul class="simple">
<li><p>Whether to include and expect enclosing <code class="docutils literal notranslate"><span class="pre">&lt;~</span></code> and <code class="docutils literal notranslate"><span class="pre">~&gt;</span></code> markers.</p></li>
<li><p>Whether to fold the input into multiple lines.</p></li>
<li><p>The set of ASCII characters used for encoding.</p></li>
<li><p>Compact encodings of sequences of spaces and null bytes.</p></li>
<li><p>The encoding of zero-padding bytes applied to the input.</p></li>
</ul>
<p>Refer to the documentation of the individual functions for more information.</p>
<dl class="py function">
<dt class="sig sig-object py" id="base64.a85encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">a85encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">foldspaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pad</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">adobe</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.a85encode" title="Link to this definition"></a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>b</em> using Ascii85 and return the
encoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p><em>foldspaces</em> is an optional flag that uses the special short sequence ‘y’
instead of 4 consecutive spaces (ASCII 0x20) as supported by ‘btoa’. This
feature is not supported by the standard encoding used in PDF.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not insert any newlines.</p>
<p><em>pad</em> controls whether zero-padding applied to the end of the input
is fully retained in the output encoding, as done by <code class="docutils literal notranslate"><span class="pre">btoa</span></code>,
producing an exact multiple of 5 bytes of output. This is not part
of the standard encoding used in PDF, as it does not preserve the
length of the data.</p>
<p><em>adobe</em> controls whether the encoded byte sequence is framed with
<code class="docutils literal notranslate"><span class="pre">&lt;~</span></code> and <code class="docutils literal notranslate"><span class="pre">~&gt;</span></code>, as in a PostScript base-85 string literal. Note
that while ASCII85Decode streams in PDF documents <em>must</em> be
terminated with <code class="docutils literal notranslate"><span class="pre">~&gt;</span></code>, they <em>must not</em> use a leading <code class="docutils literal notranslate"><span class="pre">&lt;~</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.a85decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">a85decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">foldspaces</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">adobe</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b'</span> <span class="pre">\t\n\r\x0b'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.a85decode" title="Link to this definition"></a></dt>
<dd><p>Decode the Ascii85 encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>b</em> and
return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p><em>foldspaces</em> is a flag that specifies whether the ‘y’ short sequence
should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
This feature is not supported by the standard Ascii85 encoding used in
PDF and PostScript.</p>
<p><em>adobe</em> controls whether the <code class="docutils literal notranslate"><span class="pre">&lt;~</span></code> and <code class="docutils literal notranslate"><span class="pre">~&gt;</span></code> markers are
present. While the leading <code class="docutils literal notranslate"><span class="pre">&lt;~</span></code> is not required, the input must
end with <code class="docutils literal notranslate"><span class="pre">~&gt;</span></code>, or a <a class="reference internal" href="../builtins/exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised.</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.
This should only contain whitespace characters, and by
default contains all whitespace characters in ASCII.</p>
<p>If <em>canonical</em> is true, non-canonical encodings are rejected.
See <a class="reference internal" href="binascii.html#binascii.a2b_ascii85" title="binascii.a2b_ascii85"><code class="xref py py-func docutils literal notranslate"><span class="pre">binascii.a2b_ascii85()</span></code></a> for details.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>canonical</em> parameter.
Single-character final groups are now always rejected as encoding
violations.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b85encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b85encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pad</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b85encode" title="Link to this definition"></a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>b</em> using base85 (as used in e.g.
git-style binary diffs) and return the encoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>The input is padded with <code class="docutils literal notranslate"><span class="pre">b'\0'</span></code> so its length is a multiple of 4
bytes before encoding. If <em>pad</em> is true, all the resulting
characters are retained in the output, which will always be a
multiple of 5 bytes, and thus the length of the data may not be
preserved on decoding.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not add any newlines.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>wrapcol</em> parameter.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.b85decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">b85decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.b85decode" title="Link to this definition"></a></dt>
<dd><p>Decode the base85-encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>b</em> and
return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.</p>
<p>If <em>canonical</em> is true, non-canonical encodings are rejected.
See <a class="reference internal" href="binascii.html#binascii.a2b_base85" title="binascii.a2b_base85"><code class="xref py py-func docutils literal notranslate"><span class="pre">binascii.a2b_base85()</span></code></a> for details.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.4.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>canonical</em> and <em>ignorechars</em> parameters.
Single-character final groups are now always rejected as encoding
violations.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.z85encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">z85encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pad</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">wrapcol</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.z85encode" title="Link to this definition"></a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em> using Z85 (as used in ZeroMQ)
and return the encoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p>The input is padded with <code class="docutils literal notranslate"><span class="pre">b'\0'</span></code> so its length is a multiple of 4
bytes before encoding. If <em>pad</em> is true, all the resulting
characters are retained in the output, which will always be a
multiple of 5 bytes, as required by the ZeroMQ standard.</p>
<p>If <em>wrapcol</em> is non-zero, insert a newline (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) character
after at most every <em>wrapcol</em> characters.
If <em>wrapcol</em> is zero (default), do not add any newlines.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.13.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>The <em>pad</em> parameter was added.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>wrapcol</em> parameter.</p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.z85decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">z85decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">ignorechars</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">b''</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">canonical</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.z85decode" title="Link to this definition"></a></dt>
<dd><p>Decode the Z85-encoded <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> or ASCII string <em>s</em> and
return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<p><em>ignorechars</em> should be a <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> containing characters
to ignore from the input.</p>
<p>If <em>canonical</em> is true, non-canonical encodings are rejected.
See <a class="reference internal" href="binascii.html#binascii.a2b_base85" title="binascii.a2b_base85"><code class="xref py py-func docutils literal notranslate"><span class="pre">binascii.a2b_base85()</span></code></a> for details.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.13.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.15: </span>Added the <em>canonical</em> and <em>ignorechars</em> parameters.
Single-character final groups are now always rejected as encoding
violations.</p>
</div>
</dd></dl>
</section>
<section id="legacy-interface">
<span id="base64-legacy"></span><h2>Legacy Interface<a class="headerlink" href="#legacy-interface" title="Link to this heading"></a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="base64.decode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">output</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decode" title="Link to this definition"></a></dt>
<dd><p>Decode the contents of the binary <em>input</em> file and write the resulting binary
data to the <em>output</em> file. <em>input</em> and <em>output</em> must be <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file objects</span></a>. <em>input</em> will be read until <code class="docutils literal notranslate"><span class="pre">input.readline()</span></code> returns an
empty bytes object.</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.decodebytes">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">decodebytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.decodebytes" title="Link to this definition"></a></dt>
<dd><p>Decode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>, which must contain one or more
lines of base64 encoded data, and return the decoded <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a>.</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.1.</span></p>
</div>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.encode">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">input</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">output</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encode" title="Link to this definition"></a></dt>
<dd><p>Encode the contents of the binary <em>input</em> file and write the resulting base64
encoded data to the <em>output</em> file. <em>input</em> and <em>output</em> must be <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file
objects</span></a>. <em>input</em> will be read until <code class="docutils literal notranslate"><span class="pre">input.read()</span></code> returns
an empty bytes object. <code class="xref py py-func docutils literal notranslate"><span class="pre">encode()</span></code> inserts a newline character (<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>)
after every 76 bytes of the output, as well as ensuring that the output
always ends with a newline, as per <span class="target" id="index-10"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a> (MIME).</p>
</dd></dl>
<dl class="py function">
<dt class="sig sig-object py" id="base64.encodebytes">
<span class="sig-prename descclassname"><span class="pre">base64.</span></span><span class="sig-name descname"><span class="pre">encodebytes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#base64.encodebytes" title="Link to this definition"></a></dt>
<dd><p>Encode the <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> <em>s</em>, which can contain arbitrary binary
data, and return <a class="reference internal" href="../builtins/stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> containing the base64-encoded data, with newlines
(<code class="docutils literal notranslate"><span class="pre">b'\n'</span></code>) inserted after every 76 bytes of output, and ensuring that
there is a trailing newline, as per <span class="target" id="index-11"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc2045.html"><strong>RFC 2045</strong></a> (MIME).</p>
<div class="versionadded">
<p><span class="versionmodified added">Added in version 3.1.</span></p>
</div>
</dd></dl>
<p>An example usage of the module:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span><span class="w"> </span><span class="nn">base64</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">encoded</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64encode</span><span class="p">(</span><span class="sa">b</span><span class="s1">'data to be encoded'</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">encoded</span>
<span class="go">b'ZGF0YSB0byBiZSBlbmNvZGVk'</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b64decode</span><span class="p">(</span><span class="n">encoded</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">b'data to be encoded'</span>
</pre></div>
</div>
</section>
<section id="security-considerations">
<span id="base64-security"></span><h2>Security Considerations<a class="headerlink" href="#security-considerations" title="Link to this heading"></a></h2>
<p>A new security considerations section was added to <span class="target" id="index-12"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4648.html"><strong>RFC 4648</strong></a> (section 12); it’s
recommended to review the security section for any code deployed to production.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt>Module <a class="reference internal" href="binascii.html#module-binascii" title="binascii: Tools for converting between binary and various ASCII-encoded binary representations."><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code></a></dt><dd><p>Support module containing ASCII-to-binary and binary-to-ASCII conversions.</p>
</dd>
<dt><span class="target" id="index-13"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc1521.html"><strong>RFC 1521</strong></a> - MIME (Multipurpose Internet Mail Extensions) Part One: Mechanisms for Specifying and Describing the Format of Internet Message Bodies</dt><dd><p>Section 5.2, “Base64 Content-Transfer-Encoding,” provides the definition of the
base64 encoding.</p>
</dd>
<dt><a class="reference external" href="https://pdfa.org/resource/iso-32000-2/">ISO 32000-2 Portable document format - Part 2: PDF 2.0</a></dt><dd><p>Section 7.4.3, “ASCII85Decode Filter,” provides the definition
of the Ascii85 encoding used in PDF and PostScript, including
the output character set and the details of data length preservation
using zero-padding and partial output groups.</p>
</dd>
<dt><a class="reference external" href="https://rfc.zeromq.org/spec/32/">ZeroMQ RFC 32/Z85</a></dt><dd><p>The “Formal Specification” section provides the character set used in Z85.</p>
</dd>
</dl>
</div>
</section>
</section>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="Main">
<div class="sphinxsidebarwrapper">
<div>
<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">base64</span></code> — Base16, Base32, Base64, Base85 Data Encodings</a><ul>
<li><a class="reference internal" href="#rfc-4648-encodings">RFC 4648 Encodings</a></li>
<li><a class="reference internal" href="#base85-encodings">Base85 Encodings</a></li>
<li><a class="reference internal" href="#legacy-interface">Legacy Interface</a></li>
<li><a class="reference internal" href="#security-considerations">Security Considerations</a></li>
</ul>
</li>
</ul>
</div>
<div>
<h4>Previous topic</h4>
<p class="topless"><a href="mimetypes.html"
title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mimetypes</span></code> — Map filenames to MIME types</a></p>
</div>
<div>
<h4>Next topic</h4>
<p class="topless"><a href="binascii.html"
title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">binascii</span></code> — Convert between binary and ASCII</a></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const title = document.querySelector('meta[property="og:title"]').content;
const elements = document.querySelectorAll('.improvepage');
const pageurl = window.location.href.split('?')[0];
elements.forEach(element => {
const url = new URL(element.href.split('?')[0].replace("-nojs", ""));
url.searchParams.set('pagetitle', title);
url.searchParams.set('pageurl', pageurl);
url.searchParams.set('pagesource', "library/base64.rst");
element.href = url.toString();
});
});
</script>
<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 class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.15/Doc/library/base64.rst?plain=1"
rel="nofollow">Show source
</a>
</li>
</ul>
</div>
</div>
<div id="sidebarbutton" title="Collapse sidebar">
<span>«</span>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="Related">
<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="binascii.html" title="binascii — Convert between binary and ASCII"
>next</a> |</li>
<li class="right" >
<a href="mimetypes.html" title="mimetypes — Map filenames to MIME types"
>previous</a> |</li>
<li class="root-icon"><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> &#187;</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.15.0 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python standard library</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
<li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">base64</span></code> — Base16, Base32, Base64, Base85 Data Encodings</a></li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">
<input type="submit" value="Go">
</form>
</div>
|
</li>
<li class="right">
<label class="theme-selector-label">
Theme
<select class="theme-selector" oninput="activateTheme(this.value)">
<option value="auto" selected>Auto</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</label> |</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">Copyright</a> 2001 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>
See <a href="/license.html">History and License</a> for more information.<br>
<br>
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br>
<br>
Last updated on Oct 09, 2026 (11:35 UTC).
<a href="/bugs.html">Found a bug</a>?
<br>
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.
</div>
</body>
</html>