网站首页/ 信息中心/ 技术指南/

零门槛实操:搭建支持时间戳服务的档案系统完整步骤

发布时间:2026年06月10日 14:50:03 浏览量:0

前置准备

需安装环境:1. Python3.8+;2. 依赖包。Python安装方式:Linux执行`sudo apt install python3 python3-pip`;Mac执行`brew install python3`;Windows从https://www.python.org/downloads/下载,安装时勾选「Add Python to PATH」选项。依赖安装命令:执行以下命令完成核心库安装: ``` pip install fastapi uvicorn pyhanko[timestamp] python-multipart ```

步骤1:搭建本地时间戳服务(TSA)

TSA用于为档案生成可信时间戳,需单独启动服务终端,操作如下:

1.1 生成TSA密钥与证书

执行以下命令生成有效期1年的TSA签名密钥和证书: ``` openssl req -x509 -newkey rsa:2048 -keyout tsa_key.pem -out tsa_cert.pem -days 365 -nodes -subj "/C=CN/ST=Beijing/L=Beijing/O=Local TSA/OU=Dev/CN=Local Timestamp Authority" ``` 注意

1.2 启动TSA服务

新开一个终端窗口,执行以下命令启动TSA服务: ``` pyhanko tsa serve --port 8080 --key tsa_key.pem --cert tsa_cert.pem ``` 服务启动后会提示「TSA running at http://0.0.0.0:8080」,此时不要关闭该终端,需保持运行状态。

步骤2:搭建带时间戳的档案系统核心服务

本服务实现档案上传、存储、时间戳绑定、下载功能,直接编写代码启动即可:

2.1 编写档案系统代码(app.py)

零门槛实操:搭建支持时间戳服务的档案系统完整步骤

新建名为`app.py`的文件,将以下内容完整复制写入: ```python from fastapi import FastAPI, File, UploadFile, HTTPException import sqlite3 import os from datetime import datetime import httpx app = FastAPI() 本地TSA服务地址(与步骤1端口对应) TSA_URL = "http://localhost:8080/timestamp" 档案数据库路径 DB_PATH = "archives.db" 初始化档案数据库 def init_db(): conn = sqlite3.connect(DB_PATH) c = conn.cursor() 创建档案表(自动存在则跳过) c.execute('''CREATE TABLE IF NOT EXISTS archives (id INTEGER PRIMARY KEY AUTOINCREMENT, filename TEXT, content BLOB, timestamp TEXT, create_time TEXT)''') conn.commit() conn.close() init_db() 上传档案并生成绑定时间戳 @app.post("/upload") async def upload_archive(file: UploadFile = File(...)): try: 读取上传文件内容 content = await file.read() 向本地TSA申请时间戳 async with httpx.AsyncClient() as client: resp = await client.post(TSA_URL, content=content) resp.raise_for_status() timestamp_hex = resp.content.hex() 档案信息存入数据库 conn = sqlite3.connect(DB_PATH) c = conn.cursor() create_time = datetime.utcnow().isoformat() c.execute('INSERT INTO archives (filename, content, timestamp, create_time) VALUES (?, ?, ?, ?)', (file.filename, content, timestamp_hex, create_time)) conn.commit() archive_id = c.lastrowid conn.close() 返回上传结果 return {"code":0, "msg":"上传成功", "data":{"id":archive_id, "filename":file.filename, "timestamp_preview":timestamp_hex[:20]+"..."}} except Exception as e: raise HTTPException(status_code=500, detail=f"处理失败: {str(e)}") 获取档案列表 @app.get("/archives") def get_archives(): conn = sqlite3.connect(DB_PATH) c = conn.cursor() c.execute('SELECT id, filename, create_time FROM archives ORDER BY create_time DESC') archives = c.fetchall() conn.close() return {"code":0, "data":[{"id":a[0], "filename":a[1], "create_time":a[2]} for a in archives]} 下载指定档案 @app.get("/download/{archive_id}") def download_archive(archive_id: int): conn = sqlite3.connect(DB_PATH) c = conn.cursor() c.execute('SELECT filename, content FROM archives WHERE id = ?', (archive_id,)) archive = c.fetchone() conn.close() if not archive: raise HTTPException(status_code=404, detail="档案不存在") return {"filename":archive[0], "content":archive[1]} ```

2.2 启动档案服务

回到最初的终端窗口(或新建非TSA终端),执行以下命令启动档案服务: ``` uvicorn app:app --host 0.0.0.0 --port 8000 --reload ``` 服务启动后,访问http://localhost:8000/docs可查看自动生成的接口文档,测试上传、列表、下载功能。

步骤3:验证时间戳有效性

需确认档案对应的时间戳未被篡改,操作如下:

3.1 准备验证文件与参数

1. 从档案服务下载某条测试档案,保存为`test_archive.bin`;2. 从`archives.db`中取出该档案对应的`timestamp`字段(完整hex字符串)。

3.2 执行验证命令

在终端执行以下命令,替换「时间戳hex字符串」为实际获取的hex值: ``` pyhanko tsa verify --cert tsa_cert.pem --data test_archive.bin --timestamp 时间戳hex字符串 ``` 若返回「Timestamp verified successfully」则说明时间戳有效,且未被篡改。

至此,支持时间戳服务的档案系统已完成搭建,所有操作无需额外付费工具,全流程可直接复现。

档案库房系统选型避坑指南,看完省下几十万
档案库房系统选型避坑指南,看完省下几十万
你有没有经历过这种事?老板突然冲进办公室,让你找一份三年前的合同。你满头大汗钻进库房,结果发现堆得像山的纸盒子上全是灰。你翻箱倒柜两小时,最后两手空空回去挨骂。这种痛,谁懂啊?其实,这不是你能力不行,...
2026年06月10日 14:50:03
微信咨询
电话联系
QQ客服
微信咨询一对一服务
服务热线: 028-8744 4417
QQ客服: 2305721818