MD5 implementation

Discussion in 'ASP .Net Security' started by Casper Hornstrup, Feb 4, 2004.

  1. I need to use MD5 to generate a hash of a string. It needs to be
    compatible with the MD5 implementation in PHP.

    public string ComputeMD5(string plain)
    {
    MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash));
    //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    }


    With PHP I get:
    md5('test1test1test1test1test1test1') = '0b2fc97f2a37500552d805c4727295a8'

    In .NET I get:
    ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    ComputeMD5('test1test1test1test1test1test1') = 'Cy/Jfyo3UAVS2AXEcnKVqA=='
    (base64)

    Am I doing it wrong or are the PHP and .NET implementations not compatible?

    Thanks in advance,
    Casper Hornstrup
     
    Casper Hornstrup, Feb 4, 2004
    #1
    1. Advertising

  2. Hi,

    If you compare both hashes in the same encoding (the PHP version is in
    Hexadecimal and the .NET version is in base64 or binary.), they will match.

    --
    Hernan de Lahitte
    Lagash Systems S.A.
    http://www.lagash.com



    "Casper Hornstrup" <> wrote in message
    news:OZ$...
    > I need to use MD5 to generate a hash of a string. It needs to be
    > compatible with the MD5 implementation in PHP.
    >
    > public string ComputeMD5(string plain)
    > {
    > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash));
    > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    > }
    >
    >
    > With PHP I get:
    > md5('test1test1test1test1test1test1') = '0b2fc97f2a37500552d805c4727295a8'
    >
    > In .NET I get:
    > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    > ComputeMD5('test1test1test1test1test1test1') = 'Cy/Jfyo3UAVS2AXEcnKVqA=='
    > (base64)
    >
    > Am I doing it wrong or are the PHP and .NET implementations not

    compatible?
    >
    > Thanks in advance,
    > Casper Hornstrup
    >
    >
    >
     
    Hernan de Lahitte, Feb 4, 2004
    #2
    1. Advertising

  3. Thanks for you answer.
    How do I get a string in the .NET version that I can compare to the PHP
    generated string and have a match?

    Casper

    "Hernan de Lahitte" <> wrote in message
    news:...
    > Hi,
    >
    > If you compare both hashes in the same encoding (the PHP version is in
    > Hexadecimal and the .NET version is in base64 or binary.), they will

    match.
    >
    > --
    > Hernan de Lahitte
    > Lagash Systems S.A.
    > http://www.lagash.com
    >
    >
    >
    > "Casper Hornstrup" <> wrote in message
    > news:OZ$...
    > > I need to use MD5 to generate a hash of a string. It needs to be
    > > compatible with the MD5 implementation in PHP.
    > >
    > > public string ComputeMD5(string plain)
    > > {
    > > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    > > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    > > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash));
    > > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    > > }
    > >
    > >
    > > With PHP I get:
    > > md5('test1test1test1test1test1test1') =

    '0b2fc97f2a37500552d805c4727295a8'
    > >
    > > In .NET I get:
    > > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    > > ComputeMD5('test1test1test1test1test1test1') =

    'Cy/Jfyo3UAVS2AXEcnKVqA=='
    > > (base64)
    > >
    > > Am I doing it wrong or are the PHP and .NET implementations not

    > compatible?
    > >
    > > Thanks in advance,
    > > Casper Hornstrup
    > >
    > >
    > >

    >
    >
     
    Casper Hornstrup, Feb 4, 2004
    #3
  4. This code will encode in Hexa your hashed byte array.

    public static string EncodeHexString(byte[] sArray)
    {
    StringBuilder sb = new StringBuilder( sArray.Length * 2 );
    for(int index = 0; index < sArray.Length; index++)
    {
    sb.AppendFormat( "{0:X2}", sArray[index] );
    }
    return sb.ToString();
    }

    --
    Hernan de Lahitte
    Lagash Systems S.A.
    http://www.lagash.com



    "Casper Hornstrup" <> wrote in message
    news:#...
    > Thanks for you answer.
    > How do I get a string in the .NET version that I can compare to the PHP
    > generated string and have a match?
    >
    > Casper
    >
    > "Hernan de Lahitte" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > If you compare both hashes in the same encoding (the PHP version is in
    > > Hexadecimal and the .NET version is in base64 or binary.), they will

    > match.
    > >
    > > --
    > > Hernan de Lahitte
    > > Lagash Systems S.A.
    > > http://www.lagash.com
    > >
    > >
    > >
    > > "Casper Hornstrup" <> wrote in message
    > > news:OZ$...
    > > > I need to use MD5 to generate a hash of a string. It needs to be
    > > > compatible with the MD5 implementation in PHP.
    > > >
    > > > public string ComputeMD5(string plain)
    > > > {
    > > > MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
    > > > byte[] bMD5Hash = Encoding.ASCII.GetBytes(plain);
    > > > return Encoding.ASCII.GetString(md5.ComputeHash(bMD5Hash));
    > > > //return Convert.ToBase64String(md5.ComputeHash(bMD5Hash));
    > > > }
    > > >
    > > >
    > > > With PHP I get:
    > > > md5('test1test1test1test1test1test1') =

    > '0b2fc97f2a37500552d805c4727295a8'
    > > >
    > > > In .NET I get:
    > > > ComputeMD5('test1test1test1test1test1test1') = ' /I*7PRXDrr('
    > > > ComputeMD5('test1test1test1test1test1test1') =

    > 'Cy/Jfyo3UAVS2AXEcnKVqA=='
    > > > (base64)
    > > >
    > > > Am I doing it wrong or are the PHP and .NET implementations not

    > > compatible?
    > > >
    > > > Thanks in advance,
    > > > Casper Hornstrup
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Hernan de Lahitte, Feb 4, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aerodyne
    Replies:
    2
    Views:
    393
    Roedy Green
    Jun 30, 2005
  2. Replies:
    9
    Views:
    17,106
    John Salerno
    May 8, 2006
  3. Michael Tsang
    Replies:
    32
    Views:
    1,122
    Richard Bos
    Mar 1, 2010
  4. Michael Tsang
    Replies:
    54
    Views:
    1,203
    Phil Carmody
    Mar 30, 2010
  5. Peter Woodsky

    create a md5 / md5 passwd with a salt

    Peter Woodsky, Nov 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    217
    Brian Candler
    Nov 21, 2008
Loading...

Share This Page