Hướng dẫn how to hash password in python - cách băm mật khẩu trong python

Trong bài viết này, chúng tôi sẽ biết cách băm mật khẩu trong Python.

Một mật khẩu mạnh cung cấp an toàn. Mật khẩu văn bản thuần túy cực kỳ không an toàn, vì vậy chúng tôi cần tăng cường mật khẩu bằng cách băm mật khẩu. & NBSP; Hash Breath Mật khẩu là một phương pháp rẻ tiền và an toàn để giữ cho mật khẩu an toàn trước hoạt động độc hại. Mật khẩu băm tạo ra một mật khẩu duy nhất cho mỗi văn bản, ngay cả khi mật khẩu bản rõ giống nhau.

Tại sao chúng ta cần băm một mật khẩu?

Băm được sử dụng chủ yếu để bảo vệ mật khẩu khỏi tin tặc. Giả sử, nếu một trang web bị hack, tội phạm mạng don don có quyền truy cập vào mật khẩu của bạn. Thay vào đó, họ chỉ có quyền truy cập vào phần được mã hóa Hash Hash được tạo ra bởi phương pháp băm.

Muối trong băm là gì?

Trong mật mã, một muối là dữ liệu ngẫu nhiên được sử dụng làm đầu vào bổ sung cho hàm một chiều băm dữ liệu, chẳng hạn như mật khẩu. Muối được sử dụng để giữ mật khẩu an toàn trong khi chúng được lưu trữ. Trong lịch sử, chỉ có hàm băm mật khẩu mật khẩu được duy trì trên một hệ thống, nhưng theo thời gian, các biện pháp phòng ngừa bổ sung đã được phát triển để ngăn chặn việc xác định mật khẩu trùng lặp hoặc thông thường. Một phòng ngừa như vậy là muối.

Hướng dẫn how to hash password in python - cách băm mật khẩu trong python

Mã hóa: Mã hóa là quá trình mã hóa văn bản thuần túy hoặc bất kỳ thông tin nào theo cách mà chỉ những người được ủy quyền mới có thể đọc nó bằng khóa tương ứng để có thể bảo vệ dữ liệu bí mật khỏi những người trái phép. & NBSP;Encryption is the process of encoding plain text or any information in such a way that only authorized people can read it with a corresponding key so that confidential data can be protected from unauthorized persons. 

Băm: băm chuyển đổi bất kỳ lượng dữ liệu nào thành một hàm băm có độ dài cố định không thể đảo ngược. Nó được sử dụng rộng rãi trong mật mã. Hash cho phép chúng tôi xác thực nếu đầu vào đã thay đổi một chút, nếu nó được thay đổi thì băm kết quả sẽ khác nhau. Trong bài viết này, chúng tôi sẽ học & nbsp; kỹ thuật băm mật khẩu muối. Nó bao gồm chuyển đổi một thuật toán thành bản đồ dữ liệu có kích thước thành một chiều dài cố định.Hashing converts any amount of data into a fixed-length hash that cannot be reversed. It is widely used in cryptography. The hash allows us to validate if the input has changed even slightly, if it is changed the resulting hash will be different. In this article, we are going to learn the  Salted Password Hashing technique. It includes converting an algorithm to map data of any size to a fixed length.

Bcrypt là gì?

Thuật toán BCRYPT được sử dụng để băm và mật khẩu muối một cách an toàn. BCRYPT cho phép tạo lớp bảo vệ mật khẩu có thể phát triển đổi mới phần cứng cục bộ để bảo vệ chống lại các mối nguy hiểm hoặc các mối đe dọa lâu dài, chẳng hạn như những kẻ tấn công có khả năng tính toán để đoán mật khẩu hiệu quả gấp đôi.

Cài đặt BCRYPT bằng PIP:

 pip install bcrypt

Ví dụ: Trong chương trình này, chúng tôi sẽ băm mật khẩu bằng BCRYPT.In this Program, we will be hashing the password using bcrypt.

Ở đây, chúng tôi đang sử dụng GeekPassword, làm đầu vào để được chuyển đổi thành hàm băm.as an input to be converted to a hash.

Python

import bcrypt

pip install hashlib
0____11
pip install hashlib
2
pip install hashlib
3

pip install hashlib
4
pip install hashlib
1
pip install hashlib
6

pip install hashlib
7
pip install hashlib
1
pip install hashlib
9

import os

salt = os.urandom(32)
0
import os

salt = os.urandom(32)
1
import os

salt = os.urandom(32)
2
import os

