mirror of
https://github.com/Luzifer/browser-privacy.git
synced 2024-11-09 09:30:00 +00:00
Add automated checksum generation
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
0605dffe1b
commit
f163789855
2 changed files with 41 additions and 0 deletions
5
Makefile
Normal file
5
Makefile
Normal file
|
@ -0,0 +1,5 @@
|
|||
default:
|
||||
|
||||
auto-hook-pre-commit:
|
||||
python3 checksum.py
|
||||
git status --porcelain | grep -q '^M filters.txt' && git add filters.txt || true
|
36
checksum.py
Normal file
36
checksum.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
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)
|
Loading…
Reference in a new issue