aboutsummaryrefslogtreecommitdiff
path: root/src/Sproxy/Application/State.hs
blob: 29d925266c26002da862f36919bc1ea4407d4811 (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
26
27
28
29
30
module Sproxy.Application.State (
  decode
, encode
) where

import Data.ByteString (ByteString)
import Data.ByteString.Lazy (fromStrict, toStrict)
import Data.Digest.Pure.SHA (hmacSha1, bytestringDigest)
import qualified Data.ByteString.Base64 as Base64
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)