How to hide password input from terminal in ruby script
Best method from @eclectic923's answer:
require 'io/console'
password = STDIN.noecho(&:gets).chomp
For 1.9.3 (and above), this requires you adding require 'io/console'
to your code.
Original Answer:
Ruby "Password" is another alternative.
Hide user input in Ruby
Assuming you are using at least ruby 1.9, you can use the noecho
method on IO
: http://www.ruby-doc.org/stdlib-2.0/libdoc/io/console/rdoc/IO.html#method-i-noecho
So:
require 'io/console'
print "Password: "
STDIN.noecho(&:gets).chomp
This will not obfuscate the characters, but will simply leave the input terminal blank.
Ruby how to mask user input using a specific character?
You need this gem: highline. And your code might be like this:
require 'highline/import'
def ask_user_password
puts 'Enter your password'
ask("> ") { |q| q.echo = '*' }
end
If you dont want to echo anything, just change the echo to false.
How can I read a password from the command line in Ruby?
To answer my own question, and for the benefit of anyone else who would like to know, there is a Ruby gem called HighLine that you need.
require 'rubygems'
require 'highline/import'
def get_password(prompt="Enter Password")
ask(prompt) {|q| q.echo = false}
end
thePassword = get_password()
how to show dummy character while typing in terminal in ruby?
There is a gem called highline can do this:
require 'highline'
cli = HighLine.new
cli.ask('password:') { |q| q.echo = 'x' }
How do I use gets/chomp in ruby to login in account without display strings in terminal?
There are a few gems which can do what you want - one of them is password
:
Ruby/Password is a collection of password handling routines for Ruby,
including an interface to CrackLib for the purposes of testing
password strength.require 'password'
# Define and check a password in code
pw = Password.new( "bigblackcat" )
pw.check
# Get and check a password from the keyboard
begin
password = Password.get( "New password: " )
password.check
rescue Password::WeakPassword => reason
puts reason
retry
end
In Ruby, how to ask the terminal not to echo key presses?
You cann also use stty to disable echo :
def getch
%x[stty -echo raw]
c = $stdin.getc
%x[stty echo -raw]
c
end
And in 1.9.3, the io/console library will allow you to do it in a more portable way:
require 'io/console'
$stdin.getch # does the same thing as above :)
Masking password input from the console : Java
A full example ?. Run this code : (NB: This example is best run in the console and not from within an IDE, since the System.console() method might return null in that case.)
import java.io.Console;
public class Main {
public void passwordExample() {
Console console = System.console();
if (console == null) {
System.out.println("Couldn't get Console instance");
System.exit(0);
}
console.printf("Testing password%n");
char[] passwordArray = console.readPassword("Enter your secret password: ");
console.printf("Password entered was: %s%n", new String(passwordArray));
}
public static void main(String[] args) {
new Main().passwordExample();
}
}
How do I convert a password into asterisks while it is being entered?
If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the pwinput
module (formerly called stdiomask):
pip install pwinput
Unlike getpass.getpass()
(which is in the Python Standard Library), the pwinput
module can display *** mask characters as you type. It is also cross-platform, while getpass
is Linux and macOS only.
Example usage:
>>> pwinput.pwinput()
Password: *********
'swordfish'
>>> pwinput.pwinput(mask='X') # Change the mask character.
Password: XXXXXXXXX
'swordfish'
>>> pwinput.pwinput(prompt='PW: ', mask='*') # Change the prompt.
PW: *********
'swordfish'
>>> pwinput.pwinput(mask='') # Don't display anything.
Password:
'swordfish'
Unfortunately this module, like Python's built-in getpass
module, doesn't work in IDLE or Jupyter Notebook.
More details at https://pypi.org/project/pwinput/
Related Topics
Installing Jekyll Without Root
Why Are Methods in Ruby Documentation Preceded by a Hash Sign
Checking If a Variable Is Defined in Sass
"Certificate Verify Failed" Openssl Error When Using Ruby 1.9.3
How to Solve This Trouble to Deploy a Rails App to Heroku
How to Wait for Process to Finish Using Io.Popen
Rails - Render :Action to Target Anchor Tag
Method and Variable Name Is the Same
How to Fix "Your Ruby Version Is 2.3.0, But Your Gemfile Specified 2.2.5" While Server Starting
How to Avoid Nomethoderror For Nil Elements When Accessing Nested Hashes
When to Use Each Method of Launching a Subprocess in Ruby
Match a String Against Multiple Patterns
Launching Background Process in Capistrano Task