class UnixCrypt::CommandLine

Constants

Abort

Attributes

options[R]

Public Class Methods

new(argv) click to toggle source
# File lib/unix_crypt/command_line.rb, line 14
def initialize(argv)
  @options = Opts.parse(argv)
end

Public Instance Methods

encrypt() click to toggle source
# File lib/unix_crypt/command_line.rb, line 18
def encrypt
  if @options.password.nil?
    @options.password = ask_password
  else
    password_warning
  end

  begin
    puts @options.hasher.build(@options.password, @options.salt, @options.rounds)
  rescue UnixCrypt::Error => e
    $stderr.puts "password generation failed: #{e.message}"
  end

  clear_string(@options.password)
end

Private Instance Methods

ask_noecho(message) click to toggle source
# File lib/unix_crypt/command_line.rb, line 92
def ask_noecho(message)
  $stderr.print message
  if $no_io_console
    begin
      `stty -echo`
      result = gets
    ensure
      `stty echo`
    end
  else
    result = $stdin.noecho(&:gets)
  end
  $stderr.puts
  result
end
ask_password() click to toggle source
# File lib/unix_crypt/command_line.rb, line 116
def ask_password
  password = ask_noecho("Enter password: ")
  twice    = ask_noecho("Verify password: ")

  if password != twice
    clear_string(password)
    clear_string(twice)
    raise Abort, "Passwords don't match"
  end

  clear_string(twice)
  password.chomp!
end
clear_string(string) click to toggle source
# File lib/unix_crypt/command_line.rb, line 112
def clear_string(string)
  string.replace(" " * string.length)
end
password_warning() click to toggle source
# File lib/unix_crypt/command_line.rb, line 108
def password_warning
  $stderr.puts "warning: providing a password on the command line is insecure"
end