.htpasswd generator

Make .htpasswd lines for Apache or Nginx basic authentication: bcrypt ($2y$), Apache MD5 ($apr1$) or SHA-1 hashes, for one or many users.

loading .htpasswd generator
About this tool

What is a .htpasswd file?

A .htpasswd file is a plain text list of users and password hashes. Each line is user:hash. Apache and Nginx read it to protect a site, a folder or an admin panel with HTTP basic authentication: the browser shows a login box, and the server checks what you type against the file. The tool htpasswd from Apache creates these lines. This page makes the same lines in your browser.

The password is never stored in the file. Only a hash is. This page offers three kinds. bcrypt ($2y$) is slow on purpose and has a random salt, so it is the right choice today. Apache MD5 ($apr1$) is an older salted format that every Apache and Nginx build reads. SHA-1 ({SHA}) is a bare hash with no salt. It is fast to attack, and it exists here only for old servers that cannot read the other two.

You can type many users at once, one user:password per line, up to 200. The page also checks a password against an existing .htpasswd file, which helps when you inherit a server and do not know whose password is whose. For a ready-made Authorization header instead of a file, use the basic auth generator. To hash a single password for a Linux account, see the Unix password hash page.

Everything runs in your browser. The passwords are never sent anywhere. Basic authentication itself is weak by design: the browser sends the password with every request, so use it only over HTTPS.

How to make a .htpasswd file

  1. In Users and passwords, one "user:password" per line: type one line per user, for example alice:password. A password may contain colons. A user name may not.
  2. In Hash type: choose bcrypt, Apache MD5 or SHA-1. Keep bcrypt unless you know the server needs something else.
  3. For bcrypt, set Cost (2^n):. Each step doubles the work. The default of 10 is a good balance. A higher cost slows your server's login too.
  4. Read the result under Content of the .htpasswd file:. Each time you click Generate again the lines change, because the salt is new. All of them are valid.
  5. Click Download .htpasswd or copy the text. Save it outside the web root, for example /etc/nginx/.htpasswd.
  6. Copy the snippet shown for Nginx (in the server or location block): or Apache (.htaccess or the Directory block): and point it to your file.
  7. To test, use Check a password against a .htpasswd file: paste the file, fill in User: and Password:, and read whether the password is right and which hash type the line uses.

Examples

Lines written by Apache's htpasswd

These three lines were produced by the real htpasswd tool. The user alice has the password pässword (bcrypt, cost 6), bob has secret as SHA-1, and carol has secret as Apache MD5:

alice:$2y$06$1UEUXFZYQushwNt3wUSU9uq7itJruSFYM4rAWOHVlgXLSz5zv4lGq
bob:{SHA}5en6G6MezRroT3XKqkdPOmY/BfQ=
carol:$apr1$rSIomVZo$XHfzfWrw6DhvB5G5tyjfx.

Paste them into the checker with the right user and password and it says they match. A wrong password for carol says it does not.

Why the same password gives different lines

Make a bcrypt line for alice:s3cret twice. You get two different hashes, and both accept s3cret. The salt is random and is stored inside the hash. SHA-1 is the exception: secret always gives {SHA}5en6G6MezRroT3XKqkdPOmY/BfQ=, which is exactly why it is weak. Anyone can precompute it for a list of common passwords.

Turning it on in the server

For Nginx, add this to the server or location block and reload:

auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

For Apache, put this in .htaccess or in the Directory block:

AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

Common errors and how to fix them

  • "Line {line} has no colon. Write it as "user:password".": the line has no :. Add the user name, a colon and the password.
  • "A user name is empty. Each line is "user:password".": the line starts with a colon. Type a name before it.
  • "A user name cannot have a colon (":"), it separates the name from the hash.": remove the colon from the name. Colons are fine inside the password.
  • "A user name cannot have line breaks.": put each user on its own line.
  • "Type at least one line in the form "user:password".": the box is empty or holds only blank lines.
  • "At most {max} users at a time.": split the list in batches of 200 or fewer.
  • "The file has no line for the user "{user}".": the checker did not find that name. Check the spelling and case.
  • "The line for this user is not a hash of a known type." or "The hash of this user is damaged or incomplete.": the line was cut when copied, or it uses a type this page does not read.
  • The server still refuses a correct password: check the file path and the permissions, and that the web server user can read the file.

Frequently asked questions

Which hash should I choose for a .htpasswd file?

Choose bcrypt. It is salted and slow, which makes guessing expensive. Apache has read it since 2.4 and Nginx reads it through the system crypt library on most builds. If your server rejects it, try Apache MD5 next.

Is the .htpasswd file safe to leave in the web root?

No. Keep it outside the folders the server publishes, or deny access to it. Even hashed, the file lets an attacker try passwords offline. A strong password still matters: test yours with the password strength analyser.

Are my passwords sent to a server?

No. The hashes are computed by code that runs in your browser tab. Still, avoid pasting real production passwords into any web page when you can use the command line on a trusted machine.

Can basic authentication replace a real login system?

Only for small jobs: a staging site, a private tool or a quick lock on an admin path. It has no logout, no lockout and no second factor. For public applications use a proper login. You can also read about bcrypt to see how its cost works.

The same thing on the command line

Bash

# bcrypt, cost 10, create the file
htpasswd -nbB -C 10 alice 's3cret' > .htpasswd

# Apache MD5 and add to an existing file
htpasswd -nbm bob 's3cret' >> .htpasswd

# apr1 with openssl
openssl passwd -apr1 's3cret'

Python

# pip install bcrypt
import bcrypt

hash_ = bcrypt.hashpw(b"s3cret", bcrypt.gensalt(rounds=10, prefix=b"2b"))
# Apache reads the $2y$ prefix; $2b$ and $2y$ are equivalent
print("alice:" + hash_.decode().replace("$2b$", "$2y$", 1))
Report a problem