Python base32数据解码

Python base32 data decode

我无法理解为什么

import base64
base64.b32decode('siddh===', casefold=True);

有效但

base64.b32decode('siddha==', casefold=True);

投掷

TypeError: Incorrect padding

python base64 模块在 RFC 3548 之后。对于base32编码,

Padding at the end of the data is performed using the "=" character. Since all base 32 input is an integral number of octets, only the following cases can arise:

(1) the final quantum of encoding input is an integral multiple of 40 bits; here, the final unit of encoded output will be an integral multiple of 8 characters with no "=" padding,

(2) the final quantum of encoding input is exactly 8 bits; here, the final unit of encoded output will be two characters followed by six "=" padding characters,

(3) the final quantum of encoding input is exactly 16 bits; here, the final unit of encoded output will be four characters followed by four "=" padding characters,

(4) the final quantum of encoding input is exactly 24 bits; here, the final unit of encoded output will be five characters followed by three "=" padding characters, or

(5) the final quantum of encoding input is exactly 32 bits; here, the final unit of encoded output will be seven characters followed by one "=" padding character.

您可以看到 RFC 3548 base32 编码没有有效的情况,这会导致六个字符和两个填充字符。

五个字符总共有 25 位,因此用一个额外的位编码三个字节就足够了。六个字符会给你总共 30 位,这仍然不够四个字节。如果有七个字符,您将获得 35 位,这足以容纳四个字节。由于六个字符对于编码整数字节数并不比五个更好,因此它被排除在最终填充的 40 位输入组的标准之外,包括填充在内的八个字符。

另请注意,Base32 的字符集是大写字母 A-Z2-7=RFC 4648 (which is newer than RFC 3548)。还有一个名为 Base32-hex 的变体,它使用“0-9A-V=”,因此没有 Base32 解码器应该接受小写字母。