How can I use Perl to get the SHA1 hash of a file from a Windows command prompt?

I have a file called secure.txt in c: \ temp. I want to run the Perl command from the command line to print the SHA1 hash of the secure.txt file. I am using ActivePerl 5.8.2. I have not used Perl before, but this is the most convenient option available right now.

+6
command-line windows perl sha1
source share
3 answers
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 .

  • -E commandline

    behaves the same as -e , except that it implicitly includes all the additional functions (in the main part of the compilation). See feature .

One of these additional features is say , which makes -l not required.

  • say FILEHANDLE LIST
  • say LIST
  • say

    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.

+20
source share

Try the Digest :: SHA module.

 C:\> perl -MDigest::SHA -e "print Digest::SHA->new(1)->addfile('secure.txt')->hexdigest" 
+5
source share

Use Digest::SHA1 as follows:

 #!/usr/bin/perl -w use strict; use Digest::SHA1 qw/ sha1_hex /; # open file open IN_DATA, "<secure.txt" or die "cannot open file secure.txt for reading: $!"; # read in all file contents my $file_contents; {local $/; $file_contents = <IN_DATA>;} # close file close IN_DATA; print &sha1_hex($file_contents); 

Edit: why voice? Does this code work? Is this a solution to the problem?

-one
source share

All Articles