Digesting value in YAML file into MD5 hash - ruby

I have a YAML file containing usernames and passwords.
Overview of YAML:
users:
test:
password: test
test2:
password: test2
I want to encrypt the password value into an MD5 hash using Digest::MD5 for example:
user:
Lost Bam:
password: testtesttest #<=I want to overwrite this password with a MD5 hash
In Digest is there a way to encrypt a hash value? If so how do I implement this into a YAML file?
md5.rb Source:
require 'yaml'
require 'digest'
private
def load_file
File.exist?('info.yml') ? YAML.load_file('info.yml') : {users: {}}
end
def read_file
File.read('info.yml')
end
def save_file( hash )
File.open('info.yml', 'w') { |f| f.write(hash.to_yaml)}
end
def add_user
hash = load_file
hash["users"][prompt('Enter username:')] =
{ "password" => prompt('Enter password:') }
puts "Encrypt information?"
information = gets.chomp
case input
when /yes/i
# hash = Digest::MD5.digest(["password"]'value')<-Doesn't work
#
#This is where I want to be able to encrypt the
#value of the password key that was entered by the user
#
# save_file( hash )
else
puts "Add another?"#Not completed yet
end
save_file( hash )
end
main.rb Source:
require_relative 'md5.rb'
def main
puts <<-END.gsub(/^\s*>/, '')
>
>To load information type "L" to quit system type "Q"
>
END
input = gets.chomp.upcase
case input
when "L"
add_user
when "Q"
exit_system
else
exit_lock
end
end
def exit_system
puts "Exiting..."
exit
end
def exit_lock
puts "Locked out, please contact system administrator"
exit
end
def restart
puts "Encrypt more?"
input = gets.chomp
if input =~ /yes/i
return true
else
exit_system
end
end
def prompt( message )
puts message
gets.chomp
end
main

You can use Digest::MD5:
require 'digest'
Digest::MD5.digest('value')
http://ruby-doc.org/stdlib-2.1.0/libdoc/digest/rdoc/Digest.html

Related

How to valid input to matches data in CSV file

I'm using Ruby 2.7 above. I've been working this task and still learning. I'm pretty sure that I am not using the right code. This task require me to do a mimic atm program. One of the requirements is where I need to check user valid inputs are matches the data in the CSV.file before user can access the program.
I'm using ruby (not allowed to use rails or any advance ruby code). I searched for similar program anywhere for reference but mostly does not involve with CSV file. How do I check that input from user is valid and matches in CSV file? I'm having trouble on how to do a validation and how to valid with two inputs (username and password). This program is run on command-line. Apologies if im not being clear enough. Can you tell me from my code where I'm going wrong please?
I have three .rb files and two csv files. I am not sure if I'm supposed to create two separate csv files.
function.rb (where all the functions)
login.rb (verify username and password from 'user.csv' file before proceed to system.rb file)
system.rb (the main where all data save or changes in 'account.csv' file)
below is function.rb file.
require 'csv'
class Function
def log_in(user)
CSV.foreach('user.csv', 'r', headers => true) do |row|
#check the user is valid, else error
if row[0] == uname && row[1] == pwd
puts "succesfully login"
ATMSystem.main_menu
end
end
if login == false
puts "invalid credentials."
Login.log_menu
end
end
login.rb file
require './function'
class Inn
def signin
function = AtmFunction.new
puts "Account login"
puts "Enter username"
uname = gets.chomp
puts "Enter password"
pwd = gets.chomp
user = [uname, pwd]
function.log_in(user)
end
end
Let's say this is your users.csv file:
name,password
bob,1234
alice,5678
This is one possible option.
Load the file into an array of hashes Enumerable#to_h and Hash#transform_keys:
require 'csv'
data_file = 'user.csv'
user_map = CSV.foreach(data_file, headers: true).map do |row|
row.to_h.transform_keys(&:to_sym)
end
user_map
#=> [{:name=>"bob", :password=>"1234"}, {:name=>"alice", :password=>"5678"}]
Then, given the input from the user:
input_username = 'bob'
input_password = '1234'
Check if user exists and in case compare the password:
user = user_map.find { |h| h[:name] == input_username }
#=> {:name=>"bob", :password=>"1234"}
user[:password] == input_password
#=> true
Check the password if Enumerable#find returns a non nil value: the user doesn't exist:
input_username = 'ron'
user = user_map.find { |h| h[:name] == input_username }
user
#=> nil
Following your implementation you can also write:
login_passed = false
CSV.foreach(data_file, headers: true) do |row|
login_passed = row['name'] == input_username && row['password'] == input_password
break if login_passed
end
login_passed
#=> true (or false)

