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.
This commit is contained in:
Rodolphe Breard 2019-07-27 19:25:25 +02:00
parent e9817df97e
commit 591c151e85

View file

@ -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