2012-09-08 03:23:52 +00:00
|
|
|
{- values verified using a shared secret
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2012 Joey Hess <id@joeyh.name>
|
2012-09-08 03:23:52 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-09-08 03:23:52 +00:00
|
|
|
-}
|
|
|
|
|
2019-11-23 15:07:22 +00:00
|
|
|
module Utility.Verifiable (
|
|
|
|
Secret,
|
|
|
|
HMACDigest,
|
|
|
|
Verifiable(..),
|
|
|
|
mkVerifiable,
|
|
|
|
verify,
|
|
|
|
prop_verifiable_sane,
|
|
|
|
) where
|
2012-09-08 03:23:52 +00:00
|
|
|
|
2015-04-19 15:05:32 +00:00
|
|
|
import Data.ByteString.UTF8 (fromString)
|
|
|
|
import qualified Data.ByteString as S
|
2012-09-08 03:23:52 +00:00
|
|
|
|
2015-04-19 15:05:32 +00:00
|
|
|
import Utility.Hash
|
2020-11-10 00:07:31 +00:00
|
|
|
import Utility.QuickCheck
|
2015-04-19 15:05:32 +00:00
|
|
|
|
|
|
|
type Secret = S.ByteString
|
2012-09-08 03:23:52 +00:00
|
|
|
type HMACDigest = String
|
|
|
|
|
|
|
|
{- A value, verifiable using a HMAC digest and a secret. -}
|
|
|
|
data Verifiable a = Verifiable
|
2012-09-08 06:02:39 +00:00
|
|
|
{ verifiableVal :: a
|
|
|
|
, verifiableDigest :: HMACDigest
|
2012-09-08 03:23:52 +00:00
|
|
|
}
|
|
|
|
deriving (Eq, Read, Show)
|
|
|
|
|
|
|
|
mkVerifiable :: Show a => a -> Secret -> Verifiable a
|
|
|
|
mkVerifiable a secret = Verifiable a (calcDigest (show a) secret)
|
|
|
|
|
2012-09-10 21:53:51 +00:00
|
|
|
verify :: (Eq a, Show a) => Verifiable a -> Secret -> Bool
|
|
|
|
verify v secret = v == mkVerifiable (verifiableVal v) secret
|
2012-09-08 03:23:52 +00:00
|
|
|
|
|
|
|
calcDigest :: String -> Secret -> HMACDigest
|
2015-04-19 15:05:32 +00:00
|
|
|
calcDigest v secret = calcMac HmacSha1 secret (fromString v)
|
2012-09-08 03:23:52 +00:00
|
|
|
|
2020-11-10 00:07:31 +00:00
|
|
|
prop_verifiable_sane :: TestableString -> TestableString -> Bool
|
|
|
|
prop_verifiable_sane v ts =
|
|
|
|
verify (mkVerifiable (fromTestableString v) secret) secret
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2020-11-10 00:07:31 +00:00
|
|
|
secret = fromString (fromTestableString ts)
|