anchor.diff:      {IV, Crypt} = lists:split(16, EData),
       crypto:aes_cfb_128_decrypt(Key, list_to_binary(IV),list_to_binary(Crypt)).
   
  -cookie_gen_key(UserName,ExpirationTime,ServerKey)->
  -    crypto:md5_mac(ServerKey, [UserName, ExpirationTime]).
  -cookie_gen_hmac(UserName,ExpirationTime,Data,SessionKey,Key)->
  -    crypto:sha_mac(Key,[UserName,ExpirationTime,Data,SessionKey]).
  +cookie_gen_key(ExpirationTime,ServerKey)->
  +    crypto:md5_mac(ServerKey, [ExpirationTime]).
  +cookie_gen_hmac(ExpirationTime,Data,SessionKey,Key)->
  +    crypto:sha_mac(Key,[ExpirationTime,Data,SessionKey]).
   
   %% @doc Using 
   cookie_decode (Encoded) ->
  -    erlang:binary_to_term(hexstr_to_bin(Encoded)).
  +    hexstr_to_bin(Encoded).
   cookie_encode (Term) ->
  -    bin_to_hexstr(erlang:term_to_binary (Term, [compressed,{minor_version,1}])).
  +    bin_to_hexstr(Term).
   
   bin_to_hexstr(Bin) ->
     lists:flatten([io_lib:format("~2.16.0B", [X]) ||
  
positive.code: server_key()->%setup function
      ["adfasdfasfs",timestamp_sec(now())].
  
  generate_check_session_cookie([ServerKey,TimeStamp]) ->
      [?_assertEqual({true,["alice",integer_to_list(TimeStamp+1000),["alice"]]},
  		   check_session_cookie(generate_session_data("alice",TimeStamp+1000,["alice"],fun(A)-> A end,ServerKey),
  					TimeStamp,fun(A)-> A end,ServerKey)),
       ?_assertEqual({true,["alice",integer_to_list(TimeStamp+1000),[["sex","female",a]]]},
  		   check_session_cookie(generate_session_data("alice",TimeStamp+1000,[["sex","female",a]],fun(A)-> A end,ServerKey),
  					TimeStamp,fun(A)-> A end,ServerKey)),
       ?_assertEqual({true,["alice",integer_to_list(TimeStamp+1000),{tuple,one}]},
  		   check_session_cookie(generate_session_data("alice",TimeStamp+1000,{tuple,one},fun(A)-> A end,ServerKey),
  					TimeStamp,fun(A)-> A end,ServerKey)),
       ?_assertEqual({false,["alice",integer_to_list(TimeStamp-1),{tuple,one}]},
  		   check_session_cookie(generate_session_data("alice",TimeStamp-1,{tuple,one},fun(A)-> A end,ServerKey),
  					TimeStamp,fun(A)-> A end,ServerKey))%current timestamp newer than cookie, it's expired
      ].
  
  
negative.code: %%      In order to achieve more security, it is adviced to use https
  
  -module(mochiweb_session).
  -export([generate_session_data/4,generate_session_cookie/4,check_session_cookie/4]).
  -export([cookie_encode/1,cookie_decode/1,timestamp_sec/1]).%Useful fuctions for more specific purposes
  
  %% @spec generate_session_data(ExpirationTime,Data :: string(),FSessionKey : function(A),ServerKey) -> string()
  %% @doc generates a secure encrypted string convining all the parameters.
  %%      The expritation time is considered in seconds
  generate_session_data(ExpirationTime,Data,FSessionKey,ServerKey) when is_integer(ExpirationTime),is_list(Data), is_function(FSessionKey)->
      BData=list_to_binary(Data),
      ExpTime=integer_to_list(ExpirationTime),
      Key=cookie_gen_key(ExpTime,ServerKey),
      Hmac=cookie_gen_hmac(ExpTime,BData,FSessionKey(integer_to_list(ExpirationTime)),Key),
      EData=cookie_encrypt_data(BData,Key),
      iolist_to_binary([ ExpTime,$,, EData,  Hmac ]).
  
  %% @spec generate_session_data(UserName,ExpirationTime,SessionExtraData,FSessionKey : function(A),ServerKey) -> mochiweb_cookie()
  %% @doc generates a secure encrypted cookie using the generate_session_data function.
  generate_session_cookie(ExpirationTime,Data,FSessionKey,ServerKey) when is_integer(ExpirationTime)->
      CookieData=generate_session_data(ExpirationTime,Data,FSessionKey,ServerKey),
      mochiweb_cookies:cookie("id",CookieData,[{max_age,20000},{local_time,calendar:universal_time_to_local_time(calendar:universal_time())}]).
  
  %% @spec cookie_check_session(RawData,ExpirationTime,FSessionKey : function(A), ServerKey)->{false,[UserName,Expiration,Data]} | 
  