使用Python passlib创建密码

时间:2014-02-25 20:19:57

标签: python

我正在尝试使用以下其他用户发布的以下问题的答案:

>>> # import the hash algorithm
>>> from passlib.hash import sha256_crypt

>>> # generate new salt, and hash a password
>>> hash = sha256_crypt.encrypt("toomanysecrets")
>>> hash

但是当我输入from passlib.hash import sha256_crypt时,我收到以下错误:

Traceback (most recent call last):   File "<stdin>", line 1, in
<module> ImportError: No module named passlib.hash
>>>

我已经完成了pip install passlib。有什么想法吗?

运行结果:pip install passlib

Downloading/unpacking passlib   Downloading passlib-1.6.2.tar.gz (408kB): 408kB downloaded   Running setup.py egg_info for package passlib

Installing collected packages: passlib   Running setup.py install for passlib
    error: could not create '/Library/Python/2.7/site-packages/passlib': Permission denied
    Complete output from command /usr/bin/python -c "import setuptools;__file__='/private/var/folders/2t/1yj5qss57xz8sb7p9wymtkdr0000gn/T/pip_build_<user>/passlib/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/2t/1yj5qss57xz8sb7p9wymtkdr0000gn/T/pip-epiHNK-record/install-record.txt
--single-version-externally-managed:
    running install

running build

running build_py

creating build

creating build/lib

creating build/lib/passlib

copying passlib/__init__.py -> build/lib/passlib

copying passlib/apache.py -> build/lib/passlib

copying passlib/apps.py -> build/lib/passlib

copying passlib/context.py -> build/lib/passlib

copying passlib/exc.py -> build/lib/passlib

copying passlib/hash.py -> build/lib/passlib

copying passlib/hosts.py -> build/lib/passlib

copying passlib/ifc.py -> build/lib/passlib

copying passlib/registry.py -> build/lib/passlib

copying passlib/win32.py -> build/lib/passlib

creating build/lib/passlib/ext

copying passlib/ext/__init__.py -> build/lib/passlib/ext

creating build/lib/passlib/ext/django

copying passlib/ext/django/__init__.py -> build/lib/passlib/ext/django

copying passlib/ext/django/models.py -> build/lib/passlib/ext/django

copying passlib/ext/django/utils.py -> build/lib/passlib/ext/django

creating build/lib/passlib/handlers

copying passlib/handlers/__init__.py -> build/lib/passlib/handlers

copying passlib/handlers/bcrypt.py -> build/lib/passlib/handlers

copying passlib/handlers/cisco.py -> build/lib/passlib/handlers

copying passlib/handlers/des_crypt.py -> build/lib/passlib/handlers

copying passlib/handlers/digests.py -> build/lib/passlib/handlers

copying passlib/handlers/django.py -> build/lib/passlib/handlers

copying passlib/handlers/fshp.py -> build/lib/passlib/handlers

copying passlib/handlers/ldap_digests.py -> build/lib/passlib/handlers

copying passlib/handlers/md5_crypt.py -> build/lib/passlib/handlers

copying passlib/handlers/misc.py -> build/lib/passlib/handlers

copying passlib/handlers/mssql.py -> build/lib/passlib/handlers

copying passlib/handlers/mysql.py -> build/lib/passlib/handlers

copying passlib/handlers/oracle.py -> build/lib/passlib/handlers

copying passlib/handlers/pbkdf2.py -> build/lib/passlib/handlers

copying passlib/handlers/phpass.py -> build/lib/passlib/handlers

copying passlib/handlers/postgres.py -> build/lib/passlib/handlers

copying passlib/handlers/roundup.py -> build/lib/passlib/handlers

copying passlib/handlers/scram.py -> build/lib/passlib/handlers

copying passlib/handlers/sha1_crypt.py -> build/lib/passlib/handlers

copying passlib/handlers/sha2_crypt.py -> build/lib/passlib/handlers

copying passlib/handlers/sun_md5_crypt.py -> build/lib/passlib/handlers

copying passlib/handlers/windows.py -> build/lib/passlib/handlers

creating build/lib/passlib/tests

copying passlib/tests/__init__.py -> build/lib/passlib/tests

copying passlib/tests/__main__.py -> build/lib/passlib/tests

copying passlib/tests/_test_bad_register.py -> build/lib/passlib/tests

