⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmhash.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
字号:
/*****************************************************************************/
/*                             SOURCE FILE                                   */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description: Implementation of the SuperHash Class

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: 2 $ : $Date: 12/08/97 2:32p $";
/*****************************************************************************/



#include "../../../stdafx.h"
#include "VMHash.h"



/*****************************************************************************/
/*
     FUNCTION NAME: HashKeyGenerator

       DESCRIPTION:	Every bit of the key affects every bit of the return value.  
                    Every 1-bit and 2-bit delta achieves avalanche. About 6*len+35 
                    instructions. The best hash table sizes are powers of 2. There 
                    is no need to do mod a prime (mod is sooo slow!). If you need 
                    less than 32 bits, use a bitmask. For example, if you need only 
                    10 bits, do h = (h & hashmask(10)); In which case, the hash 
                    table should have hashsize(10) elements. If you are hashing n 
                    strings (ub1 **)k, do it like this: for (i=0, h=0; i<n; ++i) 
                    h = hash( k[i], len[i], h);

                    Use for hash table lookup, or anything where one collision in 
                    2^^32 is acceptable.  
                    Do NOT use for cryptographic purposes.

             INPUT:      k --  the key (the unaligned variable-length array 
                               of bytes)
                    length -- the length of the key, counting by bytes
                   initval -- the previous hash, or any 4-byte value

           RETURNS:	Returns a 32-bit value
*/
UINT VMSuperHash::HashKeyGenerator( const char* k, ub4 length, ub4 initval )
{
   register ub4 a,b,c,len;
   static ub4 prevhash=1;

   if(!length)
		length = strlen(k);

   if(!initval)
	   initval = 1;
   
   // Set up the internal state
   len = length;
   a = b = 0x9e3779b9;  // the golden ratio; an arbitrary value
   c = initval;         // the previous hash value 

   // handle most of the key 
   //
   while (len >= 12)
   {
      a += (k[0] +((ub4)k[1]<<8) +((ub4)k[2]<<16) +((ub4)k[3]<<24));
      b += (k[4] +((ub4)k[5]<<8) +((ub4)k[6]<<16) +((ub4)k[7]<<24));
      c += (k[8] +((ub4)k[9]<<8) +((ub4)k[10]<<16)+((ub4)k[11]<<24));
      mix(a,b,c);
      k += 12; len -= 12;
   }

   // handle the last 11 bytes 
   //
   c += length;
   switch(len)              
   {  // By design all the case statements fall through 
      case 11: c+=((ub4)k[10]<<24);
      case 10: c+=((ub4)k[9]<<16);
      case 9 : c+=((ub4)k[8]<<8);
      // the first byte of c is reserved for the length 
      case 8 : b+=((ub4)k[7]<<24);
      case 7 : b+=((ub4)k[6]<<16);
      case 6 : b+=((ub4)k[5]<<8);
      case 5 : b+=k[4];
      case 4 : a+=((ub4)k[3]<<24);
      case 3 : a+=((ub4)k[2]<<16);
      case 2 : a+=((ub4)k[1]<<8);
      case 1 : a+=k[0];
      // case 0: nothing left to add 
   }
   mix(a,b,c);

   prevhash = c;

   return c;
}
/*	end of function "HashKeyGenerator" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: HashString

       DESCRIPTION:	Accepts a username and a password, concats them into a
                    single string then hashes that string

             INPUT: 

           RETURNS:	the hash value for the combined username password
*/
UINT VMSuperHash::HashString( const char* pchToHash )
{
  return HashKeyGenerator( pchToHash, strlen( pchToHash ) );
}
/*	end of function "HashString" */
/*****************************************************************************/





/*****************************************************************************/
/* Check-in history */
/*
*$Log:   $
*/
/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -