在 Ruby 中生成 GUID
我有一个问题,用 GUID 很容易解决。
特别是,对于密码重置工作流程,我想将 GUID 令牌发送到用户的电子邮件并让他们使用该令牌重置密码。由于 GUID 是唯一的,因此这非常安全,并且可以避免我通过电子邮件向人们发送密码,这是有风险的。
我注意到有一个 uuid gem @ rubyforge 但它看起来很旧,并且它向文件系统写入内容。
有谁知道还有其他可以创建全球唯一标识符的宝石吗?
我知道我可以回到:
(0..16).to_a.map{ |a| rand(16).to_s(16) }.join
但它看起来并不是一个合适的 GUID ...
I have a problem that is really easily solved with GUIDs.
In particular, for a password reset workflow, I would like to send a GUID token to a user's email and have them reset their password using the token. Since GUIDs are unique, this is pretty secure and saves me emailing people passwords, which is risky.
I noticed there is one uuid gem @ rubyforge but it looks quite old, and it writes stuff to the file system.
Does anyone know of any other gems that can create a globally unique identifier?
I know I can just fall back to:
(0..16).to_a.map{ |a| rand(16).to_s(16) }.join
But it does not really seem like a proper GUID ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
这是我从 JavaScript 中学到的一项 Neet 技术:
尽管也可以以更“Ruby 的方式”执行以下操作:
This is a neet technique I learnt from JavaScript:
Although in a more 'ruby way' one could also do:
对于运行 postgreSQL 数据库的 Rails,需要做两件事。
第 1 步:生成脚手架/模型
第 2 步:向迁移文件添加一行
如果您现在
rails db:migrate
,您将得到一个PG::UndefinedFunction: ERROR: function gen_random_uuid( )不存在
错误。因此,将此添加到迁移文件中
示例:
您已完成!
最后一件事,您只需将
enable_extension 'pgcrypto' except extension_enabled?('pgcrypto')
添加到一个迁移文件(例如,第一次使用 uuid 类型时)。For rails running a postgreSQL database do 2 things.
Step 1: generate a scaffold/model
Step 2: Add a line to migration file
If you
rails db:migrate
now, you'll get aPG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist
error.So add this to the migration file
Example:
You're done!
Oh one last thing, you only nee to add
enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')
to one migration file (e.g. the first time you use uuid type).对于新的 Rails 版本 >= 5.1
对于新的 Rails 版本,您现在可以使用
Digest::UUID
下的方法,例如Digest::UUID.uuid_v4
请参阅 API 文档:
FOR NEW RAILS VERSIONS >= 5.1
For new Rails versions, you can now use
the methods under
Digest::UUID
, e.g.Digest::UUID.uuid_v4
See API Docs:
在深夜编程时,我想出了以下解决方案(基于 Simone 的答案),用于在 Rails 中生成唯一的 GUID。我并不为此感到自豪,但它确实运作得很好。
While programming late at night I came up with the following solution (based off Simone's answer) for generating a unique GUID in Rails. I am not proud of it but it does work quite well.
我尝试了这个问题中最推荐的 uuid gem,但没有一个让我满意,我们需要唯一且随机的 uuid。我直接在 ruby 中运行系统命令
uuidgen
,我喜欢结果,并在这里分享。如果与
uuid
gem 比较,你就会知道区别。测试环境为linux和Mac OS环境。
I try most recommended uuid gems in this question, but no one make me satisfied, we need unique and random uuid. I directly run system command
uuidgen
in ruby, and I like the result, and share here.if compare with
uuid
gem, you will know the difference.Test environment is linux and Mac OS environment.
从 Ruby 1.9 开始,uuid 生成是内置的。使用
SecureRandom.uuid
函数。例如:
As of Ruby 1.9, uuid generation is built-in. Use the
SecureRandom.uuid
function.For example:
如何在 Ruby 中创建小型、独特的令牌
How to create small, unique tokens in Ruby
您查看过 UUIDTools 吗?
Did you look at UUIDTools?
Google 生成以下 Ruby 库。
另外,在 ruby-forum 他们说你可以安装一个 gem (执行
gem uuid
在命令行上安装它),然后在代码中执行以查看新的 UUID。
(提示:我在 Google 上搜索了guid ruby)
Google yields the following Ruby library.
Also, over at ruby-forum they say you can install a gem (execute
gem uuid
on the command line to install it) and then doin your code to see a new UUID.
(Hint: I Googled for guid ruby)
对Simone Carletti回答的小更新:
可以替换为:
Small update to Simone Carletti answer:
can be replaced with:
创建正确的 mysql、varchar 32 GUID
To create a proper, mysql, varchar 32 GUID