anchor.diff:  check_session_cookie([],_,_,_) ->
       {false,[]};
   check_session_cookie(ECookie,ExpirationTime,FSessionKey,ServerKey) ->
  -    Cookie=hexstr_to_bin(ECookie),
  -    {P1,_}=binary:match(Cookie,<<",">>),
  -    ExpirationTimeCookie=binary:part(Cookie,0,P1),
  -    Data=binary:part(Cookie,P1+1,byte_size(Cookie)-(20+P1+1)),
  -    Hmac=binary:part(Cookie,byte_size(Cookie)-20,20),
  -    check_session_cookie(binary_to_list(ExpirationTimeCookie),Data,Hmac,ExpirationTime,FSessionKey,ServerKey);
  -check_session_cookie(_,_,_,_) ->
  -    {false,[]}.
  -check_session_cookie(ExpirationTime1, EData, BHmac,ExpirationTime,FSessionKey,ServerKey) 
  -  when is_integer(ExpirationTime) , is_list(ServerKey), is_list(ExpirationTime1)->
  -    ExpTime=list_to_integer(ExpirationTime1),
  +    Cookie=base64:decode(ECookie),
  +    <<ExpirationTime1:32/binary,BHmac:16/binary,EData/binary>> = Cookie,
  +    ExpTime=binary:decode_unsigned(<<ExpirationTime1>>),
       Key=gen_key(ExpirationTime1,ServerKey),
       Data=decrypt_data(EData,Key),
       Hmac2=gen_hmac(ExpirationTime1,Data,FSessionKey(ExpirationTime1),Key),
  
positive.code: gen_hmac(ExpirationTime,Data,SessionKey,Key)->
      crypto:sha_mac(Key,[ExpirationTime,Data,SessionKey]).
  
  %% @doc Using 
  cookie_decode (Encoded) ->
      hexstr_to_bin(Encoded).
  cookie_encode (Term) ->
      bin_to_hexstr(Term).
  
  bin_to_hexstr(Bin) ->
    lists:flatten([io_lib:format("~2.16.0B", [X]) ||
      X <- binary_to_list(Bin)]).
  
  hexstr_to_bin(S) ->
    hexstr_to_bin(S, []).
  hexstr_to_bin([], Acc) ->
    list_to_binary(lists:reverse(Acc));
  hexstr_to_bin([X,Y|T], Acc) ->
    {ok, [V], []} = io_lib:fread("~16u", [X,Y]),
    hexstr_to_bin(T, [V | Acc]).
  
  
  timestamp_sec({MGS,S,_})->
      MGS*1000000+S.
  
  
  -ifdef(TEST).
  -include_lib("eunit/include/eunit.hrl").
  
negative.code: 
  -module(mochiweb_session).
  -export([generate_session_data/4,generate_session_cookie/4,check_session_cookie/4]).
  
  
  %% @spec generate_session_data(ExpirationTime,Data :: string(),FSessionKey : function(A),ServerKey) -> string()
  %% @doc generates a secure encrypted string convining all the parameters.
  %%      The expiration time is considered in seconds
  generate_session_data(ExpirationTime,Data,FSessionKey,ServerKey) when is_integer(ExpirationTime),is_list(Data), is_function(FSessionKey)->
      BData=ensure_binary(Data),
      ExpTime=integer_to_list(ExpirationTime),
      BExpTime=ensure_binary(ExpTime),
      Key=gen_key(ExpTime,ServerKey),
      Hmac=gen_hmac(ExpTime,BData,FSessionKey(integer_to_list(ExpirationTime)),Key),
      EData=encrypt_data(BData,Key),
      io:format("data:~p length:~p~n",[<<ExpirationTime:32>>,length(integer_to_list(binary:decode_unsigned(<<ExpirationTime:32>>)))]),
      base64:encode(<< BExpTime:32,Hmac/binary, EData/binary>>).
  
  %% @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.
  