// [M|h]ashes up an IP address and a secret key // to return a hopefully unique masked IP. in_addr_t MD5_ip(char *secret, in_addr_t ip) { char ipbuf[32]; char obuf[16]; union { struct bytes { unsigned char b1; unsigned char b2; unsigned char b3; unsigned char b4; } bytes; in_addr_t ip; } conv; // MD5sum a secret + the IP address memset(&ipbuf, 0, sizeof(ipbuf)); snprintf(ipbuf, sizeof(ipbuf), "%lu%s", (unsigned long)ip, secret); MD5_String2binary(ipbuf, obuf); // Fold the md5sum to 32 bits, pack the bytes to an in_addr_t conv.bytes.b1 = obuf[0] ^ obuf[1] ^ obuf[8] ^ obuf[9]; conv.bytes.b2 = obuf[2] ^ obuf[3] ^ obuf[10] ^ obuf[11]; conv.bytes.b3 = obuf[4] ^ obuf[5] ^ obuf[12] ^ obuf[13]; conv.bytes.b4 = obuf[6] ^ obuf[7] ^ obuf[14] ^ obuf[15]; return conv.ip; }
/** output is the coded character sequence in the character sequence which wants to code string. */ void MD5_String(const char *string, char *output) { unsigned char digest[16]; MD5_String2binary(string,digest); sprintf(output, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[ 0], digest[ 1], digest[ 2], digest[ 3], digest[ 4], digest[ 5], digest[ 6], digest[ 7], digest[ 8], digest[ 9], digest[10], digest[11], digest[12], digest[13], digest[14], digest[15]); }
/** output is the coded binary in the character sequence which wants to code string. */ void MD5_Binary(const char *string, unsigned char *output) { MD5_String2binary(string,output); }