Merge pull request #4 from puzza007/add-hexhash

Add hexhash/2
This commit is contained in:
Paul Oliver 2016-08-06 19:54:47 +12:00 committed by GitHub
commit be3d418c5a
7 changed files with 65 additions and 26 deletions

View File

@ -1,14 +1,18 @@
language: erlang language: erlang
install: wget https://github.com/erlang/rebar3/releases/download/3.1.0/rebar3 && chmod 755 rebar3
script: PATH=.:$PATH make update test dialyzer
notifications: notifications:
disabled: true disabled: true
branches:
only:
- develop
- 0.1.0
otp_release: otp_release:
- R15B02
- R15B01
- R15B
- 18.0 - 18.0
- 18.1 - 18.1
- 18.2 - 18.2
cache:
directories:
- $HOME/.cache/rebar3/

View File

@ -1,23 +1,30 @@
.PHONY: doc REBAR?=rebar3
.PHONY: \
all \
clean \
nuke \
test \
update \
dialyzer
all: all:
./rebar compile @$(REBAR) compile
./rebar doc
./rebar xref
./rebar eunit
compile:
./rebar compile
doc:
./rebar doc
xref: compile
./rebar xref
clean: clean:
./rebar clean @$(REBAR) clean
test: xref nuke: clean
./rebar eunit @rm -rf _build
test:
@$(REBAR) eunit
update:
@$(REBAR) update
dialyzer:
@$(REBAR) dialyzer
coveralls:
@${REBAR} coveralls send

View File

@ -1,8 +1,11 @@
{erl_opts, [{i, "src"}, {erl_opts, [{i, "src"},
warnings_as_errors, warnings_as_errors,
debug_info,
{w, all}, {w, all},
warn_export_all]}. warn_export_all]}.
{deps, [{hex2bin, "1.0.0"}]}.
{clean_files, [".eunit", {clean_files, [".eunit",
"ebin/*.beam"]}. "ebin/*.beam"]}.

View File

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

View File

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

View File

@ -2,6 +2,8 @@
-export([hash_init/1, hash_update/2, hash_final/1, hash/2]). -export([hash_init/1, hash_update/2, hash_final/1, hash/2]).
-export([hexhash/2]).
-on_load(init/0). -on_load(init/0).
-type bitlen() :: 224 | 256 | 384 | 512. -type bitlen() :: 224 | 256 | 384 | 512.
@ -11,6 +13,8 @@
-type digest() :: <<_:224>> | <<_:256>> | <<_:384>> | <<_:512>>. -type digest() :: <<_:224>> | <<_:256>> | <<_:384>> | <<_:512>>.
-export_type([bitlen/0, context/0, digest/0]).
-define(nif_stub, nif_stub_error(?LINE)). -define(nif_stub, nif_stub_error(?LINE)).
nif_stub_error(Line) -> nif_stub_error(Line) ->
erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}). erlang:nif_error({nif_not_loaded,module,?MODULE,line,Line}).
@ -55,3 +59,7 @@ hash_final(_Context) ->
hash(_BitLen, _Binary) -> hash(_BitLen, _Binary) ->
?nif_stub. ?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>>, ?assertEqual(<<16#94EE7851163C39C3489373AA0BF885D95925EAD7484C586D2E0D01D9C8069D3C30E2EEA2DC63A91B517FE53E43A31D764A2154A2DA92876366B138ABC4406805:512>>,
sha3:hash(512, <<16#00112233445566778899AABBCCDDEEFF:128>>)). 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>>)).