當前位置: 首頁>>代碼示例>>Python>>正文


Python KDF.scrypt方法代碼示例

本文整理匯總了Python中Crypto.Protocol.KDF.scrypt方法的典型用法代碼示例。如果您正苦於以下問題:Python KDF.scrypt方法的具體用法?Python KDF.scrypt怎麽用?Python KDF.scrypt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Crypto.Protocol.KDF的用法示例。


在下文中一共展示了KDF.scrypt方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: _decode_keyfile_json_v3

# 需要導入模塊: from Crypto.Protocol import KDF [as 別名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 別名]
def _decode_keyfile_json_v3(keyfile_json, password):
    crypto = keyfile_json['crypto']
    kdf = crypto['kdf']

    # Derive the encryption key from the password using the key derivation
    # function.
    if kdf == 'pbkdf2':
        derived_key = _derive_pbkdf_key(crypto, password)
    elif kdf == 'scrypt':
        derived_key = _derive_scrypt_key(crypto, password)
    else:
        raise TypeError("Unsupported key derivation function: {0}".format(kdf))

    # Validate that the derived key matchs the provided MAC
    ciphertext = decode_hex(crypto['ciphertext'])
    mac = keccak(derived_key[16:32] + ciphertext)

    expected_mac = decode_hex(crypto['mac'])

    if not hmac.compare_digest(mac, expected_mac):
        raise ValueError("MAC mismatch")

    # Decrypt the ciphertext using the derived encryption key to get the
    # private key.
    encrypt_key = derived_key[:16]
    cipherparams = crypto['cipherparams']
    iv = big_endian_to_int(decode_hex(cipherparams['iv']))

    private_key = decrypt_aes_ctr(ciphertext, encrypt_key, iv)

    return private_key


#
# Key derivation
# 
開發者ID:crytic,項目名稱:etheno,代碼行數:38,代碼來源:keyfile.py

示例2: _scrypt_hash

# 需要導入模塊: from Crypto.Protocol import KDF [as 別名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 別名]
def _scrypt_hash(password, salt, n, r, p, buflen):
    derived_key = scrypt(
        password,
        salt=salt,
        key_len=buflen,
        N=n,
        r=r,
        p=p,
        num_keys=1,
    )
    return derived_key 
開發者ID:crytic,項目名稱:etheno,代碼行數:13,代碼來源:keyfile.py

示例3: get_default_work_factor_for_kdf

# 需要導入模塊: from Crypto.Protocol import KDF [as 別名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 別名]
def get_default_work_factor_for_kdf(kdf):
    if kdf == 'pbkdf2':
        return 1000000
    elif kdf == 'scrypt':
        return 262144
    else:
        raise ValueError("Unsupported key derivation function: {0}".format(kdf)) 
開發者ID:crytic,項目名稱:etheno,代碼行數:9,代碼來源:keyfile.py

示例4: scrypt_hash

# 需要導入模塊: from Crypto.Protocol import KDF [as 別名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 別名]
def scrypt_hash(
        self, salt: str = "", key_length: int = 64, N: int = 14, r: int = 8, p: int = 1
    ):
        """Get Scrypt hash

        scrypt is a password-based key derivation function (PBKDF) created by Colin Percival. 
        The algorithm was specifically designed to make it costly to perform large-scale 
        custom hardware attacks by requiring large amounts of memory. In 2016, the scrypt 
        algorithm was published by IETF as RFC 7914.
        
        Args:
            salt (str, optional): A string of characters that modifies the hash. Defaults to "".
            key_length (int, optional): number of bytes to use when autogenerating new salts. Defaults to 64.
            N (int, optional): CPU/memory cost parameter. Defaults to 14.
            r (int, optional): The blocksize parameter. Defaults to 8.
            p (int, optional): Parallelization parameter;. Defaults to 1.
        
        Returns:
            Chepy: The Chepy object.

        Examples:
            >>> Chepy("abc").scrypt_hash(salt="", key_length=16).out()
            "f352f3374cf4e344dde4108b96985248"
        """
        assert N < 32, "N must be less than 32"
        self.state = _crypto_scrypt(
            self._convert_to_bytes(), salt=salt, key_len=key_length, N=2 ** N, r=r, p=p
        ).hex()
        return self 
開發者ID:securisec,項目名稱:chepy,代碼行數:31,代碼來源:hashing.py

示例5: test2

# 需要導入模塊: from Crypto.Protocol import KDF [as 別名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 別名]
def test2(self):

        for tv in self.data:
            try:
                output = scrypt(tv.P, tv.S, tv.dkLen, tv.N, tv.r, tv.p)
            except ValueError as e:
                if " 2 " in str(e) and tv.N >= 1048576:
                    import warnings
                    warnings.warn("Not enough memory to unit test scrypt() with N=1048576", RuntimeWarning)
                    continue
                else:
                    raise e
            self.assertEqual(output, tv.output) 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:15,代碼來源:test_KDF.py

示例6: test3

# 需要導入模塊: from Crypto.Protocol import KDF [as 別名]
# 或者: from Crypto.Protocol.KDF import scrypt [as 別名]
def test3(self):
        ref = scrypt(b("password"), b("salt"), 12, 16, 1, 1)

        # Same output, but this time split over 2 keys
        key1, key2 = scrypt(b("password"), b("salt"), 6, 16, 1, 1, 2)
        self.assertEqual((ref[:6], ref[6:]), (key1, key2))

        # Same output, but this time split over 3 keys
        key1, key2, key3 = scrypt(b("password"), b("salt"), 4, 16, 1, 1, 3)
        self.assertEqual((ref[:4], ref[4:8], ref[8:]), (key1, key2, key3)) 
開發者ID:bkerler,項目名稱:android_universal,代碼行數:12,代碼來源:test_KDF.py


注:本文中的Crypto.Protocol.KDF.scrypt方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。