在 URL 参数中使用带有 A-Za-z0-9 的授权码
作为 Web 应用程序的一部分,我需要一个身份验证代码作为 URL 参数传递。 我目前正在使用(在 Rails 中): Digest::SHA1.hexdigest((object_id + rand(255)).to_s)
它提供长字符串,例如: http://myapp.com/objects/1?auth_code=833fe7bdc789dff996f5de46075dcb409b4bb4f0
是的太长了,我想我也许可以在 URL 中使用更多的合法字符来“压缩”这个链,比如除了数字之外的整个大写和小写字母。
你有一个代码片段可以做到这一点吗?
As part of a web application I need an auth-code to pass as a URL parameter.
I am currently using (in Rails) :
Digest::SHA1.hexdigest((object_id + rand(255)).to_s)
Which provides long strings like :
http://myapp.com/objects/1?auth_code=833fe7bdc789dff996f5de46075dcb409b4bb4f0
However it is too long and I think I might be able to "compress" this chain using more legal characters in an URL like the whole uppercase and lowercase alphabet in addition to numbers.
Do you have a code snipplet which does just that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将您的 auth_code 从基数 16(十六进制)转换为使用 [0-9a-z] 的基数 36
就我个人而言,如果您觉得代码太长,我会将其切成两部分。
Converts your auth_code from base 16 (hexadecimal) to base 36 which uses [0-9a-z]
Personally I'd just cut the code in two if you feel it's too long.
由我的一位同事提供:
还有一个使用 32+ 范围内的一堆 ascii 字符,但由于非法字符,它不适合您的用例(url),但您可能想用于密码盐等这是 James Buck 提供的:
通过这两个片段,您可以根据自己的需要对其进行自定义。
Courtesy of a coworker of mine:
There's also one that uses a bunch ascii characters from range 32+, but it isn't suitable for your use case (urls) due to illegal characters, but you might want to use for password salts, etc. This one courtesy of James Buck:
With those two snippets you can probably customize it for your needs.
gpaul 的意思是,哈希函数即使被截断,仍然是哈希函数,只是碰撞的可能性更高,尽管只有 10 位,碰撞率仍然相当低。例如,如果您查看 bit.ly,它们的哈希值非常小,但正如您所指出的,它们使用的是 base-32 而不是 base-16,这并不重要。
重要的是你要问如果人们发生碰撞会有什么风险,因为即使使用完整的 SHA1,仍然有机会发生(从加密角度来说是不可能的)。如果确实没有巨大的危险,我认为您可以减少到 5-10 个字符。
但问题仍然在于它为何重要。在您的电子邮件中,您可能发送了一个人们只需点击的链接,正确吗?如果您能告诉我们网址太长的原因,可能会有更好的选择。
What gpaul is getting at is that hash functions are still hash functions even if they're truncated, there's just a higher chance of collision though with only 10 bits it's still quite a low rate of collision. If you look at bit.ly for instance their hashes are completely miniscule but as you noted they're using base-32 instead of base-16, it doesn't really matter that much.
What's important is for you to ask what's at risk if people collide, because even with full SHA1 there's still the chance (cryptographically impossible). If there's really not a huge danger I think you could go down to 5-10 characters.
But the question still remains of why it matters. In your emails presumably you're sending a link which people just click on correct? There may be a better option entirely if you can tell us why the url is too long.
这是正确的:我的应用程序有用户单击包含授权码的电子邮件上的链接。
当用户单击链接时,他最终会进入 Web 应用程序,但不会被重定向。授权码将保留在 URL 栏中。
我的每个用户都有一个授权码。如果发生冲突,问题在于无法区分两个用户。
感谢您非常有价值的输入,我能够弄清楚在谷歌中输入什么来获取有关该主题的信息:“base 62”。
所以我找到了base62 gem: http://github.com/jtzemp/base62
现在,我的公式是:
这给了我一个 auth_code,例如: Fw1eDr701PY
这是一个很好的折衷方案。如果我的应用程序征服了世界,我仍然可以添加数据库查找以避免重复,但现在我会坚持使用它。
That is correct : my app has Users which click a link on an email containing an auth code.
When the user clicks the link, he ends up on the webapp but he is not redirected. The auth code will stay in the URL bar.
Each one of my users has an auth code. What's at stake if collision occur is that two users cannot be distinguished between each other.
Thanks to your very valuable input I was able to figure out what to type in google to get info on that topic : "base 62".
So I found the base62 gem : http://github.com/jtzemp/base62
And now, my formula is :
which gives me an auth_code like : Fw1eDr701PY
Its a good compromise. If my app conquers the world, I can still add a DB lookup to avoid duplicates but for now I will stick to it.