Search code examples
pythonubuntujupyter-notebookjupyter-labsha

Set Jupyter Lab password encrypted with SHA 256


I am currently running a Jupyter lab service on my Ubuntu 18.04 server. I have set the password on my lab using the following command:

$ jupyter notebook --generate-config
$ jupyter notebook password

It responds with the following output:

[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json

After that I add the config settings in .jupyter/jupyter_notebook_config.py file as follows:

c.NotebookApp.password = u'sha1:bcd259ccf...<my hashed password here>'

What I want is to get SHA 256 hashed password instead of SHA 1 purely because of the additional level of encryption offered by SHA 256 hash due to its bigger length.

I am wondering if there's a way to make this possible? Currently I have tried several options and none of them seem to work.


Solution

  • The notebook.auth module offers a function called passwd. The second argument is an algorithm. You can use that function to obtain a SHA 256 hashed password.

    """Parameters
    ----------
    passphrase : str
        Password to hash.  If unspecified, the user is asked to input
        and verify a password.
    algorithm : str
        Hashing algorithm to use (e.g, 'sha1' or any argument supported
        by :func:`hashlib.new`, or 'argon2').
    
    Returns
    -------
    hashed_passphrase : str
        Hashed password, in the format 'hash_algorithm:salt:passphrase_hash'."""
    
    from notebook.auth import passwd
    
    my_password = "spam-and-eggs"
    
    hashed_password = passwd(passphrase=my_password, algorithm='sha256')
    
    print(hashed_password)
    

    OUTPUT: sha256:128c5116bc40:94cfe1eef8703b657a7ef28af741fa05465c7a7355645a65040bd51bccc6039b