in reply to Creative presentation algorithm: IP to a color

Here is simply another way of using the first three octets of the users ip address to create a hexadecimal string that can be used as html color argument.
#!/usr/bin/perl -w use strict; my @octets = split(/\./, $ENV{'REMOTE_ADDR'}); pop(@octets); my @hexval; push(@hexval, dec2hex($_)) for (@octets); my $hex = join("", @hexval); print "Content-type: text/html; charset=ISO-8859-1\n\n"; print "The hex value is: <font color=$hex>$hex</font>\n"; sub dec2hex { my $decnum = shift; my ($hexnum, $modulus, @hexval); $hexnum = int($decnum / 16); $hexnum = chr($hexnum + 55) if ($hexnum > 9); push(@hexval, $hexnum); $modulus = $decnum % 16; $modulus = chr($modulus + 55) if ($modulus > 9); push(@hexval, $modulus); return join("", @hexval); }

Not sure how you plan on making it irreversible. If you use the method of converting the IP into a hex number it will always be able to be reversed if anyone figures out how you vreate the hex. However all that will be able to be recovered is the first three octets. Also with this method you have the possibility of having 253 users (255 minus 1 for broadcast and one for gateway) with the same color. Be it a very very small chance that every user from that IP range will visit your site at once.

Replies are listed 'Best First'.
Re^2: Creative presentation algorithm: IP to a color
by eXile (Priest) on Aug 29, 2005 at 05:54 UTC
    I think using MD5 (as i demonstrated above) sort-of solves this irreversibility-problem. MD5 is a one-way hashing algorithm, so in principle you'll only be able to get back from rgb-value to IP if you have a full table of IP to rgb-value's (a 'brute force' attack). MD5 is not the strongest of one-way hashing algoritms, but it's widely used and widely available.