Exemple #1
0
function md5crypt($pw, $salt = "", $magic = "")
{
    $MAGIC = "\$1\$";
    if ($magic == "") {
        $magic = $MAGIC;
    }
    if ($salt == "") {
        $salt = create_salt();
    }
    $slist = explode("\$", $salt);
    if ($slist[0] == "1") {
        $salt = $slist[1];
    }
    $salt = substr($salt, 0, 8);
    $ctx = $pw . $magic . $salt;
    $final = hex2bin(md5($pw . $salt . $pw));
    for ($i = strlen($pw); $i > 0; $i -= 16) {
        if ($i > 16) {
            $ctx .= substr($final, 0, 16);
        } else {
            $ctx .= substr($final, 0, $i);
        }
    }
    $i = strlen($pw);
    while ($i > 0) {
        if ($i & 1) {
            $ctx .= chr(0);
        } else {
            $ctx .= $pw[0];
        }
        $i = $i >> 1;
    }
    $final = hex2bin(md5($ctx));
    for ($i = 0; $i < 1000; $i++) {
        $ctx1 = "";
        if ($i & 1) {
            $ctx1 .= $pw;
        } else {
            $ctx1 .= substr($final, 0, 16);
        }
        if ($i % 3) {
            $ctx1 .= $salt;
        }
        if ($i % 7) {
            $ctx1 .= $pw;
        }
        if ($i & 1) {
            $ctx1 .= substr($final, 0, 16);
        } else {
            $ctx1 .= $pw;
        }
        $final = hex2bin(md5($ctx1));
    }
    $passwd = "";
    $passwd .= to64(ord($final[0]) << 16 | ord($final[6]) << 8 | ord($final[12]), 4);
    $passwd .= to64(ord($final[1]) << 16 | ord($final[7]) << 8 | ord($final[13]), 4);
    $passwd .= to64(ord($final[2]) << 16 | ord($final[8]) << 8 | ord($final[14]), 4);
    $passwd .= to64(ord($final[3]) << 16 | ord($final[9]) << 8 | ord($final[15]), 4);
    $passwd .= to64(ord($final[4]) << 16 | ord($final[10]) << 8 | ord($final[5]), 4);
    $passwd .= to64(ord($final[11]), 2);
    return "{$magic}{$salt}\${$passwd}";
}
Exemple #2
0
function crypt_apr_md5($plain, $salt = null)
{
    if (is_null($salt)) {
        $salt = salt(8);
    } elseif (preg_match('/^\\$apr1\\$/', $salt)) {
        $salt = preg_replace('/^\\$apr1\\$([^$]+)\\$.*/', '\\1', $salt);
    } else {
        $salt = substr($salt, 0, 8);
    }
    $length = strlen($plain);
    $context = $plain . '$apr1$' . $salt;
    $binary = hex2bin(md5($plain . $salt . $plain));
    for ($i = $length; $i > 0; $i -= 16) {
        $context .= substr($binary, 0, $i > 16 ? 16 : $i);
    }
    for ($i = $length; $i > 0; $i >>= 1) {
        $context .= $i & 1 ? chr(0) : $plain[0];
    }
    $binary = hex2bin(md5($context));
    for ($i = 0; $i < 1000; $i++) {
        $new = $i & 1 ? $plain : substr($binary, 0, 16);
        if ($i % 3) {
            $new .= $salt;
        }
        if ($i % 7) {
            $new .= $plain;
        }
        $new .= $i & 1 ? substr($binary, 0, 16) : $plain;
        $binary = hex2bin(md5($new));
    }
    $p = array();
    for ($i = 0; $i < 5; $i++) {
        $k = $i + 6;
        $j = $i + 12;
        if ($j == 16) {
            $j = 5;
        }
        $p[] = to64(ord($binary[$i]) << 16 | ord($binary[$k]) << 8 | ord($binary[$j]), 5);
    }
    return '$apr1$' . $salt . '$' . implode($p) . to64(ord($binary[11]), 3);
}