copying passlib/tests/backports.py -> build/lib/passlib/tests

copying passlib/tests/test_apache.py -> build/lib/passlib/tests

copying passlib/tests/test_apps.py -> build/lib/passlib/tests

copying passlib/tests/test_context.py -> build/lib/passlib/tests

copying passlib/tests/test_context_deprecated.py -> build/lib/passlib/tests

copying passlib/tests/test_ext_django.py -> build/lib/passlib/tests

copying passlib/tests/test_handlers.py -> build/lib/passlib/tests

copying passlib/tests/test_handlers_bcrypt.py -> build/lib/passlib/tests

copying passlib/tests/test_handlers_django.py -> build/lib/passlib/tests

copying passlib/tests/test_hosts.py -> build/lib/passlib/tests

copying passlib/tests/test_registry.py -> build/lib/passlib/tests

copying passlib/tests/test_utils.py -> build/lib/passlib/tests

copying passlib/tests/test_utils_crypto.py -> build/lib/passlib/tests

copying passlib/tests/test_utils_handlers.py -> build/lib/passlib/tests

copying passlib/tests/test_win32.py -> build/lib/passlib/tests

copying passlib/tests/tox_support.py -> build/lib/passlib/tests

copying passlib/tests/utils.py -> build/lib/passlib/tests

creating build/lib/passlib/utils

copying passlib/utils/__init__.py -> build/lib/passlib/utils

copying passlib/utils/compat.py -> build/lib/passlib/utils

copying passlib/utils/des.py -> build/lib/passlib/utils

copying passlib/utils/handlers.py -> build/lib/passlib/utils

copying passlib/utils/md4.py -> build/lib/passlib/utils

copying passlib/utils/pbkdf2.py -> build/lib/passlib/utils

creating build/lib/passlib/utils/_blowfish

copying passlib/utils/_blowfish/__init__.py -> build/lib/passlib/utils/_blowfish

copying passlib/utils/_blowfish/_gen_files.py -> build/lib/passlib/utils/_blowfish

copying passlib/utils/_blowfish/base.py -> build/lib/passlib/utils/_blowfish

copying passlib/utils/_blowfish/unrolled.py -> build/lib/passlib/utils/_blowfish

creating build/lib/passlib/_setup

copying passlib/_setup/__init__.py -> build/lib/passlib/_setup

copying passlib/_setup/docdist.py -> build/lib/passlib/_setup

copying passlib/_setup/stamp.py -> build/lib/passlib/_setup

copying passlib/tests/sample1.cfg -> build/lib/passlib/tests

copying passlib/tests/sample1b.cfg -> build/lib/passlib/tests

copying passlib/tests/sample1c.cfg -> build/lib/passlib/tests

copying passlib/tests/sample_config_1s.cfg -> build/lib/passlib/tests

running install_lib

creating /Library/Python/2.7/site-packages/passlib

error: could not create '/Library/Python/2.7/site-packages/passlib': Permission denied

---------------------------------------- Cleaning up... Command /usr/bin/python -c "import setuptools;__file__='/private/var/folders/2t/1yj5qss57xz8sb7p9wymtkdr0000gn/T/pip_build_<user>/passlib/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/2t/1yj5qss57xz8sb7p9wymtkdr0000gn/T/pip-epiHNK-record/install-record.txt
--single-version-externally-managed failed with error code 1 in /private/var/folders/2t/1yj5qss57xz8sb7p9wymtkdr0000gn/T/pip_build_<user>/passlib Storing complete log in /Users/<user>/Library/Logs/pip.log

3 个答案:

答案 0 :(得分:7)

您收到了“拒绝权限”错误。尝试

sudo pip install passlib

pip install --user passlib

答案 1 :(得分:1)

安装passlib被拒绝,因此需要sudo运行:

sudo pip install passlib

归功于@Drewness,@ wnnmaw和@cmd

答案 2 :(得分:1)

看起来你现在可以使用非常光滑的东西:

sqlalchemy_utils.types.password.PasswordType

然后SQLAlchemy将为您进行编码/解码。例如:

class User(Base):
    password = Column(PasswordType())

然后设置和获取是非常标准的python代码,并且所有实际操作都在幕后发生:

user = User()

# set password
user.password = 'foo'   # '$5$rounds=80000$H.............'

# check password
user.password == 'foo'  # true