Welcome, guest | Sign In | My Account | Store | Cart
# -*- 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

Diff to Previous Revision

--- revision 3 2010-08-16 07:18:00
+++ revision 4 2010-08-16 07:18:43
@@ -21,3 +21,9 @@
         return _n^_KEY
     except:
         return s
+
+>>> id = 717
+>>> mask(id)
+'xlqlpqqhtt'
+>>> unmask('xlqlpqqhtt')
+717

History