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