Class: ManageIQ::Password::Key
- Inherits:
-
Object
- Object
- ManageIQ::Password::Key
- Defined in:
- lib/manageiq/password.rb
Constant Summary collapse
- GENERATED_KEY_SIZE =
32
Instance Attribute Summary collapse
-
#key ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute key.
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
Constructor Details
#initialize(algorithm = nil, key = nil, iv = nil) ⇒ Key
Returns a new instance of Key.
182 183 184 185 186 187 188 |
# File 'lib/manageiq/password.rb', line 182 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 |
Instance Attribute Details
#key ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute key.
179 180 181 |
# File 'lib/manageiq/password.rb', line 179 def key @key end |
Class Method Details
.generate_key(password = nil, salt = nil) ⇒ Object
174 175 176 177 |
# File 'lib/manageiq/password.rb', line 174 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
198 199 200 |
# File 'lib/manageiq/password.rb', line 198 def decrypt(str) apply(:decrypt, str) end |
#decrypt64(str) ⇒ Object
202 203 204 |
# File 'lib/manageiq/password.rb', line 202 def decrypt64(str) decrypt(Base64.decode64(str)) end |
#encrypt(str) ⇒ Object
190 191 192 |
# File 'lib/manageiq/password.rb', line 190 def encrypt(str) apply(:encrypt, str) end |
#encrypt64(str) ⇒ Object
194 195 196 |
# File 'lib/manageiq/password.rb', line 194 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 |