def gen_timed_link(relative_path, secret_key, timeout, type): start_time = '%08x' % (time.time() - 10) stop_time = '%08x' % (time.time() + int(timeout)) hashable = secret_key + relative_path + start_time + stop_time if type == 'md5': hashed = python.md5(hashable).hexdigest() else: hashed = python.sha1(hashable).hexdigest() return '%s%s%s' % (hashed, start_time, stop_time)
def getIdentifier(self, path): """ The returned identifier is a digest of the path encoded in hex string. The hash function used is SHA1. It caches the identifiers in a dictionary indexed by path and with a maximum number of entry specified by the constant ID_CACHE_MAX_SIZE. @return: an identifier for path. """ ident = self._identifiers.get(path, None) if ident is None: sha1Hash = python.sha1() sha1Hash.update(self._cachePrefix + path) ident = sha1Hash.digest().encode("hex").strip('\n') # Prevent the cache from growing endlessly if len(self._identifiers) >= ID_CACHE_MAX_SIZE: self._identifiers.clear() self._identifiers[path] = ident return ident