Hướng dẫn hashlib python - trăn băm

Mã nguồn: lib/hashlib.py Lib/hashlib.py


Mô -đun này thực hiện một giao diện chung cho nhiều thuật toán tiêu hóa an toàn và thông báo khác nhau. Bao gồm là các thuật toán băm bảo mật FIPS SHA1, SHA224, SHA256, SHA384 và SHA512 (được định nghĩa trong FIP 180-2) cũng như thuật toán RSA MD MD5 (được xác định trong Internet RFC 1321). Các thuật ngữ bảo mật băm và thông báo tiêu hóa có thể hoán đổi cho nhau. Các thuật toán cũ hơn được gọi là tiêu hóa tin nhắn. Thuật ngữ hiện đại là băm an toàn.RFC 1321). The terms “secure hash” and “message digest” are interchangeable. Older algorithms were called message digests. The modern term is secure hash.

Ghi chú

Nếu bạn muốn các hàm băm Adler32 hoặc CRC32, chúng có sẵn trong mô -đun

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
6.

Cảnh báo

Một số thuật toán đã biết các điểm yếu va chạm băm, tham khảo phần See See cũng ở cuối.

Thuật toán băm

Có một phương thức cấu trúc có tên cho mỗi loại băm. Tất cả trả về một đối tượng băm với cùng một giao diện đơn giản. Ví dụ: sử dụng

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
7 để tạo đối tượng băm SHA-256. Bây giờ bạn có thể cung cấp cho đối tượng này bằng các đối tượng giống như byte (thường là
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
8) bằng phương pháp
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9. Tại bất kỳ thời điểm nào, bạn có thể yêu cầu tiêu hóa việc kết hợp dữ liệu được cung cấp cho đến nay bằng cách sử dụng các phương thức
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
0 hoặc
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
1.bytes-like objects (normally
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
8) using the
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9 method. At any point you can ask it for the digest of the concatenation of the data fed to it so far using the
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
0 or
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
1 methods.

Ghi chú

Nếu bạn muốn các hàm băm Adler32 hoặc CRC32, chúng có sẵn trong mô -đun

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
6.GIL is released for data larger than 2047 bytes at object creation or on update.

Ghi chú

Nếu bạn muốn các hàm băm Adler32 hoặc CRC32, chúng có sẵn trong mô -đun

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
6.

Cảnh báo

Một số thuật toán đã biết các điểm yếu va chạm băm, tham khảo phần See See cũng ở cuối.SHA3 (Keccak) and SHAKE constructors

>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
1,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
2,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
3,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
4,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
5,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
6.

Thuật toán bămAll hashlib constructors take a keyword-only argument usedforsecurity with default value

>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
3. A false value allows the use of insecure and blocked hashing algorithms in restricted environments.
>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
4 indicates that the hashing algorithm is not used in a security context, e.g. as a non-cryptographic one-way compression function.

Có một phương thức cấu trúc có tên cho mỗi loại băm. Tất cả trả về một đối tượng băm với cùng một giao diện đơn giản. Ví dụ: sử dụng

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
7 để tạo đối tượng băm SHA-256. Bây giờ bạn có thể cung cấp cho đối tượng này bằng các đối tượng giống như byte (thường là
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
8) bằng phương pháp
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9. Tại bất kỳ thời điểm nào, bạn có thể yêu cầu tiêu hóa việc kết hợp dữ liệu được cung cấp cho đến nay bằng cách sử dụng các phương thức
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
0 hoặc
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
1.

Để có hiệu suất đa luồng tốt hơn, Python Gil được phát hành cho dữ liệu lớn hơn 2047 byte khi tạo đối tượng hoặc trên bản cập nhật.

>>> import hashlib
>>> m = hashlib.sha256()
>>> m.update(b"Nobody inspects")
>>> m.update(b" the spammish repetition")
>>> m.digest()
b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
>>> m.digest_size
32
>>> m.block_size
64

Cho ăn các đối tượng chuỗi vào

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9 không được hỗ trợ, vì băm hoạt động trên byte, không phải trên các ký tự.

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'

