How to update hashlib.md5 hash using an existing hash in python?

I have a cached hasher instance:

m1 = hashlib.md5()
m1.update(b'very-very-long-data')
cached_sum = m1

and I would like to update an external hash with the amount cached to:

def append_cached_hash(external_hasher):
    # something like this
    external_hasher.update(cached_sum)

Unfortunately, it does not work, since update () expects bytes. I could again pass the same “very, very, very long” bytes, but he abandons the idea of ​​pre-caching md5 sum for a shared long-data object.

I could do something like the following:

external_hasher.update(cached_sum.hexdigest())

However, it does not produce the same necessary result as:

external_hasher.update(b'very-very-long-data')

How to implement the function above?


The same problem can be formulated in different ways. There are 3 large datasets, and it is necessary to calculate md5 sums using python for all possible combinations. It is allowed to calculate md5 once for each data source.

m1 = hashlib.md5(b'very-big-data-1')
m2 = hashlib.md5(b'very-big-data-2')
m3 = hashlib.md5(b'very-big-data-3')

, ?

print("sum for data 1 and data 2 is:", m1.update(m2))
print("sum for data 1 and data 3 is:", m1.update(m3))
print("sum for data 2 and data 3 is:", m2.update(m3))
print("sum for data 1, data 2 and data 3 is:", m1.update(m2.update(m3)))

!

+4
1

- , , . , hashlib , , .

IMO , - , , . , hashlib .

, , . hash.copy , , :

hash.copy()

( "" ) -. , .

import hashlib

d1 = 'data-1'
d2 = 'data-2'
d3 = 'data-3'

h1 = hashlib.md5(d1)
# instead of hashlib.md5(d1).update(d2), or hashlib.md5(d1 + d2)
h12 = h1.copy()
h12.update(d2)
# instead of hashlib.md5(d1).update(d3), or hashlib.md5(d1 + d3)
h13 = h1.copy()
h13.update(d3)

h2 = hashlib.md5(d2)
# instead of hashlib.md5(d2).update(d1), or hashlib.md5(d2 + d1)
h21 = h2.copy()
h21.update(d1)

# ...

, ?

+4

Source: https://habr.com/ru/post/1548024/


All Articles