Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Hash function in c language
Hash function in c language
Hash, generally translated as "hash" or directly transliterated as "hash", is to convert an input of any length (also known as the previous image) into an output of a fixed length through a hash algorithm, and the output is a hash value. This transformation is a kind of compressed mapping, that is, the space of hash value is usually much smaller than that of input, and different inputs may be hashed into the same output, so it is impossible to uniquely determine the input value from hash value. Simply put, it is the function of compressing messages of any length into message digests of fixed length.

Hash algorithm is mainly used for encryption algorithm in the field of information security. It converts some information with different lengths into 128 bits of gibberish, which is called hash value. In other words, hashing is to find the mapping relationship between data content and data storage address. The application of hash algorithm in information security is mainly reflected in the following three aspects: file verification, digital signature and authentication protocol.

Program realization

//Description: The application goal of Hash function in programming is to map an object to an object through some transformation mechanism.

An integer value of type size _ t (that is, an unsigned long integer).

//Hash functions are mainly used in hash tables (widely used), passwords and other fields.

//Implementation description:

//( 1), where function objects and generic technology are used, making it applicable to all types of objects (keywords).

//(2), commonly used types have corresponding specialization, such as string, char*, various shaping, etc.

//(3), the version is extensible. If there is a special demand for a certain type, specialization can be realized later.

//(4), the following implementation is generally placed in the header file, and anyone who contains it can use the hash function object.

//-to achieve.

# include & lt string & gt

Use STD::string;;

inline size _ thash _ str(const char * s)

{

Unsigned long integer RES = 0;;

for(; * s; ++s)

RES = 5 * RES+* s;

returnsize _ t(RES);

}

Template & lt class key>

Structural hash

{

size _ top operator()(const Key & amp; K) constant;

};

//General objects, such as vector.

Template & lt class key>

size _ thash & ltKey & gt* operator()(const Key & amp; K) constant

{

size _ tres = 0;

size _ tlen = sizeof(Key);

const char * p = reinterpret _ cast & lt; const char * & gt(& ampk);

while (len -)

{

RES =(RES & lt; & lt 1)^*p++;

}

Return to res

}

//specialization

Template & lt& gt

Size _ thash & lt string & gt* operator () (constant string & ampstr) constant.

{

Returns hash _ str (str.c _ str ());

}

typedef char * PChar

Template & lt& gt

size _ thash & ltPChar & gt* operator()(const PChar & amp; S) constant

{

Returns a hash string;

}

typedef const char * PCChar

Template & lt& gt

size _ thash & ltPCChar & gt* operator()(const PCChar & amp; S) constant

{

Returns a hash string;

}

Template & lt > size _ thash & lt char & gt * operator () (constchar &: x)const { return x; }

Template & lt size _ thash & lt unsigned character & gt* operator () (constant unsigned character & ampx)const {return x;; }

Template & lt size _ thash & lt signed character & gt * operator () (constsigned char &: x)const { return x; }

Template & lt > size _ thash & lt short & gt * operator () (constshort &: x)const { return x; }

Template & lt size _ thash & lt unsigned short integer & gt* operator()(const unsigned short & ampx)const {return x;; }

Template & lt > size _ thash & lt > * operator () (constint &: x)const { return x; }

Template & lt size _ thash & lt unsigned integer & gt * operator () (constunsigned int&: x)const { return x; }

Template & lt > size _ thash & lt long & gt * operator () (constlong &: x)const { return x; }

Template & lt size _ thash & lt unsigned long integer & gt * operator () (constunsigned long &: x)const { return x; }

//Instructions for use:

//

//( 1), first of all, because it is generic, you need to add keyword types.

//

//(2) Secondly, there should be a function object, which can be temporary, local or global, as long as it is within the scope.

//

//(3), the application function object acts on the corresponding type of object.

//-Examples of using hash functions.

# include & ltiostream & gt

# include & ltvector & gt

# include & lt string & gt

Use namespace std

int main()

{

Vector & lt string & gtvstr (2);

vstr[0]= " sjw ";

vstr[ 1]= " sun INF ";

Hash & lt string & gtstrhash// local function object

Cout & lt& lt hash value: "<< strhash (vstr [0]) < & ltendl

Cout & lt& lt hash value: "<< strhash (vstr [1]) < & ltendl

Cout & lt& lt hash value: "< hash & lt vector & lt string & gt> () (vstr) < & ltendl

Cout & lt& lt hash value: "<& lt hash & lt () (100) < & ltendl// hash & ltint & gt () temporary function object

Returns 0;

}