reSIProcate/rutil  9694
DigestStream.cxx
Go to the documentation of this file.
00001 #include "rutil/DigestStream.hxx"
00002 
00003 // Remove warning about 'this' use in initiator list - pointer is only stored
00004 #if defined(WIN32) && !defined(__GNUC__)
00005 #pragma warning( disable : 4355 ) // using this in base member initializer list 
00006 #endif
00007 
00008 using namespace resip;
00009 
00010 DigestBuffer::DigestBuffer(const EVP_MD* digest)
00011 {
00012    EVP_MD_CTX_init(&mContext);
00013    EVP_DigestInit_ex(&mContext, digest, 0);
00014    setp(mBuf, mBuf + sizeof(mBuf));
00015 }
00016 
00017 DigestBuffer::~DigestBuffer()
00018 {
00019    EVP_MD_CTX_cleanup(&mContext);
00020 }
00021 
00022 int
00023 DigestBuffer::sync()
00024 {
00025    size_t len = pptr() - pbase();
00026    if (len > 0) 
00027    {
00028       EVP_DigestUpdate(&mContext, reinterpret_cast <unsigned const char*>(pbase()), len);
00029       // reset the put buffer
00030       setp(mBuf, mBuf + sizeof(mBuf));
00031    }
00032    return 0;
00033 }
00034 
00035 int
00036 DigestBuffer::overflow(int c)
00037 {
00038    sync();
00039    if (c != -1) 
00040    {
00041       mBuf[0] = c;
00042       pbump(1);
00043       return c;
00044    }
00045    return 0;
00046 }
00047 
00048 Data 
00049 DigestBuffer::getHex()
00050 {
00051    unsigned char buf[EVP_MD_CTX_size(&mContext)];
00052    unsigned int len;
00053    EVP_DigestFinal_ex(&mContext, buf, &len);
00054 
00055    Data digest(Data::Share, (const char*)buf, len);
00056    return digest.hex();
00057 }
00058 
00059 Data
00060 DigestBuffer::getBin()
00061 {
00062    unsigned char buf[EVP_MD_CTX_size(&mContext)];
00063    unsigned int len;
00064    EVP_DigestFinal_ex(&mContext, buf, &len);
00065 
00066    Data digest(Data::Share, (const char*)buf, len);
00067    return digest;
00068 }
00069 
00070 DigestStream::DigestStream(const EVP_MD* digest)
00071    : DigestBuffer(digest), std::ostream(this)
00072 {
00073 }
00074 
00075 DigestStream::~DigestStream()
00076 {}
00077 
00078 Data 
00079 DigestStream::getHex()
00080 {
00081    flush();
00082    return DigestBuffer::getHex();
00083    //return mStreambuf.getHex();
00084 }
00085 
00086 Data
00087 DigestStream::getBin()
00088 {
00089    flush();
00090    return DigestBuffer::getBin();
00091 }
00092 
00093 /* ====================================================================
00094  * The Vovida Software License, Version 1.0 
00095  * 
00096  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00097  * 
00098  * Redistribution and use in source and binary forms, with or without
00099  * modification, are permitted provided that the following conditions
00100  * are met:
00101  * 
00102  * 1. Redistributions of source code must retain the above copyright
00103  *    notice, this list of conditions and the following disclaimer.
00104  * 
00105  * 2. Redistributions in binary form must reproduce the above copyright
00106  *    notice, this list of conditions and the following disclaimer in
00107  *    the documentation and/or other materials provided with the
00108  *    distribution.
00109  * 
00110  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00111  *    and "Vovida Open Communication Application Library (VOCAL)" must
00112  *    not be used to endorse or promote products derived from this
00113  *    software without prior written permission. For written
00114  *    permission, please contact vocal@vovida.org.
00115  *
00116  * 4. Products derived from this software may not be called "VOCAL", nor
00117  *    may "VOCAL" appear in their name, without prior written
00118  *    permission of Vovida Networks, Inc.
00119  * 
00120  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00121  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00122  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00123  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00124  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00125  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00126  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00127  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00128  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00129  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00130  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00131  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00132  * DAMAGE.
00133  * 
00134  * ====================================================================
00135  * 
00136  * This software consists of voluntary contributions made by Vovida
00137  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00138  * Inc.  For more information on Vovida Networks, Inc., please see
00139  * <http://www.vovida.org/>.
00140  *
00141  */