All pastes #1414977 Raw Edit

md5 similarity search

public php v1 · immutable
#1414977 ·published 2009-05-07 04:04 UTC
rendered paste body
<?php/*check repetition at any point in the strings*/function checkRepetition($string1, $string2) {  if (($length = strlen($string1)) != strlen($string2)) {    return false;  }  $pos1 = 0;  while ($pos1 < $length) {    $pos2 = 0;    while ($pos2 < $length) {      if (substr($string1, $pos1, 1) == substr($string2, $pos2, 1)) {        $maxRepetition = max($maxRepetition, checkLengthOfRepetition(substr($string1, $pos1), substr($string2, $pos2)));      }      $pos2++;    }    $pos1++;  }  return $maxRepetition;}/*check repetition of the first few chars*/function checkLengthOfRepetition($string1, $string2) {  global $_cr_instances;  global $_cr_repetitions;  if (!$_cr_instances) {    $_cr_repetitions = 0;  }  $_cr_instances++;  if (strlen($string1) == 0) {    $_cr_instances--;    return $_cr_repetitions;  }  elseif (substr($string1, 0, 1) == substr($string2, 0, 1)) {    $_cr_repetitions++;    $rtnVal = checkLengthOfRepetition(substr($string1, 1), substr($string2, 1));    $_cr_instances--;    return $rtnVal;  }  else {    $_cr_instances--;    return $_cr_repetitions;  }}/*let's find some md5 recurrences!*/$current = md5(rand());while (true) {  if (($blah = checkRepetition($current, $new = md5($current))) > 4) {    echo $current . ' ' . $new . ' ' . $blah . chr(10);  }  $current = $new;}?>