在 Ruby 中生成 GUID [英] Generating GUIDs in Ruby
问题描述
我有一个很容易用 GUID 解决的问题.
I have a problem that is really easily solved with GUIDs.
特别是,对于密码重置工作流程,我想将 GUID 令牌发送到用户的电子邮件,并让他们使用该令牌重置密码.由于 GUID 是唯一的,因此非常安全,并且可以省去我通过电子邮件向他人发送密码的风险.
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.
我注意到有一个 uuid gem @ rubyforge 但它看起来很旧,它会向文件系统.
I noticed there is one uuid gem @ rubyforge but it looks quite old, and it writes stuff to the file system.
有谁知道其他可以创建全局唯一标识符的 gem 吗?
Does anyone know of any other gems that can create a globally unique identifier?
我知道我可以回到:
(0..16).to_a.map{ |a| rand(16).to_s(16) }.join
但它看起来并不是一个合适的 GUID ...
But it does not really seem like a proper GUID ...
推荐答案
从 Ruby 1.9 开始,uuid 生成是内置的.使用 SecureRandom.uuid
函数.
As of Ruby 1.9, uuid generation is built-in. Use the SecureRandom.uuid
function.
例如:
require 'securerandom'
SecureRandom.uuid # => "96b0a57c-d9ae-453f-b56f-3b154eb10cda"
这篇关于在 Ruby 中生成 GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!