App-bcrypt

 view release on metacpan or  search on metacpan

Makefile.PL  view on Meta::CPAN

a module.

To build the distribution, run this file normally:

	% perl Makefile.PL

But, it's more interesting than that. You can load it with C<require>
and call C<arguments> to get the data structure it passes to
C<WriteMakefile>:

	my $package = require '/path/to/Makefile.PL';
	my $arguments = $package->arguments;

Note that C<require>-ing a file makes an entry in C<%INC> for exactly
that name. If you try to C<require> another file with the same name,
even from a different path, C<require> thinks it has already loaded
the file. As such, I recommend you always require the full path to the
file.

The return value of the C<require> is a package name (in this case,
the name of the main module. Use that to call the C<arguments> method.

script/bcrypt  view on Meta::CPAN

			_message( $_ );
			}
		return EX_USAGE;
		}

	if( ! defined $processed_args->{'password'} ) {
		$processed_args->{'password'} = _read_password();
		}

	if( defined $processed_args->{compare} ) {
		state $rc = require Crypt::Bcrypt;
		my $r = Crypt::Bcrypt::bcrypt_check( $processed_args->@{qw(password compare)} );

		return do {
			if( $r ) {
				_message("Match");
				EX_SUCCESS;
				}
			else {
				_message("Does not match");
				EX_NO_MATCH;
				}
			};
		}

	if( $processed_args->{'debug'} ) {
		say _dumper($processed_args);
		}

	state $rc = require Crypt::Bcrypt;
	my $hashed = Crypt::Bcrypt::bcrypt( $processed_args->@{qw(password type cost salt)} );

	local $\ = do {
		if( $processed_args->{'no-eol'} ) { undef }
		elsif( $processed_args->{'eol'} ) { $processed_args->{'eol'} }
		else                              { "\n" }
		};

	print $hashed;

script/bcrypt  view on Meta::CPAN

		salt      => _default_salt(),
		type      => lc( $ENV{BCRYPT_TYPE} // '2b' ),
		);

	return \%hash;
	}

sub _default_salt () {
	my $unencoded_salt = do {
		if( exists $ENV{BCRYPT_SALT} ) {
			state $rc = require Encode;
			Encode::encode( 'UTF-8', $ENV{BCRYPT_SALT} )
			}
		else {
			state $rc = require Crypt::URandom;
			Crypt::URandom::urandom(16)
			}
		};
	}

sub _dumper {
	state $rc = require Data::Dumper;
	Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(1)->Dump
	}

sub _message ($m) {
	chomp $m;
	say { _message_fh() } $m;
	}

sub _message_fh () {
	$App::bcrypt::fh // *STDOUT
	}

sub _modules () {
	qw(Crypt::Bcrypt Crypt::URandom Encode);
	}

sub _process_args ($args) {
	state $c = require Getopt::Long;

	my %opts = _defaults()->%*;
	my %opts_description = (
		'compare=s'      => \ $opts{'compare'},
		'cost|c=i'       => \ $opts{'cost'},
		'debug|d'        => \ $opts{'debug'},
		'eol|e=s'        => \ $opts{'eol'},
		'help|h'         => \ $opts{'help'},
		'no-eol|n'       => \ $opts{'no-eol'},
		'password|p=s'   => \ $opts{'password'},

script/bcrypt  view on Meta::CPAN

	}

sub _read_password () {
	_message( 'Reading password on standard input...' );
	my $password = <STDIN>;
	chomp $password;
	$password;
	}

sub _show_help () {
	state $rc = require Pod::Usage;
	print Pod::Usage::pod2usage(
		-verbose => 2,
		-exitval => 'NOEXIT',
		);
	return EX_SUCCESS;
	}

sub _show_version ($debug = 0) {
	_message( "$0 " . __PACKAGE__->VERSION );
	return EX_SUCCESS unless $debug;

script/bcrypt  view on Meta::CPAN

		my $rel_path = $module =~ s|::|/|gr . '.pm';
		_message( sprintf "\t%-*s\n\t\t%f\n\t\t%s", $width, $module, $module->VERSION, $INC{$rel_path} )
		}

	return EX_SUCCESS;
	}

sub _types () { qw( 2a 2b 2x 2y ) }

sub _update_modules () {
	state $rc = require App::Cpan;
	App::Cpan->run( _modules() )
	}

sub _validate_args ( $processed_args ) {
	my( $c, $t, $s, $p ) = $processed_args->@{qw(cost type salt password)};

	my @errors;

	push @errors, qq(The cost must be a whole number between 5 and 31, inclusively, but got "$c")
		unless( int($c) == $c and ( 4 < $c && $c < 32 ) );

t/lib/common.pl  view on Meta::CPAN


subtest 'sanity' => sub {
	ok( -e $program, "$program exists" );
	SKIP: {
		skip "Windows doesn't think about 'executable'", 1 if $^O eq 'MSWin32';
		ok( -x $program, "$program is executable" );
		}
	};

sub dumper {
	state $rc = require Data::Dumper;
	Data::Dumper->new([@_])->Indent(1)->Sortkeys(1)->Terse(1)->Useqq(1)->Dump
	}

sub run_command ( %hash ) {
	state $rc = require IPC::Open3;
	state $rc2 = require Symbol;

	my @command = ( $^X, $program, exists $hash{args} ? $hash{args}->@* : () );

	my $pid = IPC::Open3::open3(
		my $input_fh,
		my $output_fh,
		my $error_fh = Symbol::gensym(),
		@command
		);



( run in 0.287 second using v1.01-cache-2.11-cpan-0d8aa00de5b )