ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

国产哈希算法WJLHA(四):基于分段迭代、权系数轮函数和混沌映射可自定义哈希值长度的WJLHA3开源

2021-06-02 09:31:14  阅读:245  来源: 互联网

标签:coder 自定义 int unsigned char Length WJLHA3 哈希 HashValueBuFF


文件名:WJLHA3.h

/**************************************************************************
 * Based on Weighted Probability Model Code(Jielin Code) And <Hash Algorithm with Adaptive Hash Value Length Based on Weighted Probability Model>
 * @author JieLin Wang(China)
 * @copyright JieLin Wang 2021-05-26
 * @Version 3.0.0
 * @email 254908447@qq.com
 */
#ifndef _WJLHA3_h
#define _WJLHA3_h
#include "math.h"
#include "memory.h"
#include "stdlib.h"
#include <stdio.h>
/*************************************************************************************
the main Wang Jie lin hash function
InBytesBuFF: the first address of bytes cache waiting to be encoding.
InBytesBuFF_Length: the bytes cache length.
keytBuFF: the keyt bytes for encoding.
keytBuFF_Length:the keytBuFF's byte length.
HashValueBuFF:the hash value Byte Buff.
HashValueBuFF_Length: the hash value's byte length, by user-defined or system-defined.
*************************************************************************************/
void WJLHA3(unsigned char *InBytesBuFF, int InBytesBuFF_Length, unsigned char *keytBuFF, int keytBuFF_Length, unsigned char *HashValueBuFF, int HashValueBuFF_Length);

#endif

文件名:WJLHA3.c

