| 1 |
TupleHash256 |
| 2 |
============ |
| 3 |
|
| 4 |
TupleHash256 is a variable-lengh hash function for tuples of byte strings, |
| 5 |
derived from SHA-3, and standardized in `NIST SP 800-185 <https://nvlpubs.nist.gov/nistpubs/specialpublications/nist.sp.800-185.pdf>`_. |
| 6 |
|
| 7 |
TupleHash256 provides a robust way to hash a sequence of byte strings, |
| 8 |
while maintaining the semantics of each single string, and with a security strength of 256 bits. |
| 9 |
|
| 10 |
For example, let's assume that a banking application combines the following strings |
| 11 |
to validate a money transfer: ``deposit``, amount (e.g., ``100``), and username (e.g., ``joe``). |
| 12 |
The application uses SHA256 and naively concatenates the strings to obtain a credential to authorize the operation:: |
| 13 |
|
| 14 |
SHA256("deposit100joe") |
| 15 |
|
| 16 |
A malicious user could register a second user ``000joe``, |
| 17 |
so that the system computes, for the same small transfer:: |
| 18 |
|
| 19 |
SHA256("deposit100000joe") |
| 20 |
|
| 21 |
which is also the same credential that authorizes a much larger transfer to the other user ``joe``. |
| 22 |
|
| 23 |
TupleHash256 makes the composition of all strings into a digest more robust |
| 24 |
by ensuring that the size of each individual byte string is considered. |
| 25 |
Each byte string is submitted in its entirety via the ``update()`` method. |
| 26 |
|
| 27 |
This is an example showing how to generate a TupleHash256 for the 3 bytes strings above:: |
| 28 |
|
| 29 |
>>> from Crypto.Hash import TupleHash256 |
| 30 |
>>> |
| 31 |
>>> hd = TupleHash256.new(digest_bytes=16) |
| 32 |
>>> hd.update(b'deposit') |
| 33 |
>>> hd.update(b'100') |
| 34 |
>>> hd.update(b'joe') |
| 35 |
>>> print(hd.hexdigest()) |
| 36 |
b101225b7e5f1f086fc6d0be01abfa1e |
| 37 |
|
| 38 |
Multiple byte strings can be submitted. |
| 39 |
Each argument is a separate element in the tuple:: |
| 40 |
|
| 41 |
>>> from Crypto.Hash import TupleHash256 |
| 42 |
>>> |
| 43 |
>>> hd = TupleHash128.new(digest_bytes=16) |
| 44 |
>>> hd.update(b'deposit', b'100', b'joe') |
| 45 |
>>> print(hd.hexdigest()) |
| 46 |
b101225b7e5f1f086fc6d0be01abfa1e |
| 47 |
|
| 48 |
Any or even all the byte strings in the sequence can be empty. |
| 49 |
An empty byte string is significant: calling ``update(b'')`` will still contribute to and modify the final digest. |
| 50 |
|
| 51 |
An application can select the length of the digest by means of the initialization parameters ``digest_bytes`` or ``digest_bits``. |
| 52 |
For instance, while the traditional SHA256 can only produce 32-byte tags, |
| 53 |
with TupleHash256 you can produce a 16-byte tag (see the example above) but also a 33-byte tag:: |
| 54 |
|
| 55 |
>>> from Crypto.Hash import TupleHash256 |
| 56 |
>>> |
| 57 |
>>> hd = TupleHash256.new(digest_bytes=33) |
| 58 |
>>> hd.update(b'deposit') |
| 59 |
>>> hd.update(b'100') |
| 60 |
>>> hd.update(b'joe') |
| 61 |
>>> print(hd.hexdigest()) |
| 62 |
29cbb43b90e19bfebf7ff0acfa651a889f106486dae9f9f42c34a48e1b8a7bfa6f |
| 63 |
|
| 64 |
Note how the 16-byte digest is NOT just the truncated version of the 33-byte digest: they are cryptographically uncorrelated. |
| 65 |
|
| 66 |
.. automodule:: Crypto.Hash.TupleHash256 |
| 67 |
:members: |