#! /usr/bin/env python3

 """Base16, Base32, Base64 (RFC 3548), Base85 and Ascii85 data encodings"""

 # Modified 04-Oct-1995 by Jack Jansen to use binascii module
# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
# Modified 22-May-2007 by Guido van Rossum to use bytes everywhere import re
import struct
import binascii __all__ = [
# Legacy interface exports traditional RFC 1521 Base64 encodings
'encode', 'decode', 'encodebytes', 'decodebytes',
# Generalized interface for other encodings
'b64encode', 'b64decode', 'b32encode', 'b32decode',
'b16encode', 'b16decode',
# Base85 and Ascii85 encodings
'b85encode', 'b85decode', 'a85encode', 'a85decode',
# Standard Base64 encoding
'standard_b64encode', 'standard_b64decode',
# Some common Base64 alternatives. As referenced by RFC 3458, see thread
# starting at:
#
# http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
'urlsafe_b64encode', 'urlsafe_b64decode',
] bytes_types = (bytes, bytearray) # Types acceptable as binary data def _bytes_from_decode_data(s):
'''
返回字节包,否则抛出异常
'''
if isinstance(s, str):
try:
return s.encode('ascii')
except UnicodeEncodeError:
raise ValueError('string argument should contain only ASCII characters')
if isinstance(s, bytes_types): #s是否是bytes或者bytearray中的某种类型。isinstance(s,(type1,type2,...)) return s
try:
return memoryview(s).tobytes() #memoryview()返回内存指针,s必须是bytes或bytearray类型
#memoryview(b'abc') ==> <memory at 0x0000000003723368>
#memoryview(b'abc').tobytes() ==> b'abc'
#memoryview(b'abc')[0] ==> b'a' except TypeError:
raise TypeError("argument should be a bytes-like object or ASCII "
"string, not %r" % s.__class__.__name__) from None # Base64 encoding/decoding uses binascii def b64encode(s, altchars=None):
"""Encode a byte string using Base64. s is the byte string to encode. Optional altchars must be a byte
string 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 encoded byte string is returned.
"""
# Strip off the trailing newline
encoded = binascii.b2a_base64(s)[:-1] #将二进制字节包s转换成经过base64编码的ascii字节包 if altchars is not None: #如果有altchars选项,且altchars长度为2,则用其替换'+'和'/' assert len(altchars) == 2, repr(altchars)
return encoded.translate(bytes.maketrans(b'+/', altchars)) #在已经是base64编码的encoded中替换b'+/'为altchars对应的字符
#bytes.maketrans(frm,to)返回一个从frm到to的映射表
#encoded.translate()采用maketrans返回的映射表将encoded转换 return encoded def b64decode(s, altchars=None, validate=False):
"""Decode a Base64 encoded byte string. s is the byte string to decode. Optional altchars must be a
string of length 2 which specifies the alternative alphabet used
instead of the '+' and '/' characters. The decoded string is returned. A binascii.Error is raised if s is
incorrectly padded. If validate is False (the default), non-base64-alphabet characters are
discarded prior to the padding check. If validate is True,
non-base64-alphabet characters in the input result in a binascii.Error.
"""
s = _bytes_from_decode_data(s)
if altchars is not None:
altchars = _bytes_from_decode_data(altchars)
assert len(altchars) == 2, repr(altchars)
s = s.translate(bytes.maketrans(altchars, b'+/'))
if validate and not re.match(b'^[A-Za-z0-9+/]*={0,2}$', s): #base64编码最后补位的等号个数只能是0,1,2 raise binascii.Error('Non-base64 digit found')
return binascii.a2b_base64(s) #将ascii形式的base64解码 def standard_b64encode(s):
"""Encode a byte string using the standard Base64 alphabet. s is the byte string to encode. The encoded byte string is returned.
"""
return b64encode(s) def standard_b64decode(s):
"""Decode a byte string encoded with the standard Base64 alphabet. s is the byte string to decode. The decoded byte string is
returned. binascii.Error is raised if the input is incorrectly
padded or if there are non-alphabet characters present in the
input.
"""
return b64decode(s) _urlsafe_encode_translation = bytes.maketrans(b'+/', b'-_')
_urlsafe_decode_translation = bytes.maketrans(b'-_', b'+/') #url的base64编码中将'+/'替换成了'-_' def urlsafe_b64encode(s):
"""Encode a byte string using a url-safe Base64 alphabet. s is the byte string to encode. The encoded byte string is
returned. The alphabet uses '-' instead of '+' and '_' instead of
'/'.
"""
return b64encode(s).translate(_urlsafe_encode_translation) def urlsafe_b64decode(s):
"""Decode a byte string encoded with the standard Base64 alphabet. s is the byte string to decode. The decoded byte string is
returned. binascii.Error is raised if the input is incorrectly
padded or if there are non-alphabet characters present in the
input. The alphabet uses '-' instead of '+' and '_' instead of '/'.
"""
s = _bytes_from_decode_data(s)
s = s.translate(_urlsafe_decode_translation)
return b64decode(s) # Base32 encoding/decoding must be done in Python
_b32alphabet = b'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567' #base32用的32个字符是'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
_b32tab2 = None
_b32rev = None def b32encode(s):
"""Encode a byte string using Base32. s is the byte string to encode. The encoded byte string is returned.
"""
global _b32tab2
# Delay the initialization of the table to not waste memory
# if the function is never called
if _b32tab2 is None:
b32tab = [bytes((i,)) for i in _b32alphabet] #bytes(iterable_of_ints) -> bytes
#(i,)是一个可迭代的数字元组,如(61,) (62,)……也可以是bytes([i,])
#bytes((66,)) ==> b'B'
#bytes((66,67)) ==> b'BC'
#[i for i in _b32alphabet]返回一个int类型的列表 _b32tab2 = [a + b for a in b32tab for b in b32tab] #_b32tab2 = [b'AA',b'AB',b'AC',.......,b'76',b'77']
#len(_b32tab2) ==> 1024,每10bit截取一次,每次都对应着1024中的一组 b32tab = None if not isinstance(s, bytes_types):
s = memoryview(s).tobytes()
leftover = len(s) % 5
# Pad the last quantum with zero bits if necessary
if leftover:
s = s + bytes(5 - leftover) # Don't use += ! #填充5-leftover个字节的0 encoded = bytearray()
from_bytes = int.from_bytes
b32tab2 = _b32tab2
for i in range(0, len(s), 5):
c = from_bytes(s[i: i + 5], 'big') #int.from_bytes(b'\x00\x10', byteorder='big') ==> 16
#int.from_bytes(b'\x00\x10', byteorder='little') ==> 4096 encoded += (b32tab2[c >> 30] + # bits 1 - 10
b32tab2[(c >> 20) & 0x3ff] + # bits 11 - 20
b32tab2[(c >> 10) & 0x3ff] + # bits 21 - 30
b32tab2[c & 0x3ff] # bits 31 - 40
)
# Adjust for any leftover partial quanta
if leftover == 1:
encoded[-6:] = b'======'
elif leftover == 2:
encoded[-4:] = b'===='
elif leftover == 3:
encoded[-3:] = b'==='
elif leftover == 4:
encoded[-1:] = b'='
return bytes(encoded) '''
以b'abc'编码为例,解释编码后补齐的=个数:
a b c x x 8 + 2 6 +4 4 +6 2 +8
-------- ----- ------ -------
AB CD 4 +1 5 + 2 + 8
-----
E ===
base32,5bit一个编码字符
''' def b32decode(s, casefold=False, map01=None):
"""Decode a Base32 encoded byte string. s is the byte string to decode. Optional casefold is a flag
specifying whether a lowercase alphabet is acceptable as input.
For security purposes, the default is False. RFC 3548 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 is not None, the digit 0 is always mapped to
the letter O). For security purposes the default is None, so that
0 and 1 are not allowed in the input. The decoded byte string is returned. binascii.Error is raised if
the input is incorrectly padded or if there are non-alphabet
characters present in the input.
"""
global _b32rev
# Delay the initialization of the table to not waste memory
# if the function is never called
if _b32rev is None:
_b32rev = {v: k for k, v in enumerate(_b32alphabet)} #{65: 0, 66: 1, 67: 2, 68: 3, 69: 4, 70: 5, 71: 6, 72: 7, 73: 8,
#74: 9, 75: 10, 76: 11, 77: 12, 78: 13, 79: 14, 80: 15, 81: 16,
#82: 17, 83: 18, 84: 19, 85: 20, 86: 21, 87: 22, 88: 23, 89: 24,
#90: 25, 50: 26, 51: 27, 52: 28, 53: 29, 54: 30, 55: 31} s = _bytes_from_decode_data(s)
if len(s) % 8:
raise binascii.Error('Incorrect padding')
# Handle section 2.4 zero and one mapping. The flag map01 will be either
# False, or the character to map the digit 1 (one) to. It should be
# either L (el) or I (eye).
if map01 is not None:
map01 = _bytes_from_decode_data(map01)
assert len(map01) == 1, repr(map01)
s = s.translate(bytes.maketrans(b'', b'O' + map01))
if casefold:
s = s.upper()
# Strip off pad characters from the right. We need to count the pad
# characters because this will tell us how many null bytes to remove from
# the end of the decoded string.
l = len(s)
s = s.rstrip(b'=')
padchars = l - len(s) #填充了几个等号
# Now decode the full quanta
decoded = bytearray()
b32rev = _b32rev
for i in range(0, len(s), 8):
quanta = s[i: i + 8]
acc = 0
try:
for c in quanta:
acc = (acc << 5) + b32rev[c]
except KeyError:
raise binascii.Error('Non-base32 digit found') from None
decoded += acc.to_bytes(5, 'big')
# Process the last, partial quanta
if padchars:
acc <<= 5 * padchars #每一个padchars相当于占了5bit
last = acc.to_bytes(5, 'big') #计算包含补充等号的后五位
if padchars == 1: #填充了一个字符位
decoded[-5:] = last[:-1]
elif padchars == 3: #填充了两个字符位
decoded[-5:] = last[:-2]
elif padchars == 4: #填充了三个字符位
decoded[-5:] = last[:-3]
elif padchars == 6: #填充了四个字符位
decoded[-5:] = last[:-4]
else:
raise binascii.Error('Incorrect padding')
return bytes(decoded)