#include "WJLHA3.h"
extern const unsigned char RandomOfUCharTable[256];
extern const unsigned char RandomOfbitTable[256];
extern const unsigned char bitOfByteTable[256][8];
extern const unsigned int Ps = 256;// Customizable
// Jielincode Encoding Struct
typedef struct
{
	unsigned int RC_CODE_BITS;
	unsigned int RC_SHIFT_BITS;
	unsigned int RC_MAX_RANGE;
	unsigned int RC_MIN_RANGE;
	double p0;
	double p1;
	double JIELINCOE;
	unsigned int EFLow;
	unsigned int EFRange;
	unsigned int EFDigits;
	unsigned int EFFollow;
	unsigned int EOut_buff_loop;
	unsigned char *HashValueBuFF;
	int HashValueBuFF_Length;
}WJLCoder;
// Random - generated byte 2D table,Customizable tables
const unsigned char RandomOfUCharTable[256] =	{
	143,254,212,99,131,164,146,48,66,137,58,219,171,251,193,50,
	94,43,86,32,68,48,217,36,242,56,183,150,73,48,82,213,
	143,12,156,62,127,28,182,213,23,235,49,190,164,108,121,92,
	178,59,73,121,131,85,96,153,126,112,204,252,183,237,213,249,
	220,75,88,129,35,170,182,14,162,35,179,67,94,67,132,38,
	122,96,58,232,187,17,3,218,35,105,75,82,137,114,160,2,
	140,157,84,106,186,209,79,221,213,41,37,228,47,74,141,115,
	189,203,124,9,33,28,206,90,7,206,30,230,136,89,149,128,
	221,216,214,146,133,135,208,157,79,85,45,84,1,23,95,97,
	62,148,184,231,37,249,204,152,146,227,97,35,113,170,167,197,
	107,63,233,178,99,118,249,249,155,99,152,204,23,223,214,222,
	199,86,226,230,84,90,249,64,214,173,94,242,209,202,229,147,
	146,55,115,158,202,125,19,58,180,187,127,151,24,135,11,125,
	186,2,102,215,97,187,126,146,39,91,208,115,54,174,197,18,
	41,69,133,125,6,111,70,193,192,249,161,224,78,238,178,138,
	218,250,158,88,134,71,46,8,248,233,103,224,141,74,174,5
};
// Random - generated bit 2D table, Customizable tables
const unsigned char RandomOfbitTable[256] =	{
	1,1,0,1,0,1,1,0,1,0,0,0,0,1,0,0,
	1,1,0,0,0,1,0,1,0,0,1,1,1,1,1,1,
	0,1,1,1,0,1,1,0,1,0,0,1,0,1,0,0,
	1,0,0,1,0,0,1,0,1,1,1,0,1,1,0,1,
	1,1,0,1,1,1,1,0,0,0,1,1,0,1,0,1,
	0,0,0,1,0,1,0,0,1,1,0,0,0,0,1,0,
	1,0,1,1,1,1,0,1,0,0,0,1,1,0,0,1,
	1,1,0,0,0,0,1,0,0,0,0,0,1,1,1,1,
	0,0,0,0,1,1,1,1,1,0,1,0,1,1,1,0,
	0,1,1,1,1,1,0,0,1,0,1,0,1,1,0,0,
	0,0,0,0,1,1,0,1,0,0,1,0,0,0,1,1,
	0,1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,
	0,1,1,0,0,0,1,1,0,0,1,1,0,0,0,0,
	1,1,1,0,0,0,0,0,1,0,1,1,0,0,0,1,
	0,0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,
	0,1,0,1,0,1,1,0,1,0,0,1,0,1,0,1
};
// Bit value at each position in each byte
const unsigned char bitOfByteTable[256][8] =	{
	{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,1},{0,0,0,0,0,0,1,0},{0,0,0,0,0,0,1,1},{0,0,0,0,0,1,0,0},{0,0,0,0,0,1,0,1},{0,0,0,0,0,1,1,0},{0,0,0,0,0,1,1,1},	//0~7
	{0,0,0,0,1,0,0,0},{0,0,0,0,1,0,0,1},{0,0,0,0,1,0,1,0},{0,0,0,0,1,0,1,1},{0,0,0,0,1,1,0,0},{0,0,0,0,1,1,0,1},{0,0,0,0,1,1,1,0},{0,0,0,0,1,1,1,1},	//8~15	
	{0,0,0,1,0,0,0,0},{0,0,0,1,0,0,0,1},{0,0,0,1,0,0,1,0},{0,0,0,1,0,0,1,1},{0,0,0,1,0,1,0,0},{0,0,0,1,0,1,0,1},{0,0,0,1,0,1,1,0},{0,0,0,1,0,1,1,1},	//16~23
	{0,0,0,1,1,0,0,0},{0,0,0,1,1,0,0,1},{0,0,0,1,1,0,1,0},{0,0,0,1,1,0,1,1},{0,0,0,1,1,1,0,0},{0,0,0,1,1,1,0,1},{0,0,0,1,1,1,1,0},{0,0,0,1,1,1,1,1},	//24~31
	{0,0,1,0,0,0,0,0},{0,0,1,0,0,0,0,1},{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,1},{0,0,1,0,0,1,0,0},{0,0,1,0,0,1,0,1},{0,0,1,0,0,1,1,0},{0,0,1,0,0,1,1,1},	//32~39
	{0,0,1,0,1,0,0,0},{0,0,1,0,1,0,0,1},{0,0,1,0,1,0,1,0},{0,0,1,0,1,0,1,1},{0,0,1,0,1,1,0,0},{0,0,1,0,1,1,0,1},{0,0,1,0,1,1,1,0},{0,0,1,0,1,1,1,1},	//40~47
	{0,0,1,1,0,0,0,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},{1,1,0,1,1,1,0,1},{1,1,0,1,1,1,1,0},{1,1,0,1,1,1,1,1},	//216~223
	{1,1,1,0,0,0,0,0},{1,1,1,0,0,0,0,1},{1,1,1,0,0,0,1,0},{1,1,1,0,0,0,1,1},{1,1,1,0,0,1,0,0},{1,1,1,0,0,1,0,1},{1,1,1,0,0,1,1,0},{1,1,1,0,0,1,1,1},	//224~231
	{1,1,1,0,1,0,0,0},{1,1,1,0,1,0,0,1},{1,1,1,0,1,0,1,0},{1,1,1,0,1,0,1,1},{1,1,1,0,1,1,0,0},{1,1,1,0,1,1,0,1},{1,1,1,0,1,1,1,0},{1,1,1,0,1,1,1,1},	//232~239
	{1,1,1,1,0,0,0,0},{1,1,1,1,0,0,0,1},{1,1,1,1,0,0,1,0},{1,1,1,1,0,0,1,1},{1,1,1,1,0,1,0,0},{1,1,1,1,0,1,0,1},{1,1,1,1,0,1,1,0},{1,1,1,1,0,1,1,1},	//240~247
	{1,1,1,1,1,0,0,0},{1,1,1,1,1,0,0,1},{1,1,1,1,1,0,1,0},{1,1,1,1,1,0,1,1},{1,1,1,1,1,1,0,0},{1,1,1,1,1,1,0,1},{1,1,1,1,1,1,1,0},{1,1,1,1,1,1,1,1}		//248~255
};
// dissection function,Cut the data into ⌈InBytesBuFF_Length/256⌉ segments
int DissectionTheDatas(int InBytesBuFF_Length)
{
	int paragraphs = InBytesBuFF_Length / Ps;
	if(InBytesBuFF_Length > paragraphs * Ps){
		return paragraphs + 1;
	}else{
		return paragraphs;
	}
}
// Query 'RandomOfbitTable' table function
unsigned char QuerybitTable(int index, unsigned char bitSymbol)
{
	return RandomOfbitTable[ (index + bitSymbol) % 256 ] ^ bitSymbol;
};
// Query 'RandomOfUCharTable' table function
double QueryUCharTable(int index, unsigned char bitSymbol, double r, unsigned char OneKeytByte)
{
	return r - (double)RandomOfUCharTable[ (index + bitSymbol) % 256 ]/(100000.0 + OneKeytByte);
};
// Obtain a sequence Y and obtain a weighted coefficient---JIELINCODE Coefficient
double TheCOEofY(WJLCoder *coder, int partIndex, int paragraphs, unsigned char *Y, int Y_len){
	unsigned char tmpBit, tmpByte = 0;
	int i, j, Count0 = 0;
	double H = 0;
	// Number of statistical symbols 0
	for(i = 0; i < Y_len; ++i){
		tmpByte = 0x00;
		for(j = 0; j < 8; ++ j) {
			tmpByte <<= 1;
			tmpBit = QuerybitTable((i * 8 + j) % 256, bitOfByteTable[(int)Y[i]][j]);
			if(tmpBit == 0) {
				Count0 ++;
			}else{
				tmpByte |= tmpBit;
			}
		}
		Y[i] = tmpByte;
	}
	// Get the probability p0 of symbol 0 and the probability p1 of symbol 1
	coder->p0 = (double)Count0 / (double)(Y_len * 8.0);
	coder->p1 = 1.0 - coder->p0;
	// Binary sequences of all 0 or all 1 need to be preprocessed
	if(coder->p0 == 0.0){
		coder->p0 = 0.0000000001;
		coder->p1 = 1.0 - coder->p0;
	}else if(coder->p1 == 0.0){
		coder->p1 = 0.0000000001;
		coder->p0 = 1.0 - coder->p1;
	}
	// get the standard information entropy
	H = -coder->p0 * (log(coder->p0)/log(2.0))- coder->p1 * (log(coder->p1)/log(2.0));
	// get the Jielin code coefficient
	return pow( 2.0, H - ( ((double)coder->HashValueBuFF_Length) / (double)Y_len ));
}
// Output bytes to cache and weighted encoding
void OutPutByte(WJLCoder *coder, unsigned char ucByte)
{
	coder->HashValueBuFF[ coder->EOut_buff_loop % coder->HashValueBuFF_Length ] = coder->HashValueBuFF[ coder->EOut_buff_loop % coder->HashValueBuFF_Length ] ^ (unsigned char)ucByte;
	coder->EOut_buff_loop = coder->EOut_buff_loop + 1;
}
// Encode by JielinCeo
void Encode(WJLCoder *coder, unsigned char symbol)
{	
	unsigned int High = 0,i = 0;
	if (1 == symbol){// the Symbol 1
		coder->EFLow = coder->EFLow +  (unsigned int)((double)coder->EFRange * coder->p0);
		coder->EFRange = (unsigned int)((double)coder->EFRange * coder->p1 * coder->JIELINCOE);
	}else{
		coder->EFRange = (unsigned int)((double)coder->EFRange * coder->p0 * coder->JIELINCOE);
	}
	while(coder->EFRange <= coder->RC_MIN_RANGE){
		High = coder->EFLow + coder->EFRange - 1;
		if(coder->EFFollow != 0) {
			if (High <= coder->RC_MAX_RANGE) {
				OutPutByte(coder, coder->EFDigits);
				for (i = 1; i <= coder->EFFollow - 1; ++i){
					OutPutByte(coder, 0xFF);
				}
				coder->EFFollow = 0;
				coder->EFLow = coder->EFLow + coder->RC_MAX_RANGE;
			} else if (coder->EFLow >= coder->RC_MAX_RANGE) {
				OutPutByte(coder, coder->EFDigits + 1);
				for (i = 1; i <= coder->EFFollow - 1; ++i){
					OutPutByte(coder, 0x00);
				}									
				coder->EFFollow = 0;
			} else {
				coder->EFFollow += 1;
				coder->EFLow = (coder->EFLow << 8) & (coder->RC_MAX_RANGE - 1);
				coder->EFRange = coder->EFRange << 8;
				continue;
			}
		}
		if  (((coder->EFLow ^ High) & (0xFF << coder->RC_SHIFT_BITS)) == 0) {
			OutPutByte(coder, (unsigned char)(coder->EFLow >> coder->RC_SHIFT_BITS));
		}else{
			coder->EFLow = coder->EFLow - coder->RC_MAX_RANGE;
			coder->EFDigits = coder->EFLow >> coder->RC_SHIFT_BITS;
			coder->EFFollow = 1;
		}
		coder->EFLow = ( ( (coder->EFLow << 8) & (coder->RC_MAX_RANGE - 1) ) | (coder->EFLow & coder->RC_MAX_RANGE) );
		coder->EFRange = coder->EFRange << 8;
	}
}
// Finish Encode by JielinCeo
void FinishEncode(WJLCoder *coder)
{
	int n = 0;
	if (coder->EFFollow != 0) {
		if (coder->EFLow < coder->RC_MAX_RANGE) {
			OutPutByte(coder, coder->EFDigits);
			for (n = 1; n <= coder->EFFollow - 1; n++) {
				OutPutByte(coder, 0xFF);
			}
		} else {
			OutPutByte(coder, coder->EFDigits + 1);
			for (n = 1; n <= coder->EFFollow - 1; n++) {
				OutPutByte(coder, 0x00);
			}
		}
	}
	coder->EFLow = coder->EFLow << 1;
	n = coder->RC_CODE_BITS + 1;
	do {
		n -= 8;
		OutPutByte(coder, (unsigned char)(coder->EFLow >> n));
	} while (!(n <= 0));
}
// Initialization WJLCoder
void InitializationWJLCoder(WJLCoder *coder)
{
	coder->RC_CODE_BITS = 31;
	coder->RC_SHIFT_BITS = coder->RC_CODE_BITS - 8;
	coder->RC_MAX_RANGE = 1 << coder->RC_CODE_BITS;
	coder->RC_MIN_RANGE = 1 << coder->RC_SHIFT_BITS;
	coder->p0 = 0.0;
	coder->p1 = 0.0;
	coder->JIELINCOE = 0.0;
	coder->EFLow = coder->RC_MAX_RANGE;
	coder->EFRange = coder->RC_MAX_RANGE;
	coder->EFDigits = 0;
	coder->EFFollow = 0;
	coder->EOut_buff_loop = 0;
	coder->HashValueBuFF_Length = 0;
}
// the main function
void WJLHA3(unsigned char *InBytesBuFF, int InBytesBuFF_Length, unsigned char *keytBuFF, int keytBuFF_Length, unsigned char *HashValueBuFF, int HashValueBuFF_Length)
{
	unsigned char *Y, *tempInbuff;
	int Y_len = Ps, i = 0, j = 0;
	unsigned int k = 0;
	int partIndex = 0, paragraphs = 0;
	WJLCoder *coder;
	double coe = 0.0;
	if(HashValueBuFF == 0){
		HashValueBuFF = (unsigned char *)malloc(HashValueBuFF_Length);
	}
	// Initialization WJLCoder Object
	coder = (WJLCoder *)malloc(sizeof(WJLCoder));
	Y = (unsigned char *)malloc(Y_len * sizeof(unsigned char));
	paragraphs = DissectionTheDatas(InBytesBuFF_Length);
	InitializationWJLCoder(coder);
	coder->HashValueBuFF_Length = HashValueBuFF_Length;
	coder->HashValueBuFF  = (unsigned char *)malloc(HashValueBuFF_Length);

	if(InBytesBuFF_Length < 256){
		tempInbuff = (unsigned char *)malloc(256);
		memcpy(tempInbuff, InBytesBuFF, InBytesBuFF_Length);
		for(i = InBytesBuFF_Length; i < 256; ++i){
			tempInbuff[i] = (unsigned char)(RandomOfUCharTable[i] + (InBytesBuFF[i % InBytesBuFF_Length] << 1) | 0x01 ) ^ (InBytesBuFF[i % InBytesBuFF_Length] + RandomOfbitTable[255 - i] + RandomOfUCharTable[i]);
		}
		InBytesBuFF_Length = 256;
	}else{
		tempInbuff = InBytesBuFF;
	}

	if(keytBuFF_Length > InBytesBuFF_Length * 8){
		for(i = InBytesBuFF_Length * 8; i < keytBuFF_Length; ++i){
			keytBuFF[i % (InBytesBuFF_Length * 8)] = (unsigned char)(keytBuFF[i % (InBytesBuFF_Length * 8)] * keytBuFF[i]) ^ (keytBuFF[i] + keytBuFF[i % (InBytesBuFF_Length * 8)]);
		}
		keytBuFF_Length = InBytesBuFF_Length * 8;
	}

	// Segprocessing
	for(partIndex = 1; partIndex <= paragraphs; ++partIndex){
		if(partIndex == paragraphs){
			Y_len = InBytesBuFF_Length - (paragraphs - 1)* Ps;
		}
		memcpy(Y, tempInbuff + (partIndex - 1) * Ps, Y_len);
		// Get COE of Y
		coe = TheCOEofY(coder, partIndex, paragraphs, Y, Y_len);
		// Encode each bits
		for(i = 0; i < Y_len; ++i){
			for(j = 0; j < 8; ++j){
				coder->JIELINCOE = QueryUCharTable(i, bitOfByteTable[(int)Y[i]][j], coe, keytBuFF[ k % keytBuFF_Length ]);
				Encode(coder, bitOfByteTable[(int)Y[i]][j]);
				k ++;
			}
		}
	}
	FinishEncode(coder);
	memcpy(HashValueBuFF, coder->HashValueBuFF, HashValueBuFF_Length);
	// Free memory
	free(Y);
	free(tempInbuff);
	free(coder->HashValueBuFF);
	free(coder);
}

