2020-05-20 06:23:17 +00:00
|
|
|
""" https://en.wikipedia.org/wiki/Atbash """
|
|
|
|
import string
|
|
|
|
|
|
|
|
|
|
|
|
def atbash_slow(sequence: str) -> str:
|
|
|
|
"""
|
|
|
|
>>> atbash_slow("ABCDEFG")
|
|
|
|
'ZYXWVUT'
|
|
|
|
|
|
|
|
>>> atbash_slow("aW;;123BX")
|
|
|
|
'zD;;123YC'
|
|
|
|
"""
|
2019-10-05 05:14:13 +00:00
|
|
|
output = ""
|
2020-05-20 06:23:17 +00:00
|
|
|
for i in sequence:
|
2019-06-05 01:09:04 +00:00
|
|
|
extract = ord(i)
|
|
|
|
if 65 <= extract <= 90:
|
2019-10-05 05:14:13 +00:00
|
|
|
output += chr(155 - extract)
|
2019-06-05 01:09:04 +00:00
|
|
|
elif 97 <= extract <= 122:
|
2019-10-05 05:14:13 +00:00
|
|
|
output += chr(219 - extract)
|
2019-05-26 16:40:04 +00:00
|
|
|
else:
|
2019-08-19 13:37:49 +00:00
|
|
|
output += i
|
2020-05-20 06:23:17 +00:00
|
|
|
return output
|
|
|
|
|
|
|
|
|
|
|
|
def atbash(sequence: str) -> str:
|
|
|
|
"""
|
|
|
|
>>> atbash("ABCDEFG")
|
|
|
|
'ZYXWVUT'
|
|
|
|
|
|
|
|
>>> atbash("aW;;123BX")
|
|
|
|
'zD;;123YC'
|
|
|
|
"""
|
|
|
|
letters = string.ascii_letters
|
|
|
|
letters_reversed = string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1]
|
|
|
|
return "".join(
|
|
|
|
letters_reversed[letters.index(c)] if c in letters else c for c in sequence
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def benchmark() -> None:
|
2022-11-10 02:41:28 +00:00
|
|
|
"""Let's benchmark our functions side-by-side..."""
|
2020-05-20 06:23:17 +00:00
|
|
|
from timeit import timeit
|
|
|
|
|
|
|
|
print("Running performance benchmarks...")
|
2022-11-10 02:41:28 +00:00
|
|
|
setup = "from string import printable ; from __main__ import atbash, atbash_slow"
|
|
|
|
print(f"> atbash_slow(): {timeit('atbash_slow(printable)', setup=setup)} seconds")
|
|
|
|
print(f"> atbash(): {timeit('atbash(printable)', setup=setup)} seconds")
|
2019-05-26 16:40:04 +00:00
|
|
|
|
2019-07-08 15:27:51 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2021-03-22 06:59:51 +00:00
|
|
|
for example in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"):
|
|
|
|
print(f"{example} encrypted in atbash: {atbash(example)}")
|
2020-05-20 06:23:17 +00:00
|
|
|
benchmark()
|