-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupper_dot_lower.py
95 lines (71 loc) · 2.15 KB
/
upper_dot_lower.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
"""
<Program Name>
upper_dot_lower.py
<Started>
March, 2017
<Author>
Lukas Puehringer <[email protected]>
<Purpose>
Provides functions to encode and decode to and from dot-lower encoding.
Dot-lower encoding replaces all upper case letters in a string
with a dot followed by the lower case representation of that letter.
Additionally, the encoding prepends an underscore.
This module can be used to generate RepyV2 conformant non-hidden filenames
from base64 encoded strings.
Note:
The module can be used as Python or RepyV2 module.
<Example>
>>> base64_enc = "c3lzdGVtanMvYWNlL3JlbmFtLnNo"
>>> dot_lower_enc = encode_dot_lower(base64_enc)
>>> dot_lower_enc
'_c3lzd.g.vtan.mv.y.w.nl.l3.jlbm.ft.ln.no'
>>> dot_lower_dec = decode_dot_lower(dot_lower_enc)
>>> base64_enc == dot_lower_dec
True
"""
def encode_dot_lower(string):
"""
<Purpose>
Dot-lower encodes a passed string by replacing all upper case letters
with a dot (.) and the lower case representation of that string.
Additionally the encoder prepends an underscore (_).
<Arguments>
string
A string to dot-lower encode
<Returns>
A dot lower encoded version of the passed string
"""
string_enc = "_"
for char in string:
if char.isupper():
char_enc = "." + char.lower()
else:
char_enc = char
string_enc += char_enc
return string_enc
def decode_dot_lower(string_enc):
"""
<Purpose>
Decodes a passed dot-lower encoded string by replacing all
all dots (.) and the succeeding lower case letter with the upper case
representation of that letter.
The first letter is ignored, because it should be an underscore (_).
<Arguments>
sting_enc
A string to dot-lower decode
<Returns>
The dot lower decoded version of the passed string
"""
string_dec = ""
length = len(string_enc)
# Start at 1 to ignore the leading underscore
i = 1
while i < length:
if (string_enc[i] == "." and i + 1 < length and
string_enc[i + 1].isalpha()):
string_dec += string_enc[i + 1].upper()
i += 2
else:
string_dec += string_enc[i]
i += 1
return string_dec