/lib/haval/src/haval.erl

https://github.com/rakuto/erlang_haval · Erlang · 64 lines · 33 code · 12 blank · 19 comment · 2 complexity · 9ca11beb56fc55eeea54f323f58bf9b3 MD5 · raw file

  1. %%% --------------------------------------------------------------------
  2. %%% @doc HAVAL bindings for Erlang
  3. %%% @author Rakuto Furutani <xri://=rakuto/(+profile)>
  4. %%% @end
  5. %%% --------------------------------------------------------------------
  6. -module(haval).
  7. -author('Rakuto Furutani <xri://=rakuto/(+profile)>').
  8. %% Functions
  9. -define(DRV_INFO, 1).
  10. -define(DRV_HAVAL_STRING, 2).
  11. -define(DRV_HAVAL_FILE, 3).
  12. -export([start/0, stop/0]).
  13. -export([info/0, haval_string/1, haval_file/1]).
  14. %% Unit Test
  15. %% All functions whoes names match _test() or ..._test_() to be automatically exported.
  16. -include("eunit.hrl").
  17. %%
  18. %% Public interface
  19. %%
  20. start() ->
  21. application:start(haval).
  22. stop() ->
  23. application:stop(haval).
  24. info() ->
  25. [haval_string, haval_file].
  26. %% Caluculate a value of HAVAL from string
  27. haval_string(Str) ->
  28. binary_to_term(control(?DRV_HAVAL_STRING, Str)).
  29. %% Caluculate a value of HAVAL from file
  30. haval_file(FileName) ->
  31. binary_to_term(control(?DRV_HAVAL_FILE, FileName)).
  32. %%
  33. %% Internal functions
  34. %%
  35. control(Cmd, Data) ->
  36. [{port, Port}| _] = ets:lookup(haval_server_table, port),
  37. erlang:port_control(Port, Cmd, Data).
  38. %%
  39. %% Unit Tests
  40. %%
  41. haval_string_test() ->
  42. haval:start(),
  43. Str = "I'm a fun of Erlang",
  44. ValidHash = "EDBCF3A39D3AEA64181B90EA2C30F830103FA5DEB790A6E6299C8F686743AA56",
  45. ?assert(haval:haval_string(Str) == ValidHash),
  46. haval:stop().
  47. haval_file_test() ->
  48. haval:start(),
  49. File = filename:join([?FILE, "sample_file"]),
  50. ValidHash = "71EB15A01D6C592F0DE228F062AD3B91102FFD98A3D5C871AAF3FA20EF9EA377",
  51. ?assert(haval:haval_file(File) == ValidHash),
  52. haval:stop().