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.
178 179 180 181 182 183 184 |
# File 'lib/manageiq/password.rb', line 178 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
173 174 175 176 |
# File 'lib/manageiq/password.rb', line 173 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
194 195 196 |
# File 'lib/manageiq/password.rb', line 194 def decrypt(str) apply(:decrypt, str) end |
#decrypt64(str) ⇒ Object
198 199 200 |
# File 'lib/manageiq/password.rb', line 198 def decrypt64(str) decrypt(Base64.decode64(str)) end |
#encrypt(str) ⇒ Object
186 187 188 |
# File 'lib/manageiq/password.rb', line 186 def encrypt(str) apply(:encrypt, str) end |
#encrypt64(str) ⇒ Object
190 191 192 |
# File 'lib/manageiq/password.rb', line 190 def encrypt64(str) Base64.strict_encode64(encrypt(str)) end |
#to_h ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/manageiq/password.rb', line 206 def to_h { :algorithm => @algorithm, :key => @key }.tap do |h| h[:iv] = @iv if @iv end end |
#to_s ⇒ Object
202 203 204 |
# File 'lib/manageiq/password.rb', line 202 def to_s @key end |