برنامهای بنویسند که نام کاربری و گذارواژه را از کاربر سوال کند و سپس بررسی کند که برای آن نام کاربری، گذرواژه درستی استفاده شده است یا نه. در صورت صحت گذرواژه پیام خوش آمدگویی چاپ کند و در صورت خلاف آن پیام خطا را به کاربر نشان دهد. توجه داشته باشید که گذرواژه ورودی توسط کاربر نمایش داده نمیشود. نمونهی خروجی برنامه باید بصورت زیر باشد:
Enter your username: Mahdi
Enter your password:
Welcome Mahdi!
%%@author Mahdi Hosseini Moghaddam <m.hoseini.m@gmail.com> %%@doc Exercise 15 from "Exercises for programmers" Book %%@reference from <a href="http://erlang.blog.ir">مثال هایی در ارلنگ</a>, %% 2018 %%@copyright 2018 by Mahdi Hosseini Moghaddam %%@version 0.1 -module(ex15). -export([main/0]). %@doc This is my main function, It handles the exceptions and runs the program main() -> try run() catch error:{badkey,_} -> io:format( "Your account does not exist~n"); throw:negativeNumber -> io:format( "Please Enter only non-zero positive number~n") end. %@doc This fucntion print out the result, it first calls readData function then check_login to calculate the result run() -> Account = readData(), Result = check_login(Account), if Result == true -> io:fwrite("Login Succeed! ~n"); true -> io:fwrite("Login failed! ~n") end. %@doc This function check the account exist it first try to retrieve the account, if the account exist it then compare the hash version of the entered password with the stored one. if the account does not exist it prompt different message check_login({User, Passwd}) -> Map = accounts(), Val=maps:get(User, Map), Val == crypto:hash(sha256,Passwd). accounts() -> #{"mahdi" => crypto:hash(sha256,"123456")}. %@doc This function reads the user input the returns the result readData() -> Usr = string:strip(io:get_line("Enter your username: "),right,$\n), io:fwrite("Enter you password:"), Passwd = io:get_password(), {Usr, Passwd}.