How to use unix_md5_crypt from Perl in Python?

Nico Grubert nicogrubert at gmail.com
Thu Mar 22 04:54:32 EDT 2007


Dear list members,

I have a Perl script which crypts a password using a clearteaxt password 
and a username.

Example:
   username = 'root at localhost'
   password = 'root'

The crypted password is: 'roK20XGbWEsSM'

The crypted password always starts with the first 2 characters of the 
username (here: 'ro' from 'root at localhost').

The Perl script contains this code and I am wondering how I can do this 
in Python:
#################################################################################
my $CryptedPw = '';
# md5 pw
if ($Self->{ConfigObject}->Get('AuthModule::DB::CryptType') &&
     $Self->{ConfigObject}->Get('AuthModule::DB::CryptType') eq 'md5') {
     $CryptedPw = unix_md5_crypt($Pw, $Param{UserLogin});
}
# crypt pw
else {
     # crypt given pw (unfortunately there is a mod_perl2 bug
     # on RH8 - check if
     # crypt() is working correctly) :-/
     if (crypt('root', 'root at localhost') eq 'roK20XGbWEsSM') {
         $CryptedPw = crypt($Pw, $Param{UserLogin});
     }
     else {
         $Self->{LogObject}->Log(
             Priority => 'notice',
             Message => "The crypt() of your mod_perl(2) is not working 
correctly! Update mod_perl!",
         );
         my $TempUser = quotemeta($Param{UserLogin});
         my $TempPw = quotemeta($Pw);
         my $CMD = "perl -e \"print crypt('$TempPw', '$TempUser');\"";
         open (IO, " $CMD | ") || print STDERR "Can't open $CMD: $!";
         while (<IO>) {
             $CryptedPw .= $_;
         }
         close (IO);
         chomp $CryptedPw;
     }
}
#################################################################################


What do I have to do in Python to create the crypted password 
'roK20XGbWEsSM' by using 'root at localhost' for username and 'root' for 
password?

I tried:
 >>> username = 'root at localhost'
 >>> password = 'root'
 >>> import crypt
 >>> crypt.crypt(username, password)
'roowueH.vq6VM'
 >>>

This creates 'roowueH.vq6VM' but not 'roK20XGbWEsSM'. :-(

Thanks in advance,
Nico



More information about the Python-list mailing list