You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This leads to an error, however, when the login route hits the hash_token function in crypto.py:
def hash_token(token):
'''
Calculates the hash of a token.
input is unhexlified
token must contain an even number of hex digits or a binascii.Error
exception will be raised
'''
digest = hashes.Hash(sha(), backend=default_backend())
digest.update(binascii.unhexlify(token))
return binascii.hexlify(digest.finalize()).decode()
specifically when calling hashes.Hash.
My fix was to change settings.py to leverage the hashes being imported into crypto.py: cryptography.hazmat.primitives like so:
Is this something that needs to be updated or am I missing something in how to correctly use the suggested code?
Full error:
Internal Server Error: /login/
Traceback (most recent call last):
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
return view_func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/tutorial/tutorial/views.py", line 34, in post
return super(LoginView, self).post(request, format=None)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/knox/views.py", line 63, in post
instance, token = AuthToken.objects.create(request.user, token_ttl)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/knox/models.py", line 14, in create
digest = crypto.hash_token(token)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/sarah/Projects/django-rest-tutorial/venv/lib/python3.11/site-packages/knox/crypto.py", line 26, in hash_token
digest = hashes.Hash(sha(), backend=default_backend())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Expected instance of hashes.HashAlgorithm.
The text was updated successfully, but these errors were encountered:
@stocktons There are upcoming changes in Knox, not yet on PyPI. Previously, it used the cryptography package, but the new update employs hashlib. The 'hashlib.sha512' suggestion in 'docs/settings.md' is for this new changes. And your edit using 'cryptography.hazmat.primitives.hashes.SHA512' is accurate. Since the update isn't released yet and is just on GitHub (Also you can install the GitHub version)
The example given in the docs/settings.md uses a hashlib algorithm:
This leads to an error, however, when the login route hits the
hash_token
function incrypto.py
:specifically when calling
hashes.Hash
.My fix was to change settings.py to leverage the hashes being imported into
crypto.py
:cryptography.hazmat.primitives
like so:Is this something that needs to be updated or am I missing something in how to correctly use the suggested code?
Full error:
The text was updated successfully, but these errors were encountered: