From 591c151e855cf3d029a9839a8b817cb3efa2b5da Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Sat, 27 Jul 2019 19:25:25 +0200 Subject: [PATCH] Use Django's force_bytes() instead of bytes() Unlike bytes() which accepts only a string, force_bytes() accept all sort of input type. This is required since SECRET_KEY may be either a string or bytes. --- pwdb/models.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pwdb/models.py b/pwdb/models.py index 7fd5229..f38d80c 100644 --- a/pwdb/models.py +++ b/pwdb/models.py @@ -3,6 +3,7 @@ from cryptography.hazmat.primitives.kdf.hkdf import HKDF from cryptography.hazmat.primitives import hashes, padding from cryptography.hazmat.backends import default_backend from django.utils.translation import ugettext_lazy as _ +from django.utils.encoding import force_bytes from django.conf import settings from django.db import models from neluser.models import NelUser @@ -34,7 +35,7 @@ class SharedPassword(models.Model): info=None, ) key = key or settings.SECRET_KEY - key = bytes(key, encoding=ENCODING) + key = force_bytes(key, encoding=ENCODING) return hkdf.derive(key) @staticmethod @@ -44,7 +45,7 @@ class SharedPassword(models.Model): @staticmethod def padd_password(clear_password): - clear_password = bytes(clear_password, encoding=ENCODING) + clear_password = force_bytes(clear_password, encoding=ENCODING) padder = padding.PKCS7(BLOCK_SIZE).padder() padded_password = padder.update(clear_password) + padder.finalize() return padded_password