#! /usr/bin/perl -w # Example program to generate MD5 hashed passwords suitable for use in # /etc/shadow on a Linux system. # You could pass the output of this function to useradd -p xxxxx, # where xxxxx is the output of hash_md5_password(). # Based on /usr/share/doc/samba-2.2.1a/examples/LDAP/ldapsync.pl, # distributed with samba. # A portable alternative is the module Crypt::PasswdMD5, available # through the cpan program. use strict; sub hash_md5_password($) { my $clear_text_password = shift; my $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64]; $salt = '$1$'.$salt.'$'; my $hashed_password = crypt( $clear_text_password, $salt ); return $hashed_password; } # This code is a stub just to test the function: our $clear_password = shift; our $hashed_password = hash_md5_password( $clear_password ); print "MD5 Hash of '$clear_password' is '$hashed_password'\n";