PWSafe3/PasswordPolicy.pm

filter: Statementstmt 20.0% 22 / 110 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 23.8% 5 / 21 Podpod 0.0% 0 / 3 totaltotal 14.9% 27 / 181 CC 29 SCAR 29.7
CC
29
Cov
14%
decode
56.0
encode
42.0
new
30.0
CRAP 19.4
« PWSafe3.pm ↑ Summary PWSafe3/Record.pm »
1 #
2 # Copyright (c) 2011-2015 T.v.Dein <tlinden |AT| cpan.org>.
3 #
4 # Licensed under the terms of the Artistic License 2.0
5 # see: http://www.perlfoundation.org/artistic_license_2_0
6 #
7 package Crypt::PWSafe3::PasswordPolicy;
8  
9  
10 1 ▶use Carp::Heavy;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 $Crypt::PWSafe3::PasswordPolicy::VERSION = '1.01';
17  
18 1 my %flagbits = (
19 UseLowercase => 0x8000,
20 UseUppercase => 0x4000,
21 UseDigits => 0x2000,
22 UseSymbols => 0x1000,
23 UseHexDigits => 0x0800,
24 UseEasyVision => 0x0400,
25 MakePronounceable => 0x0200
26 );
27  
28  
29 1 my @flags = qw(UseLowercase UseUppercase UseDigits UseSymbols UseHexDigits UseEasyVision MakePronounceable);
30 1 my @fields = qw(raw MaxLength MinLowercase MinUppercase MinDigits MinSymbols);
31  
32 1 foreach my $field (@fields, @flags) {
33 0 ▶ eval qq(
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Subroutine __ANON__: not called CC=2 SCAR=17.7
Branch: 13 branches
BranchTrueFalseTotal
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
if ($arg) { } 0 0 0
34 *Crypt::PWSafe3::PasswordPolicy::$field = sub {
35 my(\$this, \$arg) = \@_;
36 if (\$arg) {
37 return \$this->{$field} = \$arg;
38 }
39 else {
40 return \$this->{$field};
41 }
42 }
43 );
44 }
45  
46 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod undocumented
47 #
48 # new PasswordPolicy object
49 0 my($this, %param) = @_;
50 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
51 0 my $self = \%param;
52 0 bless($self, $class);
53  
54  
55 0 ▶ if (exists $param{raw}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{'raw'}) { } 0 0 0
56 0 $self->decode($param{raw});
57 }
58 else {
59 0 foreach my $field (@fields, @flags) {
60 0 ▶ if(exists $param{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{$field}) { } 0 0 0
61 0 $self->{$field} = $param{$field};
62 }
63 else {
64 0 $self->{$field} = 0;
65 }
66 }
67 }
68  
69 0 return $self;
70 }
71  
72  
73 0 ▶sub decode {
Subroutine decode: not called CC=7 SCAR=40.3
Pod undocumented
74 0 my($this, $raw) = @_;
75  
76 0 ▶ return if $raw eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "" 0 0 0
77  
78 # expected input: ffffnnnllluuudddsss
79  
80 # create a 6-elemt array
81 0 my @exp = unpack("A4A3A3A3A3A3", $raw);
82  
83 # convert the hex strings to integers
84 0 my %pwpol;
85 0 foreach my $i (0 .. 5) {
86 0 $pwpol{$fields[$i]} = hex($exp[$i]);
87 }
88  
89 # assign the numbers to interns
90 0 foreach my $field (@fields) {
91 0 ▶ next if $field eq "raw";
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "raw" 0 0 0
92 0 $this->{$field} = $pwpol{$field};
93 }
94  
95 # convert binary flags to true/false values
96 0 foreach my $bit (keys %flagbits) {
97 0 ▶ if($pwpol{raw} & $flagbits{$bit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pwpol{'raw'} & $flagbits{$bit}) { } 0 0 0
98 0 $this->{$bit} = 1;
99 }
100 else {
101 0 $this->{$bit} = 0;
102 }
103 }
104 }
105  
106  
107 0 ▶sub encode {
Subroutine encode: not called CC=6 SCAR=37.4
Pod undocumented
108 0 my($this) = @_;
109
110 # create the bitmask
111 0 my $mask = 0;
112 0 foreach my $bit (keys %flagbits) {
113 0 ▶ if($this->{$bit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{$bit}) 0 0 0
114 0 $mask |= $flagbits{$bit};
115 }
116 }
117  
118 # first 4hex chars for the bitmask of policy flags
119 0 my $raw = sprintf "%04x", $mask;
120  
121 # followed by the number fields
122 0 foreach my $field (@fields) {
123 0 ▶ next if $field eq "raw";
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "raw" 0 0 0
124 0 $raw .= sprintf "%03x", $this->{$field};
125 }
126  
127 0 ▶ if($raw =~ /^0*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw =~ /^0*$/) { } 0 0 0
128 0 return '';
129 }
130 else {
131 0 return $raw;
132 }
133 }
134  
135  
136  
137 =head1 NAME
138  
139 Crypt::PWSafe3::PasswordPolicy - represent a passwordsafe v3 passwprd policy entry of a record.
140  
141 =head1 SYNOPSIS
142  
143 use Data::Dumper;
144 use Crypt::PWSafe3;
145 use Crypt::PWSafe3::PasswordPolicy;
146 my $record = $vault->getrecord($uuid);
147 my $policy = $record->policy;
148  
149 # print current values
150 print Dumper($policy);
151  
152 # print some values
153 print $policy->UseEasyVision;
154  
155 # change some of them
156 $policy->MaxLength(8);
157 $policy->MinSymbols(2);
158  
159 # put back into record
160 $record->policy($policy);
161  
162
163  
164 =head1 DESCRIPTION
165  
166 The following flags can be set (1 = TRUE, 0 = FALSE):
167  
168 - UseLowercase
169 - UseUppercase
170 - UseDigits
171 - UseSymbols
172 - UseHexDigits
173 - UseEasyVision
174 - MakePronounceable
175  
176 The following numerical settings can be tuned:
177  
178 - MaxLength
179 - MinLowercase
180 - MinUppercase
181 - MinDigits
182 - MinSymbols
183  
184 All of them can be called as functions, see SYNOPSIS for examples. If called with an argument,
185 the value will be changed, otherwise it will just returned.
186  
187 The raw database value can be assigned by using the B<raw> parameter.
188  
189 =head1 SEE ALSO
190  
191 L<Crypt::PWSafe3::Record>
192  
193 =head1 AUTHOR
194  
195 T.v.Dein <tlinden@cpan.org>
196  
197 =head1 COPYRIGHT
198  
199 Copyright (c) 2011-2015 by T.v.Dein <tlinden@cpan.org>.
200 All rights reserved.
201  
202 =head1 LICENSE
203  
204 This program is free software; you can redistribute it
205 and/or modify it under the same terms of the Artistic
206 License 2.0, see: L<http://www.perlfoundation.org/artistic_license_2_0>
207  
208  
209 =cut
210  
211 1 1;
« PWSafe3.pm ↑ Summary PWSafe3/Record.pm »