简体   繁体   中英

How to generate .htpasswd under PHP

Im trying to generate long htpasswd file with a lot of users (100+).

My DirectAdmin panel on linux server generates htpasswd file with password hashes starting with $1$.

I tried something like this:

function random_salt($length = 9) // 9 characters and $1$ = 12 characters
{      
    $chars = 'bcdfghjklmnprstvwxzaeiou';

    for ($p = 0; $p < $length; $p++)
    {
        $result .= ($p%2) ? $chars[mt_rand(19, 23)] : $chars[mt_rand(0, 18)];
    }

    return $result;
}

echo crypt($myplainpassword, "$1$".random_salt());

It produces hash which starts with $1$, but server doesnt let me in. My passwords are 4-digit random "pin codes" generated in excel. Examples:

1215 5325 6261

What im doing wrong?

This how I generate the .htpasswd passwords...

$new_password = password_hash($old_password, PASSWORD_BCRYPT);

password_hash() creates a new password hash using a strong one-way hashing algorithm. password_hash() is compatible with crypt(). Therefore, password hashes created by crypt() can be used with password_hash().

The following algorithms are currently supported:

PASSWORD_BCRYPT - Use the CRYPT_BLOWFISH algorithm to create the hash. This will produce a standard crypt() compatible hash using the "$2y$" identifier. The result will always be a 60 character string, or FALSE on failure. Supported Options:

http://php.net/manual/en/function.password-hash.php

I would recommend looking into the exact way how the hashes are generated. If you create a hash using your method, does it look the same as the one generated by DirectAdmin?

In general, I have previously used this to generate the entries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM