2

I'm trying to recreate the functionallity of

slappasswd -h {md5}

on .Net

I have this code on Perl

use Digest::MD5;
use MIME::Base64;
$ctx = Digest::MD5->new;
$ctx->add('fredy');
print "Line $.: ", $ctx->clone->hexdigest, "\n";
print "Line $.: ", $ctx->digest, "\n";
$hashedPasswd = '{MD5}' . encode_base64($ctx->digest,'');
print $hashedPasswd . "\n";

I've tried to do the same on VB.Net , C# etc etc , but only works the

$ctx->clone->hexdigest # result : b89845d7eb5f8388e090fcc151d618c8

part in C# using the MSDN Sample

static string GetMd5Hash(MD5 md5Hash, string input)
    {

        // Convert the input string to a byte array and compute the hash. 
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

        // Create a new Stringbuilder to collect the bytes 
        // and create a string.
        StringBuilder sBuilder = new StringBuilder();

        // Loop through each byte of the hashed data  
        // and format each one as a hexadecimal string. 
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        // Return the hexadecimal string. 
        return sBuilder.ToString();
    }

With this code in Console App :

 string source = "fredy";
        using (MD5 md5Hash = MD5.Create())
        {
            string hash = GetMd5Hash(md5Hash, source);

            Console.WriteLine("The MD5 hash of " + source + " is: " + hash + ".");


        }

outputs : The MD5 hash of fredy is: b89845d7eb5f8388e090fcc151d618c8.

but i need to implement the $ctx->digest function, it outputs some binary data like

¸˜E×ë_ƒˆàüÁQÖÈ

this output happens on Linux and Windows with Perl.

Any ideas?

Thanks

Fredy
  • 21
  • 4
  • 4
    This is an implementation / debugging issue and should instead be asked on Stack Overflow. –  Aug 11 '15 at 16:11
  • What happens if you hex->bin the output? – whatsisname Aug 11 '15 at 16:17
  • The first line in your C# already gives you a bunch of bytes. Those would be the same as the what `$ctx->digest` does in Perl. However, if you print stuff in Perl, it tries to represent it as character data. That's why you see the _weird_ output. So in order to check if they are the same, you would to compare them you need to get them into the same representation. Hex is one of them. Converting them to a string that looks like binary (`1100101...`) might work also. Or you can try to figure out how to simply convert the bytes to whatever characters they are in C#. I'll edit the question for you – simbabque Aug 12 '15 at 07:55

1 Answers1

2

As I already said in my comment above, you are mixing some things up. What the digest in Perl creates is a set of bytes. When those are printed, Perl will convert them automatically to a string-representation, because (simplified) it thinks if you print stuff it goes to a screen and you want to be able to read it. C# does not do that. That doesn't mean the Perl digest and the C# digest are not the same. Just their representation is different.

You have already established that they are equal if you convert both of them to a hexadecimal representation.

Now what you need to do to get output in C# that looks like the string that Perl prints when you do this:

print $ctx->digest; # output: ¸˜E×ë_ƒˆàüÁQÖÈ

... is to convert the C# byte[] data to a string of characters.

That has been answered before,f or example here: How to convert byte[] to string?

Using that technique, I believe your function to get it would look like this. Please note I am a Perl developer and I have no means of testing this. Consider it C#-like pseudo-code.

static string GetMd5PerlishString(MD5 md5Hash, string input)
    {

        // Convert the input string to a byte array and compute the hash. 
        byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

        string result = System.Text.Encoding.UTF8.GetString(data);

        return result;
    }

Now it should look the same.


Please also note that MD5 is not a secure hashing algorithm for passwords any more. Please do not store use it to store user passwords!

Community
  • 1
  • 1
simbabque
  • 53,749
  • 8
  • 73
  • 136