-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase64.py
More file actions
executable file
·66 lines (50 loc) · 1.67 KB
/
base64.py
File metadata and controls
executable file
·66 lines (50 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
HEX_STRING = '49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d1'
B64_STRING = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29tEA=='
HEX_CHARS = '0123456789abcdef'
B64_CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
# 444 = 0100 0100 0100
# RE = 010001 000100
def binaryify(hex_):
if not hex_:
return []
ret = []
num = HEX_CHARS.index(hex_[0].lower())
for _ in xrange(4):
ret.insert(0, num % 2)
num /= 2
return ret + binaryify(hex_[1:])
def toBase64Char(bits):
value = 0
for i in xrange(6):
value = value * 2 + bits[i]
return B64_CHARS[value]
def toBase64(hex_):
bbits = binaryify(hex_)
sneakyEqualsSigns = ''
while len(bbits) % 6 != 0:
bbits += [0, 0, 0, 0]
sneakyEqualsSigns += '='
chunkedBits = [bbits[i:i+6] for i in xrange(0, len(bbits), 6)]
return ''.join([toBase64Char(bits_) for bits_ in chunkedBits]) + sneakyEqualsSigns
def toHexChar(bits):
value = 0
for i in xrange(4):
value = value * 2 + bits[i]
return HEX_CHARS[value]
def toHex(bits):
chunkedBits = [bits[i:i+4] for i in xrange(0, len(bits), 4)]
return ''.join([toHexChar(bits_) for bits_ in chunkedBits])
def toAsciiChar(bits):
value = 0
for i in xrange(8):
value = value * 2 + bits[i]
return chr(value)
def toAscii(hex_):
binbits = binaryify(hex_)
chunkedBits = [binbits[i:i+8] for i in xrange(0, len(binbits), 8)]
return ''.join([toAsciiChar(bits_) for bits_ in chunkedBits])
def main():
print toBase64(HEX_STRING)
if __name__ == '__main__':
main()