Python
import subprocess
import requests
import tempfile
import os
TSA_URL = os.getenv(“TSA_URL”, “https://tsr.open-tsa.eu“)
def timestamp(file_path: str) -> bytes:
“””
Request an RFC 3161 timestamp for a file.
Returns the TSR as bytes.
“””
with tempfile.NamedTemporaryFile(suffix=’.tsq’, delete=False) as f:
tsq_path = f.name
try:
subprocess.run(
[‘openssl’, ‘ts’, ‘-query’,
‘-data’, file_path,
‘-cert’, ‘-sha256’, ‘-no_nonce’,
‘-out’, tsq_path],
check=True,
capture_output=True
)
with open(tsq_path, ‘rb’) as f:
tsq_data = f.read()
response = requests.post(
TSA_URL,
data=tsq_data,
headers={“Content-Type”: “application/timestamp-query”},
timeout=15
)
response.raise_for_status()
return response.content
finally:
os.unlink(tsq_path)
# Usage
if __name__ == “__main__”:
tsr = timestamp(“document.pdf”)
with open(“document.tsr”, “wb”) as f:
f.write(tsr)
print(f”Timestamp saved ({len(tsr)} bytes)”)
Requirements
pip install requests