Permalink
Browse files

fix previous commit: add hmac doc and remove rebar

The previous commit added the hmac module and was supposed to include
hmac documentation, but by accident it didn't. Add hmac specs and
doc.

Also remove local copy of rebar -- rebar is popular enough now that
erlang developers generally need a copy on their system, and we don't
want to have to keep updating a local copy here every time the rebar
devs make a change.
  • Loading branch information...
1 parent fa5681b commit c4b4bd8af0ec83fa9b2a90e114f00d18d4fc9394 @vinoski committed Oct 1, 2011
Showing with 62 additions and 10 deletions.
  1. +3 −1 Makefile
  2. +5 −4 README.md
  3. BIN rebar
  4. +54 −5 src/hmac.erl
View
@@ -2,7 +2,9 @@
PREFIX:=../
DEST:=$(PREFIX)$(PROJECT)
-REBAR=./rebar
+# Expect to find rebar in the PATH. If you don't have rebar, you can get it
+# from https://github.com/basho/rebar .
+REBAR=rebar
.PHONY: all edoc test clean build_plt dialyzer
View
@@ -2,10 +2,11 @@
## Description
-The **erlsha2** library application implements the SHA-2 Secure Hash Standard
-(SHA-224, SHA-256, SHA-384, SHA-512) using Erlang NIFs. It also
-provides pure Erlang implementations, though they are much slower than
-the C NIF implementations.
+The **erlsha2** library application implements the SHA-2 Secure Hash
+Standard (SHA-224, SHA-256, SHA-384, SHA-512) using Erlang NIFs. (It
+also provides pure Erlang implementations, though they are much slower
+than the C NIF implementations.) It also includes HMAC wrappers for
+the SHA-2 functions.
See the following links for details:
View
BIN rebar
Binary file not shown.
View
@@ -1,5 +1,5 @@
-%% @author Jared Flatow
-%% @doc hmac wrappers for erlsha2
+%% @author Jared Flatow, Steve Vinoski
+%% @doc HMAC wrappers for erlsha2
%% @reference
%% Based on the
%% <a href="http://erlang.org/pipermail/erlang-questions/2011-May/058174.html">
@@ -21,28 +21,77 @@
-define(SHA_384_BLOCKSIZE, 128).
-define(SHA_512_BLOCKSIZE, 128).
+%% @spec hexlify(binary()) -> list()
+%% @doc Convert binary to equivalent hexadecimal string.
+%%
hexlify(Binary) when is_binary(Binary) ->
- lists:flatten([io_lib:format("~2.16.0B", [B]) || B <- binary_to_list(Binary)]).
+ lists:flatten([io_lib:format("~2.16.0B", [B]) ||
+ B <- binary_to_list(Binary)]).
+%% @spec hmac224(key(), data()) -> mac()
+%% where
+%% key() = iolist() | binary()
+%% data() = iolist() | binary()
+%% mac() = binary()
+%% @doc Compute a SHA-224 MAC message authentication code from key and data.
+%%
hmac224(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha224/1, ?SHA_224_BLOCKSIZE).
+%% @spec hmac256(key(), data()) -> mac()
+%% where
+%% key() = iolist() | binary()
+%% data() = iolist() | binary()
+%% mac() = binary()
+%% @doc Compute a SHA-256 MAC message authentication code from key and data.
+%%
hmac256(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha256/1, ?SHA_256_BLOCKSIZE).
+%% @spec hmac384(key(), data()) -> mac()
+%% where
+%% key() = iolist() | binary()
+%% data() = iolist() | binary()
+%% mac() = binary()
+%% @doc Compute a SHA-384 MAC message authentication code from key and data.
+%%
hmac384(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha384/1, ?SHA_384_BLOCKSIZE).
+%% @spec hmac512(key(), data()) -> mac()
+%% where
+%% key() = iolist() | binary()
+%% data() = iolist() | binary()
+%% mac() = binary()
+%% @doc Compute a SHA-512 MAC message authentication code from key and data.
+%%
hmac512(Key, Data) ->
hmac(Key, Data, fun erlsha2:sha512/1, ?SHA_512_BLOCKSIZE).
+%% @spec hmac(key(), data()) -> mac()
+%% where
+%% key() = iolist() | binary()
+%% data() = iolist() | binary()
+%% mac() = binary()
+%% @doc Compute a SHA MAC message authentication code from key and data.
+%%
hmac(Key, Data) ->
crypto:sha_mac(Key, Data).
+%% @spec hmac(key(), data(), hash(), blocksize()) -> mac()
+%% where
+%% key() = iolist() | binary()
+%% data() = iolist() | binary()
+%% hash() = fun((binary()) -> binary())
+%% blocksize() = non_neg_integer()
+%% mac() = binary()
+%% @doc Compute a SHA MAC message authentication code from key and data using
+%% the specified hash function and blocksize.
+%%
hmac(Key, Data, Hash, Blocksize) when is_list(Key) ->
- hmac(list_to_binary(Key), Data, Hash, Blocksize);
+ hmac(iolist_to_binary(Key), Data, Hash, Blocksize);
hmac(Key, Data, Hash, Blocksize) when is_list(Data) ->
- hmac(Key, list_to_binary(Data), Hash, Blocksize);
+ hmac(Key, iolist_to_binary(Data), Hash, Blocksize);
hmac(Key, Data, Hash, Blocksize) when is_binary(Key), is_binary(Data) ->
HashKey =
case Blocksize - byte_size(Key) of

0 comments on commit c4b4bd8

Please sign in to comment.