From a4aafb5caea268dae350bdaa6a9c53b65b00910e Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 07:19:33 +0300 Subject: publish --- app/Main.hs | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 app/Main.hs (limited to 'app/Main.hs') diff --git a/app/Main.hs b/app/Main.hs new file mode 100644 index 0000000..9b2267a --- /dev/null +++ b/app/Main.hs @@ -0,0 +1,112 @@ +module Main + ( main + ) where + +import Codec.Picture +import qualified Data.Vector.Unboxed as VU +import Lib +import Shell.Utility.Log (info, notice) +import Shell.Utility.Verbosity +import System.Environment (getArgs, getProgName) +import System.Exit (die) +import System.IO () +import Text.Read (readMaybe) + +run :: Int -> Int -> Verbosity -> IO () +run caesarShift bitsPerByte verbosity = do + let sourceTextPath = "./res/biblio.txt" + let sourceImagePath = "./res/img.bmp" + let encryptedTextPath = + "./tmp/biblio-" ++ show caesarShift ++ ".txt" + let encodedImagePath = + "./tmp/img-" ++ show bitsPerByte ++ "-" ++ show caesarShift ++ ".bmp" + let decodedTextPath = "./tmp/biblio.txt" + notice verbosity $ "Reading text from file `" ++ sourceTextPath ++ "'...\n" + inputText <- readFile sourceTextPath + info verbosity $ + "Text sample (10 symbols): \"" ++ take 10 inputText ++ "\"\n" + notice verbosity $ "Encrypting text...\n" + let encryptedText = encryptCaesar caesarShift inputText + info verbosity $ + "Encrypted text sample (10 symbols): \"" ++ take 10 encryptedText ++ "\"\n" + notice verbosity $ + "Writing encrypted text to file `" ++ encryptedTextPath ++ "'...\n" + writeFile encryptedTextPath encryptedText + notice verbosity $ "Text saved to `" ++ encryptedTextPath ++ "'\n" + notice verbosity $ "Converting text to bits...\n" + let encryptedTextBits = textToBits encryptedText + info verbosity $ + "Encrypted data sample (10 bits): " ++ + show (take 10 $ VU.toList encryptedTextBits) ++ "\n" + notice verbosity $ "Encoding encrypted data into image...\n" + notice verbosity $ "Reading image from file `" ++ sourceImagePath ++ "'...\n" + readSourceImageResult <- readImage sourceImagePath + case readSourceImageResult of + Left err -> error $ "Error reading image: " ++ err + Right dynImg -> do + let img = convertRGB8 dynImg + let width = imageWidth img + let height = imageHeight img + let totalBits = width * height * 3 * bitsPerByte + let bits = + encryptedTextBits VU.++ + VU.replicate (totalBits - VU.length encryptedTextBits) 0 + let resultImage = + generateImage (encodePixel bitsPerByte img bits) width height + notice verbosity $ + "Writing image to file `" ++ encodedImagePath ++ "'...\n" + saveBmpImage encodedImagePath (ImageRGB8 resultImage) + notice verbosity $ "Image saved to `" ++ encodedImagePath ++ "'\n" + notice verbosity $ "Decoding data from image...\n" + case extractShift encodedImagePath of + Just extractedCaesarShift -> do + info verbosity $ + "Cipher key extracted from file name: " ++ + show extractedCaesarShift ++ "\n" + notice verbosity $ + "Reading image from file `" ++ encodedImagePath ++ "'...\n" + readEncodedImageResult <- readImage encodedImagePath + case readEncodedImageResult of + Left err -> error $ "Error reading image: " ++ err + Right dynImg -> do + let img = convertRGB8 dynImg + let bits = VU.fromList $ extractBitsFromImage bitsPerByte img + info verbosity $ + "Data sample (10 bits): " ++ show (take 10 $ VU.toList bits) ++ "\n" + notice verbosity $ "Converting text to bits...\n" + let encryptedTextFromImage = takeWhile (/= '\NUL') (bitsToText bits) + info verbosity $ + "Decoded text sample (10 symbols): \"" ++ + take 10 encryptedTextFromImage ++ "\"\n" + notice verbosity $ "Decrypting text...\n" + let decryptedText = + decryptCaesar extractedCaesarShift encryptedTextFromImage + info verbosity $ + "Decrypted text sample (10 symbols): \"" ++ + take 10 decryptedText ++ "\"\n" + notice verbosity $ + "Writing decrypted text to file `" ++ decodedTextPath ++ "'...\n" + writeFile decodedTextPath decryptedText + notice verbosity $ "Text saved to `" ++ decodedTextPath ++ "'\n" + Nothing -> error "Unable to extract cipher key from image filename!" + +main :: IO () +main = do + args <- getArgs + case args of + (arg1:arg2:_) -> do + let shift = + case readMaybe arg1 :: Maybe Int of + Just i -> i + Nothing -> error "First argument (SHIFT) is not an integer!" + let bits = + case readMaybe arg2 :: Maybe Int of + Just i + | 1 <= i && i <= 8 -> i + | otherwise -> + error "Second argument (BITS) is out of range [1, 8]!" + Nothing -> error "Second argument (BITS) is not an integer!" + run shift bits verbose + _ -> do + arg0 <- getProgName + die $ "Usage: " ++ arg0 ++ " SHIFT BITS" -- cgit v1.3.1