在Ruby中生成向导 [英] Generating Guids in Ruby

查看:65
本文介绍了在Ruby中生成向导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题确实可以通过Guids轻松解决.

I have 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.

我注意到有一个 Guid gem 用于Ruby;但它看起来很旧,并且将内容写入文件系统.

I noticed there is one Guid gem for Ruby; 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?

我知道我可以回到:

(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中生成向导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