Class: ManageIQ::Password::Key
- Inherits:
-
Object
- Object
- ManageIQ::Password::Key
- Defined in:
- lib/manageiq/password.rb
Constant Summary collapse
- GENERATED_KEY_SIZE =
32
Class Method Summary collapse
Instance Method Summary collapse
- #decrypt(str) ⇒ Object
- #decrypt64(str) ⇒ Object
- #encrypt(str) ⇒ Object
- #encrypt64(str) ⇒ Object
-
#initialize(algorithm = nil, key = nil, iv = nil) ⇒ Key
constructor
A new instance of Key.
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(algorithm = nil, key = nil, iv = nil) ⇒ Key
Returns a new instance of Key.
215 216 217 218 219 220 221 |
# File 'lib/manageiq/password.rb', line 215 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
210 211 212 213 |
# File 'lib/manageiq/password.rb', line 210 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
231 232 233 |
# File 'lib/manageiq/password.rb', line 231 def decrypt(str) apply(:decrypt, str) end |
#decrypt64(str) ⇒ Object
235 236 237 |
# File 'lib/manageiq/password.rb', line 235 def decrypt64(str) decrypt(Base64.decode64(str)) end |
#encrypt(str) ⇒ Object
223 224 225 |
# File 'lib/manageiq/password.rb', line 223 def encrypt(str) apply(:encrypt, str) end |
#encrypt64(str) ⇒ Object
227 228 229 |
# File 'lib/manageiq/password.rb', line 227 def encrypt64(str) Base64.strict_encode64(encrypt(str)) end |
#to_h ⇒ Object
243 244 245 246 247 248 249 250 |
# File 'lib/manageiq/password.rb', line 243 def to_h { :algorithm => @algorithm, :key => @key }.tap do |h| h[:iv] = @iv if @iv end end |
#to_s ⇒ Object
239 240 241 |
# File 'lib/manageiq/password.rb', line 239 def to_s @key end |