blib/script/bcrypt

filter: Statementstmt 50.5% 51 / 101 Branchbran 31.5% 12 / 38 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 68.1% 15 / 22 Podpod 0.0% 0 / 5 totaltotal 42.9% 85 / 198 CC 31 SCAR 35.4
CC
31
Cov
44%
run
100.7
_validate_args
42.0
_defaults
10.9
CRAP 34.4
↑ Summary /usr/local/bin/perldoc »
1 #!/usr/local/bin/perl
2  
3 28 ▶use v5.30;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use experimental qw(signatures);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf8
7  
8 =head1 NAME
9  
10 bcrypt - A command-line tool to deal with bcrypt password hashing
11  
12 =head1 SYNOPSIS
13  
14 Run this from the command line. By default, it reads one line of standard
15 input to get the password. This way the password doesn't show up in
16 your shell's history:
17  
18 $ bcrypt
19 Reading password on standard input...
20 Hello World
21 $2b$12$yQePGL/7.u6mVY8tt6fqTO08hQEc2qdWQ..FehvCwBIgMxzM0ULkq
22  
23 Control the various inputs:
24  
25 $ bcrypt --cost 10 --salt abcdef0123456789 --type 2a
26 Reading password on standard input...
27 Hello World
28 $2a$10$WUHhXETkKBCwKxOzLha2MOVweNmcwXQeoLR9jJlmEcl7rMR2ymqNu
29  
30 To check a password against a hash, use C<--compare>. Since the hashed
31 value has C<$>, which is a shell special character in various shells,
32 quote the value:
33  
34 $ perl script/bcrypt --compare '$2a$10$WUHhXETkKBCwKxOzLha2MOVweNmcwXQeoLR9jJlmEcl7rMR2ymqNu' <<<"Hello World"
35 Reading password on standard input...
36 Hello World
37 Match
38  
39 In a program, load the program as a module and call C<run> with the
40 same strings you'd use on the command line. The return value of C<run>
41 is the exit status:
42  
43 require '/path/to/bcrypt';
44  
45 my $exit_status = App::bcrypt::run( @command_line_strings );
46 exit $exit_status;
47  
48 =head1 DESCRIPTION
49  
50 =head2 Options
51  
52 =over 4
53  
54 =item * C<--compare> - the hash to compare to the password
55  
56 =item * C<--cost>, C<-c> - the cost factor, which should be between 5 and 31, inclusively. This is the exponent for a power of two, so things get slow quickly (default: 12)
57  
58 =item * C<--debug>, C<-d> - turn on debugging (default: off)
59  
60 =item * C<--eol>, C<-e> - the string to add to the end of the hash on output (default: newline)
61  
62 =item * C<--help>, C<-h> - show the documentation and exit. This takes precedence over all other options besides C<--update-modules> and C<--version>.
63  
64 =item * C<--no-eol>, C<-n> - do not add anything to the end of the line (default: off)
65  
66 =item * C<--password>, C<-p> - the password to hash or to compare. But, it's probably better to send it through standard input if you are using this as a command-line program and not a library.
67  
68 =item * C<--quiet>, C<-q> - suppress information messages (default: off)
69  
70 =item * C<--salt>, C<-s> - the salt to use. This is a string the should encode to 16-octet UTF-8 (default: random string)
71  
72 =item * C<--type>, C<-t> - the Bcrypt type, which is one of C<2a>, C<2b> (default), C<2x>, or C<2y>
73  
74 =item * C<--update-modules> - use cpan(1) to update or install all the Perl modules this program needs. This takes precedence over all other options.
75  
76 =item * C<--version>, C<-v> - show the version. With C<--debug>, this shows extra information about the Perl bits. This option takes precedence over all others except C<--update-modules>.
77  
78 =back
79  
80 =head2 Environment
81  
82 If these environment variables are set, they are used as the default values
83 when you do not specify a value through the command line options:
84  
85 =over 4
86  
87 =item * BCRYPT_COST
88  
89 =item * BCRYPT_EOL
90  
91 =item * BCRYPT_NO_EOL
92  
93 =item * BCRYPT_QUIET
94  
95 =item * BCRYPT_SALT
96  
97 =item * BCRYPT_TYPE
98  
99 =back
100  
101 =head2 Exit values
102  
103 =over 4
104  
105 =item * 0 - Success in either making the new hash, or that the hash and password match for C<--compare>
106  
107 =item * 1 - The hash and password for C<--compare> do not match
108  
109 =item * 2 - Invalid input
110  
111 =back
112  
113 =head2 Examples
114  
115 Install the modules using cpan if you've dropped this file into a system:
116  
117 $ bcrypt --update-modules
118  
119 Create a password with the defaults:
120  
121 $ bcrypt
122 Reading password on standard input...
123 Hello World
124  
125 Same thing, but without the extra output:
126  
127 $ perl script/bcrypt -q
128 Hello World
129 $2b$12$IqR8.HcodAzvngql3qDw2upnUrjj9HSxutWCgPrUsga0gm7AvTOUu
130  
131 By default, there's a newline at the end of the hash because it's a normal
132 output line, but you can remove that with C<--no-eol>. First, normally:
133  
134 $ bcrypt -q | hexdump -C
135 Hello World
136 00000000 24 32 62 24 31 32 24 35 4b 63 68 65 4f 6d 65 48 |$2b$12$5KcheOmeH|
137 00000010 61 67 52 62 77 2f 4a 57 70 79 54 55 65 73 70 39 |agRbw/JWpyTUesp9|
138 00000020 67 76 41 58 78 32 66 6e 79 6e 41 33 2f 6b 75 35 |gvAXx2fnynA3/ku5|
139 00000030 6a 43 50 4a 7a 70 5a 38 39 47 58 65 0a |jCPJzpZ89GXe.|
140 0000003d
141  
142 Now, notice the lack of the C<0a> at the end:
143  
144 $ bcrypt -q --no-eol | hexdump -C
145 Hello World
146 00000000 24 32 62 24 31 32 24 4d 36 67 2e 54 64 7a 48 49 |$2b$12$M6g.TdzHI|
147 00000010 44 58 52 30 73 33 66 2f 66 57 74 74 4f 61 54 70 |DXR0s3f/fWttOaTp|
148 00000020 34 2f 7a 57 5a 47 63 44 76 56 63 45 6d 6a 49 4e |4/zWZGcDvVcEmjIN|
149 00000030 50 79 4c 79 41 74 48 62 71 56 79 75 |PyLyAtHbqVyu|
150 0000003c
151  
152 Choose your own settings. The value for C<--salt> should be a string that
153 will UTF-8 encode to exactly 16 octets. Care with C<--cost> because that's
154 a power of 2:
155  
156 $ bcrypt --type 2a --cost 30 --salt abcdef0123456789
157 Reading password on standard input...
158 Hello World
159 $2a$10$WUHhXETkKBCwKxOzLha2MOVweNmcwXQeoLR9jJlmEcl7rMR2ymqNu
160  
161 Compare a hash to a password supplied on standard input:
162  
163 $ bcrypt --compare '$2a$10$WUHhXETkKBCwKxOzLha2MOVweNmcwXQeoLR9jJlmEcl7rMR2ymqNu'
164 Reading password on standard input...
165 Hello Worldd
166 Does not match
167  
168 Do the same thing quietly, but use the exit value to figure out what
169 happened. This bad password has an extra C<d>:
170  
171 $ bcrypt --quiet --compare '$2a$10$WUHhXETkKBCwKxOzLha2MOVweNmcwXQeoLR9jJlmEcl7rMR2ymqNu'
172 Hello Worldd
173 $ echo $?
174 1
175  
176 This password is the right one:
177  
178 $ bcrypt --quiet --compare '$2a$10$WUHhXETkKBCwKxOzLha2MOVweNmcwXQeoLR9jJlmEcl7rMR2ymqNu'
179 Hello World
180 $ echo $?
181 0
182  
183 =head1 SEE ALSO
184  
185 =over 4
186  
187 =item * Crypt::Bcrypt (which this program uses)
188  
189 =item * Mojolicious::Plugin::Bcrypt
190  
191 =item * Mojolicious::Command::bcrypt
192  
193 =item * https://github.com/shoenig/bcrypt-tool, a Golang tool
194  
195 =back
196  
197 =head1 SOURCE AVAILABILITY
198  
199 This source is in Github:
200  
201 http://github.com/briandfoy/app-bcrypt
202  
203 =head1 AUTHOR
204  
205 brian d foy, C<< <brian d foy> >>
206  
207 =head1 COPYRIGHT AND LICENSE
208  
209 Copyright © 2023-2026, brian d foy, All Rights Reserved.
210  
211 You may redistribute this under the terms of the Artistic License 2.0.
212  
213 =cut
214  
215 package App::bcrypt;
216  
217 20 ▶sub VERSION { '1.001' }
Subroutine VERSION: called CC=1 SCAR=0.0
Pod undocumented
218  
219 28 ▶exit run( @ARGV ) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 28 0 28
220  
221 26 ▶sub EX_SUCCESS () { 0 }
Subroutine EX_SUCCESS: called CC=1 SCAR=0.0
Pod undocumented
222 0 ▶sub EX_NO_MATCH () { 1 }
Subroutine EX_NO_MATCH: not called CC=1 SCAR=6.9
Pod undocumented
223 2 ▶sub EX_USAGE () { 2 }
Subroutine EX_USAGE: called CC=1 SCAR=0.0
Pod undocumented
224  
225 28 ▶sub run ( @args ) {
Subroutine run: called CC=14 SCAR=46.1
Pod undocumented
226 28 ▶ return _update_modules() if grep { $_ eq '--update-modules' } @args;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "--update-modules";} @args 0 28 28
227  
228 28 my $processed_args = _process_args(\@args);
229 28 ▶ exit EX_USAGE unless defined $processed_args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $processed_args 2 26 28
230  
231 26 ▶ return _show_version($processed_args->{'debug'}) if $processed_args->{'version'};
Branch: 1 branch
BranchTrueFalseTotal
if $processed_args->{"version"} 20 6 26
232 6 ▶ return _show_help() if $processed_args->{'help'};
Branch: 1 branch
BranchTrueFalseTotal
if $processed_args->{"help"} 6 0 6
233  
234 0 ▶ if( $processed_args->{quiet} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed_args->{"quiet"}) 0 0 0
235 28 ▶ no warnings qw(redefine);
Subroutine BEGIN: called CC=1 SCAR=0.0
236 0 ▶ *_message = sub { 1 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 0 }
238  
239 0 my $errors = _validate_args( $processed_args );
240  
241 0 ▶ if( $errors->@* > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$errors > 0) 0 0 0
242 0 foreach ( $errors->@* ) {
243 0 _message( $_ );
244 }
245 0 return EX_USAGE;
246 }
247  
248 0 ▶ if( ! defined $processed_args->{'password'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $processed_args->{"password"}) 0 0 0
249 0 $processed_args->{'password'} = _read_password();
250 }
251  
252 0 ▶ if( defined $processed_args->{compare} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $processed_args->{"compare"}) 0 0 0
253 0 state $rc = require Crypt::Bcrypt;
254 0 my $r = Crypt::Bcrypt::bcrypt_check( $processed_args->@{qw(password compare)} );
255  
256 0 return do {
257 0 ▶ if( $r ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
258 0 _message("Match");
259 0 EX_SUCCESS;
260 }
261 else {
262 0 _message("Does not match");
263 0 EX_NO_MATCH;
264 }
265 };
266 }
267  
268 0 ▶ if( $processed_args->{'debug'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($processed_args->{"debug"}) 0 0 0
269 0 say _dumper($processed_args);
270 }
271  
272 0 state $rc = require Crypt::Bcrypt;
273 0 my $hashed = Crypt::Bcrypt::bcrypt( $processed_args->@{qw(password type cost salt)} );
274  
275 0 local $\ = do {
276 0 ▶ if( $processed_args->{'no-eol'} ) { undef }
Branch: 2 branches
BranchTrueFalseTotal
if ($processed_args->{'no-eol'}) { } 0 0 0
elsif ($processed_args->{'eol'}) { } 0 0 0
277 0 elsif( $processed_args->{'eol'} ) { $processed_args->{'eol'} }
278 0 else { "\n" }
279 };
280  
281 0 print $hashed;
282  
283 0 return EX_SUCCESS;
284 }
285  
286 28 ▶sub _defaults () {
Subroutine _defaults: called CC=7 SCAR=23.8
287 my %hash = (
288 'no-eol' => $ENV{BCRYPT_NO_EOL} // 0,
289 cost => $ENV{BCRYPT_COST} // 12,
290 debug => $ENV{BCRYPT_DEBUG} // 0,
291 eol => $ENV{BCRYPT_EOL} // "\n",
292 quiet => $ENV{BCRYPT_QUIET} // 0,
293 salt => _default_salt(),
294 28 ▶ type => lc( $ENV{BCRYPT_TYPE} // '2b' ),
Condition50%6/12
$ENV{"BCRYPT_NO_EOL"} // 0
l!l
028
$ENV{"BCRYPT_COST"} // 12
l!l
028
$ENV{"BCRYPT_DEBUG"} // 0
l!l
028
$ENV{"BCRYPT_EOL"} // "\n"
l!l
028
$ENV{"BCRYPT_QUIET"} // 0
l!l
028
$ENV{"BCRYPT_TYPE"} // "2b"
l!l
028
MC/DC0%0/1
$ENV{"BCRYPT_NO_EOL"} // 0
$ENV{"BCRYPT_NO_EOL"}
MC/DC0%0/1
$ENV{"BCRYPT_COST"} // 12
$ENV{"BCRYPT_COST"}
MC/DC0%0/1
$ENV{"BCRYPT_DEBUG"} // 0
$ENV{"BCRYPT_DEBUG"}
MC/DC0%0/1
$ENV{"BCRYPT_EOL"} // "\n"
$ENV{"BCRYPT_EOL"}
MC/DC0%0/1
$ENV{"BCRYPT_QUIET"} // 0
$ENV{"BCRYPT_QUIET"}
MC/DC0%0/1
$ENV{"BCRYPT_TYPE"} // "2b"
$ENV{"BCRYPT_TYPE"}
Truth table50%1/2
A
A= $ENV{"BCRYPT_NO_EOL"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{"BCRYPT_COST"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{"BCRYPT_DEBUG"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{"BCRYPT_EOL"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{"BCRYPT_QUIET"}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{"BCRYPT_TYPE"}
Aresult
11
00
295 );
296  
297 28 return \%hash;
298 }
299  
300 28 ▶sub _default_salt () {
Subroutine _default_salt: called CC=2 SCAR=7.9
301 28 my $unencoded_salt = do {
302 28 ▶ if( exists $ENV{BCRYPT_SALT} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'BCRYPT_SALT'}) { } 0 28 28
303 0 state $rc = require Encode;
304 Encode::encode( 'UTF-8', $ENV{BCRYPT_SALT} )
305 0 }
306 else {
307 28 state $rc = require Crypt::URandom;
308 28 Crypt::URandom::urandom(16)
309 }
310 };
311 }
312  
313 0 ▶sub _dumper {
Subroutine _dumper: not called CC=1 SCAR=6.9
314 0 state $rc = require Data::Dumper;
315 0 Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(1)->Dump
316 }
317  
318 60 ▶sub _message ($m) {
Subroutine _message: called CC=1 SCAR=0.0
319 60 chomp $m;
320 60 say { _message_fh() } $m;
321 }
322  
323 60 ▶sub _message_fh () {
Subroutine _message_fh: called CC=2 SCAR=9.2
324 60 ▶ $App::bcrypt::fh // *STDOUT
Condition33%1/3
$App::bcrypt::fh // *STDOUT
l!l&&r!l&&!r
0600
MC/DC0%0/2
$App::bcrypt::fh // *STDOUT
$App::bcrypt::fh *STDOUT
Truth table33%1/3
A // B
A= $App::bcrypt::fhB= *STDOUT
ABresult
1X1
011
000
325 }
326  
327 20 ▶sub _modules () {
Subroutine _modules: called CC=1 SCAR=0.0
328 20 qw(Crypt::Bcrypt Crypt::URandom Encode);
329 }
330  
331 28 ▶sub _process_args ($args) {
Subroutine _process_args: called CC=2 SCAR=6.9
332 28 state $c = require Getopt::Long;
333  
334 28 my %opts = _defaults()->%*;
335 my %opts_description = (
336 'compare=s' => \ $opts{'compare'},
337 'cost|c=i' => \ $opts{'cost'},
338 'debug|d' => \ $opts{'debug'},
339 'eol|e=s' => \ $opts{'eol'},
340 'help|h' => \ $opts{'help'},
341 'no-eol|n' => \ $opts{'no-eol'},
342 'password|p=s' => \ $opts{'password'},
343 'quiet|q' => \ $opts{'quiet'},
344 'salt|s=s' => \ $opts{'salt'},
345 'type|t=s' => \ $opts{'type'},
346 'update-modules' => \ $opts{'update-modules'},
347 28 'version|v' => \ $opts{'version'},
348 );
349  
350 28 my $ret = Getopt::Long::GetOptionsFromArray( $args, %opts_description );
351 28 ▶ return unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 2 26 28
352  
353 26 return \%opts;
354 }
355  
356 0 ▶sub _read_password () {
Subroutine _read_password: not called CC=1 SCAR=6.9
357 0 _message( 'Reading password on standard input...' );
358 0 my $password = <STDIN>;
359 0 chomp $password;
360 0 $password;
361 }
362  
363 6 ▶sub _show_help () {
Subroutine _show_help: called CC=1 SCAR=0.0
364 6 state $rc = require Pod::Usage;
365 6 print Pod::Usage::pod2usage(
366 -verbose => 2,
367 -exitval => 'NOEXIT',
368 );
369 6 return EX_SUCCESS;
370 }
371  
372 20 ▶sub _show_version ($debug = 0) {
Subroutine _show_version: called CC=4 SCAR=13.9
373 20 _message( "$0 " . __PACKAGE__->VERSION );
374 20 ▶ return EX_SUCCESS unless $debug;
Branch: 1 branch
BranchTrueFalseTotal
unless $debug 10 10 20
375  
376 10 _message( "\tperl $^V at $^X" );
377  
378 10 my $width = ( sort { $a <=> $b } map { length } _modules() )[-1];
379  
380 10 foreach my $module ( sort { $a cmp $b} _modules() ) {
381 30 my $rc = eval "require $module";
382 30 my $rel_path = $module =~ s|::|/|gr . '.pm';
383 30 _message( sprintf "\t%-*s\n\t\t%f\n\t\t%s", $width, $module, $module->VERSION, $INC{$rel_path} )
384 }
385  
386 10 return EX_SUCCESS;
387 }
388  
389 0 ▶sub _types () { qw( 2a 2b 2x 2y ) }
Subroutine _types: not called CC=1 SCAR=6.9
390  
391 0 ▶sub _update_modules () {
Subroutine _update_modules: not called CC=1 SCAR=6.9
392 0 state $rc = require App::Cpan;
393 0 App::Cpan->run( _modules() )
394 }
395  
396 0 ▶sub _validate_args ( $processed_args ) {
Subroutine _validate_args: not called CC=6 SCAR=37.4
397 0 my( $c, $t, $s, $p ) = $processed_args->@{qw(cost type salt password)};
398  
399 0 my @errors;
400  
401 0 ▶ push @errors, qq(The cost must be a whole number between 5 and 31, inclusively, but got "$c")
Branch: 1 branch
BranchTrueFalseTotal
unless int $c == $c and (4 < $c and $c < 32) 0 0 0
Condition0%0/6
int $c == $c and (4 < $c and $c < 32)
!ll&&!rl&&r
000
4 < $c and $c < 32
!ll&&!rl&&r
000
MC/DC0%0/3
int $c == $c and (4 < $c and $c < 32)
int $c == $c 4 < $c $c < 32
Truth table0%0/4
A and B and C
A= int $c == $cB= 4 < $cC= $c < 32
ABCresult
0XX0
10X0
1100
1111
402 unless( int($c) == $c and ( 4 < $c && $c < 32 ) );
403 0 push @errors, qq(The type must be one of @{[ _types ]}, but got "$t")
404 0 ▶ unless grep { $t eq $_ } _types();
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$t eq $_;} _types() 0 0 0
405 0 ▶ push @errors, sprintf "The salt must be 16 octets, but got %s", _dumper($s) =~ s/\R+\z//r
Branch: 1 branch
BranchTrueFalseTotal
unless 16 == length $s 0 0 0
406 unless 16 == length $s;
407  
408 0 return \@errors;
409 }
410  
411 1;
↑ Summary /usr/local/bin/perldoc »