from binascii import unhexlify B64_ALPHABET list ABCDEFGHIJKLMNOPQRST

 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
from binascii import unhexlify
B64_ALPHABET = list(
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
)
def hex_to_b64(val_hex):
val_bytes = unhexlify(val_hex)
print('|'.join((format(x, '08b') for x in val_bytes)))
b64_digits = []
for idx in range(0, len(val_bytes) * 8, 6):
print('----------- idx=%d' % idx)
byte_offset, bit_offset = divmod(idx, 8)
print('byte offset', byte_offset, 'bit_offset', bit_offset)
num = val_bytes[byte_offset]
num = (num << bit_offset) % (2**8)
num = (num >> 2)
if bit_offset >= 3:
if len(val_bytes) >= byte_offset:
num2 = val_bytes[byte_offset + 1]
else:
num2 = 0
num2 = num2 >> (10 - bit_offset)
num = num | num2
print('result num', num, 'as bits', format(num, '016b'))
char = B64_ALPHABET[num]
print('result b64 char: [%s]' % char)
b64_digits.append(char)
return ''.join(b64_digits)
def main():
val_hex = '49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d'
val_b64 = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t'
calc_b64 = hex_to_b64(val_hex)
print('Calculated b64', calc_b64)
assert calc_b64 == val_b64
if __name__ == '__main__':
main()