当前位置: 首页>>代码示例>>Python>>正文


Python pbkdf2.PBKDF2HMAC属性代码示例

本文整理汇总了Python中cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC属性的典型用法代码示例。如果您正苦于以下问题:Python pbkdf2.PBKDF2HMAC属性的具体用法?Python pbkdf2.PBKDF2HMAC怎么用?Python pbkdf2.PBKDF2HMAC使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在cryptography.hazmat.primitives.kdf.pbkdf2的用法示例。


在下文中一共展示了pbkdf2.PBKDF2HMAC属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _get_key

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_key():
    if this.key:
        return this.key

    secret = getpass.getpass()
    try:
        salt = config().get('Security', 'salt')
    except NoOptionError:
        salt = base64.urlsafe_b64encode(os.urandom(16))
        config().set('Security', 'salt', salt)

    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
        backend=default_backend()
    )
    this.key = base64.urlsafe_b64encode(kdf.derive(secret))
    return this.key 
开发者ID:rtshome,项目名称:pgrepup,代码行数:22,代码来源:crypt.py

示例2: _get_key

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_key(self, alg, key, p2s, p2c):
        if isinstance(key, bytes):
            plain = key
        else:
            plain = key.encode('utf8')
        salt = bytes(self.name.encode('utf8')) + b'\x00' + p2s

        if self.hashsize == 256:
            hashalg = hashes.SHA256()
        elif self.hashsize == 384:
            hashalg = hashes.SHA384()
        elif self.hashsize == 512:
            hashalg = hashes.SHA512()
        else:
            raise ValueError('Unknown Hash Size')

        kdf = PBKDF2HMAC(algorithm=hashalg, length=_inbytes(self.keysize),
                         salt=salt, iterations=p2c, backend=self.backend)
        rk = kdf.derive(plain)
        if _bitsize(rk) != self.keysize:
            raise InvalidJWEKeyLength(self.keysize, len(rk))
        return JWK(kty="oct", use="enc", k=base64url_encode(rk)) 
开发者ID:latchset,项目名称:jwcrypto,代码行数:24,代码来源:jwa.py

示例3: pbkdf2

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def pbkdf2(password, salt, iterations, dklen=0, digest=None):
    """
    Implements PBKDF2 with the same API as Django's existing
    implementation, using cryptography.

    :type password: any
    :type salt: any
    :type iterations: int
    :type dklen: int
    :type digest: cryptography.hazmat.primitives.hashes.HashAlgorithm
    """
    if digest is None:
        digest = settings.CRYPTOGRAPHY_DIGEST
    if not dklen:
        dklen = digest.digest_size
    password = force_bytes(password)
    salt = force_bytes(salt)
    kdf = PBKDF2HMAC(
        algorithm=digest,
        length=dklen,
        salt=salt,
        iterations=iterations,
        backend=settings.CRYPTOGRAPHY_BACKEND)
    return kdf.derive(password) 
开发者ID:georgemarshall,项目名称:django-cryptography,代码行数:26,代码来源:crypto.py

示例4: encrypt

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def encrypt(input_str, key, iterations=100000):
    """Basic encryption with a predefined key. Its purpose is to protect not very important data, just to avoid
    saving them as plaintext."""

    salt = b'D9\x82\xbfSibW(\xb1q\xeb\xd1\x84\x118'
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=iterations,
        backend=default_backend()
    )
    key = base64.urlsafe_b64encode(kdf.derive(key.encode('utf-8')))
    fer = Fernet(key)
    h = fer.encrypt(input_str.encode('utf-8'))
    h = h.hex()
    return h 
开发者ID:Bertrand256,项目名称:dash-masternode-tool,代码行数:19,代码来源:app_utils.py

示例5: decrypt

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def decrypt(input_str, key, iterations=100000):
    try:
        input_str = binascii.unhexlify(input_str)
        salt = b'D9\x82\xbfSibW(\xb1q\xeb\xd1\x84\x118'
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32,
            salt=salt,
            iterations=iterations,
            backend=default_backend()
        )
        key = base64.urlsafe_b64encode(kdf.derive(key.encode('utf-8')))
        fer = Fernet(key)
        h = fer.decrypt(input_str)
        h = h.decode('utf-8')
    except:
        raise
    return h 
开发者ID:Bertrand256,项目名称:dash-masternode-tool,代码行数:20,代码来源:app_utils.py

示例6: decrypt

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def decrypt(data, password):
    """Decrypts data using the password.
    Decrypts the data using the provided password using the cryptography module.
    If the pasword or data is incorrect this will return None. 
    """

    password = bytes(password)

    #Salt is equal to password as we want the encryption to be reversible only
    #using the password itself
    kdf = PBKDF2HMAC(algorithm=hashes.AES(),
                     length=32,
                     salt=bytes(password),
                     iterations=100000,
                     backend=default_backend())

    key = base64.urlsafe_b64encode(kdf.derive(password))
    f = Fernet(key)
    token = f.decrypt(data)
    return token 
开发者ID:nukeop,项目名称:hide.py,代码行数:22,代码来源:hide.py

