1 /*
2 
3 Copyright (c) 2003-2018, Arvid Norberg
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 
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.
18 
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.
30 
31 */
32 
33 #ifndef TORRENT_HASHER_HPP_INCLUDED
34 #define TORRENT_HASHER_HPP_INCLUDED
35 
36 #include "libtorrent/config.hpp"
37 #include "libtorrent/sha1_hash.hpp"
38 #include "libtorrent/span.hpp"
39 
40 #include <cstdint>
41 
42 #include "libtorrent/aux_/disable_warnings_push.hpp"
43 #ifdef TORRENT_USE_LIBGCRYPT
44 #include <gcrypt.h>
45 
46 #elif TORRENT_USE_COMMONCRYPTO
47 #include <CommonCrypto/CommonDigest.h>
48 
49 #elif TORRENT_USE_CRYPTOAPI
50 #include "libtorrent/aux_/win_crypto_provider.hpp"
51 
52 #elif defined TORRENT_USE_LIBCRYPTO
53 
54 extern "C" {
55 #include <openssl/sha.h>
56 }
57 
58 #else
59 #include "libtorrent/sha1.hpp"
60 #endif
61 #include "libtorrent/aux_/disable_warnings_pop.hpp"
62 
63 namespace libtorrent {
64 
65 	// this is a SHA-1 hash class.
66 	//
67 	// You use it by first instantiating it, then call ``update()`` to feed it
68 	// with data. i.e. you don't have to keep the entire buffer of which you want to
69 	// create the hash in memory. You can feed the hasher parts of it at a time. When
70 	// You have fed the hasher with all the data, you call ``final()`` and it
71 	// will return the sha1-hash of the data.
72 	//
73 	// The constructor that takes a ``char const*`` and an integer will construct the
74 	// sha1 context and feed it the data passed in.
75 	//
76 	// If you want to reuse the hasher object once you have created a hash, you have to
77 	// call ``reset()`` to reinitialize it.
78 	//
79 	// The built-in software version of sha1-algorithm was implemented
80 	// by Steve Reid and released as public domain.
81 	// For more info, see ``src/sha1.cpp``.
82 	class TORRENT_EXPORT hasher
83 	{
84 	public:
85 
86 		hasher();
87 
88 		// this is the same as default constructing followed by a call to
89 		// ``update(data, len)``.
90 		hasher(char const* data, int len);
91 		explicit hasher(span<char const> data);
92 		hasher(hasher const&);
93 		hasher& operator=(hasher const&) &;
94 
95 		// append the following bytes to what is being hashed
96 		hasher& update(span<char const> data);
97 		hasher& update(char const* data, int len);
98 
99 		// returns the SHA-1 digest of the buffers previously passed to
100 		// update() and the hasher constructor.
101 		sha1_hash final();
102 
103 		// restore the hasher state to be as if the hasher has just been
104 		// default constructed.
105 		void reset();
106 
107 		// hidden
108 		~hasher();
109 
110 	private:
111 
112 #ifdef TORRENT_USE_LIBGCRYPT
113 		gcry_md_hd_t m_context;
114 #elif TORRENT_USE_COMMONCRYPTO
115 		CC_SHA1_CTX m_context;
116 #elif TORRENT_USE_CRYPTOAPI
117 		aux::crypt_hash<CALG_SHA1, PROV_RSA_FULL> m_context;
118 #elif defined TORRENT_USE_LIBCRYPTO
119 		SHA_CTX m_context;
120 #else
121 		sha1_ctx m_context;
122 #endif
123 	};
124 
125 }
126 
127 #endif // TORRENT_HASHER_HPP_INCLUDED
128