Generating Guids in Ruby

后端 未结 10 1715
南笙
南笙 2020-11-27 10:53

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

相关标签:
10条回答
  • 2020-11-27 11:43

    Google yields the following Ruby library:

    http://raa.ruby-lang.org/project/ruby-guid/

    Also, over at http://www.ruby-forum.com/topic/99262 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)

    0 讨论(0)
  • 2020-11-27 11:43

    To create a proper, mysql, varchar 32 GUID

    SecureRandom.uuid.gsub('-','').upcase
    
    0 讨论(0)
  • 2020-11-27 11:43

    Small update to Simone Carletti answer:

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

    => "AEWQyovNFo0"

    can be replaced with:

    SecureRandom.urlsafe_base64(8)

    0 讨论(0)
  • 2020-11-27 11:50

    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"
    
    0 讨论(0)
提交回复
热议问题