Class: Lazylead::Salt

Inherits:
Object
  • Object
show all
Defined in:
lib/lazylead/salt.rb

Overview

A cryptography salt defined in environment variables.

Salt is random data that is used as an additional input to a one-way function that hashes data, a password or passphrase. Salts are used to safeguard passwords in storage. Historically a password was stored in plaintext on a system, but over time additional safeguards developed to protect a user’s password against being read from the system. A salt is one of those methods.

Read more: en.wikipedia.org/wiki/Salt_(cryptography).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, env = ENV.to_h) ⇒ Salt

Each salt should be defined as a environment variable with id, like

salt1=E1F53135E559C253
salt2=84B03D034B409D4E
...
saltN=xxxxxxxxx


50
51
52
53
# File 'lib/lazylead/salt.rb', line 50

def initialize(id, env = ENV.to_h)
  @id = id
  @env = env
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



41
42
43
# File 'lib/lazylead/salt.rb', line 41

def id
  @id
end

Instance Method Details

#decrypt(password) ⇒ Object



59
60
61
# File 'lib/lazylead/salt.rb', line 59

def decrypt(password)
  ActiveSupport::MessageEncryptor.new(@env[@id]).decrypt_and_verify password
end

#encrypt(password) ⇒ Object



55
56
57
# File 'lib/lazylead/salt.rb', line 55

def encrypt(password)
  ActiveSupport::MessageEncryptor.new(@env[@id]).encrypt_and_sign password
end

#specified?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/lazylead/salt.rb', line 63

def specified?
  @env.key?(@id) && !@env[@id].blank?
end