-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher.hs
35 lines (27 loc) · 950 Bytes
/
cipher.hs
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
import Data.Char
shouldcipher :: Char -> Bool
shouldcipher c = isLetter (c) && isAscii (c)
cipherchar :: Int -> Char -> Char
cipherchar shift c
| shouldcipher c = chr (ord (c) + shift)
| otherwise = c
cipher :: Int -> [Char] -> [Char]
cipher shift plaintext = map (betterchipherchar shift) plaintext
decipher :: Int -> [Char] -> [Char]
decipher shift ciphertext = cipher (- shift) ciphertext
wraparound shift c
| isLower (c) && ord (c) + shift > ord 'z' = True
| isUpper (c) && ord (c) + shift > ord 'Z' = True
| otherwise = False
betterchipherchar :: Int -> Char -> Char
betterchipherchar shift c
| shouldcipher c = chr (ord (c) + adjustedshift)
| otherwise = c
where
adjustedshift =
let shift' = shift `mod` 26
in if (wraparound shift' c)
then shift' - 26
else shift'
-- import Test.QuickCheck
-- quickCheck ((\n -> (\s -> ((decipher n (cipher n s)) == s))) :: Int -> [Char] -> Bool)