Using SHA-256 to hash video

  1. each blocks is 1KB
  2. encrypt from the end

Case study by course slide

  • Construction: Merkle-Damgard function
  • Compression function: Davies-Meyer function
  • Block cipher: SHACAL-2

In [1]:
# import
from Crypto.Hash import SHA256

In [2]:
def hash_file(file_name):
    text = list()
    l = None
    
    with open(file_name, "rb") as f:
        '''
            let it like a stack
        '''
        p = f.seek(0,2)
        f.seek(0,0)
        
        q, r = divmod(p,1024)
        for ite in range(q):
            text.insert(0,f.read(1024))
        if(r != 0):
            text.insert(0,f.read(r))

    #h = SHA256.new(m)
    for m in (text):
        '''
            do not need continue hashing
        '''
        if l:
            '''
                put the H_i-1 to the f of Hi
            '''
            h = SHA256.new(m)
            h.update(l)
        else:
            h = SHA256.new()
            h.update(m)
        l = h.digest()

    return h.hexdigest()

In [3]:
print('verify    : '+hash_file('6.2.birthday.mp4_download'))#should be b'03c08f4ee0b576fe319338139c045c89c3e8e9409633bea29442e21425006ea8'
print('tag of 6.1: '+hash_file('6.1.intro.mp4_download'))


verify    : 03c08f4ee0b576fe319338139c045c89c3e8e9409633bea29442e21425006ea8
tag of 6.1: 5b96aece304a1422224f9a41b228416028f9ba26b0d1058f400200f06a589949