Search code examples
pythonhashlib

Sha 256 function from hashlib returns NoneType


I have a variable c that contains a string gained from tkinters Entry object. I want to apply a hash function to this string and than transmith it to some other computer.

I've already tried encoding my string with bytes function.

bytes(c)

or encode function

Else i tried using "cryptography" library and got the same error.

I used hashlib library and created a variable "digest" that contains #hashlib.sha256() .

digest = hashlib.sha256()

Than i applied an update function as it was said in documentation.

c = digest.update(c)                      

c = c.digest()

And i've got an Error

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1547, in __call__
    return self.func(*args)
  File "C:\Users\HTTPS\Desktop\registration_form.py", line 79, in register
    c = c.digest()
AttributeError: 'NoneType' object has no attribute 'digest'

Solution

  • c = digest.update(c)                      
    

    ...should just be:

    digest.update(c)                      
    

    Operations which are called for their side effects in Python return None, to make it clear to callers that they're mutating the object on which they're called rather than returning a new value.