Classified ads

Hash is class that carries 160bit digest. More...
#include <hash.h>
Public Member Functions  
Hash ()  
Hash (const quint32 aHash160bits[KNumberOfIntsInHash])  
Hash (const quint32 aBits1, const quint32 aBits2, const quint32 aBits3, const quint32 aBits4, const quint32 aBits5)  
Hash (const unsigned char aDigest[KNumberOfIntsInHash *4])  
Hash (const char *aHexString, int aHexStringLen)  
Hash (const QSslCertificate &aSslCert)  
void  fromString (const unsigned char *aBuf) 
QString  toString () const 
void  fromQVariant (const QVariant &aQVariantHashValue) 
QVariant  toQVariant () const 
void  calculate (const QByteArray &aBuf) 
bool  operator== (const Hash &aHashToCompare) const 
bool  operator!= (const Hash &aHashToCompare) const 
bool  operator< (const Hash &aHashToCompare) const 
bool  operator> (const Hash &aHashToCompare) const 
Hash &  operator= (const Hash &aHashToSubstitute) 
Hash  distanceFrom (const Hash &aHash) 
Hash  operator (const Hash &aToBeSubstracted) const 
Hash  operator+ (const Hash &aToBeAdded) const 
Public Attributes  
quint32  iHash160bits [KNumberOfIntsInHash] 
Static Public Attributes  
static const int  KNumberOfIntsInHash = 5 
Private Member Functions  
void  setFromCharArray (const unsigned char aDigest[KNumberOfIntsInHash *4]) 
Hash is class that carries 160bit digest.
Hash number class. This is usually an encryption key fingerprint or something returned by SHA1 function. The number that this class carries identifies an object inside classified ads, such objects as "classified ad", "user", "node", "attachment file" or anything that is present in the network. Here the hash is number between 02^160.
Hash::Hash  (  ) 
constructor. produces zerovalue hash.
Hash::Hash  (  const quint32  aHash160bits[KNumberOfIntsInHash]  ) 
constructor.
aHash160bits  is array of KNumberOfIntsInHash containing 32 bits each. The mostsignificant bits come in first, leastsignificant come in last. .. e.g. for constructing a hash that has value (decimal) 1 you say Hash(0,0,0,0,1). 
Hash::Hash  (  const quint32  aBits1, 
const quint32  aBits2,  
const quint32  aBits3,  
const quint32  aBits4,  
const quint32  aBits5  
) 
constructor.
aBits1  most significant bits 
aBits2  quite significant bits 
aBits3  somewhat significant bits 
aBits4  less significant bits 
aBits5  least significant bits 
Hash::Hash  (  const unsigned char  aDigest[KNumberOfIntsInHash *4]  ) 
constructor.
aDigest  160 bits in array of unsigned 8bit octets. Most significant bits first. If you take SHA1 function of openssl library, this is the format the method returns. 
Hash::Hash  (  const char *  aHexString, 
int  aHexStringLen  
) 
constructor that takes the hash in ascii string similar that printf("X... produces.
aHexString  is the string containing hash value 
aHexStringLen  is length of said string. if this param does not have value of 40 then a null hash will be instantiated. Purpose of this parameter is mainly to clearly distinguis this constuctor from the one that has single "unsigned char *" param but expects different length for the string 
Hash::Hash  (  const QSslCertificate &  aSslCert  ) 
constructor
aSslCert  is ssl certificate. Its SHA1 digest is used to initialize value of this hash 
void Hash::calculate  (  const QByteArray &  aBuf  ) 
calculates SHA1 hash of data and sets value of this class instance to be the result
Method for calculating distance between 2 hashes.
Obligatory J.R.R. Tolkien quote belongs here.
In this program distance is defined in "number of rings to rule them all" meaning that 1) hash values roll over e.g. distance between Hash(0) and Hash(max value) is 1. Distance between Hash(1) and Hash(max value) is 2. Distance between Hash(1) and Hash(5) is 4. As is to be expected. 2) We may invent many rings to address same objects because from articles (for instance) we can record the article content hash, pgpkey fingerprint of the sender, node used to send, hash of the forum containing the article etc. and in different situations it may be useful to search through different ring. This method distanceFrom tells how far away the number we're seeking for is and notable thing here is that number may roll over when going via path of shortest distance.
aHash  is the number from which we calculate distance 
void Hash::fromQVariant  (  const QVariant &  aQVariantHashValue  ) 
Sets hash value from QVariant produced by toQVariant() method of this same class.
void Hash::fromString  (  const unsigned char *  aBuf  ) 
Sets hash value from string produced by toString() method of this same class.
bool Hash::operator!=  (  const Hash &  aHashToCompare  )  const 
Mankinds greatest invention ever follows: bignum operations for small bignums.
Subtraction operator. As our hash if you think of it as a number, is unsigned, this operator will definitely return undefinedkindofcrap if you try to subtract bigger number from a smaller one. Something you'll get but it can be anything.
Other way around it seems to do something.
bool Hash::operator<  (  const Hash &  aHashToCompare  )  const 
substitution too, it might make code more readable
bool Hash::operator==  (  const Hash &  aHashToCompare  )  const 
Comparison operator enabling us to use sort operations
bool Hash::operator>  (  const Hash &  aHashToCompare  )  const 

private 
QVariant Hash::toQVariant  (  )  const 
Returns the hash value as a QVariant
QString Hash::toString  (  )  const 
Returns the hash value as a string.
quint32 Hash::iHash160bits[KNumberOfIntsInHash] 
this public array carries the calculated (SHA1) hash. there is 5*32 bits and in db too we're supposed to use 5 columns for storing a hash ; we can still compare and do ordering if we decide that loworder bits go into iHash160bits[0] and the highest into iHash160bits[4].
In 64bit boxen we could fit the bits into 64bit integers and have this thing working a bit faster  hopefully this thing will still work fast enough to work also in lesspowerful 32bit architectures for which we don't here want to invent a separate implementation.

static 
This constant KNumberOfIntsInHash defines size of our hash ; we take this many unsigned ints each 32bits in size so with value 5 w get 5*32=160.