perl -MDigest::SHA1=sha1_hex -le "print sha1_hex <>" secure.txt
Perl command line options are documented in perlrun . Move from left to right in the above command:
-M Digest::SHA1=sha1_hex loads the Digest :: SHA1 module at compile time and imports sha1_hex , which gives the digest in hexadecimal form.-l automatically appends a newline at the end of any print-e enter the Perl code to execute
Funny diamond is a special case of the Perls readline statement:
The null file descriptor <> is special: it can be used to emulate the behavior of sed and awk . The input from <> comes either from standard input, or from each file specified on the command line. Here's how it works: for the first time <> , the @ARGV array is @ARGV , and if it is empty, $ARGV[0] set to "-" , which when opened gives standard input. The @ARGV array @ARGV then treated as a list of file names.
Since secure.txt is the only file named on the command line, its contents become the sha1_hex argument.
With Perl version 5.10 or later, you can reduce the specified single-line font by five characters.
perl -MDigest::SHA=sha1_hex -E 'say sha1_hex<>' secure.txt
The code will drop optional (with all versions of Perl) spaces before <> , -l and switch from -e to -e .
One of these additional features is say , which makes -l not required.
say FILEHANDLE LISTsay LISTsay
Just like print , but implicitly adds a new line. say LIST is just an abbreviation for
{ local $\ = "\n"; print LIST }
This keyword is only available when say : is enabled. See feature .
If you like this code in a convenient utility, say mysha1sum.pl , use
#! /usr/bin/perl use warnings; use strict; use Digest::SHA1; die "Usage: $0 file ..\n" unless @ARGV; foreach my $file (@ARGV) { my $fh; unless (open $fh, $file) { warn "$0: open $file: $!"; next; } my $sha1 = Digest::SHA1->new; $sha1->addfile($fh); print $sha1->hexdigest, " $file\n"; close $fh; }
This will calculate the digest for each file with a name on the command line, and the output format will be compatible with the Unix sha1sum utility.
C:\> mysha1sum.pl mysha1sum.pl mysha1sum.pl 8f3a7288f1697b172820ef6be0a296560bc13bae mysha1sum.pl 8f3a7288f1697b172820ef6be0a296560bc13bae mysha1sum.pl
You did not say if Cygwin was installed, but if you do, sha1sum is part of the coreutils package.