Authen-Passphrase

 view release on metacpan or  search on metacpan

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

				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]{10}[.26AEIMQUYcgkosw]\z#
				or croak "\"$value\" is not a valid ".
					"encoded hash";
			$self->{hash} = base64_to_block($value);
		} elsif($attr eq "passphrase") {
			croak "passphrase specified redundantly"
				if exists($self->{hash}) ||
					defined($passphrase);
			$passphrase = $value;
		} else {
			croak "unrecognised attribute `$attr'";
		}
	}
	$self->{fold} = !!0 unless exists $self->{fold};
	$self->{initial} = "\0\0\0\0\0\0\0\0" unless exists $self->{initial};
	$self->{nrounds} = 25 unless exists $self->{nrounds};
	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::DESCrypt->from_crypt(PASSWD)

Generates a new passphrase recogniser object using the DES-based crypt()
algorithm, from a crypt string.  Two forms of crypt string are supported.

The first form of crypt string must consist of 13 base 64 digits.
The first two give the salt, and the next eleven give the hash.
Long passphrases are not folded, the initial block is all bits zero,
and 25 encryption rounds are performed.

The second form of crypt string must consist of an "B<_>" followed
by 19 base 64 digits.  The first four give the number of encryption
rounds, the next four give the salt, and the next eleven give the hash.
Long passphrases are folded, and the initial block is all bits zero.

=cut

sub from_crypt {
	my($class, $passwd) = @_;
	if($passwd =~ /\A[^\$].{12}\z/s) {
		$passwd =~ m#\A([./0-9A-Za-z]{2})([./0-9A-Za-z]{11})\z#
			or croak "malformed DES crypt data";
		my($salt, $hash) = ($1, $2);
		return $class->new(salt_base64 => $salt, hash_base64 => $hash);
	} elsif($passwd =~ /\A_.{19}\z/s) {
		$passwd =~ m#\A_([./0-9A-Za-z]{4})([./0-9A-Za-z]{4})
				([./0-9A-Za-z]{11})\z#x
			or croak "malformed _ data";
		my($nr, $salt, $hash) = ($1, $2, $3);
		return $class->new(fold => 1, nrounds_base64 => $nr,
				   salt_base64 => $salt, hash_base64 => $hash);
	}
	return $class->SUPER::from_crypt($passwd);
}

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

Generates a new passphrase recogniser object using the DES-based
crypt() algorithm, 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->fold

Returns a truth value indicating whether passphrase folding is used.

=cut

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

=item $ppr->initial

Returns the initial block, as a string of eight bytes.

=cut

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

=item $ppr->initial_base64

Returns the initial block, as a string of eleven base 64 digits.

=cut

sub initial_base64 {
	my($self) = @_;
	return block_to_base64($self->{initial});
}

=item $ppr->nrounds

Returns the number of encryption rounds, as a Perl integer.

=cut

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

=item $ppr->nrounds_base64_4



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