A funny, simple, and efficient encrypt for an object ID. Always used against an unsolicited automated spider that scrawls your site to collect data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | # -*- coding: utf-8 -*-
'''
Provide a simple and efficient mask tool for the id of an object.\n
Always used in url against a spider.
'''
_KEY = 9878676540
_BOX = ['a', 'b', 'h', 't', 'n', 's', 'p', 'q', 'l', 'x']
def mask(n):
_id = int(n)^_KEY
_id = [int(i) for i in list(str(_id))]
return ''.join(map(lambda i: _BOX[i], _id))
def unmask(s):
try:
_s = map(lambda l: _BOX.index(l), list(s))
_n = int(''.join(map(str, _s)))
return _n^_KEY
except:
return s
>>> id = 717
>>> mask(id)
'xlqlpqqhtt'
>>> unmask('xlqlpqqhtt')
717
|
Tags: encryption, url