11.1.2.1 Creating Digests for Strings
11.1.2.1 Creating Digests for Strings
The first program goes into a loop where it asks the user to type in two messages in each iteration. The program computes the message digests for the two messages using the MD5 algorithm. It prints a message saying if the user typed in the same message twice, or if the user typed two different messages. The user can type in q to get out of the loop and exit the program.
Program 11.1
#!/usr/bin/perl
#file md5Terminal.pl
use Digest::MD5;
use strict;
my ($context, $message1, $digest1, $message2, $digest2);
my $round;
$context = Digest::MD5->new ();
while (1) {
$round++;
print "Round $round\n";
print "_" x 50, "\n";
print "Please type a message, q to quit: ";
$message1 = ;
chomp ($message1);
if ($message1 =~ /^q$/){ exit;}
$context -> add($message1);
$digest1 = $context -> digest();
print "Please type a second message: ";
$message2 = ;
chomp ($message2);
$context -> reset();
$context -> add ($message2);
$digest2 = $context -> digest();
if ($digest1 eq $digest2){
print "You typed the same message twice\n";
}
else{
print "You typed two different messages\n";
}
printf "%s\t%s\n", "Message", "Digest";
printf "%s\t%s\n", $message1, unpack ("H*", $digest1);
printf "%s\t%s\n", $message2, unpack ("H*", $digest2);;
print "\n";
}
The program creates a new context for the digesting to take place. This is done using the following line of code.
$context = Digest::MD5->new ();
We can now use this context to create digests. In this example, the program asks the user to enter a message. The message is read and chomped, and then added to the digest’s context. The addition is done using the line given below.
$context -> add($message1);
We can add more data to the context if we like. Once we have added all the data, we can ask that an MD5 digest be computed on the data currently in the context. We compute the digest by issuing the following command.
$digest1 = $context -> digest();
The digest produced is a binary digest and is stored in the variable $digest1.
Next, the program asks the user to type in another message. The program computes the MD5 digest for this message as well. It uses the same context for computing the digest. In order to do so, the program resets the context by using the following line of code.
$context -> reset();
Of course, we could have used two contexts if we wanted.
The program compares the two digests and lets the user know if he or she typed the same string or two different strings. The program prints the two strings the user typed in along with the corresponding digests. The digests are binary data. Therefore, they are converted to hexadecimal so they can be printed on the terminal. One of the print statements is shown below.
printf "%s\t%s\n", $message1, unpack ("H*", $digest1);
Thus, digest is unpacked before printing. The H* argument to unpack asks that the resulting bytes be in hexadecimal and thus, printable form. The unpack command takes a format template and a string, and converts the string into the form asked by the template.
A terminal interaction with this program is given below.
Round 1
__________________________________________________
Please type a message, q to quit: abcdefghijklmnopqrst
Please type a second message: abcdefghijklmnopqrst
You typed the same message twice
Message Digest
abcdefghijklmnopqrst 6aa8de45918023095f6e831efe48d00b
abcdefghijklmnopqrst 6aa8de45918023095f6e831efe48d00b
Round 2
__________________________________________________
Please type a message, q to quit: I love Lucy
Please type a second message: I love Lucy
You typed the same message twice
Message Digest
I love Lucy fd3d476fcddf1d1218afc0b1c77039c7
I love Lucy fd3d476fcddf1d1218afc0b1c77039c7
Round 3
__________________________________________________
Please type a message, q to quit: I love Lucy
Please type a second message: I like Lucy
You typed two different messages
Message Digest
I love Lucy fd3d476fcddf1d1218afc0b1c77039c7
I like Lucy 7bc1d963c4aa3af575dfa6d3239df403
Round 4
__________________________________________________
Please type a message, q to quit: q
pikespeak[96]:
