Using SHA1 Message Digest in Perl

This section provides a tutorial example on how to use SHA1 message digest algorithm in Perl. John Allen implemented the entire SHA1 algorithm in 8 lines of Perl 5 code.

If you are interested in using SHA1 in Perl, you can look a very interesting implementation by John Allen in 8 lines of perl5, see

Here is a copy of John's code, stored in

#!/usr/bin/perl -iD9T4C`>_-JXF8NMS^$#)4=L/2X?!:@GF9;MGKH8\;O-S*8L'6
@A=unpack"N*",unpack u,$^I;@K=splice@A,5,4;sub M{($x=pop)-($m=1+~0)*int$x/$m};
sub L{$n=pop;($x=pop)<<$n|2**$n-1&$x>>32-$n}@F=(sub{$b&($c^$d)^$d},$S=sub{$b^$c
^$d},sub{($b|$c)&$d|$b&$c},$S);do{$l+=$r=read STDIN,$_,64;$r++,$_.="\x80"if$r<
64&&!$p++;@W=unpack N16,$_."\0"x7;$W[15]=$l*8 if$r<57;for(16..79){push@W,L$W[$_
M$_+${$v++}}@A}while$r>56;printf'%.8x'x5 ."\n",@A

To test this Perl program on Windows, I did the following in a command window:

herong> copy con empty.txt
        1 file(s) copied.

herong> perl < empty.txt

herong> copy con abc.txt
        1 file(s) copied.

herong> perl < abc.txt

herong> copy  con a_to_z.txt
        1 file(s) copied.

herong> perl < a_to_z.txt

The output matches the testing result listed in FreeBSD libmd makefile: The output proves that John's program works perfectly. Note that:

Table of Contents

 About This Book

 Cryptography Terminology

 Cryptography Basic Concepts

 Introduction to AES (Advanced Encryption Standard)

 Introduction to DES Algorithm

 DES Algorithm - Illustrated with Java Programs

 DES Algorithm Java Implementation

 DES Algorithm - Java Implementation in JDK JCE

 DES Encryption Operation Modes

 DES in Stream Cipher Modes

 PHP Implementation of DES - mcrypt

 Blowfish - 8-Byte Block Cipher

 Secret Key Generation and Management

 Cipher - Secret Key Encryption and Decryption

 Introduction of RSA Algorithm

 RSA Implementation using java.math.BigInteger Class

 Introduction of DSA (Digital Signature Algorithm)

 Java Default Implementation of DSA

 Private key and Public Key Pair Generation

 PKCS#8/X.509 Private/Public Encoding Standards

 Cipher - Public Key Encryption and Decryption

 MD5 Mesasge Digest Algorithm

SHA1 Mesasge Digest Algorithm

 What Is SHA1 Message Digest Algorithm?

 SHA1 Message Digest Algorithm Overview

 Using SHA1 Message Digest in Java

 Using SHA1 Message Digest in PHP

Using SHA1 Message Digest in Perl

 OpenSSL Introduction and Installation

 OpenSSL Generating and Managing RSA Keys

 OpenSSL Managing Certificates

 OpenSSL Generating and Signing CSR

 OpenSSL Validating Certificate Path

 "keytool" and "keystore" from JDK

 "OpenSSL" Signing CSR Generated by "keytool"

 Migrating Keys from "keystore" to "OpenSSL" Key Files

 Certificate X.509 Standard and DER/PEM Formats

 Migrating Keys from "OpenSSL" Key Files to "keystore"

 Using Certificates in IE

 Using Certificates in Google Chrome

 Using Certificates in Firefox

 Archived Tutorials


 Full Version in PDF/EPUB