Skip to main content
Ruby

RUBY – Protecting passwords with NOECHO

By October 14, 2013September 12th, 2022No Comments



If we have become used to Ruby programming then we will know that we can use gets to obtain user input. One side effect to this that the input is also echoed or printed on the screen. This may be OK for a username but not for passwords. There is simple remedy though in the form of noecho.

#!/usr/bin/ruby
require 'io/console'
print "Enter Password: "
password = STDIN.noecho(&:gets).chomp

You can see in the above code that we add in the module io/console , this is part of the core ruby modules. We then use the method noecho to iterate through the input from gets. We now do not have the password shown on the screen as we type.