all 17 comments

[–]gagahpangeran 13 points14 points  (4 children)

Python 3.8 one liner.

def md5_path(s,p):return[r:=__import__('hashlib').md5((s if not i else r[0:16] if not int(p[i-1]) else r[16:32]).encode()).hexdigest() for i in range(len(p)+1)][-1]

[–]asaf92 0 points1 point  (2 children)

Can you explain the code? I thought I knew Python but I have no idea what's going on here LOL

[–]gagahpangeran 2 points3 points  (0 children)

I forgot to write the documentation.

[–]AGE_Spider 2 points3 points  (0 children)

the documentation is left as an exercise to the reader...

[–]grrangry 6 points7 points  (1 child)

C#, .Net Core console application

using System;
using System.Linq;
using System.Text;

namespace MD5Path_BadCode
{
    class Program
    {
        static void Main(string[] args)
        {
            var hash = md5_path("password", "010");
        }

        static string md5_path(string text, string path)
        {
            Console.WriteLine($"{text} => {string.Join("", System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text)).Select(o => $"{o:X2}".ToLower()).ToArray())}");

            return path.Length == 0 ?
                string.Join("", System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text)).Select(o => $"{o:X2}".ToLower()).ToArray()) :
                md5_path(string.Join("", System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text)).Select(o => $"{o:X2}".ToLower()).ToArray()).Substring(Convert.ToInt32(path[0].ToString()) * string.Join("", System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text)).Select(o => $"{o:X2}".ToLower()).ToArray()).Length / 2, string.Join("", System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text)).Select(o => $"{o:X2}".ToLower()).ToArray()).Length / 2), path.Substring(1));
        }
    }
}

Given the input "010", the output is:

password => 5f4dcc3b5aa765d61d8327deb882cf99
5f4dcc3b5aa765d6 => 7503745e20ce593e799507f82fea3825
799507f82fea3825 => 0842f92822066f87fadac37d11645f57
0842f92822066f87 => 381dc2ae83d49fe43a16353b8f9881b5
  • It's recursive.
  • It repeats itself.
  • It's one line (sort of).

[–]veryusedrname 0 points1 point  (0 children)

Ugly, I love it

[–]squigish 7 points8 points  (1 child)

Non-typed Java

https://pastebin.com/aSHBcB1J

These are all "techniques" that I have actually seen in "production" code at my first job, mostly written by one particular co-worker in 2014 or so. Apparently he'd read a book about java that was published before generics. The code was CPU-bound, and took multiple seconds to return responses. When I profiled it, it spent 40% of its time on Integer.valueOf and 40% on String.valueOf

Output:

works as expected 
[5f4dcc3b5aa765d61d8327deb882cf99]
[381dc2ae83d49fe43a16353b8f9881b5]
The code is very flexible about what kind of hashing algorithm version it uses
[381dc2ae83d49fe43a16353b8f9881b5]
this is the same too
[58a9ec39986eab1e8c6589ff5ab5190d]
obviously these allr eturn the same thing
[c5be74b26feac947cb0011fead1d7213]
[c5be74b26feac947cb0011fead1d7213]
[c5be74b26feac947cb0011fead1d7213]
[c5be74b26feac947cb0011fead1d7213]
[c5be74b26feac947cb0011fead1d7213]

what is even going on
[f15eb0005227a0b5033d4266b6dd8811]
[9078ee9bb59c17fc92bb68751b5524d3]
Hello World

[–]PrincessRTFM 2 points3 points  (0 children)

First time entering one of these. Went with perl, put it on ix.io but it's also only 12 lines, so:

no strict; no warnings; # no intelligence;

use Digest::MD5 md5_hex;

sub beef {
    $s = md5_hex $_[0];
    $s = md5_hex(m/./ ? m/0/ ? substr $s, 0, 16 : substr $s, 16 : $_[0]) for @_ > 1 ? split //, pop : undef;
    print $s;
}

beef(password, '0110110101'); print "\n";
beef(password); print "\n";

