Skip to content

Commit

Permalink
revamp the original pure Erlang implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vinoski committed Feb 19, 2011
0 parents commit 475111d
Show file tree
Hide file tree
Showing 47 changed files with 2,089 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@
*~
.eunit
c_src/config.h
c_src/*.o
doc
ebin
priv
1 change: 1 addition & 0 deletions AUTHORS
@@ -0,0 +1 @@
Steve Vinoski <vinoski@ieee.org>
27 changes: 27 additions & 0 deletions LICENSE
@@ -0,0 +1,27 @@
Copyright (c) 2009-2011 Stephen B. Vinoski
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
30 changes: 30 additions & 0 deletions README.md
@@ -0,0 +1,30 @@
# SHA-224, SHA-256, SHA-384, SHA-512 implemented in Erlang NIFs.

## 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.

See the following links for details:

* (Secure Hash Standard)[href="http://csrc.nist.gov/publications/fips/fips180-3/fips180-3_final.pdf"]
* (Wikipedia SHA1 article)[href="http://en.wikipedia.org/wiki/SHA1"]

The code implemented here was written by simply following the
algorithm descriptions provided in the standard. Provided functions
follow the same style as those found in the standard Erlang `crypto`
module: for each hash variant there's a simple function returning a
binary digest and a set of three functions for initializing a digest
context, updating the context with additional data to be hashed, and
finalizing the context to get a binary digest result.

## Building and Installing

The **erlsha2** app is built with
[rebar](https://github.com/basho/rebar), which must be in the command `PATH`.

### Erlang Version

The **erlsha2** app requires Erlang R14B or later.
39 changes: 39 additions & 0 deletions c_src/config.sh
@@ -0,0 +1,39 @@
#!/usr/bin/env bash

set -e

CONFIG_HDR=c_src/config.h

if [[ $1 = clean ]]; then
rm -f $CONFIG_HDR
exit 0
fi

[[ -n "$CC" ]]

tmpfile=`mktemp /tmp/erlsha2.XXXXXX`
tmpcfile=${tmpfile}.c
trap "rm -f $tmpfile $tmpcfile" EXIT
mv $tmpfile $tmpcfile
echo '#include <stdint.h>' > $tmpcfile
if $CC $CFLAGS -c -o /dev/null $tmpcfile 2>/dev/null ; then
echo '#define HAVE_STDINT_H 1' > $CONFIG_HDR
else
echo '#include <inttypes.h>' > $tmpcfile
if $CC $CFLAGS -c -o /dev/null $tmpcfile 2>/dev/null ; then
echo '#define HAVE_INTTYPES_H 1' > $CONFIG_HDR
else
echo 'neither <stdint.h> nor <inttypes.h> found, aborting' 1>&2
exit 1
fi
fi

v='16#12345678'
prog="case <<$v:32/native>> of <<$v:32/big>> -> 0; <<$v:32/little>> -> 1 end"
if erl -noinput -noshell -eval "halt($prog)."; then
echo '#define WORDS_BIGENDIAN 1' >> $CONFIG_HDR
else
echo '#undef WORDS_BIGENDIAN' >> $CONFIG_HDR
fi

exit 0

0 comments on commit 475111d

Please sign in to comment.