Các hàm tạo cho các thuật toán băm luôn có trong mô -đun này là
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
3,
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
4,
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
7,
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
6,
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
7,
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
8 và
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
9.
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
0 thường có sẵn, mặc dù nó có thể bị thiếu hoặc bị chặn nếu bạn đang sử dụng một bản dựng Python của Python. Các thuật toán bổ sung cũng có thể có sẵn tùy thuộc vào thư viện OpenSSL mà Python sử dụng trên nền tảng của bạn. Trên hầu hết các nền tảng
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
1,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
2,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
3,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
4,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
5,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
6 cũng có sẵn.(name, [data, ]*, usedforsecurity=True)

Mới trong phiên bản 3.6: SHA3 (KECCAK) và các hàm tạo lắc

>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
1,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
2,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
3,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
4,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
5,
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
6.

Đã thay đổi trong phiên bản 3.9: Tất cả các trình xây dựng Hashlib có một đối số chỉ sử dụng từ khóa với giá trị mặc định

>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
3. Một giá trị sai cho phép sử dụng các thuật toán băm không an toàn và bị chặn trong các môi trường bị hạn chế.
>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
4 chỉ ra rằng thuật toán băm không được sử dụng trong bối cảnh bảo mật, ví dụ: Là một chức năng nén một chiều không phải là cryptographic.

>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'

Hashlib hiện sử dụng Sha3 và lắc từ OpenSSL 1.1.1 và mới hơn.

Ví dụ: để có được tiêu hóa của chuỗi byte
>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
5:

Kéo cô đọng hơn:

________ 46 ________ 47 (Tên, [Dữ liệu,]*, usedForSecurity = true) ¶

Là một hàm tạo chung lấy tên chuỗi của thuật toán mong muốn làm tham số đầu tiên của nó. Nó cũng tồn tại để cho phép truy cập vào các băm được liệt kê ở trên cũng như bất kỳ thuật toán nào khác mà thư viện OpenSSL của bạn có thể cung cấp. Các hàm tạo được đặt tên nhanh hơn nhiều so với
>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
8 và nên được ưu tiên.

Sử dụng

>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
8 với thuật toán được cung cấp bởi OpenSSL:

________ 46 ________ 47 (Tên, [Dữ liệu,]*, usedForSecurity = true) ¶

Là một hàm tạo chung lấy tên chuỗi của thuật toán mong muốn làm tham số đầu tiên của nó. Nó cũng tồn tại để cho phép truy cập vào các băm được liệt kê ở trên cũng như bất kỳ thuật toán nào khác mà thư viện OpenSSL của bạn có thể cung cấp. Các hàm tạo được đặt tên nhanh hơn nhiều so với

>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
8 và nên được ưu tiên.

Sử dụng
>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
8 với thuật toán được cung cấp bởi OpenSSL:

Hashlib cung cấp các thuộc tính không đổi sau:

________ 46 ________ 51¶

Một bộ chứa tên của các thuật toán băm được đảm bảo được hỗ trợ bởi mô -đun này trên tất cả các nền tảng. Lưu ý rằng ‘MD5, nằm trong danh sách này mặc dù một số nhà cung cấp thượng nguồn cung cấp một bản dựng Python Python tuân thủ FIP lẻ không bao gồm nó.

Mới trong phiên bản 3.2.

________ 46 ________ 53¶

Một bộ chứa tên của các thuật toán băm có sẵn trong trình thông dịch Python đang chạy. Những tên này sẽ được công nhận khi được chuyển đến

>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
8.
>>> from hashlib import blake2b
>>> blake2b(b'Hello world').hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
5 sẽ luôn là một tập hợp con. Thuật toán tương tự có thể xuất hiện nhiều lần trong tập hợp này dưới các tên khác nhau (nhờ OpenSSL).

Các giá trị sau được cung cấp dưới dạng các thuộc tính không đổi của các đối tượng băm được trả về bởi các hàm tạo:The name attribute has been present in CPython since its inception, but until Python 3.4 was not formally specified, so may not exist on some platforms.

________ 56 ________ 57¶

