added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / hasher.hpp
blobf1dba7d1ad34b5dd513a3cdd2a6e06c57bf08875
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_HASHER_HPP_INCLUDED
34 #define TORRENT_HASHER_HPP_INCLUDED
36 #include <boost/cstdint.hpp>
38 #include "libtorrent/peer_id.hpp"
39 #include "libtorrent/config.hpp"
40 #include "libtorrent/assert.hpp"
41 #include "zlib.h"
43 #ifdef TORRENT_USE_OPENSSL
44 extern "C"
46 #include <openssl/sha.h>
48 #else
49 // from sha1.cpp
50 struct TORRENT_EXPORT SHA_CTX
52 boost::uint32_t state[5];
53 boost::uint32_t count[2];
54 boost::uint8_t buffer[64];
57 TORRENT_EXPORT void SHA1_Init(SHA_CTX* context);
58 TORRENT_EXPORT void SHA1_Update(SHA_CTX* context, boost::uint8_t const* data, boost::uint32_t len);
59 TORRENT_EXPORT void SHA1_Final(boost::uint8_t* digest, SHA_CTX* context);
61 #endif
63 namespace libtorrent
66 class adler32_crc
68 public:
69 adler32_crc(): m_adler(adler32(0, 0, 0)) {}
71 void update(const char* data, int len)
73 TORRENT_ASSERT(data != 0);
74 TORRENT_ASSERT(len > 0);
75 m_adler = adler32(m_adler, (const Bytef*)data, len);
77 unsigned long final() const { return m_adler; }
78 void reset() { m_adler = adler32(0, 0, 0); }
80 private:
82 unsigned long m_adler;
86 class hasher
88 public:
90 hasher() { SHA1_Init(&m_context); }
91 hasher(const char* data, int len)
93 SHA1_Init(&m_context);
94 TORRENT_ASSERT(data != 0);
95 TORRENT_ASSERT(len > 0);
96 SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
98 void update(const char* data, int len)
100 TORRENT_ASSERT(data != 0);
101 TORRENT_ASSERT(len > 0);
102 SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
105 sha1_hash final()
107 sha1_hash digest;
108 SHA1_Final(digest.begin(), &m_context);
109 return digest;
112 void reset() { SHA1_Init(&m_context); }
114 private:
116 SHA_CTX m_context;
121 #endif // TORRENT_HASHER_HPP_INCLUDED