當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。