htpasswd snippets

Generate a 56-bit DES encrypted (htpasswd) password with Ruby

Tagged ruby, irb, htpasswd, nginx, apache  Languages css

Run the following in an irb console to generate a 56-bit DES encrypted password:


The password can be used in an Apache or Nginx htpasswd file to enable basic authentication.

The generated password can also be used in other Unix password files.

Password protecting a folder/resource with Nginx

Tagged htpasswd, perl, ruby, nginx, auth_basic  Languages bash

First add the following to your Nginx configuration file:

location / {
  auth_basic            "Restricted";
  auth_basic_user_file  /etc/nginx/htpasswd;

Then create the htpasswd file:

# this be passwords

To generate a htpasswd password without installing Apache you can use the following Perl or Ruby code:


perl -le 'print crypt("password", "salt")'

Ruby (run in irb)


The crypt() method uses 56-bit DES encryption, which is used in /etc/passwd and htpasswd.