Skip to content

Instantly share code, notes, and snippets.

@agners
Last active June 16, 2025 16:16
Show Gist options
  • Save agners/2be849a92202f78489c29a74bb56a19d to your computer and use it in GitHub Desktop.
Save agners/2be849a92202f78489c29a74bb56a19d to your computer and use it in GitHub Desktop.

Revisions

  1. agners revised this gist Jun 16, 2025. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions securetar-extract.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,10 @@
    #!/usr/bin/env python3
    # /// script
    # requires-python = ">=3.10"
    # dependencies = [
    # "securetar",
    # ]
    # ///
    import gzip
    import io
    import os
  2. agners created this gist Jan 23, 2025.
    47 changes: 47 additions & 0 deletions securetar-extract.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/usr/bin/env python3
    import gzip
    import io
    import os
    import tarfile
    from pathlib import Path
    import hashlib
    from securetar import (
    SECURETAR_MAGIC,
    SecureTarFile,
    )

    def password_to_key(password: str) -> bytes:
    """Generate an AES Key from a password.
    Matches the implementation in supervisor.backups.utils.password_to_key.
    """
    key: bytes = password.encode()
    for _ in range(100):
    key = hashlib.sha256(key).digest()
    return key[:16]

    def extract_secure_tar(input_file: Path, output_dir: Path, password: str, bufsize: int) -> None:
    """Extracts an encrypted SecureTar file."""
    os.makedirs(output_dir, exist_ok=True)

    key = password_to_key(password)

    with SecureTarFile(input_file, "r", gzip=True, bufsize=bufsize, key=key) as tar_file:
    tar_file.extractall(path=output_dir, filter='tar')

    if __name__ == "__main__":
    import argparse

    parser = argparse.ArgumentParser(description="Extract an encrypted SecureTar file.")
    parser.add_argument("input_file", type=Path, help="Path to the encrypted SecureTar file.")
    parser.add_argument("output_dir", type=Path, help="Directory where the extracted files will be saved.")
    parser.add_argument("--password", type=str, required=True, help="Password for the SecureTar file.")
    parser.add_argument("--bufsize", type=int, default=10240, help="Buffer size for reading files (default: 10240 bytes).")

    args = parser.parse_args()

    try:
    extract_secure_tar(args.input_file, args.output_dir, args.password, args.bufsize)
    print("Extraction completed successfully.")
    except Exception as e:
    print(f"Error: {e}")