django-aesfield icon indicating copy to clipboard operation
django-aesfield copied to clipboard

Error when generating new key

Open kamil-i2a opened this issue 7 years ago • 4 comments

Using python 3.4 and django 1.11 I get the following when I try to generate a key:

  File ".../aesfield/management/commands/generate_aes_keys.py", line 16, in generate_key
    return os.urandom(byte_length).encode('hex')
AttributeError: 'bytes' object has no attribute 'encode'

kamil-i2a avatar Jan 05 '18 08:01 kamil-i2a

We're officially only testing against Python 2.7, 3.5 and 3.6. Unfortunately, I don't have time to also maintain Python 3.4, so if you need support for it please feel free to submit a pull request and I'm very happy to merge it.

EnTeQuAk avatar Jan 08 '18 10:01 EnTeQuAk

@EnTeQuAk Well, I'm getting the same error with Python 3.6.2.

Steps to reproduce:

  • start a new Django project in Python 3.6.2 virtualenv
  • install django-aesfield
  • add to settings:
AES_KEYS = {
    'default': os.path.join(os.path.dirname(__file__), 'sample.key')
}
  • python manage.py generate_aes_keys

kamil-i2a avatar Jan 09 '18 09:01 kamil-i2a

@EnTeQuAk The management command really doesn't work in Python 3, because os.urandom produces bytes object which doesn't have decode method.

One possible solution would be something like binascii.hexlify(os.urandom(byte_length)).decode(). It gives unicode string in Python 2 and string in Python 3.

dspechnikov avatar Mar 02 '18 16:03 dspechnikov

Ugh, indeed. I'll have a look at this shortly.

EnTeQuAk avatar Mar 20 '18 09:03 EnTeQuAk