user715697
user715697

Reputation: 887

Gem to create PublicKey and PrivateKey?

I'm searching for a Gem that will create a private and public key from a given string, that can then be stored in a database. Can anyone recommend such a Gem?

Upvotes: 1

Views: 374

Answers (1)

Gazler
Gazler

Reputation: 84180

Devise allows this for a token via token_authenticable, I have not found a gem that does this however, so my models usually looks like:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :token_authenticatable, :confirmable
  before_save :ensure_authentication_token
  before_save :create_secret_token

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me


  private

  def create_secret_token
    self.secret_token = ActiveSupport::SecureRandom.base64(20).tr('+/=', '-_ ').strip.delete("\n") unless self.secret_token
  end
end

The secret_token generator line ensures that the +/= characters are replaced and then whitespace is removed. "+/=" characters can be difficult to deal with when using RESTful APIs:

ActiveSupport::SecureRandom.base64(20).tr('+/=', '-_ ').strip.delete("\n")

Upvotes: 2

Related Questions