Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../lib64/python2....
File: base64.py
#! /usr/bin/python2.7
[0] Fix | Delete
[1] Fix | Delete
"""RFC 3548: Base16, Base32, Base64 Data Encodings"""
[2] Fix | Delete
[3] Fix | Delete
# Modified 04-Oct-1995 by Jack Jansen to use binascii module
[4] Fix | Delete
# Modified 30-Dec-2003 by Barry Warsaw to add full RFC 3548 support
[5] Fix | Delete
[6] Fix | Delete
import re
[7] Fix | Delete
import struct
[8] Fix | Delete
import string
[9] Fix | Delete
import binascii
[10] Fix | Delete
[11] Fix | Delete
[12] Fix | Delete
__all__ = [
[13] Fix | Delete
# Legacy interface exports traditional RFC 1521 Base64 encodings
[14] Fix | Delete
'encode', 'decode', 'encodestring', 'decodestring',
[15] Fix | Delete
# Generalized interface for other encodings
[16] Fix | Delete
'b64encode', 'b64decode', 'b32encode', 'b32decode',
[17] Fix | Delete
'b16encode', 'b16decode',
[18] Fix | Delete
# Standard Base64 encoding
[19] Fix | Delete
'standard_b64encode', 'standard_b64decode',
[20] Fix | Delete
# Some common Base64 alternatives. As referenced by RFC 3458, see thread
[21] Fix | Delete
# starting at:
[22] Fix | Delete
#
[23] Fix | Delete
# http://zgp.org/pipermail/p2p-hackers/2001-September/000316.html
[24] Fix | Delete
'urlsafe_b64encode', 'urlsafe_b64decode',
[25] Fix | Delete
]
[26] Fix | Delete
[27] Fix | Delete
_translation = [chr(_x) for _x in range(256)]
[28] Fix | Delete
EMPTYSTRING = ''
[29] Fix | Delete
[30] Fix | Delete
[31] Fix | Delete
def _translate(s, altchars):
[32] Fix | Delete
translation = _translation[:]
[33] Fix | Delete
for k, v in altchars.items():
[34] Fix | Delete
translation[ord(k)] = v
[35] Fix | Delete
return s.translate(''.join(translation))
[36] Fix | Delete
[37] Fix | Delete
[38] Fix | Delete
[39] Fix | Delete
# Base64 encoding/decoding uses binascii
[40] Fix | Delete
[41] Fix | Delete
def b64encode(s, altchars=None):
[42] Fix | Delete
"""Encode a string using Base64.
[43] Fix | Delete
[44] Fix | Delete
s is the string to encode. Optional altchars must be a string of at least
[45] Fix | Delete
length 2 (additional characters are ignored) which specifies an
[46] Fix | Delete
alternative alphabet for the '+' and '/' characters. This allows an
[47] Fix | Delete
application to e.g. generate url or filesystem safe Base64 strings.
[48] Fix | Delete
[49] Fix | Delete
The encoded string is returned.
[50] Fix | Delete
"""
[51] Fix | Delete
# Strip off the trailing newline
[52] Fix | Delete
encoded = binascii.b2a_base64(s)[:-1]
[53] Fix | Delete
if altchars is not None:
[54] Fix | Delete
return encoded.translate(string.maketrans(b'+/', altchars[:2]))
[55] Fix | Delete
return encoded
[56] Fix | Delete
[57] Fix | Delete
[58] Fix | Delete
def b64decode(s, altchars=None):
[59] Fix | Delete
"""Decode a Base64 encoded string.
[60] Fix | Delete
[61] Fix | Delete
s is the string to decode. Optional altchars must be a string of at least
[62] Fix | Delete
length 2 (additional characters are ignored) which specifies the
[63] Fix | Delete
alternative alphabet used instead of the '+' and '/' characters.
[64] Fix | Delete
[65] Fix | Delete
The decoded string is returned. A TypeError is raised if s is
[66] Fix | Delete
incorrectly padded. Characters that are neither in the normal base-64
[67] Fix | Delete
alphabet nor the alternative alphabet are discarded prior to the padding
[68] Fix | Delete
check.
[69] Fix | Delete
"""
[70] Fix | Delete
if altchars is not None:
[71] Fix | Delete
s = s.translate(string.maketrans(altchars[:2], '+/'))
[72] Fix | Delete
try:
[73] Fix | Delete
return binascii.a2b_base64(s)
[74] Fix | Delete
except binascii.Error, msg:
[75] Fix | Delete
# Transform this exception for consistency
[76] Fix | Delete
raise TypeError(msg)
[77] Fix | Delete
[78] Fix | Delete
[79] Fix | Delete
def standard_b64encode(s):
[80] Fix | Delete
"""Encode a string using the standard Base64 alphabet.
[81] Fix | Delete
[82] Fix | Delete
s is the string to encode. The encoded string is returned.
[83] Fix | Delete
"""
[84] Fix | Delete
return b64encode(s)
[85] Fix | Delete
[86] Fix | Delete
def standard_b64decode(s):
[87] Fix | Delete
"""Decode a string encoded with the standard Base64 alphabet.
[88] Fix | Delete
[89] Fix | Delete
Argument s is the string to decode. The decoded string is returned. A
[90] Fix | Delete
TypeError is raised if the string is incorrectly padded. Characters that
[91] Fix | Delete
are not in the standard alphabet are discarded prior to the padding
[92] Fix | Delete
check.
[93] Fix | Delete
"""
[94] Fix | Delete
return b64decode(s)
[95] Fix | Delete
[96] Fix | Delete
_urlsafe_encode_translation = string.maketrans(b'+/', b'-_')
[97] Fix | Delete
_urlsafe_decode_translation = string.maketrans(b'-_', b'+/')
[98] Fix | Delete
[99] Fix | Delete
def urlsafe_b64encode(s):
[100] Fix | Delete
"""Encode a string using the URL- and filesystem-safe Base64 alphabet.
[101] Fix | Delete
[102] Fix | Delete
Argument s is the string to encode. The encoded string is returned. The
[103] Fix | Delete
alphabet uses '-' instead of '+' and '_' instead of '/'.
[104] Fix | Delete
"""
[105] Fix | Delete
return b64encode(s).translate(_urlsafe_encode_translation)
[106] Fix | Delete
[107] Fix | Delete
def urlsafe_b64decode(s):
[108] Fix | Delete
"""Decode a string using the URL- and filesystem-safe Base64 alphabet.
[109] Fix | Delete
[110] Fix | Delete
Argument s is the string to decode. The decoded string is returned. A
[111] Fix | Delete
TypeError is raised if the string is incorrectly padded. Characters that
[112] Fix | Delete
are not in the URL-safe base-64 alphabet, and are not a plus '+' or slash
[113] Fix | Delete
'/', are discarded prior to the padding check.
[114] Fix | Delete
[115] Fix | Delete
The alphabet uses '-' instead of '+' and '_' instead of '/'.
[116] Fix | Delete
"""
[117] Fix | Delete
return b64decode(s.translate(_urlsafe_decode_translation))
[118] Fix | Delete
[119] Fix | Delete
[120] Fix | Delete
[121] Fix | Delete
# Base32 encoding/decoding must be done in Python
[122] Fix | Delete
_b32alphabet = {
[123] Fix | Delete
0: 'A', 9: 'J', 18: 'S', 27: '3',
[124] Fix | Delete
1: 'B', 10: 'K', 19: 'T', 28: '4',
[125] Fix | Delete
2: 'C', 11: 'L', 20: 'U', 29: '5',
[126] Fix | Delete
3: 'D', 12: 'M', 21: 'V', 30: '6',
[127] Fix | Delete
4: 'E', 13: 'N', 22: 'W', 31: '7',
[128] Fix | Delete
5: 'F', 14: 'O', 23: 'X',
[129] Fix | Delete
6: 'G', 15: 'P', 24: 'Y',
[130] Fix | Delete
7: 'H', 16: 'Q', 25: 'Z',
[131] Fix | Delete
8: 'I', 17: 'R', 26: '2',
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
_b32tab = _b32alphabet.items()
[135] Fix | Delete
_b32tab.sort()
[136] Fix | Delete
_b32tab = [v for k, v in _b32tab]
[137] Fix | Delete
_b32rev = dict([(v, long(k)) for k, v in _b32alphabet.items()])
[138] Fix | Delete
[139] Fix | Delete
[140] Fix | Delete
def b32encode(s):
[141] Fix | Delete
"""Encode a string using Base32.
[142] Fix | Delete
[143] Fix | Delete
s is the string to encode. The encoded string is returned.
[144] Fix | Delete
"""
[145] Fix | Delete
parts = []
[146] Fix | Delete
quanta, leftover = divmod(len(s), 5)
[147] Fix | Delete
# Pad the last quantum with zero bits if necessary
[148] Fix | Delete
if leftover:
[149] Fix | Delete
s += ('\0' * (5 - leftover))
[150] Fix | Delete
quanta += 1
[151] Fix | Delete
for i in range(quanta):
[152] Fix | Delete
# c1 and c2 are 16 bits wide, c3 is 8 bits wide. The intent of this
[153] Fix | Delete
# code is to process the 40 bits in units of 5 bits. So we take the 1
[154] Fix | Delete
# leftover bit of c1 and tack it onto c2. Then we take the 2 leftover
[155] Fix | Delete
# bits of c2 and tack them onto c3. The shifts and masks are intended
[156] Fix | Delete
# to give us values of exactly 5 bits in width.
[157] Fix | Delete
c1, c2, c3 = struct.unpack('!HHB', s[i*5:(i+1)*5])
[158] Fix | Delete
c2 += (c1 & 1) << 16 # 17 bits wide
[159] Fix | Delete
c3 += (c2 & 3) << 8 # 10 bits wide
[160] Fix | Delete
parts.extend([_b32tab[c1 >> 11], # bits 1 - 5
[161] Fix | Delete
_b32tab[(c1 >> 6) & 0x1f], # bits 6 - 10
[162] Fix | Delete
_b32tab[(c1 >> 1) & 0x1f], # bits 11 - 15
[163] Fix | Delete
_b32tab[c2 >> 12], # bits 16 - 20 (1 - 5)
[164] Fix | Delete
_b32tab[(c2 >> 7) & 0x1f], # bits 21 - 25 (6 - 10)
[165] Fix | Delete
_b32tab[(c2 >> 2) & 0x1f], # bits 26 - 30 (11 - 15)
[166] Fix | Delete
_b32tab[c3 >> 5], # bits 31 - 35 (1 - 5)
[167] Fix | Delete
_b32tab[c3 & 0x1f], # bits 36 - 40 (1 - 5)
[168] Fix | Delete
])
[169] Fix | Delete
encoded = EMPTYSTRING.join(parts)
[170] Fix | Delete
# Adjust for any leftover partial quanta
[171] Fix | Delete
if leftover == 1:
[172] Fix | Delete
return encoded[:-6] + '======'
[173] Fix | Delete
elif leftover == 2:
[174] Fix | Delete
return encoded[:-4] + '===='
[175] Fix | Delete
elif leftover == 3:
[176] Fix | Delete
return encoded[:-3] + '==='
[177] Fix | Delete
elif leftover == 4:
[178] Fix | Delete
return encoded[:-1] + '='
[179] Fix | Delete
return encoded
[180] Fix | Delete
[181] Fix | Delete
[182] Fix | Delete
def b32decode(s, casefold=False, map01=None):
[183] Fix | Delete
"""Decode a Base32 encoded string.
[184] Fix | Delete
[185] Fix | Delete
s is the string to decode. Optional casefold is a flag specifying whether
[186] Fix | Delete
a lowercase alphabet is acceptable as input. For security purposes, the
[187] Fix | Delete
default is False.
[188] Fix | Delete
[189] Fix | Delete
RFC 3548 allows for optional mapping of the digit 0 (zero) to the letter O
[190] Fix | Delete
(oh), and for optional mapping of the digit 1 (one) to either the letter I
[191] Fix | Delete
(eye) or letter L (el). The optional argument map01 when not None,
[192] Fix | Delete
specifies which letter the digit 1 should be mapped to (when map01 is not
[193] Fix | Delete
None, the digit 0 is always mapped to the letter O). For security
[194] Fix | Delete
purposes the default is None, so that 0 and 1 are not allowed in the
[195] Fix | Delete
input.
[196] Fix | Delete
[197] Fix | Delete
The decoded string is returned. A TypeError is raised if s were
[198] Fix | Delete
incorrectly padded or if there are non-alphabet characters present in the
[199] Fix | Delete
string.
[200] Fix | Delete
"""
[201] Fix | Delete
quanta, leftover = divmod(len(s), 8)
[202] Fix | Delete
if leftover:
[203] Fix | Delete
raise TypeError('Incorrect padding')
[204] Fix | Delete
# Handle section 2.4 zero and one mapping. The flag map01 will be either
[205] Fix | Delete
# False, or the character to map the digit 1 (one) to. It should be
[206] Fix | Delete
# either L (el) or I (eye).
[207] Fix | Delete
if map01:
[208] Fix | Delete
s = s.translate(string.maketrans(b'01', b'O' + map01))
[209] Fix | Delete
if casefold:
[210] Fix | Delete
s = s.upper()
[211] Fix | Delete
# Strip off pad characters from the right. We need to count the pad
[212] Fix | Delete
# characters because this will tell us how many null bytes to remove from
[213] Fix | Delete
# the end of the decoded string.
[214] Fix | Delete
padchars = 0
[215] Fix | Delete
mo = re.search('(?P<pad>[=]*)$', s)
[216] Fix | Delete
if mo:
[217] Fix | Delete
padchars = len(mo.group('pad'))
[218] Fix | Delete
if padchars > 0:
[219] Fix | Delete
s = s[:-padchars]
[220] Fix | Delete
# Now decode the full quanta
[221] Fix | Delete
parts = []
[222] Fix | Delete
acc = 0
[223] Fix | Delete
shift = 35
[224] Fix | Delete
for c in s:
[225] Fix | Delete
val = _b32rev.get(c)
[226] Fix | Delete
if val is None:
[227] Fix | Delete
raise TypeError('Non-base32 digit found')
[228] Fix | Delete
acc += _b32rev[c] << shift
[229] Fix | Delete
shift -= 5
[230] Fix | Delete
if shift < 0:
[231] Fix | Delete
parts.append(binascii.unhexlify('%010x' % acc))
[232] Fix | Delete
acc = 0
[233] Fix | Delete
shift = 35
[234] Fix | Delete
# Process the last, partial quanta
[235] Fix | Delete
last = binascii.unhexlify('%010x' % acc)
[236] Fix | Delete
if padchars == 0:
[237] Fix | Delete
last = '' # No characters
[238] Fix | Delete
elif padchars == 1:
[239] Fix | Delete
last = last[:-1]
[240] Fix | Delete
elif padchars == 3:
[241] Fix | Delete
last = last[:-2]
[242] Fix | Delete
elif padchars == 4:
[243] Fix | Delete
last = last[:-3]
[244] Fix | Delete
elif padchars == 6:
[245] Fix | Delete
last = last[:-4]
[246] Fix | Delete
else:
[247] Fix | Delete
raise TypeError('Incorrect padding')
[248] Fix | Delete
parts.append(last)
[249] Fix | Delete
return EMPTYSTRING.join(parts)
[250] Fix | Delete
[251] Fix | Delete
[252] Fix | Delete
[253] Fix | Delete
# RFC 3548, Base 16 Alphabet specifies uppercase, but hexlify() returns
[254] Fix | Delete
# lowercase. The RFC also recommends against accepting input case
[255] Fix | Delete
# insensitively.
[256] Fix | Delete
def b16encode(s):
[257] Fix | Delete
"""Encode a string using Base16.
[258] Fix | Delete
[259] Fix | Delete
s is the string to encode. The encoded string is returned.
[260] Fix | Delete
"""
[261] Fix | Delete
return binascii.hexlify(s).upper()
[262] Fix | Delete
[263] Fix | Delete
[264] Fix | Delete
def b16decode(s, casefold=False):
[265] Fix | Delete
"""Decode a Base16 encoded string.
[266] Fix | Delete
[267] Fix | Delete
s is the string to decode. Optional casefold is a flag specifying whether
[268] Fix | Delete
a lowercase alphabet is acceptable as input. For security purposes, the
[269] Fix | Delete
default is False.
[270] Fix | Delete
[271] Fix | Delete
The decoded string is returned. A TypeError is raised if s is
[272] Fix | Delete
incorrectly padded or if there are non-alphabet characters present in the
[273] Fix | Delete
string.
[274] Fix | Delete
"""
[275] Fix | Delete
if casefold:
[276] Fix | Delete
s = s.upper()
[277] Fix | Delete
if re.search('[^0-9A-F]', s):
[278] Fix | Delete
raise TypeError('Non-base16 digit found')
[279] Fix | Delete
return binascii.unhexlify(s)
[280] Fix | Delete
[281] Fix | Delete
[282] Fix | Delete
[283] Fix | Delete
# Legacy interface. This code could be cleaned up since I don't believe
[284] Fix | Delete
# binascii has any line length limitations. It just doesn't seem worth it
[285] Fix | Delete
# though.
[286] Fix | Delete
[287] Fix | Delete
MAXLINESIZE = 76 # Excluding the CRLF
[288] Fix | Delete
MAXBINSIZE = (MAXLINESIZE//4)*3
[289] Fix | Delete
[290] Fix | Delete
def encode(input, output):
[291] Fix | Delete
"""Encode a file."""
[292] Fix | Delete
while True:
[293] Fix | Delete
s = input.read(MAXBINSIZE)
[294] Fix | Delete
if not s:
[295] Fix | Delete
break
[296] Fix | Delete
while len(s) < MAXBINSIZE:
[297] Fix | Delete
ns = input.read(MAXBINSIZE-len(s))
[298] Fix | Delete
if not ns:
[299] Fix | Delete
break
[300] Fix | Delete
s += ns
[301] Fix | Delete
line = binascii.b2a_base64(s)
[302] Fix | Delete
output.write(line)
[303] Fix | Delete
[304] Fix | Delete
[305] Fix | Delete
def decode(input, output):
[306] Fix | Delete
"""Decode a file."""
[307] Fix | Delete
while True:
[308] Fix | Delete
line = input.readline()
[309] Fix | Delete
if not line:
[310] Fix | Delete
break
[311] Fix | Delete
s = binascii.a2b_base64(line)
[312] Fix | Delete
output.write(s)
[313] Fix | Delete
[314] Fix | Delete
[315] Fix | Delete
def encodestring(s):
[316] Fix | Delete
"""Encode a string into multiple lines of base-64 data."""
[317] Fix | Delete
pieces = []
[318] Fix | Delete
for i in range(0, len(s), MAXBINSIZE):
[319] Fix | Delete
chunk = s[i : i + MAXBINSIZE]
[320] Fix | Delete
pieces.append(binascii.b2a_base64(chunk))
[321] Fix | Delete
return "".join(pieces)
[322] Fix | Delete
[323] Fix | Delete
[324] Fix | Delete
def decodestring(s):
[325] Fix | Delete
"""Decode a string."""
[326] Fix | Delete
return binascii.a2b_base64(s)
[327] Fix | Delete
[328] Fix | Delete
[329] Fix | Delete
[330] Fix | Delete
# Useable as a script...
[331] Fix | Delete
def test():
[332] Fix | Delete
"""Small test program"""
[333] Fix | Delete
import sys, getopt
[334] Fix | Delete
try:
[335] Fix | Delete
opts, args = getopt.getopt(sys.argv[1:], 'deut')
[336] Fix | Delete
except getopt.error, msg:
[337] Fix | Delete
sys.stdout = sys.stderr
[338] Fix | Delete
print msg
[339] Fix | Delete
print """usage: %s [-d|-e|-u|-t] [file|-]
[340] Fix | Delete
-d, -u: decode
[341] Fix | Delete
-e: encode (default)
[342] Fix | Delete
-t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0]
[343] Fix | Delete
sys.exit(2)
[344] Fix | Delete
func = encode
[345] Fix | Delete
for o, a in opts:
[346] Fix | Delete
if o == '-e': func = encode
[347] Fix | Delete
if o == '-d': func = decode
[348] Fix | Delete
if o == '-u': func = decode
[349] Fix | Delete
if o == '-t': test1(); return
[350] Fix | Delete
if args and args[0] != '-':
[351] Fix | Delete
with open(args[0], 'rb') as f:
[352] Fix | Delete
func(f, sys.stdout)
[353] Fix | Delete
else:
[354] Fix | Delete
func(sys.stdin, sys.stdout)
[355] Fix | Delete
[356] Fix | Delete
[357] Fix | Delete
def test1():
[358] Fix | Delete
s0 = "Aladdin:open sesame"
[359] Fix | Delete
s1 = encodestring(s0)
[360] Fix | Delete
s2 = decodestring(s1)
[361] Fix | Delete
print s0, repr(s1), s2
[362] Fix | Delete
[363] Fix | Delete
[364] Fix | Delete
if __name__ == '__main__':
[365] Fix | Delete
test()
[366] Fix | Delete
[367] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function