在 Ruby 中生成 GUID

发布于 2024-08-06 06:29:38 字数 451 浏览 2 评论 0原文

我有一个问题,用 GUID 很容易解决。

特别是,对于密码重置工作流程,我想将 GUID 令牌发送到用户的电子邮件并让他们使用该令牌重置密码。由于 GUID 是唯一的,因此这非常安全,并且可以避免我通过电子邮件向人们发送密码,这是有风险的。

我注意到有一个 uuid gem @ ruby​​forge 但它看起来很旧,并且它向文件系统写入内容。

有谁知道还有其他可以创建全球唯一标识符的宝石吗?

我知道我可以回到:

(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 技术交流群。

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

发布评论

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

评论(11

酷遇一生 2024-08-13 06:29:39

这是我从 JavaScript 中学到的一项 Neet 技术:

def uuid
    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".gsub("x") do
        "0123456789ABCDEF"[rand(16)]
    end
end

尽管也可以以更“Ruby 的方式”执行以下操作:

def uuid
    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".gsub("x") do
        rand(16).to_s(16)
    end
end

This is a neet technique I learnt from JavaScript:

def uuid
    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".gsub("x") do
        "0123456789ABCDEF"[rand(16)]
    end
end

Although in a more 'ruby way' one could also do:

def uuid
    "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx".gsub("x") do
        rand(16).to_s(16)
    end
end
梦在深巷 2024-08-13 06:29:39

对于运行 postgreSQL 数据库的 Rails,需要做两件事。

第 1 步:生成脚手架/模型

rails g scaffold manager name:string --primary-key-type=uuid

第 2 步:向迁移文件添加一行

如果您现在 rails db:migrate,您将得到一个 PG::UndefinedFunction: ERROR: function gen_random_uuid( )不存在错误。

因此,将添加到迁移文件中

enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')

示例:

class CreateManagers < ActiveRecord::Migration[7.0]

  enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')

  def change
    create_table :managers, id: :uuid do |t|
      t.string :name
      t.timestamps
    end
  end
end

您已完成!

最后一件事,您只需将 enable_extension 'pgcrypto' except extension_enabled?('pgcrypto') 添加到一个迁移文件(例如,第一次使用 uuid 类型时)。

For rails running a postgreSQL database do 2 things.

Step 1: generate a scaffold/model

rails g scaffold manager name:string --primary-key-type=uuid

Step 2: Add a line to migration file

If you rails db:migrate now, you'll get a PG::UndefinedFunction: ERROR: function gen_random_uuid() does not exist error.

So add this to the migration file

enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')

Example:

class CreateManagers < ActiveRecord::Migration[7.0]

  enable_extension 'pgcrypto' unless extension_enabled?('pgcrypto')

  def change
    create_table :managers, id: :uuid do |t|
      t.string :name
      t.timestamps
    end
  end
end

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).

过气美图社 2024-08-13 06:29:39

对于新的 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:

辞旧 2024-08-13 06:29:39

在深夜编程时,我想出了以下解决方案(基于 Simone 的答案),用于在 Rails 中生成唯一的 GUID。我并不为此感到自豪,但它确实运作得很好。

while Order.find_by_guid(guid = rand(36**8).to_s(36).upcase).present?; end

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.

while Order.find_by_guid(guid = rand(36**8).to_s(36).upcase).present?; end
陈甜 2024-08-13 06:29:39

当我使用这个问题中推荐的uuid gems时,没有人可以生成唯一且随机的UUID。我的答案是一个变通办法,如果我们以后有 gem 来满足请求,你最好在 Ruby 中使用 gem。

我尝试了这个问题中最推荐的 uuid gem,但没有一个让我满意,我们需要唯一且随机的 uuid。我直接在 ruby​​ 中运行系统命令 uuidgen ,我喜欢结果,并在这里分享。

puts `uuidgen`
8adea17d-b918-43e0-b82f-f81b3029f688
puts `uuidgen`
6a4adcce-8f64-41eb-bd7e-e65ee6d11231
puts `uuidgen`
51d5348b-8fc3-4c44-a6f7-9a8588d7f08a
puts `uuidgen`
332a0fa3-7b07-41e1-9fc8-ef804a377e4e

如果与 uuid gem 比较,你就会知道区别。

irb(main):003:0> uuid.generate
=> "40cdf890-ebf5-0132-2250-20c9d088be77"
irb(main):004:0> uuid.generate
=> "4161ac40-ebf5-0132-2250-20c9d088be77"

测试环境为linux和Mac OS环境。

When I used uuid gems recommended in this question, no one can generate unique and random UUID. My answer is a work around, if we have gem later to satisfy the request, you'd better to use gem in Ruby.

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.

puts `uuidgen`
8adea17d-b918-43e0-b82f-f81b3029f688
puts `uuidgen`
6a4adcce-8f64-41eb-bd7e-e65ee6d11231
puts `uuidgen`
51d5348b-8fc3-4c44-a6f7-9a8588d7f08a
puts `uuidgen`
332a0fa3-7b07-41e1-9fc8-ef804a377e4e

if compare with uuid gem, you will know the difference.

irb(main):003:0> uuid.generate
=> "40cdf890-ebf5-0132-2250-20c9d088be77"
irb(main):004:0> uuid.generate
=> "4161ac40-ebf5-0132-2250-20c9d088be77"

Test environment is linux and Mac OS environment.

月下客 2024-08-13 06:29:38

从 Ruby 1.9 开始,uuid 生成是内置的。使用 SecureRandom.uuid 函数。

例如:

require 'securerandom'
SecureRandom.uuid # => "96b0a57c-d9ae-453f-b56f-3b154eb10cda"

As of Ruby 1.9, uuid generation is built-in. Use the SecureRandom.uuid function.

For example:

require 'securerandom'
SecureRandom.uuid # => "96b0a57c-d9ae-453f-b56f-3b154eb10cda"
不乱于心 2024-08-13 06:29:38

如何在 Ruby 中创建小型、独特的令牌

>> require 'digest'
=> []
>> Digest::SHA1.hexdigest("some-random-string")[8..16]
=> "2ebe5597f"

>> SecureRandom.base64(8).gsub("/","_").gsub(/=+$/,"")
=> "AEWQyovNFo0" 

>> rand(36**8).to_s(36)
=> "uur0cj2h"

How to create small, unique tokens in Ruby

>> require 'digest'
=> []
>> Digest::SHA1.hexdigest("some-random-string")[8..16]
=> "2ebe5597f"

>> SecureRandom.base64(8).gsub("/","_").gsub(/=+$/,"")
=> "AEWQyovNFo0" 

>> rand(36**8).to_s(36)
=> "uur0cj2h"
轻拂→两袖风尘 2024-08-13 06:29:38

您查看过 UUIDTools 吗?

UUIDTools 被设计为一个简单的库,用于生成任何各种类型的 UUID(或者 GUID,如果您更喜欢这样称呼它们)。它尽可能符合 RFC 4122。

Did you look at UUIDTools?

UUIDTools was designed to be a simple library for generating any of the various types of UUIDs (or GUIDs if you prefer to call them that). It conforms to RFC 4122 whenever possible.

会发光的星星闪亮亮i 2024-08-13 06:29:38

Google 生成以下 Ruby 库

另外,在 ruby-forum 他们说你可以安装一个 gem (执行 gem uuid 在命令行上安装它),然后

gem 'uuid'
puts UUID.new

在代码中执行以查看新的 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 do

gem 'uuid'
puts UUID.new

in your code to see a new UUID.

(Hint: I Googled for guid ruby)

原谅我要高飞 2024-08-13 06:29:38

Simone Carletti回答的小更新:

SecureRandom.base64(8).gsub("/","_").gsub(/=+$/,"")

=> "AEWQyovNFo0" 

可以替换为:

SecureRandom.urlsafe_base64(8)

Small update to Simone Carletti answer:

SecureRandom.base64(8).gsub("/","_").gsub(/=+$/,"")

=> "AEWQyovNFo0" 

can be replaced with:

SecureRandom.urlsafe_base64(8)
吃兔兔 2024-08-13 06:29:38

创建正确的 mysql、varchar 32 GUID

SecureRandom.uuid.gsub('-','').upcase

To create a proper, mysql, varchar 32 GUID

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