3 use Getopt::Long qw(:config no_ignore_case);
7 use Math::Random::Secure qw(irand);
11 qw[ help|?! man! f|format=s l|len=i s|salt=s S|Salt=s z|saltlen:i ]
13 pod2usage(1) if $opts{help};
14 pod2usage(-verbose => 2) if $opts{man};
17 if (exists $opts{l}) {
18 my @length = (224, 256, 384, 512);
19 if (grep {$_ eq $opts{l}} @length) {
28 return encode_base64(shift, '')."\n";
32 return unpack('H*', shift)."\n";
39 my $fmt = \&{'fmt_base64'};
40 if (exists $opts{f}) {
41 my %format = ('m' => \&{'fmt_base64'}, 'base64' => \&{'fmt_base64'},
42 'x' => \&{'fmt_hex'}, 'hex' => \&{'fmt_hex'},
43 'b' => \&{'fmt_bin'}, 'bin' => \&{'fmt_bin'});
44 $fmt = $format{$opts{f}};
51 my $password = $ARGV[0];
52 if (!defined $password) {
53 print "Missing password\n";
58 if (exists $opts{S}) {
60 print "Redundant salt\n";
63 $salt = pack('H*', $opts{S});
64 } elsif (!defined $salt and exists $opts{z}) {
69 print "Bad salt length\n";
73 $salt .= pack('N', irand());
76 $salt .= substr(pack('N', irand()), 1, $ssiz) if ($ssiz > 0);
79 my $ctx = Digest::SHA->new($len);
81 $ctx->add($salt) if (defined $salt);
82 my $dig = $ctx->digest;
83 $dig .= $salt if (defined $salt);
91 ssha2passwd - Generate a SHA2 hashed password
95 Hash the given password into a SHA2 digest with optional salt.
99 ssha2passwd [options] <password>
105 =item B<-f> or B<-format> <format>
111 =item B<m> or B<base64> : base64 encoded (default)
113 =item B<x> or B<hex> : hexadecimal string
115 =item B<b> or B<bin> : binary string
119 =item B<-l> or B<-length> <length>
121 Hash algorithm bit length (224, 256, 384, or 512 | default: 256).
123 =item B<-s> or B<-salt> <string>
125 =item B<-S> or B<-Salt> <hexadecimal string>
127 Salt string appended to password and hashed. The resultant digest then
128 has the salt string appended.
130 =item B<-z> or B<-saltlen> [<length>]
132 Byte length of random salt appended to password and hashed, if no salt
133 string is explicitly given (0 is default, default: 8).
135 =item B<-?> or B<-help>
137 Print a brief help message.
141 Print the manual page.