base64
— Base16、Base32、Base64、Base85 数据编码
¶
源代码: Lib/base64.py
This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data. It provides encoding and decoding functions for the encodings specified in RFC 4648 , which defines the Base16, Base32, and Base64 algorithms, and for the de-facto standard Ascii85 and Base85 encodings.
The RFC 4648 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. The encoding algorithm is not the same as the uuencode program.
There are two interfaces provided by this module. The modern interface supports encoding
像字节对象
到 ASCII
bytes
,和解码
像字节对象
or strings containing ASCII to
bytes
. Both base-64 alphabets defined in
RFC 4648
(normal, and URL- and filesystem-safe) are supported.
The legacy interface does not support decoding from strings, but it does provide functions for encoding and decoding to and from
文件对象
. It only supports the Base64 standard alphabet, and it adds newlines every 76 characters as per
RFC 2045
. Note that if you are looking for
RFC 2045
support you probably want to be looking at the
email
package instead.
3.3 版改变: 现在,现代接口解码函数可以接受仅 ASCII 的Unicode 字符串。
3.4 版改变: 任何 像字节对象 are now accepted by all encoding and decoding functions in this module. Ascii85/Base85 support added.
现代接口提供:
- base64. b64encode ( s , altchars = None ) ¶
-
编码 像字节对象 s 使用 Base64,并返回编码
bytes.可选 altchars 必须为 像字节对象 of length 2 which specifies an alternative alphabet for the
+and/characters. This allows an application to e.g. generate URL or filesystem safe Base64 strings. The default isNone,使用标准 Base64 字母。May assert or raise a
ValueErrorif the length of altchars is not 2. Raises aTypeErrorif altchars is not a 像字节对象 .
- base64. b64decode ( s , altchars = None , validate = False ) ¶
-
解码 Base64 编码 像字节对象 或 ASCII 字符串 s 并返回解码
bytes.可选 altchars 必须为 像字节对象 or ASCII string of length 2 which specifies the alternative alphabet used instead of the
+and/字符。A
binascii.Error异常被引发,若 s 被不正确填充。若 validate is
False(the default), characters that are neither in the normal base-64 alphabet nor the alternative alphabet are discarded prior to the padding check. If validate isTrue, these non-alphabet characters in the input result in abinascii.Error.For more information about the strict base64 check, see
binascii.a2b_base64()May assert or raise a
ValueErrorif the length of altchars is not 2.
- base64. urlsafe_b64encode ( s ) ¶
-
编码 像字节对象 s using the URL- and filesystem-safe alphabet, which substitutes
-而不是+and_而不是/in the standard Base64 alphabet, and return the encodedbytes。结果仍可包含=.
- base64. urlsafe_b64decode ( s ) ¶
-
解码 像字节对象 或 ASCII 字符串 s using the URL- and filesystem-safe alphabet, which substitutes
-而不是+and_而不是/in the standard Base64 alphabet, and return the decodedbytes.
- base64. b32decode ( s , casefold = False , map01 = None ) ¶
-
解码 Base32 编码 像字节对象 或 ASCII 字符串 s 并返回解码
bytes.可选 casefold is a flag specifying whether a lowercase alphabet is acceptable as input. For security purposes, the default is
False.RFC 4648 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 map01 when not
None, specifies which letter the digit 1 should be mapped to (when map01 不是None, the digit 0 is always mapped to the letter O). For security purposes the default isNone, so that 0 and 1 are not allowed in the input.A
binascii.Error被引发若 s is incorrectly padded or if there are non-alphabet characters present in the input.
- base64. b32hexencode ( s ) ¶
-
类似于
b32encode()but uses the Extended Hex Alphabet, as defined in RFC 4648 .3.10 版添加。