Module: PasswordManager
- Defined in:
- lib/password_manager.rb,
lib/password_manager/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
- .ask_password(confirmed = true) ⇒ Object
- .change_password(group) ⇒ Object
- .config_path=(config_path) ⇒ Object
- .export(group) ⇒ Object
- .generate(group) ⇒ Object
- .get(service, group, size = nil) ⇒ Object
- .group_path(group) ⇒ Object
- .import(group) ⇒ Object
- .list ⇒ Object
- .remove(group) ⇒ Object
- .store_keys(group, rsa, password, path = group_path(group)) ⇒ Object
Class Method Details
.ask_password(confirmed = true) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/password_manager.rb', line 79 def self.ask_password(confirmed = true) password = ask("Password:"){ |q| q.echo = '*' } return password if confirmed raise "Passwords didn't match" if ask("Confirm password:"){ |q| q.echo = '*' } != password password end |
.change_password(group) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/password_manager.rb', line 21 def self.change_password(group) password = ask("Password:"){ |q| q.echo = '*' } new_password = ask_password false rsa = OpenSSL::PKey::RSA.new File.read( group_path(group) ), password store_keys group, rsa, new_password end |
.config_path=(config_path) ⇒ Object
8 9 10 11 |
# File 'lib/password_manager.rb', line 8 def self.config_path=(config_path) @config_path = config_path Dir.mkdir(@config_path) unless Dir.exists?(@config_path) end |
.export(group) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/password_manager.rb', line 29 def self.export(group) password = ask("Password:"){ |q| q.echo = '*' } new_password = ask_password false rsa = OpenSSL::PKey::RSA.new File.read( group_path(group) ), password store_keys group, rsa, new_password, "#{group}.pem" end |
.generate(group) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/password_manager.rb', line 13 def self.generate(group) group ||= 'default' key_pair = OpenSSL::PKey::RSA.generate 1024 password = ask_password false store_keys group, key_pair, password end |
.get(service, group, size = nil) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/password_manager.rb', line 59 def self.get(service, group, size = nil) group ||= 'default' size ||= 16 rsa = OpenSSL::PKey::RSA.new File.read( group_path group ), ask_password Base64.encode64(rsa.private_encrypt(service)).gsub(/[\+\/]/, '').chomp[0..size.to_i] rescue puts "Invalid password or group or something else" end |
.group_path(group) ⇒ Object
68 69 70 |
# File 'lib/password_manager.rb', line 68 def self.group_path(group) File.join @config_path, "#{group}.pem" end |
.import(group) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/password_manager.rb', line 37 def self.import(group) password = ask("Password:"){ |q| q.echo = '*' } new_password = ask_password false rsa = OpenSSL::PKey::RSA.new File.read( "#{group}.pem" ), password store_keys group, rsa, new_password end |
.list ⇒ Object
55 56 57 |
# File 'lib/password_manager.rb', line 55 def self.list Dir.glob(File.join @config_path, "*.pem").map{ |x| File.basename(x).match(/([^\.]*)\./)[1] } end |
.remove(group) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/password_manager.rb', line 45 def self.remove(group) if self.get('whatever', group) File.delete group_path(group) else false end rescue false end |
.store_keys(group, rsa, password, path = group_path(group)) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/password_manager.rb', line 72 def self.store_keys(group, rsa, password, path = group_path(group)) cipher = OpenSSL::Cipher::Cipher.new 'aes-256-cbc' private_key = rsa.to_pem cipher, password public_key = rsa.public_key.to_pem cipher, password File.open(path, 'w'){ |f| f.puts(private_key + public_key) } end |