Class: Humpass::Database
- Inherits:
-
Object
- Object
- Humpass::Database
- Defined in:
- lib/humpass/database.rb
Instance Attribute Summary collapse
-
#file_path ⇒ Object
Returns the value of attribute file_path.
Instance Method Summary collapse
- #database_password ⇒ Object
- #decrypt(key, data) ⇒ Object
- #encrypt(key, data) ⇒ Object
-
#initialize(file_path = __dir__ + '/humpass.dat') ⇒ Database
constructor
A new instance of Database.
- #read ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(file_path = __dir__ + '/humpass.dat') ⇒ Database
Returns a new instance of Database.
16 17 18 19 |
# File 'lib/humpass/database.rb', line 16 def initialize(file_path = __dir__ + '/humpass.dat') raise 'Configuration not sat!' if Humpass.configuration.nil? @file_path = file_path end |
Instance Attribute Details
#file_path ⇒ Object
Returns the value of attribute file_path.
15 16 17 |
# File 'lib/humpass/database.rb', line 15 def file_path @file_path end |
Instance Method Details
#database_password ⇒ Object
35 36 37 38 |
# File 'lib/humpass/database.rb', line 35 def database_password Humpass.configuration.master_password = gets.chomp if Humpass.configuration.master_password.nil? Humpass.configuration.master_password end |
#decrypt(key, data) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/humpass/database.rb', line 46 def decrypt(key, data) cipher = OpenSSL::Cipher::AES.new(128, :CBC).decrypt cipher.key = Digest::SHA2.hexdigest(key)[0..15] s = data.unpack("C*").pack("c*") begin s.empty? ? "" : cipher.update(s) + cipher.final rescue abort("Wrong Master Password!") end end |
#encrypt(key, data) ⇒ Object
40 41 42 43 44 |
# File 'lib/humpass/database.rb', line 40 def encrypt(key, data) cipher = OpenSSL::Cipher::AES.new(128, :CBC).encrypt cipher.key = Digest::SHA2.hexdigest(key)[0..15] cipher.update(data) + cipher.final end |
#read ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/humpass/database.rb', line 27 def read begin Base64.decode64(decrypt(database_password, File.open(file_path, 'r').read)) rescue '' end end |
#write(data) ⇒ Object
21 22 23 24 25 |
# File 'lib/humpass/database.rb', line 21 def write(data) File.open(file_path, 'w+') { |file| file.write(encrypt(database_password, Base64.encode64(data))) } end |