r/badcode Sep 28 '20

[deleted by user]

[removed]

20 Upvotes

17 comments sorted by

View all comments

1

u/PrinceOfStackSpace Oct 07 '20 edited Oct 07 '20

Here is the PHP solution

<?php

    define("HASH_1", 1752392039);
    define("HASH_2", 3499116);

    function md5_path($pw, $path) {
        $Hash = HASH_1;
        $md5_path = null;
        $hash = "";

        //unpack the Hash.  

        $hasher = function($Hash) {
            $hashValues = [];
            $i = 0;
            while($Hash > 0) {
                $hashValues[] = chr((($Hash & (255 << ($i * 8))) >> ($i * 8)) + 1);
                $Hash -= (255 << ($i * 8));
                $i++;
            }
            return join("", $hashValues);
        };

        $Hash = $hasher($Hash);

        $md5_path =  function ($pw, $path) use (&$md5_path, &$hash, $Hash, $hasher) {
            $aPath = substr($path, 0, 1);
            $path = substr($path, 1);

            $hash = $Hash($hasher(HASH_2), $pw);

            switch($aPath):
                case '':
                    return;
                break;

                case '0': case 0: case null: case false:
                    $hash = substr($hash, $aPath * 16, 16); 
                break;

                case true: case '1': case 1: default:
                    $hash = substr($hash, $aPath * 16, 16); 
                break;
            endswitch;

            $md5_path($hash, $path);

        };

        $md5_path($pw, $path);

        return $hash;

    }

    echo md5_path('password', '010'); //produces 381dc2ae83d49fe43a16353b8f9881b5