diff options
author | Joey Hess <joey@kitenet.net> | 2012-09-07 23:23:52 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-09-07 23:23:52 -0400 |
commit | 92df8250fa7c6d8c36ca214e45c7b5a6c9d307a9 (patch) | |
tree | 31dff3d544a99f3b53a2a4747d9286dbd52c5b2a /Utility | |
parent | c12caf0a4e665d121c7ffd5a992306f7511bc305 (diff) |
broke out Verifiable to a utility library, and added a quickcheck test
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/Verifiable.hs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Utility/Verifiable.hs b/Utility/Verifiable.hs new file mode 100644 index 000000000..58218db2a --- /dev/null +++ b/Utility/Verifiable.hs @@ -0,0 +1,37 @@ +{- values verified using a shared secret + - + - Copyright 2012 Joey Hess <joey@kitenet.net> + - + - Licensed under the GNU GPL version 3 or higher. + -} + +module Utility.Verifiable where + +import Data.Digest.Pure.SHA +import Data.ByteString.Lazy.UTF8 (fromString) +import qualified Data.ByteString.Lazy as L + +type Secret = L.ByteString +type HMACDigest = String + +{- A value, verifiable using a HMAC digest and a secret. -} +data Verifiable a = Verifiable + { val :: a + , digest :: HMACDigest + } + deriving (Eq, Read, Show) + +mkVerifiable :: Show a => a -> Secret -> Verifiable a +mkVerifiable a secret = Verifiable a (calcDigest (show a) secret) + +verified :: (Eq a, Show a) => Verifiable a -> Secret -> Bool +verified v secret = v == mkVerifiable (val v) secret + +calcDigest :: String -> Secret -> HMACDigest +calcDigest v secret = showDigest $ hmacSha1 secret $ fromString v + +{- for quickcheck -} +prop_verifiable_sane :: String -> String -> Bool +prop_verifiable_sane a s = verified (mkVerifiable a secret) secret + where + secret = fromString s |