Skip to content

Instantly share code, notes, and snippets.

@NikhilPeri
Last active September 30, 2019 21:11
Show Gist options
  • Save NikhilPeri/58be4a5c939640cefc4b07aa2d499978 to your computer and use it in GitHub Desktop.
Save NikhilPeri/58be4a5c939640cefc4b07aa2d499978 to your computer and use it in GitHub Desktop.
fast, irreversible obfuscation that preserves structure
# 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