00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef TORRENT_HASHER_HPP_INCLUDED
00034 #define TORRENT_HASHER_HPP_INCLUDED
00035
00036 #include <cassert>
00037 #include <boost/cstdint.hpp>
00038
00039 #include "libtorrent/peer_id.hpp"
00040 #include "libtorrent/config.hpp"
00041
00042
00043 struct TORRENT_EXPORT SHA1_CTX
00044 {
00045 boost::uint32_t state[5];
00046 boost::uint32_t count[2];
00047 boost::uint8_t buffer[64];
00048 };
00049
00050 TORRENT_EXPORT void SHA1Init(SHA1_CTX* context);
00051 TORRENT_EXPORT void SHA1Update(SHA1_CTX* context, boost::uint8_t const* data, boost::uint32_t len);
00052 TORRENT_EXPORT void SHA1Final(SHA1_CTX* context, boost::uint8_t* digest);
00053
00054 extern "C"
00055 {
00056
00057 unsigned long adler32(unsigned long adler, const char* data, unsigned int len);
00058 }
00059
00060 namespace libtorrent
00061 {
00062
00063 class adler32_crc
00064 {
00065 public:
00066 adler32_crc(): m_adler(adler32(0, 0, 0)) {}
00067
00068 void update(const char* data, int len)
00069 {
00070 assert(data != 0);
00071 assert(len > 0);
00072 m_adler = adler32(m_adler, data, len);
00073 }
00074 unsigned long final() const { return m_adler; }
00075 void reset() { m_adler = adler32(0, 0, 0); }
00076
00077 private:
00078
00079 unsigned long m_adler;
00080
00081 };
00082
00083 class hasher
00084 {
00085 public:
00086
00087 hasher() { SHA1Init(&m_context); }
00088 hasher(const char* data, int len)
00089 {
00090 SHA1Init(&m_context);
00091 assert(data != 0);
00092 assert(len > 0);
00093 SHA1Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
00094 }
00095 void update(const char* data, int len)
00096 {
00097 assert(data != 0);
00098 assert(len > 0);
00099 SHA1Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
00100 }
00101
00102 sha1_hash final()
00103 {
00104 sha1_hash digest;
00105 SHA1Final(&m_context, digest.begin());
00106 return digest;
00107 }
00108
00109 void reset() { SHA1Init(&m_context); }
00110
00111 private:
00112
00113 SHA1_CTX m_context;
00114
00115 };
00116 }
00117
00118 #endif // TORRENT_HASHER_HPP_INCLUDED