Add hexhash/2

This commit is contained in:
Paul Oliver 2016-08-06 19:33:06 +12:00
parent c67eb9fae6
commit e963bef653
No known key found for this signature in database
GPG Key ID: 9250069E8AF6BF4F
5 changed files with 30 additions and 2 deletions

View File

@ -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"]}.

View File

@ -1 +1 @@
[].
[{<<"hex2bin">>,{pkg,<<"hex2bin">>,<<"1.0.0">>},0}].

View File

@ -5,7 +5,8 @@
{registered, []},
{applications, [
kernel,
stdlib
stdlib,
hex2bin
]},
{modules, [sha3]},
{env, []}

View File

@ -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)).

View File

@ -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>>)).