Skip to content
Browse files

Get rid of crypto warnings

Also should be backwards compatible options back to R14 based on
compile macros in rebar
  • Loading branch information...
1 parent 680dba8 commit 6e06bd60897c018af9951f4549d8d279952b19db @ferd ferd committed
Showing with 23 additions and 1 deletion.
  1. +2 −1 rebar.config
  2. +21 −0 src/mochiweb_session.erl
View
3 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,
View
21 src/mochiweb_session.erl
@@ -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),
@@ -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").

0 comments on commit 6e06bd6

Please sign in to comment.
Something went wrong with that request. Please try again.