Skip to content

Instantly share code, notes, and snippets.

@daisylb
Created March 8, 2015 21:59
Show Gist options
  • Save daisylb/3b4d75ba039e6e455f7c to your computer and use it in GitHub Desktop.
Save daisylb/3b4d75ba039e6e455f7c to your computer and use it in GitHub Desktop.
VALID_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
def cipher_string(token, pad, decipher=False):
result = []
for token_char, pad_char in zip(token, pad):
token_index = VALID_CHARS.index(token_char)
pad_index = VALID_CHARS.index(pad_char)
if decipher:
result_index = (token_index - pad_index) % len(VALID_CHARS)
else:
result_index = (token_index + pad_index) % len(VALID_CHARS)
result.append(VALID_CHARS[result_index])
return ''.join(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment