{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
module Crypto.Classical.Cipher.Vigenere where
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 = concat . repeat . take (n+1)
where n = floor @Double . logBase 2 . fromIntegral . B.length $ m