本文整理匯總了Python中passlib.context.CryptContext方法的典型用法代碼示例。如果您正苦於以下問題:Python context.CryptContext方法的具體用法?Python context.CryptContext怎麽用?Python context.CryptContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類passlib.context
的用法示例。
在下文中一共展示了context.CryptContext方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def __init__(self, app):
"""
Create a passlib CryptContext.
Args:
password_hash(str): The name of a valid passlib password hash.
Examples: ``'bcrypt', 'pbkdf2_sha512', 'sha512_crypt' or 'argon2'``.
Example:
``password_manager = PasswordManager('bcrypt')``
"""
self.app = app
self.user_manager = app.user_manager
# Create a passlib CryptContext
self.password_crypt_context = CryptContext(
schemes=self.user_manager.USER_PASSLIB_CRYPTCONTEXT_SCHEMES,
**self.user_manager.USER_PASSLIB_CRYPTCONTEXT_KEYWORDS)
示例2: _get_pwd_context
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def _get_pwd_context(app):
pw_hash = cv("PASSWORD_HASH", app=app)
schemes = cv("PASSWORD_SCHEMES", app=app)
deprecated = cv("DEPRECATED_PASSWORD_SCHEMES", app=app)
if pw_hash not in schemes:
allowed = ", ".join(schemes[:-1]) + " and " + schemes[-1]
raise ValueError(
"Invalid password hashing scheme %r. Allowed values are %s"
% (pw_hash, allowed)
)
cc = CryptContext(
schemes=schemes,
default=pw_hash,
deprecated=deprecated,
**cv("PASSWORD_HASH_PASSLIB_OPTIONS", app=app),
)
return cc
示例3: test_22_to_string
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def test_22_to_string(self):
"""test to_string() method"""
pa = CryptPolicy(**self.sample_config_5pd)
s = pa.to_string() # NOTE: can't compare string directly, ordering etc may not match
pb = CryptPolicy.from_string(s)
self.assertEqual(pb.to_dict(), self.sample_config_5pd)
s = pa.to_string(encoding="latin-1")
self.assertIsInstance(s, bytes)
#===================================================================
#
#===================================================================
#=============================================================================
# CryptContext
#=============================================================================
示例4: test_01_replace
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def test_01_replace(self):
"""test replace()"""
cc = CryptContext(["md5_crypt", "bsdi_crypt", "des_crypt"])
self.assertIs(cc.policy.get_handler(), hash.md5_crypt)
cc2 = cc.replace()
self.assertIsNot(cc2, cc)
# NOTE: was not able to maintain backward compatibility with this...
##self.assertIs(cc2.policy, cc.policy)
cc3 = cc.replace(default="bsdi_crypt")
self.assertIsNot(cc3, cc)
# NOTE: was not able to maintain backward compatibility with this...
##self.assertIs(cc3.policy, cc.policy)
self.assertIs(cc3.policy.get_handler(), hash.bsdi_crypt)
示例5: test_12_hash_needs_update
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def test_12_hash_needs_update(self):
"""test hash_needs_update() method"""
cc = CryptContext(**self.sample_policy_1)
# check deprecated scheme
self.assertTrue(cc.hash_needs_update('9XXD4trGYeGJA'))
self.assertFalse(cc.hash_needs_update('$1$J8HC2RCr$HcmM.7NxB2weSvlw2FgzU0'))
# check min rounds
self.assertTrue(cc.hash_needs_update('$5$rounds=1999$jD81UCoo.zI.UETs$Y7qSTQ6mTiU9qZB4fRr43wRgQq4V.5AAf7F97Pzxey/'))
self.assertFalse(cc.hash_needs_update('$5$rounds=2000$228SSRje04cnNCaQ$YGV4RYu.5sNiBvorQDlO0WWQjyJVGKBcJXz3OtyQ2u8'))
# check max rounds
self.assertFalse(cc.hash_needs_update('$5$rounds=3000$fS9iazEwTKi7QPW4$VasgBC8FqlOvD7x2HhABaMXCTh9jwHclPA9j5YQdns.'))
self.assertTrue(cc.hash_needs_update('$5$rounds=3001$QlFHHifXvpFX4PLs$/0ekt7lSs/lOikSerQ0M/1porEHxYq7W/2hdFpxA3fA'))
#===================================================================
# border cases
#===================================================================
示例6: get_user_category
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def get_user_category(self, user):
"""
Helper for hashing passwords per-user --
figure out the CryptContext category for specified Django user object.
.. note::
This may be overridden via PASSLIB_GET_CATEGORY django setting
"""
if user.is_superuser:
return "superuser"
elif user.is_staff:
return "staff"
else:
return None
#=============================================================================
# patch control
#=============================================================================
示例7: hash
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def hash(self, password):
"""
Hash the password to store it. If not configured for hashes, this is a no-op.
:param password:
:return:
"""
logger.info('Checking hash on password')
if self.do_hash:
logger.info('Hashing password prior to storage')
context = dict(schemes=['argon2'])
cc = CryptContext(**context)
password = cc.hash(password)
else:
logger.info('No hash requirement set in config')
return password
示例8: _get_hashing_context
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def _get_hashing_context(app):
schemes = cv("HASHING_SCHEMES", app=app)
deprecated = cv("DEPRECATED_HASHING_SCHEMES", app=app)
return CryptContext(schemes=schemes, deprecated=deprecated)
示例9: create_password_crypt_context
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def create_password_crypt_context(self, authc_settings):
context = dict(schemes=[authc_settings.preferred_algorithm])
context.update(authc_settings.preferred_algorithm_context)
return CryptContext(**context)
示例10: crypt_context
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def crypt_context():
return CryptContext(schemes=['sha256_crypt'])
示例11: _hash_password
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def _hash_password(self, password):
userctx = CryptContext(schemes=["sha256_crypt", "md5_crypt"])
self.password = userctx.hash(password)
示例12: _verify_password
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def _verify_password(self, password):
userctx = CryptContext(schemes=["sha256_crypt", "md5_crypt"])
return userctx.verify(password, self.password)
示例13: get_crypt_context
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def get_crypt_context():
"Return the default signac crypto context."
return CryptContext(schemes=('bcrypt', ))
示例14: _get_hashing_context
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def _get_hashing_context(self, app: FlaskUnchained) -> CryptContext:
"""
Get the token hashing (and verifying) context.
"""
return CryptContext(schemes=app.config.SECURITY_HASHING_SCHEMES,
deprecated=app.config.SECURITY_DEPRECATED_HASHING_SCHEMES)
示例15: _get_pwd_context
# 需要導入模塊: from passlib import context [as 別名]
# 或者: from passlib.context import CryptContext [as 別名]
def _get_pwd_context(self, app: FlaskUnchained) -> CryptContext:
"""
Get the password hashing context.
"""
pw_hash = app.config.SECURITY_PASSWORD_HASH
schemes = app.config.SECURITY_PASSWORD_SCHEMES
if pw_hash not in schemes:
allowed = (', '.join(schemes[:-1]) + ' and ' + schemes[-1])
raise ValueError(f'Invalid password hashing scheme {pw_hash}. '
f'Allowed values are {allowed}.')
return CryptContext(schemes=schemes, default=pw_hash,
deprecated=app.config.SECURITY_DEPRECATED_PASSWORD_SCHEMES)