Newer
Older
renshu-2021 / syonaimeguri / login / checkpass.rb
@Iwaki Iwaki on 21 Dec 2021 1 KB add new directory
require 'digest/sha2'
require 'active_record'

ActiveRecord::Base.configurations = YAML.load_file('database.yml')
ActiveRecord::Base.establish_connection :development

class Account < ActiveRecord::Base
end

puts "ユーザー名を入力してください"
trial_username = gets
puts "パスワードを入力してください"
trial_passwd = gets

begin
    a = Account.find(trial_username)
    db_username = a.id
    db_salt = a.salt
    db_hashed = a.hashed
    db_algo = a.algo
rescue  #データベースにないユーザー名が入力されたとき
    puts "User #{trial_username} is not found."
    exit(-1)
end

if db_algo == "1"
    trial_hashed = Digest::SHA256.hexdigest(trial_passwd + db_salt)
else
    puts "Unknown algorithm is used for user #{trial_username}"
    exit(-2)
end

puts "--- DB ---"
puts "username = #{db_username}"
puts "salt = #{db_salt}"
puts "algorithm = #{db_algo}"
puts "hashed passwd = #{db_hashed}"
puts ""
puts "--- TRIAL ---"
puts "username = #{trial_username}"
puts "passwd = #{trial_passwd}"
puts "hashed passwd = #{trial_hashed}"
puts ""

if db_hashed == trial_hashed
    puts "Login Success"
else
    puts "Login Failure"
end