Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Added basic application callback and supervisor
Restructured build process and bumped version to 0.2
  • Loading branch information
hntrmrrs committed Aug 18, 2009
1 parent 6d81df9 commit af5de6278e31a871d0f36a6cb3350db112bbe2a8
Showing 9 changed files with 155 additions and 42 deletions.
@@ -146,5 +146,6 @@ AC_REVISION([LOCAL_VERSION])
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([lib/Makefile])
AC_CONFIG_FILES([lib/bcrypt/Makefile])
AC_CONFIG_FILES([lib/bcrypt/priv/Makefile])

AC_OUTPUT
@@ -1,4 +1,4 @@
## Copyright (c) 2008 Hunter Morris <huntermorris@gmail.com>
## Copyright (c) 2009 Hunter Morris <huntermorris@gmail.com>

## Permission to use, copy, modify, and distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
@@ -12,25 +12,20 @@
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

bcryptebindir = $(localerlanglibdir)/bcrypt/ebin
bcryptprivlibdir = $(localerlanglibdir)/bcrypt/priv/lib

locallibbin_PROGRAMS = bcrypt
SUBDIRS = priv

bcrypt_SOURCES = \
c_src/bcrypt_erlang.c \
c_src/blowfish.c \
c_src/bcrypt.c \
c_src/erl_blf.h

bcrypt_LDADD = -lpthread -lerl_interface -lei
bcryptebindir = $(localerlanglibdir)/bcrypt/ebin

bcrypt_file_collection = \
src/bcrypt_app.erl \
src/bcrypt_sup.erl \
src/bcrypt.erl

bcryptebin_static_file = ebin/bcrypt.app

bcryptebin_make_generated_file_list = \
ebin/bcrypt_app.beam \
ebin/bcrypt_sup.beam \
ebin/bcrypt.beam

bcryptebin_DATA = \
@@ -2,12 +2,11 @@
%%% application.
{application, bcrypt,
[{description, "An Erlang wrapper for the OpenBSD password scheme, bcrypt."},
{vsn, "0.1.0"},
{modules, [bcrypt]},
{registered, []},
{env, [
{lib_dir, "/tmp"}
]},
{applications, [kernel, stdlib, sasl, crypto]}
{vsn, "0.2.0"},
{modules, [bcrypt_app, bcrypt_sup, bcrypt]},
{registered, [bcrypt]},
{env, []},
{applications, [kernel, stdlib, sasl, crypto]},
{mod, {bcrypt_app, []}}
]
}.
Empty file.
@@ -0,0 +1,25 @@
## Copyright (c) 2009 Hunter Morris <huntermorris@gmail.com>

## Permission to use, copy, modify, and distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.

## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
## ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

bcryptprivlibdir = $(localerlanglibdir)/bcrypt/priv

bcryptprivlib_PROGRAMS = bcrypt

bcrypt_SOURCES = \
../c_src/bcrypt_erlang.c \
../c_src/blowfish.c \
../c_src/bcrypt.c \
../c_src/erl_blf.h

bcrypt_LDADD = -lpthread -lerl_interface -lei
Binary file not shown.
@@ -1,5 +1,5 @@
%% @author Hunter Morris <huntermorris@gmail.com>
%% @copyright 2008 Hunter Morris
%% @copyright 2009 Hunter Morris
%%
%% @doc Wrapper around the OpenBSD Blowfish password hashing algorithm, as
%% described in "A Future-Adaptable Password Scheme" by Niels Provos and
@@ -29,9 +29,9 @@
-behaviour(gen_server).

