To generate a secure password in Ruby, we can make use of the bcrypt
gem which allows us to easily encrypt and verify passwords.
Here's an example of how to create a hashed password using bcrypt
:
main.rb305 chars16 lines
In this example, we first require the bcrypt
gem. We then create a plaintext password, and use the create
method of BCrypt::Password
to generate a hashed password. We can then store this hashed password in a database (not shown in the example).
To verify a password, we use the BCrypt::Password.new
method to create a new BCrypt::Password
object from the hashed password, and then compare it to the plaintext password using the ==
operator.
By using bcrypt
we are increasing the security of our passwords, as bcrypt
uses a salted hash which makes it much harder for hackers to crack the password.
gistlibby LogSnag