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
application_loop conn mpass
Ut.CopyEntryPassword -> do
return ()
copy_entry_pass conn mpass
application_loop conn mpass
Ut.AddNewEntry -> do
add_entry conn mpass
@ -136,6 +136,24 @@ list_all_entries conn = do
let entries_map = Ut.map_entries entries
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 conn mpass = do
putStrLn "Please Enter the service name:"
@ -152,19 +170,19 @@ add_entry conn mpass = do
else do
putStrLn "Please Enter the 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 conn mpass = do
Ut.clear_screen
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
case result of
Just (service, login) -> do
putStrLn "Please Enter the new 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
Ut.set_red
putStrLn "Invalid entry ID!!!"
@ -174,7 +192,7 @@ update_entry conn mpass = do
delete_entry :: Connection -> IO ()
delete_entry conn = do
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
case result of
Just (service, login) -> do

View File

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

View File

@ -7,7 +7,7 @@ module Crypto (
import Data.ByteString (ByteString)
import Data.Hashable
import Crypto.Cipher.AES
import Data.Char (chr, ord)
pepper :: ByteString
pepper = "pepper"
@ -15,14 +15,12 @@ pepper = "pepper"
hash' :: ByteString -> Int
hash' = hashWithSalt 0 . mappend pepper
encrypt' :: ByteString -> ByteString -> ByteString
encrypt' key plain_text = plain_text
encrypt' :: String -> String -> String
encrypt' = caesar_cipher (+)
decrypt' :: ByteString -> ByteString -> ByteString
decrypt' key cypher_text = cypher_text
decrypt' :: String -> String -> String
decrypt' = caesar_cipher (-)
-- encrypt' :: ByteString -> ByteString -> ByteString
-- encrypt' = encryptECB . initAES
-- decrypt' :: ByteString -> ByteString -> ByteString
-- decrypt' = decryptECB . initAES
-- modulo trzeba zrobić
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

View File

@ -64,12 +64,10 @@ update_entry :: Connection -> T.Text -> T.Text -> T.Text -> IO ()
update_entry conn service login new_password = do
execute conn "UPDATE entries SET entryPassword = ? WHERE entryService = ? AND entryLogin = ?" (new_password, service, login)
get_entry :: Connection -> Int -> IO (Maybe Entry)
get_entry conn id = do
r <- query conn "SELECT * from entries WHERE entryId = ?" (Only id) :: IO [Entry]
return $ case r of
[] -> Nothing
[entry] -> Just entry
get_entry :: Connection -> T.Text -> T.Text -> IO Entry
get_entry conn service login = do
r <- query conn "SELECT * from entries WHERE entryService = ? AND entryLogin = ?" (service, login) :: IO [Entry]
return $ head r
get_all_entries :: Connection -> IO [Entry]
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
putStrLn $ show num ++ " " ++ show (Db.entryService entry) ++ " " ++ show (Db.entryLogin entry) ++ " " ++ "********"
choose_entry_to_update :: IO Int
choose_entry_to_update = do
putStrLn "Please choose the entry ID to update:"
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:"
choose_entry :: String -> IO Int
choose_entry command = do
putStrLn $ command
entry_id <- getLine
return (read entry_id :: Int)

View File

@ -4,6 +4,7 @@ import System.Console.ANSI
import System.IO
import Control.Exception
import qualified Data.Map as Map
import qualified Data.Text as T
import qualified Database as Db
@ -44,4 +45,4 @@ get_password = do
return pass
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))