You can use a Python library. (This answer has been updated from the
comments and for Python3 in 2024). On my Fedora system I did:
sudo dnf search bcrypt
(the sudo is just to avoid wasting space for a user dnf cache) and from the result I can see there is a Python3 package:
python3-bcrypt.x86_64 : Modern password hashing for your software and your servers
Install this and list the files in the package:
sudo dnf install python3-bcrypt
rpm -ql python3-bcrypt
This shows there is a file /usr/lib64/python*/site-packages/bcrypt/__init__.py
so I can get the documentation with
pydoc bcrypt
This shows me enough to write the following command which will hash the string "password"
:
$ python -c 'import bcrypt; print(bcrypt.hashpw("password".encode(), bcrypt.gensalt()).decode())'
$2b$10$0W/r1Q43/tF0yW5PfBIB5Ouc7TRAG7TEWnm5OUt94iICgg2N8U.n6
For better security, you can get the program to prompt for the cleartext
password and read it from the terminal without echoing:
python -c 'import bcrypt, getpass;
print(bcrypt.hashpw(getpass.getpass().encode(), bcrypt.gensalt()).decode())'