File: md5sum.py

package info (click to toggle)
python-cpl 0.7.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 484 kB
  • sloc: python: 2,882; ansic: 1,710; makefile: 89; sh: 3
file content (20 lines) | stat: -rw-r--r-- 584 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import hashlib

def datamd5(hdulist):
    '''Calculate the MD5SUM of all data regions of a HDUList.
    '''
    md5sum = hashlib.md5()
    for hdu in hdulist:
        if hdu.data is not None:
            md5sum.update(bytes(hdu.data.data))
            pad = b'\0' * (2880 - hdu.data.nbytes % 2880)
            md5sum.update(pad)
    return md5sum.hexdigest()

def verify_md5(hdulist):
    return hdulist[0].header.get('DATAMD5') == datamd5(hdulist)

def update_md5(hdulist):
    md5sum = datamd5(hdulist)
    hdulist[0].header['DATAMD5'] =  (md5sum, 'MD5 checksum')
    return md5sum