Search code examples
pythonpython-3.xhashlib

Python 3: Error - Hashlib has no attribute 'SHA256'


import hashlib    
h = hashlib.SHA256(string)

Error raised: AttributeError: module 'hashlib' has no attribute 'SHA256'

I found this similar to my question:

But my output for "import hashlib" and print(dir(hashlib)) is:

['__all__', '__builtin_constructor_cache', '__builtins__', '__cached__', 
'__doc__', '__file__', '__get_builtin_constructor', '__loader__', 
'__name__', '__package__', '__spec__', '_hashlib', 'algorithms_available', 
'algorithms_guaranteed', 'blake2b', 'blake2s', 'md5', 'new', 'pbkdf2_hmac', 
'sha1', 'sha224', 'sha256', 'sha384', 'sha3_224', 'sha3_256', 'sha3_384', 
'sha3_512', 'sha512', 'shake_128', 'shake_256']

Solution

  • Python is case-sensitive, meaning the difference between capital letters (upper-case) to lower-case (non capital) letters matters. Use the names as they appear in your printout, for example sha256 instead of SHA256

    So the correct code will be

    import hashlib    
    h = hashlib.sha256(string)