From 42e65ffe5f2659d998ff0a7e5ebea2573c23a86f Mon Sep 17 00:00:00 2001
From: Sverker Eriksson
Date: Thu, 16 Aug 2012 16:38:31 +0200
Subject: crypto: Add more generic hash interface
---
lib/crypto/doc/src/crypto.xml | 51 +++++++++++++++++++++++++++++++++++++++++++
lib/crypto/src/crypto.erl | 33 ++++++++++++++++++++++++++--
2 files changed, 82 insertions(+), 2 deletions(-)
diff --git a/lib/crypto/doc/src/crypto.xml b/lib/crypto/doc/src/crypto.xml
index 36f8bc6deb..0d78dbc426 100644
--- a/lib/crypto/doc/src/crypto.xml
+++ b/lib/crypto/doc/src/crypto.xml
@@ -255,6 +255,57 @@ Mpint() = >]]>
the computed SHA message digest.
+
+ hash(Type, Data) -> Digest
+
+
+ Type = md4 | md5 | sha | sha224 | sha256 | sha384 | sha512
+ Data = iodata()
+ Digest = binary()
+
+
+ Computes a message digest of type Type from Data.
+
+
+
+ hash_init(Type) -> Context
+
+
+ Type = md4 | md5 | sha | sha224 | sha256 | sha384 | sha512
+
+
+ Initializes the context for streaming hash operations. Type determines
+ which digest to use. The returned context should be used as argument
+ to hash_update.
+
+
+
+ hash_update(Context, Data) -> NewContext
+
+
+ Data = iodata()
+
+
+ Updates the digest represented by Context using the given Data. Context
+ must have been generated using hash_init
+ or a previous call to this function. Data can be any length. NewContext
+ must be passed into the next call to hash_update
+ or hash_final.
+
+
+
+ hash_final(Context) -> Digest
+
+
+ Digest = binary()
+
+
+ Finalizes the hash operation referenced by Context returned
+ from a previous call to hash_update.
+ The size of Digest is determined by the type of hash
+ function used to generate it.
+
+
md5_mac(Key, Data) -> Mac
Compute an MD5 MACmessage authentification code
diff --git a/lib/crypto/src/crypto.erl b/lib/crypto/src/crypto.erl
index a07e7a30b4..63043888b9 100644
--- a/lib/crypto/src/crypto.erl
+++ b/lib/crypto/src/crypto.erl
@@ -22,7 +22,7 @@
-module(crypto).
-export([start/0, stop/0, info/0, info_lib/0, version/0]).
--export([hash/2]).
+-export([hash/2, hash_init/1, hash_update/2, hash_final/1]).
-export([md4/1, md4_init/0, md4_update/2, md4_final/1]).
-export([md5/1, md5_init/0, md5_update/2, md5_final/1]).
-export([sha/1, sha_init/0, sha_update/2, sha_final/1]).
@@ -194,11 +194,40 @@ version() -> ?CRYPTO_VSN.
hash(md5, Data) -> md5(Data);
hash(md4, Data) -> md4(Data);
hash(sha, Data) -> sha(Data);
-hash(sha1, Data) -> sha(Data);
+hash(sha224, Data) -> sha224(Data);
hash(sha256, Data) -> sha256(Data);
hash(sha384, Data) -> sha384(Data);
hash(sha512, Data) -> sha512(Data).
+-spec hash_init('md5'|'md4'|'sha'|'sha224'|'sha256'|'sha384'|'sha512') -> any().
+
+hash_init(md5) -> {md5, md5_init()};
+hash_init(md4) -> {md4, md4_init()};
+hash_init(sha) -> {sha, sha_init()};
+hash_init(sha224) -> {sha224, sha224_init()};
+hash_init(sha256) -> {sha256, sha256_init()};
+hash_init(sha384) -> {sha384, sha384_init()};
+hash_init(sha512) -> {sha512, sha512_init()}.
+
+-spec hash_update(_, iodata()) -> any().
+
+hash_update({md5,Context}, Data) -> {md5, md5_update(Context,Data)};
+hash_update({md4,Context}, Data) -> {md4, md4_update(Context,Data)};
+hash_update({sha,Context}, Data) -> {sha, sha_update(Context,Data)};
+hash_update({sha224,Context}, Data) -> {sha224, sha224_update(Context,Data)};
+hash_update({sha256,Context}, Data) -> {sha256, sha256_update(Context,Data)};
+hash_update({sha384,Context}, Data) -> {sha384, sha384_update(Context,Data)};
+hash_update({sha512,Context}, Data) -> {sha512, sha512_update(Context,Data)}.
+
+-spec hash_final(_) -> binary().
+
+hash_final({md5,Context}) -> md5_final(Context);
+hash_final({md4,Context}) -> md4_final(Context);
+hash_final({sha,Context}) -> sha_final(Context);
+hash_final({sha224,Context}) -> sha224_final(Context);
+hash_final({sha256,Context}) -> sha256_final(Context);
+hash_final({sha384,Context}) -> sha384_final(Context);
+hash_final({sha512,Context}) -> sha512_final(Context).
%%
%% MD5
--
cgit v1.2.3