Skip to content

Commit 016078f

Browse files
authored
Moving into subdirectory 'tools' + renamed
1 parent 2b9bfa4 commit 016078f

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

tools/text2base64.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
#Takes a string as an argument and returns 3 base64 encoded string partials.
3+
#One of these strings is guaranteed to be present in the base64 content if
4+
#the original plain-text code contained the (case-sensitive) input string.
5+
6+
#Due to the 8 bit to 6 bit encoding conversion involved in base64 encoding,
7+
#the edges don't always align nicely, so trimming is required as the first
8+
#and last characters may be different depending on the immediate context.
9+
10+
from base64 import b64encode
11+
import sys
12+
13+
def main(input):
14+
L_offset = 0
15+
R_offset = 3 - ((len(input) + L_offset) % 3)
16+
if (R_offset == 3):
17+
R_offset = 0
18+
19+
e1, e2, e3 = encode(input)
20+
21+
e1 = trim(e1, L_offset, R_offset)
22+
23+
L_offset += 1
24+
R_offset = 3 - ((len(input) + L_offset) % 3)
25+
if (R_offset == 3):
26+
R_offset = 0
27+
28+
e2 = trim(e2, L_offset, R_offset)
29+
30+
L_offset += 1
31+
R_offset = 3 - ((len(input) + L_offset) % 3)
32+
if (R_offset == 3):
33+
R_offset = 0
34+
35+
e3 = trim(e3, L_offset, R_offset)
36+
37+
print(e1)
38+
print(e2)
39+
print(e3)
40+
41+
def offset_2_chars(offset):
42+
if (offset == 0):
43+
return 0
44+
elif (offset == 1):
45+
return 2
46+
else:
47+
return 3
48+
49+
def trim(input, L_offset, R_offset):
50+
input = trimL(input, L_offset)
51+
input = trimR(input, R_offset)
52+
return input
53+
54+
def trimL(input, offset):
55+
left_cut = offset_2_chars(offset)
56+
return input[left_cut:]
57+
58+
def trimR(input, offset):
59+
if (offset == 0):
60+
return input
61+
right_cut = offset_2_chars(offset)
62+
return input[:-right_cut]
63+
64+
def encode(input):
65+
e1 = b64encode(input)
66+
e2 = b64encode('0' + input)
67+
e3 = b64encode('00' + input)
68+
return e1, e2, e3
69+
70+
if __name__ == '__main__':
71+
main(sys.argv[1])

0 commit comments

Comments
 (0)