Skip to content

Commit

Permalink
Get rid of crypto warnings
Browse files Browse the repository at this point in the history
Also should be backwards compatible options back to R14 based on
compile macros in rebar
  • Loading branch information
ferd committed Jul 16, 2013
1 parent 680dba8 commit 6e06bd6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion rebar.config
@@ -1,6 +1,7 @@
% -*- mode: erlang -*-
{erl_opts, [debug_info,
{platform_define, "R15", 'gen_tcp_r15b_workaround'}]}.
{platform_define, "R15", 'gen_tcp_r15b_workaround'},
{platform_define, "(R14|R15|R16B-)", 'crypto_compatibility'}]}.
{cover_enabled, true}.
{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
{dialyzer_opts, [{warnings, [no_return,
Expand Down
21 changes: 21 additions & 0 deletions src/mochiweb_session.erl
Expand Up @@ -100,6 +100,7 @@ ensure_binary(B) when is_binary(B) ->
ensure_binary(L) when is_list(L) ->
iolist_to_binary(L).

-ifdef(crypto_compatibility).
-spec encrypt_data(binary(), binary()) -> binary().
encrypt_data(Data, Key) ->
IV = crypto:rand_bytes(16),
Expand All @@ -118,6 +119,26 @@ gen_key(ExpirationTime, ServerKey)->
gen_hmac(ExpirationTime, Data, SessionKey, Key) ->
crypto:sha_mac(Key, [ExpirationTime, Data, SessionKey]).

-else.
-spec encrypt_data(binary(), binary()) -> binary().
encrypt_data(Data, Key) ->
IV = crypto:rand_bytes(16),
Crypt = crypto:block_encrypt(aes_cfb128, Key, IV, Data),
<<IV/binary, Crypt/binary>>.

-spec decrypt_data(binary(), binary()) -> binary().
decrypt_data(<<IV:16/binary, Crypt/binary>>, Key) ->
crypto:block_decrypt(aes_cfb128, Key, IV, Crypt).

-spec gen_key(iolist(), iolist()) -> binary().
gen_key(ExpirationTime, ServerKey)->
crypto:hmac(md5, ServerKey, [ExpirationTime]).

-spec gen_hmac(iolist(), binary(), iolist(), binary()) -> binary().
gen_hmac(ExpirationTime, Data, SessionKey, Key) ->
crypto:hmac(sha, Key, [ExpirationTime, Data, SessionKey]).

-endif.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
Expand Down

0 comments on commit 6e06bd6

Please sign in to comment.