salt = os.urandom(32)
3

import os

salt = os.urandom(32)
0
import os

salt = os.urandom(32)
5

import os

salt = os.urandom(32)
0
import os

salt = os.urandom(32)
1
import os

salt = os.urandom(32)
8
import os

salt = os.urandom(32)
3

import os

salt = os.urandom(32)
0
import hashlib
import os

salt = os.urandom(32) # Remember this
password = 'password123'

key = hashlib.pbkdf2_hmac(
    'sha256', # The hash digest algorithm for HMAC
    password.encode('utf-8'), # Convert the password to bytes
    salt, # Provide the salt
    100000 # It is recommended to use at least 100,000 iterations of SHA-256 
)
1

Output:

Hướng dẫn how to hash password in python - cách băm mật khẩu trong python

Hashlib là gì?

Mô -đun Hashlib Python là một giao diện cho các tin nhắn băm dễ dàng. Điều này chứa nhiều phương pháp sẽ xử lý băm bất kỳ thông báo thô nào thành định dạng được mã hóa. Mục đích chính của mô -đun này là sử dụng hàm băm trên một chuỗi và mã hóa nó để rất khó để giải mã nó. Thư viện băm: Nó được sử dụng để tạo bảng băm. Bảng băm là một cấu trúc dữ liệu được thiết kế để tìm kiếm thông qua một tập hợp các mục, mỗi mục được xác định bởi một khóa duy nhất.hash library: It is used to create a hash table. The hash table is a data structure that is designed for searching through a set of entries, each of which is identified by a unique key.

Cài đặt Hashlib bằng PIP:

pip install hashlib

Ví dụ 2: Trong chương trình này, chúng tôi sẽ băm mật khẩu bằng Hashlib. In this Program, we will be hashing the password using hashlib.

Ở đây, chúng tôi đang sử dụng GeekPassword, làm đầu vào để được chuyển đổi thành hàm băm.as an input to be converted to a hash.

Python

import bcrypt

pip install hashlib
0____11
pip install hashlib
2
pip install hashlib
3

pip install hashlib
4
pip install hashlib
1
pip install hashlib
6

pip install hashlib
7
pip install hashlib
1
pip install hashlib
9

Hashlib là gì?

import os

salt = os.urandom(32)
0
import hashlib
import os

salt = os.urandom(32) # Remember this
password = 'password123'

key = hashlib.pbkdf2_hmac(
    'sha256', # The hash digest algorithm for HMAC
    password.encode('utf-8'), # Convert the password to bytes
    salt, # Provide the salt
    100000, # It is recommended to use at least 100,000 iterations of SHA-256 
    dklen=128 # Get a 128 byte key
)
9

Output:

Hướng dẫn how to hash password in python - cách băm mật khẩu trong python


Trong hướng dẫn này, tôi đề cập đến việc sử dụng PBKDF2_HMAC để mã hóa mật khẩu bằng muối bằng Python.

  • Tại sao bạn cần mật khẩu băm
  • Tại sao không sử dụng SHA-256 hoặc một cái gì đó tương tự?
  • Mật khẩu băm với pbkdf2_hmac
    • Tạo ra một muối
    • Băm
    • Lưu trữ băm và muối
  • xác minh
  • Ví dụ về việc thêm người dùng và xác minh

Tại sao bạn cần mật khẩu băm

Tại sao không sử dụng SHA-256 hoặc một cái gì đó tương tự?never store the password in plaintext. If an attacker finds a database of plaintext passwords, they can easily be used in combination with matching emails to login to the associated site/account and even used to attempt to log into other accounts since a lot of people use the same password.

Mật khẩu băm với pbkdf2_hmac

Tại sao không sử dụng SHA-256 hoặc một cái gì đó tương tự?

Mật khẩu băm với pbkdf2_hmac

Tạo ra một muối

Băm

Lưu trữ băm và muối

Bất cứ khi nào xác minh người dùng hoặc thứ gì đó tương tự bằng mật khẩu, bạn không bao giờ được lưu trữ mật khẩu trong bản rõ. Nếu kẻ tấn công tìm thấy cơ sở dữ liệu mật khẩu văn bản, chúng có thể dễ dàng được sử dụng kết hợp với các email phù hợp để đăng nhập vào trang web/tài khoản được liên kết và thậm chí được sử dụng để cố gắng đăng nhập vào các tài khoản khác vì rất nhiều người sử dụng cùng một mật khẩu.

