File indexing completed on 2025-01-05 04:37:11

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef DHTKEY_H
0007 #define DHTKEY_H
0008 
0009 #include <QByteArray>
0010 #include <ktorrent_export.h>
0011 #include <util/sha1hash.h>
0012 
0013 namespace dht
0014 {
0015 /**
0016  * @author Joris Guisson
0017  * @brief Key in the distributed hash table
0018  *
0019  * Key's in the distributed hash table are just SHA-1 hashes.
0020  * Key provides all necessary operators to be used as a value.
0021  */
0022 class KTORRENT_EXPORT Key : public bt::SHA1Hash
0023 {
0024 public:
0025     /**
0026      * Constructor, sets key to 0.
0027      */
0028     Key();
0029 
0030     /**
0031      * Copy constructor. Seeing that Key doesn't add any data
0032      * we just pass a SHA1Hash, Key's are automatically covered by this
0033      * @param k Hash to copy
0034      */
0035     Key(const bt::SHA1Hash &k);
0036 
0037     /**
0038      * Make a key out of a bytearray
0039      * @param ba The QByteArray
0040      */
0041     Key(const QByteArray &ba);
0042 
0043     /**
0044      * Make a key out of a 20 byte array.
0045      * @param d The array
0046      */
0047     Key(const bt::Uint8 *d);
0048 
0049     /// Destructor.
0050     ~Key() override;
0051 
0052     /**
0053      * Create a random key.
0054      * @return A random Key
0055      */
0056     static Key random();
0057 
0058     /// Get the minimum key (all zeros)
0059     static Key min();
0060 
0061     /// Get the maximum key (all FF)
0062     static Key max();
0063 
0064     /**
0065      * Equality operator.
0066      * @param other The key to compare
0067      * @return true if this key is equal to other
0068      */
0069     bool operator==(const Key &other) const;
0070 
0071     /**
0072      * Inequality operator.
0073      * @param other The key to compare
0074      * @return true if this key is not equal to other
0075      */
0076     bool operator!=(const Key &other) const;
0077 
0078     /**
0079      * Smaller then operator.
0080      * @param other The key to compare
0081      * @return rue if this key is smaller then other
0082      */
0083     bool operator<(const Key &other) const;
0084 
0085     /**
0086      * Smaller then or equal operator.
0087      * @param other The key to compare
0088      * @return rue if this key is smaller then or equal to other
0089      */
0090     bool operator<=(const Key &other) const;
0091 
0092     /**
0093      * Greater then operator.
0094      * @param other The key to compare
0095      * @return rue if this key is greater then other
0096      */
0097     bool operator>(const Key &other) const;
0098 
0099     /**
0100      * Greater then or equal operator.
0101      * @param other The key to compare
0102      * @return rue if this key is greater then or equal to other
0103      */
0104     bool operator>=(const Key &other) const;
0105 
0106     /**
0107      * Divide by a number operator
0108      */
0109     Key operator/(int value) const;
0110 
0111     /**
0112      * Addition for keys
0113      * @param a The first key
0114      * @param b The second key
0115      */
0116     friend KTORRENT_EXPORT Key operator+(const Key &a, const Key &b);
0117 
0118     /**
0119      * Subtraction for keys
0120      * @param a The first key
0121      * @param b The second key
0122      */
0123     friend KTORRENT_EXPORT Key operator-(const Key &a, const Key &b);
0124 
0125     /**
0126      * Addition for key and a value
0127      * @param a The key
0128      * @param b The value
0129      */
0130     friend KTORRENT_EXPORT Key operator+(const Key &a, bt::Uint8 value);
0131 
0132     /**
0133      * The distance of two keys is the keys xor together.
0134      * @param a The first key
0135      * @param b The second key
0136      * @return a xor b
0137      */
0138     static Key distance(const Key &a, const Key &b);
0139 
0140     /**
0141      * Calculate the middle between two keys.
0142      * @param a The first key
0143      * @param b The second key
0144      * @return The middle
0145      */
0146     static Key mid(const Key &a, const Key &b);
0147 };
0148 
0149 }
0150 
0151 #endif