python3 base64模块代码分析的更多相关文章

  1. Python代码分析工具之dis模块

    转自:http://hi.baidu.com/tinyweb/item/923d012e8146d00872863ec0  ,格式调整过. 代码分析不是一个新的话题,代码分析重要性的判断比较主观,不同 ...

  2. MDU某产品OMCI模块代码质量现状分析

    说明 本文参考MDU系列某产品OMCI模块现有代码,提取若干实例以说明目前的代码质量,亦可作为甄别不良代码的参考. 本文旨在就事论事,而非否定前人(没有前人的努力也难有后人的进步).希望以史为鉴,不破 ...

  3. insmod模块加载过程代码分析1【转】

    转自:http://blog.chinaunix.net/uid-27717694-id-3966290.html 一.概述模块是作为ELF对象文件存放在文件系统中的,并通过执行insmod程序链接到 ...

  4. Python之数据加密与解密及相关操作(hashlib模块、hmac模块、random模块、base64模块、pycrypto模块)

    本文内容 数据加密概述 Python中实现数据加密的模块简介 hashlib与hmac模块介绍 random与secrets模块介绍 base64模块介绍 pycrypto模块介绍 总结 参考文档 提 ...

  5. angular代码分析之异常日志设计

    angular代码分析之异常日志设计 错误异常是面向对象开发中的记录提示程序执行问题的一种重要机制,在程序执行发生问题的条件下,异常会在中断程序执行,同时会沿着代码的执行路径一步一步的向上抛出异常,最 ...

  6. 彻底告别加解密模块代码拷贝-JCE核心Cpiher详解

    前提 javax.crypto.Cipher,翻译为密码,其实叫做密码器更加合适.Cipher是JCA(Java Cryptographic Extension,Java加密扩展)的核心,提供基于多种 ...

  7. python-网络安全编程第七天(base64模块)

    前言 睡不着,那就起来学习其实base64模块很早之前用过今天做爬虫的时候有个URL需要用它来编码一下 所以百度又学了一下遇到最大的问题就是python3和python2区别问题 python3的这个 ...

  8. Android代码分析工具lint学习

    1 lint简介 1.1 概述 lint是随Android SDK自带的一个静态代码分析工具.它用来对Android工程的源文件进行检查,找出在正确性.安全.性能.可使用性.可访问性及国际化等方面可能 ...

  9. STM32启动代码分析 IAR 比较好

    stm32启动代码分析 (2012-06-12 09:43:31) 转载▼     最近开始使用ST的stm32w108芯片(也是一款zigbee芯片).开始看他的启动代码看的晕晕呼呼呼的. 还好在c ...

