Perl SHA-256 checksums with Digest::SHA
last modified November 25, 2026 Perl 5.44
A digest is a fixed-length value calculated from a sequence of bytes. SHA-256 produces a 256-bit digest, usually displayed as 64 hexadecimal digits. When a publisher supplies a SHA-256 checksum for a download, we can calculate the same digest locally and compare the two values.
Digest::SHA comes with Perl. It provides a function for hashing a
short value all at once and an object interface for adding bytes in pieces or
reading them from a file.
Hashing a string
The sha256_hex function returns the digest as lowercase hexadecimal
text. Start with an ASCII string so that each character supplies one byte.
#!/usr/bin/perl
use v5.34.0;
use warnings;
use Digest::SHA qw(sha256_hex);
say sha256_hex("ZetCode\n");
The newline inside the string is part of the input. The newline printed by
say is not. For non-ASCII text, first choose an encoding such as
UTF-8 and hash the encoded bytes; see Perl UTF-8 and
encoding.
$ perl digest_string.pl dd5c8ca5395eb0f4c4dfe63e6fdd36abf26c9f7da683988f8b29e391f5168aec
Adding bytes in pieces
When the input arrives in several chunks, create a digest object with
Digest::SHA->new(256) and add each chunk in order.
#!/usr/bin/perl
use v5.34.0;
use warnings;
use Digest::SHA;
my $sha = Digest::SHA->new(256);
my @chunks = ('Zet', 'Code', "\n");
for my $chunk (@chunks) {
$sha->add($chunk);
}
say $sha->hexdigest;
The object hashes the same bytes as in the first example. The calls to
add do not insert separators, so splitting the input into chunks
does not change the result. hexdigest finishes the digest and
resets the object for a new input.
$ perl digest_chunks.pl dd5c8ca5395eb0f4c4dfe63e6fdd36abf26c9f7da683988f8b29e391f5168aec
Hashing a file
Use addfile to read a file into a digest without loading the whole
file into a string. For this example, create sample.txt with
printf 'ZetCode\n' > sample.txt in a POSIX shell.
#!/usr/bin/perl use v5.34.0; use warnings; use Digest::SHA; @ARGV == 1 or die "Usage: perl digest_file.pl FILE\n"; my ($path) = @ARGV; open my $input, '<:raw', $path or die "Cannot open $path: $!\n"; my $sha = Digest::SHA->new(256); $sha->addfile($input); close $input or die "Cannot close $path: $!\n"; say $sha->hexdigest;
The :raw layer reads the exact bytes stored in the file. In
particular, it prevents platforms that normally translate line endings from
changing the input. addfile reads from the open handle but leaves
closing it to the caller. The sample file contains the same bytes as the
string in the first example.
$ perl digest_file.pl sample.txt dd5c8ca5395eb0f4c4dfe63e6fdd36abf26c9f7da683988f8b29e391f5168aec
Verifying a checksum
A verifier takes a file and its expected digest. Check that the supplied value
contains exactly 64 hexadecimal digits, then hash the file using the same raw
read as before. For the mismatch example, create a second file with
printf 'ZetCode!' > changed.txt. The files have the same size,
but their last bytes differ.
#!/usr/bin/perl
use v5.34.0;
use warnings;
use Digest::SHA;
@ARGV == 2 or die "Usage: perl verify_sha256.pl FILE EXPECTED_SHA256\n";
my ($path, $expected) = @ARGV;
unless ($expected =~ /\A[0-9a-fA-F]{64}\z/) {
die "Expected a 64-digit hexadecimal SHA-256 checksum\n";
}
open my $input, '<:raw', $path or die "Cannot open $path: $!\n";
my $sha = Digest::SHA->new(256);
$sha->addfile($input);
close $input or die "Cannot close $path: $!\n";
my $actual = $sha->hexdigest;
if ($actual eq lc $expected) {
say "$path: OK";
exit 0;
}
say STDERR "$path: checksum mismatch";
exit 1;
Uppercase and lowercase hexadecimal digits represent the same value, so
lc normalizes the expected digest before eq compares
it. The program exits with status 0 for a match and 1 for a mismatch. In a
shell, $? gives the status of the most recent command. Invalid
arguments and file errors also cause a nonzero exit.
$ checksum=DD5C8CA5395EB0F4C4DFE63E6FDD36ABF26C9F7DA683988F8B29E391F5168AEC $ perl verify_sha256.pl sample.txt "$checksum" sample.txt: OK $ perl verify_sha256.pl changed.txt "$checksum" changed.txt: checksum mismatch $ echo $? 1
Best practices
A checksum tells us whether bytes match a reference. It does not identify who created the file or whether the file is safe to open. Get the expected digest from a trusted source, such as the publisher's authenticated release page or a checksum manifest with a verified signature. A checksum from the same untrusted source as the file does not establish authenticity.
Source
Digest::SHA documentation · PerlIO layers
In this article we have hashed a string, added bytes in chunks, calculated a file's digest and verified it against a known SHA-256 checksum. See also Perl binary files and Perl command line.
Author
List all Perl tutorials.