Własny algorytm skrótu - opinie i uwagi

0

Witam
Ostatnio naszło mnie na napisanie sobie własnego algorytmu tworzącego skróty ciągów znaków.
Co sądzicie o samym kodzie i złożoności. Co można by było usprawnić. Bądź kiedy wygenerowany będzie błąd.

 
char * myhash(char *str){
	int size;
	int weight=0;
	int x;
	int tab[]={
		0xcf3c08d7, 0x1a066610, 0xc0b8705d, 0x316ff3d3,
		0x6d1d4ff2, 0xb6e08af3, 0xb52a3d58, 0x4ac44174,
		0x0f835531, 0x6af74b42, 0x4741755e, 0x49853153,
		0x784c5ad8, 0x5c12ef86, 0x68cdc45b, 0x4ed967a9,
		0xd7444022, 0x2f4e0d9d, 0xd7155c13, 0xde6e1f22,
		0xc4a5428f, 0x31db1201, 0xed00a2bd, 0xf3a9d21f,
		0x894bd8f0, 0x014d472f, 0x8f8e8b69, 0xad44d497,
		0xee4b8941, 0xefb3eba8, 0x407f5670, 0x331c5a36,
		0x2068fd6d, 0x4a60fa07, 0x5bf4893c, 0xc7a26731,
		0x71e48932, 0xbf003560, 0x278df200, 0x39464fb7,
		0x78b8f4a1, 0x2dcd40a8, 0xbd273d64, 0x33f7dfc6,
		0x44167841, 0xc0e17a6f, 0xe9a1a4ee, 0xb5b6dfb5,
		0x99b00023, 0xc6066481, 0x0084ccd2, 0x139d66b1,
		0xe15d0411, 0xe12f7fb4, 0x43fc66d6, 0x8ca25661,
		0x796f70f6, 0x8a81324b, 0x165c95de, 0x6ca51157,
		0xcdfe15f8, 0x3c66c237, 0xee4cc684, 0x6c56e948,
		0xde159bdd, 0x4ba77854, 0x8fc6c5de, 0xb2280f4c,
		0xaaffeae5, 0x04b41a23, 0xd4e61872, 0xbb5db88c,
		0x991d1c97, 0xd40bba17, 0x43ec5676, 0x36dcc548,
		0xc6a4c008, 0xdf47fdc1, 0xbf42edb1, 0xbd2952b6,
		0x6d9844dc, 0x210fc4ba, 0x37b249dc, 0xcdb2e4a1,
		0x7099a5e5, 0x72790ee4, 0x02077248, 0x35371c62,
		0x6893db2d, 0x2f33fa59, 0x7d8ff161, 0x80f7e189,
		0x3956976c, 0x8fca3df2, 0x3121f339, 0x05b95d09,
		0x7f259433, 0x0f236ebd, 0x219cc6a8, 0xef18bc8b,
		0xb5a5187d, 0x4129b94f, 0x81106a85, 0x35c4afef,
		0x0d928076, 0x82999777, 0xd71baf66, 0xdd38941a,
		0x0438fe7a, 0x7e8e26f4, 0x53ec4a22, 0x6a50a7f4,
		0x3e29ab51, 0x606ef547, 0xd8964bc9, 0xee7b5672,
		0x38ae392e, 0x20669df5, 0xc87fecf1, 0x0232d7b2,
		0x5c2a16df, 0x56d45dac, 0xd7e4979c, 0x658b4b3c,
		0x75763ebf, 0x7883f3ae, 0xf84e3c78, 0x4e41b353,
		0x1ddb09e9, 0x72f9163e, 0x49df7686, 0xd7fa29cb,
		0xbbb8d61b, 0x353f3da9, 0x0629f5d9, 0xaa237d5f,
		0x71eaf714, 0x5d8dbc40, 0x3b01fae7, 0xd729d7dd,
		0x17eb396e, 0x7861aa10, 0x8b04fbd9, 0x1eb98414,
		0x75932869, 0xa8dd19cd, 0xb68da4ac, 0x580b2c59,
		0x8192f29f, 0xb5170801, 0xc8a6dff2, 0x35a02d21,
		0x8ee0cc38, 0x828b0464, 0x84b65bae, 0xca0b6528,
		0x77125aeb, 0x550a0f39, 0x626c1ed3, 0x29dfe94c,
		0x1af60440, 0xb129713a, 0x4caf8e3c, 0xdbc92e56,
		0x1d3065c6, 0x2702675d, 0xcb318e59, 0xe790a6c0,
		0xb9005f8c, 0x70028d17, 0x427ec70b, 0xefba4422,
		0x86a4e26a, 0x37eed997, 0xf600d0b7, 0x4ce44a53,
		0x2b2526d4, 0x0505fdbf, 0xfa675aa9, 0x97aa5b12,
		0x2bd561ea, 0x7789a8a2, 0x5cf29c40, 0x0220589a,
		0x710d1712, 0xb1907c14, 0xf23214ea, 0x2abb3aac,
		0x0246a1fa, 0xeb983f6d, 0x108fcdeb, 0x39b2ade2,
		0xfa25ef54, 0x9e35f6f2, 0xefe3a19d, 0xb9320bea,
		0x5269f8ca, 0x73ddfa8b, 0x105a9743, 0x5d95078b,
		0xf530a3d7, 0xafb0b721, 0x25b4a145, 0x9a22b9fa,
		0xf2253761, 0x7a00810b, 0xb9b575c9, 0x6f9e16fd,
		0xa7fbcaf7, 0x782b29d6, 0x920b2c3e, 0xff96e1fd,
		0xb1c0a3f4, 0x5f398c78, 0x2e64ae58, 0x03606198,
		0xe4b4d663, 0x9dd177f3, 0x7b5934f6, 0xefcafa11,
		0x8dcc136e, 0xbc812165, 0x1f2b8bae, 0x8372a28e,
		0x307b47c3, 0x971a4f86, 0x87b98021, 0xff099a82,
		0xede6dae3, 0xf8ef6eb9, 0xb99a31dd, 0x8b555597,
		0xd217131f, 0x51abe6d9, 0x08e9c178, 0x43d830df,
		0x9f36bda3, 0x8bad79a0, 0xa7042666, 0xb8e5e309,
		0xeae3ec9e, 0xa5b87f47, 0x9617a388, 0xddb5a77b,
		0x566df244, 0xd0a561a7, 0x80bc5138, 0x3c98c179,
		0x4f50779d, 0xf173c2b0, 0x1bb928a5, 0x56787301,
		0xec58e38f, 0x6e323010, 0x06523576, 0x74a17e11,
    };
	size=strlen(str);
	for(x=0;x<size;x++){
		weight ^= ~ str[x] << 8 * ( x % 4 );
	}
	int out[4]={
		select_byte( weight , 3 ) ^ select_byte ( weight , 1 ) ^ select_byte( weight , 2 ) ^ select_byte( weight , 0 ) << size+24,
		size | size << 24,
		select_byte( weight , (int) str[size-1] ) ^ 0x0ffffff0,
		select_byte( weight , (int) str[0] ) ^ 0xafffffff
	};
	for(x=0;x<size;x++){
		out[0] ^= tab[(int)str[x]] ^ (str[x]) << 8;
        out[2] ^= tab[(int)str[x]] ^ (str[x]) << 16;
        out[1] ^= tab[(int)str[x]] ^ (str[x]) << 8;
        out[3] ^= tab[(int)str[x]] ^ (str[x]) << 16;
	}
	char *ret;
	ret = (char *) malloc(sizeof(char));
	sprintf( ret , "%08x%08x%08x%08x" , out[3] , out[1] , out[2] , out[0] );
	return ret;
}
int select_byte(int in, int byte){
	int max;
	int out;
	int tmp;
	max = in >> 31;
    if ( byte % 4==0 )
        out = in >> 8 * ( byte % 4 );
    else if ( byte % 4 == 3 ){
        out = in >> 8 * ( byte % 4 ) ^ max << 8;
		return out;
	}
    else
        out = in >> 8 * ( byte % 4 ) ^ max << ( 32 - 8 * ( byte % 4 ) );
    tmp = in >> 8 * ( ( byte + 1 ) % 4 ) ^ max << ( 32 - 8 * (( byte + 1 ) % 4 ) );
    out = out << 8 * ( byte % 4 ) ^ tmp << 8 * ( ( byte + 1 ) % 4 );
    return out >> 8 * ( byte % 4 );
}
0
ret = (char *) malloc(sizeof(char));
        sprintf( ret , "%08x%08x%08x%08x"

Alokujesz pamięć dla jednego bajta a wrzucasz 32 bajty? Dość (nie)typowe.

ATSD:
Po co tworzyć kolejny algorytm haszujący? Weź jakiś znany, zamiast tworzyć coś własnego dziurawego.

0

No z tym string'iem to rzeczywiście nie zwróciłem uwagi.
Ogólnie napisałem to tak dla podszkolenia się. Niekoniecznie do szerszego zastosowania tego algorytmu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1