随机推荐

  1. 【2018 CCPC网络赛】1003 - 费马小定理

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6440 这题主要是理解题意: 题意:定义一个加法和乘法,使得 (m+n)p = mp+np; 其中给定 ...

  2. PowerShell(PHPStorm terminal with PowerShell)运行git log中文乱码

    解决方案: 1)以管理员身份运行PowerShell 2)新建一个针对PowerShell的Pofile文件 New-Item -Path $Profile -ItemType file -Force ...

  3. linux php安装ODBC扩展

    进入php源码安装目录的ext/pdo_odbc $ sudo /data/apps/php/bin/phpize $ ./configure --with-php-config=/data/apps ...

  4. Linux中一些约定俗成的文件扩展名

    注:Linux中的所有内容均以文件的形式保存,但不依靠扩展名区分文件类型(根据权限区分),约定俗成的文件扩展名是为了方便管理员对文件进行区分 压缩包:“*.gz”.“*.bz2”.“*.tar.bz2 ...

  5. transformer模型解读

    最近在关注谷歌发布关于BERT模型,它是以Transformer的双向编码器表示.顺便回顾了<Attention is all you need>这篇文章主要讲解Transformer编码 ...

  6. Python数据分析 Pandas模块 基础数据结构与简介(一)

    pandas 入门 简介 pandas 组成 = 数据面板 + 数据分析工具 poandas 把数组分为3类 一维矩阵:Series 把ndarray强大在可以存储任意数据类型可以专门处理时间数据 二 ...

  7. 转载 vue的基础使用

    转载https://www.cnblogs.com/majj/p/9957597.html#top vue的介绍 前端框架和库的区别 nodejs的简单使用 vue的起步 指令系统 组件的使用 过滤器 ...

  8. 分享下找到的Github上大神的EmpireofCode进攻策略:反正我是用了没反应,改了代码后单位不进攻,蠢站在那里,我自己的策略调调能打败不少人,这个日后慢慢研究吧,Github上暂时找到的唯一策略

    from queue import PriorityQueue from enum import Enum from battle import commander from battle impor ...

  9. Codeforces 5D Follow Traffic Rules

    [题意概述] 某个物体要从A途经B到达C,在通过B的时候速度不能超过vd.  它的加速度为a,最大速度为vm:AB之间距离为d,AC之间距离为L: 问物体最少花多少时间到达C. [题解] 分情况讨论. ...

  10. POJ 3122 pie (二分法)

    Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have ...