diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/Main.hs | 112 |
1 files changed, 112 insertions, 0 deletions
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 @@ | |||
| 1 | module Main | ||
| 2 | ( main | ||
| 3 | ) where | ||
| 4 | |||
| 5 | import Codec.Picture | ||
| 6 | import qualified Data.Vector.Unboxed as VU | ||
| 7 | import Lib | ||
| 8 | import Shell.Utility.Log (info, notice) | ||
| 9 | import Shell.Utility.Verbosity | ||
| 10 | import System.Environment (getArgs, getProgName) | ||
| 11 | import System.Exit (die) | ||
| 12 | import System.IO () | ||
| 13 | import Text.Read (readMaybe) | ||
| 14 | |||
| 15 | run :: Int -> Int -> Verbosity -> IO () | ||
| 16 | run caesarShift bitsPerByte verbosity = do | ||
| 17 | let sourceTextPath = "./res/biblio.txt" | ||
| 18 | let sourceImagePath = "./res/img.bmp" | ||
| 19 | let encryptedTextPath = | ||
| 20 | "./tmp/biblio-" ++ show caesarShift ++ ".txt" | ||
| 21 | let encodedImagePath = | ||
| 22 | "./tmp/img-" ++ show bitsPerByte ++ "-" ++ show caesarShift ++ ".bmp" | ||
| 23 | let decodedTextPath = "./tmp/biblio.txt" | ||
| 24 | notice verbosity $ "Reading text from file `" ++ sourceTextPath ++ "'...\n" | ||
| 25 | inputText <- readFile sourceTextPath | ||
| 26 | info verbosity $ | ||
| 27 | "Text sample (10 symbols): \"" ++ take 10 inputText ++ "\"\n" | ||
| 28 | notice verbosity $ "Encrypting text...\n" | ||
| 29 | let encryptedText = encryptCaesar caesarShift inputText | ||
| 30 | info verbosity $ | ||
| 31 | "Encrypted text sample (10 symbols): \"" ++ take 10 encryptedText ++ "\"\n" | ||
| 32 | notice verbosity $ | ||
| 33 | "Writing encrypted text to file `" ++ encryptedTextPath ++ "'...\n" | ||
| 34 | writeFile encryptedTextPath encryptedText | ||
| 35 | notice verbosity $ "Text saved to `" ++ encryptedTextPath ++ "'\n" | ||
| 36 | notice verbosity $ "Converting text to bits...\n" | ||
| 37 | let encryptedTextBits = textToBits encryptedText | ||
| 38 | info verbosity $ | ||
| 39 | "Encrypted data sample (10 bits): " ++ | ||
| 40 | show (take 10 $ VU.toList encryptedTextBits) ++ "\n" | ||
| 41 | notice verbosity $ "Encoding encrypted data into image...\n" | ||
| 42 | notice verbosity $ "Reading image from file `" ++ sourceImagePath ++ "'...\n" | ||
| 43 | readSourceImageResult <- readImage sourceImagePath | ||
| 44 | case readSourceImageResult of | ||
| 45 | Left err -> error $ "Error reading image: " ++ err | ||
| 46 | Right dynImg -> do | ||
| 47 | let img = convertRGB8 dynImg | ||
| 48 | let width = imageWidth img | ||
| 49 | let height = imageHeight img | ||
| 50 | let totalBits = width * height * 3 * bitsPerByte | ||
| 51 | let bits = | ||
| 52 | encryptedTextBits VU.++ | ||
| 53 | VU.replicate (totalBits - VU.length encryptedTextBits) 0 | ||
| 54 | let resultImage = | ||
| 55 | generateImage (encodePixel bitsPerByte img bits) width height | ||
| 56 | notice verbosity $ | ||
| 57 | "Writing image to file `" ++ encodedImagePath ++ "'...\n" | ||
| 58 | saveBmpImage encodedImagePath (ImageRGB8 resultImage) | ||
| 59 | notice verbosity $ "Image saved to `" ++ encodedImagePath ++ "'\n" | ||
| 60 | notice verbosity $ "Decoding data from image...\n" | ||
| 61 | case extractShift encodedImagePath of | ||
| 62 | Just extractedCaesarShift -> do | ||
| 63 | info verbosity $ | ||
| 64 | "Cipher key extracted from file name: " ++ | ||
| 65 | show extractedCaesarShift ++ "\n" | ||
| 66 | notice verbosity $ | ||
| 67 | "Reading image from file `" ++ encodedImagePath ++ "'...\n" | ||
| 68 | readEncodedImageResult <- readImage encodedImagePath | ||
| 69 | case readEncodedImageResult of | ||
| 70 | Left err -> error $ "Error reading image: " ++ err | ||
| 71 | Right dynImg -> do | ||
| 72 | let img = convertRGB8 dynImg | ||
| 73 | let bits = VU.fromList $ extractBitsFromImage bitsPerByte img | ||
| 74 | info verbosity $ | ||
| 75 | "Data sample (10 bits): " ++ show (take 10 $ VU.toList bits) ++ "\n" | ||
| 76 | notice verbosity $ "Converting text to bits...\n" | ||
| 77 | let encryptedTextFromImage = takeWhile (/= '\NUL') (bitsToText bits) | ||
| 78 | info verbosity $ | ||
| 79 | "Decoded text sample (10 symbols): \"" ++ | ||
| 80 | take 10 encryptedTextFromImage ++ "\"\n" | ||
| 81 | notice verbosity $ "Decrypting text...\n" | ||
| 82 | let decryptedText = | ||
| 83 | decryptCaesar extractedCaesarShift encryptedTextFromImage | ||
| 84 | info verbosity $ | ||
| 85 | "Decrypted text sample (10 symbols): \"" ++ | ||
| 86 | take 10 decryptedText ++ "\"\n" | ||
| 87 | notice verbosity $ | ||
| 88 | "Writing decrypted text to file `" ++ decodedTextPath ++ "'...\n" | ||
| 89 | writeFile decodedTextPath decryptedText | ||
| 90 | notice verbosity $ "Text saved to `" ++ decodedTextPath ++ "'\n" | ||
| 91 | Nothing -> error "Unable to extract cipher key from image filename!" | ||
| 92 | |||
| 93 | main :: IO () | ||
| 94 | main = do | ||
| 95 | args <- getArgs | ||
| 96 | case args of | ||
| 97 | (arg1:arg2:_) -> do | ||
| 98 | let shift = | ||
| 99 | case readMaybe arg1 :: Maybe Int of | ||
| 100 | Just i -> i | ||
| 101 | Nothing -> error "First argument (SHIFT) is not an integer!" | ||
| 102 | let bits = | ||
| 103 | case readMaybe arg2 :: Maybe Int of | ||
| 104 | Just i | ||
| 105 | | 1 <= i && i <= 8 -> i | ||
| 106 | | otherwise -> | ||
| 107 | error "Second argument (BITS) is out of range [1, 8]!" | ||
| 108 | Nothing -> error "Second argument (BITS) is not an integer!" | ||
| 109 | run shift bits verbose | ||
| 110 | _ -> do | ||
| 111 | arg0 <- getProgName | ||
| 112 | die $ "Usage: " ++ arg0 ++ " SHIFT BITS" | ||
