Last active
September 30, 2019 21:11
-
-
Save NikhilPeri/58be4a5c939640cefc4b07aa2d499978 to your computer and use it in GitHub Desktop.
fast, irreversible obfuscation that preserves structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PS this would make a great interview question | |
def map_int(i, state): | |
i = ord('0') + ( i + state ) % 9 | |
state += i | |
return i, state | |
def map_lower(c, state): | |
x = ord('a') + (c + state ) % 25 | |
state += x | |
return x, state | |
def map_upper(c, state): | |
c = ord('A') + (c + state ) % 25 | |
state += c | |
return c, state | |
def obfuscate(example): | |
state=hash(example) | |
example=[ord(i) for i in example] | |
for i, x in enumerate(example): | |
if x in range(ord('a'), ord('z') + 1): | |
example[i], state = map_upper(x, state) | |
elif x in range(ord('A'), ord('Z') + 1): | |
example[i], state = map_lower(x, state) | |
elif x in range(ord('0'), ord('9') + 1): | |
example[i], state = map_int(x, state) | |
example=[chr(i) for i in example] | |
return ''.join(example) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment