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


Python scrypt.Scrypt方法代碼示例

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


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

示例1: scrypt_generate

# 需要導入模塊: from cryptography.hazmat.primitives.kdf import scrypt [as 別名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 別名]
def scrypt_generate(message):

    backend = default_backend()
    salt = os.urandom(16)

    kdf = Scrypt(
        salt=salt,
        length=32,
        n=2**14,
        r=8,
        p=1,
        backend=backend
    )

    key = kdf.derive(message.encode())

    print(hexlify(salt).decode(), hexlify(key).decode()) 
開發者ID:fportantier,項目名稱:vulpy,代碼行數:19,代碼來源:scrypt-generate.py

示例2: password_set

# 需要導入模塊: from cryptography.hazmat.primitives.kdf import scrypt [as 別名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 別名]
def password_set(username, password):

    backend = default_backend()
    salt = os.urandom(16)

    kdf = Scrypt(
        salt=salt,
        length=32,
        n=2**14,
        r=8,
        p=1,
        backend=backend
    )

    key = kdf.derive(password.encode())

    conn = sqlite3.connect('db_users.sqlite')
    conn.set_trace_callback(print)
    conn.row_factory = sqlite3.Row
    c = conn.cursor()

    print('Changing password for', username)
    c.execute("UPDATE users SET password = ?, salt = ? WHERE username = ?", (hexlify(key).decode(), hexlify(salt).decode(), username))
    conn.commit() 
開發者ID:fportantier,項目名稱:vulpy,代碼行數:26,代碼來源:libuser.py

示例3: scrypt

# 需要導入模塊: from cryptography.hazmat.primitives.kdf import scrypt [as 別名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 別名]
def scrypt(passphrase, salt, scrypt_n=1<<13, scrypt_r=16, scrypt_p=1):
    kdf = Scrypt(salt, length=32, n=scrypt_n, r=scrypt_r, p=scrypt_p, backend=default_backend())
    return kdf.derive(passphrase) 
開發者ID:lbryio,項目名稱:torba,代碼行數:5,代碼來源:hash.py

示例4: login

# 需要導入模塊: from cryptography.hazmat.primitives.kdf import scrypt [as 別名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 別名]
def login(username, password, **kwargs):

    conn = sqlite3.connect('db_users.sqlite')
    conn.set_trace_callback(print)
    conn.row_factory = sqlite3.Row
    c = conn.cursor()

    user = c.execute("SELECT * FROM users WHERE username = ?", (username,)).fetchone()

    if not user:
        #print('The user doesnt exists')
        return False

    backend = default_backend()

    kdf = Scrypt(
        salt=unhexlify(user['salt']),
        length=32,
        n=2**14,
        r=8,
        p=1,
        backend=backend
    )

    try:
        kdf.verify(password.encode(), unhexlify(user['password']))
        #print('valid')
        return username
    except InvalidKey:
        #print('invalid1')
        return False
    except Exception as e:
        #print('invalid2', e)
        return False

    #print('No deberia haber llegado aca')
    return False 
開發者ID:fportantier,項目名稱:vulpy,代碼行數:39,代碼來源:libuser.py

示例5: crack_scrypt

# 需要導入模塊: from cryptography.hazmat.primitives.kdf import scrypt [as 別名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 別名]
def crack_scrypt(key, salt):

    try:
        salt = unhexlify(sys.argv[1].encode())
        key = unhexlify(sys.argv[2].encode())
    except binascii.Error:
        print('Non-hexadecimal data on salt and/or key', file=sys.stderr)
        return False

    backend = default_backend()

    for number in range(10000):

        kdf = Scrypt(
            salt=salt,
            length=32,
            n=2**14,
            r=8,
            p=1,
            backend=backend
        )

        try:
            kdf.verify(str(number).encode(), key)
            print('Cracked! Password:', number)
            break
        except InvalidKey:
            pass 
開發者ID:fportantier,項目名稱:vulpy,代碼行數:30,代碼來源:scrypt-crack.py

示例6: __call__

# 需要導入模塊: from cryptography.hazmat.primitives.kdf import scrypt [as 別名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 別名]
def __call__(self,
                 password: bytes,
                 salt: bytes,
                 **kwargs) -> bytes:
        """
        Derives a symmetric encryption key from a pair of password and salt.
        It also accepts an additional _scrypt_cost argument.
        WARNING: RFC7914 recommends that you use a 2^20 cost value for sensitive
        files. It is NOT recommended to change the `_scrypt_cost` value unless
        you know what you are doing.
        :param password: byte-encoded password used to derive a symmetric key
        :param salt: cryptographic salt added during key derivation
        :return:
        """

        _scrypt_cost = kwargs.get('_scrypt_cost', Scrypt.__DEFAULT_SCRYPT_COST)
        try:
            derived_key = CryptographyScrypt(
                salt=salt,
                length=SecretBox.KEY_SIZE,
                n=2 ** _scrypt_cost,
                r=8,
                p=1,
                backend=default_backend()
            ).derive(password)
        except InternalError as e:
            required_memory = 128 * 2**_scrypt_cost * 8 // (10**6)
            if e.err_code[0].reason == 65:
                raise MemoryError(
                    "Scrypt key derivation requires at least {} MB of memory. "
                    "Please free up some memory and try again.".format(required_memory)
                )
            else:
                raise e
        else:
            return derived_key 
開發者ID:nucypher,項目名稱:pyUmbral,代碼行數:38,代碼來源:keys.py

示例7: decrypt

# 需要導入模塊: from cryptography.hazmat.primitives.kdf import scrypt [as 別名]
# 或者: from cryptography.hazmat.primitives.kdf.scrypt import Scrypt [as 別名]
def decrypt(self, jsons):
        """Import file is AES GCM encrypted, let's decrypt it.

        Based on the import script from Aegis:
        https://github.com/beemdevelopment/Aegis/blob/master/scripts/decrypt.py
        Format documentation:
        https://github.com/beemdevelopment/Aegis/blob/master/docs/vault.md
        """
        if not CRYPTOGRAPHY:
            raise ImportError(name='cryptography')

        password = getpassword(self.prefix)
        master_key = None
        for slot in jsons['header']['slots']:
            if slot['type'] != 1:
                continue

            kdf = Scrypt(salt=bytes.fromhex(slot['salt']),
                         length=32,
                         n=slot['n'],
                         r=slot['r'],
                         p=slot['p'],
                         backend=default_backend())
            key = kdf.derive(password.encode("utf-8"))

            cipher = AESGCM(key)
            param = slot['key_params']
            try:
                nonce = bytes.fromhex(param['nonce'])
                data = bytes.fromhex(slot['key']) + bytes.fromhex(param['tag'])
                master_key = cipher.decrypt(nonce=nonce,
                                            data=data,
                                            associated_data=None)
            except InvalidTag:  # pragma: no cover
                pass

        if master_key is None:  # pragma: no cover
            raise FormatError("unable to decrypt the master key.")

        cipher = AESGCM(master_key)
        param = jsons['header']['params']
        content = base64.b64decode(jsons['db']) + bytes.fromhex(param['tag'])
        plain = cipher.decrypt(nonce=bytes.fromhex(param['nonce']),
                               data=content,
                               associated_data=None)
        return plain.decode('utf-8') 
開發者ID:roddhjav,項目名稱:pass-import,代碼行數:48,代碼來源:aegis.py


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