File: Strength.hsc

package info (click to toggle)
haskell-zxcvbn-c 1.0.1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: ansic: 1,273; cpp: 1,269; makefile: 49; haskell: 15
file content (41 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-# LANGUAGE ForeignFunctionInterface #-}

module Text.Password.Strength (Password, UserDict, Entropy, estimate) where

#include <zxcvbn.h>

import Foreign
import Foreign.C
import System.IO.Unsafe

foreign import ccall unsafe "zxcvbn.h ZxcvbnMatch" zxcvbnMatch 
	:: CString
	-- ^ password
	-> Ptr CString
	-- ^ array of user dictionary words
	-> Ptr ()
	-- ^ used to get information about parts of the password,
	-- but this binding does not implement that, so a null pointer
	-> IO CDouble

type Password = String

-- | Entropy estimation in bits.
type Entropy = Double

-- | List of words that would be particularly bad in the password,
-- to suppliment the built-in word lists.
-- The name of the user is a good candidate to include here.
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