Kích thước của băm kết quả trong byte.(data)

________ 56 ________ 59¶bytes-like object. Repeated calls are equivalent to a single call with the concatenation of all the arguments:

>>> from hashlib import blake2b
>>> items = [b'Hello', b' ', b'world']
>>> h = blake2b()
>>> for item in items:
...     h.update(item)
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
5 is equivalent to
>>> from hashlib import blake2b
>>> items = [b'Hello', b' ', b'world']
>>> h = blake2b()
>>> for item in items:
...     h.update(item)
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
6.

Kích thước khối bên trong của thuật toán băm trong byte.The Python GIL is released to allow other threads to run while hash updates on data larger than 2047 bytes is taking place when using hash algorithms supplied by OpenSSL.

________ 56 ________ 68 ()()

Trả về tiêu hóa của dữ liệu được truyền vào phương thức

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9 cho đến nay. Đây là đối tượng byte có kích thước
>>> from hashlib import blake2b
>>> h = blake2b(digest_size=20)
>>> h.update(b'Replacing SHA1 with the more secure function')
>>> h.hexdigest()
'd24f26cf8de66472d58d4e1b1774b4c9158b1f4c'
>>> h.digest_size
20
>>> len(h.digest())
20
0 có thể chứa byte trong toàn bộ phạm vi từ 0 đến 255.

________ 56 ________ 72 ()()

Giống như

>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
0 ngoại trừ tiêu hóa được trả về dưới dạng đối tượng chuỗi có độ dài gấp đôi, chỉ chứa các chữ số thập lục phân. Điều này có thể được sử dụng để trao đổi giá trị một cách an toàn trong email hoặc các môi trường không nhị phân khác.

________ 56 ________ 75 ()()

Trả về một bản sao (bản sao của bản sao) của đối tượng băm. Điều này có thể được sử dụng để tính toán hiệu quả các tiêu hóa của dữ liệu chia sẻ một chuỗi con ban đầu chung.

Lắc Digests Chiều dài biến đổi

Các thuật toán

>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
5 và
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
6 cung cấp các tiêu hóa độ dài thay đổi với độ dài_in_bits // 2 lên đến 128 hoặc 256 bit bảo mật. Như vậy, các phương pháp tiêu hóa của họ đòi hỏi một độ dài. Chiều dài tối đa không bị giới hạn bởi thuật toán lắc.

________ 78 ________ 68 (chiều dài) ¶(length)

Trả về tiêu hóa của dữ liệu được truyền vào phương thức

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9 cho đến nay. Đây là một đối tượng byte có độ dài kích thước có thể chứa byte trong toàn bộ phạm vi từ 0 đến 255.

________ 78 ________ 72 (chiều dài) ¶(length)

Giống như

>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
0 ngoại trừ tiêu hóa được trả về dưới dạng đối tượng chuỗi có độ dài gấp đôi, chỉ chứa các chữ số thập lục phân. Điều này có thể được sử dụng để trao đổi giá trị một cách an toàn trong email hoặc các môi trường không nhị phân khác.

________ 56 ________ 75 ()

Trả về một bản sao (bản sao của bản sao) của đối tượng băm. Điều này có thể được sử dụng để tính toán hiệu quả các tiêu hóa của dữ liệu chia sẻ một chuỗi con ban đầu chung.

Lắc Digests Chiều dài biến đổi(hash_name, password, salt, iterations, dklen=None)

Các thuật toán

>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
5 và
>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'
6 cung cấp các tiêu hóa độ dài thay đổi với độ dài_in_bits // 2 lên đến 128 hoặc 256 bit bảo mật. Như vậy, các phương pháp tiêu hóa của họ đòi hỏi một độ dài. Chiều dài tối đa không bị giới hạn bởi thuật toán lắc.

________ 78 ________ 68 (chiều dài) ¶

Trả về tiêu hóa của dữ liệu được truyền vào phương thức

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9 cho đến nay. Đây là một đối tượng byte có độ dài kích thước có thể chứa byte trong toàn bộ phạm vi từ 0 đến 255.

________ 78 ________ 72 (chiều dài) ¶

