1
0
Fork 0
mirror of https://github.com/Luzifer/browser-privacy.git synced 2024-11-09 17:40:00 +00:00
browser-privacy/checksum.py
Knut Ahlers f163789855
Add automated checksum generation
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-06-10 13:57:17 +02:00

36 lines
844 B
Python

import sys
import re
import codecs
import hashlib
import base64
checksumRegexp = re.compile(
r'^\s*!\s*checksum[\s\-:]+([\w\+\/=]+).*\n', re.I | re.M)
def addChecksum(data):
checksum = calculateChecksum(data)
data = re.sub(checksumRegexp, '', data)
data = re.sub(r'(\r?\n)', r'\1! Checksum: %s\1' % checksum, data, 1)
return data
def calculateChecksum(data):
data = normalize(data)
hash = hashlib.md5()
hash.update(data.encode('utf-8'))
return base64.b64encode(hash.digest()).decode('utf-8').rstrip('=')
def normalize(data):
data = re.sub(r'\r', '', data)
data = re.sub(r'\n+', '\n', data)
data = re.sub(checksumRegexp, '', data)
return data
if __name__ == '__main__':
data = addChecksum(open('filters.txt').read())
with open('filters.txt', 'w') as f:
f.write(data)