PKIFCryptoPPExternalDigest.cpp

Go to the documentation of this file.
00001 
00010 #include "PKIFCryptoPPExternalDigest.h"
00011 
00012 using namespace CryptoPP;
00013 
00021 PKIFCryptoPPExternalDigest::~PKIFCryptoPPExternalDigest()
00022 {
00023     if(m_digestBuf) {
00024         delete[] m_digestBuf;
00025         m_digestBuf = 0;
00026     }
00027 }
00028 
00037 void PKIFCryptoPPExternalDigest::SetDigest(const unsigned char * buf, unsigned int length)
00038 {
00039     if(m_digestBuf) {
00040         delete[] m_digestBuf;
00041         m_digestSize = length;
00042         m_digestBuf = new unsigned char[m_digestSize];
00043     } else {
00044         m_digestSize = length;
00045         m_digestBuf = new unsigned char[m_digestSize];
00046     }
00047     memcpy(m_digestBuf,buf,m_digestSize);
00048 }
00049 
00055 unsigned int PKIFCryptoPPExternalDigest::DigestSize() const {
00056     return m_digestSize;
00057 }
00058 
00064 void PKIFCryptoPPExternalDigest::TruncatedFinal( 
00066                                     byte * digest, 
00068                                     size_t digestSize)
00069 {
00070     // I'd *really* like this to throw if digestSize and m_digestSize don't match, but
00071     // there's no documentation in crypto++ around where this could be called. Since the
00072     // name strongly implies that truncation should be OK, that's what we'll do. XXX Revisit
00073     if(digestSize > 0) {
00074         memcpy(digest,m_digestBuf,digestSize > m_digestSize ? m_digestSize : digestSize);
00075     }
00076 }
00077 
00084 bool PKIFCryptoPPExternalDigest::TruncatedVerify(
00086                                      const byte * digest,
00088                                      size_t digestLength)
00089 {
00090     if(digestLength != m_digestSize) return false;
00091     return (0 == memcmp(m_digestBuf,digest, digestLength));
00092 }
00093 
00094 
00103 PKIFCryptoPPExternalDigestAccumulator * NewEDAccumulator(
00105     unsigned char * digest,
00107     size_t len)
00108 {
00109     // this is ugly but it's the only way to let the caller calculate a hash
00110     // without subclassing every digest algorithm we'd like this to work with.
00111     // Since doing that would mean the same code copied and pasted at least for
00112     // MD5, SHA-1, SHA-256, SHA-384 and SHA-512, this is reluctantly preferred.
00113     // This is not generally a great way to use crypto++, as it removes most of
00114     // the power of the filter pattern they use, but that's unneeded inside PKIF
00115     // for the most part. Do not emulate this outside PKIF.
00116     PKIFCryptoPPExternalDigestAccumulator * acc = new PKIFCryptoPPExternalDigestAccumulator();
00117     HashTransformation & h = acc->AccessHash();
00118     PKIFCryptoPPExternalDigest * ed = dynamic_cast<PKIFCryptoPPExternalDigest *>(&h);
00119     ed->SetDigest(digest,(unsigned int)len);
00120     return acc;
00121 }

Generated on Mon Nov 15 11:15:54 2010 for PublicKeyInfrastructureFramework(PKIF) by  doxygen 1.5.6