>>> from hashlib import pbkdf2_hmac
>>> our_app_iters = 500_000  # Application specific, read above.
>>> dk = pbkdf2_hmac('sha256', b'password', b'bad salt'*2, our_app_iters)
>>> dk.hex()
'15530bba69924174860db778f2c6f8104d3aaf9d26241840c8c4a641c8d000a9'

Đạo hàm chính

Các thuật toán kéo dài chính và các thuật toán kéo dài chính được thiết kế cho băm mật khẩu an toàn. Các thuật toán ngây thơ như

>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'
4 không chống lại các cuộc tấn công vũ phu. Một chức năng băm mật khẩu tốt phải được điều chỉnh, chậm và bao gồm muối.

________ 46 ________ 86 (hash_name, mật khẩu, muối, lặp lại, dklen = none) ¶

Hàm cung cấp chức năng dẫn xuất khóa dựa trên mật khẩu PKCS#5 2. Nó sử dụng HMAC làm hàm giả.Slow Python implementation of pbkdf2_hmac is deprecated. In the future the function will only be available when Python is compiled with OpenSSL.

Chuỗi Hash_name là tên mong muốn của thuật toán tiêu hóa băm cho HMAC, ví dụ: ‘Sha1, hoặc‘ sha256. Mật khẩu và muối được hiểu là bộ đệm của byte. Các ứng dụng và thư viện nên giới hạn mật khẩu ở độ dài hợp lý (ví dụ: 1024). Muối phải khoảng 16 byte trở lên từ một nguồn thích hợp, ví dụ:
>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'
7.(password, *, salt, n, r, p, maxmem=0, dklen=64)

Số lần lặp nên được chọn dựa trên thuật toán băm và sức mạnh tính toán. Tính đến năm 2022, hàng trăm ngàn lần lặp của SHA-256 được đề xuất. Đối với Rationale về lý do và cách chọn những gì tốt nhất cho ứng dụng của bạn, hãy đọc Phụ lục A.2.2 của NIST-SP-800-132. Các câu trả lời trên câu hỏi lặp lại PBKDF2 Stackexchange giải thích chi tiết.RFC 7914.

Dklen là chiều dài của phím dẫn xuất. Nếu dklen là

>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'
8 thì kích thước tiêu hóa của thuật toán băm hash_name được sử dụng, ví dụ: 64 cho SHA-512.bytes-like objects. Applications and libraries should limit password to a sensible length (e.g. 1024). salt should be about 16 or more bytes from a proper source, e.g.
>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'
7.

Mới trong phiên bản 3.4.

Ghi chú

Việc triển khai nhanh PBKDF2_HMAC có sẵn với OpenSSL. Việc triển khai Python sử dụng phiên bản nội tuyến của >>> from hashlib import blake2b, blake2s >>> blake2b(digest_size=10).hexdigest() '6fa1d8fcfd719046d762' >>> blake2b(digest_size=11).hexdigest() 'eb6ec15daf9546254f0809' >>> blake2s(digest_size=10).hexdigest() '1bf21a98c78a1c376ae9' >>> blake2s(digest_size=11).hexdigest() '567004bf96e4a25773ebf4' 9. Nó chậm hơn khoảng ba lần và không phát hành Gil.

Không dùng nữa kể từ phiên bản 3.10: Việc thực hiện PBKDF2_HMAC chậm của PBKDF2_HMAC. Trong tương lai, chức năng sẽ chỉ có sẵn khi Python được biên dịch với OpenSSL.RFC 7693 that comes in two flavors:

  • ________ 46 ________ 91 (Mật khẩu, *, muối, n, r, p, maxmem = 0, dklen = 64) ¶, optimized for 64-bit platforms and produces digests of any size between 1 and 64 bytes,

  • Hàm cung cấp chức năng dẫn xuất khóa dựa trên mật khẩu Scrypt như được định nghĩa trong RFC 7914., optimized for 8- to 32-bit platforms and produces digests of any size between 1 and 32 bytes.

