Crypt-Eksblowfish

 view release on metacpan or  search on metacpan

lib/Crypt/Eksblowfish/Bcrypt.pm  view on Meta::CPAN

The number of operations is proportional to 2^cost.

=item B<salt>

Exactly sixteen octets of salt.

=back

=cut

sub bcrypt_hash($$) {
	my($settings, $password) = @_;
	$password .= "\0" if $settings->{key_nul} || $password eq "";
	my $cipher = Crypt::Eksblowfish->new($settings->{cost},
			$settings->{salt}, substr($password, 0, 72));
	my $hash = join("", map {
				my $blk = $_;
				for(my $i = 64; $i--; ) {
					$blk = $cipher->encrypt($blk);
				}
				$blk;

lib/Crypt/Eksblowfish/Bcrypt.pm  view on Meta::CPAN

	return $hash;
}

=item en_base64(BYTES)

Encodes the octet string textually using the form of base 64 that is
conventionally used with bcrypt.

=cut

sub en_base64($) {
	my($octets) = @_;
	my $text = encode_base64($octets, "");
	$text =~ tr#A-Za-z0-9+/=#./A-Za-z0-9#d;
	return $text;
}

=item de_base64(TEXT)

Decodes an octet string that was textually encoded using the form of
base 64 that is conventionally used with bcrypt.

=cut

sub de_base64($) {
	my($text) = @_;
	croak "bad base64 encoding"
		unless $text =~ m#\A(?>(?:[./A-Za-z0-9]{4})*)
				  (?:|[./A-Za-z0-9]{2}[.CGKOSWaeimquy26]|
				      [./A-Za-z0-9][.Oeu])\z#x;
	$text =~ tr#./A-Za-z0-9#A-Za-z0-9+/#;
	$text .= "=" x (3 - (length($text) + 3) % 4);
	return decode_base64($text);
}

lib/Crypt/Eksblowfish/Bcrypt.pm  view on Meta::CPAN


The PASSWORD is hashed according to the SETTINGS.  The value returned
is a string which encodes the algorithm parameters and the hash: the
parameters are in the same format required in SETTINGS, and the hash is
appended in the form of 31 base 64 digits.  This result is suitable to
be used as a SETTINGS string for input to this function: the hash part
of the string is ignored on input.

=cut

sub bcrypt($$) {
	my($password, $settings) = @_;
	croak "bad bcrypt settings"
		unless $settings =~ m#\A\$2(a?)\$([0-9]{2})\$
					([./A-Za-z0-9]{22})#x;
	my($key_nul, $cost, $salt_base64) = ($1, $2, $3);
	my $hash = bcrypt_hash({
			key_nul => $key_nul,
			cost => $cost,
			salt => de_base64($salt_base64),
		   }, $password);



( run in 0.266 second using v1.01-cache-2.11-cpan-1f129e94a17 )