lookyloo/tools/generate_sri.py

24 lines
632 B
Python
Raw Normal View History

2021-06-17 02:38:22 +02:00
#!/usr/bin/env python3
import base64
import hashlib
import json
2024-01-12 17:15:41 +01:00
from typing import Dict, Any
2021-06-17 02:52:27 +02:00
2021-10-18 13:06:43 +02:00
from lookyloo.default import get_homedir
2021-06-17 02:38:22 +02:00
if __name__ == '__main__':
dest_dir = get_homedir() / 'website' / 'web'
2024-01-12 17:15:41 +01:00
to_save: Dict[str, Any] = {'static': {}}
2021-06-17 02:38:22 +02:00
for resource in (dest_dir / 'static').glob('*'):
if resource.name[0] == '.':
continue
2021-06-17 02:38:22 +02:00
with resource.open('rb') as f:
to_save['static'][resource.name] = base64.b64encode(hashlib.sha512(f.read()).digest()).decode('utf-8')
2021-06-17 02:52:27 +02:00
with (dest_dir / 'sri.txt').open('w') as fw:
2021-06-17 03:20:59 +02:00
json.dump(to_save, fw, indent=2, sort_keys=True)