3 # cryptpasswd Generate or check md5 and DES hashed passwords.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 # Copyright (C) 2001 The FreeRADIUS Project http://www.freeradius.org
21 # Written by Miquel van Smoorenburg <miquels@cistron-office.nl>
29 return (crypt("fnord", "aa") =~ m/^aa/);
33 return (crypt("fnord", "\$1\$aa") =~ m/^\$1\$/);
37 die "Usage: cryptpasswd [--des|--md5|--check] plaintext_password [crypted_password]\n";
40 @saltc = ( '.', '/', '0'..'9', 'A'..'Z', 'a'..'z' );
47 Getopt::Long::Configure("no_ignore_case", "bundling");
48 my @options = ( "des|d+", "md5|m+", "check|c+" );
49 usage() unless (eval { Getopt::Long::GetOptions(@options) } );
52 usage unless ($#ARGV == 1);
53 if (crypt($ARGV[0], $ARGV[1]) ne $ARGV[1]) {
54 print "Password BAD\n";
57 print "Password OK\n";
62 usage() unless ($opt_des || $opt_md5);
63 usage() unless ($#ARGV == 0);
65 die "DES password hashing not available\n"
66 if ($opt_des && !check_des());
67 die "MD5 password hashing not available\n"
68 if ($opt_md5 && !check_md5());
70 $salt = ($opt_md5 ? '$1$' : '');
71 for ($i = 0; $i < ($opt_md5 ? 8 : 2); $i++) {
72 $salt .= $saltc[rand 64];
74 $salt .= '$' if ($opt_md5);
75 print crypt($ARGV[0], $salt), "\n";