TTOPM commited on
Commit
a70b493
·
verified ·
1 Parent(s): cc0e514

Create content_hasher.py

Browse files
belel-shield/pearce-shield/content_hasher.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import argparse, hashlib, json, time, os, base64
3
+ from pathlib import Path
4
+
5
+ AUTH_DIR = Path.home()/".belel"
6
+ AUTH_DIR.mkdir(parents=True, exist_ok=True)
7
+ LOG = AUTH_DIR/"auth_log.jsonl"
8
+
9
+ def sha256_file(p):
10
+ h = hashlib.sha256()
11
+ with open(p, "rb") as f:
12
+ for chunk in iter(lambda: f.read(8192), b""):
13
+ h.update(chunk)
14
+ return h.hexdigest()
15
+
16
+ def sha256_text(t):
17
+ return hashlib.sha256(t.encode("utf-8")).hexdigest()
18
+
19
+ def main():
20
+ ap = argparse.ArgumentParser()
21
+ ap.add_argument("--text", help="Raw text to hash")
22
+ ap.add_argument("--file", help="Path to file (image/video/pdf)")
23
+ ap.add_argument("--source", help="Optional source URL")
24
+ args = ap.parse_args()
25
+
26
+ ts = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())
27
+ if args.text:
28
+ digest = sha256_text(args.text)
29
+ item = {"ts": ts, "kind": "text", "sha256": digest, "source": args.source or None}
30
+ elif args.file:
31
+ p = os.path.abspath(args.file)
32
+ digest = sha256_file(p)
33
+ item = {"ts": ts, "kind": "file", "path": p, "sha256": digest, "source": args.source or None}
34
+ else:
35
+ raise SystemExit("Provide --text or --file")
36
+
37
+ with open(LOG, "a") as f:
38
+ f.write(json.dumps(item)+"\n")
39
+ print(json.dumps(item, indent=2))
40
+
41
+ if __name__ == "__main__":
42
+ main()