Created
February 5, 2016 19:10
-
-
Save zbskii/efb7436d344164e5f615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main (main) where | |
import Control.Monad | |
import Control.Applicative | |
import Test.QuickCheck | |
import Test.QuickCheck.Gen | |
import Data.Digest.BCrypt | |
import System.Random | |
import System.Exit | |
import qualified Data.ByteString as B | |
import qualified Data.ByteString.Char8 as B8 | |
main :: IO () | |
main = checkResult >> return () | |
where | |
checkResult = case quickCheckResult bcryptCheck of | |
Success _ _ _ -> exitSuccess | |
_ -> exitFailure | |
bcryptCheck :: B.ByteString -> BSalt -> Bool | |
bcryptCheck plain salt = encrypted == encrypted' | |
where | |
ct = bcrypt plain salt | |
encrypted = case packBSalt ct of | |
Just s -> s | |
_ -> error "Impossible packing of salt" | |
encrypted' = case packBSalt $ bcrypt plain encrypted of | |
Just s' -> s' | |
_ -> error "Impossible packing of salt #2" | |
instance Arbitrary B.ByteString where | |
arbitrary = do | |
count <- choose (3, 15) | |
let char = choose ('a', 'z') | |
B8.pack <$> replicateM count char | |
instance Arbitrary BSalt where | |
arbitrary = salting | |
salting = MkGen (\r _ -> mkSalt r) | |
get16Bytes g = B.pack . take 16 . map fromInteger . randomRs (0, 255) $ g | |
mkSalt :: RandomGen g => g -> BSalt | |
mkSalt g = case genSalt 4 bytes of | |
Just salted -> salted | |
_ -> error $ "Bad bsalt " ++ (B8.unpack bytes) | |
where | |
bytes = get16Bytes g |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
or: