summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Cost.hs16
-rw-r--r--Entropy.hs9
-rw-r--r--Types.hs4
3 files changed, 27 insertions, 2 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.
--
diff --git a/Entropy.hs b/Entropy.hs
new file mode 100644
index 0000000..2ff28b2
--- /dev/null
+++ b/Entropy.hs
@@ -0,0 +1,9 @@
+module Entropy where
+
+import Data.List
+
+entropy :: String -> Double
+entropy = sum . map lg' . fq' . map (fromIntegral.length) . group . sort
+ where
+ lg' c = (c * ) . logBase 2 $ 1.0 / c
+ fq' c = let sc = sum c in map (/ sc) c
diff --git a/Types.hs b/Types.hs
index 3b5d39f..8787273 100644
--- a/Types.hs
+++ b/Types.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving #-}
module Types where
@@ -8,10 +8,12 @@ import Raaz.Core.Encode
import Data.Monoid
import Data.Word
import Data.Time.Clock
+import Data.String
import Text.Read
-- | A password used to encrypt a key stored in keysafe.
newtype Password = Password B.ByteString
+ deriving (IsString)
-- | A name associated with a key stored in keysafe.
newtype Name = Name B.ByteString