Class: ManageIQ::Password::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/manageiq/password.rb

Constant Summary collapse

GENERATED_KEY_SIZE =
32

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm = nil, key = nil, iv = nil) ⇒ Key

Returns a new instance of Key.



169
170
171
172
173
174
175
# File 'lib/manageiq/password.rb', line 169

def initialize(algorithm = nil, key = nil, iv = nil)
  @algorithm = algorithm || "aes-256-cbc"
  @key       = key || generate_key
  @raw_key   = Base64.decode64(@key)
  @iv        = iv
  @raw_iv    = iv && Base64.decode64(iv)
end

Class Method Details

.generate_key(password = nil, salt = nil) ⇒ Object



164
165
166
167
# File 'lib/manageiq/password.rb', line 164

def self.generate_key(password = nil, salt = nil)
  password ||= OpenSSL::Random.random_bytes(GENERATED_KEY_SIZE)
  Base64.strict_encode64(Digest::SHA256.digest("#{password}#{salt}")[0, GENERATED_KEY_SIZE])
end

Instance Method Details

#decrypt(str) ⇒ Object



185
186
187
# File 'lib/manageiq/password.rb', line 185

def decrypt(str)
  apply(:decrypt, str)
end

#decrypt64(str) ⇒ Object



189
190
191
# File 'lib/manageiq/password.rb', line 189

def decrypt64(str)
  decrypt(Base64.decode64(str))
end

#encrypt(str) ⇒ Object



177
178
179
# File 'lib/manageiq/password.rb', line 177

def encrypt(str)
  apply(:encrypt, str)
end

#encrypt64(str) ⇒ Object



181
182
183
# File 'lib/manageiq/password.rb', line 181

def encrypt64(str)
  Base64.strict_encode64(encrypt(str))
end

#to_hObject



197
198
199
200
201
202
203
204
# File 'lib/manageiq/password.rb', line 197

def to_h
  {
    :algorithm => @algorithm,
    :key       => @key
  }.tap do |h|
    h[:iv] = @iv if @iv
  end
end

#to_sObject



193
194
195
# File 'lib/manageiq/password.rb', line 193

def to_s
  @key
end