[Netadm-devel] gwc/gwclib syscrypto.c,NONE,1.1
Status: Beta
Brought to you by:
linuxpark
From: linuxpark <lin...@us...> - 2006-04-23 06:49:20
|
Update of /cvsroot/netadm/gwc/gwclib In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20772 Added Files: syscrypto.c Log Message: Initial-checkin int gwc_get32uid_by_md4 (char *string) desc: this function return uniqeue 4 byte integer for a given string (max 32byte) @string: any string return: uniqueue integer --- NEW FILE: syscrypto.c --- /* Title : syscrypto.c Author : Jeho-Park <lin...@gm...> or <de...@sk...> Created date : 2006. 04. 23. (SUN) 15:31:33 KST Description : gwc crypto library */ #ident "@(#) $Header: /cvsroot/netadm/gwc/gwclib/syscrypto.c,v 1.1 2006/04/23 06:49:16 linuxpark Exp $" /* * This program is non-GPL but follows the BSD license * so If you want to use this code, you must not cut this comment. * * The half_md4_transform() was ported from linux/lib/halfmd4.c and made for more extensible * to get a unique id with more long string. * * jeho-park <lin...@ga...> or <de...@sk...> 2006. 4 */ #include <stdio.h> #include <string.h> #include <unistd.h> /* F, G and H are basic MD4 functions: selection, majority, parity */ #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z))) #define H(x, y, z) ((x) ^ (y) ^ (z)) /* * The generic round function. The application is so specific that * we don't bother protecting all the arguments with parens, as is generally * good macro practice, in favor of extra legibility. * Rotation is separate from addition to prevent recomputation */ #define ROUND(f, a, b, c, d, x, s) \ (a += f(b, c, d) + x, a = (a << s) | (a >> (32 - s))) #define K1 0 #define K2 013240474631UL #define K3 015666365641UL /* * Basic cut-down MD4 transform. Returns only 32 bits of result. */ static int half_md4_transform(int buf[4], int const in[32]) { int a = buf[0], b = buf[1], c = buf[2], d = buf[3]; /* Round 1 */ ROUND(F, a, b, c, d, in[0] + K1, 3); ROUND(F, d, a, b, c, in[1] + K1, 7); ROUND(F, c, d, a, b, in[2] + K1, 11); ROUND(F, b, c, d, a, in[3] + K1, 19); ROUND(F, a, b, c, d, in[4] + K1, 3); ROUND(F, d, a, b, c, in[5] + K1, 7); ROUND(F, c, d, a, b, in[6] + K1, 11); ROUND(F, b, c, d, a, in[7] + K1, 19); /* Round 2 */ ROUND(G, a, b, c, d, in[1] + K2, 3); ROUND(G, d, a, b, c, in[3] + K2, 5); ROUND(G, c, d, a, b, in[5] + K2, 9); ROUND(G, b, c, d, a, in[7] + K2, 13); ROUND(G, a, b, c, d, in[0] + K2, 3); ROUND(G, d, a, b, c, in[2] + K2, 5); ROUND(G, c, d, a, b, in[4] + K2, 9); ROUND(G, b, c, d, a, in[6] + K2, 13); /* Round 3 */ ROUND(H, a, b, c, d, in[3] + K3, 3); ROUND(H, d, a, b, c, in[7] + K3, 9); ROUND(H, c, d, a, b, in[2] + K3, 11); ROUND(H, b, c, d, a, in[6] + K3, 15); ROUND(H, a, b, c, d, in[1] + K3, 3); ROUND(H, d, a, b, c, in[5] + K3, 9); ROUND(H, c, d, a, b, in[0] + K3, 11); ROUND(H, b, c, d, a, in[4] + K3, 15); /* Round 4 --LP */ ROUND(H, a, b, c, d, in[12] + K3, 3); ROUND(H, d, a, b, c, in[19] + K3, 9); ROUND(H, c, d, a, b, in[4] + K3, 11); ROUND(H, b, c, d, a, in[7] + K3, 15); ROUND(H, a, b, c, d, in[29] + K3, 3); ROUND(H, d, a, b, c, in[13] + K3, 9); ROUND(H, c, d, a, b, in[16] + K3, 11); ROUND(H, b, c, d, a, in[9] + K3, 15); /* Round 5 --LP */ ROUND(H, a, b, c, d, in[13] + K3, 3); ROUND(H, d, a, b, c, in[19] + K3, 9); ROUND(H, c, d, a, b, in[26] + K3, 11); ROUND(H, b, c, d, a, in[19] + K3, 15); ROUND(H, a, b, c, d, in[21] + K3, 3); ROUND(H, d, a, b, c, in[24] + K3, 9); ROUND(H, c, d, a, b, in[31] + K3, 11); ROUND(H, b, c, d, a, in[7] + K3, 15); buf[0] += a; buf[1] += b; buf[2] += c; buf[3] += d; return buf[1]; /* "most hashed" word */ } /* * gwc_get32uid_by_md4 * @string: any random string (max 32 byte) * @return: uniqueue 32bit integer ( not fully tested ) */ int gwc_get32uid_by_md4 (char *string) { int i = 0; int max = strlen(string); int buf [4] = { 15733, 337534321, 1369311751, 73951137 }; int in [8 * 4] = { 34243, 454331, 34381, 32332, 34095, 13225, 1241, 3221, 33215, 11231, 3237, 43111, 13425, 97723, 36631, 5467, 45411, 24333, 5307, 28871, 2457, 43261, 34457, 5473, 33321, 45347, 739421, 23227, 3323, 3326, 33311, 33697 }; int md4; while (i < max) { printf("?\n"); if ( i < 4 ) { buf [i] = (int)string[i]; } in [i] = 0x789ABCDE & (int)string[i]; ++i; } md4 = half_md4_transform(buf, in); if (md4 <= 0 ) md4 = 0xFFFFFFFF ^ md4; return md4; } |