use caesar cipher and make the UX better

This commit is contained in:
yanvoi 2024-05-26 13:35:02 +02:00
parent a475ba5648
commit 5599850c59
6 changed files with 40 additions and 32 deletions

View File

@ -108,7 +108,7 @@ application_loop conn mpass = do
list_all_entries conn list_all_entries conn
application_loop conn mpass application_loop conn mpass
Ut.CopyEntryPassword -> do Ut.CopyEntryPassword -> do
return () copy_entry_pass conn mpass
application_loop conn mpass application_loop conn mpass
Ut.AddNewEntry -> do Ut.AddNewEntry -> do
add_entry conn mpass add_entry conn mpass
@ -136,6 +136,24 @@ list_all_entries conn = do
let entries_map = Ut.map_entries entries let entries_map = Ut.map_entries entries
return entries_map return entries_map
copy_entry_pass :: Connection -> String -> IO ()
copy_entry_pass conn mpass = do
entries <- list_all_entries conn
chosen_entry <- Ui.choose_entry "Please choose the entry ID to copy password:"
let result = Map.lookup chosen_entry entries
case result of
Just (service, login) -> do
entry <- Db.get_entry conn (T.pack service) (T.pack login)
setClipboard (Cr.decrypt' mpass (T.unpack $ Db.entryPassword entry))
Ut.set_green
putStrLn "Password copied to clipboard!!!"
Ut.reset_color
Nothing -> do
Ut.set_red
putStrLn "Invalid entry ID!!!"
Ut.reset_color
return ()
add_entry :: Connection -> String -> IO () add_entry :: Connection -> String -> IO ()
add_entry conn mpass = do add_entry conn mpass = do
putStrLn "Please Enter the service name:" putStrLn "Please Enter the service name:"
@ -152,19 +170,19 @@ add_entry conn mpass = do
else do else do
putStrLn "Please Enter the password:" putStrLn "Please Enter the password:"
password <- Ut.get_password password <- Ut.get_password
Db.add_entry conn (T.pack service) (T.pack login) (T.pack (toString (Cr.encrypt' (fromString mpass) (fromString password)))) Db.add_entry conn (T.pack service) (T.pack login) (T.pack (Cr.encrypt' mpass password))
update_entry :: Connection -> String -> IO () update_entry :: Connection -> String -> IO ()
update_entry conn mpass = do update_entry conn mpass = do
Ut.clear_screen Ut.clear_screen
entries <- list_all_entries conn entries <- list_all_entries conn
chosen_entry <- Ui.choose_entry_to_update chosen_entry <- Ui.choose_entry "Please choose the entry ID to update:"
let result = Map.lookup chosen_entry entries let result = Map.lookup chosen_entry entries
case result of case result of
Just (service, login) -> do Just (service, login) -> do
putStrLn "Please Enter the new password:" putStrLn "Please Enter the new password:"
password <- Ut.get_password password <- Ut.get_password
Db.update_entry conn (T.pack service) (T.pack login) (T.pack (toString (Cr.encrypt' (fromString mpass) (fromString password)))) Db.update_entry conn (T.pack service) (T.pack login) (T.pack (Cr.encrypt' mpass password))
Nothing -> do Nothing -> do
Ut.set_red Ut.set_red
putStrLn "Invalid entry ID!!!" putStrLn "Invalid entry ID!!!"
@ -174,7 +192,7 @@ update_entry conn mpass = do
delete_entry :: Connection -> IO () delete_entry :: Connection -> IO ()
delete_entry conn = do delete_entry conn = do
entries <- list_all_entries conn entries <- list_all_entries conn
chosen_entry <- Ui.choose_entry_to_delete chosen_entry <- Ui.choose_entry "Please choose the entry ID to delete:"
let result = Map.lookup chosen_entry entries let result = Map.lookup chosen_entry entries
case result of case result of
Just (service, login) -> do Just (service, login) -> do

View File

@ -28,7 +28,6 @@ dependencies:
- utf8-string - utf8-string
- ansi-terminal - ansi-terminal
- Hclip - Hclip
- cipher-aes
- base >= 4.7 && < 5 - base >= 4.7 && < 5
ghc-options: ghc-options:

View File

@ -7,7 +7,7 @@ module Crypto (
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Data.Hashable import Data.Hashable
import Crypto.Cipher.AES import Data.Char (chr, ord)
pepper :: ByteString pepper :: ByteString
pepper = "pepper" pepper = "pepper"
@ -15,14 +15,12 @@ pepper = "pepper"
hash' :: ByteString -> Int hash' :: ByteString -> Int
hash' = hashWithSalt 0 . mappend pepper hash' = hashWithSalt 0 . mappend pepper
encrypt' :: ByteString -> ByteString -> ByteString encrypt' :: String -> String -> String
encrypt' key plain_text = plain_text encrypt' = caesar_cipher (+)
decrypt' :: ByteString -> ByteString -> ByteString decrypt' :: String -> String -> String
decrypt' key cypher_text = cypher_text decrypt' = caesar_cipher (-)
-- encrypt' :: ByteString -> ByteString -> ByteString -- modulo trzeba zrobić
-- encrypt' = encryptECB . initAES caesar_cipher :: (Int -> Int -> Int) -> String -> String -> String
caesar_cipher operation key text = zipWith (\k t -> chr $ mod (operation (ord t) (ord k)) (ord maxBound)) (cycle key) text
-- decrypt' :: ByteString -> ByteString -> ByteString
-- decrypt' = decryptECB . initAES

View File

@ -64,12 +64,10 @@ update_entry :: Connection -> T.Text -> T.Text -> T.Text -> IO ()
update_entry conn service login new_password = do update_entry conn service login new_password = do
execute conn "UPDATE entries SET entryPassword = ? WHERE entryService = ? AND entryLogin = ?" (new_password, service, login) execute conn "UPDATE entries SET entryPassword = ? WHERE entryService = ? AND entryLogin = ?" (new_password, service, login)
get_entry :: Connection -> Int -> IO (Maybe Entry) get_entry :: Connection -> T.Text -> T.Text -> IO Entry
get_entry conn id = do get_entry conn service login = do
r <- query conn "SELECT * from entries WHERE entryId = ?" (Only id) :: IO [Entry] r <- query conn "SELECT * from entries WHERE entryService = ? AND entryLogin = ?" (service, login) :: IO [Entry]
return $ case r of return $ head r
[] -> Nothing
[entry] -> Just entry
get_all_entries :: Connection -> IO [Entry] get_all_entries :: Connection -> IO [Entry]
get_all_entries conn = query_ conn "SELECT * from entries" get_all_entries conn = query_ conn "SELECT * from entries"

View File

@ -11,14 +11,8 @@ print_entry :: (Int, Db.Entry) -> IO ()
print_entry (num, entry) = do print_entry (num, entry) = do
putStrLn $ show num ++ " " ++ show (Db.entryService entry) ++ " " ++ show (Db.entryLogin entry) ++ " " ++ "********" putStrLn $ show num ++ " " ++ show (Db.entryService entry) ++ " " ++ show (Db.entryLogin entry) ++ " " ++ "********"
choose_entry_to_update :: IO Int choose_entry :: String -> IO Int
choose_entry_to_update = do choose_entry command = do
putStrLn "Please choose the entry ID to update:" putStrLn $ command
entry_id <- getLine
return (read entry_id :: Int)
choose_entry_to_delete :: IO Int
choose_entry_to_delete = do
putStrLn "Please choose the entry ID to delete:"
entry_id <- getLine entry_id <- getLine
return (read entry_id :: Int) return (read entry_id :: Int)

View File

@ -4,6 +4,7 @@ import System.Console.ANSI
import System.IO import System.IO
import Control.Exception import Control.Exception
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Database as Db import qualified Database as Db
@ -44,4 +45,4 @@ get_password = do
return pass return pass
map_entries :: [Db.Entry] -> Map.Map Int (String, String) map_entries :: [Db.Entry] -> Map.Map Int (String, String)
map_entries = Map.fromList . zip [1..] . map (\entry -> (show $ Db.entryService entry, show $ Db.entryLogin entry)) map_entries = Map.fromList . zip [1..] . map (\entry -> (T.unpack $ Db.entryService entry, T.unpack $ Db.entryLogin entry))