module Crypto.Classical.Cipher.Vigenere where
import Control.Applicative
import Crypto.Classical.Cipher.Stream
import Crypto.Classical.Types
import qualified Data.ByteString.Lazy.Char8 as B
import Data.Modular
import Lens.Micro
import Lens.Micro.TH
newtype Vigenère a = Vigenère { _vigenère :: a } deriving (Eq,Show,Functor)
makeLenses ''Vigenère
instance Applicative Vigenère where
pure = Vigenère
Vigenère f <*> Vigenère a = Vigenère $ f a
instance Monad Vigenère where
return = pure
Vigenère a >>= f = f a
instance Cipher [ℤ/26] Vigenère where
encrypt k m = pure . (^. stream) . encrypt (vigKey m k) $ m
decrypt k m = pure . (^. stream) . decrypt (vigKey m k) $ m
vigKey :: B.ByteString -> [ℤ/26] -> [ℤ/26]
vigKey m k = concat . repeat . take (n+1) $ k
where n = floor . logBase 2 . fromIntegral . B.length $ m