commit
2d25b27042
@ -1,34 +1,65 @@
|
|||||||
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
|
|
||||||
{-| How to use @cryptonite@
|
{- How to use @cryptonite@ with symmetric block ciphers
|
||||||
|
|
||||||
> -- | Beware MUST BE 256bits as we use AES256
|
> {-# LANGUAGE OverloadedStrings #-}
|
||||||
> import Data.ByteString (ByteString)
|
> {-# LANGUAGE ScopedTypeVariables #-}
|
||||||
> import Crypto.Cipher.AES (AES256)
|
> {-# LANGUAGE GADTs #-}
|
||||||
> import Crypto.Cipher.Types (BlockCipher(..), Cipher(..),nullIV)
|
>
|
||||||
> import Crypto.Error (CryptoFailable(..))
|
> import Crypto.Cipher.AES (AES256)
|
||||||
>
|
> import Crypto.Cipher.Types (BlockCipher(..), Cipher(..), nullIV, KeySizeSpecifier(..), IV, makeIV)
|
||||||
> secretKey :: ByteString
|
> import Crypto.Error (CryptoFailable(..), CryptoError(..))
|
||||||
> secretKey = "012-456-89A-CDE-012-456-89A-CDE-"
|
>
|
||||||
>
|
> import qualified Crypto.Random.Types as CRT
|
||||||
> encrypt :: ByteString -> ByteString -> ByteString
|
>
|
||||||
> encrypt secret = ctrCombine ctx nullIV
|
> import Data.ByteArray (ByteArray)
|
||||||
> where
|
> import Data.ByteString (ByteString)
|
||||||
> ctx = cipherInitNoErr (cipherMakeKey (undefined :: AES256) secret)
|
>
|
||||||
> cipherInitNoErr :: BlockCipher c => Key c -> c
|
> -- | Not required, but most general implementation
|
||||||
> cipherInitNoErr (Key k) = case cipherInit k of
|
> data Key c a where
|
||||||
> CryptoPassed a -> a
|
> Key :: (BlockCipher c, ByteArray a) => a -> Key c a
|
||||||
> CryptoFailed e -> error (show e)
|
>
|
||||||
> cipherMakeKey :: Cipher cipher => cipher -> ByteString -> Key cipher
|
> -- | Generates a string of bytes (key) of a specific length for a given block cipher
|
||||||
> cipherMakeKey _ = Key -- Yeah Lazyness!!!!!!
|
> genSecretKey :: forall m c a. (CRT.MonadRandom m, BlockCipher c, ByteArray a) => c -> Int -> m (Key c a)
|
||||||
>
|
> genSecretKey _ = fmap Key . CRT.getRandomBytes
|
||||||
>
|
>
|
||||||
> decrypt :: ByteString -> ByteString -> ByteString
|
> -- | 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 _ = do
|
||||||
|
> bytes :: ByteString <- CRT.getRandomBytes $ blockSize (undefined :: c)
|
||||||
|
> return $ makeIV bytes
|
||||||
|
>
|
||||||
|
> -- | Initialize a block cipher
|
||||||
|
> initCipher :: (BlockCipher c, ByteArray a) => Key c a -> Either CryptoError c
|
||||||
|
> initCipher (Key k) = case cipherInit k of
|
||||||
|
> CryptoFailed e -> Left e
|
||||||
|
> CryptoPassed a -> Right a
|
||||||
|
>
|
||||||
|
> encrypt :: (BlockCipher c, ByteArray a) => Key c a -> IV c -> a -> Either CryptoError a
|
||||||
|
> encrypt secretKey initIV msg =
|
||||||
|
> case initCipher secretKey of
|
||||||
|
> Left e -> Left e
|
||||||
|
> Right c -> Right $ ctrCombine c initIV msg
|
||||||
|
>
|
||||||
|
> decrypt :: (BlockCipher c, ByteArray a) => Key c a -> IV c -> a -> Either CryptoError a
|
||||||
> decrypt = encrypt
|
> decrypt = encrypt
|
||||||
|
>
|
||||||
|
> exampleAES256 :: ByteString -> IO ()
|
||||||
|
> exampleAES256 msg = do
|
||||||
|
> -- secret key needs 256 bits (32 * 8)
|
||||||
|
> secretKey <- genSecretKey (undefined :: AES256) 32
|
||||||
|
> mInitIV <- genRandomIV (undefined :: AES256)
|
||||||
|
> case mInitIV of
|
||||||
|
> Nothing -> error "Failed to generate and initialization vector."
|
||||||
|
> Just initIV -> do
|
||||||
|
> let encryptedMsg = encrypt secretKey initIV msg
|
||||||
|
> decryptedMsg = decrypt secretKey initIV =<< encryptedMsg
|
||||||
|
> case (,) <$> encryptedMsg <*> decryptedMsg of
|
||||||
|
> Left err -> error $ show err
|
||||||
|
> Right (eMsg, dMsg) -> do
|
||||||
|
> putStrLn $ "Original Message: " ++ show msg
|
||||||
|
> putStrLn $ "Message after encryption: " ++ show eMsg
|
||||||
|
> putStrLn $ "Message after decryption: " ++ show dMsg
|
||||||
|
|
||||||
|-}
|
|-}
|
||||||
|
|
||||||
module Crypto.Tutorial () where
|
module Crypto.Tutorial where
|
||||||
|
|
||||||
import Crypto.Cipher.Types
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user