Một phương pháp phổ biến được sử dụng ngày nay là mật khẩu băm khi mật khẩu được cung cấp. Nên sử dụng muối khi băm và lưu trữ muối bằng mật khẩu băm.

Các thuật toán băm an toàn là các hàm một chiều, nghĩa là, một khi bản rõ được băm, chúng ta không thể lấy bản rõ từ băm. Điều này là tốt vì nó giữ mật khẩu ẩn và cho phép xác minh đơn giản bằng cách băm mật khẩu do người dùng cung cấp và so sánh nó với băm được lưu trữ của mật khẩu thực tế.

Xin lưu ý rằng việc sử dụng phương pháp này không ngăn chặn các cuộc tấn công vũ lực / từ điển hoặc sử dụng các bảng cầu vồng, nó chỉ đơn giản là làm cho các phương pháp này trở nên khó khăn hơn về mặt tính toán.

PBKDF2_HMAC là việc triển khai chức năng dẫn xuất khóa PBKDF2 bằng HMAC làm hàm giả.

import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]
0 có thể được tìm thấy trong thư viện
import hashlib
import os

salt = os.urandom(32) # Remember this
password = 'password123'

key = hashlib.pbkdf2_hmac(
    'sha256', # The hash digest algorithm for HMAC
    password.encode('utf-8'), # Convert the password to bytes
    salt, # Provide the salt
    100000 # It is recommended to use at least 100,000 iterations of SHA-256 
)
3 (đi kèm với Python) và ở Python 3,4 trở lên.
import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]
0 mất năm tham số:

  • import os
    import hashlib
    
    # Example generation
    salt = os.urandom(32)
    key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)
    
    # Store them as:
    storage = salt + key 
    
    # Getting the values back out
    salt_from_storage = storage[:32] # 32 is the length of the salt
    key_from_storage = storage[32:]
    
    4: Thuật toán tiêu hóa băm cho HMAC
  • import hashlib
    import os
    
    salt = os.urandom(32) # Remember this
    password = 'password123'
    
    key = hashlib.pbkdf2_hmac(
        'sha256', # The hash digest algorithm for HMAC
        password.encode('utf-8'), # Convert the password to bytes
        salt, # Provide the salt
        100000, # It is recommended to use at least 100,000 iterations of SHA-256 
        dklen=128 # Get a 128 byte key
    )
    
    2: Mật khẩu được biến thành khóa
  • import hashlib
    import os
    
    salt = os.urandom(32) # Remember this
    password = 'password123'
    
    key = hashlib.pbkdf2_hmac(
        'sha256', # The hash digest algorithm for HMAC
        password.encode('utf-8'), # Convert the password to bytes
        salt, # Provide the salt
        100000, # It is recommended to use at least 100,000 iterations of SHA-256 
        dklen=128 # Get a 128 byte key
    )
    
    4: Muối được tạo ngẫu nhiên
  • import os
    import hashlib
    
    # Example generation
    salt = os.urandom(32)
    key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)
    
    # Store them as:
    storage = salt + key 
    
    # Getting the values back out
    salt_from_storage = storage[:32] # 32 is the length of the salt
    key_from_storage = storage[32:]
    
    7: Lặp lại trong tính toán (có nghĩa là cần nhiều tính toán nhiều hơn)
  • import os
    import hashlib
    
    # Example generation
    salt = os.urandom(32)
    key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)
    
    # Store them as:
    storage = salt + key 
    
    # Getting the values back out
    salt_from_storage = storage[:32] # 32 is the length of the salt
    key_from_storage = storage[32:]
    
    8: Độ dài của phím đầu ra (không bắt buộc)

Tạo ra một muối

Trước khi tạo khóa bằng cách sử dụng

import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]
0, bạn cần tạo ra một loại muối ngẫu nhiên. Muối làm cho không gian tìm kiếm lớn hơn trong trường hợp bắt buộc và thêm khó khăn cho các bảng cầu vồng; Sử dụng muối chỉ yêu cầu bạn làm thêm một chút và lưu trữ một chuỗi byte ngẫu nhiên thêm.

Muối không cần phải ẩn, mã hóa hoặc băm; Điều này là do chúng chỉ đơn giản là kết hợp với mật khẩu để làm cho đầu vào một phạm vi lớn hơn. Sự kết hợp này được thực hiện bởi

import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]
0 vì vậy đừng tự làm điều đó.

Để tạo muối, hãy sử dụng hàm

import hashlib

salt = b'' # Get the salt you stored for *this* user
key = b'' # Get this users key calculated

