Digest/main.cpp

00001 //
00002 //  Copyright (c) 2006 by Autodesk, Inc.
00003 //
00004 //  By using this code, you are agreeing to the terms and conditions of
00005 //  the License Agreement included in the documentation for this code.
00006 //
00007 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED,
00008 //  AS TO THE CORRECTNESS OF THIS CODE OR ANY DERIVATIVE
00009 //  WORKS WHICH INCORPORATE IT.
00010 //
00011 //  AUTODESK PROVIDES THE CODE ON AN "AS-IS" BASIS
00012 //  AND EXPLICITLY DISCLAIMS ANY LIABILITY, INCLUDING
00013 //  CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00014 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00015 //
00016 //  Use, duplication, or disclosure by the U.S. Government is subject to
00017 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00018 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00019 //  Data and Computer Software), as applicable.
00020 //
00021 
00022 #include <iostream>
00023 #include <iomanip>
00024 
00025 #include "dwfcore/DigestInputStream.h"
00026 #include "dwfcore/DigestOutputStream.h"
00027 #include "dwfcore/FileInputStream.h"
00028 #include "dwfcore/FileOutputStream.h"
00029 #include "dwfcore/StreamFileDescriptor.h"
00030 
00031 using namespace std;
00032 using namespace DWFCore;
00033 
00034 /*
00035     This sample shows the use of digests in the DWFCore. To calculate the digest 
00036     when reading in a file or writing it out, create a digest input stream, with
00037     the a digest object. NOTE: The digest input stream will own the digest.
00038     e.g.
00039         DWFDigest* pDigest = DWFCORE_ALLOC_OBJECT( DWFMD5Digest* );
00040         DWFDigestInputStream oInDigestStream( pDigest, NULL, false );
00041     and then chain the source (or target) stream to the digest stream. E.g.
00042         oInDigestStream.chainInputStream( &oInFilestream, false );
00043 
00044     Now read in (or write out) the data using the digest stream as you would 
00045     directly with the source (or target) stream. Once the reading (or writing)
00046     is complete, you can query the digest stream for the digest value.
00047 
00048     Digests that are progressive can be queried for the digest during the read/write
00049     operations, but this maybe quite a bit slower since a final digest calculation
00050     can correspond to multiple digest updates depending on the type of digest.
00051 */
00052 
00053 
00054 std::wostream& operator<<( std::wostream& os, const DWFString& zString )
00055 {
00056     os << (const wchar_t*)zString;
00057     return os;
00058 }
00059 
00060 void ShowFileDigest( const DWFString& zFileName,
00061                      DWFDigestInputStream& rInDigestStream,
00062                      DWFDigestOutputStream& rOutDigestStream )
00063 {
00064     //
00065     //  Create the file and descriptor, for reading.
00066     //
00067     DWFFile                 oInFile( zFileName );
00068     DWFStreamFileDescriptor oInDescriptor( oInFile, L"rb" );
00069     oInDescriptor.open();
00070 
00071     //
00072     //  Create the file and descriptor, for writing.
00073     //
00074     DWFString               zOutFileName( "Out_" );
00075     zOutFileName.append( zFileName );
00076     DWFFile                 oOutFile( zOutFileName );
00077     DWFStreamFileDescriptor oOutDescriptor( oOutFile, L"wb" );
00078     oOutDescriptor.open();
00079 
00080     //
00081     //  Create input/output file streams and attach the descriptors.
00082     //
00083     DWFFileInputStream oInFilestream;
00084     oInFilestream.attach( &oInDescriptor, false );
00085 
00086     DWFFileOutputStream oOutFilestream;
00087     oOutFilestream.attach( &oOutDescriptor, false );
00088 
00089     //
00090     //  Chain the input/output file streams to the corresponding digest streams
00091     //
00092     rInDigestStream.chainInputStream( &oInFilestream, false );
00093     rOutDigestStream.chainOutputStream( &oOutFilestream, false );
00094 
00095     #define DATA_BUFFER_BYTES   4096
00096     unsigned char* pDataBuffer = DWFCORE_ALLOC_MEMORY( unsigned char, DATA_BUFFER_BYTES );
00097 
00098     //
00099     //  Read in the file. This will compute the digest in the process
00100     //
00101     size_t nBytes = 0;
00102     int nLoopCount = 0;
00103     while (rInDigestStream.available() > 0)
00104     {
00105         nBytes = rInDigestStream.read( pDataBuffer, DATA_BUFFER_BYTES );
00106         rOutDigestStream.write( pDataBuffer, nBytes );
00107 
00108         if (rInDigestStream.isDigestProgressive() && rInDigestStream.available()>0)
00109         {
00110             if (nLoopCount==0)
00111             {
00112                 wcout << endl;
00113             }
00114 
00115             if (nLoopCount%2==0)
00116             {
00117                 wcout << "\tProgressive (input) = " << rInDigestStream.digest() << endl;
00118             }
00119         }
00120 
00121         nLoopCount++;
00122     }
00123 
00124     //
00125     //  Print out the digest
00126     //
00127     DWFString zInDigest = rInDigestStream.digest();
00128     DWFString zOutDigest = rOutDigestStream.digest();
00129     wcout << endl
00130           << "\tFinal (input)  = " << zInDigest << endl
00131           << "\tFinal (output) = " << zOutDigest << endl;
00132 
00133     DWFString zBase64Digest = rInDigestStream.digestBase64();
00134     wcout << "\tDigest Base 64 = " << zBase64Digest << endl;
00135 
00136     unsigned char* pCharArray = NULL;
00137     size_t nRawBytes = rInDigestStream.digestRawBytes( pCharArray );
00138     if (pCharArray)
00139     {
00140         wcout << "\tRaw (" << nRawBytes << " bytes) = ";
00141         size_t i = 0;
00142         for (; i<nRawBytes; i++)
00143         {
00144             wcout << int(pCharArray[i]) << " ";
00145         }
00146         wcout << endl;
00147 
00148         DWFCORE_FREE_MEMORY( pCharArray );
00149     }
00150 
00151     std::flush( wcout );
00152 
00153     oInDescriptor.close();
00154     oOutDescriptor.close();
00155 }
00156 
00157 
00158 int main( int /*argc*/, char* /*argv[]*/ )
00159 {
00160         //
00161         //  Create input/output digest streams and chain the input/output file streams
00162         //  to it, to compute the digests.
00163         //
00164         DWFDigestInputStream oInDigestStream( DWFCORE_ALLOC_OBJECT(DWFMD5Digest), NULL, false );
00165         DWFDigestOutputStream oOutDigestStream( DWFCORE_ALLOC_OBJECT(DWFMD5Digest), NULL, false );
00166 
00167         wcout << endl << "Testing MD5" << endl;
00168         wcout << "\tString in file = ""12345678901234567890123456789012345678901234567890123456789012345678901234567890""" << endl;
00169         wcout << "\tDigest expected= ""57edf4a22be3c955ac49da2e2107b67a""" << endl;
00170         ShowFileDigest( "testMD5.txt", oInDigestStream, oOutDigestStream );
00171     
00172         //
00173         //  Create input/output digest streams and chain the input/output file streams
00174         //  to it, to compute the digests.
00175         //
00176         DWFDigestInputStream oInDigestStream2( DWFCORE_ALLOC_OBJECT(DWFSHA1Digest), NULL, false );
00177         DWFDigestOutputStream oOutDigestStream2( DWFCORE_ALLOC_OBJECT(DWFSHA1Digest), NULL, false );
00178 
00179         wcout << endl << "Testing SHA1" << endl;
00180         wcout << "\tString in file = ""abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq""" << endl;
00181         wcout << "\tDigest expected= ""84983e441c3bd26ebaae4aa1f95129e5e54670f1""" << endl;
00182         ShowFileDigest( "testSHA1.txt", oInDigestStream2, oOutDigestStream2 );
00183 
00184         return 0;
00185 
00186 }
00187 
00188 

Generated on Tue Jan 6 22:39:28 2009 for Autodesk DWF Core Library by  doxygen 1.4.5