diff options
-rw-r--r-- | Cost.hs | 7 | ||||
-rw-r--r-- | Entropy.hs | 21 |
2 files changed, 19 insertions, 9 deletions
@@ -7,8 +7,6 @@ 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 @@ -79,10 +77,7 @@ data UnknownPassword -- 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 +passwordEntropy (Password p) = Entropy $ floor $ totalEntropy p -- | CostCalc for a brute force linear search through an entropy space -- in which each step entails paying a cost. @@ -1,9 +1,24 @@ +{-# LANGUAGE FlexibleInstances #-} + module Entropy where import Data.List +import qualified Data.ByteString.UTF8 as B + +class ToChars t where + toChars :: t -> [Char] + +instance ToChars String where + toChars = id -entropy :: String -> Double -entropy = sum . map lg' . fq' . map (fromIntegral.length) . group . sort +instance ToChars B.ByteString where + toChars = B.toString + +shannonEntropy :: ToChars s => s -> Double +shannonEntropy = sum . map lg' . fq' . map (fromIntegral.length) . group . sort . toChars where lg' c = (c * ) . logBase 2 $ 1.0 / c - fq' c = let sc = sum c in map (/ sc) c + fq' c = let sc = sum c in map (/ sc) c + +totalEntropy :: ToChars s => s -> Double +totalEntropy s = shannonEntropy s * fromIntegral (length (toChars s)) |