diff options
author | Joey Hess <joeyh@joeyh.name> | 2016-08-07 12:09:53 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2016-08-07 12:09:53 -0400 |
commit | 6205bbac7abc919371a5e7af9e4ba1a8d70de85e (patch) | |
tree | 668f3247182d01f025ce9122f6b495def240eb71 | |
parent | 016877aa55764d14af3c16645d64fd95bf6a2da1 (diff) | |
download | keysafe-6205bbac7abc919371a5e7af9e4ba1a8d70de85e.tar.gz |
basic password entropy calculation
-rw-r--r-- | Cost.hs | 16 | ||||
-rw-r--r-- | Entropy.hs | 9 | ||||
-rw-r--r-- | Types.hs | 4 |
3 files changed, 27 insertions, 2 deletions
@@ -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 @@ -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 |