password_to_check = 'password246' # The password provided by the user to check

# Use the exact same setup you used to generate the key, but this time put in the password to check
new_key = hashlib.pbkdf2_hmac(
    'sha256',
    password_to_check.encode('utf-8'), # Convert the password to bytes
    salt, 
    100000
)

if new_key == key:
    print('Password is correct')
else:
    print('Password is incorrect')
1 vì nó trả về các byte ngẫu nhiên phù hợp để sử dụng mật mã. Hàm này không sử dụng các trình tạo số giả ngẫu nhiên để giá trị trả về không thể đoán trước được; Chính xác những gì được yêu cầu.

import os

salt = os.urandom(32)

32 là kích thước được trả về trong byte. Bạn có thể chọn bất kỳ kích thước nào nhưng tôi khuyên bạn nên làm nó trên 16 byte.

Đầu ra từ điều này sẽ được sử dụng trong

import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]
0 và sau đó được lưu trữ bên cạnh phím đầu ra từ
import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]
0. Mỗi mật khẩu liên quan đến người dùng/tổ chức phải có muối riêng; Không sử dụng cùng một loại muối cho tất cả mật khẩu của người dùng/thực thể.do not use the same salt for all user's/entities passwords.

Băm

Bây giờ những điều cơ bản của các khái niệm này đã hết cách, chúng ta có thể xuống để thực hiện một số mã. Cách tốt nhất để học là ví dụ và ứng dụng, vì vậy đây là một ví dụ:

import hashlib
import os

salt = os.urandom(32) # Remember this
password = 'password123'

key = hashlib.pbkdf2_hmac(
    'sha256', # The hash digest algorithm for HMAC
    password.encode('utf-8'), # Convert the password to bytes
    salt, # Provide the salt
    100000 # It is recommended to use at least 100,000 iterations of SHA-256 
)

Vì không có chiều dài khóa nào được cung cấp, kích thước tiêu hóa của thuật toán băm được sử dụng; Trong trường hợp này, chúng tôi sử dụng SHA-256 vì vậy kích thước sẽ là 64 byte. Nếu bạn yêu cầu một khóa dài hơn cho một cái gì đó như sử dụng khóa này trong AES, hãy chuyển kích thước khóa mong muốn đến

import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]
8 sau khi lặp trong
import hashlib

salt = b'' # Get the salt you stored for *this* user
key = b'' # Get this users key calculated

password_to_check = 'password246' # The password provided by the user to check

# Use the exact same setup you used to generate the key, but this time put in the password to check
new_key = hashlib.pbkdf2_hmac(
    'sha256',
    password_to_check.encode('utf-8'), # Convert the password to bytes
    salt, 
    100000
)

if new_key == key:
    print('Password is correct')
else:
    print('Password is incorrect')
5; Ví dụ:

import hashlib
import os

salt = os.urandom(32) # Remember this
password = 'password123'

key = hashlib.pbkdf2_hmac(
    'sha256', # The hash digest algorithm for HMAC
    password.encode('utf-8'), # Convert the password to bytes
    salt, # Provide the salt
    100000, # It is recommended to use at least 100,000 iterations of SHA-256 
    dklen=128 # Get a 128 byte key
)

Trong các ví dụ này, chìa khóa là "băm" của bạn

Lưu trữ băm và muối

Từ đoạn trích ở trên, bạn cần lưu trữ muối và chìa khóa. Về lưu trữ, bạn có thể sử dụng bất kỳ phương pháp nào; JSON, SQL, CSV hoặc thậm chí là một tệp văn bản thô. Hãy chắc chắn rằng bạn không lưu trữ mật khẩu vì đó là mục tiêu của tất cả những điều này, không phải lưu trữ mật khẩu thực tế.

Nếu bạn chỉ giới hạn trong một trường để lưu trữ, bạn có thể thêm muối và mật khẩu với nhau và sau đó lưu trữ chúng. Khi đọc chúng ra, sau đó bạn có thể tách chúng ra khi bạn biết độ dài của muối và chìa khóa. Ví dụ:

import os
import hashlib

# Example generation
salt = os.urandom(32)
key = hashlib.pbkdf2_hmac('sha256', 'mypassword'.encode('utf-8'), salt, 100000)

# Store them as:
storage = salt + key 

# Getting the values back out
salt_from_storage = storage[:32] # 32 is the length of the salt
key_from_storage = storage[32:]

