english

learn php

learn css

learn html

learn javascript

learn php

7. functions

solution 7.3
1 minute / 110 words

seven / solution 7.3

<?php

// save password and hash it. after this, the program doesn't know it other than by hash.
$realPassword = "qwerty78B";
$realPassword = password_hash($realPassword, PASSWORD_DEFAULT);

$startTime = time();

crack();

function generatePassword($size, $start = "")
{
    global $realPassword, $startTime;
    foreach (str_split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") as $character) {
        if ($size > 1) {
            generatePassword($size - 1, $start . $character);
        } else {
            $candidate = $start . $character;
            print_r("trying " . $candidate . "\n");
            if (password_verify($candidate, $realPassword)) {
                print_r("password is " . $candidate . "\n");
                print_r("cracked in " . time() - $startTime . " seconds.");
                die();
            }
        }
    }
}

function crack(int $maxLength = 12)
{
    global $realPassword;
    print_r("cracking hashed password " . $realPassword . " to a maximum length of " . $maxLength . " characters.\n");
    for ($i = 1; $i <= $maxLength; $i++) {
        generatePassword($i);
    }
}

learn php

learn css

learn html

learn javascript

powered by akelei. an avi sato project.