Skip to content

Instantly share code, notes, and snippets.

@davidwtbuxton
Created December 15, 2019 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwtbuxton/65e77c21710e2c9ec3bb82df15e0ac81 to your computer and use it in GitHub Desktop.
Save davidwtbuxton/65e77c21710e2c9ec3bb82df15e0ac81 to your computer and use it in GitHub Desktop.
Other approaches to obfuscating a value
# https://github.com/potatolondon/django-gcloud-connectors/blob/b5279492b5b4b615640efa7cc5783a2cee0c1cbe/gcloudc/forms/fields.py#L145
import base64
import itertools
_VC_KEY = "1K94KG8L" # Fixed key, don't change this!!
def model_path(obj):
return obj._meta.db_table
def vc_encode(string):
enc = []
for i in range(len(string)):
key_c = _VC_KEY[i % len(_VC_KEY)]
enc_c = chr((ord(string[i]) + ord(key_c)) % 256)
enc.append(enc_c)
ret = base64.urlsafe_b64encode("".join(enc).encode("utf-8")).decode("ascii")
return ret
def vc_decode(enc):
dec = []
enc = base64.urlsafe_b64decode(enc).decode("utf-8")
for i in range(len(enc)):
key_c = _VC_KEY[i % len(_VC_KEY)]
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
_dbux_key = tuple(ord(c) for c in _VC_KEY)
def bux_encode(value) -> str:
caesars = itertools.cycle(_dbux_key)
points = bytearray((ord(c) + next(caesars)) % 256 for c in value)
result = base64.urlsafe_b64encode(points).decode('ascii')
return result
def bux_decode(value) -> str:
caesars = itertools.cycle(_dbux_key)
points = base64.urlsafe_b64decode(value.encode('ascii'))
points = bytearray((256 + p - next(caesars)) % 256 for p in points)
result = points.decode('utf-8')
return result
def simple_encode(value) -> str:
return base64.urlsafe_b64encode(value.encode('utf-8')).decode('ascii')
def simple_decode(value) -> str:
return base64.urlsafe_b64decode(value.encode('ascii')).decode('utf-8')
import unittest
class PotatoTestCase(unittest.TestCase):
def test_encode(self):
result = vc_encode('django_user|123456789')
self.assertEqual(result, 'wpXCtcKawqLCssK2wpfDgcKkwrDCq8KwfHlrwoBmwoFwbMKE')
def test_decode(self):
result = vc_decode('wpXCtcKawqLCssK2wpfDgcKkwrDCq8KwfHlrwoBmwoFwbMKE')
self.assertEqual(result, 'django_user|123456789')
class BuxtonTestCase(unittest.TestCase):
def test_encode(self):
result = bux_encode('django_user|123456789')
self.assertEqual(result, 'lbWaorK2l8GksKuwfHlrgGaBcGyE')
def test_decode(self):
result = bux_decode('lbWaorK2l8GksKuwfHlrgGaBcGyE')
self.assertEqual(result, 'django_user|123456789')
def test_dbux_key(self):
self.assertEqual(_dbux_key, (49, 75, 57, 52, 75, 71, 56, 76))
class SimpleTestCase(unittest.TestCase):
def test_encode(self):
result = simple_encode('django_user|123456789')
self.assertEqual(result, 'ZGphbmdvX3VzZXJ8MTIzNDU2Nzg5')
def test_decode(self):
result = simple_decode('ZGphbmdvX3VzZXJ8MTIzNDU2Nzg5')
self.assertEqual(result, 'django_user|123456789')
import timeit
speed_tests = [
'vc_encode("django_user|123456789")',
'vc_decode("wpXCtcKawqLCssK2wpfDgcKkwrDCq8KwfHlrwoBmwoFwbMKE")',
'bux_encode("django_user|123456789")',
'bux_decode("lbWaorK2l8GksKuwfHlrgGaBcGyE")',
'simple_encode("django_user|123456789")',
'simple_decode("ZGphbmdvX3VzZXJ8MTIzNDU2Nzg5")',
]
if __name__ == '__main__':
for stmt in speed_tests:
print('Timing', stmt)
print(timeit.repeat(stmt, repeat=3, number=100000, globals=globals()))
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment