{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# 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
newtype Vigenère a = Vigenère { _vigenère :: a } deriving (Eq,Show,Functor)
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