# cp5 crypt functions

# create an encrypted password
sub mkpasswd {
 $what = $_[0];
 $salt = chr(65+rand(27)).chr(65+rand(27));
 $salt =~ s/\W/x/g;
 return crypt($what, $salt);
}

# check an encrypted password with a non-encrypted one
sub ckpasswd {
 ($plain, $encrypted) = @_;
 if (!$encrypted) {
  ($plain, $encrypted) = split(/\s+/, $plain, 2);
 }
 return '' unless ($plain && $encrypted);
 if ($encrypted =~ /^\$\d\$(\w\w)\$/) {
  $salt = $1;
 }
 else {
  $salt = substr($encrypted, 0, 2);
 }
 return ($encrypted eq crypt($plain, $salt));
}

1;