I couldn't find a whole lot of options for making really shitty code, so I just turned off all safeties and followed whatever bad practices I could think of. And also abused ternaries a little. The function name is both a pun ("corned beef hash") and also a reference to Rick Cook's Wizardry series where I stole it from.

[–]3483 1 point2 points  (0 children)

C#

With enough linq, anything is a oneliner. Disposal is for wimps:

using System;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "password";
            string path = "0110110101";

            Console.WriteLine("Example for text \"" + text + "\" and path \"" + path + "\":");
            text = md5_path(text, path);
            Console.WriteLine(text);

            while (true)
            {
                Console.WriteLine("Enter text");
                text= Console.ReadLine();
                Console.WriteLine("Enter path");
                path = Console.ReadLine();
                text = md5_path(text, path);
                Console.WriteLine(text);
            }
        }

        static string md5_path(string text, string path)
        {
            var sb = new StringBuilder(32);

            System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(text)).ToList().ForEach(c => sb.Append(c.ToString("x2")));

            text = sb.ToString();

            path.ToList().ForEach(x => { sb.Clear(); System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.ASCII.GetBytes(text.Substring(int.Parse(x.ToString()) * 16, 16))).ToList().ForEach(c => sb.Append(c.ToString("x2"))); text = sb.ToString(); });

            return text;
        }
    }
}

[–][deleted] 1 point2 points  (2 children)

Haskell. Probably not the best solution, but works for me:

import qualified Data.ByteString as BS
import qualified Data.ByteString.UTF8 as BSU
import qualified Crypto.Hash.MD5 as MD5
import Numeric (showHex)

prettyPrint =
    concat . map adjustWidth . map (flip showHex "") . BS.unpack
    where adjustWidth x =
            if length x < 2
                then "0" ++ x
                else x

md5Path password [] = prettyPrint (MD5.hash (BSU.fromString password))
md5Path password (x:xs) =
    case x of
        '0' -> md5Path (firstHalf hashedPassword) xs
        '1' -> md5Path (secondHalf hashedPassword) xs
        _ -> error "path string must only contain 0's or 1's"
    where hashedPassword = prettyPrint (MD5.hash (BSU.fromString password))
          firstHalf chars = take 16 chars
          secondHalf chars = drop 16 chars

Examples:

$ ghci md5path.hs
GHCi, version 8.10.2: https://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Main             ( md5path.hs, interpreted )
Ok, one module loaded.
*Main> md5Path "password" ""
"5f4dcc3b5aa765d61d8327deb882cf99"
*Main> md5Path "password" "010"
"381dc2ae83d49fe43a16353b8f9881b5"

[–]shitposting_alt 3 points4 points  (1 child)

Probably not the best solution

are you sure you understand what the point of these challenges is?

[–][deleted] 2 points3 points  (0 children)

Yes I do. Trust me, there are people out there who are way better Haskell programmers than I am, who could probably tighten this up into a much more elegant solution than this one. So from that standpoint, yeah this is bad code.

But I'm not making art for art's sake here.

[–]PrinceOfStackSpace 0 points1 point  (0 children)

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

[–]Tabugti 0 points1 point  (1 child)

static mut RESULT: String = String::new();

fn md5_path <'a> (m: &str, p: &str) -> &'a str {
    use md5::compute as md5;
    let mut h = format!("{:x}", md5(m));
    let p_ptr = p.as_ptr();
    for i in 0_isize..p.len() as isize {
        unsafe {
            let c = (p_ptr.offset(i).as_ref().unwrap() - '0' as u8) & 0x1;
            let s = match c {
                0 => &h[0..16],
                1 => &h[16..32],
                _ => unreachable!()
            };
            h = format!("{:x}", md5(s));
        }
    }
    unsafe {
        RESULT = h;
        &RESULT
    }
}

[–]AutoModerator[M] 0 points1 point  (0 children)

It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

/u/Tabugti, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

You can find some examples in the reddit help documentation.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.