Question

I'm looking for a solution in PHP Similar to the output of radcrypt.

I've looked into mcrypt and can't seem to figure out which cipher and setting i should use.

Mcrypt wants a salt to add to the encryption but radcrypt doesn't seem to have a salt.

Anyone knows what the cipher and/or settings of radcrypt are?

Was it helpful?

Solution

You don't need mcrypt for this. These passwords as hashed, not encrypted.

The radcrypt script is built from this bit of Perl. It relies on Perl's built-in crypt function, which has semantics similar to the standard crypt function.

PHP also provides crypt. Until 5.3, PHP used the same exact functionality, called in the same exact way, generating the same hashes. Starting in 5.3, PHP's implementation became bundled with the language instead of relying on external libraries, allowing for additions and enhancements.

The radcrypt script automatically produces a salt, using two characters for DES and eight for MD5:

$salt = ($opt_md5 ? '$1$' : '');
for ($i = 0; $i < ($opt_md5 ? 8 : 2); $i++) {
    $salt .= $saltc[rand 64];
}
$salt .= '$' if ($opt_md5);
print crypt($ARGV[0], $salt), "\n";

DES crypt stores the hash as the first two characters. For example, crypt('password', 'aa') produces aajfMKNH1hTm2.

MD5 crypt stores the hash as part of a $ delimited section. For example, crypt('password', '$1$iamasalt$') produces $1$iamasalt$RSUhkOk5NVnvbM3BPw/g8/.

Knowing this, you can now correctly create the expected password format using PHP, or any other language that supplies standard crypt. crypt also knows how to extract the salt from the hashed string, so you can pass the already hashed password right back in order to verify the password:

$hash = '$1$iamasalt$RSUhkOk5NVnvbM3BPw/g8/';
$password = 'password';
if($hash == crypt($password, $hash)) {
    print "Passwords matched.\n";
}

Incidentally, that code is valid in both Perl and PHP.

Please be sure to read the Wikipedia article above, especially the sections concerning password security. Neither the DES nor MD5 hashes are secure against brute-force attacks. You should not use either of these methods unless you are required to by something that only supports these two hash types (like, I dunno, FreeRADIUS).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top