Mật khẩu và muối phải là các đối tượng giống như byte. Các ứng dụng và thư viện nên giới hạn mật khẩu ở độ dài hợp lý (ví dụ: 1024). Muối phải khoảng 16 byte trở lên từ một nguồn thích hợp, ví dụ:

>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'
7.keyed mode (a faster and simpler replacement for HMAC), salted hashing, personalization, and tree hashing.

N là hệ số chi phí CPU/bộ nhớ, R kích thước khối, hệ số song song p và bộ nhớ giới hạn MAXMEM (OpenSSL 1.1.0 mặc định là 32 MIB). Dklen là chiều dài của phím dẫn xuất.

Mới trong phiên bản 3.6.

Blake2¶

Blake2 là một hàm băm mật mã được xác định trong RFC 7693 có hai hương vị:(data=b'', *, digest_size=64, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False, usedforsecurity=True)
>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'
6
>>> from hashlib import blake2b
>>> h = blake2b(key=b'pseudorandom key', digest_size=16)
>>> h.update(b'message data')
>>> h.hexdigest()
'3d363ff7401e02026f4a4687d4863ced'
7(data=b'', *, digest_size=32, key=b'', salt=b'', person=b'', fanout=1, depth=1, leaf_size=0, node_offset=0, node_depth=0, inner_size=0, last_node=False, usedforsecurity=True)

Blake2b, được tối ưu hóa cho các nền tảng 64 bit và tạo ra các tiêu hóa ở bất kỳ kích thước nào trong khoảng từ 1 đến 64 byte,

  • data: initial chunk of data to hash, which must be bytes-like object. It can be passed only as positional argument.

  • digest_size: size of output digest in bytes.

  • key: key for keyed hashing (up to 64 bytes for BLAKE2b, up to 32 bytes for BLAKE2s).

  • salt: salt for randomized hashing (up to 16 bytes for BLAKE2b, up to 8 bytes for BLAKE2s).

  • person: personalization string (up to 16 bytes for BLAKE2b, up to 8 bytes for BLAKE2s).

The following table shows limits for general parameters (in bytes):

Hash

digest_size

len(key)

len(salt)

len(person)

BLAKE2b

64

64

16

16

BLAKE2s

32

32

8

8

Note

BLAKE2 specification defines constant lengths for salt and personalization parameters, however, for convenience, this implementation accepts byte strings of any size up to the specified length. If the length of the parameter is less than specified, it is padded with zeros, thus, for example,

>>> from hashlib import blake2b
>>> h = blake2b(key=b'pseudorandom key', digest_size=16)
>>> h.update(b'message data')
>>> h.hexdigest()
'3d363ff7401e02026f4a4687d4863ced'
8 and
>>> from hashlib import blake2b
>>> h = blake2b(key=b'pseudorandom key', digest_size=16)
>>> h.update(b'message data')
>>> h.hexdigest()
'3d363ff7401e02026f4a4687d4863ced'
9 is the same value. (This is not the case for key.)

These sizes are available as module constants described below.

Constructor functions also accept the following tree hashing parameters:

  • fanout: fanout (0 to 255, 0 if unlimited, 1 in sequential mode).

  • depth: maximal depth of tree (1 to 255, 255 if unlimited, 1 in sequential mode).

  • leaf_size: maximal byte length of leaf (0 to

    >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
    
    00, 0 if unlimited or in sequential mode).

  • node_offset: node offset (0 to

    >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
    
    01 for BLAKE2b, 0 to
    >>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
    'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
    
    02 for BLAKE2s, 0 for the first, leftmost, leaf, or in sequential mode).

  • node_depth: node depth (0 to 255, 0 for leaves, or in sequential mode).

  • inner_size: inner digest size (0 to 64 for BLAKE2b, 0 to 32 for BLAKE2s, 0 in sequential mode).

  • last_node: boolean indicating whether the processed node is the last one (False for sequential mode).

Hướng dẫn hashlib python - trăn băm

See section 2.10 in BLAKE2 specification for comprehensive review of tree hashing.

Constants¶

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
03
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
04¶
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
05
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
04¶

Salt length (maximum length accepted by constructors).

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
03
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
08¶
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
05
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
08¶

