From 3b19f768ce14df65b4bfcd5cbcb0ef7ec3270d0f Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Tue, 7 Apr 2015 10:56:38 +0100 Subject: [PATCH] add crypto errors standard enumeration and some helper. --- Crypto/Error.hs | 12 ++++++++++ Crypto/Error/Types.hs | 54 +++++++++++++++++++++++++++++++++++++++++++ cryptonite.cabal | 2 ++ 3 files changed, 68 insertions(+) create mode 100644 Crypto/Error.hs create mode 100644 Crypto/Error/Types.hs diff --git a/Crypto/Error.hs b/Crypto/Error.hs new file mode 100644 index 0000000..4657ec0 --- /dev/null +++ b/Crypto/Error.hs @@ -0,0 +1,12 @@ +-- | +-- Module : Crypto.Error +-- License : BSD-style +-- Maintainer : Vincent Hanquez +-- Stability : Stable +-- Portability : Excellent +-- +module Crypto.Error + ( module Crypto.Error.Types + ) where + +import Crypto.Error.Types diff --git a/Crypto/Error/Types.hs b/Crypto/Error/Types.hs new file mode 100644 index 0000000..cb206ed --- /dev/null +++ b/Crypto/Error/Types.hs @@ -0,0 +1,54 @@ +-- | +-- Module : Crypto.Error.Types +-- License : BSD-style +-- Maintainer : Vincent Hanquez +-- Stability : stable +-- Portability : Good +-- +-- Cryptographic Error enumeration and handling +-- +{-# LANGUAGE DeriveDataTypeable #-} +module Crypto.Error.Types + ( CryptoError(..) + , CryptoFailable(..) + ) where + +import qualified Control.Exception as E +import Data.Data + +import Crypto.Internal.Imports + +-- | Enumeration of all possible errors that can be found in this library +data CryptoError = + -- symmetric cipher errors + CryptoError_KeySizeInvalid + | CryptoError_IvSizeInvalid + deriving (Show,Eq,Enum,Data,Typeable) + +instance E.Exception CryptoError + +-- | A simple Either like type to represent a computation that can fail +-- +-- 2 possibles values are: +-- * 'CryptoPassed' : +data CryptoFailable a = + CryptoPassed a + | CryptoFailed CryptoError + +instance Functor CryptoFailable where + fmap f (CryptoPassed a) = CryptoPassed (f a) + fmap _ (CryptoFailed r) = CryptoFailed r + +instance Applicative CryptoFailable where + pure a = CryptoPassed a +instance Monad CryptoFailable where + return a = CryptoPassed a + +throwCryptoError :: CryptoFailable a -> IO a +throwCryptoError = undefined + +eitherCryptoError :: CryptoFailable a -> Either CryptoError a +eitherCryptoError = undefined + +maybeCryptoError :: CryptoFailable a -> Maybe a +maybeCryptoError = undefined diff --git a/cryptonite.cabal b/cryptonite.cabal index 3bdeca9..6a53cbc 100644 --- a/cryptonite.cabal +++ b/cryptonite.cabal @@ -32,6 +32,7 @@ Library Crypto.Cipher.RC4 Crypto.Cipher.Types Crypto.Data.AFIS + Crypto.Error Crypto.MAC.Poly1305 Crypto.MAC.HMAC Crypto.Number.Basic @@ -87,6 +88,7 @@ Library Crypto.Cipher.Types.GF Crypto.Cipher.Types.Stream Crypto.Cipher.Types.Utils + Crypto.Error.Types Crypto.Hash.Utils Crypto.Hash.Utils.Cpu Crypto.Hash.Types