Add deterministic hash salt.
This commit is contained in:
parent
4cc68e2535
commit
82c32c2a0b
13
main.py
13
main.py
@ -16,16 +16,16 @@ indexer = None
|
|||||||
|
|
||||||
|
|
||||||
class FileIndexer:
|
class FileIndexer:
|
||||||
def __init__(self, path: str):
|
def __init__(self, path: str, salt: str | None = None):
|
||||||
self.path = Path(path)
|
self.path = Path(path)
|
||||||
self.file_mapping = {}
|
self.file_mapping = {}
|
||||||
self._salt = None
|
self._salt = salt
|
||||||
self._index()
|
self._index()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def salt(self) -> str:
|
def salt(self) -> str:
|
||||||
"""Generate a random salt for hashing"""
|
"""Generate a random salt for hashing"""
|
||||||
if not self._salt:
|
if self._salt is None:
|
||||||
self._salt = secrets.token_hex(16)
|
self._salt = secrets.token_hex(16)
|
||||||
return self._salt
|
return self._salt
|
||||||
|
|
||||||
@ -97,7 +97,11 @@ def initialize_server(args: argparse.Namespace):
|
|||||||
if not src_path.exists():
|
if not src_path.exists():
|
||||||
raise SystemExit(f"Source path {src_path} does not exist")
|
raise SystemExit(f"Source path {src_path} does not exist")
|
||||||
|
|
||||||
indexer = FileIndexer(src_path) if src_path.is_dir() else INDEXER_MAP[src_path.suffix](src_path)
|
indexer = (
|
||||||
|
FileIndexer(src_path, args.salt)
|
||||||
|
if src_path.is_dir()
|
||||||
|
else INDEXER_MAP[src_path.suffix](src_path, args.salt)
|
||||||
|
)
|
||||||
|
|
||||||
print(f"Indexed {len(indexer.file_mapping)} files")
|
print(f"Indexed {len(indexer.file_mapping)} files")
|
||||||
|
|
||||||
@ -167,6 +171,7 @@ if __name__ == "__main__":
|
|||||||
parser.add_argument("source", type=str, help="Path to directory or ZIP archive")
|
parser.add_argument("source", type=str, help="Path to directory or ZIP archive")
|
||||||
parser.add_argument("--host", type=str, default="0.0.0.0", help="Host to bind to")
|
parser.add_argument("--host", type=str, default="0.0.0.0", help="Host to bind to")
|
||||||
parser.add_argument("--port", type=int, default=8000, help="Port to bind to")
|
parser.add_argument("--port", type=int, default=8000, help="Port to bind to")
|
||||||
|
parser.add_argument("--salt", type=str, default=None, help="Salt for hashing file paths")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
initialize_server(args)
|
initialize_server(args)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user