Search code examples
c#bashmd5sum

Difference between md5sum created by c# and bash


I'm trying understand why when i use a linux md5sum function, like this:

#!/bin/bash
p="4c4712a4141d261ec0ca8f90379"
Etime="123456"
TOTP=$(echo $Etime$p|md5sum|cut -f1 -d' ')
echo $TOTP

I've got: ce007ddfb5eb0ccda6d4a6ddd631c563

But when i trying generate md5sum in c# using this code:

        public static string Hash()
        {
            string p = "4c4712a4141d261ec0ca8f90379";
            string Etime = "123456";
            string h = Etime + p ;
            string r = md5test(h);
            return r;
        }

        public static string md5test(string testString)
        {
            byte[] asciiBytes = ASCIIEncoding.UTF8.GetBytes(testString);
            byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
            string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
            return hashedString;
        }

I've got different value: acb6242ab9ad7a969fb27f10644a0283

I think the problem is in conversion bytes to string, but i'm not sure. Does anyone could explain where i make mistake or how to do to have expected value:ce007ddfb5eb0ccda6d4a6ddd631c563

Thanks Wojtek


Solution

  • The problem is the newline at the end of the output of echo. Use echo -n or printf instead:

    TOTP=$(printf %s "$Etime$p" | md5sum | cut -f1 -d' ')