找到一个hash函数

来源:互联网 发布:nodejs如何运行js文件 编辑:程序博客网 时间:2024/04/26 13:47

前言

在lua5.3.3代码中看到一个hash函数,摘出来玩玩。

试验

// hw.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <windows.h>#include <stdlib.h>#include <stdio.h>#include <math.h>#include <time.h>unsigned int hashProc(const char* pcSrc, size_t l, unsigned int seed){    unsigned int h = seed ^(unsigned int)l;    size_t step = (l >> 5) + 1;    for (; l >= step; l -= step) {        h ^= ((h << 5) + (h >> 2) + (BYTE)pcSrc[l - 1]);    }    return h;}int main(int argc, char* argv[]){    srand((UINT)time(NULL));    const char* pcMsg = "string for calc hash";    UINT iHash = hashProc(pcMsg, strlen(pcMsg), rand());    printf("hash value = 0x%X, str = [%s]\r\n", iHash, pcMsg);    system("pause");    /** run result    hash value = 0xEE996D0, str = [string for calc hash]    请按任意键继续. . .    */    return 0;}
0 0
原创粉丝点击