def _generate_cache_key(request, method, headerlist, key_prefix): """Return a cache key from the headers given in the header list.""" ctx = md5(usedforsecurity=False) for header in headerlist: value = request.META.get(header) if value is not None: ctx.update(value.encode()) url = md5(request.build_absolute_uri().encode('ascii'), usedforsecurity=False) cache_key = 'views.decorators.cache.cache_page.%s.%s.%s.%s' % ( key_prefix, method, url.hexdigest(), ctx.hexdigest()) return _i18n_cache_key_suffix(request, cache_key)
def _generate_cache_header_key(key_prefix, request): """Return a cache key for the header cache.""" url = md5(request.build_absolute_uri().encode('ascii'), usedforsecurity=False) cache_key = 'views.decorators.cache.cache_header.%s.%s' % (key_prefix, url.hexdigest()) return _i18n_cache_key_suffix(request, cache_key)
def make_template_fragment_key(fragment_name, vary_on=None): hasher = md5(usedforsecurity=False) if vary_on is not None: for arg in vary_on: hasher.update(str(arg).encode()) hasher.update(b":") return TEMPLATE_FRAGMENT_KEY_TEMPLATE % (fragment_name, hasher.hexdigest())
def names_digest(*args, length): """ Generate a 32-bit digest of a set of arguments that can be used to shorten identifying names. """ h = md5(usedforsecurity=False) for arg in args: h.update(arg.encode()) return h.hexdigest()[:length]
def file_hash(self, name, content=None): """ Return a hash of the file with the given name and optional content. """ if content is None: return None hasher = md5(usedforsecurity=False) for chunk in content.chunks(): hasher.update(chunk) return hasher.hexdigest()[:12]
def _key_to_file(self, key, version=None): """ Convert a key into a cache file path. Basically this is the root cache path joined with the md5sum of the key and a suffix. """ key = self.make_and_validate_key(key, version=version) return os.path.join( self._dir, ''.join([ md5(key.encode(), usedforsecurity=False).hexdigest(), self.cache_suffix, ]))
def _sqlite_md5(text): if text is None: return None return md5(text.encode()).hexdigest()
def get_new_connection(self, conn_params): conn = Database.connect(**conn_params) create_deterministic_function = functools.partial( conn.create_function, deterministic=True, ) create_deterministic_function('django_date_extract', 2, _sqlite_datetime_extract) create_deterministic_function('django_date_trunc', 4, _sqlite_date_trunc) create_deterministic_function('django_datetime_cast_date', 3, _sqlite_datetime_cast_date) create_deterministic_function('django_datetime_cast_time', 3, _sqlite_datetime_cast_time) create_deterministic_function('django_datetime_extract', 4, _sqlite_datetime_extract) create_deterministic_function('django_datetime_trunc', 4, _sqlite_datetime_trunc) create_deterministic_function('django_time_extract', 2, _sqlite_time_extract) create_deterministic_function('django_time_trunc', 4, _sqlite_time_trunc) create_deterministic_function('django_time_diff', 2, _sqlite_time_diff) create_deterministic_function('django_timestamp_diff', 2, _sqlite_timestamp_diff) create_deterministic_function('django_format_dtdelta', 3, _sqlite_format_dtdelta) create_deterministic_function('regexp', 2, _sqlite_regexp) create_deterministic_function('ACOS', 1, none_guard(math.acos)) create_deterministic_function('ASIN', 1, none_guard(math.asin)) create_deterministic_function('ATAN', 1, none_guard(math.atan)) create_deterministic_function('ATAN2', 2, none_guard(math.atan2)) create_deterministic_function('BITXOR', 2, none_guard(operator.xor)) create_deterministic_function('CEILING', 1, none_guard(math.ceil)) create_deterministic_function('COS', 1, none_guard(math.cos)) create_deterministic_function('COT', 1, none_guard(lambda x: 1 / math.tan(x))) create_deterministic_function('DEGREES', 1, none_guard(math.degrees)) create_deterministic_function('EXP', 1, none_guard(math.exp)) create_deterministic_function('FLOOR', 1, none_guard(math.floor)) create_deterministic_function('LN', 1, none_guard(math.log)) create_deterministic_function('LOG', 2, none_guard(lambda x, y: math.log(y, x))) create_deterministic_function('LPAD', 3, _sqlite_lpad) create_deterministic_function( 'MD5', 1, none_guard(lambda x: md5(x.encode()).hexdigest())) create_deterministic_function('MOD', 2, none_guard(math.fmod)) create_deterministic_function('PI', 0, lambda: math.pi) create_deterministic_function('POWER', 2, none_guard(operator.pow)) create_deterministic_function('RADIANS', 1, none_guard(math.radians)) create_deterministic_function('REPEAT', 2, none_guard(operator.mul)) create_deterministic_function('REVERSE', 1, none_guard(lambda x: x[::-1])) create_deterministic_function('RPAD', 3, _sqlite_rpad) create_deterministic_function( 'SHA1', 1, none_guard(lambda x: hashlib.sha1(x.encode()).hexdigest())) create_deterministic_function( 'SHA224', 1, none_guard(lambda x: hashlib.sha224(x.encode()).hexdigest())) create_deterministic_function( 'SHA256', 1, none_guard(lambda x: hashlib.sha256(x.encode()).hexdigest())) create_deterministic_function( 'SHA384', 1, none_guard(lambda x: hashlib.sha384(x.encode()).hexdigest())) create_deterministic_function( 'SHA512', 1, none_guard(lambda x: hashlib.sha512(x.encode()).hexdigest())) create_deterministic_function('SIGN', 1, none_guard(lambda x: (x > 0) - (x < 0))) create_deterministic_function('SIN', 1, none_guard(math.sin)) create_deterministic_function('SQRT', 1, none_guard(math.sqrt)) create_deterministic_function('TAN', 1, none_guard(math.tan)) # Don't use the built-in RANDOM() function because it returns a value # in the range [-1 * 2^63, 2^63 - 1] instead of [0, 1). conn.create_function('RAND', 0, random.random) conn.create_aggregate('STDDEV_POP', 1, list_aggregate(statistics.pstdev)) conn.create_aggregate('STDDEV_SAMP', 1, list_aggregate(statistics.stdev)) conn.create_aggregate('VAR_POP', 1, list_aggregate(statistics.pvariance)) conn.create_aggregate('VAR_SAMP', 1, list_aggregate(statistics.variance)) conn.execute('PRAGMA foreign_keys = ON') return conn
def encode(self, password, salt): if salt != "": raise ValueError("salt must be empty.") return md5(password.encode()).hexdigest()
def encode(self, password, salt): self._check_encode_args(password, salt) hash = md5((salt + password).encode()).hexdigest() return "%s$%s$%s" % (self.algorithm, salt, hash)
def set_response_etag(response): if not response.streaming and response.content: response.headers['ETag'] = quote_etag( md5(response.content, usedforsecurity=False).hexdigest(), ) return response