C语言下的测试:main.c

#include "WJLHA3.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <math.h>
#ifdef WIN32
#define  inline __inline
#endif // WIN32

// test
int main(){
	long t1,t2;
	int i = 0, tmp = 0;
	int InBUFF_Length = 10240;// 输入的字节长度
	int HashValueBuFF_Length = 19; // 自定义哈希值的长度,单位为字节
	int keyt_Length = 257;// 自定义密钥的长度,单位为字节
	unsigned char *In_BUFF;
	unsigned char *HashValue_BUFF;
	unsigned char *keyt_BUFF;

	In_BUFF = (unsigned char *)malloc(InBUFF_Length);
	HashValue_BUFF = (unsigned char *)malloc(HashValueBuFF_Length);
	keyt_BUFF  = (unsigned char *)malloc(keyt_Length);
	// 给出In_BUFF_Len长度的原始数据
	for(i = 0; i < InBUFF_Length; ++i){
		In_BUFF[i] = (unsigned char)(i % 256);
	}
	// 给出keyt_Length长度的秘钥数据
	for(i = 0; i < keyt_Length; ++i){
		keyt_BUFF[i] = (unsigned char)(i % 255);
	}
	printf("\n");
	// 调用WJLHA3算法
	t1 = GetTickCount();
	WJLHA3(In_BUFF, InBUFF_Length, keyt_BUFF, keyt_Length, HashValue_BUFF, HashValueBuFF_Length);
	t2 = GetTickCount();
	printf("编码耗时 :%d ms\n", t2 - t1);
	printf("\n有符号的WJLHA:");
	for(i = 0; i < HashValueBuFF_Length; ++i){
		printf("%d,",(char)HashValue_BUFF[i]);
	}
	printf("\n");
	printf("\nWJLHA字符串:");
	for(i = 0; i < HashValueBuFF_Length; ++i){
		printf("%02X",HashValue_BUFF[i]);
	}
	printf("\n");
	system("pause");
	return 0;
}

标签:coder,自定义,int,unsigned,char,Length,WJLHA3,哈希,HashValueBuFF
来源: https://blog.csdn.net/wjlxueshu/article/details/117459417

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有