Personalization string length (maximum length accepted by constructors).

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
03
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
12¶
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
05
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
12¶

Maximum key size.

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
03
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
16¶
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
05
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
16¶

Maximum digest size that the hash function can output.

Examples¶

Simple hashing¶

To calculate hash of some data, you should first construct a hash object by calling the appropriate constructor function (

>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
8 or
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
9), then update it with the data by calling
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
9 on the object, and, finally, get the digest out of the object by calling
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
0 (or
>>> h = hashlib.new('sha256')
>>> h.update(b"Nobody inspects the spammish repetition")
>>> h.hexdigest()
'031edd7d41651593c5fe5c006fa5752b37fddff7bc4e843aa6af0c950f4b9406'
1 for hex-encoded string).

>>> from hashlib import blake2b
>>> h = blake2b()
>>> h.update(b'Hello world')
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'

As a shortcut, you can pass the first chunk of data to update directly to the constructor as the positional argument:

>>> from hashlib import blake2b
>>> blake2b(b'Hello world').hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'

You can call

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
24 as many times as you need to iteratively update the hash:

>>> from hashlib import blake2b
>>> items = [b'Hello', b' ', b'world']
>>> h = blake2b()
>>> for item in items:
...     h.update(item)
>>> h.hexdigest()
'6ff843ba685842aa82031d3f53c48b66326df7639a63d128974c5c14f31a0f33343a8c65551134ed1ae0f2b0dd2bb495dc81039e3eeb0aa1bb0388bbeac29183'

Using different digest sizes¶

BLAKE2 has configurable size of digests up to 64 bytes for BLAKE2b and up to 32 bytes for BLAKE2s. For example, to replace SHA-1 with BLAKE2b without changing the size of output, we can tell BLAKE2b to produce 20-byte digests:

>>> from hashlib import blake2b
>>> h = blake2b(digest_size=20)
>>> h.update(b'Replacing SHA1 with the more secure function')
>>> h.hexdigest()
'd24f26cf8de66472d58d4e1b1774b4c9158b1f4c'
>>> h.digest_size
20
>>> len(h.digest())
20

Hash objects with different digest sizes have completely different outputs (shorter hashes are not prefixes of longer hashes); BLAKE2b and BLAKE2s produce different outputs even if the output length is the same:

>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'

Keyed hashing¶

Keyed hashing can be used for authentication as a faster and simpler replacement for Hash-based message authentication code (HMAC). BLAKE2 can be securely used in prefix-MAC mode thanks to the indifferentiability property inherited from BLAKE.

This example shows how to get a (hex-encoded) 128-bit authentication code for message

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
25 with key
>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
26:

>>> from hashlib import blake2b
>>> h = blake2b(key=b'pseudorandom key', digest_size=16)
>>> h.update(b'message data')
>>> h.hexdigest()
'3d363ff7401e02026f4a4687d4863ced'

As a practical example, a web application can symmetrically sign cookies sent to users and later verify them to make sure they weren’t tampered with:

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
0

Even though there’s a native keyed hashing mode, BLAKE2 can, of course, be used in HMAC construction with

>>> from hashlib import blake2b, blake2s
>>> blake2b(digest_size=10).hexdigest()
'6fa1d8fcfd719046d762'
>>> blake2b(digest_size=11).hexdigest()
'eb6ec15daf9546254f0809'
>>> blake2s(digest_size=10).hexdigest()
'1bf21a98c78a1c376ae9'
>>> blake2s(digest_size=11).hexdigest()
'567004bf96e4a25773ebf4'
9 module:

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
1

Randomized hashing¶

By setting salt parameter users can introduce randomization to the hash function. Randomized hashing is useful for protecting against collision attacks on the hash function used in digital signatures.

