Classified ads
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | List of all members
Hash Class Reference

Hash is class that carries 160-bit 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
 
Hashoperator= (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])
 

Detailed Description

Hash is class that carries 160-bit 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 0-2^160.

Constructor & Destructor Documentation

Hash::Hash ( )

constructor. produces zero-value hash.

Hash::Hash ( const quint32  aHash160bits[KNumberOfIntsInHash])

constructor.

Parameters
aHash160bitsis array of KNumberOfIntsInHash containing 32 bits each. The most-significant bits come in first, least-significant 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.

Parameters
aBits1most significant bits
aBits2quite significant bits
aBits3somewhat significant bits
aBits4less significant bits
aBits5least significant bits
Hash::Hash ( const unsigned char  aDigest[KNumberOfIntsInHash *4])

constructor.

Parameters
aDigest160 bits in array of unsigned 8-bit 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.

Parameters
aHexStringis the string containing hash value
aHexStringLenis 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

Parameters
aSslCertis ssl certificate. Its SHA1 digest is used to initialize value of this hash

Member Function Documentation

void Hash::calculate ( const QByteArray &  aBuf)

calculates SHA1 hash of data and sets value of this class instance to be the result

Hash Hash::distanceFrom ( const Hash aHash)

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, pgp-key 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.

Parameters
aHashis the number from which we calculate distance
Returns
return value is also a hash, that is, a number
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
Hash Hash::operator+ ( const Hash aToBeAdded) const

addition operation for integers of 160 bits

Hash Hash::operator- ( const Hash aToBeSubstracted) const

Man-kinds 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 undefined-kind-of-crap 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
Hash& Hash::operator= ( const Hash aHashToSubstitute)

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
void Hash::setFromCharArray ( const unsigned char  aDigest[KNumberOfIntsInHash *4])
private
QVariant Hash::toQVariant ( ) const

Returns the hash value as a QVariant

QString Hash::toString ( ) const

Returns the hash value as a string.

Member Data Documentation

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 low-order bits go into iHash160bits[0] and the highest into iHash160bits[4].

In 64-bit boxen we could fit the bits into 64-bit integers and have this thing working a bit faster - hopefully this thing will still work fast enough to work also in less-powerful 32-bit architectures for which we don't here want to invent a separate implementation.

const int Hash::KNumberOfIntsInHash = 5
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.


The documentation for this class was generated from the following file: