File indexing completed on 2024-06-02 05:05:38
0001 /* 0002 SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #ifndef BTSHA1HASH_H 0007 #define BTSHA1HASH_H 0008 0009 #include "constants.h" 0010 #include <QByteArray> 0011 #include <ktorrent_export.h> 0012 0013 class QString; 0014 0015 namespace bt 0016 { 0017 class Log; 0018 0019 /** 0020 * @author Joris Guisson 0021 * @brief Stores a SHA1 hash 0022 * 0023 * This class keeps track of a SHA1 hash. A SHA1 hash is a 20 byte 0024 * array of bytes. 0025 */ 0026 class KTORRENT_EXPORT SHA1Hash 0027 { 0028 protected: 0029 Uint32 hash[5]; 0030 0031 public: 0032 /** 0033 * Constructor, sets every byte in the hash to 0. 0034 */ 0035 SHA1Hash(); 0036 0037 /** 0038 * Copy constructor. 0039 * @param other Hash to copy 0040 */ 0041 SHA1Hash(const SHA1Hash &other); 0042 0043 /** 0044 * Directly set the hash data. 0045 * @param h The hash data must be 20 bytes large 0046 */ 0047 SHA1Hash(const Uint8 *h); 0048 0049 /** 0050 * Destructor. 0051 */ 0052 virtual ~SHA1Hash(); 0053 0054 /// Get the idx'th byte of the hash. 0055 Uint8 operator[](const Uint32 idx) const 0056 { 0057 return idx < 20 ? hash[idx] : 0; 0058 } 0059 0060 /** 0061 * Assignment operator. 0062 * @param other Hash to copy 0063 */ 0064 SHA1Hash &operator=(const SHA1Hash &other); 0065 0066 /** 0067 * Test whether another hash is equal to this one. 0068 * @param other The other hash 0069 * @return true if equal, false otherwise 0070 */ 0071 bool operator==(const SHA1Hash &other) const; 0072 0073 /** 0074 * Test whether another hash is not equal to this one. 0075 * @param other The other hash 0076 * @return true if not equal, false otherwise 0077 */ 0078 bool operator!=(const SHA1Hash &other) const 0079 { 0080 return !operator==(other); 0081 } 0082 0083 /** 0084 * Generate an SHA1 hash from a bunch of data. 0085 * @param data The data 0086 * @param len Size in bytes of data 0087 * @return The generated SHA1 hash 0088 */ 0089 static SHA1Hash generate(const Uint8 *data, Uint32 len); 0090 0091 /** 0092 * Convert the hash to a printable string. 0093 * @return The string 0094 */ 0095 QString toString() const; 0096 0097 /** 0098 * Convert the hash to a string, usable in http get requests. 0099 * @return The string 0100 */ 0101 QString toURLString() const; 0102 0103 /** 0104 * Directly get pointer to the data. 0105 * @return The data 0106 */ 0107 const Uint8 *getData() const 0108 { 0109 return (Uint8 *)hash; 0110 } 0111 0112 /** 0113 * Function to print a SHA1Hash to the Log. 0114 * @param out The Log 0115 * @param h The hash 0116 * @return out 0117 */ 0118 KTORRENT_EXPORT friend Log &operator<<(Log &out, const SHA1Hash &h); 0119 0120 /** 0121 * XOR two SHA1Hashes 0122 * @param a The first hash 0123 * @param b The second 0124 * @return a xor b 0125 */ 0126 KTORRENT_EXPORT friend SHA1Hash operator^(const SHA1Hash &a, const SHA1Hash &b); 0127 0128 /** 0129 * Function to compare 2 hashes 0130 * @param a The first hash 0131 * @param h The second hash 0132 * @return whether a is smaller then b 0133 */ 0134 KTORRENT_EXPORT friend bool operator<(const SHA1Hash &a, const SHA1Hash &b); 0135 0136 /** 0137 * Function to support the use of SHA1Hash as QHash keys 0138 * @param key SHA1Hash used to compute a hash key 0139 * @return hash key 0140 */ 0141 KTORRENT_EXPORT friend size_t qHash(const SHA1Hash &key, size_t seed) noexcept; 0142 0143 /** 0144 * Convert the hash to a byte array. 0145 */ 0146 QByteArray toByteArray() const; 0147 }; 0148 0149 } 0150 0151 #endif