summaryrefslogtreecommitdiffhomepage
path: root/Cost.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-07 12:09:53 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-07 12:09:53 -0400
commit6205bbac7abc919371a5e7af9e4ba1a8d70de85e (patch)
tree668f3247182d01f025ce9122f6b495def240eb71 /Cost.hs
parent016877aa55764d14af3c16645d64fd95bf6a2da1 (diff)
downloadkeysafe-6205bbac7abc919371a5e7af9e4ba1a8d70de85e.tar.gz
basic password entropy calculation
Diffstat (limited to 'Cost.hs')
-rw-r--r--Cost.hs16
1 files changed, 15 insertions, 1 deletions
diff --git a/Cost.hs b/Cost.hs
index de5a82d..3c7c21d 100644
--- a/Cost.hs
+++ b/Cost.hs
@@ -2,9 +2,13 @@
module Cost where
+import Types
+import Entropy
import Utility.HumanTime
import Data.Monoid
+import qualified Data.ByteString.UTF8 as B
+
-- | An estimated cost to perform an operation.
data Cost op
= CPUCost Seconds
@@ -61,7 +65,7 @@ type CostCalc op t = Entropy t -> Cost op
-- | Number of bits of entropy
newtype Entropy t = Entropy Int
- deriving (Num)
+ deriving (Num, Show)
-- | Entropy can never go negative when subtracting bits from it.
reduceEntropy :: Entropy t -> Int -> Entropy t
@@ -70,6 +74,16 @@ reduceEntropy (Entropy a) b = Entropy (max 0 (a - b))
-- | Things that can have entropy
data UnknownPassword
+-- | Naive calculation of the entropy of a password.
+-- Does not take common passowrds and password generation patterns into
+-- account, so this is an overestimation of how hard a password
+-- is to crack.
+passwordEntropy :: Password -> Entropy UnknownPassword
+passwordEntropy (Password p) = Entropy $ floor $
+ entropy s * fromIntegral (length s)
+ where
+ s = B.toString p
+
-- | CostCalc for a brute force linear search through an entropy space
-- in which each step entails paying a cost.
--