reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
SHA1Stream.cxx
Go to the documentation of this file.
1#ifndef NOMINMAX
2#define NOMINMAX
3#endif
4
5#include <cstddef>
6#include <cstring>
7#include <new>
8#include <stdexcept>
9#include <algorithm>
10#include "rutil/ResipAssert.h"
11
12#if defined(HAVE_CONFIG_H)
13 #include "config.h"
14#endif
15
16#include "rutil/Socket.hxx" // for ntohl under windows
18
19#if defined(USE_SSL)
21
22#include "openssl/evp.h"
23
24// Remove warning about 'this' use in initiator list - pointer is only stored
25# if defined(WIN32) && !defined(__GNUC__)
26# pragma warning( disable : 4355 ) // using this in base member initializer list
27# endif // WIN32
28
29using namespace resip;
30
32 : mBlown(false)
33{
34 std::memset(mBuf, 0, sizeof(mBuf));
35 setp(mBuf, mBuf + sizeof(mBuf));
36
37 mContext.reset(EVP_MD_CTX_new());
38 if (!mContext)
39 throw std::bad_alloc();
40 int res = EVP_DigestInit_ex(mContext.get(), EVP_sha1(), NULL);
41 if (!res)
42 throw std::runtime_error("Failed to initialize SHA1 context");
43}
44
48
49int
51{
52 std::size_t len = pptr() - pbase();
53 if (len > 0)
54 {
55 EVP_DigestUpdate(mContext.get(), reinterpret_cast<unsigned const char*>(pbase()), len);
56 // reset the put buffer
57 setp(mBuf, mBuf + sizeof(mBuf));
58 }
59 return 0;
60}
61
62int
64{
65 sync();
66 if (c != -1)
67 {
68 mBuf[0] = c;
69 pbump(1);
70 return c;
71 }
72 return 0;
73}
74
75Data
77{
78 resip_assert(mBlown == false);
79 unsigned char md_value[EVP_MAX_MD_SIZE];
80 unsigned int md_len = 0u;
81 EVP_DigestFinal_ex(mContext.get(), md_value, &md_len);
82 mBlown = true;
83 Data digest(Data::Share, reinterpret_cast<const char*>(md_value), md_len);
84 return digest.hex();
85}
86
87Data
89{
90 resip_assert(mBlown == false);
91 resip_assert(bits % 8 == 0);
92 resip_assert(bits / 8 <= EVP_MAX_MD_SIZE);
93 unsigned char md_value[EVP_MAX_MD_SIZE];
94 unsigned int md_len = 0u;
95 EVP_DigestFinal_ex(mContext.get(), md_value, &md_len);
96 mBlown = true;
97 unsigned int len = std::min(bits / 8u, md_len);
98 return Data(md_value + (md_len - len), len);
99}
100
102 : std::ostream(this)
103{
104}
105
108
109Data
111{
112 flush();
113 return SHA1Buffer::getHex();
114 //return mStreambuf.getHex();
115}
116
117Data
119{
120 flush();
121 return SHA1Buffer::getBin(bits);
122}
123
126{
127 flush();
128 uint32_t input = *((uint32_t*)getBin(32).c_str());
129 return ntohl(input);
130}
131
132
133#endif // USE_SSL
134
135/* ====================================================================
136 * The Vovida Software License, Version 1.0
137 *
138 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
139 *
140 * Redistribution and use in source and binary forms, with or without
141 * modification, are permitted provided that the following conditions
142 * are met:
143 *
144 * 1. Redistributions of source code must retain the above copyright
145 * notice, this list of conditions and the following disclaimer.
146 *
147 * 2. Redistributions in binary form must reproduce the above copyright
148 * notice, this list of conditions and the following disclaimer in
149 * the documentation and/or other materials provided with the
150 * distribution.
151 *
152 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
153 * and "Vovida Open Communication Application Library (VOCAL)" must
154 * not be used to endorse or promote products derived from this
155 * software without prior written permission. For written
156 * permission, please contact vocal@vovida.org.
157 *
158 * 4. Products derived from this software may not be called "VOCAL", nor
159 * may "VOCAL" appear in their name, without prior written
160 * permission of Vovida Networks, Inc.
161 *
162 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
163 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
164 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
165 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
166 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
167 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
168 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
169 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
170 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
171 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
172 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
173 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
174 * DAMAGE.
175 *
176 * ====================================================================
177 *
178 * This software consists of voluntary contributions made by Vovida
179 * Networks, Inc. and many individuals on behalf of Vovida Networks,
180 * Inc. For more information on Vovida Networks, Inc., please see
181 * <http://www.vovida.org/>.
182 *
183 */
Data bits(size_t v)
Definition Data.cxx:2342
static Data digest("digest")
#define resip_assert(x)
This file needs to work for both C and C++ code.
DEPRECATED - Use DigestStream.hxx instead.
Handles cross-platform sockets compatibility.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
const char * c_str() const
Returns a null-terminated string representing.
Definition Data.cxx:1097
@ Share
The Data instance will use the buffer in a read-only mode.
Definition Data.hxx:266
Data hex() const
Returns a hexadecimal representation of the contents of this Data.
Definition Data.cxx:1577
virtual ~SHA1Buffer()
virtual int overflow(int c=-1)
Data getBin(unsigned int bits)
virtual int sync()
Data getBin(unsigned int bits=160)
Calls flush() on itself and returns the SHA1 data in binary format.
Data getHex()
Calls flush() on itself and returns the SHA1 data in hex format.
uint32_t getUInt32()
Calls getBin(32) and converts to a UInt32.
dcm! – add UnusedChecking(_enum) below;
unsigned int uint32_t
Definition Stun.hxx:34