/src/resiprocate/rutil/DigestStream.hxx
Line | Count | Source |
1 | | #if !defined(RESIP_DigestSTREAM_HXX) |
2 | | #define RESIP_DigestSTREAM_HXX |
3 | | |
4 | | #include <iostream> |
5 | | #include "rutil/Data.hxx" |
6 | | |
7 | | namespace resip |
8 | | { |
9 | | |
10 | | enum DigestType |
11 | | { |
12 | | MD5, |
13 | | SHA1 |
14 | | #ifdef USE_SSL |
15 | | , SHA256 |
16 | | , SHA512 |
17 | | , SHA512_256 |
18 | | #endif |
19 | | }; |
20 | | |
21 | | /** |
22 | | @brief Implementation of std::streambuf used to back the DigestStream. |
23 | | */ |
24 | | class DigestBuffer : public std::streambuf |
25 | | { |
26 | | public: |
27 | | DigestBuffer(DigestType digestType = MD5); |
28 | | virtual ~DigestBuffer(); |
29 | | |
30 | | /** @returns the Digest hexadecimal representation of the data from the buffer */ |
31 | | Data getHex(); |
32 | | |
33 | | /** @returns the Digest binary representation of the data from the buffer */ |
34 | | const Data& getBin(); |
35 | | |
36 | 0 | size_t bytesTaken() { return mBytesTaken; } |
37 | | |
38 | | protected: |
39 | | virtual int sync(); |
40 | | virtual int overflow(int c = -1); |
41 | | |
42 | | private: |
43 | | DigestType mDigestType; |
44 | | char mBuf[64]; |
45 | | void* mContext; |
46 | | size_t mBytesTaken; |
47 | | Data mFinalBin; |
48 | | }; |
49 | | |
50 | | /** |
51 | | @brief Used to accumlate data written to the stream in a DigestBuffer and |
52 | | convert the data to Digest. |
53 | | */ |
54 | | class DigestStream : private DigestBuffer, public std::ostream |
55 | | { |
56 | | public: |
57 | | DigestStream(DigestType digestType = MD5); |
58 | | ~DigestStream(); |
59 | | /** Calls flush() on itself and returns the Digest data in hex format. |
60 | | @returns the Digest hexadecimal representation of the data written to the |
61 | | stream and convert the data to Digest. |
62 | | */ |
63 | | Data getHex(); |
64 | | const Data& getBin(); |
65 | | |
66 | | size_t bytesTaken(); |
67 | | |
68 | | static const Data& getDigestName(DigestType digestType); |
69 | | }; |
70 | | |
71 | | } |
72 | | |
73 | | #endif |
74 | | /* ==================================================================== |
75 | | * The Vovida Software License, Version 1.0 |
76 | | * |
77 | | * Copyright (c) 2026 SIP Spectrum, Inc. https://www.sipspectrum.com |
78 | | * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. |
79 | | * |
80 | | * Redistribution and use in source and binary forms, with or without |
81 | | * modification, are permitted provided that the following conditions |
82 | | * are met: |
83 | | * |
84 | | * 1. Redistributions of source code must retain the above copyright |
85 | | * notice, this list of conditions and the following disclaimer. |
86 | | * |
87 | | * 2. Redistributions in binary form must reproduce the above copyright |
88 | | * notice, this list of conditions and the following disclaimer in |
89 | | * the documentation and/or other materials provided with the |
90 | | * distribution. |
91 | | * |
92 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
93 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
94 | | * not be used to endorse or promote products derived from this |
95 | | * software without prior written permission. For written |
96 | | * permission, please contact vocal@vovida.org. |
97 | | * |
98 | | * 4. Products derived from this software may not be called "VOCAL", nor |
99 | | * may "VOCAL" appear in their name, without prior written |
100 | | * permission of Vovida Networks, Inc. |
101 | | * |
102 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
103 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
104 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
105 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
106 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
107 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
108 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
109 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
110 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
111 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
112 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
113 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
114 | | * DAMAGE. |
115 | | * |
116 | | * ==================================================================== |
117 | | * |
118 | | * This software consists of voluntary contributions made by Vovida |
119 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
120 | | * Inc. For more information on Vovida Networks, Inc., please see |
121 | | * <http://www.vovida.org/>. |
122 | | * |
123 | | */ |