Added more comprehensive tutorial
This commit is contained in:
parent
f9b593520f
commit
c76217f75d
@ -1,34 +1,57 @@
|
|||||||
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
{-# LANGUAGE GADTs #-}
|
||||||
|
|
||||||
{-| How to use @cryptonite@
|
{- How to use @cryptonite@ with symmetric block ciphers
|
||||||
|
|
||||||
> -- | Beware MUST BE 256bits as we use AES256
|
> import Crypto.Cipher.AES (AES256)
|
||||||
> import Data.ByteString (ByteString)
|
> import Crypto.Cipher.Types (BlockCipher(..), Cipher(..), nullIV, KeySizeSpecifier(..))
|
||||||
> import Crypto.Cipher.AES (AES256)
|
> import Crypto.Error (CryptoFailable(..), CryptoError(..))
|
||||||
> import Crypto.Cipher.Types (BlockCipher(..), Cipher(..),nullIV)
|
>
|
||||||
> import Crypto.Error (CryptoFailable(..))
|
> import qualified Crypto.Random.Types as CRT
|
||||||
>
|
>
|
||||||
> secretKey :: ByteString
|
> import Data.ByteArray (ByteArray)
|
||||||
> secretKey = "012-456-89A-CDE-012-456-89A-CDE-"
|
> import Data.ByteString (ByteString)
|
||||||
>
|
>
|
||||||
> encrypt :: ByteString -> ByteString -> ByteString
|
> -- | Not required, but most general implementation
|
||||||
> encrypt secret = ctrCombine ctx nullIV
|
> data Key c a where
|
||||||
> where
|
> Key :: (BlockCipher c, ByteArray a) => a -> Key c a
|
||||||
> ctx = cipherInitNoErr (cipherMakeKey (undefined :: AES256) secret)
|
>
|
||||||
> cipherInitNoErr :: BlockCipher c => Key c -> c
|
> genPrivateKey :: forall m c a. (CRT.MonadRandom m, BlockCipher c, ByteArray a)
|
||||||
> cipherInitNoErr (Key k) = case cipherInit k of
|
> => c -> m (Key c a)
|
||||||
> CryptoPassed a -> a
|
> genPrivateKey _ = fmap Key $ CRT.getRandomBytes $
|
||||||
> CryptoFailed e -> error (show e)
|
> case cipherKeySize (undefined :: c) of
|
||||||
> cipherMakeKey :: Cipher cipher => cipher -> ByteString -> Key cipher
|
> KeySizeRange _ maxSize -> maxSize
|
||||||
> cipherMakeKey _ = Key -- Yeah Lazyness!!!!!!
|
> KeySizeFixed ks -> ks
|
||||||
>
|
> KeySizeEnum [] -> error "No key size specified"
|
||||||
>
|
> KeySizeEnum kss -> last kss -- largest key size
|
||||||
> decrypt :: ByteString -> ByteString -> ByteString
|
>
|
||||||
|
> 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 -> a -> Either CryptoError a
|
||||||
|
> encrypt privKey msg =
|
||||||
|
> case initCipher privKey of
|
||||||
|
> Left e -> Left e
|
||||||
|
> Right c -> Right $ ctrCombine c nullIV msg
|
||||||
|
>
|
||||||
|
> decrypt :: (BlockCipher c, ByteArray a) => Key c a -> a -> Either CryptoError a
|
||||||
> decrypt = encrypt
|
> decrypt = encrypt
|
||||||
|
>
|
||||||
|
> exampleAES256 :: ByteString -> IO ()
|
||||||
|
> exampleAES256 msg = do
|
||||||
|
> privKey <- genPrivateKey (undefined :: AES256)
|
||||||
|
> let eMsg = encrypt privKey msg >>= decrypt privKey
|
||||||
|
> case eMsg of
|
||||||
|
> Left err -> error $ show err
|
||||||
|
> Right msg' -> do
|
||||||
|
> putStrLn $ "Original Message: " ++ show msg
|
||||||
|
> putStrLn $ "Message after encryption & decryption: " ++ show msg'
|
||||||
|
>
|
||||||
|
> -- | More Examples... ?
|
||||||
|
|
||||||
|-}
|
|-}
|
||||||
|
|
||||||
module Crypto.Tutorial () where
|
module Crypto.Tutorial.General where
|
||||||
|
|
||||||
import Crypto.Cipher.Types
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user