module Botan.Low.Bcrypt
(
bcryptGenerate
, bcryptIsValid
, BcryptWorkFactor(..)
, pattern BcryptFast
, pattern BcryptGood
, pattern BcryptStrong
, BcryptPassword(..)
, BcryptDigest(..)
) where
import qualified Data.ByteString as ByteString
import Botan.Bindings.Bcrypt
import Botan.Low.Error
import Botan.Low.Make
import Botan.Low.Prelude
import Botan.Low.RNG
import Data.ByteString.Internal as ByteString
type BcryptWorkFactor = Int
pattern BcryptFast
, BcryptGood
, BcryptStrong
:: BcryptWorkFactor
pattern $mBcryptFast :: forall {r}. BcryptWorkFactor -> ((# #) -> r) -> ((# #) -> r) -> r
$bBcryptFast :: BcryptWorkFactor
BcryptFast = BOTAN_BCRYPT_WORK_FACTOR_FAST
pattern $mBcryptGood :: forall {r}. BcryptWorkFactor -> ((# #) -> r) -> ((# #) -> r) -> r
$bBcryptGood :: BcryptWorkFactor
BcryptGood = BOTAN_BCRYPT_WORK_FACTOR_GOOD
pattern $mBcryptStrong :: forall {r}. BcryptWorkFactor -> ((# #) -> r) -> ((# #) -> r) -> r
$bBcryptStrong :: BcryptWorkFactor
BcryptStrong = BOTAN_BCRYPT_WORK_FACTOR_STRONG
type BcryptPassword = ByteString
type BcryptDigest = ByteString
bcryptGenerate
:: BcryptPassword
-> RNG
-> BcryptWorkFactor
-> IO BcryptDigest
bcryptGenerate :: BcryptPassword -> RNG -> BcryptWorkFactor -> IO BcryptPassword
bcryptGenerate BcryptPassword
password RNG
rng BcryptWorkFactor
factor = BcryptPassword
-> (Ptr CChar -> IO BcryptPassword) -> IO BcryptPassword
forall a. BcryptPassword -> (Ptr CChar -> IO a) -> IO a
asCString BcryptPassword
password ((Ptr CChar -> IO BcryptPassword) -> IO BcryptPassword)
-> (Ptr CChar -> IO BcryptPassword) -> IO BcryptPassword
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
passwordPtr -> do
RNG -> (BotanRNG -> IO BcryptPassword) -> IO BcryptPassword
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO BcryptPassword) -> IO BcryptPassword)
-> (BotanRNG -> IO BcryptPassword) -> IO BcryptPassword
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG -> do
(Ptr CSize -> IO BcryptPassword) -> IO BcryptPassword
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CSize -> IO BcryptPassword) -> IO BcryptPassword)
-> (Ptr CSize -> IO BcryptPassword) -> IO BcryptPassword
forall a b. (a -> b) -> a -> b
$ \ Ptr CSize
szPtr -> do
Ptr CSize -> CSize -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr CSize
szPtr CSize
80
BcryptWorkFactor
-> (Ptr Word8 -> IO BcryptPassword) -> IO BcryptPassword
forall a b. BcryptWorkFactor -> (Ptr a -> IO b) -> IO b
allocaBytes BcryptWorkFactor
80 ((Ptr Word8 -> IO BcryptPassword) -> IO BcryptPassword)
-> (Ptr Word8 -> IO BcryptPassword) -> IO BcryptPassword
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
outPtr -> do
HasCallStack => IO BotanErrorCode -> IO ()
IO BotanErrorCode -> IO ()
throwBotanIfNegative_ (IO BotanErrorCode -> IO ()) -> IO BotanErrorCode -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr Word8
-> Ptr CSize
-> ConstPtr CChar
-> BotanRNG
-> CSize
-> Word32
-> IO BotanErrorCode
botan_bcrypt_generate
Ptr Word8
outPtr
Ptr CSize
szPtr
(Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
passwordPtr)
BotanRNG
botanRNG
(BcryptWorkFactor -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral BcryptWorkFactor
factor)
Word32
0
Ptr CChar -> IO BcryptPassword
ByteString.packCString (Ptr Word8 -> Ptr CChar
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
outPtr)
bcryptIsValid
:: BcryptPassword
-> BcryptDigest
-> IO Bool
bcryptIsValid :: BcryptPassword -> BcryptPassword -> IO Bool
bcryptIsValid BcryptPassword
password BcryptPassword
hash = BcryptPassword -> (Ptr CChar -> IO Bool) -> IO Bool
forall a. BcryptPassword -> (Ptr CChar -> IO a) -> IO a
asCString BcryptPassword
password ((Ptr CChar -> IO Bool) -> IO Bool)
-> (Ptr CChar -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
passwordPtr -> do
BcryptPassword -> (Ptr CChar -> IO Bool) -> IO Bool
forall a. BcryptPassword -> (Ptr CChar -> IO a) -> IO a
asCString BcryptPassword
hash ((Ptr CChar -> IO Bool) -> IO Bool)
-> (Ptr CChar -> IO Bool) -> IO Bool
forall a b. (a -> b) -> a -> b
$ \ Ptr CChar
hashPtr -> do
HasCallStack => IO BotanErrorCode -> IO Bool
IO BotanErrorCode -> IO Bool
throwBotanCatchingSuccess (IO BotanErrorCode -> IO Bool) -> IO BotanErrorCode -> IO Bool
forall a b. (a -> b) -> a -> b
$ ConstPtr CChar -> ConstPtr CChar -> IO BotanErrorCode
botan_bcrypt_is_valid (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
passwordPtr) (Ptr CChar -> ConstPtr CChar
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr CChar
hashPtr)