aboutsummaryrefslogtreecommitdiff
path: root/src/Sproxy/Application/State.hs
blob: 5f836e60bd7b0e72471fbd515f6b70d6270f2ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module Sproxy.Application.State
  ( decode
  , encode
  ) where

import Data.ByteString (ByteString)
import qualified Data.ByteString.Base64 as Base64
import Data.ByteString.Lazy (fromStrict, toStrict)
import Data.Digest.Pure.SHA (bytestringDigest, hmacSha1)
import qualified Data.Serialize as DS

-- FIXME: Compress / decompress ?
encode :: ByteString -> ByteString -> ByteString
encode key payload = Base64.encode . DS.encode $ (payload, digest key payload)

decode :: ByteString -> ByteString -> Either String ByteString
decode key d = do
  (payload, dgst) <- DS.decode =<< Base64.decode d
  if dgst /= digest key payload
    then Left "junk"
    else Right payload

digest :: ByteString -> ByteString -> ByteString
digest key payload =
  toStrict . bytestringDigest $ hmacSha1 (fromStrict key) (fromStrict payload)