We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Replacing MD5 hashing algorithm with SHA512
In file: common_middleware.py, method: __call__, the used hashing algorithm is no longer considered secure because it is possible to have collisions. This can lead to brute force attempt to find two or more inputs that produce the same hash. iCR suggested that safer alternative hash algorithms, such as SHA-256, SHA-512, SHA-3 are used. 

In the file, MD5 is used to generate a key based on several parameters and inserted into the database as `user_key`. In that case, it's recommended to use a more secure, less collision prone hash function such as- SHA256 or SHA512.


### Sponsorship and Support:

This work is done by the security researchers from OpenRefactory and is supported by the [Open Source Security Foundation (OpenSSF)](https://openssf.org/): [Project Alpha-Omega](https://alpha-omega.dev/). Alpha-Omega is a project partnering with open source software project maintainers to systematically find new, as-yet-undiscovered vulnerabilities in open source code - and get them fixed – to improve global software supply chain security.

The bug is found by running the Intelligent Code Repair (iCR) tool by OpenRefactory and then manually triaging the results.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `ckanext/tracking/middleware.py`
Content:
```
1 import hashlib
2 
3 from urllib.parse import unquote
4 
5 from ckan.model.meta import engine
6 from ckan.common import request
7 from ckan.types import Response
8 
9 
10 def track_request(response: Response) -> Response:
11     path = request.environ.get('PATH_INFO')
12     method = request.environ.get('REQUEST_METHOD')
13     if path == '/_tracking' and method == 'POST':
14         # wsgi.input is a BytesIO object
15         payload = request.environ['wsgi.input'].read().decode()
16         parts = payload.split('&')
17         data = {}
18         for part in parts:
19             k, v = part.split('=')
20             data[k] = unquote(v)
21 
22         # we want a unique anonomized key for each user so that we do
23         # not count multiple clicks from the same user.
24         key = ''.join([
25             request.environ['HTTP_USER_AGENT'],
26             request.environ['REMOTE_ADDR'],
27             request.environ.get('HTTP_ACCEPT_LANGUAGE', ''),
28             request.environ.get('HTTP_ACCEPT_ENCODING', ''),
29         ])
30         key = hashlib.md5(key.encode()).hexdigest()
31         # store key/data here
32         sql = '''INSERT INTO tracking_raw
33                     (user_key, url, tracking_type)
34                     VALUES (%s, %s, %s)'''
35         engine.execute(  # type: ignore
36             sql, key, data.get('url'), data.get('type')
37             )
38     return response
39
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/ckanext/tracking/middleware.py b/ckanext/tracking/middleware.py
--- a/ckanext/tracking/middleware.py
+++ b/ckanext/tracking/middleware.py
@@ -27,7 +27,9 @@
             request.environ.get('HTTP_ACCEPT_LANGUAGE', ''),
             request.environ.get('HTTP_ACCEPT_ENCODING', ''),
         ])
-        key = hashlib.md5(key.encode()).hexdigest()
+        # raises a type error on python<3.9
+        h = hashlib.new('md5', usedforsecurity=False)  # type: ignore
+        key = h.update(key.encode()).hexdigest()
         # store key/data here
         sql = '''INSERT INTO tracking_raw
                     (user_key, url, tracking_type)

{"golden_diff": "diff --git a/ckanext/tracking/middleware.py b/ckanext/tracking/middleware.py\n--- a/ckanext/tracking/middleware.py\n+++ b/ckanext/tracking/middleware.py\n@@ -27,7 +27,9 @@\n             request.environ.get('HTTP_ACCEPT_LANGUAGE', ''),\n             request.environ.get('HTTP_ACCEPT_ENCODING', ''),\n         ])\n-        key = hashlib.md5(key.encode()).hexdigest()\n+        # raises a type error on python<3.9\n+        h = hashlib.new('md5', usedforsecurity=False)  # type: ignore\n+        key = h.update(key.encode()).hexdigest()\n         # store key/data here\n         sql = '''INSERT INTO tracking_raw\n                     (user_key, url, tracking_type)\n", "issue": "Replacing MD5 hashing algorithm with SHA512\nIn file: common_middleware.py, method: __call__, the used hashing algorithm is no longer considered secure because it is possible to have collisions. This can lead to brute force attempt to find two or more inputs that produce the same hash. iCR suggested that safer alternative hash algorithms, such as SHA-256, SHA-512, SHA-3 are used. \n\nIn the file, MD5 is used to generate a key based on several parameters and inserted into the database as `user_key`. In that case, it's recommended to use a more secure, less collision prone hash function such as- SHA256 or SHA512.\n\n\n### Sponsorship and Support:\n\nThis work is done by the security researchers from OpenRefactory and is supported by the [Open Source Security Foundation (OpenSSF)](https://openssf.org/): [Project Alpha-Omega](https://alpha-omega.dev/). Alpha-Omega is a project partnering with open source software project maintainers to systematically find new, as-yet-undiscovered vulnerabilities in open source code - and get them fixed \u2013 to improve global software supply chain security.\n\nThe bug is found by running the Intelligent Code Repair (iCR) tool by OpenRefactory and then manually triaging the results.\n", "before_files": [{"content": "import hashlib\n\nfrom urllib.parse import unquote\n\nfrom ckan.model.meta import engine\nfrom ckan.common import request\nfrom ckan.types import Response\n\n\ndef track_request(response: Response) -> Response:\n    path = request.environ.get('PATH_INFO')\n    method = request.environ.get('REQUEST_METHOD')\n    if path == '/_tracking' and method == 'POST':\n        # wsgi.input is a BytesIO object\n        payload = request.environ['wsgi.input'].read().decode()\n        parts = payload.split('&')\n        data = {}\n        for part in parts:\n            k, v = part.split('=')\n            data[k] = unquote(v)\n\n        # we want a unique anonomized key for each user so that we do\n        # not count multiple clicks from the same user.\n        key = ''.join([\n            request.environ['HTTP_USER_AGENT'],\n            request.environ['REMOTE_ADDR'],\n            request.environ.get('HTTP_ACCEPT_LANGUAGE', ''),\n            request.environ.get('HTTP_ACCEPT_ENCODING', ''),\n        ])\n        key = hashlib.md5(key.encode()).hexdigest()\n        # store key/data here\n        sql = '''INSERT INTO tracking_raw\n                    (user_key, url, tracking_type)\n                    VALUES (%s, %s, %s)'''\n        engine.execute(  # type: ignore\n            sql, key, data.get('url'), data.get('type')\n            )\n    return response\n", "path": "ckanext/tracking/middleware.py"}], "after_files": [{"content": "import hashlib\n\nfrom urllib.parse import unquote\n\nfrom ckan.model.meta import engine\nfrom ckan.common import request\nfrom ckan.types import Response\n\n\ndef track_request(response: Response) -> Response:\n    path = request.environ.get('PATH_INFO')\n    method = request.environ.get('REQUEST_METHOD')\n    if path == '/_tracking' and method == 'POST':\n        # wsgi.input is a BytesIO object\n        payload = request.environ['wsgi.input'].read().decode()\n        parts = payload.split('&')\n        data = {}\n        for part in parts:\n            k, v = part.split('=')\n            data[k] = unquote(v)\n\n        # we want a unique anonomized key for each user so that we do\n        # not count multiple clicks from the same user.\n        key = ''.join([\n            request.environ['HTTP_USER_AGENT'],\n            request.environ['REMOTE_ADDR'],\n            request.environ.get('HTTP_ACCEPT_LANGUAGE', ''),\n            request.environ.get('HTTP_ACCEPT_ENCODING', ''),\n        ])\n        # raises a type error on python<3.9\n        h = hashlib.new('md5', usedforsecurity=False)  # type: ignore\n        key = h.update(key.encode()).hexdigest()\n        # store key/data here\n        sql = '''INSERT INTO tracking_raw\n                    (user_key, url, tracking_type)\n                    VALUES (%s, %s, %s)'''\n        engine.execute(  # type: ignore\n            sql, key, data.get('url'), data.get('type')\n            )\n    return response\n", "path": "ckanext/tracking/middleware.py"}]}