SGP for domain.com/12345678: vZitFmz2JA

<?php

$domain = $_GET['domain'] ?? "cheese.com";
$master = $_GET['master'] ?? "12345678";
$len    = 10;

$sgp = supergen_pass($domain, $master, 'md5', $len);
print "SGP for $domain/$master: <code style=\"background: lightgray;\">$sgp</code>\n";

print "<br /><br />\n";
highlight_file(__FILE__);

///////////////////////////////////////////////////////////////////////////////

function supergen_pass($domain, $master, $algo = 'md5', $length = 10) {
    $domain = strtolower($domain);
    $count  = 0;
    $hash   = $master . ':' . $domain;

    while (true) {
        $md5  = hash($algo, $hash, true);
        $b64  = base64_encode($md5);
        $b64  = strtr($b64, "+/=", "98A");
        $hash = $b64;

        $count++;

        // Loop a minimum of 10 times
        if ($count < 10) {
            continue;
        }

        $tmp = substr($hash, 0, $length);

        # Must start with lower case letter
        # Must contain an uppercase letter
        # Must contain a digit
        $is_valid = (preg_match("/^[a-z]/", $tmp)) && (preg_match("/[A-Z]/", $tmp)) && (preg_match("/[0-9]/", $tmp));

        # If it's valid we stop, otherwise we continue looping UNTIL we are valid
        if ($is_valid) {
            break;
        }
    }

    $ret = $tmp;

    return $ret;
}

///////////////////////////////////////////////////////////////////////////////

// vim: tabstop=4 shiftwidth=4 noexpandtab autoindent softtabstop=4