Skip to content
Browse files

Add checks for character types

  • Loading branch information...
1 parent adb92ad commit b72eabaa3f1436ea852f4d242fcdf8a7b0390bd3 @msantos committed Aug 17, 2010
Showing with 95 additions and 1 deletion.
  1. +21 −0 README
  2. +37 −0 include/cerck.hrl
  3. +37 −1 src/cerck.erl
View
21 README
@@ -12,6 +12,16 @@ HOW TO USE IT
check(Password) -> ok | {error, string()}
Types Passwod = [ binary() | string() ]
+quality(Password) -> #passwd_quality{}
+
+has(Type, Stats) -> true | false
+ Types Type = [ upper | lower | number | other ]
+ Stats = #passwd_quality{}
+
+has(Type, Stats, Min) -> true | false
+ Types Type = [ upper | lower | number | other ]
+ Stats = #passwd_quality{}
+ Min = integer()
EXAMPLES
@@ -20,6 +30,17 @@ EXAMPLES
2> 4> cerck:check(<<"f00b4r">>).
ok
+For quality checks:
+
+1> rr("include/cerck.hrl").
+[passwd_quality]
+2> S = cerck:quality(<<"f00b4r">>).
+#passwd_quality{upper = 0,lower = 3,number = 3,other = 0}
+3> cerck:has(number, S).
+true
+4> cerck:has(other, S).
+false
+
*** WARNING ***
View
37 include/cerck.hrl
@@ -0,0 +1,37 @@
+%% Copyright (c) 2010, Michael Santos <michael.santos@gmail.com>
+%% All rights reserved.
+%%
+%% Redistribution and use in source and binary forms, with or without
+%% modification, are permitted provided that the following conditions
+%% are met:
+%%
+%% Redistributions of source code must retain the above copyright
+%% notice, this list of conditions and the following disclaimer.
+%%
+%% 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.
+%%
+%% Neither the name of the author nor the names of its contributors
+%% may be used to endorse or promote products derived from this software
+%% without specific prior written permission.
+%%
+%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+%% "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
+%% COPYRIGHT HOLDER OR CONTRIBUTORS 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.
+
+-record(passwd_quality, {
+ upper = 0, % count of uppercase letters
+ lower = 0, % count of lowercase letters
+ number = 0, % count of numeric characters
+ other = 0 % count of everything else
+ }).
View
38 src/cerck.erl
@@ -29,12 +29,16 @@
%% ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%% POSSIBILITY OF SUCH DAMAGE.
-module(cerck).
+-include("cerck.hrl").
-export([
dictpath/0,
- check/1, check/2
+ check/1, check/2,
+ quality/1,
+ has/2, has/3
]).
+
-on_load(on_load/0).
@@ -49,6 +53,38 @@ check(_,_) ->
check(Passwd) ->
check(Passwd, dictpath()).
+quality(Passwd) when is_binary(Passwd) ->
+ quality(binary_to_list(Passwd));
+quality(Passwd) when is_list(Passwd) ->
+ quality1(Passwd, #passwd_quality{}).
+
+quality1([], Stats) ->
+ Stats;
+quality1([H|T], #passwd_quality{lower = N} = Stats) when H >= $a, H =< $z ->
+ quality1(T, Stats#passwd_quality{lower = N+1});
+quality1([H|T], #passwd_quality{upper = N} = Stats) when H >= $A, H =< $Z ->
+ quality1(T, Stats#passwd_quality{upper = N+1});
+quality1([H|T], #passwd_quality{number = N} = Stats) when H >= $0, H =< $9 ->
+ quality1(T, Stats#passwd_quality{number = N+1});
+quality1([_|T], #passwd_quality{other = N} = Stats) ->
+ quality1(T, Stats#passwd_quality{other = N+1}).
+
+has(lower, Stats) ->
+ has(lower, Stats, 1);
+has(upper, Stats) ->
+ has(upper, Stats, 1);
+has(number, Stats) ->
+ has(number, Stats, 1);
+has(other, Stats) ->
+ has(other, Stats, 1).
+
+has(lower, #passwd_quality{lower = N}, Min) when N >= Min -> true;
+has(upper, #passwd_quality{upper = N}, Min) when N >= Min -> true;
+has(number, #passwd_quality{number = N}, Min) when N >= Min -> true;
+has(other, #passwd_quality{other = N}, Min) when N >= Min -> true;
+has(Type, #passwd_quality{}, _) when Type == lower; Type == upper; Type == number; Type == other ->
+ false.
+
privdir(File) ->
filename:join([

0 comments on commit b72eaba

Please sign in to comment.
Something went wrong with that request. Please try again.