summaryrefslogtreecommitdiffstats
path: root/libktorrent/util/sha1hash.h
blob: a831d2d0cb93ac758ac75f5d2576216b7826e83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   [email protected]                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
 ***************************************************************************/
#ifndef BTSHA1HASH_H
#define BTSHA1HASH_H

#include <qcstring.h> 
#include "constants.h"

class QString;

namespace bt
{
	class Log;

	/**
	 * @author Joris Guisson
	 * @brief Stores a SHA1 hash
	 *
	 * This class keeps track of a SHA1 hash. A SHA1 hash is a 20 byte
	 * array of bytes.
	*/
	class SHA1Hash
	{
	protected:
		Uint8 hash[20];
	public:
		/**
		 * Constructor, sets every byte in the hash to 0.
		 */
		SHA1Hash();
		
		/**
		 * Copy constructor.
		 * @param other Hash to copy
		 */
		SHA1Hash(const SHA1Hash & other);
		
		/**
		 * Directly set the hash data.
		 * @param h The hash data must be 20 bytes large
		 */
		SHA1Hash(const Uint8* h);
		
		/**
		 * Destructor.
		 */
		virtual ~SHA1Hash();

		/// Get the idx'th byte of the hash.
		Uint8 operator [] (const Uint32 idx) const {return idx < 20 ? hash[idx] : 0;}
		
		/**
		 * Assignment operator.
		 * @param other Hash to copy
		 */
		SHA1Hash & operator = (const SHA1Hash & other);

		/**
		 * Test wether another hash is equal to this one.
		 * @param other The other hash
		 * @return true if equal, false otherwise
		 */
		bool operator == (const SHA1Hash & other) const;

		/**
		 * Test wether another hash is not equal to this one.
		 * @param other The other hash
		 * @return true if not equal, false otherwise
		 */
		bool operator != (const SHA1Hash & other) const {return !operator ==(other);}

		/**
		 * Generate an SHA1 hash from a bunch of data.
		 * @param data The data
		 * @param len Size in bytes of data
		 * @return The generated SHA1 hash
		 */
		static SHA1Hash generate(const Uint8* data,Uint32 len);

		/**
		 * Convert the hash to a printable string.
		 * @return The string
		 */
		QString toString() const;

		/**
		 * Convert the hash to a string, usable in http get requests.
		 * @return The string
		 */
		QString toURLString() const;
		
		/**
		 * Directly get pointer to the data.
		 * @return The data
		 */
		const Uint8* getData() const {return hash;}

		/**
		 * Function to print a SHA1Hash to the Log.
		 * @param out The Log
		 * @param h The hash
		 * @return out
		 */
		friend Log & operator << (Log & out,const SHA1Hash & h);
		
		
		/**
		 * XOR two SHA1Hashes
		 * @param a The first hash
		 * @param b The second
		 * @return a xor b
		 */
		friend SHA1Hash operator ^ (const SHA1Hash & a,const SHA1Hash & b);
		
		/**
		 * Function to compare 2 hashes
		 * @param a The first hash
		 * @param h The second hash
		 * @return wether a is smaller then b
		 */
		friend bool operator < (const SHA1Hash & a,const SHA1Hash & b);
		
		/**
		 * Convert the hash to a byte array.
		 */
		QByteArray toByteArray() const;
	};

}

#endif