prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <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.
  
  </issue>
  <code>
  [start of ckanext/tracking/middleware.py]
  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 of ckanext/tracking/middleware.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: 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)
  
verification_info: {"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"}]}