module Text.Password.Strength (Password, UserDict, Entropy, estimate) where
import Foreign
import Foreign.C
import System.IO.Unsafe
foreign import ccall unsafe "zxcvbn.h ZxcvbnMatch" zxcvbnMatch
:: CString
-> Ptr CString
-> Ptr ()
-> IO CDouble
type Password = String
type Entropy = Double
type UserDict = [String]
estimate :: Password -> UserDict -> Entropy
estimate pw ud = unsafePerformIO $
withCString pw $ \c_pw ->
convud [] ud $ \c_udl ->
withArray0 nullPtr c_udl $ \c_ud -> do
ent <- zxcvbnMatch c_pw c_ud nullPtr
return $ fromRational $ toRational ent
where
convud cs [] a = a cs
convud cs (x:xs) a = withCString x $ \c_x ->
convud (c_x : cs) xs a