Decrypt *.csv.gpg file using public / private key

I have a requirement to decrypt a .csv.pgp file that should be decrypted by using the public / private key, shared in public.
I tried to find some resources
https://github.com/rocketjob/rocketjob/wiki/PGP-Encryption-with-Ruby
Ruby: file encryption/decryption with private/public keys
But did not work out.
Thanks in advance!
Assuming you have a Gemfile like:
source 'https://rubygems.org'
gem 'iostreams', '~> 0.14.0'
The following script will prompt you for the receiver's key ID and passphrase
require 'rubygems'
require 'bundler/setup'
require 'io/console'
require 'iostreams'
csv_filename = './data.csv'
encrypted_filename = './secure.pgp'
csv_data = File.read(csv_filename)
puts "Generating sender's key..."
signer_passphrase = 'somethingreallysecure'
sender_key_id = IOStreams::Pgp.generate_key(
name: 'Sender',
email: 'sender#example.org',
passphrase: signer_passphrase
)
puts 'Enter receiver key ID:'
receiver_key_id = gets.strip
puts "Downloading receiver's key..."
puts `gpg --keyserver keyserver.ubuntu.com --recv #{receiver_key_id}`
puts "Encrypting #{csv_filename} to #{encrypted_filename}"
sender_key = IOStreams::Pgp.list_keys(key_id: sender_key_id).first
receiver_key = IOStreams::Pgp.list_keys(key_id: receiver_key_id).first
IOStreams::Pgp::Writer.open(
'secure.pgp',
recipient: receiver_key[:email],
signer: sender_key[:email],
signer_passphrase: signer_passphrase
) do |output|
output.puts(csv_data)
end
puts "Decrypting #{encrypted_filename}"
puts 'Enter receiver passphrase:'
receiver_passphrase = STDIN.noecho(&:gets).chomp
decrypted_data = ''
IOStreams::Pgp::Reader.open('secure.pgp', passphrase: receiver_passphrase) do |stream|
decrypted_data += stream.read(10) until stream.eof?
end
puts ''
puts 'Source data'
puts '--------------'
puts csv_data
puts '--------------'
puts ''
puts 'Decrypted data'
puts '--------------'
puts decrypted_data
puts '--------------'
The bit you may have been missing is calling out to download ("receive") the key from the public server for the recipient.
Thanks to the RocketJob docs for some of the legwork here.

How do I add login functionality (username/password) to my app with CSV

Ruby newbie here. Basically I've got several users in a CSV file (headers below):
first_name,age,location,gender,phone_number,email,username,password
I want user's to login with their username which will check the CSV file for the corresponding username, and when it finds the username it will ask the user for the password, if the passwords match then it will run the 'user_mainmenu' variable which then takes the user to the User Main Menu.
def user_login
print "Enter username: "
username_access = $stdin.gets.chomp
CSV.foreach('users.csv', headers: true) do |row|
if row["#username"] == username_access then
#user = User.new(row.to_hash)
break
end
end
print "Enter password: "
password_access = $stdin.gets.chomp
CSV.foreach('users.csv', headers: true) do |row|
if row["#password"] == password_access then
user_mainmenu
break
end
end
end
I'm pretty sure I'm not using the right code, I'm just using Ruby (not allowed to use Rails as its in a course and we are learning that later).
I can't find any answers anywhere as most involve Rails.
Apologies if there isn't enough info or if I'm not being clear enough, first time posting on here.
You don’t need to read a CSV file twice. Using CSV#open and CSV::Table#new, one might get the data in handy format into memory:
def user_login
# load CSV
csv = CSV::Table.new(CSV.open('users.csv', headers: true))
print "Enter username: "
username_access = $stdin.gets.chomp
# detect row with this username
row = csv.detect { |e| e["username"] == username_access }
# immediately throw if no such user
raise "No such user" unless row
print "Enter password: "
password_access = $stdin.gets.chomp
raise "Wrong password" unless row["password"] == password_access
# everything is fine, logged in, return user
User.new(row.to_hash)
end
Had a mentor help me:
def user_login
login_start
verified(gets.chomp)
end
def verified(input)
user_row = authentication(input)
if user_row
puts 'Please enter your password:'
print "> "
password = gets.chomp
if user_row['password'] == password
user_mainmenu
else
puts "Incorrect password."
sleep 1
user_login
end
else
failed
end
end
def authentication(username)
CSV.open('users.csv', headers: true).find { |row| row['username'] == username }
end
def failed
puts "Username not recognised. Please try again."
sleep(1)
user_login
end
def login_start
puts "Enter username:"
print "> "
end

Ruby script to prompt for password and have user enter password again for verification

I've written a ruby script that asks for a password and assigns it to a variable for user later on. I would like to be able to ask the user for the password again for verification.
Here's what I have now, I'm just not sure how to ask for the password again in order to verify.
#!/usr/bin/ruby
#
require 'rubygems'
require 'highline/import'
def getPassword(prompt)
loop do
word = ask("#{prompt}") { |x| x.echo = "*" }
if word.nil? or word.empty?
puts 'Password cannot be blank.'
else
return word
break
end
end
end
user_password = getPassword('Enter User Password')
This is what I have changed:
#!/usr/bin/ruby
#
require 'rubygems'
require 'highline/import'
def getPassword(prompt)
loop do
word = ask("#{prompt}") { |x| x.echo = "*" }
verify = ask("#{prompt} Again") { |z| z.echo = "*" }
if word != verify
puts "They do not match"
else
puts "They Match"
return word
break
end
end
end
user_password = getPassword('Enter User Password')

How can I store user defined data in a hash

Help, I am a noob, just need some advice on this bit of code. I have got most of my program working this part has me stuped i want to get a name and password. Then make the name the key and the password the value. Now it must be user defined.. Then I must be able to pull that hash info again. I thought that return would work... here is my code
def login_prompt
vault = {}
puts "WELCOME! please enter an existing username: "
username = gets.chomp
checkname = Noxread.new
comparename = checkname.read_file
comparename.keys.include?("#{username}")
if comparename == true
puts "please enter your password: "
password = gets.chomp
vault[username]= password
else puts "username already exists!! would you like to retry? (y/n)"
case answer
when /^y/
login_prompt
when /^n/
exit
end
end
end
so that should gather the info. and this is my code to merge that and an hash that i pulled from a file. in a NoxRead class
require_relative 'read'
require 'csv'
class Noxwrite
attr_accessor :name :password
def initialize
#name = name
#password = password
end
def upsum
x = Noxread.new
y = x.read_file
z = login_prompt
y.merge(z) {|name, password| name + ',' + password}
return y
end
def write_file
ehash = upsum
CSV.open("data.csv", "wb") do |csv|
csv << ehash
end
end
end
What is the problem with this code. Seems fine, apart from the fact that passwords should not be read like this in open text.
When you write something like
user_hash = login_prompt
user_hash will have the hash as desired
{"username"=>"password"}

Resources