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

📄 toeplitz_hash.c

📁 toeplitz hash算法实现
💻 C
字号:
#include <sys/types.h>
#include <sys/socket.h>

#include <arpa/inet.h>
#include <netinet/in.h>

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

/* toeplitz_key: 16 bytes for IPV4, 40 bytes for IPV6 */

static uint8_t toeplitz_key[] = {
0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2,
0x41, 0x67, 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0,
0xd0, 0xca, 0x2b, 0xcb, 0xae, 0x7b, 0x30, 0xb4,
0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30, 0xf2, 0x0c,
0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa
};

uint32_t toeplitz_hash(uint8_t *buf, uint32_t len)
{
	uint32_t res=0;
	uint8_t bdata=0;
	uint32_t key = 0;
	int i,j;

	key = ntohl(*(uint32_t *)toeplitz_key);

	printf("key: 0x%x\n",key);

	for (i=0;i<len;i++){
		bdata = buf[i];
		for (j=0;j<8;j++){
			if((bdata << j)& (0x80))
				res ^= key;
			key =  (key <<1) | ((toeplitz_key[i+4] >> (7-j)) & 0x1);
			}
		}
	return res;

}
//161.142.100.80 :1766	66.9.149.187 :2794
int main(void)
{
	uint8_t input[13] = {0x42,0x09,0x95,0xbb,0xA1,0x8E,0x64,0x50,0x0a,0xea,0x06,0xe6};
	printf("res = 0x%x\n",toeplitz_hash(input, 8));
	return 0;

	

}

⌨️ 快捷键说明

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