示例7: _get_key

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_key(self, salt, password):
        """Returns an encryption key.

        Args:
            salt (bytes): a salt used during the encryption.
            password (bytes): the password used to decrypt/encrypt
                the message.

        Returns:
            Bytes with the encryption key.
        """
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32,
            salt=salt,
            iterations=100000,
            backend=backends.default_backend()
        )
        return base64.urlsafe_b64encode(
            kdf.derive(password)) 
开发者ID:google,项目名称:timesketch,代码行数:22,代码来源:crypto.py

示例8: __init__

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def __init__(self, key=None):

        if key is None:
            key = os.urandom(32)

        if isinstance(key, str):
            key_as_bytes = key.encode()
        else:
            key_as_bytes = key

        salt_string = "constant so that session cookies carry across restarts of this app"
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32,
            salt=salt_string.encode(),
            iterations=100000,
            backend=default_backend(),
        )
        hashed_key = kdf.derive(key_as_bytes)
        key_bytes = base64.encodebytes(hashed_key)
        self.cipher = Fernet(key=key_bytes) 
开发者ID:codeforpdx,项目名称:recordexpungPDX,代码行数:23,代码来源:crypto.py

示例9: __derive_key

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def __derive_key(self, salt: bytes, iterations: int) -> bytes:
        """Derive a secret key from a given passphrase and salt."""
        kdf = PBKDF2HMAC(algorithm=hashes.SHA256(),
                         length=32,
                         salt=salt,
                         iterations=iterations,
                         backend=default_backend())
        return urlsafe_b64encode(kdf.derive(self.passphrase.encode())) 
开发者ID:smallwat3r,项目名称:shhh,代码行数:10,代码来源:utils.py

示例10: get_encryptor

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def get_encryptor(salt, key):
    generated_key = PBKDF2HMAC(algorithm=hashes.SHA256(),
                               length=32,
                               salt=bytes(salt, 'utf-8'),
                               iterations=2 ** 20,
                               backend=default_backend())
    return Fernet(base64.urlsafe_b64encode(generated_key.derive(bytes(key, 'utf-8')))) 
开发者ID:mitre,项目名称:caldera,代码行数:9,代码来源:file_decryptor.py

示例11: _get_encryptor

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def _get_encryptor(self):
        generated_key = PBKDF2HMAC(algorithm=hashes.SHA256(),
                                   length=32,
                                   salt=bytes(self.get_config('crypt_salt'), 'utf-8'),
                                   iterations=2 ** 20,
                                   backend=default_backend())
        return Fernet(base64.urlsafe_b64encode(generated_key.derive(bytes(self.get_config('encryption_key'), 'utf-8')))) 
开发者ID:mitre,项目名称:caldera,代码行数:9,代码来源:file_svc.py

示例12: pbkdf2_sha256

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def pbkdf2_sha256(password, salt, iters):
            """PBKDF2 with HMAC-SHA256"""
            ctx = pbkdf2.PBKDF2HMAC(hashes.SHA256(), 32, salt, iters, default_backend())
            return ctx.derive(password) 
开发者ID:BasioMeusPuga,项目名称:Lector,代码行数:6,代码来源:rarfile.py

示例13: configure

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def configure(self):
        backend = self.configured_data['BACKEND']
        digest = self.configured_data['DIGEST']
        salt = self.configured_data['SALT']
        # Key Derivation Function
        kdf = pbkdf2.PBKDF2HMAC(
            algorithm=digest,
            length=digest.digest_size,
            salt=salt,
            iterations=30000,
            backend=backend,
        )
        self.configured_data['KEY'] = kdf.derive(
            force_bytes(self.configured_data['KEY'] or settings.SECRET_KEY))
        return self.configured_data 
开发者ID:georgemarshall,项目名称:django-cryptography,代码行数:17,代码来源:conf.py

示例14: generatefernet

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def generatefernet(self):
        """Return a fernet object used to encrypt/decrypt the password"""
        kdf = PBKDF2HMAC(
                algorithm = hashes.SHA256(),
                length = 32,
                salt = self.salt,
                iterations = 100000,
                backend = default_backend())
        key = base64.urlsafe_b64encode(kdf.derive(config.SALT.encode()))
        return Fernet(key) 
开发者ID:LibrIT,项目名称:passhport,代码行数:12,代码来源:passentry.py

示例15: decrypt

# 需要导入模块: from cryptography.hazmat.primitives.kdf import pbkdf2 [as 别名]
# 或者: from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC [as 别名]
def decrypt(message_encrypted, secret_key):
    message_salt = message_encrypted[-24:]
    message_payload = message_encrypted[:-24]
    salt = base64.b64decode(message_salt)
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=100000,
        backend=backend
    )
    key = base64.urlsafe_b64encode(kdf.derive(bytes(secret_key, encoding='utf8')))
    f = Fernet(key)
    return f.decrypt(bytes(message_payload, encoding='latin1')).decode('utf8') 
开发者ID:jet-admin,项目名称:jet-bridge,代码行数:16,代码来源:crypt.py


注:本文中的cryptography.hazmat.primitives.kdf.pbkdf2.PBKDF2HMAC属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。