Page 1 of 1

unique IDs

Posted: Sat Aug 19, 2006 8:02 pm
by GeXus
I know about doing md5 or unique w/ md5... but what if I wanted to create a 10 character unique int. Is that posible even? If I stripped 22 of the characters off the md5.. would I risk the chance of there being a match?

Posted: Sat Aug 19, 2006 8:04 pm
by feyd
You're always risking a match. The smaller the set, the greater the chance of collision.

Posted: Sat Aug 19, 2006 9:14 pm
by Z3RO21
I agree with feyd, the smaller you set the easier it will be to match... But, if you need a solution to do this I got a set of functions I used to do the same thing. What it does is it condenses that data down but preserves each charecter by using a logical AND function. It is still suggested to use larger sets, but it is your business :-p.

p.s Sorry for lack of comments, if you need any clarification just say so :)

Code: Select all

<?PHP
	function PadByte($Bin) {
		return substr('00000000',0,8 - strlen($Bin)) . $Bin;
	}
	function Str2Seq($Str) {
		$Seq = '';
		for ($i = 0; $i < strlen($Str); $i++ ) {
			$Seq .= PadByte(decbin(ord($Str[$i])));
		}
		return $Seq;
	}
	function Seq2Str($Seq) {
		$Str = '';
		for ($i = 0; $i < strlen($Seq); $i += {
			$Str .= chr(bindec(substr($Seq, $i, ));
		}
		return $Str;
	}
	function bAND($A, $B) {
		$bA = Str2Seq($A);
		$bB = Str2Seq($B);
		$RetChr = '';
		for ($i = 0; $i < strlen($bA); $i++) {
			if ($bA[$i] == $bB[$i]) {
				$RetChr .= 1;
			} else {
				$RetChr .= 0;
			}
		}
		return Seq2Str($RetChr);
	}
	function Condense($Str, $Size) {
		$DataBase = substr($Str, 0, $Size);
		$DataMask = substr($Str, $Size, strlen($Str) - $Size);
		for ($i = 0; $i < strlen($DataMask); $i++) {
			$MaskChar = $DataMask[$i];
			for ($j = 0; $j < strlen($DataBase); $j++) {
				$DataBase[$j] = bAND($DataBase[$j], $MaskChar);
			}
		}
		return $DataBase;
	}
?>
To use just to as follows:

Code: Select all

<?PHP
	$MyStr = 'This is a string!';
	$MyMD5Hash = md5($MyStr);
	$CondensedHash = Condense($MyMD5Hash, 10);
	echo '<strong>Original Hash:</strong> ' . $MyMD5Hash . ' <strong>Condensed Hash:</strong> ' . $CondensedHash; 
?>
The above yeilds the following for results:
Original Hash: 7b25f6963e1cb3997405f16644240e02 Condensed Hash: 4a16e5:50f
Hope this was helpful :)