python,哈希函数选择

发布于 2024-08-23 04:22:12 字数 498 浏览 6 评论 0原文

使用 Python 和 Django,我将让我的用户向他们的朋友赠送基于 pdf 的礼物,该朋友将能够通过从电子邮件链接进入我的网站来索取 pdf 礼物。

这是计划

  1. 用户向他的朋友赠送礼物,输入朋友的电子邮件

  2. 在后台,保存一个礼物模型,其中将包含一个唯一的在保存时生成的哈希代码。

  3. 朋友收到电子邮件,提供了下载 pdf 的链接,该链接类似于 (www.mydomain.com/gift/<此处的哈希代码>)

  4. 单击邮寄的链接时,系统会检查此类礼物是否具有给定哈希代码的模型存在。

  5. 如果是这样,下载开始,否则 404。

这是解决这个问题的聪明方法吗?如果是这样,您会推荐什么哈希函数?有趣的是,/gift/ 是向公众开放的,如果有幸找到一个链接,任何人都可以领取它。我计划通过接收者的名字加上礼物模型的 pk 来提供哈希函数

Using Python and Django, I will let my users to give pdf based gifts to their friends, which the said friend will be able to claim pdf by entering to my site from the emailed link.

Here is the plan

  1. User gives a gives to his friend, enters friends email

  2. In the background, a gift model is saved which will contain a uniquely generated hash code at the save.

  3. Friend receives the email, provided the link to download the pdf which will be like (www.mydomain.com/gift/<hash code here>)

  4. When the mailed link is clicked, system checks if such gift model with the given hash code exists.

  5. If so download starts, else 404.

Is this a clever way of solving this? If so what hashing function would you recommend ? It is interesting as the /gift/ is open to the public, if somehow lucky enough to find a link, anyone can claim it. I am planning to feed the hash function by receivers first-last name plus the pk of the gift model

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゝ杯具 2024-08-30 04:22:12

不需要使用哈希,您只需要一个随机令牌。

  1. 创建随机字符串
  2. 如果已使用(不太可能),请重复步骤 1

使字符串足够长,让您满意,很难猜出

生成随机字符串的简单方法是

>>> import os
>>> os.urandom(10).encode('hex')
'3fa0c2f72ff275f48d66'
>>> os.urandom(20).encode('hex')
'ecc1143b3fc90bd99bcd609b326694f13291e3d1'
>>> os.urandom(30).encode('hex')
'd4a9a2cd7b48eca831e9805e68dd6f7db7275b654e55cdec603631a5a355'
>>> 

There is no need to use a hash, you just need a random token.

  1. Create a string of random characters
  2. If it is already used ( unlikely ) repeat step 1

Make the string of characters long enough that you are happy it will be hard to guess

an easy way to generate a random string is

>>> import os
>>> os.urandom(10).encode('hex')
'3fa0c2f72ff275f48d66'
>>> os.urandom(20).encode('hex')
'ecc1143b3fc90bd99bcd609b326694f13291e3d1'
>>> os.urandom(30).encode('hex')
'd4a9a2cd7b48eca831e9805e68dd6f7db7275b654e55cdec603631a5a355'
>>> 
绝不服输 2024-08-30 04:22:12

UUID 非常随机

In [13]: import uuid

In [14]: uuid.uuid4().hex
Out[14]: 'f7a7667e94574e32b3589f84ca35a98d'

UUIDs are pretty random

In [13]: import uuid

In [14]: uuid.uuid4().hex
Out[14]: 'f7a7667e94574e32b3589f84ca35a98d'
杀手六號 2024-08-30 04:22:12

它可能不会完全按照您希望的方式做事,但这个项目将是一个很好的起点:

http://github.com/mogga/django-token-auth/

It may not do things exactly the way you wish, but this project would be a good starting point:

http://github.com/mogga/django-token-auth/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文