{-# OPTIONS_GHC -fno-cse -fno-full-laziness #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Internal.Pbkdf2Pure (deriveBytes) where
import Cometa.Error (Error (..))
import qualified Cometa.Error.Code as Code
import Cometa.Internal.Error (fromNativeFailure)
import qualified Cometa.Internal.PasswordCrypto as Internal
import Control.Exception (evaluate)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.Text as Text
import Data.Word (Word32)
import System.IO.Unsafe (unsafePerformIO)
{-# NOINLINE deriveBytes #-}
deriveBytes :: ByteString -> ByteString -> Word32 -> Int -> Either Error ByteString
deriveBytes :: ByteString
-> ByteString -> Word32 -> Int -> Either Error ByteString
deriveBytes ByteString
password ByteString
salt Word32
iterations Int
size = IO (Either Error ByteString) -> Either Error ByteString
forall a. IO a -> a
unsafePerformIO (IO (Either Error ByteString) -> Either Error ByteString)
-> IO (Either Error ByteString) -> Either Error ByteString
forall a b. (a -> b) -> a -> b
$ do
Either Error ByteString
result <- (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
-> Word32
-> Int
-> IO (Either NativeFailure ByteString)
Internal.deriveBytes ByteString
password ByteString
salt Word32
iterations Int
size
() -> IO ()
forall a. a -> IO a
evaluate (Either Error ByteString -> ()
forceResult Either Error ByteString
result)
Either Error ByteString -> IO (Either Error ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Either Error ByteString
result
forceResult :: Either Error ByteString -> ()
forceResult :: Either Error ByteString -> ()
forceResult (Right ByteString
bytes) = ByteString -> Int
ByteString.length ByteString
bytes Int -> () -> ()
forall a b. a -> b -> b
`seq` ()
forceResult (Left (NativeError Text
operation ErrorCode
code Text
diagnostic)) =
Text -> Int
Text.length Text
operation Int -> () -> ()
forall a b. a -> b -> b
`seq` ErrorCode -> Integer
Code.toNumber ErrorCode
code Integer -> () -> ()
forall a b. a -> b -> b
`seq` Text -> Int
Text.length Text
diagnostic Int -> () -> ()
forall a b. a -> b -> b
`seq` ()
forceResult (Left (NativeContractViolation Text
operation)) = Text -> Int
Text.length Text
operation Int -> () -> ()
forall a b. a -> b -> b
`seq` ()
forceResult (Left Error
err) = Error
err Error -> () -> ()
forall a b. a -> b -> b
`seq` ()