module Botan.Bcrypt
(
WorkFactor(..)
, workFactor
, toWorkFactor
, Password(..)
, BcryptDigest(..)
, bcryptGenerate
, bcryptGenerateRNG
, unsafeBcryptGenerateRNG
, bcryptValidate
, unsafeBcryptValidate
) where
import qualified Data.ByteString as ByteString
import qualified Botan.Low.Bcrypt as Low
import qualified Botan.Low.RNG as Low
import Botan.Error
import Botan.Prelude
import Botan.RNG
data WorkFactor
= Fast
| Good
| Strong
| WorkFactor Low.BcryptWorkFactor
deriving (Int -> WorkFactor -> ShowS
[WorkFactor] -> ShowS
WorkFactor -> String
(Int -> WorkFactor -> ShowS)
-> (WorkFactor -> String)
-> ([WorkFactor] -> ShowS)
-> Show WorkFactor
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> WorkFactor -> ShowS
showsPrec :: Int -> WorkFactor -> ShowS
$cshow :: WorkFactor -> String
show :: WorkFactor -> String
$cshowList :: [WorkFactor] -> ShowS
showList :: [WorkFactor] -> ShowS
Show)
instance Eq WorkFactor where
WorkFactor
a == :: WorkFactor -> WorkFactor -> Bool
== WorkFactor
b = (WorkFactor -> Int
workFactor WorkFactor
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== WorkFactor -> Int
workFactor WorkFactor
b)
instance Ord WorkFactor where
WorkFactor
a <= :: WorkFactor -> WorkFactor -> Bool
<= WorkFactor
b = (WorkFactor -> Int
workFactor WorkFactor
a Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= WorkFactor -> Int
workFactor WorkFactor
b)
workFactor :: WorkFactor -> Low.BcryptWorkFactor
workFactor :: WorkFactor -> Int
workFactor WorkFactor
Fast = Int
Low.BcryptFast
workFactor WorkFactor
Good = Int
Low.BcryptGood
workFactor WorkFactor
Strong = Int
Low.BcryptStrong
workFactor (WorkFactor Int
wf) = Int
wf
toWorkFactor :: Low.BcryptWorkFactor -> WorkFactor
toWorkFactor :: Int -> WorkFactor
toWorkFactor Int
Low.BcryptFast = WorkFactor
Fast
toWorkFactor Int
Low.BcryptGood = WorkFactor
Good
toWorkFactor Int
Low.BcryptStrong = WorkFactor
Strong
toWorkFactor Int
wf = Int -> WorkFactor
WorkFactor Int
wf
type Password = ByteString
type BcryptDigest = ByteString
bcryptGenerate
:: (MonadRandomIO m)
=> Password
-> WorkFactor
-> m BcryptDigest
bcryptGenerate :: forall (m :: * -> *).
MonadRandomIO m =>
Password -> WorkFactor -> m Password
bcryptGenerate Password
pass WorkFactor
wf = do
RNG
rng <- m RNG
forall (m :: * -> *). MonadRandomIO m => m RNG
getRNG
RNG -> Password -> WorkFactor -> m Password
forall (m :: * -> *).
MonadIO m =>
RNG -> Password -> WorkFactor -> m Password
bcryptGenerateRNG RNG
rng Password
pass WorkFactor
wf
bcryptGenerateRNG
:: (MonadIO m)
=> RNG
-> Password
-> WorkFactor
-> m BcryptDigest
bcryptGenerateRNG :: forall (m :: * -> *).
MonadIO m =>
RNG -> Password -> WorkFactor -> m Password
bcryptGenerateRNG RNG
rng Password
pass WorkFactor
wf = IO Password -> m Password
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Password -> m Password) -> IO Password -> m Password
forall a b. (a -> b) -> a -> b
$ Password -> RNG -> Int -> IO Password
Low.bcryptGenerate Password
pass RNG
rng (WorkFactor -> Int
workFactor WorkFactor
wf)
unsafeBcryptGenerateSystem
:: Password
-> WorkFactor
-> BcryptDigest
unsafeBcryptGenerateSystem :: Password -> WorkFactor -> Password
unsafeBcryptGenerateSystem = (RNG -> Password -> WorkFactor -> IO Password)
-> RNG -> Password -> WorkFactor -> Password
forall a b c d. (a -> b -> c -> IO d) -> a -> b -> c -> d
unsafePerformIO3 RNG -> Password -> WorkFactor -> IO Password
forall (m :: * -> *).
MonadIO m =>
RNG -> Password -> WorkFactor -> m Password
bcryptGenerateRNG RNG
systemRNG
{-# NOINLINE unsafeBcryptGenerateSystem #-}
unsafeBcryptGenerateRNG
:: RNG
-> Password
-> WorkFactor
-> BcryptDigest
unsafeBcryptGenerateRNG :: RNG -> Password -> WorkFactor -> Password
unsafeBcryptGenerateRNG = (RNG -> Password -> WorkFactor -> IO Password)
-> RNG -> Password -> WorkFactor -> Password
forall a b c d. (a -> b -> c -> IO d) -> a -> b -> c -> d
unsafePerformIO3 RNG -> Password -> WorkFactor -> IO Password
forall (m :: * -> *).
MonadIO m =>
RNG -> Password -> WorkFactor -> m Password
bcryptGenerateRNG
{-# NOINLINE unsafeBcryptGenerateRNG #-}
bcryptValidate
:: (MonadIO m)
=> Password
-> BcryptDigest
-> m Bool
bcryptValidate :: forall (m :: * -> *). MonadIO m => Password -> Password -> m Bool
bcryptValidate Password
pass Password
dg = IO Bool -> m Bool
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ Password -> Password -> IO Bool
Low.bcryptIsValid Password
pass Password
dg
{-# NOINLINE bcryptValidate #-}
unsafeBcryptValidate
:: Password
-> BcryptDigest
-> Bool
unsafeBcryptValidate :: Password -> Password -> Bool
unsafeBcryptValidate = (Password -> Password -> IO Bool) -> Password -> Password -> Bool
forall a b c. (a -> b -> IO c) -> a -> b -> c
unsafePerformIO2 Password -> Password -> IO Bool
Low.bcryptIsValid
{-# NOINLINE unsafeBcryptValidate #-}