Skip to content

crypt(1) for the command line

Looks like there is no passwd-compatible crypt(1) for the command line. htpasswd, unfortunately, uses a different algorithm.

This short perl script might be a replacement:

#!/usr/bin/perl -w

use strict;

while(<>) {
    my $seed = `apg -a 1 -m 8`;
    print crypt(&#8220;$_&#8221;, &#8220;\\$1\\$$seed&#8221;). &#8220;\\n&#8221;;

Or do we have something better already in the distribution?

Update: looks like mkpasswd (from the whois package, whatever makes it belong in there) does the job quite nicely, but the script shown above takes care of automatic salt creation as well. Any ideas how to do that more elegantly, without requireing apg?


No Trackbacks


Display comments as Linear | Threaded

Nikita Youshchenko on :

apt-get install makepasswd man makepasswd makepasswd --crypt

Marc 'Zugschlus' Haber on :

That one insists on first creating a password, and then crypting it. I'd like to be able to crypt my own passwords.

glandium on :

errrrr... htpasswd does use crypt(3)... just look at the source

Marc 'Zugschlus' Haber on :

htpasswd uses either DES-based crypt, which is nowadays considered insecure, or apache-modified MD5, which is not passwd-compatible. I have not yet found a way to coax htpasswd into crypting in passwd-compatible MD5.

glandium on :

since when perl's crypt function does MD5 ?

glandium on :

oooooh GNU extension... man crypt(3)

Guido Trotter on :

mkpasswd is a crypt(1) for shell...

ultrotter@tie:~$ mkpasswd Password: dhLZQG46Xkh22 ultrotter@tie:~$ mkpasswd foo ordF1SZQML4fg ultrotter@tie:~$ mkpasswd -Hmd5 foo $1$aW1n20AJ$wZ3Uy/7vGQ3EkfWmQP5ou/

Strangely it seems to be included in the 'whois' package!

ultrotter@tie:~$ dpkg -S which mkpasswd whois: /usr/bin/mkpasswd

Arthur on :

I maintain the cvsd package which has a perl script that adds passwords to the repository. I use this:

sub cryptpasswd { my ($passwd) = @_; return crypt($passwd,join('',('.','/',0..9,'A'..'Z','a'..'z')[rand 64,rand 64])); }

Jacobo on :

Eeeeh, if you don't provide a salt to mkpasswd, it invents its own one.

$ mkpasswd -H md5 Password: $1$vyvIEQ/1$XPhpF5GY3kduYL10HoCS.. $ mkpasswd -H md5 Password: $1$wp5sbI/L$37lDLeMvZdR97Um2G8A4I0

Loïc Lefort on :

You can also use openssl:

$ openssl passwd Password: Verifying - Password: sWkTkGhy6oJy6

$ openssl passwd -1 Password: Verifying - Password: $1$x3u/FM/5$fyz246V2pgbrNGnVpNEJY0

Dirk Deimeke on :

I am using


srand (time()); my $randletter = "(int (rand(26)) + (int (rand(1) + .5) % 2 ? 65 : 97))"; my $salt = sprintf("%c%c", eval $randletter, eval $randletter); my $plaintext = shift; my $crypttext = crypt($plaintext, $salt); print "${crypttext}\n";

Maybe, it helps.

Add Comment

Markdown format allowed
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
Form options