aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel White <[email protected]>2012-09-27 02:40:11 +1000
committerSverker Eriksson <[email protected]>2012-12-12 15:35:36 +0100
commitffef856e205bcaf42a1a9a8cc5fb4965fd459edc (patch)
tree19778c527d674f63d54b27c9e888864279a2d237
parent918546531fafb9a20170fc1ba1a2c876a46b3cc1 (diff)
downloadotp-ffef856e205bcaf42a1a9a8cc5fb4965fd459edc.tar.gz
otp-ffef856e205bcaf42a1a9a8cc5fb4965fd459edc.tar.bz2
otp-ffef856e205bcaf42a1a9a8cc5fb4965fd459edc.zip
crypto: Provide a generic interface for HMAC generation
-rw-r--r--lib/crypto/doc/src/crypto.xml17
-rw-r--r--lib/crypto/src/crypto.erl21
2 files changed, 36 insertions, 2 deletions
diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml
index 0f21441748..5e2a7acb97 100644
--- a/lib/crypto/doc/src/crypto.xml
+++ b/lib/crypto/doc/src/crypto.xml
@@ -343,6 +343,23 @@ Mpint() = <![CDATA[<<ByteLen:32/integer-big, Bytes:ByteLen/binary>>]]>
</desc>
</func>
<func>
+ <name>hmac(Type, Key, Data) -> Mac</name>
+ <name>hmac(Type, Key, Data, MacLength) -> Mac</name>
+ <fsummary></fsummary>
+ <type>
+ <v>Type = md5 | sha | sha224 | sha256 | sha384 | sha512</v>
+ <v>Key = iodata()</v>
+ <v>Data = iodata()</v>
+ <v>MacLength = integer()</v>
+ <v>Mac = binary()</v>
+ </type>
+ <desc>
+ <p>Computes a HMAC of type <c>Type</c> from <c>Data</c> using
+ <c>Key</c> as the authentication key.</p> <c>MacLength</c>
+ will limit the size of the resultant <c>Mac</c>.
+ </desc>
+ </func>
+ <func>
<name>hmac_init(Type, Key) -> Context</name>
<fsummary></fsummary>
<type>
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index 461558a79e..5e7f1a948a 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -35,7 +35,7 @@
-export([sha256_mac/2, sha256_mac/3]).
-export([sha384_mac/2, sha384_mac/3]).
-export([sha512_mac/2, sha512_mac/3]).
--export([hmac_init/2, hmac_update/2, hmac_final/1, hmac_final_n/2]).
+-export([hmac/3, hmac/4, hmac_init/2, hmac_update/2, hmac_final/1, hmac_final_n/2]).
-export([des_cbc_encrypt/3, des_cbc_decrypt/3, des_cbc_ivec/1]).
-export([des_ecb_encrypt/2, des_ecb_decrypt/2]).
-export([des_cfb_encrypt/3, des_cfb_decrypt/3, des_cfb_ivec/2]).
@@ -107,7 +107,7 @@
blowfish_ecb_encrypt, blowfish_ecb_decrypt, blowfish_ofb64_encrypt,
des_cbc_ivec, des_cfb_ivec, erlint, mpint,
hash, hash_init, hash_update, hash_final,
- hmac_init, hmac_update, hmac_final, hmac_final_n, info,
+ hmac, hmac_init, hmac_update, hmac_final, hmac_final_n, info,
rc2_cbc_encrypt, rc2_cbc_decrypt,
info_lib]).
@@ -418,11 +418,28 @@ sha512_final_nif(_Context) -> ?nif_stub.
%%
%% HMAC (multiple hash options)
%%
+
+-spec hmac(_, iodata(), iodata()) -> binary().
+-spec hmac(_, iodata(), iodata(), integer()) -> binary().
-spec hmac_init(atom(), iodata()) -> binary().
-spec hmac_update(binary(), iodata()) -> binary().
-spec hmac_final(binary()) -> binary().
-spec hmac_final_n(binary(), integer()) -> binary().
+hmac(md5, Key, Data) -> md5_mac(Key, Data);
+hmac(sha, Key, Data) -> sha_mac(Key, Data);
+hmac(sha224, Key, Data) -> sha224_mac(Key, Data);
+hmac(sha256, Key, Data) -> sha256_mac(Key, Data);
+hmac(sha384, Key, Data) -> sha384_mac(Key, Data);
+hmac(sha512, Key, Data) -> sha512_mac(Key, Data).
+
+hmac(md5, Key, Data, Size) -> md5_mac_n(Key, Data, Size);
+hmac(sha, Key, Data, Size) -> sha_mac(Key, Data, Size);
+hmac(sha224, Key, Data, Size) -> sha224_mac(Key, Data, Size);
+hmac(sha256, Key, Data, Size) -> sha256_mac(Key, Data, Size);
+hmac(sha384, Key, Data, Size) -> sha384_mac(Key, Data, Size);
+hmac(sha512, Key, Data, Size) -> sha512_mac(Key, Data, Size).
+
hmac_init(_Type, _Key) -> ?nif_stub.
hmac_update(_Context, _Data) -> ? nif_stub.
hmac_final(_Context) -> ? nif_stub.