-
Notifications
You must be signed in to change notification settings - Fork 4
/
base64encode_v1_02.py
104 lines (98 loc) · 2.22 KB
/
base64encode_v1_02.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import math # required
import secrets ## optional for future use
import string # required
import binascii # required
#Author: Steven Hatzakis @2018, Licensed under Apache 2.0
# Version 1.02
#test binary data string: 0b010010010111011111001110010111011101001110001101001011100010011100011101001110011111001110010111010001001010011101100011001010010100010010011100111001000010000000000001001000
#test string as hex: '0x125df39774e34b89c74e7ce5d129d8ca512739080048'
#test string result :'INVENTEDBYSTEVENHATZAKIS@2018'
## legacy notes: #FIX: LEADING ZEROS LOST (i.e.w/ test string 0x064ce8c835f4d374e04e33244beccad0)
#also, even when no leading zero in string, the leading zeroes of the actual base64 character are discarded,
#as the encoding appears to happen from right to left, see this test string where leading zeroes of
# first char 2 are discarded, and last char is N 0xb13ae7e331ce9dfa59799e95ee8dc117
def base64en(address_hex): # important never to rename base64en to "base64" which can corrupt the Python installation.
# (renamed alphabet to base64library below) alphabet = string.digits+string.ascii_uppercase+string.punctuation
base64library=["0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
"!",
'"',
"#",
"$",
"%",
"&",
"'",
"(",
")",
"*",
'+',
",",
"-",
".",
"{",
":",
";",
"<",
"=",
">",
"?",
"@",
"[",
"}",
"]",
"^",
"_",
"`"]
b64_string = ''
# Get the number of leading zeros and convert hex to decimal
leading_zeros = len(str(address_hex))-2 #len(str(address_hex)) - len(str(address_hex.lstrip('0')))
# Convert hex to decimal
address_int = int(address_hex)
# Append digits to the start of string
while address_int > 0:
digit = address_int % 64
digit_char = base64library[digit]
b64_string = digit_char + b64_string
address_int //= 64
# Add '1' for each 2 leading zeros
#ones = leading_zeros // 8
#for one in range(ones):
# b64_string = '1' + b64_string
return b64_string
hexer=int(input('enter binary string with '0b' pad'),2)
print(base64en(hexer))
print("The above is this many char's long: ",len(str((base64en(hexer)))))