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\$/);
40 die "Usage: $name [--des|--md5|--check] plaintext_password [crypted_password]\n";
43 @saltc = ( '.', '/', '0'..'9', 'A'..'Z', 'a'..'z' );
50 Getopt::Long::Configure("no_ignore_case", "bundling");
51 my @options = ( "des|d+", "md5|m+", "check|c+" );
52 usage() unless (eval { Getopt::Long::GetOptions(@options) } );
55 usage unless ($#ARGV == 1);
56 if (crypt($ARGV[0], $ARGV[1]) ne $ARGV[1]) {
57 print "Password BAD\n";
60 print "Password OK\n";
65 $opt_des = 1 unless ($opt_des || $opt_md5);
66 usage() unless ($#ARGV == 0);
68 die "DES password hashing not available\n"
69 if ($opt_des && !check_des());
70 die "MD5 password hashing not available\n"
71 if ($opt_md5 && !check_md5());
73 $salt = ($opt_md5 ? '$1$' : '');
74 for ($i = 0; $i < ($opt_md5 ? 8 : 2); $i++) {
75 $salt .= $saltc[rand 64];
77 $salt .= '$' if ($opt_md5);
78 print crypt($ARGV[0], $salt), "\n";