Authen-Passphrase

 view release on metacpan or  search on metacpan

lib/Authen/Passphrase/PHPass.pm  view on Meta::CPAN

		} elsif($attr eq "salt") {
			croak "salt specified redundantly"
				if exists $self->{salt};
			$value =~ m#\A[\x00-\xff]{8}\z#
				or croak "\"$value\" is not a valid salt";
			$self->{salt} = "$value";
		} elsif($attr eq "salt_random") {
			croak "salt specified redundantly"
				if exists $self->{salt};
			$self->{salt} = _en_base64(random_bytes(6));
		} elsif($attr eq "hash") {
			croak "hash specified redundantly"
				if exists($self->{hash}) ||
					defined($passphrase);
			$value =~ m#\A[\x00-\xff]{16}\z#
				or croak "not a valid raw hash";
			$self->{hash} = "$value";
		} elsif($attr eq "hash_base64") {
			croak "hash specified redundantly"
				if exists($self->{hash}) ||
					defined($passphrase);
			$value =~ m#\A[./0-9A-Za-z]{21}[./01]\z#
				or croak "\"$value\" is not a valid hash";
			$self->{hash} = _de_base64($value);
		} elsif($attr eq "passphrase") {
			croak "passphrase specified redundantly"
				if exists($self->{hash}) ||
					defined($passphrase);
			$passphrase = $value;
		} else {
			croak "unrecognised attribute `$attr'";
		}
	}
	croak "cost not specified" unless exists $self->{cost};
	croak "salt not specified" unless exists $self->{salt};
	$self->{hash} = $self->_hash_of($passphrase) if defined $passphrase;
	croak "hash not specified" unless exists $self->{hash};
	return $self;
}

=item Authen::Passphrase::PHPass->from_crypt(PASSWD)

Generates a new phpass passphrase recogniser object from a crypt string.
The crypt string must consist of "B<$P$>", one base 64 character encoding
the cost, the salt, then 22 base 64 digits giving the hash.  The salt
must be exactly 8 characters long, and cannot contain any character that
cannot appear in a crypt string.

=cut

sub from_crypt {
	my($class, $passwd) = @_;
	if($passwd =~ /\A\$P\$/) {
		$passwd =~ m#\A\$P\$([./0-9A-Za-z])([!-9;-~]{8})
				([./0-9A-Za-z]{22})\z#x
			or croak "malformed \$P\$ data";
		my($cost, $salt, $hash) = ($1, $2, $3);
		return $class->new(cost_base64 => $cost, salt => $salt,
			hash_base64 => $hash);
	}
	return $class->SUPER::from_crypt($passwd);
}

=item Authen::Passphrase::PHPass->from_rfc2307(USERPASSWORD)

Generates a new phpass passphrase recogniser object from an RFC 2307
string.  The string must consist of "B<{CRYPT}>" (case insensitive)
followed by an acceptable crypt string.

=back

=head1 METHODS

=over

=item $ppr->cost

Returns the base-two logarithm of the number of hashing rounds that will
be performed.

=cut

sub cost {
	my($self) = @_;
	return $self->{cost};
}

=item $ppr->cost_base64

Returns the base-two logarithm of the number of hashing rounds that will
be performed, expressed as a single base 64 digit.

=cut

sub cost_base64 {
	my($self) = @_;
	return substr($base64_digits, $self->{cost}, 1);
}

=item $ppr->nrounds_log2

Synonym for L</cost>.

=cut

*nrounds_log2 = \&cost;

=item $ppr->nrounds_log2_base64

Synonym for L</cost_base64>.

=cut

*nrounds_log2_base64 = \&cost_base64;

=item $ppr->salt

Returns the salt, as a string of eight bytes.

=cut



( run in 1.569 second using v1.01-cache-2.11-cpan-364913b4093 )