/usr/share/pyshared/mx/UID/UID.py is in python-egenix-mxuid 3.2.1-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | """ mx.UID -- A UID datatype.
Relies on mx.DateTime.
Copyright (c) 1998-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2011, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
"""
import mimetypes
try:
import hashlib
except ImportError:
import md5 as hashlib
from mx import DateTime
#
# Import the C extension module
#
from mxUID import *
from mxUID import __version__
### Helpers
def mangle(uid, key,
md5=hashlib.md5,otp=otp):
""" Mangle the UID string uid using the given key string.
The output has the same length as the input UID string and
should make it hard to forge valid UIDs without knowledge of
the key string.
Note that the output string is not a valid UID string in
itself, i.e. it most likely won't verify().
"""
# Idea: Even if somebody finds the akey which allows decoding
# the timestamp, it should not be possible to use it to extract
# the key or the bkey from it.
akey = md5(key).digest()
bkey = md5(uid[:16] + key).digest()
# Add a little noise to please the eye (except to the counter part)
# and apply the encoding pad
ckey = 'ffff' + uid[:4] * 7
return otp(otp(uid, ckey), akey + bkey * 2)
def demangle(muid, key,
md5=hashlib.md5,otp=otp):
""" Demangle a mangle()d UID string muid using the given key
string.
"""
akey = md5(key).digest()
# First decode the counter and the noisy timestamp part
tuid = otp(muid[:16], akey)
# Next denoise the timestamp part to build the bkey
ckey = 'ffff' + tuid[:4] * 7
bkey = md5(otp(tuid, ckey) + key).digest()
# Decode and denoise the UID
return otp(otp(muid, akey + bkey * 2), ckey)
# Override the function provided by mxUID with one using DateTime
# instances:
timestamp_ticks = timestamp
def timestamp(uid,
DateTimeFromTicks=DateTime.DateTimeFromTicks,
timestamp=timestamp_ticks):
""" Returns the timestamp encoded in the UID string uid
as DateTime instance.
"""
return DateTimeFromTicks(timestamp(uid))
###
#
# Experimental Python wrapper around UID strings.
#
class ID:
""" ID class for creating unique IDs.
The generated IDs are unique to the host, process and time
with high probabilty. In addition their validity can be
verified (using a CRC type algorithm).
The algorithm uses a 40-bit timestamp in the ID which can be
extracted using the .timestamp() method. Accuracy is one second.
"""
# ID value
uid = None
def __init__(self,target=None,code='',timestamp=None):
""" Construct a new uid for the object target.
target defaults to the singleton None.
code is an optional addition to the uid that can later be
used to verify the validity of the uid together with the
UID's CRC value.
timestamp may be given as DateTime instance. It defaults
to the current local time.
"""
if timestamp:
self.uid = UID(target,code,timestamp)
else:
self.uid = UID(target,code)
def timestamp(self,
DateTimeFromTicks=DateTime.DateTimeFromTicks,
timestamp=timestamp):
""" Returns the timestamp encoded in the ID
as DateTime instance.
"""
return DateTimeFromTicks(timestamp(self.uid))
def set_uid(self,uid,code='',
verify=verify):
# Check validity
if not verify(uid,code):
raise Error,'invalid UID'
self.uid = uid
def __str__(self):
return self.uid
def __cmp__(self,other,
cmp=cmp):
return cmp(self.uid,other.uid)
def __hash__(self,
hash=hash):
return hash(self.uid)
# Helper class
class _EmptyClass:
pass
### Other constructors
def IDFromUID(s,code=''):
""" Create an ID object from the given string UID.
This can raise an Error in case the string does not map to a
valid UID. code is used in the verification process if given.
"""
id = _EmptyClass()
id.__class__ = ID
id.set_uid(s,code)
return id
if __name__ == '__main__':
uid = UID()
print uid,' timestamp =',timestamp(uid)
print verify(uid) * 'OK' or 'NOT OK'
|