diff --git a/rebar.config b/rebar.config index 1b456c9..90c08c0 100644 --- a/rebar.config +++ b/rebar.config @@ -1,8 +1,11 @@ {erl_opts, [{i, "src"}, warnings_as_errors, + debug_info, {w, all}, warn_export_all]}. +{deps, [{hex2bin, "1.0.0"}]}. + {clean_files, [".eunit", "ebin/*.beam"]}. diff --git a/rebar.lock b/rebar.lock index 57afcca..d6faa9e 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1 +1 @@ -[]. +[{<<"hex2bin">>,{pkg,<<"hex2bin">>,<<"1.0.0">>},0}]. diff --git a/src/sha3.app.src b/src/sha3.app.src index 5d0848c..d18de89 100644 --- a/src/sha3.app.src +++ b/src/sha3.app.src @@ -5,7 +5,8 @@ {registered, []}, {applications, [ kernel, - stdlib + stdlib, + hex2bin ]}, {modules, [sha3]}, {env, []} diff --git a/src/sha3.erl b/src/sha3.erl index c8f46ed..64ae999 100644 --- a/src/sha3.erl +++ b/src/sha3.erl @@ -2,6 +2,8 @@ -export([hash_init/1, hash_update/2, hash_final/1, hash/2]). +-export([hexhash/2]). + -on_load(init/0). -type bitlen() :: 224 | 256 | 384 | 512. @@ -11,6 +13,8 @@ -type digest() :: <<_:224>> | <<_:256>> | <<_:384>> | <<_:512>>. +-export_type([bitlen/0, context/0, digest/0]). + -define(nif_stub, nif_stub_error(?LINE)). nif_stub_error(Line) -> erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}). @@ -55,3 +59,7 @@ hash_final(_Context) -> hash(_BitLen, _Binary) -> ?nif_stub. +-spec hexhash(bitlen(), binary()) -> binary(). +hexhash(Bitlen, Binary) -> + Hash = hash(Bitlen, Binary), + list_to_binary(hex2bin:bin_to_hexstr(Hash)). diff --git a/test/sha3_tests.erl b/test/sha3_tests.erl index c8b262f..893b69d 100644 --- a/test/sha3_tests.erl +++ b/test/sha3_tests.erl @@ -46,3 +46,19 @@ hash_512_test() -> ?assertEqual(<<16#94EE7851163C39C3489373AA0BF885D95925EAD7484C586D2E0D01D9C8069D3C30E2EEA2DC63A91B517FE53E43A31D764A2154A2DA92876366B138ABC4406805:512>>, sha3:hash(512, <<16#00112233445566778899AABBCCDDEEFF:128>>)). +hexhash_224_test() -> + ?assertEqual(<<"038907E89C919CD8F90A7FBC5A88FF9278108DAEF3EBCDA0CEB383E1">>, + sha3:hexhash(224, <<16#00112233445566778899AABBCCDDEEFF:128>>)). + +hexhash_256_test() -> + ?assertEqual(<<"22BCE46032802AF0ABFACF3768F7BE04A34F5F01DF60F44FFD52D3CA937350C0">>, + sha3:hexhash(256, <<16#00112233445566778899AABBCCDDEEFF:128>>)). + +hexhash_384_test() -> + ?assertEqual(<<"25FAC1ADECBE1B254976FE32C2FE78829B23D7D84316141ECD208D6806A9DB4352A014ADA4106BA0D210DDA0FD18E150">>, + sha3:hexhash(384, <<16#00112233445566778899AABBCCDDEEFF:128>>)). + +hexhash_512_test() -> + ?assertEqual(<<"94EE7851163C39C3489373AA0BF885D95925EAD7484C586D2E0D01D9C8069D3C30E2EEA2DC63A91B517FE53E43A31D764A2154A2DA92876366B138ABC4406805">>, + sha3:hexhash(512, <<16#00112233445566778899AABBCCDDEEFF:128>>)). +