module Cometa.Crypto.Emip3 (encrypt, decryptBytes) where
import Cometa.Error (Error (..))
import Cometa.Internal.Error (fromNativeFailure)
import qualified Cometa.Internal.PasswordCrypto as Internal
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
encrypt :: ByteString -> ByteString -> IO (Either Error ByteString)
encrypt :: ByteString -> ByteString -> IO (Either Error ByteString)
encrypt ByteString
password ByteString
plaintext
| Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
maxBound :: Int) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
60) Integer
274877906880 = Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Int -> Error
InvalidEmip3PlaintextLength Int
size))
| Bool
otherwise = (NativeFailure -> Either Error ByteString)
-> (ByteString -> Either Error ByteString)
-> Either NativeFailure ByteString
-> Either Error ByteString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Error -> Either Error ByteString)
-> (NativeFailure -> Error)
-> NativeFailure
-> Either Error ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NativeFailure -> Error
fromNativeFailure) ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (Either NativeFailure ByteString -> Either Error ByteString)
-> IO (Either NativeFailure ByteString)
-> IO (Either Error ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> ByteString -> IO (Either NativeFailure ByteString)
Internal.encrypt ByteString
password ByteString
plaintext
where
size :: Int
size = ByteString -> Int
ByteString.length ByteString
plaintext
decryptBytes :: ByteString -> ByteString -> IO (Either Error ByteString)
decryptBytes :: ByteString -> ByteString -> IO (Either Error ByteString)
decryptBytes ByteString
password ByteString
envelope
| ByteString -> Int
ByteString.length ByteString
envelope Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
60 Bool -> Bool -> Bool
|| Int -> Integer
forall a. Integral a => a -> Integer
toInteger (ByteString -> Int
ByteString.length ByteString
envelope) Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
274877906940 = Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Int -> Error
InvalidEmip3EnvelopeLength (ByteString -> Int
ByteString.length ByteString
envelope)))
| Bool
otherwise = (NativeFailure -> Either Error ByteString)
-> (ByteString -> Either Error ByteString)
-> Either NativeFailure ByteString
-> Either Error ByteString
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Error -> Either Error ByteString)
-> (NativeFailure -> Error)
-> NativeFailure
-> Either Error ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NativeFailure -> Error
fromNativeFailure) ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (Either NativeFailure ByteString -> Either Error ByteString)
-> IO (Either NativeFailure ByteString)
-> IO (Either Error ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> ByteString -> IO (Either NativeFailure ByteString)
Internal.decryptBytes ByteString
password ByteString
envelope