Restore Haddock comment in tutorial module

Need to use ordinary comments instead of nested comments
because LANGUAGE pragmas were removed otherwise.

Also adds a table of contents.  We may have other examples
in the future.
This commit is contained in:
Olivier Chéron 2017-05-05 07:21:52 +02:00
parent 8a9bd75dc7
commit 554f0fc701

View File

@ -1,65 +1,66 @@
-- | Examples of how to use @cryptonite@.
module Crypto.Tutorial
( -- * Symmetric block ciphers
-- $symmetric_block_ciphers
) where
{- How to use @cryptonite@ with symmetric block ciphers -- $symmetric_block_ciphers
--
> {-# LANGUAGE OverloadedStrings #-} -- > {-# LANGUAGE OverloadedStrings #-}
> {-# LANGUAGE ScopedTypeVariables #-} -- > {-# LANGUAGE ScopedTypeVariables #-}
> {-# LANGUAGE GADTs #-} -- > {-# LANGUAGE GADTs #-}
> -- >
> import Crypto.Cipher.AES (AES256) -- > import Crypto.Cipher.AES (AES256)
> import Crypto.Cipher.Types (BlockCipher(..), Cipher(..), nullIV, KeySizeSpecifier(..), IV, makeIV) -- > import Crypto.Cipher.Types (BlockCipher(..), Cipher(..), nullIV, KeySizeSpecifier(..), IV, makeIV)
> import Crypto.Error (CryptoFailable(..), CryptoError(..)) -- > import Crypto.Error (CryptoFailable(..), CryptoError(..))
> -- >
> import qualified Crypto.Random.Types as CRT -- > import qualified Crypto.Random.Types as CRT
> -- >
> import Data.ByteArray (ByteArray) -- > import Data.ByteArray (ByteArray)
> import Data.ByteString (ByteString) -- > import Data.ByteString (ByteString)
> -- >
> -- | Not required, but most general implementation -- > -- | Not required, but most general implementation
> data Key c a where -- > data Key c a where
> Key :: (BlockCipher c, ByteArray a) => a -> Key c a -- > Key :: (BlockCipher c, ByteArray a) => a -> Key c a
> -- >
> -- | Generates a string of bytes (key) of a specific length for a given block cipher -- > -- | Generates a string of bytes (key) of a specific length for a given block cipher
> genSecretKey :: forall m c a. (CRT.MonadRandom m, BlockCipher c, ByteArray a) => c -> Int -> m (Key c a) -- > genSecretKey :: forall m c a. (CRT.MonadRandom m, BlockCipher c, ByteArray a) => c -> Int -> m (Key c a)
> genSecretKey _ = fmap Key . CRT.getRandomBytes -- > genSecretKey _ = fmap Key . CRT.getRandomBytes
> -- >
> -- | Generate a random initialization vector for a given block cipher -- > -- | Generate a random initialization vector for a given block cipher
> genRandomIV :: forall m c. (CRT.MonadRandom m, BlockCipher c) => c -> m (Maybe (IV c)) -- > genRandomIV :: forall m c. (CRT.MonadRandom m, BlockCipher c) => c -> m (Maybe (IV c))
> genRandomIV _ = do -- > genRandomIV _ = do
> bytes :: ByteString <- CRT.getRandomBytes $ blockSize (undefined :: c) -- > bytes :: ByteString <- CRT.getRandomBytes $ blockSize (undefined :: c)
> return $ makeIV bytes -- > return $ makeIV bytes
> -- >
> -- | Initialize a block cipher -- > -- | Initialize a block cipher
> initCipher :: (BlockCipher c, ByteArray a) => Key c a -> Either CryptoError c -- > initCipher :: (BlockCipher c, ByteArray a) => Key c a -> Either CryptoError c
> initCipher (Key k) = case cipherInit k of -- > initCipher (Key k) = case cipherInit k of
> CryptoFailed e -> Left e -- > CryptoFailed e -> Left e
> CryptoPassed a -> Right a -- > CryptoPassed a -> Right a
> -- >
> encrypt :: (BlockCipher c, ByteArray a) => Key c a -> IV c -> a -> Either CryptoError a -- > encrypt :: (BlockCipher c, ByteArray a) => Key c a -> IV c -> a -> Either CryptoError a
> encrypt secretKey initIV msg = -- > encrypt secretKey initIV msg =
> case initCipher secretKey of -- > case initCipher secretKey of
> Left e -> Left e -- > Left e -> Left e
> Right c -> Right $ ctrCombine c initIV msg -- > Right c -> Right $ ctrCombine c initIV msg
> -- >
> decrypt :: (BlockCipher c, ByteArray a) => Key c a -> IV c -> a -> Either CryptoError a -- > decrypt :: (BlockCipher c, ByteArray a) => Key c a -> IV c -> a -> Either CryptoError a
> decrypt = encrypt -- > decrypt = encrypt
> -- >
> exampleAES256 :: ByteString -> IO () -- > exampleAES256 :: ByteString -> IO ()
> exampleAES256 msg = do -- > exampleAES256 msg = do
> -- secret key needs 256 bits (32 * 8) -- > -- secret key needs 256 bits (32 * 8)
> secretKey <- genSecretKey (undefined :: AES256) 32 -- > secretKey <- genSecretKey (undefined :: AES256) 32
> mInitIV <- genRandomIV (undefined :: AES256) -- > mInitIV <- genRandomIV (undefined :: AES256)
> case mInitIV of -- > case mInitIV of
> Nothing -> error "Failed to generate and initialization vector." -- > Nothing -> error "Failed to generate and initialization vector."
> Just initIV -> do -- > Just initIV -> do
> let encryptedMsg = encrypt secretKey initIV msg -- > let encryptedMsg = encrypt secretKey initIV msg
> decryptedMsg = decrypt secretKey initIV =<< encryptedMsg -- > decryptedMsg = decrypt secretKey initIV =<< encryptedMsg
> case (,) <$> encryptedMsg <*> decryptedMsg of -- > case (,) <$> encryptedMsg <*> decryptedMsg of
> Left err -> error $ show err -- > Left err -> error $ show err
> Right (eMsg, dMsg) -> do -- > Right (eMsg, dMsg) -> do
> putStrLn $ "Original Message: " ++ show msg -- > putStrLn $ "Original Message: " ++ show msg
> putStrLn $ "Message after encryption: " ++ show eMsg -- > putStrLn $ "Message after encryption: " ++ show eMsg
> putStrLn $ "Message after decryption: " ++ show dMsg -- > putStrLn $ "Message after decryption: " ++ show dMsg
|-}
module Crypto.Tutorial where