Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/express/crypto_utils.cxx

Go to the documentation of this file.
00001 // Filename: crypto_utils.cxx
00002 // Created by:  drose (07Nov00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 // This file is compiled only if we have crypto++ installed.
00020 
00021 
00022 #include "hashVal.h"
00023 #include "config_express.h"
00024 #include "crypto_utils.h"
00025 
00026 #include <md5.h>
00027 #include <hex.h>
00028 #include <files.h>
00029 
00030 // md5 defines this, but we'd rather do without it, because it
00031 // conflicts with a similar macro defined in NSPR.
00032 #undef IS_LITTLE_ENDIAN
00033 
00034 USING_NAMESPACE(CryptoPP);
00035 USING_NAMESPACE(std);
00036 
00037 static uint
00038 read32(istream& is) {
00039   unsigned int ret = 0x0;
00040   for (int i=0; i<8; ++i) {
00041     char b;
00042     int n = 0;
00043     is >> b;
00044     switch (b) {
00045     case '0':
00046     case '1':
00047     case '2':
00048     case '3':
00049     case '4':
00050     case '5':
00051     case '6':
00052     case '7':
00053     case '8':
00054     case '9':
00055       n = b - '0';
00056       break;
00057     case 'A':
00058     case 'B':
00059     case 'C':
00060     case 'D':
00061     case 'E':
00062     case 'F':
00063       n = b - 'A' + 10;
00064       break;
00065     case 'a':
00066     case 'b':
00067     case 'c':
00068     case 'd':
00069     case 'e':
00070     case 'f':
00071       n = b - 'a' + 10;
00072       break;
00073     default:
00074       cerr << "illegal hex nibble '" << b << "'" << endl;
00075     }
00076     ret = (ret << 4) | (n & 0x0f);
00077   }
00078   return ret;
00079 }
00080 
00081 void
00082 md5_a_file(const Filename &name, HashVal &ret) {
00083   ostringstream os;
00084   MD5 md5;
00085 
00086   string fs = name.to_os_specific();
00087 
00088   try {
00089     FileSource f(fs.c_str(), true,
00090                  new HashFilter(md5, new HexEncoder(new FileSink(os))));
00091   } catch (...) {
00092     express_cat.warning()
00093       << "Unable to read " << name << " to compute md5 hash.\n";
00094     if (!name.exists()) {
00095       express_cat.warning()
00096         << "(file does not exist.)\n";
00097     } else {
00098       express_cat.warning()
00099         << "(file exists but cannot be read.)\n";
00100     }
00101     ret.hv[0] = 0;
00102     ret.hv[1] = 0;
00103     ret.hv[2] = 0;
00104     ret.hv[3] = 0;
00105     return;
00106   }
00107 
00108   istringstream is(os.str());
00109 
00110   ret.hv[0] = read32(is);
00111   ret.hv[1] = read32(is);
00112   ret.hv[2] = read32(is);
00113   ret.hv[3] = read32(is);
00114 }
00115 
00116 void
00117 md5_a_buffer(const unsigned char* buf, unsigned long len, HashVal& ret) {
00118   MD5 md5;
00119 
00120   HashFilter hash(md5);
00121   hash.Put((byte*)buf, len);
00122   hash.Close();
00123 
00124   unsigned char* outb;
00125   unsigned long outl = hash.MaxRetrieveable();
00126   outb = new uchar[outl];
00127   hash.Get((byte*)outb, outl);
00128   ret.hv[0] = (outb[0] << 24) | (outb[1] << 16) | (outb[2] << 8) | outb[3];
00129   ret.hv[1] = (outb[4] << 24) | (outb[5] << 16) | (outb[6] << 8) | outb[7];
00130   ret.hv[2] = (outb[8] << 24) | (outb[9] << 16) | (outb[10] << 8) | outb[11];
00131   ret.hv[3] = (outb[12] << 24) | (outb[13] << 16) | (outb[14] << 8) | outb[15];
00132   delete[] outb;
00133 }
00134 

Generated on Fri May 2 00:38:20 2003 for Panda by doxygen1.3