Randomized hashing is designed for situations where one party, the message preparer, generates all or part of a message to be signed by a second party, the message signer. If the message preparer is able to find cryptographic hash function collisions (i.e., two messages producing the same hash value), then they might prepare meaningful versions of the message that would produce the same hash value and digital signature, but with different results (e.g., transferring $1,000,000 to an account, rather than $10). Cryptographic hash functions have been designed with collision resistance as a major goal, but the current concentration on attacking cryptographic hash functions may result in a given cryptographic hash function providing less collision resistance than expected. Randomized hashing offers the signer additional protection by reducing the likelihood that a preparer can generate two or more messages that ultimately yield the same hash value during the digital signature generation process — even if it is practical to find collisions for the hash function. However, the use of randomized hashing may reduce the amount of security provided by a digital signature when all portions of the message are prepared by the signer.

.

Trong Blake2, muối được xử lý như một đầu vào một lần cho hàm băm trong quá trình khởi tạo, thay vì là đầu vào cho mỗi hàm nén.

Cảnh báo

Băm muối (hoặc chỉ băm) với blake2 hoặc bất kỳ hàm băm mật mã đa năng nào khác, chẳng hạn như SHA-256, không phù hợp với mật khẩu băm. Xem Câu hỏi thường gặp Blake2 để biết thêm thông tin.

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
2

Cá nhân sự

Đôi khi, rất hữu ích để buộc hàm băm để tạo ra các tiêu hóa khác nhau cho cùng một đầu vào cho các mục đích khác nhau. Trích dẫn các tác giả của hàm Hash Skein:

Chúng tôi khuyên tất cả các nhà thiết kế ứng dụng nghiêm túc xem xét làm điều này; Chúng tôi đã thấy nhiều giao thức trong đó một hàm băm được tính toán trong một phần của giao thức có thể được sử dụng trong một phần hoàn toàn khác vì hai tính toán băm được thực hiện trên tương tự. Cá nhân hóa mỗi hàm băm được sử dụng trong giao thức cuối cùng dừng loại tấn công này.

(Gia đình chức năng Hash Skein, trang 21)

Blake2 có thể được cá nhân hóa bằng cách chuyển byte cho đối số người:

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
3

Cá nhân hóa cùng với chế độ khóa cũng có thể được sử dụng để lấy các khóa khác nhau từ một khóa duy nhất.

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
4

Chế độ cây

Ở đây, một ví dụ về việc băm một cây tối thiểu với hai nút lá:

Ví dụ này sử dụng các tiêu hóa nội bộ 64 byte và trả về bản tiêu hóa cuối cùng 32 byte:

>>> hashlib.sha224(b"Nobody inspects the spammish repetition").hexdigest()
'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'
5

Tín dụng¶

Blake2 được thiết kế bởi Jean-Philippe Aumasson, Samuel Neves, Zooko Wilcox-O hèHearn, và Christian Winnerlein dựa trên vòng chung kết Sha-3 Blake được tạo bởi Jean-Philippe Aumasson, Luca Henzen, Willi Meier và Raphael C.- W. Phan.

Nó sử dụng thuật toán cốt lõi từ mật mã Chacha được thiết kế bởi Daniel J. Bernstein.

Việc triển khai STDLIB dựa trên mô -đun pyblake2. Nó được viết bởi Dmitry Hestnykh dựa trên việc thực hiện C được viết bởi Samuel Neves. Các tài liệu đã được sao chép từ pyblake2 và được viết bởi Dmitry Havenykh.

Mã C được viết lại một phần cho Python bởi Christian Heimes.

Sự cống hiến miền công cộng sau đây áp dụng cho cả triển khai chức năng băm C, mã mở rộng và tài liệu này:

Trong phạm vi có thể theo luật, (các) tác giả đã dành tất cả bản quyền và quyền liên quan và lân cận cho phần mềm này cho phạm vi công cộng trên toàn thế giới. Phần mềm này được phân phối mà không có bảo hành.

Bạn nên nhận được một bản sao của sự cống hiến miền công cộng CC0 cùng với phần mềm này. Nếu không, hãy xem https://creativecommons.org/publicdomain/zero/1.0/.

Những người sau đây đã giúp phát triển hoặc đóng góp những thay đổi của họ cho dự án và phạm vi công cộng theo sự cống hiến miền công cộng Creative Commons 1.0 Universal:

  • Alexandr Sokolovskiy