aboutsummaryrefslogtreecommitdiff
path: root/app/Main.hs
blob: 9b2267add6ec1677e5f91a6fbae5c684a3a3754e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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"