Add checksum verification for portable backups

This commit is contained in:
thePR0M3TH3AN
2025-07-01 21:43:52 -04:00
parent 6faead9f9a
commit c10e2380e5
5 changed files with 71 additions and 11 deletions

View File

@@ -14,8 +14,9 @@ import hashlib
import logging
import sys
import os
import json
import traceback
from typing import Optional
from typing import Optional, Any
from termcolor import colored
@@ -25,6 +26,17 @@ from constants import APP_DIR, SCRIPT_CHECKSUM_FILE
logger = logging.getLogger(__name__)
def canonical_json_dumps(data: Any) -> str:
"""Serialize ``data`` into a canonical JSON string."""
return json.dumps(data, sort_keys=True, separators=(",", ":"))
def json_checksum(data: Any) -> str:
"""Return SHA-256 checksum of canonical JSON serialization of ``data``."""
canon = canonical_json_dumps(data)
return hashlib.sha256(canon.encode("utf-8")).hexdigest()
def calculate_checksum(file_path: str) -> Optional[str]:
"""
Calculates the SHA-256 checksum of the given file.