htpasswd Replacement: Perl Script To Create Password Using crypt()

Webserver such as Nginx / Lighttpd accepets passwords using encoded method i.e. function crypt(3) is needs. You can create the password file with the htpasswd program from Apache. Sometime Apache is not installed use the following perl script to create password.

How do I use crypt.pl?

Download script

Set permissions:
chmod +x crypt.pl
./crypt.pl mySecrete

Sample Output:

$1$mySecret$TQgDKW6xpDmEUyxm5ZDxv/

Now you can create htpasswd file for lighttpd or nginx:

user:$1$mySecret$TQgDKW6xpDmEUyxm5ZDxv/

Perl script to replace htpasswd

#!/usr/bin/perl
use strict;
 
my $passWord=$ARGV[0];
print crypt($passWord,$passWord)."\n";
Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Fleshgrinder Mar 28, 2012 @ 19:36

    Thanks, this article and the anon comment inspired me to take it a step further:

    #!/usr/bin/perl
    use strict;
    
    chomp(my $filename=$ARGV[0]);
    chomp(my $username=$ARGV[1]);
    chomp(my $password=$ARGV[2]);
    
    if (!$filename || !$username || !$password) {
      print "USAGE: ./crypt.pl filename username password\n\n";
    } else {
      open FILE, ">>", $filename or die $!;
      print FILE $username . ":" . crypt($password, $username) . "\n";
      close FILE or die $!;
    }

    This is now super easy to use and it will always append the record to the file. Enjoy!

  • Fleshgrinder Mar 28, 2012 @ 19:32

    Thanks, this article and the anon comment inspired me to take it a step further:

    #!/usr/bin/perl
    use strict;
    
    chomp(my $filename=$ARGV[0]);
    chomp(my $username=$ARGV[1]);
    chomp(my $password=$ARGV[2]);
    
    if ($filename == '' || $username == '' || $password == '') {
      print "USAGE: ./crypt.pl filename username password\n\n";
    } else {
      open FILE, ">>", $filename or die $!;
      print FILE $username . ":" . crypt($password, $username) . "\n";
      close FILE or die $!;
    }
    

    This is now super easy to use and it will always append the record to the file. Enjoy!

  • anonymous Sep 7, 2010 @ 15:22

    I use this script:

    #!/usr/bin/perl
    use strict;
    chomp(my $username=$ARGV[0]);
    chomp(my $password=$ARGV[1]);
    print $username.":".crypt($password,$username)."\n";

    In this script $password – main string for password generation, and $username – salt.
    You can use it like that:

    ./htpasswd.pl user password > .htpasswd
    cat .htpasswd
    user:usjRS48E8ZADM

  • Hans-Gunther Schmidt May 27, 2009 @ 16:07

    Thank you very much for this one. Worked perfectly.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.