From 0a719c5441ddc14fad483e679edea900cd1e3789 Mon Sep 17 00:00:00 2001 From: MadCamel Date: Fri, 8 Oct 2010 06:00:13 -0400 Subject: Added MD5_ip() function to hash IP addresses --- src/common/md5calc.c | 31 ++++++++++++++++++++++++++++++- src/common/md5calc.h | 2 ++ 2 files changed, 32 insertions(+), 1 deletion(-) (limited to 'src/common') diff --git a/src/common/md5calc.c b/src/common/md5calc.c index 2eea918..6069070 100644 --- a/src/common/md5calc.c +++ b/src/common/md5calc.c @@ -11,7 +11,6 @@ #include #include #include "mt_rand.h" -#include #ifndef UINT_MAX #define UINT_MAX 4294967295U @@ -333,3 +332,33 @@ int pass_ok(const char *password, const char *crypted) { return(0); } +// [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; +} + diff --git a/src/common/md5calc.h b/src/common/md5calc.h index 3571466..5994376 100644 --- a/src/common/md5calc.h +++ b/src/common/md5calc.h @@ -1,11 +1,13 @@ // $Id: md5calc.h,v 1.1.1.1 2004/09/10 17:26:54 MagicalTux Exp $ #ifndef _MD5CALC_H_ #define _MD5CALC_H_ +#include void MD5_String (const char *string, char *output); void MD5_String2binary (const char *string, char *output); char *MD5_saltcrypt(const char *key, const char *salt); char *make_salt(); int pass_ok(const char *password, const char *crypted); +in_addr_t MD5_ip(char *secret, in_addr_t ip); #endif -- cgit v1.2.3-70-g09d2