home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2000 May
/
Chip_2000-05_cd1.bin
/
zkuste
/
Perl
/
ActivePerl-5.6.0.613.msi
/
䆊䌷䈹䈙䏵-䞅䞆䞀㡆䞃䄦䠥
/
_a56a78924df27eee9142b1435a2c4155
< prev
next >
Wrap
Text File
|
2000-03-23
|
9KB
|
202 lines
<HTML>
<HEAD>
<TITLE>SHA - Perl interface to the NIST Secure Hash Algorithm</TITLE>
<LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
<LINK REV="made" HREF="mailto:">
</HEAD>
<BODY>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> SHA - Perl interface to the NIST Secure Hash Algorithm</P></STRONG>
</TD></TR>
</TABLE>
<A NAME="__index__"></A>
<!-- INDEX BEGIN -->
<UL>
<LI><A HREF="#name">NAME</A></LI><LI><A HREF="#supportedplatforms">SUPPORTED PLATFORMS</A></LI>
<LI><A HREF="#synopsis">SYNOPSIS</A></LI>
<LI><A HREF="#description">DESCRIPTION</A></LI>
<LI><A HREF="#example and validation">EXAMPLE AND VALIDATION</A></LI>
<LI><A HREF="#further examples and validation">FURTHER EXAMPLES AND VALIDATION</A></LI>
<LI><A HREF="#note">NOTE</A></LI>
<LI><A HREF="#author">AUTHOR</A></LI>
</UL>
<!-- INDEX END -->
<HR>
<P>
<H1><A NAME="name">NAME</A></H1>
<P>SHA - Perl interface to the NIST Secure Hash Algorithm</P>
<P>
<HR>
<H1><A NAME="supportedplatforms">SUPPORTED PLATFORMS</A></H1>
<UL>
<LI>Linux</LI>
<LI>Solaris</LI>
<LI>Windows</LI>
</UL>
<HR>
<H1><A NAME="synopsis">SYNOPSIS</A></H1>
<PRE>
use SHA;</PRE>
<PRE>
$version = &SHA::sha_version;</PRE>
<PRE>
$context = new SHA;
$context->reset();</PRE>
<PRE>
$context->add(LIST);
$context->addfile(HANDLE);</PRE>
<PRE>
$digest = $context->digest();
$string = $context->hexdigest();</PRE>
<PRE>
$digest = $context->hash($string);
$string = $context->hexhash($string);</PRE>
<P>
<HR>
<H1><A NAME="description">DESCRIPTION</A></H1>
<P>The <STRONG>SHA</STRONG> module allows you to use the NIST SHA message digest algorithm
from within Perl programs.</P>
<P>The sha_version routine returns the version of the algorithm which is being
used: either ``SHA'' for the original algorithm or ``SHA-1'' for the updated
algorithm.</P>
<P>A new SHA context object is created with the <STRONG>new</STRONG> operation.
Multiple simultaneous digest contexts can be maintained, if desired.
The context is updated with the <STRONG>add</STRONG> operation which adds the
strings contained in the <EM>LIST</EM> parameter. Adding two strings
separately is equivalent to adding their concatenation:
<CODE>add('foo', 'bar')</CODE> produces the same effect as <CODE>add('foo')</CODE>,
<CODE>add('bar')</CODE>, which in turn produces the same effect as <CODE>add('foobar')</CODE>.</P>
<P>The final message digest value is returned by the <STRONG>digest</STRONG> operation
as a 20-byte binary string. This operation delivers the result of
operations since the last <STRONG>new</STRONG> or <STRONG>reset</STRONG> operation. Once the
operation has been performed, the context must be <STRONG>reset</STRONG> before
being used to calculate another digest value.</P>
<P>Several convenience functions are also provided. The <STRONG>addfile</STRONG>
operation takes an open file-handle and reads it until end-of file in
8192-byte blocks adding the contents to the context. The <STRONG>hexdigest</STRONG>
operation calls <STRONG>digest</STRONG> and returns the result as a printable string
of hexadecimal digits in eight-digit groups. The <STRONG>hash</STRONG> operation
performs the complete series of steps: <STRONG>reset</STRONG>, <STRONG>add</STRONG>, <STRONG>digest</STRONG> on
the supplied scalar value, and returns the result as a 20-byte binary
string. The <STRONG>hexhash</STRONG> operation does the same thing, but returns the
result in the format of the <STRONG>hexdigest</STRONG> operation.</P>
<P>
<HR>
<H1><A NAME="example and validation">EXAMPLE AND VALIDATION</A></H1>
<PRE>
use SHA 1.2;</PRE>
<PRE>
$ver = &SHA::sha_version;
$sha = new SHA;</PRE>
<PRE>
if ($ver eq 'SHA') {
print "EXPECT: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880\n";
} elsif ($ver eq 'SHA-1') {
print "EXPECT: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d\n";
} else {
die "panic! unrecognized output '$ver' from sha_version\n";
}</PRE>
<PRE>
$sha->reset();
$sha->add("abc");
print "RESULT 1: " . $sha->hexdigest() . "\n";</PRE>
<PRE>
$sha->reset();
$sha->add("a", "bc");
print "RESULT 2: " . $sha->hexdigest() . "\n";</PRE>
<PRE>
$sha->reset();
$sha->add("ab", "c");
print "RESULT 3: " . $sha->hexdigest() . "\n";</PRE>
<PRE>
$sha->reset();
$sha->add("a", "b", "c");
print "RESULT 4: " . $sha->hexdigest() . "\n";</PRE>
<PRE>
$sha->reset();
$sha->add("ab");
$sha->add("c");
print "RESULT 5: " . $sha->hexdigest() . "\n";</PRE>
<PRE>
$sha->reset();
$sha->add("a");
$sha->add("bc");
print "RESULT 6: " . $sha->hexdigest() . "\n";</PRE>
<PRE>
$sha->reset();
$sha->add("a");
$sha->add("b");
$sha->add("c");
print "RESULT 7: " . $sha->hexdigest() . "\n";</PRE>
<PRE>
print "RESULT 8: " . $sha->hexhash("abc") . "\n";</PRE>
<PRE>
$sha->reset();
$sha->add("ab", "c");
print "result a: " . unpack("H*", ($sha->digest())) . "\n";</PRE>
<PRE>
print "result b: " . unpack("H*", ($sha->hash("abc"))) . "\n";</PRE>
<P>The above example will produce one of two outputs. If the original SHA
algorithm is being used, the output will be:</P>
<PRE>
EXPECT: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 1: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 2: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 3: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 4: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 5: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 6: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 7: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
RESULT 8: 0164b8a9 14cd2a5e 74c4f7ff 082c4d97 f1edf880
result a: 0164b8a914cd2a5e74c4f7ff082c4d97f1edf880
result b: 0164b8a914cd2a5e74c4f7ff082c4d97f1edf880</PRE>
<P>If the new SHA-1 algorithm is being used, the output will be</P>
<PRE>
EXPECT: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 1: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 2: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 3: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 4: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 5: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 6: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 7: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
RESULT 8: a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
result a: a9993e364706816aba3e25717850c26c9cd0d89d
result b: a9993e364706816aba3e25717850c26c9cd0d89d</PRE>
<P>provided that the implementation is working correctly.</P>
<P>
<HR>
<H1><A NAME="further examples and validation">FURTHER EXAMPLES AND VALIDATION</A></H1>
<P>The file sha_driver.pl contains further validation data and examples of
how to use the SHA module. It may be invoked with the <STRONG>-h</STRONG> flag, which gives
further usage instructions; the <STRONG>-x</STRONG> flag runs a few more tests.</P>
<P>
<HR>
<H1><A NAME="note">NOTE</A></H1>
<P>The SHA extension may be redistributed under the same terms as Perl.
The SHA code is in the public domain. It was heavily modified by
Uwe Hollerbach following the implementation by Peter Gutmann.</P>
<P>
<HR>
<H1><A NAME="author">AUTHOR</A></H1>
<P>The SHA interface was written by Uwe Hollerbach <CODE>uh@alumni.caltech.edu</CODE>,
shamelessly stealing from the MD5 interface written by Neil Winton
(<CODE>N.Winton@axion.bt.co.uk</CODE>).</P>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
<TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
<STRONG><P CLASS=block> SHA - Perl interface to the NIST Secure Hash Algorithm</P></STRONG>
</TD></TR>
</TABLE>
</BODY>
</HTML>