Class: Hunter2::Command

Inherits:
Shebang::Command
  • Object
show all
Defined in:
lib/hunter2/command.rb

Instance Method Summary collapse

Instance Method Details

#addObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hunter2/command.rb', line 17

def add
  if option(:p).nil?
    puts "You need to enter a password."
    exit
  end

  # Encrypt password using FastAES
  encrypted_pass = AES.encrypt(option(:p))

  # Save password and key to database
  password = Hunter2::Model::Password.create_or_update(
    :key      => option(:k),
    :password => encrypted_pass
  )

  puts "Password #{option(:k)} successfully added. Use show -k "+
    "#{option(:k)} to show your password."
end

#deleteObject



52
53
54
55
56
57
# File 'lib/hunter2/command.rb', line 52

def delete
  password = Hunter2::Model::Password.filter(:key => option(:k)).limit(1)
  password.delete

  puts "Password #{option(:k)} successfully deleted."
end

#indexObject



13
14
15
# File 'lib/hunter2/command.rb', line 13

def index

end

#setupObject



72
73
74
75
76
77
78
# File 'lib/hunter2/command.rb', line 72

def setup
  Sequel::Migrator.run(
    Hunter2.database,
    File.expand_path('../../../migrations', __FILE__),
      :target => nil
  )
end

#showObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hunter2/command.rb', line 59

def show
  # Get encrypted password for this key
  password = Hunter2::Model::Password.select(:password) \
    .filter(:key => option(:k)) \
    .limit(1) \
    .single_value

  # Decrypt password
  password = AES.decrypt(password)

  puts "Password for #{option(:key)}: #{password}"
end

#updateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hunter2/command.rb', line 36

def update
  if option(:p).nil?
    puts "You need to enter a password."
    exit
  end

  # Encrypt password using FastAES
  encrypted_pass = AES.encrypt(option(:p))

  # Update password
  password = Hunter2::Model::Password.filter(:key => option(:k)).limit(1)
  password.update(:password => encrypted_pass)

  puts "Password #{option(:k)} successfully updated."
end