use*_*978 8 python postgresql python-3.x
我通过以下方式生成“难以猜测”的 URL:
import uuid
url = uuid.uuid4()
Run Code Online (Sandbox Code Playgroud)
URL 存储在 Postgres 数据库中具有普通索引的字段中(用于快速搜索)。字段的数据类型是uuid: https://www.postgresql.org/docs/9.1/datatype-uuid.html
创建“难以猜测”URL 的另一种可能性是使用secrets模块并将其存储在某些 Postgres 字符串数据类型中:
import secrets
url = secrets.token_urlsafe()
Run Code Online (Sandbox Code Playgroud)
对于数据库中的快速搜索和随机生成的 url 的安全性,哪个更好?
谢谢
不同的是secrets.token_urlsafe,没有质量保证uuid4。secrets.token_urlsafe用于生成共享秘密。uuid4旨在生成可能的通用唯一标识符。
问题是您可能应该同时使用它们:秘密令牌和您在数据库中查找的标识符:
create table foo (
id uuid primary key,
token text not null
);
Run Code Online (Sandbox Code Playgroud)
请注意, 的长度token_urlsafe应该随着时间的推移而变化,因此未来的 Python 版本可能会生成更长的字符串。
| 归档时间: |
|
| 查看次数: |
3844 次 |
| 最近记录: |