%% API
-export([start_link/1, stop/1]).
-export([gen_salt/1, gen_salt/2]).
-export([hashpw/3]).
-export([start_link/0, start_link/1, stop/0]).
-export([gen_salt/0, gen_salt/1]).
-export([hashpw/2]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
@@ -47,55 +47,74 @@
-define(MAX_LOG_ROUNDS(L), L < 32).
-define(MIN_LOG_ROUNDS(L), L > 3).

%%--------------------------------------------------------------------
%% @doc Start a bcrypt port using the default filename
%% @spec start_link() -> {ok, pid()}
%% @end
%%--------------------------------------------------------------------
start_link() ->
case application:get_env(bcrypt, bcrypt_filename) of
{ok, Filename} ->
start_link(Filename);
undefined ->
case code:priv_dir(bcrypt) of
{error, bad_name} ->
{stop, bcrypt_not_found};
Dir ->
start_link(Dir ++ "/bcrypt")
end
end.

%%--------------------------------------------------------------------
%% @doc Start a bcrypt port server
%% @spec start_link(Filename::fname()) -> {ok, pid()}
%% @end
%%--------------------------------------------------------------------
start_link(Filename) when is_list(Filename)
; is_atom(Filename) ->
gen_server:start_link(?MODULE, [{filename, Filename}], []).
gen_server:start_link({local, ?MODULE}, ?MODULE,
[{filename, Filename}], []).

%%--------------------------------------------------------------------
%% @doc Stop a bcrypt port server
%% @spec stop(Pid::pid()) -> ok
%% @spec stop() -> ok
%% @end
%%--------------------------------------------------------------------
stop(Pid) when is_pid(Pid) ->
gen_server:call(Pid, stop).
stop() ->
gen_server:call(?MODULE, stop).

%%--------------------------------------------------------------------
%% @doc Generate a salt with the default number of rounds, 12.
%% @see gen_salt/2
%% @spec gen_salt(Pid::pid()) -> string()
%% @see gen_salt/1
%% @spec gen_salt() -> string()
%% @end
%%--------------------------------------------------------------------
gen_salt(Pid) when is_pid(Pid) ->
gen_salt(Pid, ?DEFAULT_LOG_ROUNDS).
gen_salt() ->
gen_salt(?DEFAULT_LOG_ROUNDS).

%%--------------------------------------------------------------------
%% @doc Generate a random text salt for use with hashpw/3. LogRounds
%% defines the complexity of the hashing, increasing the cost as
%% 2^log_rounds.
%% @spec gen_salt(Pid::pid(), integer()) -> string()
%% @spec gen_salt(integer()) -> string()
%% @end
%%--------------------------------------------------------------------
gen_salt(Pid, LogRounds) when is_pid(Pid), is_integer(LogRounds),
?MAX_LOG_ROUNDS(LogRounds),
?MIN_LOG_ROUNDS(LogRounds) ->
gen_salt(LogRounds) when is_integer(LogRounds),
?MAX_LOG_ROUNDS(LogRounds),
?MIN_LOG_ROUNDS(LogRounds) ->
R = crypto:rand_bytes(16),
gen_server:call(Pid, {encode_salt, R, LogRounds}).
gen_server:call(?MODULE, {encode_salt, R, LogRounds}).

%%--------------------------------------------------------------------
%% @doc Hash the specified password and the salt using the OpenBSD Blowfish
%% password hashing algorithm. Returns the hashed password.
%% @spec hashpw(Pid::pid(), Password::password(), Salt::string()) -> string()
%% @doc Hash the specified password and the salt using the OpenBSD
%% Blowfish password hashing algorithm. Returns the hashed password.
%% @spec hashpw(Password::password(), Salt::string()) -> string()
%% @end
%%--------------------------------------------------------------------
hashpw(Pid, Password, Salt)
when is_pid(Pid), (is_list(Password) or is_binary(Password)),
hashpw(Password, Salt)
when is_list(Password); is_binary(Password),
is_list(Salt) ->
gen_server:call(Pid, {hashpw, Password, Salt}, infinity).
gen_server:call(?MODULE, {hashpw, Password, Salt}, infinity).

%%====================================================================
%% gen_server callbacks
@@ -0,0 +1,37 @@
%% @author Hunter Morris <huntermorris@gmail.com>
%% @copyright 2009 Hunter Morris
%%
%% @doc Bcrypt application callback module
%% @end
%%
%% Permission to use, copy, modify, and distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.

%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-module(bcrypt_app).
-author('Hunter Morris <huntermorris@gmail.com>').

-behaviour(application).

-export([start/0, start/2, stop/1]).

start() ->
start(normal, []).

start(normal, Args) ->
case bcrypt_sup:start_link(Args) of
{ok, Pid} ->
{ok, Pid};
Error ->
Error
end.

stop(_State) ->
ok.
@@ -0,0 +1,37 @@
%% @author Hunter Morris <huntermorris@gmail.com>
%% @copyright 2009 Hunter Morris
%%
%% @doc Bcrypt supervisor
%% @end
%%
%% Permission to use, copy, modify, and distribute this software for any
%% purpose with or without fee is hereby granted, provided that the above
%% copyright notice and this permission notice appear in all copies.

%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-module(bcrypt_sup).
-author('Hunter Morris <huntermorris@gmail.com>').

-behaviour(supervisor).

%% External
-export([start_link/1]).

%% supervisor cb
-export([init/1]).

start_link(Args) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, Args).

init(_Args) ->
Bcrypt =
{bcrypt, {bcrypt, start_link, []},
permanent, 2000, worker, [bcrypt]},

{ok,{{one_for_one, 15, 60}, [Bcrypt]}}.

0 comments on commit af5de62

Please sign in to comment.