Nếu có thể sử dụng hai trường trong tình huống của bạn (hầu hết các tình huống bạn có thể), thì hãy sử dụng hai trường vì nó làm cho mọi thứ trở nên ít phức tạp hơn

xác minh

Sau khi người dùng đã cung cấp mật khẩu của họ lần đầu tiên và bạn đã tạo muối cho họ, đã tính toán khóa bằng mật khẩu và muối và sau đó lưu trữ mật khẩu và muối này, bây giờ bạn có thể kiểm tra xem có thêm mật khẩu không.

import hashlib

salt = b'' # Get the salt you stored for *this* user
key = b'' # Get this users key calculated

password_to_check = 'password246' # The password provided by the user to check

# Use the exact same setup you used to generate the key, but this time put in the password to check
new_key = hashlib.pbkdf2_hmac(
    'sha256',
    password_to_check.encode('utf-8'), # Convert the password to bytes
    salt, 
    100000
)

if new_key == key:
    print('Password is correct')
else:
    print('Password is incorrect')

Về bạn đã sử dụng đoạn trích ở trên để tạo khóa đầu tiên, thực hiện việc này sẽ tạo ra "Mật khẩu không chính xác"; Điều này là tốt vì mật khẩu không khớp. Khi chạy lại điều này nhưng với mật khẩu chính xác lần này, tập lệnh sẽ xuất "Mật khẩu đúng" khi mật khẩu khớp.

Ví dụ về việc thêm người dùng và xác minh

import hashlib
import os

users = {} # A simple demo storage

# Add a user
username = 'Brent' # The users username
password = 'mypassword' # The users password

salt = os.urandom(32) # A new salt for this user
key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
users[username] = { # Store the salt and key
    'salt': salt,
    'key': key
}

# Verification attempt 1 (incorrect password)
username = 'Brent'
password = 'notmypassword'

salt = users[username]['salt'] # Get the salt
key = users[username]['key'] # Get the correct key
new_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)

assert key != new_key # The keys are not the same thus the passwords were not the same

# Verification attempt 2 (correct password)
username = 'Brent'
password = 'mypassword'

salt = users[username]['salt']
key = users[username]['key']
new_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)

assert key == new_key # The keys are the same thus the passwords were the same

# Adding a different user
username = 'Jarrod'
password = 'my$ecur3p@$$w0rd'

salt = os.urandom(32) # A new salt for this user
key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)
users[username] = {
    'salt': salt,
    'key': key
}

# Checking the other users password
username = 'Jarrod'
password = 'my$ecur3p@$$w0rd'

salt = users[username]['salt']
key = users[username]['key']
new_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt, 100000)

assert key == new_key # The keys are the same thus the passwords were the same for this user also

Mật khẩu băm trong Python là gì?

Mật khẩu băm là một phương pháp rẻ tiền và an toàn để giữ cho mật khẩu an toàn trước hoạt động độc hại. Mật khẩu băm tạo ra một mật khẩu duy nhất cho mỗi văn bản, ngay cả khi mật khẩu bản rõ giống nhau.a cheap and secure method that keeps the passwords safe from malicious activity. Password hashing generates a unique password for every text, even if the plaintext password is the same.

Làm cách nào để sử dụng mã băm trong Python?

Cú pháp của Phương thức Hash () Python:..
Cú pháp: Hash (OBJ).
Tham số: OBJ: Đối tượng mà chúng ta cần chuyển đổi thành băm ..
Trả về: Trả về giá trị băm nếu có thể ..

Làm thế nào để bạn mã hóa mật khẩu trong Python?

Cách mã hóa một trình tạo mật khẩu trong Python [trong 4 bước]..
Bước 1: Nhập các mô -đun cần thiết.Bước đầu tiên, hãy nhập mô -đun bí mật.....
Bước 2: Xác định bảng chữ cái.Bước tiếp theo là xác định bảng chữ cái.....
Bước 3: Khắc phục độ dài của mật khẩu;Tạo mật khẩu.....
Bước 4: Tùy chỉnh mật khẩu của bạn dựa trên các ràng buộc ..

Làm thế nào để bạn bỏ mật khẩu trong Python?

Đó không phải là một chuyến đi khứ hồi và một mật khẩu băm không thể không bị ảnh hưởng.Cách duy nhất để kiểm tra xem mật khẩu đầu vào có khớp với cái trong cơ sở dữ liệu hay không là băm mật khẩu đầu vào, sau đó so sánh các băm.a hashed password cannot be unhashed. The only way to check whether an input password matches the one in the database is to hash the input password as well, and then compare the hashes.